diff --git a/php-5.6.40-openssl3-pkey-export.patch b/php-5.6.40-openssl3-pkey-export.patch new file mode 100644 index 0000000..60990aa --- /dev/null +++ b/php-5.6.40-openssl3-pkey-export.patch @@ -0,0 +1,22 @@ +diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c +index af376aa..5116a4a 100644 +--- a/ext/openssl/openssl.c ++++ b/ext/openssl/openssl.c +@@ -4001,6 +4001,9 @@ PHP_FUNCTION(openssl_pkey_export_to_file) + + if (PHP_SSL_REQ_PARSE(&req, args) == SUCCESS) { + bio_out = BIO_new_file(filename, "w"); ++ if (bio_out == NULL) { ++ goto clean_exit; ++ } + + if (passphrase && req.priv_key_encrypt) { + if (req.priv_key_encrypt_cipher) { +@@ -4029,6 +4032,7 @@ PHP_FUNCTION(openssl_pkey_export_to_file) + RETVAL_TRUE; + } + } ++clean_exit: + PHP_SSL_REQ_DISPOSE(&req); + + if (key_resource == -1 && key) { diff --git a/php-5.6.40-round-places-overflow.patch b/php-5.6.40-round-places-overflow.patch new file mode 100644 index 0000000..cabd3a0 --- /dev/null +++ b/php-5.6.40-round-places-overflow.patch @@ -0,0 +1,20 @@ +--- a/ext/standard/math.c ++++ b/ext/standard/math.c +@@ -141,7 +141,7 @@ + double tmp_value; + int precision_places; + +- if (!php_math_is_finite(value)) { ++ if (!php_math_is_finite(value) || value == 0.0) { + return value; + } + +@@ -153,7 +153,7 @@ + /* If the decimal precision guaranteed by FP arithmetic is higher than + the requested places BUT is small enough to make sure a non-zero value + is returned, pre-round the result to the precision */ +- if (precision_places > places && precision_places - places < 15) { ++ if (precision_places > places && precision_places - 15 < places) { + int64_t use_precision = precision_places < INT_MIN+1 ? INT_MIN+1 : precision_places; + + f2 = php_intpow10(abs((int)use_precision)); diff --git a/php-5.6.40-sury-105.patch b/php-5.6.40-sury-105.patch new file mode 100644 index 0000000..0fdbfbb --- /dev/null +++ b/php-5.6.40-sury-105.patch @@ -0,0 +1,221268 @@ +diff --git a/NEWS b/NEWS +index 6e40f32..7225078 100644 +--- a/NEWS ++++ b/NEWS +@@ -1,5 +1,336 @@ + PHP NEWS + ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ++ ++Backported from 8.2.32 ++ ++- OpenSSL: ++ . Fixed bug GH-22187 (Memory corruption (zend_mm_heap corrupted) in ++ openssl_encrypt with AES-WRAP-PAD). (David Carlier) ++ ++Backported from 8.2.31 ++ ++- FPM: ++ . Fixed GHSA-7qg2-v9fj-4mwv (XSS within status endpoint). (CVE-2026-6735) ++ (Jakub Zelenka) ++ ++- SOAP: ++ . Fixed GHSA-m33r-qmcv-p97q (Use-after-free after header parsing failure with ++ SOAP_PERSISTENCE_SESSION). (CVE-2026-7261) (ilutov) ++ . Fixed GHSA-hmxp-6pc4-f3vv (Broken Apache map value NULL check). ++ (CVE-2026-7262) (ilutov) ++ ++Backported from 8.1.31 ++ ++- CLI: ++ . Fixed bug GHSA-4w77-75f9-2c8w (Heap-Use-After-Free in sapi_read_post_data ++ Processing in CLI SAPI Interface). (nielsdos) ++ ++- LDAP: ++ . Fixed bug GHSA-g665-fm4p-vhff (OOB access in ldap_escape). (CVE-2024-8932) ++ (nielsdos) ++ ++- Streams: ++ . Fixed bug GHSA-c5f2-jwm7-mmq2 (Configuring a proxy in a stream context ++ might allow for CRLF injection in URIs). (CVE-2024-11234) (Jakub Zelenka) ++ . Fixed bug GHSA-r977-prxv-hc43 (Single byte overread with ++ convert.quoted-printable-decode filter). (CVE-2024-11233) (nielsdos) ++ ++Backported from 8.1.30 ++ ++- CGI: ++ . Fixed bug GHSA-p99j-rfp4-xqvq (Bypass of CVE-2024-4577, Parameter Injection ++ Vulnerability). (CVE-2024-8926) (nielsdos) ++ . Fixed bug GHSA-94p6-54jq-9mwp (cgi.force_redirect configuration is ++ bypassable due to the environment variable collision). (CVE-2024-8927) ++ (nielsdos) ++ ++- SAPI: ++ . Fixed bug GHSA-9pqp-7h25-4f32 (Erroneous parsing of multipart form data). ++ (CVE-2024-8925) (Arnaud) ++ ++Backported from 8.1.29 ++ ++- CGI: ++ . Fixed bug GHSA-3qgc-jrrr-25jv (Bypass of CVE-2012-1823, Argument Injection ++ in PHP-CGI). (CVE-2024-4577) (nielsdos) ++ ++- Filter: ++ . Fixed bug GHSA-w8qr-v226-r27w (Filter bypass in filter_var FILTER_VALIDATE_URL). ++ (CVE-2024-5458) (nielsdos) ++ ++Backported from 8.1.28 ++ ++- Standard: ++ . Fixed bug GHSA-wpj3-hf5j-x4v4 (__Host-/__Secure- cookie bypass due to ++ partial CVE-2022-31629 fix). (CVE-2024-2756) (nielsdos) ++ . Fixed bug GHSA-h746-cjrr-wfmr (password_verify can erroneously return true, ++ opening ATO risk). (CVE-2024-3096) (Jakub Zelenka) ++ ++Backported from 8.0.30 ++ ++- Libxml: ++ . Fixed bug GHSA-3qrf-m4j2-pcrr (Security issue with external entity loading ++ in XML without enabling it). (CVE-2023-3823) (nielsdos, ilutov) ++ ++- Phar: ++ . Fixed bug GHSA-jqcx-ccgc-xwhv (Buffer mismanagement in phar_dir_read()). ++ (CVE-2023-3824) (nielsdos) ++ ++Backported from 8.0.29 ++ ++- Soap: ++ . Fixed bug GHSA-76gg-c692-v2mw (Missing error check and insufficient random ++ bytes in HTTP Digest authentication for SOAP). ++ (CVE-2023-3247) (nielsdos, timwolla) ++ ++Backported from 8.0.28 ++ ++- Core: ++ . Fixed bug #81744 (Password_verify() always return true with some hash). ++ (CVE-2023-0567). (Tim Düsterhus) ++ . Fixed bug #81746 (1-byte array overrun in common path resolve code). ++ (CVE-2023-0568). (Niels Dossche) ++ . Fixed bug GHSA-54hq-v5wp-fqgv (DOS vulnerability when parsing multipart ++ request body). (CVE-2023-0662) (Jakub Zelenka) ++ ++Backported from 8.0.27 ++ ++- PDO/SQLite: ++ . Fixed bug #81740 (PDO::quote() may return unquoted string). ++ (CVE-2022-31631) (cmb) ++ ++Backported from 7.4.32 ++ ++- Core: ++ . Fixed bug #81727: Don't mangle HTTP variable names that clash with ones ++ that have a specific semantic meaning. (CVE-2022-31629). (Derick) ++ ++- Phar: ++ . Fixed bug #81726: phar wrapper: DOS when using quine gzip file. ++ (CVE-2022-31628). (cmb) ++ ++Backported from 7.4.30 ++ ++- mysqlnd: ++ . Fixed bug #81719: mysqlnd/pdo password buffer overflow. ++ (CVE-2022-31626) (c dot fol at ambionics dot io) ++ ++- pgsql ++ . Fixed bug #81720: Uninitialized array in pg_query_params(). ++ (CVE-2022-31625) (cmb) ++ ++Backported from 7.3.33 ++ ++- XML: ++ . Fix #79971: special character is breaking the path in xml function. ++ (CVE-2021-21707) (cmb) ++ ++Backported from 7.3.32 ++ ++- FPM: ++ . Fixed bug #81026 (PHP-FPM oob R/W in root process leading to privilege ++ escalation) (CVE-2021-21703). (Jakub Zelenka) ++ ++Backported from 7.3.31 ++ ++- Zip: ++ . Fixed bug #81420 (ZipArchive::extractTo extracts outside of destination). ++ (CVE-2021-21706) (cmb) ++ ++Backported from 7.3.30 ++ ++- Phar: ++ . Fixed bug #81211: Symlinks are followed when creating PHAR archive (cmb) ++ ++Backported from 7.3.29 ++ ++- Core: ++ . Fixed #81122: SSRF bypass in FILTER_VALIDATE_URL. (CVE-2021-21705) (cmb) ++ ++- PDO_Firebird: ++ . Fixed #76448: Stack buffer overflow in firebird_info_cb. (CVE-2021-21704) ++ (cmb) ++ . Fixed #76449: SIGSEGV in firebird_handle_doer. (CVE-2021-21704) (cmb) ++ . Fixed #76450: SIGSEGV in firebird_stmt_execute. (CVE-2021-21704) (cmb) ++ . Fixed #76452: Crash while parsing blob data in firebird_fetch_blob. ++ (CVE-2021-21704) (cmb) ++ ++Backported from 7.3.28 ++ ++- Imap: ++ . Fixed bug #80710 (imap_mail_compose() header injection). (cmb, Stas) ++ ++Backported from 7.3.27 ++ ++- SOAP: ++ . Fixed bug #80672 (Null Dereference in SoapClient). (CVE-2021-21702) (cmb, Stas) ++ ++Backported from 7.3.26 ++ ++- Standard: ++ . Fixed bug #77423 (FILTER_VALIDATE_URL accepts URLs with invalid userinfo). ++ (CVE-2020-7071) (cmb) ++ ++Backported from 7.2.34 ++ ++- Core: ++ . Fixed bug #79699 (PHP parses encoded cookie names so malicious `__Host-` ++ cookies can be sent). (CVE-2020-7070) (Stas) ++ ++Backported from 7.2.33 ++ ++- Core: ++ . Fixed bug #79877 (getimagesize function silently truncates after a null ++ byte) (cmb) ++ ++- Phar: ++ . Fixed bug #79797 (Use of freed hash key in the phar_parse_zipfile ++ function). (CVE-2020-7068) (cmb) ++ ++Backported from 7.2.31 ++ ++- Core: ++ . Fixed bug #78875 (Long filenames cause OOM and temp files are not cleaned). ++ (CVE-2019-11048) (cmb) ++ . Fixed bug #78876 (Long variables in multipart/form-data cause OOM and temp ++ files are not cleaned). (CVE-2019-11048) (cmb) ++ ++Backported from 7.2.30 ++ ++- Standard: ++ . Fixed bug #79330 (shell_exec silently truncates after a null byte). (stas) ++ . Fixed bug #79465 (OOB Read in urldecode). (CVE-2020-7067) (stas) ++ ++Backported from 7.2.29 ++ ++- Core: ++ . Fixed bug #79329 (get_headers() silently truncates after a null byte) ++ (CVE-2020-7066) (cmb) ++ ++- EXIF: ++ . Fixed bug #79282 (Use-of-uninitialized-value in exif) (CVE-2020-7064) ++ (Nikita) ++ ++Backported from 7.2.28 ++ ++- Phar: ++ . Fixed bug #79082 (Files added to tar with Phar::buildFromIterator have ++ all-access permissions). (CVE-2020-7063) (stas) ++ ++- Session: ++ . Fixed bug #79221 (Null Pointer Dereference in PHP Session Upload Progress). ++ (CVE-2020-7062) (stas) ++ ++Backported from 7.2.27 ++ ++- Mbstring: ++ . Fixed bug #79037 (global buffer-overflow in `mbfl_filt_conv_big5_wchar`). ++ (CVE-2020-7060) (Nikita) ++ ++- Standard: ++ . Fixed bug #79099 (OOB read in php_strip_tags_ex). (CVE-2020-7059). (cmb) ++ ++Backported from 7.2.26 ++ ++- Bcmath: ++ . Fixed bug #78878 (Buffer underflow in bc_shift_addsub). (CVE-2019-11046). ++ (cmb) ++ ++- Core: ++ . Fixed bug #78862 (link() silently truncates after a null byte on Windows). ++ (CVE-2019-11044). (cmb) ++ . Fixed bug #78863 (DirectoryIterator class silently truncates after a null ++ byte). (CVE-2019-11045). (cmb) ++ ++- EXIF: ++ . Fixed bug #78793 (Use-after-free in exif parsing under memory sanitizer). ++ (CVE-2019-11050). (Nikita) ++ . Fixed bug #78910 (Heap-buffer-overflow READ in exif). (CVE-2019-11047). ++ (Nikita) ++ ++Backported from 7.1.33 ++ ++- FPM: ++ . Fixed bug #78599 (env_path_info underflow in fpm_main.c can lead to RCE). ++ (CVE-2019-11043) (Jakub Zelenka) ++ ++Backported from 7.1.32 ++ ++- mbstring: ++ . Fixed CVE-2019-13224 (don't allow different encodings for onig_new_deluxe) (stas) ++ ++- pcre: ++ . Fixed bug #75457 (heap use-after-free in pcrelib) (cmb) ++ ++Backported from 7.1.31 ++ ++- EXIF: ++ . Fixed bug #78256 (heap-buffer-overflow on exif_process_user_comment). ++ (CVE-2019-11042) (Stas) ++ . Fixed bug #78222 (heap-buffer-overflow on exif_scan_thumbnail). ++ (CVE-2019-11041) (Stas) ++ ++- Phar: ++ . Fixed bug #77919 (Potential UAF in Phar RSHUTDOWN). (cmb) ++ ++- SQLite: ++ . Upgraded to SQLite 3.28.0. (cmb) ++ ++Backported from 7.1.30 ++ ++ ++- EXIF: ++ . Fixed bug #77988 (heap-buffer-overflow on php_jpg_get16). ++ (CVE-2019-11040) (Stas) ++ ++- GD: ++ . Fixed bug #77973 (Uninitialized read in gdImageCreateFromXbm). ++ (CVE-2019-11038) (cmb) ++ ++- Iconv: ++ . Fixed bug #78069 (Out-of-bounds read in iconv.c:_php_iconv_mime_decode() ++ due to integer overflow). (CVE-2019-11039). (maris dot adam) ++ ++- SQLite: ++ . Fixed bug #77967 (Bypassing open_basedir restrictions via file uris). (Stas) ++ ++Backported from 7.1.29 ++ ++- EXIF ++ . Fixed bug #77950 (Heap-buffer-overflow in _estrndup via exif_process_IFD_TAG). ++ (CVE-2019-11036) (Stas) ++ ++Backported from 7.1.28 ++ ++- EXIF: ++ . Fixed bug #77753 (Heap-buffer-overflow in php_ifd_get32s). (CVE-2019-11034) ++ (Stas) ++ . Fixed bug #77831 (Heap-buffer-overflow in exif_iif_add_value). ++ (CVE-2019-11035) (Stas) ++ ++- SQLite3: ++ . Added sqlite3.defensive INI directive. (BohwaZ) ++ ++Backported from 7.1.27 ++ ++- Core: ++ . Fixed bug #77630 (rename() across the device may allow unwanted access during ++ processing). (Stas) ++ ++- EXIF: ++ . Fixed bug #77509 (Uninitialized read in exif_process_IFD_in_TIFF). (Stas) ++ . Fixed bug #77540 (Invalid Read on exif_process_SOFn). (Stas) ++ . Fixed bug #77563 (Uninitialized read in exif_process_IFD_in_MAKERNOTE). (Stas) ++ . Fixed bug #77659 (Uninitialized read in exif_process_IFD_in_MAKERNOTE). (Stas) ++ ++- PHAR: ++ . Fixed bug #77396 (Null Pointer Dereference in phar_create_or_parse_filename). ++ (bishop) ++ . Fixed bug #77586 (phar_tar_writeheaders_int() buffer overflow). (bishop) ++ ++- SPL: ++ . Fixed bug #77431 (openFile() silently truncates after a null byte). (cmb) ++ + 10 Jan 2019, PHP 5.6.40 + + - GD: +diff --git a/Zend/zend_API.c b/Zend/zend_API.c +index 0757cc9..89cdca5 100644 +--- a/Zend/zend_API.c ++++ b/Zend/zend_API.c +@@ -1016,6 +1016,15 @@ ZEND_API int _array_init(zval *arg, uint size ZEND_FILE_LINE_DC) /* {{{ */ + } + /* }}} */ + ++ZEND_API int _array_init_singleton(zval *arg ZEND_FILE_LINE_DC) /* {{{ */ ++{ ++ _array_init(arg, 0 ZEND_FILE_LINE_RELAY_CC); ++ /* nTableMask == 0 marks an uninitialized table, so 2 is the smallest usable size */ ++ Z_ARRVAL_P(arg)->nTableSize = 2; ++ return SUCCESS; ++} ++/* }}} */ ++ + static int zend_merge_property(zval **value TSRMLS_DC, int num_args, va_list args, const zend_hash_key *hash_key) /* {{{ */ + { + /* which name should a numeric property have ? */ +diff --git a/Zend/zend_API.h b/Zend/zend_API.h +index 41aa65f..c17dacd 100644 +--- a/Zend/zend_API.h ++++ b/Zend/zend_API.h +@@ -363,10 +363,12 @@ ZEND_API char *zend_get_type_by_const(int type); + + #define array_init(arg) _array_init((arg), 0 ZEND_FILE_LINE_CC) + #define array_init_size(arg, size) _array_init((arg), (size) ZEND_FILE_LINE_CC) ++#define array_init_singleton(arg) _array_init_singleton((arg) ZEND_FILE_LINE_CC) + #define object_init(arg) _object_init((arg) ZEND_FILE_LINE_CC TSRMLS_CC) + #define object_init_ex(arg, ce) _object_init_ex((arg), (ce) ZEND_FILE_LINE_CC TSRMLS_CC) + #define object_and_properties_init(arg, ce, properties) _object_and_properties_init((arg), (ce), (properties) ZEND_FILE_LINE_CC TSRMLS_CC) + ZEND_API int _array_init(zval *arg, uint size ZEND_FILE_LINE_DC); ++ZEND_API int _array_init_singleton(zval *arg ZEND_FILE_LINE_DC); + ZEND_API int _object_init(zval *arg ZEND_FILE_LINE_DC TSRMLS_DC); + ZEND_API int _object_init_ex(zval *arg, zend_class_entry *ce ZEND_FILE_LINE_DC TSRMLS_DC); + ZEND_API int _object_and_properties_init(zval *arg, zend_class_entry *ce, HashTable *properties ZEND_FILE_LINE_DC TSRMLS_DC); +diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c +index c7babe1..f84d25a 100644 +--- a/Zend/zend_alloc.c ++++ b/Zend/zend_alloc.c +@@ -91,7 +91,7 @@ static void zend_mm_panic(const char *message) + #if ZEND_DEBUG && defined(HAVE_KILL) && defined(HAVE_GETPID) + kill(getpid(), SIGSEGV); + #endif +- exit(1); ++ _exit(1); + } + + /*******************/ +@@ -1740,6 +1740,42 @@ ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent + } + } + ++ZEND_API void zend_mm_safe_error_ex(const char *format, ...) ++{ ++ const char *error_filename; ++ uint error_lineno; ++ va_list args; ++ char buf[1024]; ++ TSRMLS_FETCH(); ++ ++ if (zend_is_compiling(TSRMLS_C)) { ++ error_filename = zend_get_compiled_filename(TSRMLS_C); ++ error_lineno = zend_get_compiled_lineno(TSRMLS_C); ++ } else if (EG(in_execution)) { ++ error_filename = EG(active_op_array)?EG(active_op_array)->filename:NULL; ++ error_lineno = EG(opline_ptr)?(*EG(opline_ptr))->lineno:0; ++ } else { ++ error_filename = NULL; ++ error_lineno = 0; ++ } ++ if (!error_filename) { ++ error_filename = "Unknown"; ++ } ++ ++ va_start(args, format); ++ vsnprintf(buf, sizeof(buf), format, args); ++ va_end(args); ++ ++ fprintf(stderr, "\nFatal error: %s in %s on line %d\n", buf, error_filename, error_lineno); ++ ++/* See http://support.microsoft.com/kb/190351 */ ++#ifdef PHP_WIN32 ++ fflush(stderr); ++#endif ++ ++ _exit(1); ++} ++ + static void zend_mm_safe_error(zend_mm_heap *heap, + const char *format, + size_t limit, +@@ -1749,59 +1785,15 @@ static void zend_mm_safe_error(zend_mm_heap *heap, + #endif + size_t size) + { +- if (heap->reserve) { +- _zend_mm_free_int(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC); +- heap->reserve = NULL; +- } +- if (heap->overflow == 0) { +- const char *error_filename; +- uint error_lineno; +- TSRMLS_FETCH(); +- if (zend_is_compiling(TSRMLS_C)) { +- error_filename = zend_get_compiled_filename(TSRMLS_C); +- error_lineno = zend_get_compiled_lineno(TSRMLS_C); +- } else if (EG(in_execution)) { +- error_filename = EG(active_op_array)?EG(active_op_array)->filename:NULL; +- error_lineno = EG(opline_ptr)?(*EG(opline_ptr))->lineno:0; +- } else { +- error_filename = NULL; +- error_lineno = 0; +- } +- if (!error_filename) { +- error_filename = "Unknown"; +- } +- heap->overflow = 1; +- zend_try { +- zend_error_noreturn(E_ERROR, +- format, +- limit, +-#if ZEND_DEBUG +- filename, +- lineno, +-#endif +- size); +- } zend_catch { +- if (heap->overflow == 2) { +- fprintf(stderr, "\nFatal error: "); +- fprintf(stderr, +- format, +- limit, ++ (void) heap; ++ ++ zend_mm_safe_error_ex(format, ++ limit, + #if ZEND_DEBUG +- filename, +- lineno, +-#endif +- size); +- fprintf(stderr, " in %s on line %d\n", error_filename, error_lineno); +- } +-/* See http://support.microsoft.com/kb/190351 */ +-#ifdef PHP_WIN32 +- fflush(stderr); ++ filename, ++ lineno, + #endif +- } zend_end_try(); +- } else { +- heap->overflow = 2; +- } +- zend_bailout(); ++ size); + } + + static zend_mm_free_block *zend_mm_search_large_block(zend_mm_heap *heap, size_t true_size) +@@ -2472,7 +2464,7 @@ static inline size_t safe_address(size_t nmemb, size_t size, size_t offset) + "rm"(offset)); + + if (UNEXPECTED(overflow)) { +- zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset); ++ zend_mm_safe_error_ex("Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset); + return 0; + } + return res; +@@ -2501,7 +2493,7 @@ static inline size_t safe_address(size_t nmemb, size_t size, size_t offset) + + #undef LP_SUFF + if (UNEXPECTED(overflow)) { +- zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset); ++ zend_mm_safe_error_ex("Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset); + return 0; + } + return res; +@@ -2522,7 +2514,7 @@ static inline size_t safe_address(size_t nmemb, size_t size, size_t offset) + "1"(0)); + + if (UNEXPECTED(overflow)) { +- zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset); ++ zend_mm_safe_error_ex("Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset); + return 0; + } + return res; +@@ -2542,7 +2534,7 @@ static inline size_t safe_address(size_t nmemb, size_t size, size_t offset) + "r"(offset)); + + if (UNEXPECTED(overflow)) { +- zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset); ++ zend_mm_safe_error_ex("Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset); + return 0; + } + return res; +@@ -2555,7 +2547,7 @@ static inline size_t safe_address(size_t nmemb, size_t size, size_t offset) + zend_ulong64 res = (zend_ulong64)nmemb * (zend_ulong64)size + (zend_ulong64)offset; + + if (UNEXPECTED(res > (zend_ulong64)0xFFFFFFFFL)) { +- zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset); ++ zend_mm_safe_error_ex("Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset); + return 0; + } + return (size_t) res; +@@ -2570,7 +2562,7 @@ static inline size_t safe_address(size_t nmemb, size_t size, size_t offset) + double _delta = (double)res - _d; + + if (UNEXPECTED((_d + _delta ) != _d)) { +- zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset); ++ zend_mm_safe_error_ex("Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset); + return 0; + } + return res; +@@ -2582,7 +2574,7 @@ ZEND_API void *_safe_emalloc_string(size_t nmemb, size_t size, size_t offset ZEN + { + size_t str_size = safe_address(nmemb, size, offset); + if (UNEXPECTED(str_size > INT_MAX)) { +- zend_error_noreturn(E_ERROR, "String allocation overflow, max size is %d", INT_MAX); ++ zend_mm_safe_error_ex("String allocation overflow, max size is %d", INT_MAX); + } + return emalloc_rel(str_size); + } +diff --git a/Zend/zend_alloc.h b/Zend/zend_alloc.h +index 719f9c5..dab0599 100644 +--- a/Zend/zend_alloc.h ++++ b/Zend/zend_alloc.h +@@ -57,6 +57,12 @@ ZEND_API char *zend_strndup(const char *s, unsigned int length) ZEND_ATTRIBUTE_M + ZEND_API void *_emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE(1); + ZEND_API void *_safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC; + ZEND_API void *_safe_emalloc_string(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC; ++ ++#if defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__) ++ZEND_API void zend_mm_safe_error_ex(const char *format, ...) __attribute__ ((noreturn)); ++#else ++ZEND_API void zend_mm_safe_error_ex(const char *format, ...); ++#endif + ZEND_API void *_safe_malloc(size_t nmemb, size_t size, size_t offset) ZEND_ATTRIBUTE_MALLOC; + ZEND_API void _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); + ZEND_API void *_ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE2(1,2); +diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c +index bf754d2..a7a7b9b 100644 +--- a/Zend/zend_execute_API.c ++++ b/Zend/zend_execute_API.c +@@ -307,6 +307,10 @@ void shutdown_executor(TSRMLS_D) /* {{{ */ + } + } zend_end_try(); + ++ /* may be used by zend_error before init_executor */ ++ EG(error_handling) = EH_NORMAL; ++ EG(exception) = NULL; ++ + zend_try { + clean_non_persistent_constants(TSRMLS_C); + } zend_end_try(); +diff --git a/Zend/zend_llist.c b/Zend/zend_llist.c +index 3d597a3..5be4940 100644 +--- a/Zend/zend_llist.c ++++ b/Zend/zend_llist.c +@@ -115,6 +115,8 @@ ZEND_API void zend_llist_destroy(zend_llist *l) + current = next; + } + ++ l->head = NULL; ++ l->tail = NULL; + l->count = 0; + } + +diff --git a/Zend/zend_multiply.h b/Zend/zend_multiply.h +index a4d48b1..850c108 100644 +--- a/Zend/zend_multiply.h ++++ b/Zend/zend_multiply.h +@@ -62,8 +62,8 @@ + __asm__("mul %0, %2, %3\n" \ + "smulh %1, %2, %3\n" \ + "sub %1, %1, %0, asr #63\n" \ +- : "=X"(__tmpvar), "=X"(usedval) \ +- : "X"(a), "X"(b)); \ ++ : "=&r"(__tmpvar), "=&r"(usedval) \ ++ : "r"(a), "r"(b)); \ + if (usedval) (dval) = (double) (a) * (double) (b); \ + else (lval) = __tmpvar; \ + } while (0) +diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c +index 450153f..192592b 100644 +--- a/Zend/zend_operators.c ++++ b/Zend/zend_operators.c +@@ -1400,7 +1400,7 @@ ZEND_API int add_char_to_string(zval *result, const zval *op1, const zval *op2) + char *buf; + + if (UNEXPECTED(length < 0)) { +- zend_error(E_ERROR, "String size overflow"); ++ zend_mm_safe_error_ex("String size overflow"); + } + + buf = str_erealloc(Z_STRVAL_P(op1), length + 1); +@@ -1419,7 +1419,7 @@ ZEND_API int add_string_to_string(zval *result, const zval *op1, const zval *op2 + char *buf; + + if (UNEXPECTED(length < 0)) { +- zend_error(E_ERROR, "String size overflow"); ++ zend_mm_safe_error_ex("String size overflow"); + } + + buf = str_erealloc(Z_STRVAL_P(op1), length + 1); +@@ -1465,7 +1465,7 @@ ZEND_API int concat_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{ + if (Z_STRLEN_P(result) < 0 || (int) (Z_STRLEN_P(op1) + Z_STRLEN_P(op2)) < 0) { + efree(Z_STRVAL_P(result)); + ZVAL_EMPTY_STRING(result); +- zend_error(E_ERROR, "String size overflow"); ++ zend_mm_safe_error_ex("String size overflow"); + } + + Z_STRVAL_P(result) = safe_erealloc(Z_STRVAL_P(result), res_len, 1, 1); +@@ -1478,7 +1478,7 @@ ZEND_API int concat_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{ + char *buf; + + if (Z_STRLEN_P(op1) < 0 || Z_STRLEN_P(op2) < 0 || (int) (Z_STRLEN_P(op1) + Z_STRLEN_P(op2)) < 0) { +- zend_error(E_ERROR, "String size overflow"); ++ zend_mm_safe_error_ex("String size overflow"); + } + buf = (char *) safe_emalloc(length, 1, 1); + +diff --git a/Zend/zend_strtod.c b/Zend/zend_strtod.c +index e74cf0e..092d1d2 100644 +--- a/Zend/zend_strtod.c ++++ b/Zend/zend_strtod.c +@@ -490,7 +490,7 @@ static Bigint * Balloc(int k) + rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long)); + if (!rv) { + _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa_mutex); +- zend_error(E_ERROR, "Balloc() failed to allocate memory"); ++ zend_mm_safe_error_ex("Balloc() failed to allocate memory"); + } + rv->k = k; + rv->maxwds = x; +diff --git a/ext/bcmath/libbcmath/src/str2num.c b/ext/bcmath/libbcmath/src/str2num.c +index c484c15..a5e7850 100644 +--- a/ext/bcmath/libbcmath/src/str2num.c ++++ b/ext/bcmath/libbcmath/src/str2num.c +@@ -57,9 +57,9 @@ bc_str2num (bc_num *num, char *str, int scale TSRMLS_DC) + zero_int = FALSE; + if ( (*ptr == '+') || (*ptr == '-')) ptr++; /* Sign */ + while (*ptr == '0') ptr++; /* Skip leading zeros. */ +- while (isdigit((int)*ptr)) ptr++, digits++; /* digits */ ++ while (*ptr >= '0' && *ptr <= '9') ptr++, digits++; /* digits */ + if (*ptr == '.') ptr++; /* decimal point */ +- while (isdigit((int)*ptr)) ptr++, strscale++; /* digits */ ++ while (*ptr >= '0' && *ptr <= '9') ptr++, strscale++; /* digits */ + if ((*ptr != '\0') || (digits+strscale == 0)) + { + *num = bc_copy_num (BCG(_zero_)); +diff --git a/ext/bcmath/tests/bug78878.phpt b/ext/bcmath/tests/bug78878.phpt +new file mode 100644 +index 0000000..3337270 +--- /dev/null ++++ b/ext/bcmath/tests/bug78878.phpt +@@ -0,0 +1,12 @@ ++--TEST-- ++Bug #78878 (Buffer underflow in bc_shift_addsub) ++--SKIPIF-- ++ ++--FILE-- ++ ++--EXPECT-- ++0bc math warning: non-zero scale in modulus +diff --git a/ext/dom/document.c b/ext/dom/document.c +index 1970c38..6f4ae26 100644 +--- a/ext/dom/document.c ++++ b/ext/dom/document.c +@@ -1498,7 +1498,7 @@ static xmlDocPtr dom_document_parser(zval *id, int mode, char *source, int sourc + int validate, recover, resolve_externals, keep_blanks, substitute_ent; + int resolved_path_len; + int old_error_reporting = 0; +- char *directory=NULL, resolved_path[MAXPATHLEN]; ++ char *directory=NULL, resolved_path[MAXPATHLEN + 1]; + + if (id != NULL) { + intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC); +@@ -1577,6 +1577,7 @@ static xmlDocPtr dom_document_parser(zval *id, int mode, char *source, int sourc + options |= XML_PARSE_NOBLANKS; + } + ++ php_libxml_sanitize_parse_ctxt_options(ctxt); + xmlCtxtUseOptions(ctxt, options); + + ctxt->recovery = recover; +@@ -1859,7 +1860,9 @@ PHP_FUNCTION(dom_document_xinclude) + + DOM_GET_OBJ(docp, id, xmlDocPtr, intern); + ++ PHP_LIBXML_SANITIZE_GLOBALS(xinclude); + err = xmlXIncludeProcessFlags(docp, flags); ++ PHP_LIBXML_RESTORE_GLOBALS(xinclude); + + /* XML_XINCLUDE_START and XML_XINCLUDE_END nodes need to be removed as these + are added via xmlXIncludeProcess to mark beginning and ending of xincluded document +@@ -1898,6 +1901,7 @@ PHP_FUNCTION(dom_document_validate) + + DOM_GET_OBJ(docp, id, xmlDocPtr, intern); + ++ PHP_LIBXML_SANITIZE_GLOBALS(validate); + cvp = xmlNewValidCtxt(); + + cvp->userData = NULL; +@@ -1909,6 +1913,7 @@ PHP_FUNCTION(dom_document_validate) + } else { + RETVAL_FALSE; + } ++ PHP_LIBXML_RESTORE_GLOBALS(validate); + + xmlFreeValidCtxt(cvp); + +@@ -1941,14 +1946,18 @@ static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type + + DOM_GET_OBJ(docp, id, xmlDocPtr, intern); + ++ PHP_LIBXML_SANITIZE_GLOBALS(new_parser_ctxt); ++ + switch (type) { + case DOM_LOAD_FILE: + if (CHECK_NULL_PATH(source, source_len)) { ++ PHP_LIBXML_RESTORE_GLOBALS(new_parser_ctxt); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Schema file source"); + RETURN_FALSE; + } + valid_file = _dom_get_valid_file_path(source, resolved_path, MAXPATHLEN TSRMLS_CC); + if (!valid_file) { ++ PHP_LIBXML_RESTORE_GLOBALS(new_parser_ctxt); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Schema file source"); + RETURN_FALSE; + } +@@ -1969,6 +1978,7 @@ static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type + parser); + sptr = xmlSchemaParse(parser); + xmlSchemaFreeParserCtxt(parser); ++ PHP_LIBXML_RESTORE_GLOBALS(new_parser_ctxt); + if (!sptr) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Schema"); + RETURN_FALSE; +@@ -1989,11 +1999,13 @@ static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type + } + #endif + ++ PHP_LIBXML_SANITIZE_GLOBALS(validate); + xmlSchemaSetValidOptions(vptr, valid_opts); + xmlSchemaSetValidErrors(vptr, php_libxml_error_handler, php_libxml_error_handler, vptr); + is_valid = xmlSchemaValidateDoc(vptr, docp); + xmlSchemaFree(sptr); + xmlSchemaFreeValidCtxt(vptr); ++ PHP_LIBXML_RESTORE_GLOBALS(validate); + + if (is_valid == 0) { + RETURN_TRUE; +@@ -2063,12 +2075,14 @@ static void _dom_document_relaxNG_validate(INTERNAL_FUNCTION_PARAMETERS, int typ + return; + } + ++ PHP_LIBXML_SANITIZE_GLOBALS(parse); + xmlRelaxNGSetParserErrors(parser, + (xmlRelaxNGValidityErrorFunc) php_libxml_error_handler, + (xmlRelaxNGValidityWarningFunc) php_libxml_error_handler, + parser); + sptr = xmlRelaxNGParse(parser); + xmlRelaxNGFreeParserCtxt(parser); ++ PHP_LIBXML_RESTORE_GLOBALS(parse); + if (!sptr) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid RelaxNG"); + RETURN_FALSE; +@@ -2151,6 +2165,7 @@ static void dom_load_html(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */ + RETURN_FALSE; + } + ++ php_libxml_sanitize_parse_ctxt_options(ctxt); + if (options) { + htmlCtxtUseOptions(ctxt, options); + } +diff --git a/ext/dom/documentfragment.c b/ext/dom/documentfragment.c +index 5ce1e39..3ecc71d 100644 +--- a/ext/dom/documentfragment.c ++++ b/ext/dom/documentfragment.c +@@ -140,7 +140,9 @@ PHP_METHOD(domdocumentfragment, appendXML) { + } + + if (data) { ++ PHP_LIBXML_SANITIZE_GLOBALS(parse); + err = xmlParseBalancedChunkMemory(nodep->doc, NULL, NULL, 0, data, &lst); ++ PHP_LIBXML_RESTORE_GLOBALS(parse); + if (err != 0) { + RETURN_FALSE; + } +diff --git a/ext/dom/domimplementation.c b/ext/dom/domimplementation.c +index d79430b..5f2b4e6 100644 +--- a/ext/dom/domimplementation.c ++++ b/ext/dom/domimplementation.c +@@ -111,6 +111,11 @@ PHP_METHOD(domimplementation, createDocumentType) + if (systemid_len > 0) + pch2 = systemid; + ++ if (strstr(name, "%00")) { ++ php_error_docref(NULL TSRMLS_CC, E_WARNING, "URI must not contain percent-encoded NUL bytes"); ++ RETURN_FALSE; ++ } ++ + uri = xmlParseURI(name); + if (uri != NULL && uri->opaque != NULL) { + localname = xmlStrdup(uri->opaque); +diff --git a/ext/dom/tests/bug79971_2.phpt b/ext/dom/tests/bug79971_2.phpt +new file mode 100644 +index 0000000..01cd123 +--- /dev/null ++++ b/ext/dom/tests/bug79971_2.phpt +@@ -0,0 +1,20 @@ ++--TEST-- ++Bug #79971 (special character is breaking the path in xml function) ++--SKIPIF-- ++ ++--FILE-- ++createDocumentType("$uri%00foo")); ++?> ++--EXPECTF-- ++Warning: DOMImplementation::createDocumentType(): URI must not contain percent-encoded NUL bytes in %s on line %d ++bool(false) +diff --git a/ext/dom/tests/dom005.phpt b/ext/dom/tests/dom005.phpt +index 715aec4..d7e828c 100644 +--- a/ext/dom/tests/dom005.phpt ++++ b/ext/dom/tests/dom005.phpt +@@ -5,7 +5,7 @@ Test 5: HTML Test + --FILE-- + loadHTMLFile(dirname(__FILE__)."/test.html"); ++$dom->loadHTMLFile(dirname(__FILE__)."/test.html", LIBXML_NOBLANKS); + print "--- save as XML\n"; + + print adjustDoctype($dom->saveXML()); +@@ -16,7 +16,6 @@ print adjustDoctype($dom->saveHTML()); + function adjustDoctype($xml) { + return str_replace(array("DOCTYPE HTML",'
','
'),array("DOCTYPE html",'',''),$xml); + } +- + --EXPECT-- + --- save as XML + +diff --git a/ext/exif/exif.c b/ext/exif/exif.c +index cad29b7..bf2fd61 100644 +--- a/ext/exif/exif.c ++++ b/ext/exif/exif.c +@@ -1660,10 +1660,10 @@ static int exif_file_sections_free(image_info_type *ImageInfo) + /* {{{ exif_iif_add_value + Add a value to image_info + */ +-static void exif_iif_add_value(image_info_type *image_info, int section_index, char *name, int tag, int format, int length, void* value, int motorola_intel TSRMLS_DC) ++static void exif_iif_add_value(image_info_type *image_info, int section_index, char *name, int tag, int format, int length, void* value, size_t value_len, int motorola_intel TSRMLS_DC) + { + size_t idex; +- void *vptr; ++ void *vptr, *vptr_end; + image_info_value *info_value; + image_info_data *info_data; + image_info_data *list; +@@ -1685,8 +1685,12 @@ static void exif_iif_add_value(image_info_type *image_info, int section_index, c + + switch (format) { + case TAG_FMT_STRING: ++ if (length > value_len) { ++ exif_error_docref("exif_iif_add_value" EXIFERR_CC, image_info, E_WARNING, "length > value_len: %d > %zu", length, value_len); ++ value = NULL; ++ } + if (value) { +- length = php_strnlen(value, length); ++ length = (int)php_strnlen(value, length); + info_value->s = estrndup(value, length); + info_data->length = length; + } else { +@@ -1708,6 +1712,10 @@ static void exif_iif_add_value(image_info_type *image_info, int section_index, c + if (!length) + break; + case TAG_FMT_UNDEFINED: ++ if (length > value_len) { ++ exif_error_docref("exif_iif_add_value" EXIFERR_CC, image_info, E_WARNING, "length > value_len: %d > %zu", length, value_len); ++ value = NULL; ++ } + if (value) { + if (tag == TAG_MAKER_NOTE) { + length = (int) php_strnlen(value, length); +@@ -1738,7 +1746,12 @@ static void exif_iif_add_value(image_info_type *image_info, int section_index, c + } else { + info_value = &info_data->value; + } ++ vptr_end = (char *) value + value_len; + for (idex=0,vptr=value; idex<(size_t)length; idex++,vptr=(char *) vptr + php_tiff_bytes_per_format[format]) { ++ if ((char *) vptr_end - (char *) vptr < php_tiff_bytes_per_format[format]) { ++ exif_error_docref("exif_iif_add_value" EXIFERR_CC, image_info, E_WARNING, "Value too short"); ++ break; ++ } + if (length>1) { + info_value = &info_data->value.list[idex]; + } +@@ -1774,7 +1787,7 @@ static void exif_iif_add_value(image_info_type *image_info, int section_index, c + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Found value of type single"); + #endif + info_value->f = *(float *)value; +- ++ break; + case TAG_FMT_DOUBLE: + #ifdef EXIF_DEBUG + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Found value of type double"); +@@ -1792,9 +1805,9 @@ static void exif_iif_add_value(image_info_type *image_info, int section_index, c + /* {{{ exif_iif_add_tag + Add a tag from IFD to image_info + */ +-static void exif_iif_add_tag(image_info_type *image_info, int section_index, char *name, int tag, int format, size_t length, void* value TSRMLS_DC) ++static void exif_iif_add_tag(image_info_type *image_info, int section_index, char *name, int tag, int format, size_t length, void* value, size_t value_len TSRMLS_DC) + { +- exif_iif_add_value(image_info, section_index, name, tag, format, (int)length, value, image_info->motorola_intel TSRMLS_CC); ++ exif_iif_add_value(image_info, section_index, name, tag, format, (int)length, value, value_len, image_info->motorola_intel TSRMLS_CC); + } + /* }}} */ + +@@ -2218,7 +2231,7 @@ static void add_assoc_image_info(zval *value, int sub_array, image_info_type *im + */ + static void exif_process_COM (image_info_type *image_info, char *value, size_t length TSRMLS_DC) + { +- exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_STRING, length-2, value+2 TSRMLS_CC); ++ exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_STRING, length-2, value+2, length-2 TSRMLS_CC); + } + /* }}} */ + +@@ -2233,17 +2246,17 @@ static void exif_process_CME (image_info_type *image_info, char *value, size_t l + if (length>3) { + switch(value[2]) { + case 0: +- exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_UNDEFINED, length, value TSRMLS_CC); ++ exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_UNDEFINED, length, value, length TSRMLS_CC); + break; + case 1: +- exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_STRING, length, value); ++ exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_STRING, length, value, length); + break; + default: + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Undefined JPEG2000 comment encoding"); + break; + } + } else { +- exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_UNDEFINED, 0, NULL); ++ exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_UNDEFINED, 0, NULL, 0 TSRMLS_CC); + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "JPEG2000 comment section too small"); + } + } +@@ -2615,7 +2628,7 @@ static int exif_process_user_comment(image_info_type *ImageInfo, char **pszInfoP + { + int a; + char *decode; +- size_t len;; ++ size_t len; + + *pszEncoding = NULL; + /* Copy the comment */ +@@ -2628,11 +2641,11 @@ static int exif_process_user_comment(image_info_type *ImageInfo, char **pszInfoP + /* First try to detect BOM: ZERO WIDTH NOBREAK SPACE (FEFF 16) + * since we have no encoding support for the BOM yet we skip that. + */ +- if (!memcmp(szValuePtr, "\xFE\xFF", 2)) { ++ if (ByteCount >=2 && !memcmp(szValuePtr, "\xFE\xFF", 2)) { + decode = "UCS-2BE"; + szValuePtr = szValuePtr+2; + ByteCount -= 2; +- } else if (!memcmp(szValuePtr, "\xFF\xFE", 2)) { ++ } else if (ByteCount >=2 && !memcmp(szValuePtr, "\xFF\xFE", 2)) { + decode = "UCS-2LE"; + szValuePtr = szValuePtr+2; + ByteCount -= 2; +@@ -2746,12 +2759,13 @@ static int exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * valu + continue; + if (maker_note->model && (!ImageInfo->model || strcmp(maker_note->model, ImageInfo->model))) + continue; +- if (maker_note->id_string && strncmp(maker_note->id_string, value_ptr, maker_note->id_string_len)) ++ if (maker_note->id_string && value_len >= maker_note->id_string_len ++ && strncmp(maker_note->id_string, value_ptr, maker_note->id_string_len)) + continue; + break; + } + +- if (maker_note->offset >= value_len) { ++ if (value_len < 2 || maker_note->offset >= value_len - 1) { + /* Do not go past the value end */ + exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "IFD data too short: 0x%04X offset 0x%04X", value_len, maker_note->offset); + return FALSE; +@@ -2804,6 +2818,7 @@ static int exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * valu + break; + default: + case MN_OFFSET_NORMAL: ++ data_len = value_len; + break; + } + +@@ -2811,10 +2826,15 @@ static int exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * valu + exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size: 2 + 0x%04X*12 = 0x%04X > 0x%04X", NumDirEntries, 2+NumDirEntries*12, value_len); + return FALSE; + } ++ if ((dir_start - value_ptr) > value_len - (2+NumDirEntries*12)) { ++ exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size: 0x%04X > 0x%04X", (dir_start - value_ptr) + (2+NumDirEntries*12), value_len); ++ return FALSE; ++ } + + for (de=0;de)^ + ** +@@ -346,16 +1190,18 @@ extern "C" { + ** function is provided for use in DLLs since DLL users usually do not have + ** direct access to string constants within the DLL. ^The + ** sqlite3_libversion_number() function returns an integer equal to +-** [SQLITE_VERSION_NUMBER]. ^The sqlite3_sourceid() function returns ++** [SQLITE_VERSION_NUMBER]. ^(The sqlite3_sourceid() function returns + ** a pointer to a string constant whose value is the same as the +-** [SQLITE_SOURCE_ID] C preprocessor macro. ++** [SQLITE_SOURCE_ID] C preprocessor macro. Except if SQLite is built ++** using an edited copy of [the amalgamation], then the last four characters ++** of the hash might be different from [SQLITE_SOURCE_ID].)^ + ** + ** See also: [sqlite_version()] and [sqlite_source_id()]. + */ + SQLITE_API const char sqlite3_version[] = SQLITE_VERSION; +-SQLITE_API const char *SQLITE_STDCALL sqlite3_libversion(void); +-SQLITE_API const char *SQLITE_STDCALL sqlite3_sourceid(void); +-SQLITE_API int SQLITE_STDCALL sqlite3_libversion_number(void); ++SQLITE_API const char *sqlite3_libversion(void); ++SQLITE_API const char *sqlite3_sourceid(void); ++SQLITE_API int sqlite3_libversion_number(void); + + /* + ** CAPI3REF: Run-Time Library Compilation Options Diagnostics +@@ -380,8 +1226,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_libversion_number(void); + ** [sqlite_compileoption_get()] and the [compile_options pragma]. + */ + #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS +-SQLITE_API int SQLITE_STDCALL sqlite3_compileoption_used(const char *zOptName); +-SQLITE_API const char *SQLITE_STDCALL sqlite3_compileoption_get(int N); ++SQLITE_API int sqlite3_compileoption_used(const char *zOptName); ++SQLITE_API const char *sqlite3_compileoption_get(int N); ++#else ++# define sqlite3_compileoption_used(X) 0 ++# define sqlite3_compileoption_get(X) ((void*)0) + #endif + + /* +@@ -420,7 +1269,7 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_compileoption_get(int N); + ** + ** See the [threading mode] documentation for additional information. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_threadsafe(void); ++SQLITE_API int sqlite3_threadsafe(void); + + /* + ** CAPI3REF: Database Connection Handle +@@ -456,7 +1305,11 @@ typedef struct sqlite3 sqlite3; + */ + #ifdef SQLITE_INT64_TYPE + typedef SQLITE_INT64_TYPE sqlite_int64; +- typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; ++# ifdef SQLITE_UINT64_TYPE ++ typedef SQLITE_UINT64_TYPE sqlite_uint64; ++# else ++ typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; ++# endif + #elif defined(_MSC_VER) || defined(__BORLANDC__) + typedef __int64 sqlite_int64; + typedef unsigned __int64 sqlite_uint64; +@@ -517,8 +1370,8 @@ typedef sqlite_uint64 sqlite3_uint64; + ** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer + ** argument is a harmless no-op. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_close(sqlite3*); +-SQLITE_API int SQLITE_STDCALL sqlite3_close_v2(sqlite3*); ++SQLITE_API int sqlite3_close(sqlite3*); ++SQLITE_API int sqlite3_close_v2(sqlite3*); + + /* + ** The type for a callback function. +@@ -554,7 +1407,7 @@ typedef int (*sqlite3_callback)(void*,int,char**, char**); + ** from [sqlite3_malloc()] and passed back through the 5th parameter. + ** To avoid memory leaks, the application should invoke [sqlite3_free()] + ** on error message strings returned through the 5th parameter of +-** of sqlite3_exec() after the error message string is no longer needed. ++** sqlite3_exec() after the error message string is no longer needed. + ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors + ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to + ** NULL before returning. +@@ -581,7 +1434,7 @@ typedef int (*sqlite3_callback)(void*,int,char**, char**); + ** Restrictions: + ** + **+ ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER ); +-** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 ); ++** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 ); + ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 ); + **
+-** ^When the application provides any amount of scratch memory using +-** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large +-** [sqlite3_malloc|heap allocations]. +-** This can help [Robson proof|prevent memory allocation failures] due to heap +-** fragmentation in low-memory embedded systems. ++**
+-** +-** One can use this text in an SQL statement as follows: +-** +-**+-** char *zText = "It's a happy day!"; +-**
+-** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
+-** sqlite3_exec(db, zSQL, 0, 0, 0);
+-** sqlite3_free(zSQL);
+-**
+-**
+-** Because the %q format string is used, the '\'' character in zText
+-** is escaped and the SQL generated is as follows:
+-**
+-**
+-** INSERT INTO table1 VALUES('It''s a happy day!')
+-**
+-**
+-** This is correct. Had we used %s instead of %q, the generated SQL
+-** would have looked like this:
+-**
+-**
+-** INSERT INTO table1 VALUES('It's a happy day!');
+-**
+-**
+-** This second example is an SQL syntax error. As a general rule you should
+-** always use %q instead of %s when inserting text into a string literal.
+-**
+-** ^(The %Q option works like %q except it also adds single quotes around
+-** the outside of the total string. Additionally, if the parameter in the
+-** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
+-** single quotes).)^ So, for example, one could say:
+-**
+-**
+-** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
+-** sqlite3_exec(db, zSQL, 0, 0, 0);
+-** sqlite3_free(zSQL);
+-**
+-**
+-** The code above will render a correct SQL statement in the zSQL
+-** variable even if the zText variable is a NULL pointer.
+-**
+-** ^(The "%w" formatting option is like "%q" except that it expects to
+-** be contained within double-quotes instead of single quotes, and it
+-** escapes the double-quote character instead of the single-quote
+-** character.)^ The "%w" formatting option is intended for safely inserting
+-** table and column names into a constructed SQL statement.
+-**
+-** ^(The "%z" formatting option works like "%s" but with the
+-** addition that after the string has been read and copied into
+-** the result, [sqlite3_free()] is called on the input string.)^
++** See also: [built-in printf()], [printf() SQL function]
+ */
+-SQLITE_API char *SQLITE_CDECL sqlite3_mprintf(const char*,...);
+-SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char*, va_list);
+-SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int,char*,const char*, ...);
+-SQLITE_API char *SQLITE_STDCALL sqlite3_vsnprintf(int,char*,const char*, va_list);
++SQLITE_API char *sqlite3_mprintf(const char*,...);
++SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
++SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
++SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
+
+ /*
+ ** CAPI3REF: Memory Allocation Subsystem
+@@ -2662,12 +3818,12 @@ SQLITE_API char *SQLITE_STDCALL sqlite3_vsnprintf(int,char*,const char*, va_list
+ ** a block of memory after it has been released using
+ ** [sqlite3_free()] or [sqlite3_realloc()].
+ */
+-SQLITE_API void *SQLITE_STDCALL sqlite3_malloc(int);
+-SQLITE_API void *SQLITE_STDCALL sqlite3_malloc64(sqlite3_uint64);
+-SQLITE_API void *SQLITE_STDCALL sqlite3_realloc(void*, int);
+-SQLITE_API void *SQLITE_STDCALL sqlite3_realloc64(void*, sqlite3_uint64);
+-SQLITE_API void SQLITE_STDCALL sqlite3_free(void*);
+-SQLITE_API sqlite3_uint64 SQLITE_STDCALL sqlite3_msize(void*);
++SQLITE_API void *sqlite3_malloc(int);
++SQLITE_API void *sqlite3_malloc64(sqlite3_uint64);
++SQLITE_API void *sqlite3_realloc(void*, int);
++SQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64);
++SQLITE_API void sqlite3_free(void*);
++SQLITE_API sqlite3_uint64 sqlite3_msize(void*);
+
+ /*
+ ** CAPI3REF: Memory Allocator Statistics
+@@ -2692,8 +3848,8 @@ SQLITE_API sqlite3_uint64 SQLITE_STDCALL sqlite3_msize(void*);
+ ** by [sqlite3_memory_highwater(1)] is the high-water mark
+ ** prior to the reset.
+ */
+-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_used(void);
+-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag);
++SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
++SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
+
+ /*
+ ** CAPI3REF: Pseudo-Random Number Generator
+@@ -2716,17 +3872,19 @@ SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag);
+ ** internally and without recourse to the [sqlite3_vfs] xRandomness
+ ** method.
+ */
+-SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P);
++SQLITE_API void sqlite3_randomness(int N, void *P);
+
+ /*
+ ** CAPI3REF: Compile-Time Authorization Callbacks
+ ** METHOD: sqlite3
++** KEYWORDS: {authorizer callback}
+ **
+ ** ^This routine registers an authorizer callback with a particular
+ ** [database connection], supplied in the first argument.
+ ** ^The authorizer callback is invoked as SQL statements are being compiled
+ ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
+-** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. ^At various
++** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()],
++** and [sqlite3_prepare16_v3()]. ^At various
+ ** points during the compilation process, as logic is being created
+ ** to perform various actions, the authorizer callback is invoked to
+ ** see if those actions are allowed. ^The authorizer callback should
+@@ -2748,8 +3906,10 @@ SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P);
+ ** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
+ ** to the callback is an integer [SQLITE_COPY | action code] that specifies
+ ** the particular action to be authorized. ^The third through sixth parameters
+-** to the callback are zero-terminated strings that contain additional
+-** details about the action to be authorized.
++** to the callback are either NULL pointers or zero-terminated strings
++** that contain additional details about the action to be authorized.
++** Applications must always be prepared to encounter a NULL pointer in any
++** of the third through the sixth parameters of the authorization callback.
+ **
+ ** ^If the action code is [SQLITE_READ]
+ ** and the callback returns [SQLITE_IGNORE] then the
+@@ -2758,6 +3918,10 @@ SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P);
+ ** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE]
+ ** return can be used to deny an untrusted user access to individual
+ ** columns of a table.
++** ^When a table is referenced by a [SELECT] but no column values are
++** extracted from that table (for example in a query like
++** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback
++** is invoked once for that table with a column name that is an empty string.
+ ** ^If the action code is [SQLITE_DELETE] and the callback returns
+ ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
+ ** [truncate optimization] is disabled and all rows are deleted individually.
+@@ -2799,7 +3963,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P);
+ ** as stated in the previous paragraph, sqlite3_step() invokes
+ ** sqlite3_prepare_v2() to reprepare a statement after a schema change.
+ */
+-SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer(
++SQLITE_API int sqlite3_set_authorizer(
+ sqlite3*,
+ int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
+ void *pUserData
+@@ -2879,6 +4043,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer(
+ ** CAPI3REF: Tracing And Profiling Functions
+ ** METHOD: sqlite3
+ **
++** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
++** instead of the routines described here.
++**
+ ** These routines register callback functions that can be used for
+ ** tracing and profiling the execution of SQL statements.
+ **
+@@ -2900,14 +4067,108 @@ SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer(
+ ** time is in units of nanoseconds, however the current implementation
+ ** is only capable of millisecond resolution so the six least significant
+ ** digits in the time are meaningless. Future versions of SQLite
+-** might provide greater resolution on the profiler callback. The
+-** sqlite3_profile() function is considered experimental and is
+-** subject to change in future versions of SQLite.
++** might provide greater resolution on the profiler callback. Invoking
++** either [sqlite3_trace()] or [sqlite3_trace_v2()] will cancel the
++** profile callback.
+ */
+-SQLITE_API void *SQLITE_STDCALL sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
+-SQLITE_API SQLITE_EXPERIMENTAL void *SQLITE_STDCALL sqlite3_profile(sqlite3*,
++SQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*,
++ void(*xTrace)(void*,const char*), void*);
++SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
+ void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
+
++/*
++** CAPI3REF: SQL Trace Event Codes
++** KEYWORDS: SQLITE_TRACE
++**
++** These constants identify classes of events that can be monitored
++** using the [sqlite3_trace_v2()] tracing logic. The M argument
++** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
++** the following constants. ^The first argument to the trace callback
++** is one of the following constants.
++**
++** New tracing constants may be added in future releases.
++**
++** ^A trace callback has four arguments: xCallback(T,C,P,X).
++** ^The T argument is one of the integer type codes above.
++** ^The C argument is a copy of the context pointer passed in as the
++** fourth argument to [sqlite3_trace_v2()].
++** The P and X arguments are pointers whose meanings depend on T.
++**
++** ^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having ++** the extra prepFlags parameter, which is a bit array consisting of zero or ++** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The ++** sqlite3_prepare_v2() interface works exactly the same as ++** sqlite3_prepare_v3() with a zero prepFlags parameter. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare( ++SQLITE_API int sqlite3_prepare( ++ sqlite3 *db, /* Database handle */ ++ const char *zSql, /* SQL statement, UTF-8 encoded */ ++ int nByte, /* Maximum length of zSql in bytes. */ ++ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ ++ const char **pzTail /* OUT: Pointer to unused portion of zSql */ ++); ++SQLITE_API int sqlite3_prepare_v2( + sqlite3 *db, /* Database handle */ + const char *zSql, /* SQL statement, UTF-8 encoded */ + int nByte, /* Maximum length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: Statement handle */ + const char **pzTail /* OUT: Pointer to unused portion of zSql */ + ); +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare_v2( ++SQLITE_API int sqlite3_prepare_v3( + sqlite3 *db, /* Database handle */ + const char *zSql, /* SQL statement, UTF-8 encoded */ + int nByte, /* Maximum length of zSql in bytes. */ ++ unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */ + sqlite3_stmt **ppStmt, /* OUT: Statement handle */ + const char **pzTail /* OUT: Pointer to unused portion of zSql */ + ); +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare16( ++SQLITE_API int sqlite3_prepare16( ++ sqlite3 *db, /* Database handle */ ++ const void *zSql, /* SQL statement, UTF-16 encoded */ ++ int nByte, /* Maximum length of zSql in bytes. */ ++ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ ++ const void **pzTail /* OUT: Pointer to unused portion of zSql */ ++); ++SQLITE_API int sqlite3_prepare16_v2( + sqlite3 *db, /* Database handle */ + const void *zSql, /* SQL statement, UTF-16 encoded */ + int nByte, /* Maximum length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: Statement handle */ + const void **pzTail /* OUT: Pointer to unused portion of zSql */ + ); +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare16_v2( ++SQLITE_API int sqlite3_prepare16_v3( + sqlite3 *db, /* Database handle */ + const void *zSql, /* SQL statement, UTF-16 encoded */ + int nByte, /* Maximum length of zSql in bytes. */ ++ unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */ + sqlite3_stmt **ppStmt, /* OUT: Statement handle */ + const void **pzTail /* OUT: Pointer to unused portion of zSql */ + ); +@@ -3526,11 +4874,43 @@ SQLITE_API int SQLITE_STDCALL sqlite3_prepare16_v2( + ** CAPI3REF: Retrieving Statement SQL + ** METHOD: sqlite3_stmt + ** +-** ^This interface can be used to retrieve a saved copy of the original +-** SQL text used to create a [prepared statement] if that statement was +-** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()]. +-*/ +-SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt); ++** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8 ++** SQL text used to create [prepared statement] P if P was ++** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], ++** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()]. ++** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8 ++** string containing the SQL text of prepared statement P with ++** [bound parameters] expanded. ++** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8 ++** string containing the normalized SQL text of prepared statement P. The ++** semantics used to normalize a SQL statement are unspecified and subject ++** to change. At a minimum, literal values will be replaced with suitable ++** placeholders. ++** ++** ^(For example, if a prepared statement is created using the SQL ++** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345 ++** and parameter :xyz is unbound, then sqlite3_sql() will return ++** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql() ++** will return "SELECT 2345,NULL".)^ ++** ++** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory ++** is available to hold the result, or if the result would exceed the ++** the maximum string length determined by the [SQLITE_LIMIT_LENGTH]. ++** ++** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of ++** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time ++** option causes sqlite3_expanded_sql() to always return NULL. ++** ++** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P) ++** are managed by SQLite and are automatically freed when the prepared ++** statement is finalized. ++** ^The string returned by sqlite3_expanded_sql(P), on the other hand, ++** is obtained from [sqlite3_malloc()] and must be free by the application ++** by passing it to [sqlite3_free()]. ++*/ ++SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt); ++SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt); ++SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt); + + /* + ** CAPI3REF: Determine If An SQL Statement Writes The Database +@@ -3561,8 +4941,24 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt); + ** sqlite3_stmt_readonly() to return true since, while those statements + ** change the configuration of a database connection, they do not make + ** changes to the content of the database files on disk. ++** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since ++** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and ++** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so ++** sqlite3_stmt_readonly() returns false for those commands. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt); ++SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt); ++ ++/* ++** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement ++** METHOD: sqlite3_stmt ++** ++** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the ++** prepared statement S is an EXPLAIN statement, or 2 if the ++** statement S is an EXPLAIN QUERY PLAN. ++** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is ++** an ordinary statement or a NULL pointer. ++*/ ++SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt); + + /* + ** CAPI3REF: Determine If A Prepared Statement Has Been Reset +@@ -3570,7 +4966,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt); + ** + ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the + ** [prepared statement] S has been stepped at least once using +-** [sqlite3_step(S)] but has not run to completion and/or has not ++** [sqlite3_step(S)] but has neither run to completion (returned ++** [SQLITE_DONE] from [sqlite3_step(S)]) nor + ** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S) + ** interface returns false if S is a NULL pointer. If S is not a + ** NULL pointer and is not a pointer to a valid [prepared statement] +@@ -3582,7 +4979,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt); + ** for example, in diagnostic routines to search for prepared + ** statements that are holding a transaction open. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt*); ++SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*); + + /* + ** CAPI3REF: Dynamically Typed Value Object +@@ -3597,7 +4994,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt*); + ** Some interfaces require a protected sqlite3_value. Other interfaces + ** will accept either a protected or an unprotected sqlite3_value. + ** Every interface that accepts sqlite3_value arguments specifies +-** whether or not it requires a protected sqlite3_value. ++** whether or not it requires a protected sqlite3_value. The ++** [sqlite3_value_dup()] interface can be used to construct a new ++** protected sqlite3_value from an unprotected sqlite3_value. + ** + ** The terms "protected" and "unprotected" refer to whether or not + ** a mutex is held. An internal mutex is held for a protected +@@ -3616,12 +5015,13 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt*); + ** implementation of [application-defined SQL functions] are protected. + ** ^The sqlite3_value object returned by + ** [sqlite3_column_value()] is unprotected. +-** Unprotected sqlite3_value objects may only be used with +-** [sqlite3_result_value()] and [sqlite3_bind_value()]. ++** Unprotected sqlite3_value objects may only be used as arguments ++** to [sqlite3_result_value()], [sqlite3_bind_value()], and ++** [sqlite3_value_dup()]. + ** The [sqlite3_value_blob | sqlite3_value_type()] family of + ** interfaces require protected sqlite3_value objects. + */ +-typedef struct Mem sqlite3_value; ++typedef struct sqlite3_value sqlite3_value; + + /* + ** CAPI3REF: SQL Function Context Object +@@ -3699,7 +5099,9 @@ typedef struct sqlite3_context sqlite3_context; + ** ^The fifth argument to the BLOB and string binding interfaces + ** is a destructor used to dispose of the BLOB or + ** string after SQLite has finished with it. ^The destructor is called +-** to dispose of the BLOB or string even if the call to bind API fails. ++** to dispose of the BLOB or string even if the call to the bind API fails, ++** except the destructor is not called if the third parameter is a NULL ++** pointer or the fourth parameter is negative. + ** ^If the fifth argument is + ** the special value [SQLITE_STATIC], then SQLite assumes that the + ** information is in static, unmanaged space and does not need to be freed. +@@ -3723,6 +5125,15 @@ typedef struct sqlite3_context sqlite3_context; + ** [sqlite3_blob_open | incremental BLOB I/O] routines. + ** ^A negative value for the zeroblob results in a zero-length BLOB. + ** ++** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in ++** [prepared statement] S to have an SQL value of NULL, but to also be ++** associated with the pointer P of type T. ^D is either a NULL pointer or ++** a pointer to a destructor function for P. ^SQLite will invoke the ++** destructor D with a single argument of P when it is finished using ++** P. The T parameter should be a static string, preferably a string ++** literal. The sqlite3_bind_pointer() routine is part of the ++** [pointer passing interface] added for SQLite 3.20.0. ++** + ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer + ** for the [prepared statement] or with a prepared statement for which + ** [sqlite3_step()] has been called more recently than [sqlite3_reset()], +@@ -3744,19 +5155,21 @@ typedef struct sqlite3_context sqlite3_context; + ** See also: [sqlite3_bind_parameter_count()], + ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()]. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64, ++SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); ++SQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64, + void(*)(void*)); +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_double(sqlite3_stmt*, int, double); +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_int(sqlite3_stmt*, int, int); +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_null(sqlite3_stmt*, int); +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*)); +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64, ++SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double); ++SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int); ++SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); ++SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int); ++SQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*)); ++SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); ++SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64, + void(*)(void*), unsigned char encoding); +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); ++SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); ++SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*)); ++SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); ++SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64); + + /* + ** CAPI3REF: Number Of SQL Parameters +@@ -3777,7 +5190,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); + ** [sqlite3_bind_parameter_name()], and + ** [sqlite3_bind_parameter_index()]. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt*); ++SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*); + + /* + ** CAPI3REF: Name Of A Host Parameter +@@ -3798,14 +5211,14 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt*); + ** ^If the value N is out of range or if the N-th parameter is + ** nameless, then NULL is returned. ^The returned string is + ** always in UTF-8 encoding even if the named parameter was +-** originally specified as UTF-16 in [sqlite3_prepare16()] or +-** [sqlite3_prepare16_v2()]. ++** originally specified as UTF-16 in [sqlite3_prepare16()], ++** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()]. + ** + ** See also: [sqlite3_bind_blob|sqlite3_bind()], + ** [sqlite3_bind_parameter_count()], and + ** [sqlite3_bind_parameter_index()]. + */ +-SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt*, int); ++SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); + + /* + ** CAPI3REF: Index Of A Parameter With A Given Name +@@ -3816,13 +5229,14 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt*, + ** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero + ** is returned if no matching parameter is found. ^The parameter + ** name must be given in UTF-8 even if the original statement +-** was prepared from UTF-16 text using [sqlite3_prepare16_v2()]. ++** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or ++** [sqlite3_prepare16_v3()]. + ** + ** See also: [sqlite3_bind_blob|sqlite3_bind()], + ** [sqlite3_bind_parameter_count()], and +-** [sqlite3_bind_parameter_index()]. ++** [sqlite3_bind_parameter_name()]. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); ++SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); + + /* + ** CAPI3REF: Reset All Bindings On A Prepared Statement +@@ -3832,19 +5246,23 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_index(sqlite3_stmt*, const + ** the [sqlite3_bind_blob | bindings] on a [prepared statement]. + ** ^Use this routine to reset all host parameters to NULL. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_clear_bindings(sqlite3_stmt*); ++SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*); + + /* + ** CAPI3REF: Number Of Columns In A Result Set + ** METHOD: sqlite3_stmt + ** + ** ^Return the number of columns in the result set returned by the +-** [prepared statement]. ^This routine returns 0 if pStmt is an SQL +-** statement that does not return data (for example an [UPDATE]). ++** [prepared statement]. ^If this routine returns 0, that means the ++** [prepared statement] returns no data (for example an [UPDATE]). ++** ^However, just because this routine returns a positive number does not ++** mean that one or more rows of data will be returned. ^A SELECT statement ++** will always have a positive sqlite3_column_count() but depending on the ++** WHERE clause constraints and the table content, it might return no rows. + ** + ** See also: [sqlite3_data_count()] + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt); ++SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt); + + /* + ** CAPI3REF: Column Names In A Result Set +@@ -3873,8 +5291,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt); + ** then the name of the column is unspecified and may change from + ** one release of SQLite to the next. + */ +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_name(sqlite3_stmt*, int N); +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt*, int N); ++SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N); ++SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N); + + /* + ** CAPI3REF: Source Of Data In A Query Result +@@ -3922,12 +5340,12 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt*, int N + ** for the same [prepared statement] and result column + ** at the same time then the results are undefined. + */ +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_database_name(sqlite3_stmt*,int); +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_database_name16(sqlite3_stmt*,int); +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_table_name(sqlite3_stmt*,int); +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_table_name16(sqlite3_stmt*,int); +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_origin_name(sqlite3_stmt*,int); +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_origin_name16(sqlite3_stmt*,int); ++SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int); ++SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int); ++SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int); ++SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int); ++SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int); ++SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); + + /* + ** CAPI3REF: Declared Datatype Of A Query Result +@@ -3959,23 +5377,25 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_origin_name16(sqlite3_stmt* + ** is associated with individual values, not with the containers + ** used to hold those values. + */ +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_decltype(sqlite3_stmt*,int); +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt*,int); ++SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int); ++SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int); + + /* + ** CAPI3REF: Evaluate An SQL Statement + ** METHOD: sqlite3_stmt + ** +-** After a [prepared statement] has been prepared using either +-** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy ++** After a [prepared statement] has been prepared using any of ++** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()], ++** or [sqlite3_prepare16_v3()] or one of the legacy + ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function + ** must be called one or more times to evaluate the statement. + ** + ** The details of the behavior of the sqlite3_step() interface depend +-** on whether the statement was prepared using the newer "v2" interface +-** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy +-** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the +-** new "v2" interface is recommended for new applications but the legacy ++** on whether the statement was prepared using the newer "vX" interfaces ++** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()], ++** [sqlite3_prepare16_v2()] or the older legacy ++** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the ++** new "vX" interface is recommended for new applications but the legacy + ** interface will continue to be supported. + ** + ** ^In the legacy interface, the return value will be either [SQLITE_BUSY], +@@ -4021,7 +5441,8 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt*,in + ** other than [SQLITE_ROW] before any subsequent invocation of + ** sqlite3_step(). Failure to reset the prepared statement using + ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from +-** sqlite3_step(). But after version 3.6.23.1, sqlite3_step() began ++** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1], ++** sqlite3_step() began + ** calling [sqlite3_reset()] automatically in this circumstance rather + ** than returning [SQLITE_MISUSE]. This is not considered a compatibility + ** break because any application that ever receives an SQLITE_MISUSE error +@@ -4035,12 +5456,13 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt*,in + ** specific [error codes] that better describes the error. + ** We admit that this is a goofy design. The problem has been fixed + ** with the "v2" interface. If you prepare all of your SQL statements +-** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead ++** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()] ++** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead + ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces, + ** then the more specific [error codes] are returned directly +-** by sqlite3_step(). The use of the "v2" interface is recommended. ++** by sqlite3_step(). The use of the "vX" interfaces is recommended. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt*); ++SQLITE_API int sqlite3_step(sqlite3_stmt*); + + /* + ** CAPI3REF: Number of columns in a result set +@@ -4061,7 +5483,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt*); + ** + ** See also: [sqlite3_column_count()] + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt); ++SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt); + + /* + ** CAPI3REF: Fundamental Datatypes +@@ -4100,7 +5522,27 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt); + ** KEYWORDS: {column access functions} + ** METHOD: sqlite3_stmt + ** +-** These routines form the "result set" interface. ++** Summary: ++**
++** ++** Details: + ** + ** ^These routines return information about a single column of the current + ** result row of a query. ^In every case the first argument is a pointer +@@ -4123,16 +5565,29 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt); + ** are called from a different thread while any of these routines + ** are pending, then the results are undefined. + ** ++** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16) ++** each return the value of a result column in a specific data format. If ++** the result column is not initially in the requested format (for example, ++** if the query returns an integer but the sqlite3_column_text() interface ++** is used to extract the value) then an automatic type conversion is performed. ++** + ** ^The sqlite3_column_type() routine returns the + ** [SQLITE_INTEGER | datatype code] for the initial data type + ** of the result column. ^The returned value is one of [SQLITE_INTEGER], +-** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value +-** returned by sqlite3_column_type() is only meaningful if no type +-** conversions have occurred as described below. After a type conversion, +-** the value returned by sqlite3_column_type() is undefined. Future ++** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. ++** The return value of sqlite3_column_type() can be used to decide which ++** of the first six interface should be used to extract the column value. ++** The value returned by sqlite3_column_type() is only meaningful if no ++** automatic type conversions have occurred for the value in question. ++** After a type conversion, the result of calling sqlite3_column_type() ++** is undefined, though harmless. Future + ** versions of SQLite may change the behavior of sqlite3_column_type() + ** following a type conversion. + ** ++** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes() ++** or sqlite3_column_bytes16() interfaces can be used to determine the size ++** of that BLOB or string. ++** + ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes() + ** routine returns the number of bytes in that BLOB or string. + ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts +@@ -4161,16 +5616,21 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt); + ** even empty strings, are always zero-terminated. ^The return + ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. + ** +-** ^The object returned by [sqlite3_column_value()] is an +-** [unprotected sqlite3_value] object. An unprotected sqlite3_value object +-** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()]. ++** Warning: ^The object returned by [sqlite3_column_value()] is an ++** [unprotected sqlite3_value] object. In a multithreaded environment, ++** an unprotected sqlite3_value object may only be used safely with ++** [sqlite3_bind_value()] and [sqlite3_result_value()]. + ** If the [unprotected sqlite3_value] object returned by + ** [sqlite3_column_value()] is used in any other way, including calls + ** to routines like [sqlite3_value_int()], [sqlite3_value_text()], +-** or [sqlite3_value_bytes()], then the behavior is undefined. +-** +-** These routines attempt to convert the value where appropriate. ^For +-** example, if the internal representation is FLOAT and a text result ++** or [sqlite3_value_bytes()], the behavior is not threadsafe. ++** Hence, the sqlite3_column_value() interface ++** is normally only useful within the implementation of ++** [application-defined SQL functions] or [virtual tables], not within ++** top-level application code. ++** ++** The these routines may attempt to convert the datatype of the result. ++** ^For example, if the internal representation is FLOAT and a text result + ** is requested, [sqlite3_snprintf()] is used internally to perform the + ** conversion automatically. ^(The following table details the conversions + ** that are applied: +@@ -4198,12 +5658,6 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt); + ** + ** )^ + ** +-** The table above makes reference to standard C library functions atoi() +-** and atof(). SQLite does not really use these functions. It has its +-** own equivalent internal routines. The atoi() and atof() names are +-** used in the table for brevity and because they are familiar to most +-** C programmers. +-** + ** Note that when type conversions occur, pointers returned by prior + ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or + ** sqlite3_column_text16() may be invalidated. +@@ -4228,7 +5682,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt); + ** of conversion are done in place when it is possible, but sometimes they + ** are not possible and in those cases prior pointers are invalidated. + ** +-** The safest and easiest to remember policy is to invoke these routines ++** The safest policy is to invoke these routines + ** in one of the following ways: + ** + **++**
sqlite3_column_blob → BLOB result ++** sqlite3_column_double → REAL result ++** sqlite3_column_int → 32-bit INTEGER result ++** sqlite3_column_int64 → 64-bit INTEGER result ++** sqlite3_column_text → UTF-8 TEXT result ++** sqlite3_column_text16 → UTF-16 TEXT result ++** sqlite3_column_value → The result as an ++** [sqlite3_value|unprotected sqlite3_value] object. ++** ++** sqlite3_column_bytes → Size of a BLOB ++** or a UTF-8 TEXT result in bytes ++** sqlite3_column_bytes16 ++** → Size of UTF-16 ++** TEXT in bytes ++** sqlite3_column_type → Default ++** datatype of the result ++**
++** ++** Details: ++** ++** These routines extract type, size, and content information from ++** [protected sqlite3_value] objects. Protected sqlite3_value objects ++** are used to pass parameter information into implementation of ++** [application-defined SQL functions] and [virtual tables]. + ** + ** These routines work only with [protected sqlite3_value] objects. + ** Any attempt to use these routines on an [unprotected sqlite3_value] +-** object results in undefined behavior. ++** is not threadsafe. + ** + ** ^These routines work just like the corresponding [column access functions] + ** except that these routines take a single [protected sqlite3_value] object +@@ -4526,6 +6042,24 @@ SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(voi + ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces + ** extract UTF-16 strings as big-endian and little-endian respectively. + ** ++** ^If [sqlite3_value] object V was initialized ++** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)] ++** and if X and Y are strings that compare equal according to strcmp(X,Y), ++** then sqlite3_value_pointer(V,Y) will return the pointer P. ^Otherwise, ++** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer() ++** routine is part of the [pointer passing interface] added for SQLite 3.20.0. ++** ++** ^(The sqlite3_value_type(V) interface returns the ++** [SQLITE_INTEGER | datatype code] for the initial datatype of the ++** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER], ++** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^ ++** Other interfaces might change the datatype for an sqlite3_value object. ++** For example, if the datatype is initially SQLITE_INTEGER and ++** sqlite3_value_text(V) is called to extract a text value for that ++** integer, then subsequent calls to sqlite3_value_type(V) might return ++** SQLITE_TEXT. Whether or not a persistent internal datatype conversion ++** occurs is undefined and may change from one release of SQLite to the next. ++** + ** ^(The sqlite3_value_numeric_type() interface attempts to apply + ** numeric affinity to the value. This means that an attempt is + ** made to convert the value to an integer or floating point. If +@@ -4534,6 +6068,24 @@ SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(voi + ** then the conversion is performed. Otherwise no conversion occurs. + ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^ + ** ++** ^Within the [xUpdate] method of a [virtual table], the ++** sqlite3_value_nochange(X) interface returns true if and only if ++** the column corresponding to X is unchanged by the UPDATE operation ++** that the xUpdate method call was invoked to implement and if ++** and the prior [xColumn] method call that was invoked to extracted ++** the value for that column returned without setting a result (probably ++** because it queried [sqlite3_vtab_nochange()] and found that the column ++** was unchanging). ^Within an [xUpdate] method, any value for which ++** sqlite3_value_nochange(X) is true will in all other respects appear ++** to be a NULL value. If sqlite3_value_nochange(X) is invoked anywhere other ++** than within an [xUpdate] method call for an UPDATE statement, then ++** the return value is arbitrary and meaningless. ++** ++** ^The sqlite3_value_frombind(X) interface returns non-zero if the ++** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()] ++** interfaces. ^If X comes from an SQL literal value, or a table column, ++** and expression, then sqlite3_value_frombind(X) returns zero. ++** + ** Please pay particular attention to the fact that the pointer returned + ** from [sqlite3_value_blob()], [sqlite3_value_text()], or + ** [sqlite3_value_text16()] can be invalidated by a subsequent call to +@@ -4542,19 +6094,73 @@ SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(voi + ** + ** These routines must be called from the same thread as + ** the SQL function that supplied the [sqlite3_value*] parameters. ++** ++** As long as the input parameter is correct, these routines can only ++** fail if an out-of-memory error occurs during a format conversion. ++** Only the following subset of interfaces are subject to out-of-memory ++** errors: ++** ++**++**
sqlite3_value_blob → BLOB value ++** sqlite3_value_double → REAL value ++** sqlite3_value_int → 32-bit INTEGER value ++** sqlite3_value_int64 → 64-bit INTEGER value ++** sqlite3_value_pointer → Pointer value ++** sqlite3_value_text → UTF-8 TEXT value ++** sqlite3_value_text16 → UTF-16 TEXT value in ++** the native byteorder ++** sqlite3_value_text16be → UTF-16be TEXT value ++** sqlite3_value_text16le → UTF-16le TEXT value ++** ++** sqlite3_value_bytes → Size of a BLOB ++** or a UTF-8 TEXT in bytes ++** sqlite3_value_bytes16 ++** → Size of UTF-16 ++** TEXT in bytes ++** sqlite3_value_type → Default ++** datatype of the value ++** sqlite3_value_numeric_type ++** → Best numeric datatype of the value ++** sqlite3_value_nochange ++** → True if the column is unchanged in an UPDATE ++** against a virtual table. ++** sqlite3_value_frombind ++** → True if value originated from a [bound parameter] ++**
+@@ -5635,7 +7334,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_enable_load_extension(sqlite3 *db, int ono + ** See also: [sqlite3_reset_auto_extension()] + ** and [sqlite3_cancel_auto_extension()] + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(void (*xEntryPoint)(void)); ++SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void)); + + /* + ** CAPI3REF: Cancel Automatic Extension Loading +@@ -5647,7 +7346,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(void (*xEntryPoint)(void)); + ** unregistered and it returns 0 if X was not on the list of initialization + ** routines. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_cancel_auto_extension(void (*xEntryPoint)(void)); ++SQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void)); + + /* + ** CAPI3REF: Reset Automatic Extension Loading +@@ -5655,7 +7354,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_cancel_auto_extension(void (*xEntryPoint)( + ** ^This interface disables all automatic extensions previously + ** registered using [sqlite3_auto_extension()]. + */ +-SQLITE_API void SQLITE_STDCALL sqlite3_reset_auto_extension(void); ++SQLITE_API void sqlite3_reset_auto_extension(void); + + /* + ** The interface to the virtual-table mechanism is currently considered +@@ -5723,6 +7422,9 @@ struct sqlite3_module { + int (*xSavepoint)(sqlite3_vtab *pVTab, int); + int (*xRelease)(sqlite3_vtab *pVTab, int); + int (*xRollbackTo)(sqlite3_vtab *pVTab, int); ++ /* The methods above are in versions 1 and 2 of the sqlite_module object. ++ ** Those below are for version 3 and greater. */ ++ int (*xShadowName)(const char*); + }; + + /* +@@ -5757,6 +7459,17 @@ struct sqlite3_module { + ** ^Information about the ORDER BY clause is stored in aOrderBy[]. + ** ^Each term of aOrderBy records a column of the ORDER BY clause. + ** ++** The colUsed field indicates which columns of the virtual table may be ++** required by the current scan. Virtual table columns are numbered from ++** zero in the order in which they appear within the CREATE TABLE statement ++** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62), ++** the corresponding bit is set within the colUsed mask if the column may be ++** required by SQLite. If the table has at least 64 columns and any column ++** to the right of the first 63 is required, then bit 63 of colUsed is also ++** set. In other words, column iCol may be required if the expression ++** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to ++** non-zero. ++** + ** The [xBestIndex] method must fill aConstraintUsage[] with information + ** about what parameters to pass to xFilter. ^If argvIndex>0 then + ** the right-hand side of the corresponding aConstraint[] is evaluated +@@ -5782,19 +7495,39 @@ struct sqlite3_module { + ** ^The estimatedRows value is an estimate of the number of rows that + ** will be returned by the strategy. + ** ++** The xBestIndex method may optionally populate the idxFlags field with a ++** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag - ++** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite ++** assumes that the strategy may visit at most one row. ++** ++** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then ++** SQLite also assumes that if a call to the xUpdate() method is made as ++** part of the same statement to delete or update a virtual table row and the ++** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback ++** any database changes. In other words, if the xUpdate() returns ++** SQLITE_CONSTRAINT, the database contents must be exactly as they were ++** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not ++** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by ++** the xUpdate method are automatically rolled back by SQLite. ++** + ** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info +-** structure for SQLite version 3.8.2. If a virtual table extension is ++** structure for SQLite [version 3.8.2] ([dateof:3.8.2]). ++** If a virtual table extension is + ** used with an SQLite version earlier than 3.8.2, the results of attempting + ** to read or write the estimatedRows field are undefined (but are likely + ** to included crashing the application). The estimatedRows field should + ** therefore only be used if [sqlite3_libversion_number()] returns a +-** value greater than or equal to 3008002. ++** value greater than or equal to 3008002. Similarly, the idxFlags field ++** was added for [version 3.9.0] ([dateof:3.9.0]). ++** It may therefore only be used if ++** sqlite3_libversion_number() returns a value greater than or equal to ++** 3009000. + */ + struct sqlite3_index_info { + /* Inputs */ + int nConstraint; /* Number of entries in aConstraint */ + struct sqlite3_index_constraint { +- int iColumn; /* Column on left-hand side of constraint */ ++ int iColumn; /* Column constrained. -1 for ROWID */ + unsigned char op; /* Constraint operator */ + unsigned char usable; /* True if this constraint is usable */ + int iTermOffset; /* Used internally - xBestIndex should ignore */ +@@ -5816,8 +7549,21 @@ struct sqlite3_index_info { + double estimatedCost; /* Estimated cost of using this index */ + /* Fields below are only available in SQLite 3.8.2 and later */ + sqlite3_int64 estimatedRows; /* Estimated number of rows returned */ ++ /* Fields below are only available in SQLite 3.9.0 and later */ ++ int idxFlags; /* Mask of SQLITE_INDEX_SCAN_* flags */ ++ /* Fields below are only available in SQLite 3.10.0 and later */ ++ sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */ + }; + ++/* ++** CAPI3REF: Virtual Table Scan Flags ++** ++** Virtual table implementations are allowed to set the ++** [sqlite3_index_info].idxFlags field to some combination of ++** these bits. ++*/ ++#define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */ ++ + /* + ** CAPI3REF: Virtual Table Constraint Operator Codes + ** +@@ -5826,12 +7572,21 @@ struct sqlite3_index_info { + ** an operator that is part of a constraint term in the wHERE clause of + ** a query that uses a [virtual table]. + */ +-#define SQLITE_INDEX_CONSTRAINT_EQ 2 +-#define SQLITE_INDEX_CONSTRAINT_GT 4 +-#define SQLITE_INDEX_CONSTRAINT_LE 8 +-#define SQLITE_INDEX_CONSTRAINT_LT 16 +-#define SQLITE_INDEX_CONSTRAINT_GE 32 +-#define SQLITE_INDEX_CONSTRAINT_MATCH 64 ++#define SQLITE_INDEX_CONSTRAINT_EQ 2 ++#define SQLITE_INDEX_CONSTRAINT_GT 4 ++#define SQLITE_INDEX_CONSTRAINT_LE 8 ++#define SQLITE_INDEX_CONSTRAINT_LT 16 ++#define SQLITE_INDEX_CONSTRAINT_GE 32 ++#define SQLITE_INDEX_CONSTRAINT_MATCH 64 ++#define SQLITE_INDEX_CONSTRAINT_LIKE 65 ++#define SQLITE_INDEX_CONSTRAINT_GLOB 66 ++#define SQLITE_INDEX_CONSTRAINT_REGEXP 67 ++#define SQLITE_INDEX_CONSTRAINT_NE 68 ++#define SQLITE_INDEX_CONSTRAINT_ISNOT 69 ++#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70 ++#define SQLITE_INDEX_CONSTRAINT_ISNULL 71 ++#define SQLITE_INDEX_CONSTRAINT_IS 72 ++#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150 + + /* + ** CAPI3REF: Register A Virtual Table Implementation +@@ -5859,13 +7614,13 @@ struct sqlite3_index_info { + ** interface is equivalent to sqlite3_create_module_v2() with a NULL + ** destructor. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_create_module( ++SQLITE_API int sqlite3_create_module( + sqlite3 *db, /* SQLite connection to register module with */ + const char *zName, /* Name of the module */ + const sqlite3_module *p, /* Methods for the module */ + void *pClientData /* Client data for xCreate/xConnect */ + ); +-SQLITE_API int SQLITE_STDCALL sqlite3_create_module_v2( ++SQLITE_API int sqlite3_create_module_v2( + sqlite3 *db, /* SQLite connection to register module with */ + const char *zName, /* Name of the module */ + const sqlite3_module *p, /* Methods for the module */ +@@ -5928,7 +7683,7 @@ struct sqlite3_vtab_cursor { + ** to declare the format (the names and datatypes of the columns) of + ** the virtual tables they implement. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3*, const char *zSQL); ++SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL); + + /* + ** CAPI3REF: Overload A Function For A Virtual Table +@@ -5947,7 +7702,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3*, const char *zSQL); + ** purpose is to be a placeholder function that can be overloaded + ** by a [virtual table]. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); ++SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); + + /* + ** The interface to the virtual-table mechanism defined above (back up +@@ -6022,6 +7777,12 @@ typedef struct sqlite3_blob sqlite3_blob; + ** [database connection] error code and message accessible via + ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. + ** ++** A BLOB referenced by sqlite3_blob_open() may be read using the ++** [sqlite3_blob_read()] interface and modified by using ++** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a ++** different row of the same table using the [sqlite3_blob_reopen()] ++** interface. However, the column, table, or database of a [BLOB handle] ++** cannot be changed after the [BLOB handle] is opened. + ** + ** ^(If the row that a BLOB handle points to is modified by an + ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects +@@ -6045,8 +7806,12 @@ typedef struct sqlite3_blob sqlite3_blob; + ** + ** To avoid a resource leak, every open [BLOB handle] should eventually + ** be released by a call to [sqlite3_blob_close()]. ++** ++** See also: [sqlite3_blob_close()], ++** [sqlite3_blob_reopen()], [sqlite3_blob_read()], ++** [sqlite3_blob_bytes()], [sqlite3_blob_write()]. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_open( ++SQLITE_API int sqlite3_blob_open( + sqlite3*, + const char *zDb, + const char *zTable, +@@ -6060,11 +7825,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_open( + ** CAPI3REF: Move a BLOB Handle to a New Row + ** METHOD: sqlite3_blob + ** +-** ^This function is used to move an existing blob handle so that it points ++** ^This function is used to move an existing [BLOB handle] so that it points + ** to a different row of the same database table. ^The new row is identified + ** by the rowid value passed as the second argument. Only the row can be + ** changed. ^The database, table and column on which the blob handle is open +-** remain the same. Moving an existing blob handle to a new row can be ++** remain the same. Moving an existing [BLOB handle] to a new row is + ** faster than closing the existing handle and opening a new one. + ** + ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] - +@@ -6079,7 +7844,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_open( + ** + ** ^This function sets the database handle error code and message. + */ +-SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); ++SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); + + /* + ** CAPI3REF: Close A BLOB Handle +@@ -6102,7 +7867,7 @@ SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_bl + ** is passed a valid open blob handle, the values returned by the + ** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_close(sqlite3_blob *); ++SQLITE_API int sqlite3_blob_close(sqlite3_blob *); + + /* + ** CAPI3REF: Return The Size Of An Open BLOB +@@ -6118,7 +7883,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_close(sqlite3_blob *); + ** been closed by [sqlite3_blob_close()]. Passing any other pointer in + ** to this routine results in undefined and probably undesirable behavior. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *); ++SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *); + + /* + ** CAPI3REF: Read Data From A BLOB Incrementally +@@ -6147,7 +7912,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *); + ** + ** See also: [sqlite3_blob_write()]. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); ++SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); + + /* + ** CAPI3REF: Write Data Into A BLOB Incrementally +@@ -6189,7 +7954,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_read(sqlite3_blob *, void *Z, int N, + ** + ** See also: [sqlite3_blob_read()]. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); ++SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); + + /* + ** CAPI3REF: Virtual File System Objects +@@ -6220,9 +7985,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_write(sqlite3_blob *, const void *z, + ** ^(If the default VFS is unregistered, another VFS is chosen as + ** the default. The choice for the new VFS is arbitrary.)^ + */ +-SQLITE_API sqlite3_vfs *SQLITE_STDCALL sqlite3_vfs_find(const char *zVfsName); +-SQLITE_API int SQLITE_STDCALL sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); +-SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs*); ++SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName); ++SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); ++SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*); + + /* + ** CAPI3REF: Mutexes +@@ -6275,6 +8040,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs*); + **SQLITE_MUTEX_STATIC_APP1 + ** SQLITE_MUTEX_STATIC_APP2 + ** SQLITE_MUTEX_STATIC_APP3 ++** SQLITE_MUTEX_STATIC_VFS1 ++** SQLITE_MUTEX_STATIC_VFS2 ++** SQLITE_MUTEX_STATIC_VFS3 + ** + ** + ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) +@@ -6335,11 +8103,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs*); + ** + ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()]. + */ +-SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_mutex_alloc(int); +-SQLITE_API void SQLITE_STDCALL sqlite3_mutex_free(sqlite3_mutex*); +-SQLITE_API void SQLITE_STDCALL sqlite3_mutex_enter(sqlite3_mutex*); +-SQLITE_API int SQLITE_STDCALL sqlite3_mutex_try(sqlite3_mutex*); +-SQLITE_API void SQLITE_STDCALL sqlite3_mutex_leave(sqlite3_mutex*); ++SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int); ++SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*); ++SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*); ++SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*); ++SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*); + + /* + ** CAPI3REF: Mutex Methods Object +@@ -6449,8 +8217,8 @@ struct sqlite3_mutex_methods { + ** interface should also return 1 when given a NULL pointer. + */ + #ifndef NDEBUG +-SQLITE_API int SQLITE_STDCALL sqlite3_mutex_held(sqlite3_mutex*); +-SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex*); ++SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*); ++SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*); + #endif + + /* +@@ -6469,13 +8237,16 @@ SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex*); + #define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */ + #define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */ + #define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */ +-#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */ ++#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_randomness() */ + #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */ + #define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */ + #define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */ + #define SQLITE_MUTEX_STATIC_APP1 8 /* For use by application */ + #define SQLITE_MUTEX_STATIC_APP2 9 /* For use by application */ + #define SQLITE_MUTEX_STATIC_APP3 10 /* For use by application */ ++#define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */ ++#define SQLITE_MUTEX_STATIC_VFS2 12 /* For use by extension VFS */ ++#define SQLITE_MUTEX_STATIC_VFS3 13 /* For use by application VFS */ + + /* + ** CAPI3REF: Retrieve the mutex for a database connection +@@ -6487,11 +8258,12 @@ SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex*); + ** ^If the [threading mode] is Single-thread or Multi-thread then this + ** routine returns a NULL pointer. + */ +-SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3*); ++SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*); + + /* + ** CAPI3REF: Low-Level Control Of Database Files + ** METHOD: sqlite3 ++** KEYWORDS: {file control} + ** + ** ^The [sqlite3_file_control()] interface makes a direct call to the + ** xFileControl method for the [sqlite3_io_methods] object associated +@@ -6506,11 +8278,18 @@ SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3*); + ** the xFileControl method. ^The return value of the xFileControl + ** method becomes the return value of this routine. + ** +-** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes ++** A few opcodes for [sqlite3_file_control()] are handled directly ++** by the SQLite core and never invoke the ++** sqlite3_io_methods.xFileControl method. ++** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes + ** a pointer to the underlying [sqlite3_file] object to be written into +-** the space pointed to by the 4th parameter. ^The SQLITE_FCNTL_FILE_POINTER +-** case is a short-circuit path which does not actually invoke the +-** underlying sqlite3_io_methods.xFileControl method. ++** the space pointed to by the 4th parameter. The ++** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns ++** the [sqlite3_file] object associated with the journal file instead of ++** the main database. The [SQLITE_FCNTL_VFS_POINTER] opcode returns ++** a pointer to the underlying [sqlite3_vfs] object for the file. ++** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter ++** from the pager. + ** + ** ^If the second parameter (zDbName) does not match the name of any + ** open database file, then SQLITE_ERROR is returned. ^This error +@@ -6520,9 +8299,9 @@ SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3*); + ** an incorrect zDbName and an SQLITE_ERROR return from the underlying + ** xFileControl method. + ** +-** See also: [SQLITE_FCNTL_LOCKSTATE] ++** See also: [file control opcodes] + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); ++SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); + + /* + ** CAPI3REF: Testing Interface +@@ -6541,7 +8320,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_file_control(sqlite3*, const char *zDbName + ** Unlike most of the SQLite API, this function is not guaranteed to + ** operate consistently from one release to the next. + */ +-SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...); ++SQLITE_API int sqlite3_test_control(int op, ...); + + /* + ** CAPI3REF: Testing Interface Operation Codes +@@ -6566,17 +8345,203 @@ SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...); + #define SQLITE_TESTCTRL_ALWAYS 13 + #define SQLITE_TESTCTRL_RESERVE 14 + #define SQLITE_TESTCTRL_OPTIMIZATIONS 15 +-#define SQLITE_TESTCTRL_ISKEYWORD 16 +-#define SQLITE_TESTCTRL_SCRATCHMALLOC 17 ++#define SQLITE_TESTCTRL_ISKEYWORD 16 /* NOT USED */ ++#define SQLITE_TESTCTRL_SCRATCHMALLOC 17 /* NOT USED */ ++#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS 17 + #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18 + #define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */ ++#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD 19 + #define SQLITE_TESTCTRL_NEVER_CORRUPT 20 + #define SQLITE_TESTCTRL_VDBE_COVERAGE 21 + #define SQLITE_TESTCTRL_BYTEORDER 22 + #define SQLITE_TESTCTRL_ISINIT 23 + #define SQLITE_TESTCTRL_SORTER_MMAP 24 + #define SQLITE_TESTCTRL_IMPOSTER 25 +-#define SQLITE_TESTCTRL_LAST 25 ++#define SQLITE_TESTCTRL_PARSER_COVERAGE 26 ++#define SQLITE_TESTCTRL_LAST 26 /* Largest TESTCTRL */ ++ ++/* ++** CAPI3REF: SQL Keyword Checking ++** ++** These routines provide access to the set of SQL language keywords ++** recognized by SQLite. Applications can uses these routines to determine ++** whether or not a specific identifier needs to be escaped (for example, ++** by enclosing in double-quotes) so as not to confuse the parser. ++** ++** The sqlite3_keyword_count() interface returns the number of distinct ++** keywords understood by SQLite. ++** ++** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and ++** makes *Z point to that keyword expressed as UTF8 and writes the number ++** of bytes in the keyword into *L. The string that *Z points to is not ++** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns ++** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z ++** or L are NULL or invalid pointers then calls to ++** sqlite3_keyword_name(N,Z,L) result in undefined behavior. ++** ++** The sqlite3_keyword_check(Z,L) interface checks to see whether or not ++** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero ++** if it is and zero if not. ++** ++** The parser used by SQLite is forgiving. It is often possible to use ++** a keyword as an identifier as long as such use does not result in a ++** parsing ambiguity. For example, the statement ++** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and ++** creates a new table named "BEGIN" with three columns named ++** "REPLACE", "PRAGMA", and "END". Nevertheless, best practice is to avoid ++** using keywords as identifiers. Common techniques used to avoid keyword ++** name collisions include: ++** ++**
++** ++** Note that the number of keywords understood by SQLite can depend on ++** compile-time options. For example, "VACUUM" is not a keyword if ++** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also, ++** new keywords may be added to future releases of SQLite. ++*/ ++SQLITE_API int sqlite3_keyword_count(void); ++SQLITE_API int sqlite3_keyword_name(int,const char**,int*); ++SQLITE_API int sqlite3_keyword_check(const char*,int); ++ ++/* ++** CAPI3REF: Dynamic String Object ++** KEYWORDS: {dynamic string} ++** ++** An instance of the sqlite3_str object contains a dynamically-sized ++** string under construction. ++** ++** The lifecycle of an sqlite3_str object is as follows: ++**- Put all identifier names inside double-quotes. This is the official ++** SQL way to escape identifier names. ++**
- Put identifier names inside [...]. This is not standard SQL, ++** but it is what SQL Server does and so lots of programmers use this ++** technique. ++**
- Begin every identifier with the letter "Z" as no SQL keywords start ++** with "Z". ++**
- Include a digit somewhere in every identifier name. ++**
++**
++*/ ++typedef struct sqlite3_str sqlite3_str; ++ ++/* ++** CAPI3REF: Create A New Dynamic String Object ++** CONSTRUCTOR: sqlite3_str ++** ++** ^The [sqlite3_str_new(D)] interface allocates and initializes ++** a new [sqlite3_str] object. To avoid memory leaks, the object returned by ++** [sqlite3_str_new()] must be freed by a subsequent call to ++** [sqlite3_str_finish(X)]. ++** ++** ^The [sqlite3_str_new(D)] interface always returns a pointer to a ++** valid [sqlite3_str] object, though in the event of an out-of-memory ++** error the returned object might be a special singleton that will ++** silently reject new text, always return SQLITE_NOMEM from ++** [sqlite3_str_errcode()], always return 0 for ++** [sqlite3_str_length()], and always return NULL from ++** [sqlite3_str_finish(X)]. It is always safe to use the value ++** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter ++** to any of the other [sqlite3_str] methods. ++** ++** The D parameter to [sqlite3_str_new(D)] may be NULL. If the ++** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum ++** length of the string contained in the [sqlite3_str] object will be ++** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead ++** of [SQLITE_MAX_LENGTH]. ++*/ ++SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*); ++ ++/* ++** CAPI3REF: Finalize A Dynamic String ++** DESTRUCTOR: sqlite3_str ++** ++** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X ++** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()] ++** that contains the constructed string. The calling application should ++** pass the returned value to [sqlite3_free()] to avoid a memory leak. ++** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any ++** errors were encountered during construction of the string. ^The ++** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the ++** string in [sqlite3_str] object X is zero bytes long. ++*/ ++SQLITE_API char *sqlite3_str_finish(sqlite3_str*); ++ ++/* ++** CAPI3REF: Add Content To A Dynamic String ++** METHOD: sqlite3_str ++** ++** These interfaces add content to an sqlite3_str object previously obtained ++** from [sqlite3_str_new()]. ++** ++** ^The [sqlite3_str_appendf(X,F,...)] and ++** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf] ++** functionality of SQLite to append formatted text onto the end of ++** [sqlite3_str] object X. ++** ++** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S ++** onto the end of the [sqlite3_str] object X. N must be non-negative. ++** S must contain at least N non-zero bytes of content. To append a ++** zero-terminated string in its entirety, use the [sqlite3_str_appendall()] ++** method instead. ++** ++** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of ++** zero-terminated string S onto the end of [sqlite3_str] object X. ++** ++** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the ++** single-byte character C onto the end of [sqlite3_str] object X. ++** ^This method can be used, for example, to add whitespace indentation. ++** ++** ^The [sqlite3_str_reset(X)] method resets the string under construction ++** inside [sqlite3_str] object X back to zero bytes in length. ++** ++** These methods do not return a result code. ^If an error occurs, that fact ++** is recorded in the [sqlite3_str] object and can be recovered by a ++** subsequent call to [sqlite3_str_errcode(X)]. ++*/ ++SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...); ++SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list); ++SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N); ++SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn); ++SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C); ++SQLITE_API void sqlite3_str_reset(sqlite3_str*); ++ ++/* ++** CAPI3REF: Status Of A Dynamic String ++** METHOD: sqlite3_str ++** ++** These interfaces return the current status of an [sqlite3_str] object. ++** ++** ^If any prior errors have occurred while constructing the dynamic string ++** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return ++** an appropriate error code. ^The [sqlite3_str_errcode(X)] method returns ++** [SQLITE_NOMEM] following any out-of-memory error, or ++** [SQLITE_TOOBIG] if the size of the dynamic string exceeds ++** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors. ++** ++** ^The [sqlite3_str_length(X)] method returns the current length, in bytes, ++** of the dynamic string under construction in [sqlite3_str] object X. ++** ^The length returned by [sqlite3_str_length(X)] does not include the ++** zero-termination byte. ++** ++** ^The [sqlite3_str_value(X)] method returns a pointer to the current ++** content of the dynamic string under construction in X. The value ++** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X ++** and might be freed or altered by any subsequent method on the same ++** [sqlite3_str] object. Applications must not used the pointer returned ++** [sqlite3_str_value(X)] after any subsequent method call on the same ++** object. ^Applications may change the content of the string returned ++** by [sqlite3_str_value(X)] as long as they do not write into any bytes ++** outside the range of 0 to [sqlite3_str_length(X)] and do not read or ++** write any byte after any subsequent sqlite3_str method call. ++*/ ++SQLITE_API int sqlite3_str_errcode(sqlite3_str*); ++SQLITE_API int sqlite3_str_length(sqlite3_str*); ++SQLITE_API char *sqlite3_str_value(sqlite3_str*); + + /* + ** CAPI3REF: SQLite Runtime Status +@@ -6604,8 +8569,8 @@ SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...); + ** + ** See also: [sqlite3_db_status()] + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag); +-SQLITE_API int SQLITE_STDCALL sqlite3_status64( ++SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag); ++SQLITE_API int sqlite3_status64( + int op, + sqlite3_int64 *pCurrent, + sqlite3_int64 *pHighwater, +@@ -6625,8 +8590,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status64( + **- ^The sqlite3_str object is created using [sqlite3_str_new()]. ++**
- ^Text is appended to the sqlite3_str object using various ++** methods, such as [sqlite3_str_appendf()]. ++**
- ^The sqlite3_str object is destroyed and the string it created ++** is returned using the [sqlite3_str_finish()] interface. ++**
This parameter is the current amount of memory checked out + ** using [sqlite3_malloc()], either directly or indirectly. The + ** figure includes calls made to [sqlite3_malloc()] by the application +-** and internal memory usage by the SQLite library. Scratch memory +-** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache ++** and internal memory usage by the SQLite library. Auxiliary page-cache + ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in + ** this parameter. The amount returned is the sum of the allocation + ** sizes as reported by the xSize method in [sqlite3_mem_methods]. )^ +@@ -6664,32 +8628,18 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status64( + ** *pHighwater parameter to [sqlite3_status()] is of interest. + ** The value written into the *pCurrent parameter is undefined.)^ + ** +-** [[SQLITE_STATUS_SCRATCH_USED]] ^(SQLITE_STATUS_SCRATCH_USED +-**This parameter returns the number of allocations used out of the +-** [scratch memory allocator] configured using +-** [SQLITE_CONFIG_SCRATCH]. The value returned is in allocations, not +-** in bytes. Since a single thread may only have one scratch allocation +-** outstanding at time, this parameter also reports the number of threads +-** using scratch memory at the same time. )^ ++** [[SQLITE_STATUS_SCRATCH_USED]]SQLITE_STATUS_SCRATCH_USED ++**No longer used. + ** + ** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(SQLITE_STATUS_SCRATCH_OVERFLOW +-**This parameter returns the number of bytes of scratch memory +-** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH] +-** buffer and where forced to overflow to [sqlite3_malloc()]. The values +-** returned include overflows because the requested allocation was too +-** larger (that is, because the requested allocation was larger than the +-** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer +-** slots were available. +-** )^ +-** +-** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(SQLITE_STATUS_SCRATCH_SIZE +-**This parameter records the largest memory allocation request +-** handed to [scratch memory allocator]. Only the value returned in the +-** *pHighwater parameter to [sqlite3_status()] is of interest. +-** The value written into the *pCurrent parameter is undefined. )^ ++**No longer used. ++** ++** [[SQLITE_STATUS_SCRATCH_SIZE]]SQLITE_STATUS_SCRATCH_SIZE ++**No longer used. + ** + ** [[SQLITE_STATUS_PARSER_STACK]] ^(SQLITE_STATUS_PARSER_STACK +-**This parameter records the deepest parser stack. It is only ++** The *pHighwater parameter records the deepest parser stack. ++** The *pCurrent value is undefined. The *pHighwater value is only + ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH]. )^ + ** + ** +@@ -6698,12 +8648,12 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status64( + #define SQLITE_STATUS_MEMORY_USED 0 + #define SQLITE_STATUS_PAGECACHE_USED 1 + #define SQLITE_STATUS_PAGECACHE_OVERFLOW 2 +-#define SQLITE_STATUS_SCRATCH_USED 3 +-#define SQLITE_STATUS_SCRATCH_OVERFLOW 4 ++#define SQLITE_STATUS_SCRATCH_USED 3 /* NOT USED */ ++#define SQLITE_STATUS_SCRATCH_OVERFLOW 4 /* NOT USED */ + #define SQLITE_STATUS_MALLOC_SIZE 5 + #define SQLITE_STATUS_PARSER_STACK 6 + #define SQLITE_STATUS_PAGECACHE_SIZE 7 +-#define SQLITE_STATUS_SCRATCH_SIZE 8 ++#define SQLITE_STATUS_SCRATCH_SIZE 8 /* NOT USED */ + #define SQLITE_STATUS_MALLOC_COUNT 9 + + /* +@@ -6729,7 +8679,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status64( + ** + ** See also: [sqlite3_status()] and [sqlite3_stmt_status()]. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); ++SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); + + /* + ** CAPI3REF: Status Parameters for database connections +@@ -6775,6 +8725,18 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int + ** memory used by all pager caches associated with the database connection.)^ + ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0. + ** ++** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]] ++** ^(SQLITE_DBSTATUS_CACHE_USED_SHARED ++**This parameter is similar to DBSTATUS_CACHE_USED, except that if a ++** pager cache is shared between two or more connections the bytes of heap ++** memory used by that pager cache is divided evenly between the attached ++** connections.)^ In other words, if none of the pager caches associated ++** with the database connection are shared, this request returns the same ++** value as DBSTATUS_CACHE_USED. Or, if one or more or the pager caches are ++** shared, the value returned by this call will be smaller than that returned ++** by DBSTATUS_CACHE_USED. ^The highwater mark associated with ++** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0. ++** + ** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^( SQLITE_DBSTATUS_SCHEMA_USED + **This parameter returns the approximate number of bytes of heap + ** memory used to store the schema for all databases associated +@@ -6814,6 +8776,15 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int + ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0. + ** + ** ++** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(SQLITE_DBSTATUS_CACHE_SPILL ++**This parameter returns the number of dirty cache entries that have ++** been written to disk in the middle of a transaction due to the page ++** cache overflowing. Transactions are more efficient if they are written ++** to disk all at once. When pages spill mid-transaction, that introduces ++** additional overhead. This parameter can be used help identify ++** inefficiencies that can be resolve by increasing the cache size. ++** ++** + ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(SQLITE_DBSTATUS_DEFERRED_FKS + **This parameter returns zero for the current value if and only if + ** all foreign key constraints (deferred or immediate) have been +@@ -6832,7 +8803,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int + #define SQLITE_DBSTATUS_CACHE_MISS 8 + #define SQLITE_DBSTATUS_CACHE_WRITE 9 + #define SQLITE_DBSTATUS_DEFERRED_FKS 10 +-#define SQLITE_DBSTATUS_MAX 10 /* Largest defined DBSTATUS */ ++#define SQLITE_DBSTATUS_CACHE_USED_SHARED 11 ++#define SQLITE_DBSTATUS_CACHE_SPILL 12 ++#define SQLITE_DBSTATUS_MAX 12 /* Largest defined DBSTATUS */ + + + /* +@@ -6859,7 +8832,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int + ** + ** See also: [sqlite3_status()] and [sqlite3_db_status()]. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg); ++SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg); + + /* + ** CAPI3REF: Status Parameters for prepared statements +@@ -6895,6 +8868,24 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_status(sqlite3_stmt*, int op,int rese + ** used as a proxy for the total work done by the prepared statement. + ** If the number of virtual machine operations exceeds 2147483647 + ** then the value returned by this statement status code is undefined. ++** ++** [[SQLITE_STMTSTATUS_REPREPARE]] SQLITE_STMTSTATUS_REPREPARE ++**^This is the number of times that the prepare statement has been ++** automatically regenerated due to schema changes or change to ++** [bound parameters] that might affect the query plan. ++** ++** [[SQLITE_STMTSTATUS_RUN]] SQLITE_STMTSTATUS_RUN ++**^This is the number of times that the prepared statement has ++** been run. A single "run" for the purposes of this counter is one ++** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()]. ++** The counter is incremented on the first [sqlite3_step()] call of each ++** cycle. ++** ++** [[SQLITE_STMTSTATUS_MEMUSED]] SQLITE_STMTSTATUS_MEMUSED ++**^This is the approximate number of bytes of heap memory ++** used to store the prepared statement. ^This value is not actually ++** a counter, and so the resetFlg parameter to sqlite3_stmt_status() ++** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED. + ** + ** + */ +@@ -6902,6 +8893,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_status(sqlite3_stmt*, int op,int rese + #define SQLITE_STMTSTATUS_SORT 2 + #define SQLITE_STMTSTATUS_AUTOINDEX 3 + #define SQLITE_STMTSTATUS_VM_STEP 4 ++#define SQLITE_STMTSTATUS_REPREPARE 5 ++#define SQLITE_STMTSTATUS_RUN 6 ++#define SQLITE_STMTSTATUS_MEMUSED 99 + + /* + ** CAPI3REF: Custom Page Cache Object +@@ -7186,7 +9180,7 @@ typedef struct sqlite3_backup sqlite3_backup; + ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with + ** an error. + ** +-** ^A call to sqlite3_backup_init() will fail, returning SQLITE_ERROR, if ++** ^A call to sqlite3_backup_init() will fail, returning NULL, if + ** there is already a read or read-write transaction open on the + ** destination database. + ** +@@ -7328,16 +9322,16 @@ typedef struct sqlite3_backup sqlite3_backup; + ** same time as another thread is invoking sqlite3_backup_step() it is + ** possible that they return invalid values. + */ +-SQLITE_API sqlite3_backup *SQLITE_STDCALL sqlite3_backup_init( ++SQLITE_API sqlite3_backup *sqlite3_backup_init( + sqlite3 *pDest, /* Destination database handle */ + const char *zDestName, /* Destination database name */ + sqlite3 *pSource, /* Source database handle */ + const char *zSourceName /* Source database name */ + ); +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage); +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_finish(sqlite3_backup *p); +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_remaining(sqlite3_backup *p); +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_pagecount(sqlite3_backup *p); ++SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage); ++SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p); ++SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p); ++SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p); + + /* + ** CAPI3REF: Unlock Notification +@@ -7454,7 +9448,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_backup_pagecount(sqlite3_backup *p); + ** the special "DROP TABLE/INDEX" case, the extended error code is just + ** SQLITE_LOCKED.)^ + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_unlock_notify( ++SQLITE_API int sqlite3_unlock_notify( + sqlite3 *pBlocked, /* Waiting connection */ + void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */ + void *pNotifyArg /* Argument to pass to xNotify */ +@@ -7469,23 +9463,48 @@ SQLITE_API int SQLITE_STDCALL sqlite3_unlock_notify( + ** strings in a case-independent fashion, using the same definition of "case + ** independence" that SQLite uses internally when comparing identifiers. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_stricmp(const char *, const char *); +-SQLITE_API int SQLITE_STDCALL sqlite3_strnicmp(const char *, const char *, int); ++SQLITE_API int sqlite3_stricmp(const char *, const char *); ++SQLITE_API int sqlite3_strnicmp(const char *, const char *, int); + + /* + ** CAPI3REF: String Globbing + * +-** ^The [sqlite3_strglob(P,X)] interface returns zero if string X matches +-** the glob pattern P, and it returns non-zero if string X does not match +-** the glob pattern P. ^The definition of glob pattern matching used in ++** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if ++** string X matches the [GLOB] pattern P. ++** ^The definition of [GLOB] pattern matching used in + ** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the +-** SQL dialect used by SQLite. ^The sqlite3_strglob(P,X) function is case +-** sensitive. ++** SQL dialect understood by SQLite. ^The [sqlite3_strglob(P,X)] function ++** is case sensitive. ++** ++** Note that this routine returns zero on a match and non-zero if the strings ++** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()]. ++** ++** See also: [sqlite3_strlike()]. ++*/ ++SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr); ++ ++/* ++** CAPI3REF: String LIKE Matching ++* ++** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if ++** string X matches the [LIKE] pattern P with escape character E. ++** ^The definition of [LIKE] pattern matching used in ++** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E" ++** operator in the SQL dialect understood by SQLite. ^For "X LIKE P" without ++** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0. ++** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case ++** insensitive - equivalent upper and lower case ASCII characters match ++** one another. ++** ++** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though ++** only ASCII characters are case folded. + ** + ** Note that this routine returns zero on a match and non-zero if the strings + ** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()]. ++** ++** See also: [sqlite3_strglob()]. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_strglob(const char *zGlob, const char *zStr); ++SQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc); + + /* + ** CAPI3REF: Error Logging Interface +@@ -7508,7 +9527,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_strglob(const char *zGlob, const char *zSt + ** a few hundred characters, it will be truncated to the length of the + ** buffer. + */ +-SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...); ++SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...); + + /* + ** CAPI3REF: Write-Ahead Log Commit Hook +@@ -7542,9 +9561,9 @@ SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...) + ** previously registered write-ahead log callback. ^Note that the + ** [sqlite3_wal_autocheckpoint()] interface and the + ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will +-** those overwrite any prior [sqlite3_wal_hook()] settings. ++** overwrite any prior [sqlite3_wal_hook()] settings. + */ +-SQLITE_API void *SQLITE_STDCALL sqlite3_wal_hook( ++SQLITE_API void *sqlite3_wal_hook( + sqlite3*, + int(*)(void *,sqlite3*,const char*,int), + void* +@@ -7579,7 +9598,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_wal_hook( + ** is only necessary if the default setting is found to be suboptimal + ** for a particular application. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int N); ++SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N); + + /* + ** CAPI3REF: Checkpoint a database +@@ -7601,7 +9620,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int N); + ** start a callback but which do not need the full power (and corresponding + ** complication) of [sqlite3_wal_checkpoint_v2()]. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb); ++SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb); + + /* + ** CAPI3REF: Checkpoint a database +@@ -7695,7 +9714,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zD + ** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface + ** from SQL. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint_v2( ++SQLITE_API int sqlite3_wal_checkpoint_v2( + sqlite3 *db, /* Database handle */ + const char *zDb, /* Name of attached database (or NULL) */ + int eMode, /* SQLITE_CHECKPOINT_* value */ +@@ -7731,7 +9750,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint_v2( + ** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].) Further options + ** may be added in the future. + */ +-SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3*, int op, ...); ++SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...); + + /* + ** CAPI3REF: Virtual Table Configuration Options +@@ -7741,6 +9760,7 @@ SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3*, int op, ...); + ** can use to customize and optimize their behavior. + ** + **++** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]] + **
- SQLITE_VTAB_CONSTRAINT_SUPPORT + **
- Calls of the form + ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported, +@@ -7784,7 +9804,41 @@ SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3*, int op, ...); + ** of the SQL statement that triggered the call to the [xUpdate] method of the + ** [virtual table]. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_vtab_on_conflict(sqlite3 *); ++SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *); ++ ++/* ++** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE ++** ++** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn] ++** method of a [virtual table], then it returns true if and only if the ++** column is being fetched as part of an UPDATE operation during which the ++** column value will not change. Applications might use this to substitute ++** a return value that is less expensive to compute and that the corresponding ++** [xUpdate] method understands as a "no-change" value. ++** ++** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that ++** the column is not changed by the UPDATE statement, then the xColumn ++** method can optionally return without setting a result, without calling ++** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces]. ++** In that case, [sqlite3_value_nochange(X)] will return true for the ++** same column in the [xUpdate] method. ++*/ ++SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*); ++ ++/* ++** CAPI3REF: Determine The Collation For a Virtual Table Constraint ++** ++** This function may only be called from within a call to the [xBestIndex] ++** method of a [virtual table]. ++** ++** The first argument must be the sqlite3_index_info object that is the ++** first parameter to the xBestIndex() method. The second argument must be ++** an index into the aConstraint[] array belonging to the sqlite3_index_info ++** structure passed to xBestIndex. This function returns a pointer to a buffer ++** containing the name of the collation sequence for the corresponding ++** constraint. ++*/ ++SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int); + + /* + ** CAPI3REF: Conflict resolution modes +@@ -7889,7 +9943,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_vtab_on_conflict(sqlite3 *); + ** + ** See also: [sqlite3_stmt_scanstatus_reset()] + */ +-SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_stmt_scanstatus( ++SQLITE_API int sqlite3_stmt_scanstatus( + sqlite3_stmt *pStmt, /* Prepared statement for which info desired */ + int idx, /* Index of loop to report on */ + int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */ +@@ -7905,8 +9959,465 @@ SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_stmt_scanstatus( + ** This API is only available if the library is built with pre-processor + ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. + */ +-SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); ++SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); ++ ++/* ++** CAPI3REF: Flush caches to disk mid-transaction ++** ++** ^If a write-transaction is open on [database connection] D when the ++** [sqlite3_db_cacheflush(D)] interface invoked, any dirty ++** pages in the pager-cache that are not currently in use are written out ++** to disk. A dirty page may be in use if a database cursor created by an ++** active SQL statement is reading from it, or if it is page 1 of a database ++** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)] ++** interface flushes caches for all schemas - "main", "temp", and ++** any [attached] databases. ++** ++** ^If this function needs to obtain extra database locks before dirty pages ++** can be flushed to disk, it does so. ^If those locks cannot be obtained ++** immediately and there is a busy-handler callback configured, it is invoked ++** in the usual manner. ^If the required lock still cannot be obtained, then ++** the database is skipped and an attempt made to flush any dirty pages ++** belonging to the next (if any) database. ^If any databases are skipped ++** because locks cannot be obtained, but no other error occurs, this ++** function returns SQLITE_BUSY. ++** ++** ^If any other error occurs while flushing dirty pages to disk (for ++** example an IO error or out-of-memory condition), then processing is ++** abandoned and an SQLite [error code] is returned to the caller immediately. ++** ++** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK. ++** ++** ^This function does not set the database handle error code or message ++** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions. ++*/ ++SQLITE_API int sqlite3_db_cacheflush(sqlite3*); ++ ++/* ++** CAPI3REF: The pre-update hook. ++** ++** ^These interfaces are only available if SQLite is compiled using the ++** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option. ++** ++** ^The [sqlite3_preupdate_hook()] interface registers a callback function ++** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation ++** on a database table. ++** ^At most one preupdate hook may be registered at a time on a single ++** [database connection]; each call to [sqlite3_preupdate_hook()] overrides ++** the previous setting. ++** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()] ++** with a NULL pointer as the second parameter. ++** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as ++** the first parameter to callbacks. ++** ++** ^The preupdate hook only fires for changes to real database tables; the ++** preupdate hook is not invoked for changes to [virtual tables] or to ++** system tables like sqlite_master or sqlite_stat1. ++** ++** ^The second parameter to the preupdate callback is a pointer to ++** the [database connection] that registered the preupdate hook. ++** ^The third parameter to the preupdate callback is one of the constants ++** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the ++** kind of update operation that is about to occur. ++** ^(The fourth parameter to the preupdate callback is the name of the ++** database within the database connection that is being modified. This ++** will be "main" for the main database or "temp" for TEMP tables or ++** the name given after the AS keyword in the [ATTACH] statement for attached ++** databases.)^ ++** ^The fifth parameter to the preupdate callback is the name of the ++** table that is being modified. ++** ++** For an UPDATE or DELETE operation on a [rowid table], the sixth ++** parameter passed to the preupdate callback is the initial [rowid] of the ++** row being modified or deleted. For an INSERT operation on a rowid table, ++** or any operation on a WITHOUT ROWID table, the value of the sixth ++** parameter is undefined. For an INSERT or UPDATE on a rowid table the ++** seventh parameter is the final rowid value of the row being inserted ++** or updated. The value of the seventh parameter passed to the callback ++** function is not defined for operations on WITHOUT ROWID tables, or for ++** INSERT operations on rowid tables. ++** ++** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()], ++** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces ++** provide additional information about a preupdate event. These routines ++** may only be called from within a preupdate callback. Invoking any of ++** these routines from outside of a preupdate callback or with a ++** [database connection] pointer that is different from the one supplied ++** to the preupdate callback results in undefined and probably undesirable ++** behavior. ++** ++** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns ++** in the row that is being inserted, updated, or deleted. ++** ++** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to ++** a [protected sqlite3_value] that contains the value of the Nth column of ++** the table row before it is updated. The N parameter must be between 0 ++** and one less than the number of columns or the behavior will be ++** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE ++** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the ++** behavior is undefined. The [sqlite3_value] that P points to ++** will be destroyed when the preupdate callback returns. ++** ++** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to ++** a [protected sqlite3_value] that contains the value of the Nth column of ++** the table row after it is updated. The N parameter must be between 0 ++** and one less than the number of columns or the behavior will be ++** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE ++** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the ++** behavior is undefined. The [sqlite3_value] that P points to ++** will be destroyed when the preupdate callback returns. ++** ++** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate ++** callback was invoked as a result of a direct insert, update, or delete ++** operation; or 1 for inserts, updates, or deletes invoked by top-level ++** triggers; or 2 for changes resulting from triggers called by top-level ++** triggers; and so forth. ++** ++** See also: [sqlite3_update_hook()] ++*/ ++#if defined(SQLITE_ENABLE_PREUPDATE_HOOK) ++SQLITE_API void *sqlite3_preupdate_hook( ++ sqlite3 *db, ++ void(*xPreUpdate)( ++ void *pCtx, /* Copy of third arg to preupdate_hook() */ ++ sqlite3 *db, /* Database handle */ ++ int op, /* SQLITE_UPDATE, DELETE or INSERT */ ++ char const *zDb, /* Database name */ ++ char const *zName, /* Table name */ ++ sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */ ++ sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */ ++ ), ++ void* ++); ++SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **); ++SQLITE_API int sqlite3_preupdate_count(sqlite3 *); ++SQLITE_API int sqlite3_preupdate_depth(sqlite3 *); ++SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **); ++#endif ++ ++/* ++** CAPI3REF: Low-level system error code ++** ++** ^Attempt to return the underlying operating system error code or error ++** number that caused the most recent I/O error or failure to open a file. ++** The return value is OS-dependent. For example, on unix systems, after ++** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be ++** called to get back the underlying "errno" that caused the problem, such ++** as ENOSPC, EAUTH, EISDIR, and so forth. ++*/ ++SQLITE_API int sqlite3_system_errno(sqlite3*); ++ ++/* ++** CAPI3REF: Database Snapshot ++** KEYWORDS: {snapshot} {sqlite3_snapshot} ++** ++** An instance of the snapshot object records the state of a [WAL mode] ++** database for some specific point in history. ++** ++** In [WAL mode], multiple [database connections] that are open on the ++** same database file can each be reading a different historical version ++** of the database file. When a [database connection] begins a read ++** transaction, that connection sees an unchanging copy of the database ++** as it existed for the point in time when the transaction first started. ++** Subsequent changes to the database from other connections are not seen ++** by the reader until a new read transaction is started. ++** ++** The sqlite3_snapshot object records state information about an historical ++** version of the database file so that it is possible to later open a new read ++** transaction that sees that historical version of the database rather than ++** the most recent version. ++*/ ++typedef struct sqlite3_snapshot { ++ unsigned char hidden[48]; ++} sqlite3_snapshot; ++ ++/* ++** CAPI3REF: Record A Database Snapshot ++** CONSTRUCTOR: sqlite3_snapshot ++** ++** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a ++** new [sqlite3_snapshot] object that records the current state of ++** schema S in database connection D. ^On success, the ++** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly ++** created [sqlite3_snapshot] object into *P and returns SQLITE_OK. ++** If there is not already a read-transaction open on schema S when ++** this function is called, one is opened automatically. ++** ++** The following must be true for this function to succeed. If any of ++** the following statements are false when sqlite3_snapshot_get() is ++** called, SQLITE_ERROR is returned. The final value of *P is undefined ++** in this case. ++** ++**
++**
++** ++** This function may also return SQLITE_NOMEM. If it is called with the ++** database handle in autocommit mode but fails for some other reason, ++** whether or not a read transaction is opened on schema S is undefined. ++** ++** The [sqlite3_snapshot] object returned from a successful call to ++** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()] ++** to avoid a memory leak. ++** ++** The [sqlite3_snapshot_get()] interface is only available when the ++** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used. ++*/ ++SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get( ++ sqlite3 *db, ++ const char *zSchema, ++ sqlite3_snapshot **ppSnapshot ++); ++ ++/* ++** CAPI3REF: Start a read transaction on an historical snapshot ++** METHOD: sqlite3_snapshot ++** ++** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read ++** transaction or upgrades an existing one for schema S of ++** [database connection] D such that the read transaction refers to ++** historical [snapshot] P, rather than the most recent change to the ++** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK ++** on success or an appropriate [error code] if it fails. ++** ++** ^In order to succeed, the database connection must not be in ++** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there ++** is already a read transaction open on schema S, then the database handle ++** must have no active statements (SELECT statements that have been passed ++** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()). ++** SQLITE_ERROR is returned if either of these conditions is violated, or ++** if schema S does not exist, or if the snapshot object is invalid. ++** ++** ^A call to sqlite3_snapshot_open() will fail to open if the specified ++** snapshot has been overwritten by a [checkpoint]. In this case ++** SQLITE_ERROR_SNAPSHOT is returned. ++** ++** If there is already a read transaction open when this function is ++** invoked, then the same read transaction remains open (on the same ++** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT ++** is returned. If another error code - for example SQLITE_PROTOCOL or an ++** SQLITE_IOERR error code - is returned, then the final state of the ++** read transaction is undefined. If SQLITE_OK is returned, then the ++** read transaction is now open on database snapshot P. ++** ++** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the ++** database connection D does not know that the database file for ++** schema S is in [WAL mode]. A database connection might not know ++** that the database file is in [WAL mode] if there has been no prior ++** I/O on that database connection, or if the database entered [WAL mode] ++** after the most recent I/O on the database connection.)^ ++** (Hint: Run "[PRAGMA application_id]" against a newly opened ++** database connection in order to make it ready to use snapshots.) ++** ++** The [sqlite3_snapshot_open()] interface is only available when the ++** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used. ++*/ ++SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open( ++ sqlite3 *db, ++ const char *zSchema, ++ sqlite3_snapshot *pSnapshot ++); ++ ++/* ++** CAPI3REF: Destroy a snapshot ++** DESTRUCTOR: sqlite3_snapshot ++** ++** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P. ++** The application must eventually free every [sqlite3_snapshot] object ++** using this routine to avoid a memory leak. ++** ++** The [sqlite3_snapshot_free()] interface is only available when the ++** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used. ++*/ ++SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*); ++ ++/* ++** CAPI3REF: Compare the ages of two snapshot handles. ++** METHOD: sqlite3_snapshot ++** ++** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages ++** of two valid snapshot handles. ++** ++** If the two snapshot handles are not associated with the same database ++** file, the result of the comparison is undefined. ++** ++** Additionally, the result of the comparison is only valid if both of the ++** snapshot handles were obtained by calling sqlite3_snapshot_get() since the ++** last time the wal file was deleted. The wal file is deleted when the ++** database is changed back to rollback mode or when the number of database ++** clients drops to zero. If either snapshot handle was obtained before the ++** wal file was last deleted, the value returned by this function ++** is undefined. ++** ++** Otherwise, this API returns a negative value if P1 refers to an older ++** snapshot than P2, zero if the two handles refer to the same database ++** snapshot, and a positive value if P1 is a newer snapshot than P2. ++** ++** This interface is only available if SQLite is compiled with the ++** [SQLITE_ENABLE_SNAPSHOT] option. ++*/ ++SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp( ++ sqlite3_snapshot *p1, ++ sqlite3_snapshot *p2 ++); ++ ++/* ++** CAPI3REF: Recover snapshots from a wal file ++** METHOD: sqlite3_snapshot ++** ++** If a [WAL file] remains on disk after all database connections close ++** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control] ++** or because the last process to have the database opened exited without ++** calling [sqlite3_close()]) and a new connection is subsequently opened ++** on that database and [WAL file], the [sqlite3_snapshot_open()] interface ++** will only be able to open the last transaction added to the WAL file ++** even though the WAL file contains other valid transactions. ++** ++** This function attempts to scan the WAL file associated with database zDb ++** of database handle db and make all valid snapshots available to ++** sqlite3_snapshot_open(). It is an error if there is already a read ++** transaction open on the database, or if the database is not a WAL mode ++** database. ++** ++** SQLITE_OK is returned if successful, or an SQLite error code otherwise. ++** ++** This interface is only available if SQLite is compiled with the ++** [SQLITE_ENABLE_SNAPSHOT] option. ++*/ ++SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb); ++ ++/* ++** CAPI3REF: Serialize a database ++** ++** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory ++** that is a serialization of the S database on [database connection] D. ++** If P is not a NULL pointer, then the size of the database in bytes ++** is written into *P. ++** ++** For an ordinary on-disk database file, the serialization is just a ++** copy of the disk file. For an in-memory database or a "TEMP" database, ++** the serialization is the same sequence of bytes which would be written ++** to disk if that database where backed up to disk. ++** ++** The usual case is that sqlite3_serialize() copies the serialization of ++** the database into memory obtained from [sqlite3_malloc64()] and returns ++** a pointer to that memory. The caller is responsible for freeing the ++** returned value to avoid a memory leak. However, if the F argument ++** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations ++** are made, and the sqlite3_serialize() function will return a pointer ++** to the contiguous memory representation of the database that SQLite ++** is currently using for that database, or NULL if the no such contiguous ++** memory representation of the database exists. A contiguous memory ++** representation of the database will usually only exist if there has ++** been a prior call to [sqlite3_deserialize(D,S,...)] with the same ++** values of D and S. ++** The size of the database is written into *P even if the ++** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy ++** of the database exists. ++** ++** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the ++** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory ++** allocation error occurs. ++** ++** This interface is only available if SQLite is compiled with the ++** [SQLITE_ENABLE_DESERIALIZE] option. ++*/ ++SQLITE_API unsigned char *sqlite3_serialize( ++ sqlite3 *db, /* The database connection */ ++ const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */ ++ sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */ ++ unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */ ++); ++ ++/* ++** CAPI3REF: Flags for sqlite3_serialize ++** ++** Zero or more of the following constants can be OR-ed together for ++** the F argument to [sqlite3_serialize(D,S,P,F)]. ++** ++** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return ++** a pointer to contiguous in-memory database that it is currently using, ++** without making a copy of the database. If SQLite is not currently using ++** a contiguous in-memory database, then this option causes ++** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be ++** using a contiguous in-memory database if it has been initialized by a ++** prior call to [sqlite3_deserialize()]. ++*/ ++#define SQLITE_SERIALIZE_NOCOPY 0x001 /* Do no memory allocations */ ++ ++/* ++** CAPI3REF: Deserialize a database ++** ++** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the ++** [database connection] D to disconnect from database S and then ++** reopen S as an in-memory database based on the serialization contained ++** in P. The serialized database P is N bytes in size. M is the size of ++** the buffer P, which might be larger than N. If M is larger than N, and ++** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is ++** permitted to add content to the in-memory database as long as the total ++** size does not exceed M bytes. ++** ++** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will ++** invoke sqlite3_free() on the serialization buffer when the database ++** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then ++** SQLite will try to increase the buffer size using sqlite3_realloc64() ++** if writes on the database cause it to grow larger than M bytes. ++** ++** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the ++** database is currently in a read transaction or is involved in a backup ++** operation. ++** ++** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the ++** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then ++** [sqlite3_free()] is invoked on argument P prior to returning. ++** ++** This interface is only available if SQLite is compiled with the ++** [SQLITE_ENABLE_DESERIALIZE] option. ++*/ ++SQLITE_API int sqlite3_deserialize( ++ sqlite3 *db, /* The database connection */ ++ const char *zSchema, /* Which DB to reopen with the deserialization */ ++ unsigned char *pData, /* The serialized database content */ ++ sqlite3_int64 szDb, /* Number bytes in the deserialization */ ++ sqlite3_int64 szBuf, /* Total size of buffer pData[] */ ++ unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */ ++); + ++/* ++** CAPI3REF: Flags for sqlite3_deserialize() ++** ++** The following are allowed values for 6th argument (the F argument) to ++** the [sqlite3_deserialize(D,S,P,N,M,F)] interface. ++** ++** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization ++** in the P argument is held in memory obtained from [sqlite3_malloc64()] ++** and that SQLite should take ownership of this memory and automatically ++** free it when it has finished using it. Without this flag, the caller ++** is responsible for freeing any dynamically allocated memory. ++** ++** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to ++** grow the size of the database using calls to [sqlite3_realloc64()]. This ++** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used. ++** Without this flag, the deserialized database cannot increase in size beyond ++** the number of bytes specified by the M parameter. ++** ++** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database ++** should be treated as read-only. ++*/ ++#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */ ++#define SQLITE_DESERIALIZE_RESIZEABLE 2 /* Resize using sqlite3_realloc64() */ ++#define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */ + + /* + ** Undo the hack that converts floating point types to integer for +@@ -7919,8 +10430,9 @@ SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset + #if 0 + } /* End of the 'extern "C"' block */ + #endif +-#endif /* _SQLITE3_H_ */ ++#endif /* SQLITE3_H */ + ++/******** Begin file sqlite3rtree.h *********/ + /* + ** 2010 August 30 + ** +@@ -7960,7 +10472,7 @@ typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info; + ** + ** SELECT ... FROM- The database handle must not be in [autocommit mode]. ++** ++**
- Schema S of [database connection] D must be a [WAL mode] database. ++** ++**
- There must not be a write transaction open on schema S of database ++** connection D. ++** ++**
- One or more transactions must have been written to the current wal ++** file since it was created on disk (by any connection). This means ++** that a snapshot cannot be taken on a wal mode database with no wal ++** file immediately after it is first opened. At least one transaction ++** must be written to it first. ++**
WHERE MATCH $zGeom(... params ...) + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_rtree_geometry_callback( ++SQLITE_API int sqlite3_rtree_geometry_callback( + sqlite3 *db, + const char *zGeom, + int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*), +@@ -7986,7 +10498,7 @@ struct sqlite3_rtree_geometry { + ** + ** SELECT ... FROM WHERE MATCH $zQueryFunc(... params ...) + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_rtree_query_callback( ++SQLITE_API int sqlite3_rtree_query_callback( + sqlite3 *db, + const char *zQueryFunc, + int (*xQueryFunc)(sqlite3_rtree_query_info*), +@@ -8018,8 +10530,10 @@ struct sqlite3_rtree_query_info { + sqlite3_int64 iRowid; /* Rowid for current entry */ + sqlite3_rtree_dbl rParentScore; /* Score of parent node */ + int eParentWithin; /* Visibility of parent node */ +- int eWithin; /* OUT: Visiblity */ ++ int eWithin; /* OUT: Visibility */ + sqlite3_rtree_dbl rScore; /* OUT: Write the score here */ ++ /* The following fields are only available in 3.8.11 and later */ ++ sqlite3_value **apSqlParam; /* Original SQL values of parameters */ + }; + + /* +@@ -8036,6 +10550,2246 @@ struct sqlite3_rtree_query_info { + + #endif /* ifndef _SQLITE3RTREE_H_ */ + ++/******** End of sqlite3rtree.h *********/ ++/******** Begin file sqlite3session.h *********/ ++ ++#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) ++#define __SQLITESESSION_H_ 1 ++ ++/* ++** Make sure we can call this stuff from C++. ++*/ ++#if 0 ++extern "C" { ++#endif ++ ++ ++/* ++** CAPI3REF: Session Object Handle ++** ++** An instance of this object is a [session] that can be used to ++** record changes to a database. ++*/ ++typedef struct sqlite3_session sqlite3_session; ++ ++/* ++** CAPI3REF: Changeset Iterator Handle ++** ++** An instance of this object acts as a cursor for iterating ++** over the elements of a [changeset] or [patchset]. ++*/ ++typedef struct sqlite3_changeset_iter sqlite3_changeset_iter; ++ ++/* ++** CAPI3REF: Create A New Session Object ++** CONSTRUCTOR: sqlite3_session ++** ++** Create a new session object attached to database handle db. If successful, ++** a pointer to the new object is written to *ppSession and SQLITE_OK is ++** returned. If an error occurs, *ppSession is set to NULL and an SQLite ++** error code (e.g. SQLITE_NOMEM) is returned. ++** ++** It is possible to create multiple session objects attached to a single ++** database handle. ++** ++** Session objects created using this function should be deleted using the ++** [sqlite3session_delete()] function before the database handle that they ++** are attached to is itself closed. If the database handle is closed before ++** the session object is deleted, then the results of calling any session ++** module function, including [sqlite3session_delete()] on the session object ++** are undefined. ++** ++** Because the session module uses the [sqlite3_preupdate_hook()] API, it ++** is not possible for an application to register a pre-update hook on a ++** database handle that has one or more session objects attached. Nor is ++** it possible to create a session object attached to a database handle for ++** which a pre-update hook is already defined. The results of attempting ++** either of these things are undefined. ++** ++** The session object will be used to create changesets for tables in ++** database zDb, where zDb is either "main", or "temp", or the name of an ++** attached database. It is not an error if database zDb is not attached ++** to the database when the session object is created. ++*/ ++SQLITE_API int sqlite3session_create( ++ sqlite3 *db, /* Database handle */ ++ const char *zDb, /* Name of db (e.g. "main") */ ++ sqlite3_session **ppSession /* OUT: New session object */ ++); ++ ++/* ++** CAPI3REF: Delete A Session Object ++** DESTRUCTOR: sqlite3_session ++** ++** Delete a session object previously allocated using ++** [sqlite3session_create()]. Once a session object has been deleted, the ++** results of attempting to use pSession with any other session module ++** function are undefined. ++** ++** Session objects must be deleted before the database handle to which they ++** are attached is closed. Refer to the documentation for ++** [sqlite3session_create()] for details. ++*/ ++SQLITE_API void sqlite3session_delete(sqlite3_session *pSession); ++ ++ ++/* ++** CAPI3REF: Enable Or Disable A Session Object ++** METHOD: sqlite3_session ++** ++** Enable or disable the recording of changes by a session object. When ++** enabled, a session object records changes made to the database. When ++** disabled - it does not. A newly created session object is enabled. ++** Refer to the documentation for [sqlite3session_changeset()] for further ++** details regarding how enabling and disabling a session object affects ++** the eventual changesets. ++** ++** Passing zero to this function disables the session. Passing a value ++** greater than zero enables it. Passing a value less than zero is a ++** no-op, and may be used to query the current state of the session. ++** ++** The return value indicates the final state of the session object: 0 if ++** the session is disabled, or 1 if it is enabled. ++*/ ++SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable); ++ ++/* ++** CAPI3REF: Set Or Clear the Indirect Change Flag ++** METHOD: sqlite3_session ++** ++** Each change recorded by a session object is marked as either direct or ++** indirect. A change is marked as indirect if either: ++** ++** ++**
++** ++** If a single row is affected by more than one operation within a session, ++** then the change is considered indirect if all operations meet the criteria ++** for an indirect change above, or direct otherwise. ++** ++** This function is used to set, clear or query the session object indirect ++** flag. If the second argument passed to this function is zero, then the ++** indirect flag is cleared. If it is greater than zero, the indirect flag ++** is set. Passing a value less than zero does not modify the current value ++** of the indirect flag, and may be used to query the current state of the ++** indirect flag for the specified session object. ++** ++** The return value indicates the final state of the indirect flag: 0 if ++** it is clear, or 1 if it is set. ++*/ ++SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect); ++ ++/* ++** CAPI3REF: Attach A Table To A Session Object ++** METHOD: sqlite3_session ++** ++** If argument zTab is not NULL, then it is the name of a table to attach ++** to the session object passed as the first argument. All subsequent changes ++** made to the table while the session object is enabled will be recorded. See ++** documentation for [sqlite3session_changeset()] for further details. ++** ++** Or, if argument zTab is NULL, then changes are recorded for all tables ++** in the database. If additional tables are added to the database (by ++** executing "CREATE TABLE" statements) after this call is made, changes for ++** the new tables are also recorded. ++** ++** Changes can only be recorded for tables that have a PRIMARY KEY explicitly ++** defined as part of their CREATE TABLE statement. It does not matter if the ++** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY ++** KEY may consist of a single column, or may be a composite key. ++** ++** It is not an error if the named table does not exist in the database. Nor ++** is it an error if the named table does not have a PRIMARY KEY. However, ++** no changes will be recorded in either of these scenarios. ++** ++** Changes are not recorded for individual rows that have NULL values stored ++** in one or more of their PRIMARY KEY columns. ++** ++** SQLITE_OK is returned if the call completes without error. Or, if an error ++** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned. ++** ++**- The session object "indirect" flag is set when the change is ++** made, or ++**
- The change is made by an SQL trigger or foreign key action ++** instead of directly as a result of a users SQL statement. ++**
Special sqlite_stat1 Handling
++** ++** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to ++** some of the rules above. In SQLite, the schema of sqlite_stat1 is: ++**++** CREATE TABLE sqlite_stat1(tbl,idx,stat) ++**++** ++** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are ++** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes ++** are recorded for rows for which (idx IS NULL) is true. However, for such ++** rows a zero-length blob (SQL value X'') is stored in the changeset or ++** patchset instead of a NULL value. This allows such changesets to be ++** manipulated by legacy implementations of sqlite3changeset_invert(), ++** concat() and similar. ++** ++** The sqlite3changeset_apply() function automatically converts the ++** zero-length blob back to a NULL value when updating the sqlite_stat1 ++** table. However, if the application calls sqlite3changeset_new(), ++** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset ++** iterator directly (including on a changeset iterator passed to a ++** conflict-handler callback) then the X'' value is returned. The application ++** must translate X'' to NULL itself if required. ++** ++** Legacy (older than 3.22.0) versions of the sessions module cannot capture ++** changes made to the sqlite_stat1 table. Legacy versions of the ++** sqlite3changeset_apply() function silently ignore any modifications to the ++** sqlite_stat1 table that are part of a changeset or patchset. ++*/ ++SQLITE_API int sqlite3session_attach( ++ sqlite3_session *pSession, /* Session object */ ++ const char *zTab /* Table name */ ++); ++ ++/* ++** CAPI3REF: Set a table filter on a Session Object. ++** METHOD: sqlite3_session ++** ++** The second argument (xFilter) is the "filter callback". For changes to rows ++** in tables that are not attached to the Session object, the filter is called ++** to determine whether changes to the table's rows should be tracked or not. ++** If xFilter returns 0, changes is not tracked. Note that once a table is ++** attached, xFilter will not be called again. ++*/ ++SQLITE_API void sqlite3session_table_filter( ++ sqlite3_session *pSession, /* Session object */ ++ int(*xFilter)( ++ void *pCtx, /* Copy of third arg to _filter_table() */ ++ const char *zTab /* Table name */ ++ ), ++ void *pCtx /* First argument passed to xFilter */ ++); ++ ++/* ++** CAPI3REF: Generate A Changeset From A Session Object ++** METHOD: sqlite3_session ++** ++** Obtain a changeset containing changes to the tables attached to the ++** session object passed as the first argument. If successful, ++** set *ppChangeset to point to a buffer containing the changeset ++** and *pnChangeset to the size of the changeset in bytes before returning ++** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to ++** zero and return an SQLite error code. ++** ++** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes, ++** each representing a change to a single row of an attached table. An INSERT ++** change contains the values of each field of a new database row. A DELETE ++** contains the original values of each field of a deleted database row. An ++** UPDATE change contains the original values of each field of an updated ++** database row along with the updated values for each updated non-primary-key ++** column. It is not possible for an UPDATE change to represent a change that ++** modifies the values of primary key columns. If such a change is made, it ++** is represented in a changeset as a DELETE followed by an INSERT. ++** ++** Changes are not recorded for rows that have NULL values stored in one or ++** more of their PRIMARY KEY columns. If such a row is inserted or deleted, ++** no corresponding change is present in the changesets returned by this ++** function. If an existing row with one or more NULL values stored in ++** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL, ++** only an INSERT is appears in the changeset. Similarly, if an existing row ++** with non-NULL PRIMARY KEY values is updated so that one or more of its ++** PRIMARY KEY columns are set to NULL, the resulting changeset contains a ++** DELETE change only. ++** ++** The contents of a changeset may be traversed using an iterator created ++** using the [sqlite3changeset_start()] API. A changeset may be applied to ++** a database with a compatible schema using the [sqlite3changeset_apply()] ++** API. ++** ++** Within a changeset generated by this function, all changes related to a ++** single table are grouped together. In other words, when iterating through ++** a changeset or when applying a changeset to a database, all changes related ++** to a single table are processed before moving on to the next table. Tables ++** are sorted in the same order in which they were attached (or auto-attached) ++** to the sqlite3_session object. The order in which the changes related to ++** a single table are stored is undefined. ++** ++** Following a successful call to this function, it is the responsibility of ++** the caller to eventually free the buffer that *ppChangeset points to using ++** [sqlite3_free()]. ++** ++**Changeset Generation
++** ++** Once a table has been attached to a session object, the session object ++** records the primary key values of all new rows inserted into the table. ++** It also records the original primary key and other column values of any ++** deleted or updated rows. For each unique primary key value, data is only ++** recorded once - the first time a row with said primary key is inserted, ++** updated or deleted in the lifetime of the session. ++** ++** There is one exception to the previous paragraph: when a row is inserted, ++** updated or deleted, if one or more of its primary key columns contain a ++** NULL value, no record of the change is made. ++** ++** The session object therefore accumulates two types of records - those ++** that consist of primary key values only (created when the user inserts ++** a new record) and those that consist of the primary key values and the ++** original values of other table columns (created when the users deletes ++** or updates a record). ++** ++** When this function is called, the requested changeset is created using ++** both the accumulated records and the current contents of the database ++** file. Specifically: ++** ++**++**
++** ++** This means, amongst other things, that if a row is inserted and then later ++** deleted while a session object is active, neither the insert nor the delete ++** will be present in the changeset. Or if a row is deleted and then later a ++** row with the same primary key values inserted while a session object is ++** active, the resulting changeset will contain an UPDATE change instead of ++** a DELETE and an INSERT. ++** ++** When a session object is disabled (see the [sqlite3session_enable()] API), ++** it does not accumulate records when rows are inserted, updated or deleted. ++** This may appear to have some counter-intuitive effects if a single row ++** is written to more than once during a session. For example, if a row ++** is inserted while a session object is enabled, then later deleted while ++** the same session object is disabled, no INSERT record will appear in the ++** changeset, even though the delete took place while the session was disabled. ++** Or, if one field of a row is updated while a session is disabled, and ++** another field of the same row is updated while the session is enabled, the ++** resulting changeset will contain an UPDATE change that updates both fields. ++*/ ++SQLITE_API int sqlite3session_changeset( ++ sqlite3_session *pSession, /* Session object */ ++ int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */ ++ void **ppChangeset /* OUT: Buffer containing changeset */ ++); ++ ++/* ++** CAPI3REF: Load The Difference Between Tables Into A Session ++** METHOD: sqlite3_session ++** ++** If it is not already attached to the session object passed as the first ++** argument, this function attaches table zTbl in the same manner as the ++** [sqlite3session_attach()] function. If zTbl does not exist, or if it ++** does not have a primary key, this function is a no-op (but does not return ++** an error). ++** ++** Argument zFromDb must be the name of a database ("main", "temp" etc.) ++** attached to the same database handle as the session object that contains ++** a table compatible with the table attached to the session by this function. ++** A table is considered compatible if it: ++** ++**- For each record generated by an insert, the database is queried ++** for a row with a matching primary key. If one is found, an INSERT ++** change is added to the changeset. If no such row is found, no change ++** is added to the changeset. ++** ++**
- For each record generated by an update or delete, the database is ++** queried for a row with a matching primary key. If such a row is ++** found and one or more of the non-primary key fields have been ++** modified from their original values, an UPDATE change is added to ++** the changeset. Or, if no such row is found in the table, a DELETE ++** change is added to the changeset. If there is a row with a matching ++** primary key in the database, but all fields contain their original ++** values, no change is added to the changeset. ++**
++**
++** ++** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables ++** are compatible but do not have any PRIMARY KEY columns, it is not an error ++** but no changes are added to the session object. As with other session ++** APIs, tables without PRIMARY KEYs are simply ignored. ++** ++** This function adds a set of changes to the session object that could be ++** used to update the table in database zFrom (call this the "from-table") ++** so that its content is the same as the table attached to the session ++** object (call this the "to-table"). Specifically: ++** ++**- Has the same name, ++**
- Has the same set of columns declared in the same order, and ++**
- Has the same PRIMARY KEY definition. ++**
++**
++** ++** To clarify, if this function is called and then a changeset constructed ++** using [sqlite3session_changeset()], then after applying that changeset to ++** database zFrom the contents of the two compatible tables would be ++** identical. ++** ++** It an error if database zFrom does not exist or does not contain the ++** required compatible table. ++** ++** If the operation successful, SQLITE_OK is returned. Otherwise, an SQLite ++** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg ++** may be set to point to a buffer containing an English language error ++** message. It is the responsibility of the caller to free this buffer using ++** sqlite3_free(). ++*/ ++SQLITE_API int sqlite3session_diff( ++ sqlite3_session *pSession, ++ const char *zFromDb, ++ const char *zTbl, ++ char **pzErrMsg ++); ++ ++ ++/* ++** CAPI3REF: Generate A Patchset From A Session Object ++** METHOD: sqlite3_session ++** ++** The differences between a patchset and a changeset are that: ++** ++**- For each row (primary key) that exists in the to-table but not in ++** the from-table, an INSERT record is added to the session object. ++** ++**
- For each row (primary key) that exists in the to-table but not in ++** the from-table, a DELETE record is added to the session object. ++** ++**
- For each row (primary key) that exists in both tables, but features ++** different non-PK values in each, an UPDATE record is added to the ++** session. ++**
++**
++** ++** A patchset blob may be used with up to date versions of all ++** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(), ++** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly, ++** attempting to use a patchset blob with old versions of the ++** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error. ++** ++** Because the non-primary key "old.*" fields are omitted, no ++** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset ++** is passed to the sqlite3changeset_apply() API. Other conflict types work ++** in the same way as for changesets. ++** ++** Changes within a patchset are ordered in the same way as for changesets ++** generated by the sqlite3session_changeset() function (i.e. all changes for ++** a single table are grouped together, tables appear in the order in which ++** they were attached to the session object). ++*/ ++SQLITE_API int sqlite3session_patchset( ++ sqlite3_session *pSession, /* Session object */ ++ int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */ ++ void **ppPatchset /* OUT: Buffer containing patchset */ ++); ++ ++/* ++** CAPI3REF: Test if a changeset has recorded any changes. ++** ++** Return non-zero if no changes to attached tables have been recorded by ++** the session object passed as the first argument. Otherwise, if one or ++** more changes have been recorded, return zero. ++** ++** Even if this function returns zero, it is possible that calling ++** [sqlite3session_changeset()] on the session handle may still return a ++** changeset that contains no changes. This can happen when a row in ++** an attached table is modified and then later on the original values ++** are restored. However, if this function returns non-zero, then it is ++** guaranteed that a call to sqlite3session_changeset() will return a ++** changeset containing zero changes. ++*/ ++SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession); ++ ++/* ++** CAPI3REF: Create An Iterator To Traverse A Changeset ++** CONSTRUCTOR: sqlite3_changeset_iter ++** ++** Create an iterator used to iterate through the contents of a changeset. ++** If successful, *pp is set to point to the iterator handle and SQLITE_OK ++** is returned. Otherwise, if an error occurs, *pp is set to zero and an ++** SQLite error code is returned. ++** ++** The following functions can be used to advance and query a changeset ++** iterator created by this function: ++** ++**- DELETE records consist of the primary key fields only. The ++** original values of other fields are omitted. ++**
- The original values of any modified fields are omitted from ++** UPDATE records. ++**
++**
++** ++** It is the responsibility of the caller to eventually destroy the iterator ++** by passing it to [sqlite3changeset_finalize()]. The buffer containing the ++** changeset (pChangeset) must remain valid until after the iterator is ++** destroyed. ++** ++** Assuming the changeset blob was created by one of the ++** [sqlite3session_changeset()], [sqlite3changeset_concat()] or ++** [sqlite3changeset_invert()] functions, all changes within the changeset ++** that apply to a single table are grouped together. This means that when ++** an application iterates through a changeset using an iterator created by ++** this function, all changes that relate to a single table are visited ++** consecutively. There is no chance that the iterator will visit a change ++** the applies to table X, then one for table Y, and then later on visit ++** another change for table X. ++** ++** The behavior of sqlite3changeset_start_v2() and its streaming equivalent ++** may be modified by passing a combination of ++** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter. ++** ++** Note that the sqlite3changeset_start_v2() API is still experimental ++** and therefore subject to change. ++*/ ++SQLITE_API int sqlite3changeset_start( ++ sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */ ++ int nChangeset, /* Size of changeset blob in bytes */ ++ void *pChangeset /* Pointer to blob containing changeset */ ++); ++SQLITE_API int sqlite3changeset_start_v2( ++ sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */ ++ int nChangeset, /* Size of changeset blob in bytes */ ++ void *pChangeset, /* Pointer to blob containing changeset */ ++ int flags /* SESSION_CHANGESETSTART_* flags */ ++); ++ ++/* ++** CAPI3REF: Flags for sqlite3changeset_start_v2 ++** ++** The following flags may passed via the 4th parameter to ++** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]: ++** ++**- [sqlite3changeset_next()] ++**
- [sqlite3changeset_op()] ++**
- [sqlite3changeset_new()] ++**
- [sqlite3changeset_old()] ++**
- SQLITE_CHANGESETAPPLY_INVERT
- ++** Invert the changeset while iterating through it. This is equivalent to ++** inverting a changeset using sqlite3changeset_invert() before applying it. ++** It is an error to specify this flag with a patchset. ++*/ ++#define SQLITE_CHANGESETSTART_INVERT 0x0002 ++ ++ ++/* ++** CAPI3REF: Advance A Changeset Iterator ++** METHOD: sqlite3_changeset_iter ++** ++** This function may only be used with iterators created by function ++** [sqlite3changeset_start()]. If it is called on an iterator passed to ++** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE ++** is returned and the call has no effect. ++** ++** Immediately after an iterator is created by sqlite3changeset_start(), it ++** does not point to any change in the changeset. Assuming the changeset ++** is not empty, the first call to this function advances the iterator to ++** point to the first change in the changeset. Each subsequent call advances ++** the iterator to point to the next change in the changeset (if any). If ++** no error occurs and the iterator points to a valid change after a call ++** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned. ++** Otherwise, if all changes in the changeset have already been visited, ++** SQLITE_DONE is returned. ++** ++** If an error occurs, an SQLite error code is returned. Possible error ++** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or ++** SQLITE_NOMEM. ++*/ ++SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter); ++ ++/* ++** CAPI3REF: Obtain The Current Operation From A Changeset Iterator ++** METHOD: sqlite3_changeset_iter ++** ++** The pIter argument passed to this function may either be an iterator ++** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator ++** created by [sqlite3changeset_start()]. In the latter case, the most recent ++** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this ++** is not the case, this function returns [SQLITE_MISUSE]. ++** ++** If argument pzTab is not NULL, then *pzTab is set to point to a ++** nul-terminated utf-8 encoded string containing the name of the table ++** affected by the current change. The buffer remains valid until either ++** sqlite3changeset_next() is called on the iterator or until the ++** conflict-handler function returns. If pnCol is not NULL, then *pnCol is ++** set to the number of columns in the table affected by the change. If ++** pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change ++** is an indirect change, or false (0) otherwise. See the documentation for ++** [sqlite3session_indirect()] for a description of direct and indirect ++** changes. Finally, if pOp is not NULL, then *pOp is set to one of ++** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the ++** type of change that the iterator currently points to. ++** ++** If no error occurs, SQLITE_OK is returned. If an error does occur, an ++** SQLite error code is returned. The values of the output variables may not ++** be trusted in this case. ++*/ ++SQLITE_API int sqlite3changeset_op( ++ sqlite3_changeset_iter *pIter, /* Iterator object */ ++ const char **pzTab, /* OUT: Pointer to table name */ ++ int *pnCol, /* OUT: Number of columns in table */ ++ int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */ ++ int *pbIndirect /* OUT: True for an 'indirect' change */ ++); ++ ++/* ++** CAPI3REF: Obtain The Primary Key Definition Of A Table ++** METHOD: sqlite3_changeset_iter ++** ++** For each modified table, a changeset includes the following: ++** ++**
++**
++** ++** This function is used to find which columns comprise the PRIMARY KEY of ++** the table modified by the change that iterator pIter currently points to. ++** If successful, *pabPK is set to point to an array of nCol entries, where ++** nCol is the number of columns in the table. Elements of *pabPK are set to ++** 0x01 if the corresponding column is part of the tables primary key, or ++** 0x00 if it is not. ++** ++** If argument pnCol is not NULL, then *pnCol is set to the number of columns ++** in the table. ++** ++** If this function is called when the iterator does not point to a valid ++** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise, ++** SQLITE_OK is returned and the output variables populated as described ++** above. ++*/ ++SQLITE_API int sqlite3changeset_pk( ++ sqlite3_changeset_iter *pIter, /* Iterator object */ ++ unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */ ++ int *pnCol /* OUT: Number of entries in output array */ ++); ++ ++/* ++** CAPI3REF: Obtain old.* Values From A Changeset Iterator ++** METHOD: sqlite3_changeset_iter ++** ++** The pIter argument passed to this function may either be an iterator ++** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator ++** created by [sqlite3changeset_start()]. In the latter case, the most recent ++** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. ++** Furthermore, it may only be called if the type of change that the iterator ++** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise, ++** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL. ++** ++** Argument iVal must be greater than or equal to 0, and less than the number ++** of columns in the table affected by the current change. Otherwise, ++** [SQLITE_RANGE] is returned and *ppValue is set to NULL. ++** ++** If successful, this function sets *ppValue to point to a protected ++** sqlite3_value object containing the iVal'th value from the vector of ++** original row values stored as part of the UPDATE or DELETE change and ++** returns SQLITE_OK. The name of the function comes from the fact that this ++** is similar to the "old.*" columns available to update or delete triggers. ++** ++** If some other error occurs (e.g. an OOM condition), an SQLite error code ++** is returned and *ppValue is set to NULL. ++*/ ++SQLITE_API int sqlite3changeset_old( ++ sqlite3_changeset_iter *pIter, /* Changeset iterator */ ++ int iVal, /* Column number */ ++ sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */ ++); ++ ++/* ++** CAPI3REF: Obtain new.* Values From A Changeset Iterator ++** METHOD: sqlite3_changeset_iter ++** ++** The pIter argument passed to this function may either be an iterator ++** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator ++** created by [sqlite3changeset_start()]. In the latter case, the most recent ++** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. ++** Furthermore, it may only be called if the type of change that the iterator ++** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise, ++** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL. ++** ++** Argument iVal must be greater than or equal to 0, and less than the number ++** of columns in the table affected by the current change. Otherwise, ++** [SQLITE_RANGE] is returned and *ppValue is set to NULL. ++** ++** If successful, this function sets *ppValue to point to a protected ++** sqlite3_value object containing the iVal'th value from the vector of ++** new row values stored as part of the UPDATE or INSERT change and ++** returns SQLITE_OK. If the change is an UPDATE and does not include ++** a new value for the requested column, *ppValue is set to NULL and ++** SQLITE_OK returned. The name of the function comes from the fact that ++** this is similar to the "new.*" columns available to update or delete ++** triggers. ++** ++** If some other error occurs (e.g. an OOM condition), an SQLite error code ++** is returned and *ppValue is set to NULL. ++*/ ++SQLITE_API int sqlite3changeset_new( ++ sqlite3_changeset_iter *pIter, /* Changeset iterator */ ++ int iVal, /* Column number */ ++ sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */ ++); ++ ++/* ++** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator ++** METHOD: sqlite3_changeset_iter ++** ++** This function should only be used with iterator objects passed to a ++** conflict-handler callback by [sqlite3changeset_apply()] with either ++** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function ++** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue ++** is set to NULL. ++** ++** Argument iVal must be greater than or equal to 0, and less than the number ++** of columns in the table affected by the current change. Otherwise, ++** [SQLITE_RANGE] is returned and *ppValue is set to NULL. ++** ++** If successful, this function sets *ppValue to point to a protected ++** sqlite3_value object containing the iVal'th value from the ++** "conflicting row" associated with the current conflict-handler callback ++** and returns SQLITE_OK. ++** ++** If some other error occurs (e.g. an OOM condition), an SQLite error code ++** is returned and *ppValue is set to NULL. ++*/ ++SQLITE_API int sqlite3changeset_conflict( ++ sqlite3_changeset_iter *pIter, /* Changeset iterator */ ++ int iVal, /* Column number */ ++ sqlite3_value **ppValue /* OUT: Value from conflicting row */ ++); ++ ++/* ++** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations ++** METHOD: sqlite3_changeset_iter ++** ++** This function may only be called with an iterator passed to an ++** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case ++** it sets the output variable to the total number of known foreign key ++** violations in the destination database and returns SQLITE_OK. ++** ++** In all other cases this function returns SQLITE_MISUSE. ++*/ ++SQLITE_API int sqlite3changeset_fk_conflicts( ++ sqlite3_changeset_iter *pIter, /* Changeset iterator */ ++ int *pnOut /* OUT: Number of FK violations */ ++); ++ ++ ++/* ++** CAPI3REF: Finalize A Changeset Iterator ++** METHOD: sqlite3_changeset_iter ++** ++** This function is used to finalize an iterator allocated with ++** [sqlite3changeset_start()]. ++** ++** This function should only be called on iterators created using the ++** [sqlite3changeset_start()] function. If an application calls this ++** function with an iterator passed to a conflict-handler by ++** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the ++** call has no effect. ++** ++** If an error was encountered within a call to an sqlite3changeset_xxx() ++** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an ++** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding ++** to that error is returned by this function. Otherwise, SQLITE_OK is ++** returned. This is to allow the following pattern (pseudo-code): ++** ++**- The number of columns in the table, and ++**
- Which of those columns make up the tables PRIMARY KEY. ++**
++** sqlite3changeset_start(); ++** while( SQLITE_ROW==sqlite3changeset_next() ){ ++** // Do something with change. ++** } ++** rc = sqlite3changeset_finalize(); ++** if( rc!=SQLITE_OK ){ ++** // An error has occurred ++** } ++**++*/ ++SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter); ++ ++/* ++** CAPI3REF: Invert A Changeset ++** ++** This function is used to "invert" a changeset object. Applying an inverted ++** changeset to a database reverses the effects of applying the uninverted ++** changeset. Specifically: ++** ++**++**
++** ++** This function does not change the order in which changes appear within ++** the changeset. It merely reverses the sense of each individual change. ++** ++** If successful, a pointer to a buffer containing the inverted changeset ++** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and ++** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are ++** zeroed and an SQLite error code returned. ++** ++** It is the responsibility of the caller to eventually call sqlite3_free() ++** on the *ppOut pointer to free the buffer allocation following a successful ++** call to this function. ++** ++** WARNING/TODO: This function currently assumes that the input is a valid ++** changeset. If it is not, the results are undefined. ++*/ ++SQLITE_API int sqlite3changeset_invert( ++ int nIn, const void *pIn, /* Input changeset */ ++ int *pnOut, void **ppOut /* OUT: Inverse of input */ ++); ++ ++/* ++** CAPI3REF: Concatenate Two Changeset Objects ++** ++** This function is used to concatenate two changesets, A and B, into a ++** single changeset. The result is a changeset equivalent to applying ++** changeset A followed by changeset B. ++** ++** This function combines the two input changesets using an ++** sqlite3_changegroup object. Calling it produces similar results as the ++** following code fragment: ++** ++**- Each DELETE change is changed to an INSERT, and ++**
- Each INSERT change is changed to a DELETE, and ++**
- For each UPDATE change, the old.* and new.* values are exchanged. ++**
++** sqlite3_changegroup *pGrp; ++** rc = sqlite3_changegroup_new(&pGrp); ++** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA); ++** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB); ++** if( rc==SQLITE_OK ){ ++** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut); ++** }else{ ++** *ppOut = 0; ++** *pnOut = 0; ++** } ++**++** ++** Refer to the sqlite3_changegroup documentation below for details. ++*/ ++SQLITE_API int sqlite3changeset_concat( ++ int nA, /* Number of bytes in buffer pA */ ++ void *pA, /* Pointer to buffer containing changeset A */ ++ int nB, /* Number of bytes in buffer pB */ ++ void *pB, /* Pointer to buffer containing changeset B */ ++ int *pnOut, /* OUT: Number of bytes in output changeset */ ++ void **ppOut /* OUT: Buffer containing output changeset */ ++); ++ ++ ++/* ++** CAPI3REF: Changegroup Handle ++** ++** A changegroup is an object used to combine two or more ++** [changesets] or [patchsets] ++*/ ++typedef struct sqlite3_changegroup sqlite3_changegroup; ++ ++/* ++** CAPI3REF: Create A New Changegroup Object ++** CONSTRUCTOR: sqlite3_changegroup ++** ++** An sqlite3_changegroup object is used to combine two or more changesets ++** (or patchsets) into a single changeset (or patchset). A single changegroup ++** object may combine changesets or patchsets, but not both. The output is ++** always in the same format as the input. ++** ++** If successful, this function returns SQLITE_OK and populates (*pp) with ++** a pointer to a new sqlite3_changegroup object before returning. The caller ++** should eventually free the returned object using a call to ++** sqlite3changegroup_delete(). If an error occurs, an SQLite error code ++** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL. ++** ++** The usual usage pattern for an sqlite3_changegroup object is as follows: ++** ++**++**
++** ++** Any number of calls to add() and output() may be made between the calls to ++** new() and delete(), and in any order. ++** ++** As well as the regular sqlite3changegroup_add() and ++** sqlite3changegroup_output() functions, also available are the streaming ++** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm(). ++*/ ++SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp); ++ ++/* ++** CAPI3REF: Add A Changeset To A Changegroup ++** METHOD: sqlite3_changegroup ++** ++** Add all changes within the changeset (or patchset) in buffer pData (size ++** nData bytes) to the changegroup. ++** ++** If the buffer contains a patchset, then all prior calls to this function ++** on the same changegroup object must also have specified patchsets. Or, if ++** the buffer contains a changeset, so must have the earlier calls to this ++** function. Otherwise, SQLITE_ERROR is returned and no changes are added ++** to the changegroup. ++** ++** Rows within the changeset and changegroup are identified by the values in ++** their PRIMARY KEY columns. A change in the changeset is considered to ++** apply to the same row as a change already present in the changegroup if ++** the two rows have the same primary key. ++** ++** Changes to rows that do not already appear in the changegroup are ++** simply copied into it. Or, if both the new changeset and the changegroup ++** contain changes that apply to a single row, the final contents of the ++** changegroup depends on the type of each change, as follows: ++** ++**- It is created using a call to sqlite3changegroup_new(). ++** ++**
- Zero or more changesets (or patchsets) are added to the object ++** by calling sqlite3changegroup_add(). ++** ++**
- The result of combining all input changesets together is obtained ++** by the application via a call to sqlite3changegroup_output(). ++** ++**
- The object is deleted using a call to sqlite3changegroup_delete(). ++**
++**
++** ++** If the new changeset contains changes to a table that is already present ++** in the changegroup, then the number of columns and the position of the ++** primary key columns for the table must be consistent. If this is not the ++** case, this function fails with SQLITE_SCHEMA. If the input changeset ++** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is ++** returned. Or, if an out-of-memory condition occurs during processing, this ++** function returns SQLITE_NOMEM. In all cases, if an error occurs the ++** final contents of the changegroup is undefined. ++** ++** If no error occurs, SQLITE_OK is returned. ++*/ ++SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData); ++ ++/* ++** CAPI3REF: Obtain A Composite Changeset From A Changegroup ++** METHOD: sqlite3_changegroup ++** ++** Obtain a buffer containing a changeset (or patchset) representing the ++** current contents of the changegroup. If the inputs to the changegroup ++** were themselves changesets, the output is a changeset. Or, if the ++** inputs were patchsets, the output is also a patchset. ++** ++** As with the output of the sqlite3session_changeset() and ++** sqlite3session_patchset() functions, all changes related to a single ++** table are grouped together in the output of this function. Tables appear ++** in the same order as for the very first changeset added to the changegroup. ++** If the second or subsequent changesets added to the changegroup contain ++** changes for tables that do not appear in the first changeset, they are ++** appended onto the end of the output changeset, again in the order in ++** which they are first encountered. ++** ++** If an error occurs, an SQLite error code is returned and the output ++** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK ++** is returned and the output variables are set to the size of and a ++** pointer to the output buffer, respectively. In this case it is the ++** responsibility of the caller to eventually free the buffer using a ++** call to sqlite3_free(). ++*/ ++SQLITE_API int sqlite3changegroup_output( ++ sqlite3_changegroup*, ++ int *pnData, /* OUT: Size of output buffer in bytes */ ++ void **ppData /* OUT: Pointer to output buffer */ ++); ++ ++/* ++** CAPI3REF: Delete A Changegroup Object ++** DESTRUCTOR: sqlite3_changegroup ++*/ ++SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*); ++ ++/* ++** CAPI3REF: Apply A Changeset To A Database ++** ++** Apply a changeset or patchset to a database. These functions attempt to ++** update the "main" database attached to handle db with the changes found in ++** the changeset passed via the second and third arguments. ++** ++** The fourth argument (xFilter) passed to these functions is the "filter ++** callback". If it is not NULL, then for each table affected by at least one ++** change in the changeset, the filter callback is invoked with ++** the table name as the second argument, and a copy of the context pointer ++** passed as the sixth argument as the first. If the "filter callback" ++** returns zero, then no attempt is made to apply any changes to the table. ++** Otherwise, if the return value is non-zero or the xFilter argument to ++** is NULL, all changes related to the table are attempted. ++** ++** For each table that is not excluded by the filter callback, this function ++** tests that the target database contains a compatible table. A table is ++** considered compatible if all of the following are true: ++** ++**Existing Change ++**New Change ++**Output Change ++** INSERT INSERT ++** The new change is ignored. This case does not occur if the new ++** changeset was recorded immediately after the changesets already ++** added to the changegroup. ++** INSERT UPDATE ++** The INSERT change remains in the changegroup. The values in the ++** INSERT change are modified as if the row was inserted by the ++** existing change and then updated according to the new change. ++** INSERT DELETE ++** The existing INSERT is removed from the changegroup. The DELETE is ++** not added. ++** UPDATE INSERT ++** The new change is ignored. This case does not occur if the new ++** changeset was recorded immediately after the changesets already ++** added to the changegroup. ++** UPDATE UPDATE ++** The existing UPDATE remains within the changegroup. It is amended ++** so that the accompanying values are as if the row was updated once ++** by the existing change and then again by the new change. ++** UPDATE DELETE ++** The existing UPDATE is replaced by the new DELETE within the ++** changegroup. ++** DELETE INSERT ++** If one or more of the column values in the row inserted by the ++** new change differ from those in the row deleted by the existing ++** change, the existing DELETE is replaced by an UPDATE within the ++** changegroup. Otherwise, if the inserted row is exactly the same ++** as the deleted row, the existing DELETE is simply discarded. ++** DELETE UPDATE ++** The new change is ignored. This case does not occur if the new ++** changeset was recorded immediately after the changesets already ++** added to the changegroup. ++** DELETE DELETE ++** The new change is ignored. This case does not occur if the new ++** changeset was recorded immediately after the changesets already ++** added to the changegroup. ++** ++**
++** ++** If there is no compatible table, it is not an error, but none of the ++** changes associated with the table are applied. A warning message is issued ++** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most ++** one such warning is issued for each table in the changeset. ++** ++** For each change for which there is a compatible table, an attempt is made ++** to modify the table contents according to the UPDATE, INSERT or DELETE ++** change. If a change cannot be applied cleanly, the conflict handler ++** function passed as the fifth argument to sqlite3changeset_apply() may be ++** invoked. A description of exactly when the conflict handler is invoked for ++** each type of change is below. ++** ++** Unlike the xFilter argument, xConflict may not be passed NULL. The results ++** of passing anything other than a valid function pointer as the xConflict ++** argument are undefined. ++** ++** Each time the conflict handler function is invoked, it must return one ++** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or ++** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned ++** if the second argument passed to the conflict handler is either ++** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler ++** returns an illegal value, any changes already made are rolled back and ++** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different ++** actions are taken by sqlite3changeset_apply() depending on the value ++** returned by each invocation of the conflict-handler function. Refer to ++** the documentation for the three ++** [SQLITE_CHANGESET_OMIT|available return values] for details. ++** ++**- The table has the same name as the name recorded in the ++** changeset, and ++**
- The table has at least as many columns as recorded in the ++** changeset, and ++**
- The table has primary key columns in the same position as ++** recorded in the changeset. ++**
++**
++** ++** It is safe to execute SQL statements, including those that write to the ++** table that the callback related to, from within the xConflict callback. ++** This can be used to further customize the applications conflict ++** resolution strategy. ++** ++** All changes made by these functions are enclosed in a savepoint transaction. ++** If any other error (aside from a constraint failure when attempting to ++** write to the target database) occurs, then the savepoint transaction is ++** rolled back, restoring the target database to its original state, and an ++** SQLite error code returned. ++** ++** If the output parameters (ppRebase) and (pnRebase) are non-NULL and ++** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2() ++** may set (*ppRebase) to point to a "rebase" that may be used with the ++** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase) ++** is set to the size of the buffer in bytes. It is the responsibility of the ++** caller to eventually free any such buffer using sqlite3_free(). The buffer ++** is only allocated and populated if one or more conflicts were encountered ++** while applying the patchset. See comments surrounding the sqlite3_rebaser ++** APIs for further details. ++** ++** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent ++** may be modified by passing a combination of ++** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter. ++** ++** Note that the sqlite3changeset_apply_v2() API is still experimental ++** and therefore subject to change. ++*/ ++SQLITE_API int sqlite3changeset_apply( ++ sqlite3 *db, /* Apply change to "main" db of this handle */ ++ int nChangeset, /* Size of changeset in bytes */ ++ void *pChangeset, /* Changeset blob */ ++ int(*xFilter)( ++ void *pCtx, /* Copy of sixth arg to _apply() */ ++ const char *zTab /* Table name */ ++ ), ++ int(*xConflict)( ++ void *pCtx, /* Copy of sixth arg to _apply() */ ++ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ ++ sqlite3_changeset_iter *p /* Handle describing change and conflict */ ++ ), ++ void *pCtx /* First argument passed to xConflict */ ++); ++SQLITE_API int sqlite3changeset_apply_v2( ++ sqlite3 *db, /* Apply change to "main" db of this handle */ ++ int nChangeset, /* Size of changeset in bytes */ ++ void *pChangeset, /* Changeset blob */ ++ int(*xFilter)( ++ void *pCtx, /* Copy of sixth arg to _apply() */ ++ const char *zTab /* Table name */ ++ ), ++ int(*xConflict)( ++ void *pCtx, /* Copy of sixth arg to _apply() */ ++ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ ++ sqlite3_changeset_iter *p /* Handle describing change and conflict */ ++ ), ++ void *pCtx, /* First argument passed to xConflict */ ++ void **ppRebase, int *pnRebase, /* OUT: Rebase data */ ++ int flags /* SESSION_CHANGESETAPPLY_* flags */ ++); ++ ++/* ++** CAPI3REF: Flags for sqlite3changeset_apply_v2 ++** ++** The following flags may passed via the 9th parameter to ++** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]: ++** ++**- DELETE Changes
- ++** For each DELETE change, the function checks if the target database ++** contains a row with the same primary key value (or values) as the ++** original row values stored in the changeset. If it does, and the values ++** stored in all non-primary key columns also match the values stored in ++** the changeset the row is deleted from the target database. ++** ++** If a row with matching primary key values is found, but one or more of ++** the non-primary key fields contains a value different from the original ++** row value stored in the changeset, the conflict-handler function is ++** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the ++** database table has more columns than are recorded in the changeset, ++** only the values of those non-primary key fields are compared against ++** the current database contents - any trailing database table columns ++** are ignored. ++** ++** If no row with matching primary key values is found in the database, ++** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND] ++** passed as the second argument. ++** ++** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT ++** (which can only happen if a foreign key constraint is violated), the ++** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT] ++** passed as the second argument. This includes the case where the DELETE ++** operation is attempted because an earlier call to the conflict handler ++** function returned [SQLITE_CHANGESET_REPLACE]. ++** ++**
- INSERT Changes
- ++** For each INSERT change, an attempt is made to insert the new row into ++** the database. If the changeset row contains fewer fields than the ++** database table, the trailing fields are populated with their default ++** values. ++** ++** If the attempt to insert the row fails because the database already ++** contains a row with the same primary key values, the conflict handler ++** function is invoked with the second argument set to ++** [SQLITE_CHANGESET_CONFLICT]. ++** ++** If the attempt to insert the row fails because of some other constraint ++** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is ++** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT]. ++** This includes the case where the INSERT operation is re-attempted because ++** an earlier call to the conflict handler function returned ++** [SQLITE_CHANGESET_REPLACE]. ++** ++**
- UPDATE Changes
- ++** For each UPDATE change, the function checks if the target database ++** contains a row with the same primary key value (or values) as the ++** original row values stored in the changeset. If it does, and the values ++** stored in all modified non-primary key columns also match the values ++** stored in the changeset the row is updated within the target database. ++** ++** If a row with matching primary key values is found, but one or more of ++** the modified non-primary key fields contains a value different from an ++** original row value stored in the changeset, the conflict-handler function ++** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since ++** UPDATE changes only contain values for non-primary key fields that are ++** to be modified, only those fields need to match the original values to ++** avoid the SQLITE_CHANGESET_DATA conflict-handler callback. ++** ++** If no row with matching primary key values is found in the database, ++** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND] ++** passed as the second argument. ++** ++** If the UPDATE operation is attempted, but SQLite returns ++** SQLITE_CONSTRAINT, the conflict-handler function is invoked with ++** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument. ++** This includes the case where the UPDATE operation is attempted after ++** an earlier call to the conflict handler function returned ++** [SQLITE_CHANGESET_REPLACE]. ++**
++**
- SQLITE_CHANGESETAPPLY_NOSAVEPOINT
- ++** Usually, the sessions module encloses all operations performed by ++** a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The ++** SAVEPOINT is committed if the changeset or patchset is successfully ++** applied, or rolled back if an error occurs. Specifying this flag ++** causes the sessions module to omit this savepoint. In this case, if the ++** caller has an open transaction or savepoint when apply_v2() is called, ++** it may revert the partially applied changeset by rolling it back. ++** ++**
- SQLITE_CHANGESETAPPLY_INVERT
- ++** Invert the changeset before applying it. This is equivalent to inverting ++** a changeset using sqlite3changeset_invert() before applying it. It is ++** an error to specify this flag with a patchset. ++*/ ++#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001 ++#define SQLITE_CHANGESETAPPLY_INVERT 0x0002 ++ ++/* ++** CAPI3REF: Constants Passed To The Conflict Handler ++** ++** Values that may be passed as the second argument to a conflict-handler. ++** ++**
++**
++*/ ++#define SQLITE_CHANGESET_DATA 1 ++#define SQLITE_CHANGESET_NOTFOUND 2 ++#define SQLITE_CHANGESET_CONFLICT 3 ++#define SQLITE_CHANGESET_CONSTRAINT 4 ++#define SQLITE_CHANGESET_FOREIGN_KEY 5 ++ ++/* ++** CAPI3REF: Constants Returned By The Conflict Handler ++** ++** A conflict handler callback must return one of the following three values. ++** ++**- SQLITE_CHANGESET_DATA
- ++** The conflict handler is invoked with CHANGESET_DATA as the second argument ++** when processing a DELETE or UPDATE change if a row with the required ++** PRIMARY KEY fields is present in the database, but one or more other ++** (non primary-key) fields modified by the update do not contain the ++** expected "before" values. ++** ++** The conflicting row, in this case, is the database row with the matching ++** primary key. ++** ++**
- SQLITE_CHANGESET_NOTFOUND
- ++** The conflict handler is invoked with CHANGESET_NOTFOUND as the second ++** argument when processing a DELETE or UPDATE change if a row with the ++** required PRIMARY KEY fields is not present in the database. ++** ++** There is no conflicting row in this case. The results of invoking the ++** sqlite3changeset_conflict() API are undefined. ++** ++**
- SQLITE_CHANGESET_CONFLICT
- ++** CHANGESET_CONFLICT is passed as the second argument to the conflict ++** handler while processing an INSERT change if the operation would result ++** in duplicate primary key values. ++** ++** The conflicting row in this case is the database row with the matching ++** primary key. ++** ++**
- SQLITE_CHANGESET_FOREIGN_KEY
- ++** If foreign key handling is enabled, and applying a changeset leaves the ++** database in a state containing foreign key violations, the conflict ++** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument ++** exactly once before the changeset is committed. If the conflict handler ++** returns CHANGESET_OMIT, the changes, including those that caused the ++** foreign key constraint violation, are committed. Or, if it returns ++** CHANGESET_ABORT, the changeset is rolled back. ++** ++** No current or conflicting row information is provided. The only function ++** it is possible to call on the supplied sqlite3_changeset_iter handle ++** is sqlite3changeset_fk_conflicts(). ++** ++**
- SQLITE_CHANGESET_CONSTRAINT
- ++** If any other constraint violation occurs while applying a change (i.e. ++** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is ++** invoked with CHANGESET_CONSTRAINT as the second argument. ++** ++** There is no conflicting row in this case. The results of invoking the ++** sqlite3changeset_conflict() API are undefined. ++** ++**
++**
++*/ ++#define SQLITE_CHANGESET_OMIT 0 ++#define SQLITE_CHANGESET_REPLACE 1 ++#define SQLITE_CHANGESET_ABORT 2 ++ ++/* ++** CAPI3REF: Rebasing changesets ++** EXPERIMENTAL ++** ++** Suppose there is a site hosting a database in state S0. And that ++** modifications are made that move that database to state S1 and a ++** changeset recorded (the "local" changeset). Then, a changeset based ++** on S0 is received from another site (the "remote" changeset) and ++** applied to the database. The database is then in state ++** (S1+"remote"), where the exact state depends on any conflict ++** resolution decisions (OMIT or REPLACE) made while applying "remote". ++** Rebasing a changeset is to update it to take those conflict ++** resolution decisions into account, so that the same conflicts ++** do not have to be resolved elsewhere in the network. ++** ++** For example, if both the local and remote changesets contain an ++** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)": ++** ++** local: INSERT INTO t1 VALUES(1, 'v1'); ++** remote: INSERT INTO t1 VALUES(1, 'v2'); ++** ++** and the conflict resolution is REPLACE, then the INSERT change is ++** removed from the local changeset (it was overridden). Or, if the ++** conflict resolution was "OMIT", then the local changeset is modified ++** to instead contain: ++** ++** UPDATE t1 SET b = 'v2' WHERE a=1; ++** ++** Changes within the local changeset are rebased as follows: ++** ++**- SQLITE_CHANGESET_OMIT
- ++** If a conflict handler returns this value no special action is taken. The ++** change that caused the conflict is not applied. The session module ++** continues to the next change in the changeset. ++** ++**
- SQLITE_CHANGESET_REPLACE
- ++** This value may only be returned if the second argument to the conflict ++** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this ++** is not the case, any changes applied so far are rolled back and the ++** call to sqlite3changeset_apply() returns SQLITE_MISUSE. ++** ++** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict ++** handler, then the conflicting row is either updated or deleted, depending ++** on the type of change. ++** ++** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict ++** handler, then the conflicting row is removed from the database and a ++** second attempt to apply the change is made. If this second attempt fails, ++** the original row is restored to the database before continuing. ++** ++**
- SQLITE_CHANGESET_ABORT
- ++** If this value is returned, any changes applied so far are rolled back ++** and the call to sqlite3changeset_apply() returns SQLITE_ABORT. ++**
++**
++** ++** A local change may be rebased against multiple remote changes ++** simultaneously. If a single key is modified by multiple remote ++** changesets, they are combined as follows before the local changeset ++** is rebased: ++** ++**- Local INSERT
- ++** This may only conflict with a remote INSERT. If the conflict ++** resolution was OMIT, then add an UPDATE change to the rebased ++** changeset. Or, if the conflict resolution was REPLACE, add ++** nothing to the rebased changeset. ++** ++**
- Local DELETE
- ++** This may conflict with a remote UPDATE or DELETE. In both cases the ++** only possible resolution is OMIT. If the remote operation was a ++** DELETE, then add no change to the rebased changeset. If the remote ++** operation was an UPDATE, then the old.* fields of change are updated ++** to reflect the new.* values in the UPDATE. ++** ++**
- Local UPDATE
- ++** This may conflict with a remote UPDATE or DELETE. If it conflicts ++** with a DELETE, and the conflict resolution was OMIT, then the update ++** is changed into an INSERT. Any undefined values in the new.* record ++** from the update change are filled in using the old.* values from ++** the conflicting DELETE. Or, if the conflict resolution was REPLACE, ++** the UPDATE change is simply omitted from the rebased changeset. ++** ++** If conflict is with a remote UPDATE and the resolution is OMIT, then ++** the old.* values are rebased using the new.* values in the remote ++** change. Or, if the resolution is REPLACE, then the change is copied ++** into the rebased changeset with updates to columns also updated by ++** the conflicting remote UPDATE removed. If this means no columns would ++** be updated, the change is omitted. ++**
++**
++** ++** Note that conflict resolutions from multiple remote changesets are ++** combined on a per-field basis, not per-row. This means that in the ++** case of multiple remote UPDATE operations, some fields of a single ++** local change may be rebased for REPLACE while others are rebased for ++** OMIT. ++** ++** In order to rebase a local changeset, the remote changeset must first ++** be applied to the local database using sqlite3changeset_apply_v2() and ++** the buffer of rebase information captured. Then: ++** ++**- If there has been one or more REPLACE resolutions on a ++** key, it is rebased according to a REPLACE. ++** ++**
- If there have been no REPLACE resolutions on a key, then ++** the local changeset is rebased according to the most recent ++** of the OMIT resolutions. ++**
++**
++*/ ++typedef struct sqlite3_rebaser sqlite3_rebaser; ++ ++/* ++** CAPI3REF: Create a changeset rebaser object. ++** EXPERIMENTAL ++** ++** Allocate a new changeset rebaser object. If successful, set (*ppNew) to ++** point to the new object and return SQLITE_OK. Otherwise, if an error ++** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew) ++** to NULL. ++*/ ++SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew); ++ ++/* ++** CAPI3REF: Configure a changeset rebaser object. ++** EXPERIMENTAL ++** ++** Configure the changeset rebaser object to rebase changesets according ++** to the conflict resolutions described by buffer pRebase (size nRebase ++** bytes), which must have been obtained from a previous call to ++** sqlite3changeset_apply_v2(). ++*/ ++SQLITE_API int sqlite3rebaser_configure( ++ sqlite3_rebaser*, ++ int nRebase, const void *pRebase ++); ++ ++/* ++** CAPI3REF: Rebase a changeset ++** EXPERIMENTAL ++** ++** Argument pIn must point to a buffer containing a changeset nIn bytes ++** in size. This function allocates and populates a buffer with a copy ++** of the changeset rebased rebased according to the configuration of the ++** rebaser object passed as the first argument. If successful, (*ppOut) ++** is set to point to the new buffer containing the rebased changeset and ++** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the ++** responsibility of the caller to eventually free the new buffer using ++** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut) ++** are set to zero and an SQLite error code returned. ++*/ ++SQLITE_API int sqlite3rebaser_rebase( ++ sqlite3_rebaser*, ++ int nIn, const void *pIn, ++ int *pnOut, void **ppOut ++); ++ ++/* ++** CAPI3REF: Delete a changeset rebaser object. ++** EXPERIMENTAL ++** ++** Delete the changeset rebaser object and all associated resources. There ++** should be one call to this function for each successful invocation ++** of sqlite3rebaser_create(). ++*/ ++SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p); ++ ++/* ++** CAPI3REF: Streaming Versions of API functions. ++** ++** The six streaming API xxx_strm() functions serve similar purposes to the ++** corresponding non-streaming API functions: ++** ++**- An sqlite3_rebaser object is created by calling ++** sqlite3rebaser_create(). ++**
- The new object is configured with the rebase buffer obtained from ++** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure(). ++** If the local changeset is to be rebased against multiple remote ++** changesets, then sqlite3rebaser_configure() should be called ++** multiple times, in the same order that the multiple ++** sqlite3changeset_apply_v2() calls were made. ++**
- Each local changeset is rebased by calling sqlite3rebaser_rebase(). ++**
- The sqlite3_rebaser object is deleted by calling ++** sqlite3rebaser_delete(). ++**
++**
++** ++** Non-streaming functions that accept changesets (or patchsets) as input ++** require that the entire changeset be stored in a single buffer in memory. ++** Similarly, those that return a changeset or patchset do so by returning ++** a pointer to a single large buffer allocated using sqlite3_malloc(). ++** Normally this is convenient. However, if an application running in a ++** low-memory environment is required to handle very large changesets, the ++** large contiguous memory allocations required can become onerous. ++** ++** In order to avoid this problem, instead of a single large buffer, input ++** is passed to a streaming API functions by way of a callback function that ++** the sessions module invokes to incrementally request input data as it is ++** required. In all cases, a pair of API function parameters such as ++** ++**Streaming function Non-streaming equivalent ++**sqlite3changeset_apply_strm [sqlite3changeset_apply] ++** sqlite3changeset_apply_strm_v2 [sqlite3changeset_apply_v2] ++** sqlite3changeset_concat_strm [sqlite3changeset_concat] ++** sqlite3changeset_invert_strm [sqlite3changeset_invert] ++** sqlite3changeset_start_strm [sqlite3changeset_start] ++** sqlite3session_changeset_strm [sqlite3session_changeset] ++** sqlite3session_patchset_strm [sqlite3session_patchset] ++** ++** int nChangeset, ++** void *pChangeset, ++**++** ++** Is replaced by: ++** ++**++** int (*xInput)(void *pIn, void *pData, int *pnData), ++** void *pIn, ++**++** ++** Each time the xInput callback is invoked by the sessions module, the first ++** argument passed is a copy of the supplied pIn context pointer. The second ++** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no ++** error occurs the xInput method should copy up to (*pnData) bytes of data ++** into the buffer and set (*pnData) to the actual number of bytes copied ++** before returning SQLITE_OK. If the input is completely exhausted, (*pnData) ++** should be set to zero to indicate this. Or, if an error occurs, an SQLite ++** error code should be returned. In all cases, if an xInput callback returns ++** an error, all processing is abandoned and the streaming API function ++** returns a copy of the error code to the caller. ++** ++** In the case of sqlite3changeset_start_strm(), the xInput callback may be ++** invoked by the sessions module at any point during the lifetime of the ++** iterator. If such an xInput callback returns an error, the iterator enters ++** an error state, whereby all subsequent calls to iterator functions ++** immediately fail with the same error code as returned by xInput. ++** ++** Similarly, streaming API functions that return changesets (or patchsets) ++** return them in chunks by way of a callback function instead of via a ++** pointer to a single large buffer. In this case, a pair of parameters such ++** as: ++** ++**++** int *pnChangeset, ++** void **ppChangeset, ++**++** ++** Is replaced by: ++** ++**++** int (*xOutput)(void *pOut, const void *pData, int nData), ++** void *pOut ++**++** ++** The xOutput callback is invoked zero or more times to return data to ++** the application. The first parameter passed to each call is a copy of the ++** pOut pointer supplied by the application. The second parameter, pData, ++** points to a buffer nData bytes in size containing the chunk of output ++** data being returned. If the xOutput callback successfully processes the ++** supplied data, it should return SQLITE_OK to indicate success. Otherwise, ++** it should return some other SQLite error code. In this case processing ++** is immediately abandoned and the streaming API function returns a copy ++** of the xOutput error code to the application. ++** ++** The sessions module never invokes an xOutput callback with the third ++** parameter set to a value less than or equal to zero. Other than this, ++** no guarantees are made as to the size of the chunks of data returned. ++*/ ++SQLITE_API int sqlite3changeset_apply_strm( ++ sqlite3 *db, /* Apply change to "main" db of this handle */ ++ int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ ++ void *pIn, /* First arg for xInput */ ++ int(*xFilter)( ++ void *pCtx, /* Copy of sixth arg to _apply() */ ++ const char *zTab /* Table name */ ++ ), ++ int(*xConflict)( ++ void *pCtx, /* Copy of sixth arg to _apply() */ ++ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ ++ sqlite3_changeset_iter *p /* Handle describing change and conflict */ ++ ), ++ void *pCtx /* First argument passed to xConflict */ ++); ++SQLITE_API int sqlite3changeset_apply_v2_strm( ++ sqlite3 *db, /* Apply change to "main" db of this handle */ ++ int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ ++ void *pIn, /* First arg for xInput */ ++ int(*xFilter)( ++ void *pCtx, /* Copy of sixth arg to _apply() */ ++ const char *zTab /* Table name */ ++ ), ++ int(*xConflict)( ++ void *pCtx, /* Copy of sixth arg to _apply() */ ++ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ ++ sqlite3_changeset_iter *p /* Handle describing change and conflict */ ++ ), ++ void *pCtx, /* First argument passed to xConflict */ ++ void **ppRebase, int *pnRebase, ++ int flags ++); ++SQLITE_API int sqlite3changeset_concat_strm( ++ int (*xInputA)(void *pIn, void *pData, int *pnData), ++ void *pInA, ++ int (*xInputB)(void *pIn, void *pData, int *pnData), ++ void *pInB, ++ int (*xOutput)(void *pOut, const void *pData, int nData), ++ void *pOut ++); ++SQLITE_API int sqlite3changeset_invert_strm( ++ int (*xInput)(void *pIn, void *pData, int *pnData), ++ void *pIn, ++ int (*xOutput)(void *pOut, const void *pData, int nData), ++ void *pOut ++); ++SQLITE_API int sqlite3changeset_start_strm( ++ sqlite3_changeset_iter **pp, ++ int (*xInput)(void *pIn, void *pData, int *pnData), ++ void *pIn ++); ++SQLITE_API int sqlite3changeset_start_v2_strm( ++ sqlite3_changeset_iter **pp, ++ int (*xInput)(void *pIn, void *pData, int *pnData), ++ void *pIn, ++ int flags ++); ++SQLITE_API int sqlite3session_changeset_strm( ++ sqlite3_session *pSession, ++ int (*xOutput)(void *pOut, const void *pData, int nData), ++ void *pOut ++); ++SQLITE_API int sqlite3session_patchset_strm( ++ sqlite3_session *pSession, ++ int (*xOutput)(void *pOut, const void *pData, int nData), ++ void *pOut ++); ++SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*, ++ int (*xInput)(void *pIn, void *pData, int *pnData), ++ void *pIn ++); ++SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*, ++ int (*xOutput)(void *pOut, const void *pData, int nData), ++ void *pOut ++); ++SQLITE_API int sqlite3rebaser_rebase_strm( ++ sqlite3_rebaser *pRebaser, ++ int (*xInput)(void *pIn, void *pData, int *pnData), ++ void *pIn, ++ int (*xOutput)(void *pOut, const void *pData, int nData), ++ void *pOut ++); ++ ++/* ++** CAPI3REF: Configure global parameters ++** ++** The sqlite3session_config() interface is used to make global configuration ++** changes to the sessions module in order to tune it to the specific needs ++** of the application. ++** ++** The sqlite3session_config() interface is not threadsafe. If it is invoked ++** while any other thread is inside any other sessions method then the ++** results are undefined. Furthermore, if it is invoked after any sessions ++** related objects have been created, the results are also undefined. ++** ++** The first argument to the sqlite3session_config() function must be one ++** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The ++** interpretation of the (void*) value passed as the second parameter and ++** the effect of calling this function depends on the value of the first ++** parameter. ++** ++**++**
++** ++** This function returns SQLITE_OK if successful, or an SQLite error code ++** otherwise. ++*/ ++SQLITE_API int sqlite3session_config(int op, void *pArg); ++ ++/* ++** CAPI3REF: Values for sqlite3session_config(). ++*/ ++#define SQLITE_SESSION_CONFIG_STRMSIZE 1 ++ ++/* ++** Make sure we can call this stuff from C++. ++*/ ++#if 0 ++} ++#endif ++ ++#endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */ ++ ++/******** End of sqlite3session.h *********/ ++/******** Begin file fts5.h *********/ ++/* ++** 2014 May 31 ++** ++** The author disclaims copyright to this source code. In place of ++** a legal notice, here is a blessing: ++** ++** May you do good and not evil. ++** May you find forgiveness for yourself and forgive others. ++** May you share freely, never taking more than you give. ++** ++****************************************************************************** ++** ++** Interfaces to extend FTS5. Using the interfaces defined in this file, ++** FTS5 may be extended with: ++** ++** * custom tokenizers, and ++** * custom auxiliary functions. ++*/ ++ ++ ++#ifndef _FTS5_H ++#define _FTS5_H ++ ++ ++#if 0 ++extern "C" { ++#endif ++ ++/************************************************************************* ++** CUSTOM AUXILIARY FUNCTIONS ++** ++** Virtual table implementations may overload SQL functions by implementing ++** the sqlite3_module.xFindFunction() method. ++*/ ++ ++typedef struct Fts5ExtensionApi Fts5ExtensionApi; ++typedef struct Fts5Context Fts5Context; ++typedef struct Fts5PhraseIter Fts5PhraseIter; ++ ++typedef void (*fts5_extension_function)( ++ const Fts5ExtensionApi *pApi, /* API offered by current FTS version */ ++ Fts5Context *pFts, /* First arg to pass to pApi functions */ ++ sqlite3_context *pCtx, /* Context for returning result/error */ ++ int nVal, /* Number of values in apVal[] array */ ++ sqlite3_value **apVal /* Array of trailing arguments */ ++); ++ ++struct Fts5PhraseIter { ++ const unsigned char *a; ++ const unsigned char *b; ++}; ++ ++/* ++** EXTENSION API FUNCTIONS ++** ++** xUserData(pFts): ++** Return a copy of the context pointer the extension function was ++** registered with. ++** ++** xColumnTotalSize(pFts, iCol, pnToken): ++** If parameter iCol is less than zero, set output variable *pnToken ++** to the total number of tokens in the FTS5 table. Or, if iCol is ++** non-negative but less than the number of columns in the table, return ++** the total number of tokens in column iCol, considering all rows in ++** the FTS5 table. ++** ++** If parameter iCol is greater than or equal to the number of columns ++** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g. ++** an OOM condition or IO error), an appropriate SQLite error code is ++** returned. ++** ++** xColumnCount(pFts): ++** Return the number of columns in the table. ++** ++** xColumnSize(pFts, iCol, pnToken): ++** If parameter iCol is less than zero, set output variable *pnToken ++** to the total number of tokens in the current row. Or, if iCol is ++** non-negative but less than the number of columns in the table, set ++** *pnToken to the number of tokens in column iCol of the current row. ++** ++** If parameter iCol is greater than or equal to the number of columns ++** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g. ++** an OOM condition or IO error), an appropriate SQLite error code is ++** returned. ++** ++** This function may be quite inefficient if used with an FTS5 table ++** created with the "columnsize=0" option. ++** ++** xColumnText: ++** This function attempts to retrieve the text of column iCol of the ++** current document. If successful, (*pz) is set to point to a buffer ++** containing the text in utf-8 encoding, (*pn) is set to the size in bytes ++** (not characters) of the buffer and SQLITE_OK is returned. Otherwise, ++** if an error occurs, an SQLite error code is returned and the final values ++** of (*pz) and (*pn) are undefined. ++** ++** xPhraseCount: ++** Returns the number of phrases in the current query expression. ++** ++** xPhraseSize: ++** Returns the number of tokens in phrase iPhrase of the query. Phrases ++** are numbered starting from zero. ++** ++** xInstCount: ++** Set *pnInst to the total number of occurrences of all phrases within ++** the query within the current row. Return SQLITE_OK if successful, or ++** an error code (i.e. SQLITE_NOMEM) if an error occurs. ++** ++** This API can be quite slow if used with an FTS5 table created with the ++** "detail=none" or "detail=column" option. If the FTS5 table is created ++** with either "detail=none" or "detail=column" and "content=" option ++** (i.e. if it is a contentless table), then this API always returns 0. ++** ++** xInst: ++** Query for the details of phrase match iIdx within the current row. ++** Phrase matches are numbered starting from zero, so the iIdx argument ++** should be greater than or equal to zero and smaller than the value ++** output by xInstCount(). ++** ++** Usually, output parameter *piPhrase is set to the phrase number, *piCol ++** to the column in which it occurs and *piOff the token offset of the ++** first token of the phrase. Returns SQLITE_OK if successful, or an error ++** code (i.e. SQLITE_NOMEM) if an error occurs. ++** ++** This API can be quite slow if used with an FTS5 table created with the ++** "detail=none" or "detail=column" option. ++** ++** xRowid: ++** Returns the rowid of the current row. ++** ++** xTokenize: ++** Tokenize text using the tokenizer belonging to the FTS5 table. ++** ++** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback): ++** This API function is used to query the FTS table for phrase iPhrase ++** of the current query. Specifically, a query equivalent to: ++** ++** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid ++** ++** with $p set to a phrase equivalent to the phrase iPhrase of the ++** current query is executed. Any column filter that applies to ++** phrase iPhrase of the current query is included in $p. For each ++** row visited, the callback function passed as the fourth argument ++** is invoked. The context and API objects passed to the callback ++** function may be used to access the properties of each matched row. ++** Invoking Api.xUserData() returns a copy of the pointer passed as ++** the third argument to pUserData. ++** ++** If the callback function returns any value other than SQLITE_OK, the ++** query is abandoned and the xQueryPhrase function returns immediately. ++** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK. ++** Otherwise, the error code is propagated upwards. ++** ++** If the query runs to completion without incident, SQLITE_OK is returned. ++** Or, if some error occurs before the query completes or is aborted by ++** the callback, an SQLite error code is returned. ++** ++** ++** xSetAuxdata(pFts5, pAux, xDelete) ++** ++** Save the pointer passed as the second argument as the extension functions ++** "auxiliary data". The pointer may then be retrieved by the current or any ++** future invocation of the same fts5 extension function made as part of ++** the same MATCH query using the xGetAuxdata() API. ++** ++** Each extension function is allocated a single auxiliary data slot for ++** each FTS query (MATCH expression). If the extension function is invoked ++** more than once for a single FTS query, then all invocations share a ++** single auxiliary data context. ++** ++** If there is already an auxiliary data pointer when this function is ++** invoked, then it is replaced by the new pointer. If an xDelete callback ++** was specified along with the original pointer, it is invoked at this ++** point. ++** ++** The xDelete callback, if one is specified, is also invoked on the ++** auxiliary data pointer after the FTS5 query has finished. ++** ++** If an error (e.g. an OOM condition) occurs within this function, ++** the auxiliary data is set to NULL and an error code returned. If the ++** xDelete parameter was not NULL, it is invoked on the auxiliary data ++** pointer before returning. ++** ++** ++** xGetAuxdata(pFts5, bClear) ++** ++** Returns the current auxiliary data pointer for the fts5 extension ++** function. See the xSetAuxdata() method for details. ++** ++** If the bClear argument is non-zero, then the auxiliary data is cleared ++** (set to NULL) before this function returns. In this case the xDelete, ++** if any, is not invoked. ++** ++** ++** xRowCount(pFts5, pnRow) ++** ++** This function is used to retrieve the total number of rows in the table. ++** In other words, the same value that would be returned by: ++** ++** SELECT count(*) FROM ftstable; ++** ++** xPhraseFirst() ++** This function is used, along with type Fts5PhraseIter and the xPhraseNext ++** method, to iterate through all instances of a single query phrase within ++** the current row. This is the same information as is accessible via the ++** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient ++** to use, this API may be faster under some circumstances. To iterate ++** through instances of phrase iPhrase, use the following code: ++** ++** Fts5PhraseIter iter; ++** int iCol, iOff; ++** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff); ++** iCol>=0; ++** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff) ++** ){ ++** // An instance of phrase iPhrase at offset iOff of column iCol ++** } ++** ++** The Fts5PhraseIter structure is defined above. Applications should not ++** modify this structure directly - it should only be used as shown above ++** with the xPhraseFirst() and xPhraseNext() API methods (and by ++** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below). ++** ++** This API can be quite slow if used with an FTS5 table created with the ++** "detail=none" or "detail=column" option. If the FTS5 table is created ++** with either "detail=none" or "detail=column" and "content=" option ++** (i.e. if it is a contentless table), then this API always iterates ++** through an empty set (all calls to xPhraseFirst() set iCol to -1). ++** ++** xPhraseNext() ++** See xPhraseFirst above. ++** ++** xPhraseFirstColumn() ++** This function and xPhraseNextColumn() are similar to the xPhraseFirst() ++** and xPhraseNext() APIs described above. The difference is that instead ++** of iterating through all instances of a phrase in the current row, these ++** APIs are used to iterate through the set of columns in the current row ++** that contain one or more instances of a specified phrase. For example: ++** ++** Fts5PhraseIter iter; ++** int iCol; ++** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol); ++** iCol>=0; ++** pApi->xPhraseNextColumn(pFts, &iter, &iCol) ++** ){ ++** // Column iCol contains at least one instance of phrase iPhrase ++** } ++** ++** This API can be quite slow if used with an FTS5 table created with the ++** "detail=none" option. If the FTS5 table is created with either ++** "detail=none" "content=" option (i.e. if it is a contentless table), ++** then this API always iterates through an empty set (all calls to ++** xPhraseFirstColumn() set iCol to -1). ++** ++** The information accessed using this API and its companion ++** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext ++** (or xInst/xInstCount). The chief advantage of this API is that it is ++** significantly more efficient than those alternatives when used with ++** "detail=column" tables. ++** ++** xPhraseNextColumn() ++** See xPhraseFirstColumn above. ++*/ ++struct Fts5ExtensionApi { ++ int iVersion; /* Currently always set to 3 */ ++ ++ void *(*xUserData)(Fts5Context*); ++ ++ int (*xColumnCount)(Fts5Context*); ++ int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow); ++ int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken); ++ ++ int (*xTokenize)(Fts5Context*, ++ const char *pText, int nText, /* Text to tokenize */ ++ void *pCtx, /* Context passed to xToken() */ ++ int (*xToken)(void*, int, const char*, int, int, int) /* Callback */ ++ ); ++ ++ int (*xPhraseCount)(Fts5Context*); ++ int (*xPhraseSize)(Fts5Context*, int iPhrase); ++ ++ int (*xInstCount)(Fts5Context*, int *pnInst); ++ int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff); ++ ++ sqlite3_int64 (*xRowid)(Fts5Context*); ++ int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn); ++ int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken); ++ ++ int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData, ++ int(*)(const Fts5ExtensionApi*,Fts5Context*,void*) ++ ); ++ int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*)); ++ void *(*xGetAuxdata)(Fts5Context*, int bClear); ++ ++ int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*); ++ void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff); ++ ++ int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*); ++ void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol); ++}; ++ ++/* ++** CUSTOM AUXILIARY FUNCTIONS ++*************************************************************************/ ++ ++/************************************************************************* ++** CUSTOM TOKENIZERS ++** ++** Applications may also register custom tokenizer types. A tokenizer ++** is registered by providing fts5 with a populated instance of the ++** following structure. All structure methods must be defined, setting ++** any member of the fts5_tokenizer struct to NULL leads to undefined ++** behaviour. The structure methods are expected to function as follows: ++** ++** xCreate: ++** This function is used to allocate and initialize a tokenizer instance. ++** A tokenizer instance is required to actually tokenize text. ++** ++** The first argument passed to this function is a copy of the (void*) ++** pointer provided by the application when the fts5_tokenizer object ++** was registered with FTS5 (the third argument to xCreateTokenizer()). ++** The second and third arguments are an array of nul-terminated strings ++** containing the tokenizer arguments, if any, specified following the ++** tokenizer name as part of the CREATE VIRTUAL TABLE statement used ++** to create the FTS5 table. ++** ++** The final argument is an output variable. If successful, (*ppOut) ++** should be set to point to the new tokenizer handle and SQLITE_OK ++** returned. If an error occurs, some value other than SQLITE_OK should ++** be returned. In this case, fts5 assumes that the final value of *ppOut ++** is undefined. ++** ++** xDelete: ++** This function is invoked to delete a tokenizer handle previously ++** allocated using xCreate(). Fts5 guarantees that this function will ++** be invoked exactly once for each successful call to xCreate(). ++** ++** xTokenize: ++** This function is expected to tokenize the nText byte string indicated ++** by argument pText. pText may or may not be nul-terminated. The first ++** argument passed to this function is a pointer to an Fts5Tokenizer object ++** returned by an earlier call to xCreate(). ++** ++** The second argument indicates the reason that FTS5 is requesting ++** tokenization of the supplied text. This is always one of the following ++** four values: ++** ++**- SQLITE_SESSION_CONFIG_STRMSIZE
- ++** By default, the sessions module streaming interfaces attempt to input ++** and output data in approximately 1 KiB chunks. This operand may be used ++** to set and query the value of this configuration setting. The pointer ++** passed as the second argument must point to a value of type (int). ++** If this value is greater than 0, it is used as the new streaming data ++** chunk size for both input and output. Before returning, the (int) value ++** pointed to by pArg is set to the final value of the streaming interface ++** chunk size. ++**
++** ++** For each token in the input string, the supplied callback xToken() must ++** be invoked. The first argument to it should be a copy of the pointer ++** passed as the second argument to xTokenize(). The third and fourth ++** arguments are a pointer to a buffer containing the token text, and the ++** size of the token in bytes. The 4th and 5th arguments are the byte offsets ++** of the first byte of and first byte immediately following the text from ++** which the token is derived within the input. ++** ++** The second argument passed to the xToken() callback ("tflags") should ++** normally be set to 0. The exception is if the tokenizer supports ++** synonyms. In this case see the discussion below for details. ++** ++** FTS5 assumes the xToken() callback is invoked for each token in the ++** order that they occur within the input text. ++** ++** If an xToken() callback returns any value other than SQLITE_OK, then ++** the tokenization should be abandoned and the xTokenize() method should ++** immediately return a copy of the xToken() return value. Or, if the ++** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally, ++** if an error occurs with the xTokenize() implementation itself, it ++** may abandon the tokenization and return any error code other than ++** SQLITE_OK or SQLITE_DONE. ++** ++** SYNONYM SUPPORT ++** ++** Custom tokenizers may also support synonyms. Consider a case in which a ++** user wishes to query for a phrase such as "first place". Using the ++** built-in tokenizers, the FTS5 query 'first + place' will match instances ++** of "first place" within the document set, but not alternative forms ++** such as "1st place". In some applications, it would be better to match ++** all instances of "first place" or "1st place" regardless of which form ++** the user specified in the MATCH query text. ++** ++** There are several ways to approach this in FTS5: ++** ++**
- FTS5_TOKENIZE_DOCUMENT - A document is being inserted into ++** or removed from the FTS table. The tokenizer is being invoked to ++** determine the set of tokens to add to (or delete from) the ++** FTS index. ++** ++**
- FTS5_TOKENIZE_QUERY - A MATCH query is being executed ++** against the FTS index. The tokenizer is being called to tokenize ++** a bareword or quoted string specified as part of the query. ++** ++**
- (FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX) - Same as ++** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is ++** followed by a "*" character, indicating that the last token ++** returned by the tokenizer will be treated as a token prefix. ++** ++**
- FTS5_TOKENIZE_AUX - The tokenizer is being invoked to ++** satisfy an fts5_api.xTokenize() request made by an auxiliary ++** function. Or an fts5_api.xColumnSize() request made by the same ++** on a columnsize=0 database. ++**
++** ++** Whether it is parsing document or query text, any call to xToken that ++** specifies a tflags argument with the FTS5_TOKEN_COLOCATED bit ++** is considered to supply a synonym for the previous token. For example, ++** when parsing the document "I won first place", a tokenizer that supports ++** synonyms would call xToken() 5 times, as follows: ++** ++**
- By mapping all synonyms to a single token. In this case, the ++** In the above example, this means that the tokenizer returns the ++** same token for inputs "first" and "1st". Say that token is in ++** fact "first", so that when the user inserts the document "I won ++** 1st place" entries are added to the index for tokens "i", "won", ++** "first" and "place". If the user then queries for '1st + place', ++** the tokenizer substitutes "first" for "1st" and the query works ++** as expected. ++** ++**
- By querying the index for all synonyms of each query term ++** separately. In this case, when tokenizing query text, the ++** tokenizer may provide multiple synonyms for a single term ++** within the document. FTS5 then queries the index for each ++** synonym individually. For example, faced with the query: ++** ++**
++** ... MATCH 'first place' ++** ++** the tokenizer offers both "1st" and "first" as synonyms for the ++** first token in the MATCH query and FTS5 effectively runs a query ++** similar to: ++** ++**++** ... MATCH '(first OR 1st) place' ++** ++** except that, for the purposes of auxiliary functions, the query ++** still appears to contain just two phrases - "(first OR 1st)" ++** being treated as a single phrase. ++** ++**- By adding multiple synonyms for a single term to the FTS index. ++** Using this method, when tokenizing document text, the tokenizer ++** provides multiple synonyms for each token. So that when a ++** document such as "I won first place" is tokenized, entries are ++** added to the FTS index for "i", "won", "first", "1st" and ++** "place". ++** ++** This way, even if the tokenizer does not provide synonyms ++** when tokenizing query text (it should not - to do so would be ++** inefficient), it doesn't matter if the user queries for ++** 'first + place' or '1st + place', as there are entries in the ++** FTS index corresponding to both forms of the first token. ++**
++** xToken(pCtx, 0, "i", 1, 0, 1); ++** xToken(pCtx, 0, "won", 3, 2, 5); ++** xToken(pCtx, 0, "first", 5, 6, 11); ++** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11); ++** xToken(pCtx, 0, "place", 5, 12, 17); ++** ++** ++** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time ++** xToken() is called. Multiple synonyms may be specified for a single token ++** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence. ++** There is no limit to the number of synonyms that may be provided for a ++** single token. ++** ++** In many cases, method (1) above is the best approach. It does not add ++** extra data to the FTS index or require FTS5 to query for multiple terms, ++** so it is efficient in terms of disk space and query speed. However, it ++** does not support prefix queries very well. If, as suggested above, the ++** token "first" is substituted for "1st" by the tokenizer, then the query: ++** ++**++** ... MATCH '1s*' ++** ++** will not match documents that contain the token "1st" (as the tokenizer ++** will probably not map "1s" to any prefix of "first"). ++** ++** For full prefix support, method (3) may be preferred. In this case, ++** because the index contains entries for both "first" and "1st", prefix ++** queries such as 'fi*' or '1s*' will match correctly. However, because ++** extra entries are added to the FTS index, this method uses more space ++** within the database. ++** ++** Method (2) offers a midpoint between (1) and (3). Using this method, ++** a query such as '1s*' will match documents that contain the literal ++** token "1st", but not "first" (assuming the tokenizer is not able to ++** provide synonyms for prefixes). However, a non-prefix query like '1st' ++** will match against "1st" and "first". This method does not require ++** extra disk space, as no extra entries are added to the FTS index. ++** On the other hand, it may require more CPU cycles to run MATCH queries, ++** as separate queries of the FTS index are required for each synonym. ++** ++** When using methods (2) or (3), it is important that the tokenizer only ++** provide synonyms when tokenizing document text (method (2)) or query ++** text (method (3)), not both. Doing so will not cause any errors, but is ++** inefficient. ++*/ ++typedef struct Fts5Tokenizer Fts5Tokenizer; ++typedef struct fts5_tokenizer fts5_tokenizer; ++struct fts5_tokenizer { ++ int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut); ++ void (*xDelete)(Fts5Tokenizer*); ++ int (*xTokenize)(Fts5Tokenizer*, ++ void *pCtx, ++ int flags, /* Mask of FTS5_TOKENIZE_* flags */ ++ const char *pText, int nText, ++ int (*xToken)( ++ void *pCtx, /* Copy of 2nd argument to xTokenize() */ ++ int tflags, /* Mask of FTS5_TOKEN_* flags */ ++ const char *pToken, /* Pointer to buffer containing token */ ++ int nToken, /* Size of token in bytes */ ++ int iStart, /* Byte offset of token within input text */ ++ int iEnd /* Byte offset of end of token within input text */ ++ ) ++ ); ++}; ++ ++/* Flags that may be passed as the third argument to xTokenize() */ ++#define FTS5_TOKENIZE_QUERY 0x0001 ++#define FTS5_TOKENIZE_PREFIX 0x0002 ++#define FTS5_TOKENIZE_DOCUMENT 0x0004 ++#define FTS5_TOKENIZE_AUX 0x0008 ++ ++/* Flags that may be passed by the tokenizer implementation back to FTS5 ++** as the third argument to the supplied xToken callback. */ ++#define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */ ++ ++/* ++** END OF CUSTOM TOKENIZERS ++*************************************************************************/ ++ ++/************************************************************************* ++** FTS5 EXTENSION REGISTRATION API ++*/ ++typedef struct fts5_api fts5_api; ++struct fts5_api { ++ int iVersion; /* Currently always set to 2 */ ++ ++ /* Create a new tokenizer */ ++ int (*xCreateTokenizer)( ++ fts5_api *pApi, ++ const char *zName, ++ void *pContext, ++ fts5_tokenizer *pTokenizer, ++ void (*xDestroy)(void*) ++ ); ++ ++ /* Find an existing tokenizer */ ++ int (*xFindTokenizer)( ++ fts5_api *pApi, ++ const char *zName, ++ void **ppContext, ++ fts5_tokenizer *pTokenizer ++ ); ++ ++ /* Create a new auxiliary function */ ++ int (*xCreateFunction)( ++ fts5_api *pApi, ++ const char *zName, ++ void *pContext, ++ fts5_extension_function xFunction, ++ void (*xDestroy)(void*) ++ ); ++}; ++ ++/* ++** END OF REGISTRATION API ++*************************************************************************/ ++ ++#if 0 ++} /* end of the 'extern "C"' block */ ++#endif ++ ++#endif /* _FTS5_H */ ++ ++/******** End of fts5.h *********/ + + /************** End of sqlite3.h *********************************************/ + /************** Continuing where we left off in sqliteInt.h ******************/ +@@ -8044,8 +12798,9 @@ struct sqlite3_rtree_query_info { + ** Include the configuration header output by 'configure' if we're using the + ** autoconf-based build + */ +-#ifdef _HAVE_SQLITE_CONFIG_H +-#include "config.h" ++#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H) ++/* #include "config.h" */ ++#define SQLITECONFIG_H 1 + #endif + + /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/ +@@ -8139,7 +12894,7 @@ struct sqlite3_rtree_query_info { + ** Not currently enforced. + */ + #ifndef SQLITE_MAX_VDBE_OP +-# define SQLITE_MAX_VDBE_OP 25000 ++# define SQLITE_MAX_VDBE_OP 250000000 + #endif + + /* +@@ -8153,13 +12908,13 @@ struct sqlite3_rtree_query_info { + ** The suggested maximum number of in-memory pages to use for + ** the main database table and for temporary tables. + ** +-** IMPLEMENTATION-OF: R-31093-59126 The default suggested cache size +-** is 2000 pages. ++** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000, ++** which means the cache size is limited to 2048000 bytes of memory. + ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be + ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options. + */ + #ifndef SQLITE_DEFAULT_CACHE_SIZE +-# define SQLITE_DEFAULT_CACHE_SIZE 2000 ++# define SQLITE_DEFAULT_CACHE_SIZE -2000 + #endif + + /* +@@ -8172,8 +12927,9 @@ struct sqlite3_rtree_query_info { + + /* + ** The maximum number of attached databases. This must be between 0 +-** and 62. The upper bound on 62 is because a 64-bit integer bitmap +-** is used internally to track attached databases. ++** and 125. The upper bound of 125 is because the attached databases are ++** counted using a signed 8-bit integer which has a maximum value of 127 ++** and we have to allow 2 extra counts for the "main" and "temp" databases. + */ + #ifndef SQLITE_MAX_ATTACHED + # define SQLITE_MAX_ATTACHED 10 +@@ -8208,7 +12964,7 @@ struct sqlite3_rtree_query_info { + ** The default size of a database page. + */ + #ifndef SQLITE_DEFAULT_PAGE_SIZE +-# define SQLITE_DEFAULT_PAGE_SIZE 1024 ++# define SQLITE_DEFAULT_PAGE_SIZE 4096 + #endif + #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE + # undef SQLITE_DEFAULT_PAGE_SIZE +@@ -8289,7 +13045,7 @@ struct sqlite3_rtree_query_info { + ** to the next, so we have developed the following set of #if statements + ** to generate appropriate macros for a wide range of compilers. + ** +-** The correct "ANSI" way to do this is to use the intptr_t type. ++** The correct "ANSI" way to do this is to use the intptr_t type. + ** Unfortunately, that typedef is not available on all compilers, or + ** if it is available, it requires an #include of specific headers + ** that vary from one machine to the next. +@@ -8325,6 +13081,25 @@ struct sqlite3_rtree_query_info { + # define SQLITE_NOINLINE + #endif + ++/* ++** Make sure that the compiler intrinsics we desire are enabled when ++** compiling with an appropriate version of MSVC unless prevented by ++** the SQLITE_DISABLE_INTRINSIC define. ++*/ ++#if !defined(SQLITE_DISABLE_INTRINSIC) ++# if defined(_MSC_VER) && _MSC_VER>=1400 ++# if !defined(_WIN32_WCE) ++# include++# pragma intrinsic(_byteswap_ushort) ++# pragma intrinsic(_byteswap_ulong) ++# pragma intrinsic(_byteswap_uint64) ++# pragma intrinsic(_ReadWriteBarrier) ++# else ++# include ++# endif ++# endif ++#endif ++ + /* + ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2. + ** 0 means mutexes are permanently disable and the library is never +@@ -8335,6 +13110,11 @@ struct sqlite3_rtree_query_info { + ** + ** Older versions of SQLite used an optional THREADSAFE macro. + ** We support that for legacy. ++** ++** To ensure that the correct value of "THREADSAFE" is reported when querying ++** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this ++** logic is partially replicated in ctime.c. If it is updated here, it should ++** also be updated there. + */ + #if !defined(SQLITE_THREADSAFE) + # if defined(THREADSAFE) +@@ -8423,7 +13203,7 @@ struct sqlite3_rtree_query_info { + ** is set. Thus NDEBUG becomes an opt-in rather than an opt-out + ** feature. + */ +-#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) ++#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) + # define NDEBUG 1 + #endif + #if defined(NDEBUG) && defined(SQLITE_DEBUG) +@@ -8438,7 +13218,7 @@ struct sqlite3_rtree_query_info { + #endif + + /* +-** The testcase() macro is used to aid in coverage testing. When ++** The testcase() macro is used to aid in coverage testing. When + ** doing coverage testing, the condition inside the argument to + ** testcase() must be evaluated both true and false in order to + ** get full branch coverage. The testcase() macro is inserted +@@ -8484,7 +13264,7 @@ SQLITE_PRIVATE void sqlite3Coverage(int); + #endif + + /* +-** The ALWAYS and NEVER macros surround boolean expressions which ++** The ALWAYS and NEVER macros surround boolean expressions which + ** are intended to always be true or false, respectively. Such + ** expressions could be omitted from the code completely. But they + ** are included in a few cases in order to enhance the resilience +@@ -8498,7 +13278,7 @@ SQLITE_PRIVATE void sqlite3Coverage(int); + ** be true and false so that the unreachable code they specify will + ** not be counted as untested code. + */ +-#if defined(SQLITE_COVERAGE_TEST) ++#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST) + # define ALWAYS(X) (1) + # define NEVER(X) (0) + #elif !defined(NDEBUG) +@@ -8509,6 +13289,36 @@ SQLITE_PRIVATE void sqlite3Coverage(int); + # define NEVER(X) (X) + #endif + ++/* ++** Some conditionals are optimizations only. In other words, if the ++** conditionals are replaced with a constant 1 (true) or 0 (false) then ++** the correct answer is still obtained, though perhaps not as quickly. ++** ++** The following macros mark these optimizations conditionals. ++*/ ++#if defined(SQLITE_MUTATION_TEST) ++# define OK_IF_ALWAYS_TRUE(X) (1) ++# define OK_IF_ALWAYS_FALSE(X) (0) ++#else ++# define OK_IF_ALWAYS_TRUE(X) (X) ++# define OK_IF_ALWAYS_FALSE(X) (X) ++#endif ++ ++/* ++** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is ++** defined. We need to defend against those failures when testing with ++** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches ++** during a normal build. The following macro can be used to disable tests ++** that are always false except when SQLITE_TEST_REALLOC_STRESS is set. ++*/ ++#if defined(SQLITE_TEST_REALLOC_STRESS) ++# define ONLY_IF_REALLOC_STRESS(X) (X) ++#elif !defined(NDEBUG) ++# define ONLY_IF_REALLOC_STRESS(X) ((X)?(assert(0),1):0) ++#else ++# define ONLY_IF_REALLOC_STRESS(X) (0) ++#endif ++ + /* + ** Declarations used for tracing the operating system interfaces. + */ +@@ -8535,6 +13345,13 @@ SQLITE_PRIVATE void sqlite3Coverage(int); + # undef SQLITE_NEED_ERR_NAME + #endif + ++/* ++** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN ++*/ ++#ifdef SQLITE_OMIT_EXPLAIN ++# undef SQLITE_ENABLE_EXPLAIN_COMMENTS ++#endif ++ + /* + ** Return true (non-zero) if the input is an integer that is too large + ** to fit in 32-bits. This macro is used inside of various testcase() +@@ -8568,8 +13385,8 @@ SQLITE_PRIVATE void sqlite3Coverage(int); + ** This is the header file for the generic hash-table implementation + ** used in SQLite. + */ +-#ifndef _SQLITE_HASH_H_ +-#define _SQLITE_HASH_H_ ++#ifndef SQLITE_HASH_H ++#define SQLITE_HASH_H + + /* Forward declarations of structures. */ + typedef struct Hash Hash; +@@ -8601,7 +13418,7 @@ struct Hash { + unsigned int count; /* Number of entries in this table */ + HashElem *first; /* The first element of the array */ + struct _ht { /* the hash table */ +- int count; /* Number of entries with this hash */ ++ unsigned int count; /* Number of entries with this hash */ + HashElem *chain; /* Pointer to first entry with this hash */ + } *ht; + }; +@@ -8649,7 +13466,7 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*); + */ + /* #define sqliteHashCount(H) ((H)->count) // NOT USED */ + +-#endif /* _SQLITE_HASH_H_ */ ++#endif /* SQLITE_HASH_H */ + + /************** End of hash.h ************************************************/ + /************** Continuing where we left off in sqliteInt.h ******************/ +@@ -8681,139 +13498,155 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*); + #define TK_AS 24 + #define TK_WITHOUT 25 + #define TK_COMMA 26 +-#define TK_ID 27 +-#define TK_INDEXED 28 +-#define TK_ABORT 29 +-#define TK_ACTION 30 +-#define TK_AFTER 31 +-#define TK_ANALYZE 32 +-#define TK_ASC 33 +-#define TK_ATTACH 34 +-#define TK_BEFORE 35 +-#define TK_BY 36 +-#define TK_CASCADE 37 +-#define TK_CAST 38 +-#define TK_COLUMNKW 39 +-#define TK_CONFLICT 40 +-#define TK_DATABASE 41 +-#define TK_DESC 42 +-#define TK_DETACH 43 +-#define TK_EACH 44 +-#define TK_FAIL 45 +-#define TK_FOR 46 +-#define TK_IGNORE 47 +-#define TK_INITIALLY 48 +-#define TK_INSTEAD 49 +-#define TK_LIKE_KW 50 +-#define TK_MATCH 51 +-#define TK_NO 52 +-#define TK_KEY 53 +-#define TK_OF 54 +-#define TK_OFFSET 55 +-#define TK_PRAGMA 56 +-#define TK_RAISE 57 +-#define TK_RECURSIVE 58 +-#define TK_REPLACE 59 +-#define TK_RESTRICT 60 +-#define TK_ROW 61 +-#define TK_TRIGGER 62 +-#define TK_VACUUM 63 +-#define TK_VIEW 64 +-#define TK_VIRTUAL 65 +-#define TK_WITH 66 +-#define TK_REINDEX 67 +-#define TK_RENAME 68 +-#define TK_CTIME_KW 69 +-#define TK_ANY 70 +-#define TK_OR 71 +-#define TK_AND 72 +-#define TK_IS 73 +-#define TK_BETWEEN 74 +-#define TK_IN 75 +-#define TK_ISNULL 76 +-#define TK_NOTNULL 77 +-#define TK_NE 78 +-#define TK_EQ 79 +-#define TK_GT 80 +-#define TK_LE 81 +-#define TK_LT 82 +-#define TK_GE 83 +-#define TK_ESCAPE 84 +-#define TK_BITAND 85 +-#define TK_BITOR 86 +-#define TK_LSHIFT 87 +-#define TK_RSHIFT 88 +-#define TK_PLUS 89 +-#define TK_MINUS 90 +-#define TK_STAR 91 +-#define TK_SLASH 92 +-#define TK_REM 93 +-#define TK_CONCAT 94 +-#define TK_COLLATE 95 +-#define TK_BITNOT 96 +-#define TK_STRING 97 +-#define TK_JOIN_KW 98 +-#define TK_CONSTRAINT 99 +-#define TK_DEFAULT 100 +-#define TK_NULL 101 +-#define TK_PRIMARY 102 +-#define TK_UNIQUE 103 +-#define TK_CHECK 104 +-#define TK_REFERENCES 105 +-#define TK_AUTOINCR 106 +-#define TK_ON 107 +-#define TK_INSERT 108 +-#define TK_DELETE 109 +-#define TK_UPDATE 110 +-#define TK_SET 111 +-#define TK_DEFERRABLE 112 +-#define TK_FOREIGN 113 +-#define TK_DROP 114 +-#define TK_UNION 115 +-#define TK_ALL 116 +-#define TK_EXCEPT 117 +-#define TK_INTERSECT 118 +-#define TK_SELECT 119 +-#define TK_VALUES 120 +-#define TK_DISTINCT 121 +-#define TK_DOT 122 +-#define TK_FROM 123 +-#define TK_JOIN 124 +-#define TK_USING 125 +-#define TK_ORDER 126 +-#define TK_GROUP 127 +-#define TK_HAVING 128 +-#define TK_LIMIT 129 +-#define TK_WHERE 130 +-#define TK_INTO 131 +-#define TK_INTEGER 132 +-#define TK_FLOAT 133 +-#define TK_BLOB 134 +-#define TK_VARIABLE 135 +-#define TK_CASE 136 +-#define TK_WHEN 137 +-#define TK_THEN 138 +-#define TK_ELSE 139 +-#define TK_INDEX 140 +-#define TK_ALTER 141 +-#define TK_ADD 142 +-#define TK_TO_TEXT 143 +-#define TK_TO_BLOB 144 +-#define TK_TO_NUMERIC 145 +-#define TK_TO_INT 146 +-#define TK_TO_REAL 147 +-#define TK_ISNOT 148 +-#define TK_END_OF_FILE 149 +-#define TK_ILLEGAL 150 +-#define TK_SPACE 151 +-#define TK_UNCLOSED_STRING 152 +-#define TK_FUNCTION 153 +-#define TK_COLUMN 154 +-#define TK_AGG_FUNCTION 155 +-#define TK_AGG_COLUMN 156 +-#define TK_UMINUS 157 +-#define TK_UPLUS 158 +-#define TK_REGISTER 159 ++#define TK_ABORT 27 ++#define TK_ACTION 28 ++#define TK_AFTER 29 ++#define TK_ANALYZE 30 ++#define TK_ASC 31 ++#define TK_ATTACH 32 ++#define TK_BEFORE 33 ++#define TK_BY 34 ++#define TK_CASCADE 35 ++#define TK_CAST 36 ++#define TK_CONFLICT 37 ++#define TK_DATABASE 38 ++#define TK_DESC 39 ++#define TK_DETACH 40 ++#define TK_EACH 41 ++#define TK_FAIL 42 ++#define TK_OR 43 ++#define TK_AND 44 ++#define TK_IS 45 ++#define TK_MATCH 46 ++#define TK_LIKE_KW 47 ++#define TK_BETWEEN 48 ++#define TK_IN 49 ++#define TK_ISNULL 50 ++#define TK_NOTNULL 51 ++#define TK_NE 52 ++#define TK_EQ 53 ++#define TK_GT 54 ++#define TK_LE 55 ++#define TK_LT 56 ++#define TK_GE 57 ++#define TK_ESCAPE 58 ++#define TK_ID 59 ++#define TK_COLUMNKW 60 ++#define TK_DO 61 ++#define TK_FOR 62 ++#define TK_IGNORE 63 ++#define TK_INITIALLY 64 ++#define TK_INSTEAD 65 ++#define TK_NO 66 ++#define TK_KEY 67 ++#define TK_OF 68 ++#define TK_OFFSET 69 ++#define TK_PRAGMA 70 ++#define TK_RAISE 71 ++#define TK_RECURSIVE 72 ++#define TK_REPLACE 73 ++#define TK_RESTRICT 74 ++#define TK_ROW 75 ++#define TK_ROWS 76 ++#define TK_TRIGGER 77 ++#define TK_VACUUM 78 ++#define TK_VIEW 79 ++#define TK_VIRTUAL 80 ++#define TK_WITH 81 ++#define TK_CURRENT 82 ++#define TK_FOLLOWING 83 ++#define TK_PARTITION 84 ++#define TK_PRECEDING 85 ++#define TK_RANGE 86 ++#define TK_UNBOUNDED 87 ++#define TK_EXCLUDE 88 ++#define TK_GROUPS 89 ++#define TK_OTHERS 90 ++#define TK_TIES 91 ++#define TK_REINDEX 92 ++#define TK_RENAME 93 ++#define TK_CTIME_KW 94 ++#define TK_ANY 95 ++#define TK_BITAND 96 ++#define TK_BITOR 97 ++#define TK_LSHIFT 98 ++#define TK_RSHIFT 99 ++#define TK_PLUS 100 ++#define TK_MINUS 101 ++#define TK_STAR 102 ++#define TK_SLASH 103 ++#define TK_REM 104 ++#define TK_CONCAT 105 ++#define TK_COLLATE 106 ++#define TK_BITNOT 107 ++#define TK_ON 108 ++#define TK_INDEXED 109 ++#define TK_STRING 110 ++#define TK_JOIN_KW 111 ++#define TK_CONSTRAINT 112 ++#define TK_DEFAULT 113 ++#define TK_NULL 114 ++#define TK_PRIMARY 115 ++#define TK_UNIQUE 116 ++#define TK_CHECK 117 ++#define TK_REFERENCES 118 ++#define TK_AUTOINCR 119 ++#define TK_INSERT 120 ++#define TK_DELETE 121 ++#define TK_UPDATE 122 ++#define TK_SET 123 ++#define TK_DEFERRABLE 124 ++#define TK_FOREIGN 125 ++#define TK_DROP 126 ++#define TK_UNION 127 ++#define TK_ALL 128 ++#define TK_EXCEPT 129 ++#define TK_INTERSECT 130 ++#define TK_SELECT 131 ++#define TK_VALUES 132 ++#define TK_DISTINCT 133 ++#define TK_DOT 134 ++#define TK_FROM 135 ++#define TK_JOIN 136 ++#define TK_USING 137 ++#define TK_ORDER 138 ++#define TK_GROUP 139 ++#define TK_HAVING 140 ++#define TK_LIMIT 141 ++#define TK_WHERE 142 ++#define TK_INTO 143 ++#define TK_NOTHING 144 ++#define TK_FLOAT 145 ++#define TK_BLOB 146 ++#define TK_INTEGER 147 ++#define TK_VARIABLE 148 ++#define TK_CASE 149 ++#define TK_WHEN 150 ++#define TK_THEN 151 ++#define TK_ELSE 152 ++#define TK_INDEX 153 ++#define TK_ALTER 154 ++#define TK_ADD 155 ++#define TK_WINDOW 156 ++#define TK_OVER 157 ++#define TK_FILTER 158 ++#define TK_TRUEFALSE 159 ++#define TK_ISNOT 160 ++#define TK_FUNCTION 161 ++#define TK_COLUMN 162 ++#define TK_AGG_FUNCTION 163 ++#define TK_AGG_COLUMN 164 ++#define TK_UMINUS 165 ++#define TK_UPLUS 166 ++#define TK_TRUTH 167 ++#define TK_REGISTER 168 ++#define TK_VECTOR 169 ++#define TK_SELECT_COLUMN 170 ++#define TK_IF_NULL_ROW 171 ++#define TK_ASTERISK 172 ++#define TK_SPAN 173 ++#define TK_SPACE 174 ++#define TK_ILLEGAL 175 + + /************** End of parse.h ***********************************************/ + /************** Continuing where we left off in sqliteInt.h ******************/ +@@ -8823,6 +13656,18 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*); + #include + #include + ++/* ++** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY. ++** This allows better measurements of where memcpy() is used when running ++** cachegrind. But this macro version of memcpy() is very slow so it ++** should not be used in production. This is a performance measurement ++** hack only. ++*/ ++#ifdef SQLITE_INLINE_MEMCPY ++# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\ ++ int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);} ++#endif ++ + /* + ** If compiling for a processor that lacks floating point support, + ** substitute integer for floating-point +@@ -8845,7 +13690,7 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*); + + /* + ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0 +-** afterward. Having this macro allows us to cause the C compiler ++** afterward. Having this macro allows us to cause the C compiler + ** to omit code used by TEMP tables without messy #ifndef statements. + */ + #ifdef SQLITE_OMIT_TEMPDB +@@ -8879,12 +13724,11 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*); + */ + #ifndef SQLITE_TEMP_STORE + # define SQLITE_TEMP_STORE 1 +-# define SQLITE_TEMP_STORE_xc 1 /* Exclude from ctime.c */ + #endif + + /* + ** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if +-** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it ++** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it + ** to zero. + */ + #if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0 +@@ -8902,6 +13746,34 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*); + # define SQLITE_MAX_WORKER_THREADS SQLITE_DEFAULT_WORKER_THREADS + #endif + ++/* ++** The default initial allocation for the pagecache when using separate ++** pagecaches for each database connection. A positive number is the ++** number of pages. A negative number N translations means that a buffer ++** of -1024*N bytes is allocated and used for as many pages as it will hold. ++** ++** The default value of "20" was choosen to minimize the run-time of the ++** speedtest1 test program with options: --shrink-memory --reprepare ++*/ ++#ifndef SQLITE_DEFAULT_PCACHE_INITSZ ++# define SQLITE_DEFAULT_PCACHE_INITSZ 20 ++#endif ++ ++/* ++** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option. ++*/ ++#ifndef SQLITE_DEFAULT_SORTERREF_SIZE ++# define SQLITE_DEFAULT_SORTERREF_SIZE 0x7fffffff ++#endif ++ ++/* ++** The compile-time options SQLITE_MMAP_READWRITE and ++** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another. ++** You must choose one or the other (or neither) but not both. ++*/ ++#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) ++#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE ++#endif + + /* + ** GCC does not define the offsetof() macro so we'll have to do it +@@ -8914,8 +13786,12 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*); + /* + ** Macros to compute minimum and maximum of two numbers. + */ +-#define MIN(A,B) ((A)<(B)?(A):(B)) +-#define MAX(A,B) ((A)>(B)?(A):(B)) ++#ifndef MIN ++# define MIN(A,B) ((A)<(B)?(A):(B)) ++#endif ++#ifndef MAX ++# define MAX(A,B) ((A)>(B)?(A):(B)) ++#endif + + /* + ** Swap two objects of type TYPE. +@@ -9023,7 +13899,7 @@ typedef INT8_TYPE i8; /* 1-byte signed integer */ + ** 4 -> 20 1000 -> 99 1048576 -> 200 + ** 10 -> 33 1024 -> 100 4294967296 -> 320 + ** +-** The LogEst can be negative to indicate fractional values. ++** The LogEst can be negative to indicate fractional values. + ** Examples: + ** + ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40 +@@ -9037,45 +13913,70 @@ typedef INT16_TYPE LogEst; + # if defined(__SIZEOF_POINTER__) + # define SQLITE_PTRSIZE __SIZEOF_POINTER__ + # elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \ +- defined(_M_ARM) || defined(__arm__) || defined(__x86) ++ defined(_M_ARM) || defined(__arm__) || defined(__x86) || \ ++ (defined(__TOS_AIX__) && !defined(__64BIT__)) + # define SQLITE_PTRSIZE 4 + # else + # define SQLITE_PTRSIZE 8 + # endif + #endif + ++/* The uptr type is an unsigned integer large enough to hold a pointer ++*/ ++#if defined(HAVE_STDINT_H) ++ typedef uintptr_t uptr; ++#elif SQLITE_PTRSIZE==4 ++ typedef u32 uptr; ++#else ++ typedef u64 uptr; ++#endif ++ ++/* ++** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to ++** something between S (inclusive) and E (exclusive). ++** ++** In other words, S is a buffer and E is a pointer to the first byte after ++** the end of buffer S. This macro returns true if P points to something ++** contained within the buffer S. ++*/ ++#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E))) ++ ++ + /* + ** Macros to determine whether the machine is big or little endian, + ** and whether or not that determination is run-time or compile-time. + ** + ** For best performance, an attempt is made to guess at the byte-order + ** using C-preprocessor macros. If that is unsuccessful, or if +-** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined ++** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined + ** at run-time. + */ +-#ifdef SQLITE_AMALGAMATION +-SQLITE_PRIVATE const int sqlite3one = 1; +-#else +-SQLITE_PRIVATE const int sqlite3one; +-#endif +-#if (defined(i386) || defined(__i386__) || defined(_M_IX86) || \ ++#ifndef SQLITE_BYTEORDER ++# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \ + defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ + defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ +- defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER) +-# define SQLITE_BYTEORDER 1234 +-# define SQLITE_BIGENDIAN 0 +-# define SQLITE_LITTLEENDIAN 1 +-# define SQLITE_UTF16NATIVE SQLITE_UTF16LE ++ defined(__arm__) || defined(_M_ARM64) ++# define SQLITE_BYTEORDER 1234 ++# elif defined(sparc) || defined(__ppc__) ++# define SQLITE_BYTEORDER 4321 ++# else ++# define SQLITE_BYTEORDER 0 ++# endif + #endif +-#if (defined(sparc) || defined(__ppc__)) \ +- && !defined(SQLITE_RUNTIME_BYTEORDER) +-# define SQLITE_BYTEORDER 4321 ++#if SQLITE_BYTEORDER==4321 + # define SQLITE_BIGENDIAN 1 + # define SQLITE_LITTLEENDIAN 0 + # define SQLITE_UTF16NATIVE SQLITE_UTF16BE +-#endif +-#if !defined(SQLITE_BYTEORDER) +-# define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */ ++#elif SQLITE_BYTEORDER==1234 ++# define SQLITE_BIGENDIAN 0 ++# define SQLITE_LITTLEENDIAN 1 ++# define SQLITE_UTF16NATIVE SQLITE_UTF16LE ++#else ++# ifdef SQLITE_AMALGAMATION ++ const int sqlite3one = 1; ++# else ++ extern const int sqlite3one; ++# endif + # define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0) + # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1) + # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE) +@@ -9089,7 +13990,7 @@ SQLITE_PRIVATE const int sqlite3one; + #define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32)) + #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64) + +-/* ++/* + ** Round up a number to the next larger multiple of 8. This is used + ** to force 8-byte alignment on 64-bit architectures. + */ +@@ -9128,21 +14029,18 @@ SQLITE_PRIVATE const int sqlite3one; + */ + #ifdef __APPLE__ + # include +-# if TARGET_OS_IPHONE +-# undef SQLITE_MAX_MMAP_SIZE +-# define SQLITE_MAX_MMAP_SIZE 0 +-# endif + #endif + #ifndef SQLITE_MAX_MMAP_SIZE + # if defined(__linux__) \ + || defined(_WIN32) \ + || (defined(__APPLE__) && defined(__MACH__)) \ +- || defined(__sun) ++ || defined(__sun) \ ++ || defined(__FreeBSD__) \ ++ || defined(__DragonFly__) + # define SQLITE_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */ + # else + # define SQLITE_MAX_MMAP_SIZE 0 + # endif +-# define SQLITE_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */ + #endif + + /* +@@ -9152,7 +14050,6 @@ SQLITE_PRIVATE const int sqlite3one; + */ + #ifndef SQLITE_DEFAULT_MMAP_SIZE + # define SQLITE_DEFAULT_MMAP_SIZE 0 +-# define SQLITE_DEFAULT_MMAP_SIZE_xc 1 /* Exclude from ctime.c */ + #endif + #if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE + # undef SQLITE_DEFAULT_MMAP_SIZE +@@ -9177,7 +14074,7 @@ SQLITE_PRIVATE const int sqlite3one; + ** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not + ** the Select query generator tracing logic is turned on. + */ +-#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_SELECTTRACE) ++#if defined(SQLITE_ENABLE_SELECTTRACE) + # define SELECTTRACE_ENABLED 1 + #else + # define SELECTTRACE_ENABLED 0 +@@ -9185,7 +14082,7 @@ SQLITE_PRIVATE const int sqlite3one; + + /* + ** An instance of the following structure is used to store the busy-handler +-** callback for a given sqlite handle. ++** callback for a given sqlite handle. + ** + ** The sqlite.busyHandler member of the sqlite struct contains the busy + ** callback for the database handle. Each pager opened via the sqlite +@@ -9194,9 +14091,10 @@ SQLITE_PRIVATE const int sqlite3one; + */ + typedef struct BusyHandler BusyHandler; + struct BusyHandler { +- int (*xFunc)(void *,int); /* The busy callback */ +- void *pArg; /* First arg to busy callback */ +- int nBusy; /* Incremented with each busy call */ ++ int (*xBusyHandler)(void *,int); /* The busy callback */ ++ void *pBusyArg; /* First arg to busy callback */ ++ int nBusy; /* Incremented with each busy call */ ++ u8 bExtraFileArg; /* Include sqlite3_file as callback arg */ + }; + + /* +@@ -9230,9 +14128,9 @@ struct BusyHandler { + + /* + ** The following value as a destructor means to use sqlite3DbFree(). +-** The sqlite3DbFree() routine requires two parameters instead of the +-** one parameter that destructors normally want. So we have to introduce +-** this magic value that the code knows to handle differently. Any ++** The sqlite3DbFree() routine requires two parameters instead of the ++** one parameter that destructors normally want. So we have to introduce ++** this magic value that the code knows to handle differently. Any + ** pointer will work here as long as it is distinct from SQLITE_STATIC + ** and SQLITE_TRANSIENT. + */ +@@ -9256,19 +14154,19 @@ struct BusyHandler { + #define SQLITE_WSD const + #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v))) + #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config) +-SQLITE_API int SQLITE_STDCALL sqlite3_wsd_init(int N, int J); +-SQLITE_API void *SQLITE_STDCALL sqlite3_wsd_find(void *K, int L); ++SQLITE_API int sqlite3_wsd_init(int N, int J); ++SQLITE_API void *sqlite3_wsd_find(void *K, int L); + #else +- #define SQLITE_WSD ++ #define SQLITE_WSD + #define GLOBAL(t,v) v + #define sqlite3GlobalConfig sqlite3Config + #endif + + /* + ** The following macros are used to suppress compiler warnings and to +-** make it clear to human readers when a function parameter is deliberately ++** make it clear to human readers when a function parameter is deliberately + ** left unused within the body of a function. This usually happens when +-** a function is called via a function pointer. For example the ++** a function is called via a function pointer. For example the + ** implementation of an SQL aggregate step callback may not use the + ** parameter indicating the number of arguments passed to the aggregate, + ** if it knows that this is enforced elsewhere. +@@ -9296,7 +14194,6 @@ typedef struct Db Db; + typedef struct Schema Schema; + typedef struct Expr Expr; + typedef struct ExprList ExprList; +-typedef struct ExprSpan ExprSpan; + typedef struct FKey FKey; + typedef struct FuncDestructor FuncDestructor; + typedef struct FuncDef FuncDef; +@@ -9311,14 +14208,16 @@ typedef struct LookasideSlot LookasideSlot; + typedef struct Module Module; + typedef struct NameContext NameContext; + typedef struct Parse Parse; ++typedef struct PreUpdate PreUpdate; + typedef struct PrintfArguments PrintfArguments; ++typedef struct RenameToken RenameToken; + typedef struct RowSet RowSet; + typedef struct Savepoint Savepoint; + typedef struct Select Select; + typedef struct SQLiteThread SQLiteThread; + typedef struct SelectDest SelectDest; + typedef struct SrcList SrcList; +-typedef struct StrAccum StrAccum; ++typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */ + typedef struct Table Table; + typedef struct TableLock TableLock; + typedef struct Token Token; +@@ -9327,14 +14226,50 @@ typedef struct Trigger Trigger; + typedef struct TriggerPrg TriggerPrg; + typedef struct TriggerStep TriggerStep; + typedef struct UnpackedRecord UnpackedRecord; ++typedef struct Upsert Upsert; + typedef struct VTable VTable; + typedef struct VtabCtx VtabCtx; + typedef struct Walker Walker; + typedef struct WhereInfo WhereInfo; ++typedef struct Window Window; + typedef struct With With; + ++ + /* +-** Defer sourcing vdbe.h and btree.h until after the "u8" and ++** The bitmask datatype defined below is used for various optimizations. ++** ++** Changing this from a 64-bit to a 32-bit type limits the number of ++** tables in a join to 32 instead of 64. But it also reduces the size ++** of the library by 738 bytes on ix86. ++*/ ++#ifdef SQLITE_BITMASK_TYPE ++ typedef SQLITE_BITMASK_TYPE Bitmask; ++#else ++ typedef u64 Bitmask; ++#endif ++ ++/* ++** The number of bits in a Bitmask. "BMS" means "BitMask Size". ++*/ ++#define BMS ((int)(sizeof(Bitmask)*8)) ++ ++/* ++** A bit in a Bitmask ++*/ ++#define MASKBIT(n) (((Bitmask)1)<<(n)) ++#define MASKBIT32(n) (((unsigned int)1)<<(n)) ++#define ALLBITS ((Bitmask)-1) ++ ++/* A VList object records a mapping between parameters/variables/wildcards ++** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer ++** variable number associated with that parameter. See the format description ++** on the sqlite3VListAdd() routine for more information. A VList is really ++** just an array of integers. ++*/ ++typedef int VList; ++ ++/* ++** Defer sourcing vdbe.h and btree.h until after the "u8" and + ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque + ** pointer types (i.e. FuncDef) defined above. + */ +@@ -9355,8 +14290,8 @@ typedef struct With With; + ** subsystem. See comments in the source code for a detailed description + ** of what each interface routine does. + */ +-#ifndef _BTREE_H_ +-#define _BTREE_H_ ++#ifndef SQLITE_BTREE_H ++#define SQLITE_BTREE_H + + /* TODO: This definition is just included so other modules compile. It + ** needs to be revisited. +@@ -9381,6 +14316,7 @@ typedef struct With With; + typedef struct Btree Btree; + typedef struct BtCursor BtCursor; + typedef struct BtShared BtShared; ++typedef struct BtreePayload BtreePayload; + + + SQLITE_PRIVATE int sqlite3BtreeOpen( +@@ -9405,11 +14341,11 @@ SQLITE_PRIVATE int sqlite3BtreeOpen( + + SQLITE_PRIVATE int sqlite3BtreeClose(Btree*); + SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int); ++SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree*,int); + #if SQLITE_MAX_MMAP_SIZE>0 + SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64); + #endif + SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned); +-SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*); + SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix); + SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*); + SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int); +@@ -9419,7 +14355,7 @@ SQLITE_PRIVATE int sqlite3BtreeGetOptimalReserve(Btree*); + SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p); + SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int); + SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *); +-SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int); ++SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int,int*); + SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster); + SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int); + SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*); +@@ -9431,7 +14367,9 @@ SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*); + SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*); + SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *)); + SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree); ++#ifndef SQLITE_OMIT_SHARED_CACHE + SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock); ++#endif + SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int); + + SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *); +@@ -9492,8 +14430,37 @@ SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p); + #define BTREE_DATA_VERSION 15 /* A virtual meta-value */ + + /* +-** Values that may be OR'd together to form the second argument of an +-** sqlite3BtreeCursorHints() call. ++** Kinds of hints that can be passed into the sqlite3BtreeCursorHint() ++** interface. ++** ++** BTREE_HINT_RANGE (arguments: Expr*, Mem*) ++** ++** The first argument is an Expr* (which is guaranteed to be constant for ++** the lifetime of the cursor) that defines constraints on which rows ++** might be fetched with this cursor. The Expr* tree may contain ++** TK_REGISTER nodes that refer to values stored in the array of registers ++** passed as the second parameter. In other words, if Expr.op==TK_REGISTER ++** then the value of the node is the value in Mem[pExpr.iTable]. Any ++** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th ++** column of the b-tree of the cursor. The Expr tree will not contain ++** any function calls nor subqueries nor references to b-trees other than ++** the cursor being hinted. ++** ++** The design of the _RANGE hint is aid b-tree implementations that try ++** to prefetch content from remote machines - to provide those ++** implementations with limits on what needs to be prefetched and thereby ++** reduce network bandwidth. ++** ++** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by ++** standard SQLite. The other hints are provided for extentions that use ++** the SQLite parser and code generator but substitute their own storage ++** engine. ++*/ ++#define BTREE_HINT_RANGE 0 /* Range constraints on queries */ ++ ++/* ++** Values that may be OR'd together to form the argument to the ++** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint(): + ** + ** The BTREE_BULKLOAD flag is set on index cursors when the index is going + ** to be filled with content that is already in sorted order. +@@ -9507,6 +14474,32 @@ SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p); + #define BTREE_BULKLOAD 0x00000001 /* Used to full index in sorted order */ + #define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */ + ++/* ++** Flags passed as the third argument to sqlite3BtreeCursor(). ++** ++** For read-only cursors the wrFlag argument is always zero. For read-write ++** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just ++** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will ++** only be used by SQLite for the following: ++** ++** * to seek to and then delete specific entries, and/or ++** ++** * to read values that will be used to create keys that other ++** BTREE_FORDELETE cursors will seek to and delete. ++** ++** The BTREE_FORDELETE flag is an optimization hint. It is not used by ++** by this, the native b-tree engine of SQLite, but it is available to ++** alternative storage engines that might be substituted in place of this ++** b-tree system. For alternative storage engines in which a delete of ++** the main table row automatically deletes corresponding index rows, ++** the FORDELETE flag hint allows those alternative storage engines to ++** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK ++** and DELETE operations as no-ops, and any READ operation against a ++** FORDELETE cursor may return a null row: 0x01 0x00. ++*/ ++#define BTREE_WRCSR 0x00000004 /* read-write cursor */ ++#define BTREE_FORDELETE 0x00000008 /* Cursor is for seek/delete only */ ++ + SQLITE_PRIVATE int sqlite3BtreeCursor( + Btree*, /* BTree containing table to open */ + int iTable, /* Index of root page */ +@@ -9514,8 +14507,13 @@ SQLITE_PRIVATE int sqlite3BtreeCursor( + struct KeyInfo*, /* First argument to compare function */ + BtCursor *pCursor /* Space to write cursor structure */ + ); ++SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void); + SQLITE_PRIVATE int sqlite3BtreeCursorSize(void); + SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*); ++SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned); ++#ifdef SQLITE_ENABLE_CURSOR_HINTS ++SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor*, int, ...); ++#endif + + SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*); + SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked( +@@ -9527,39 +14525,91 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked( + ); + SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*); + SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*); +-SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*); +-SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey, +- const void *pData, int nData, +- int nZero, int bias, int seekResult); ++SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags); ++ ++/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */ ++#define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */ ++#define BTREE_AUXDELETE 0x04 /* not the primary delete operation */ ++#define BTREE_APPEND 0x08 /* Insert is likely an append */ ++ ++/* An instance of the BtreePayload object describes the content of a single ++** entry in either an index or table btree. ++** ++** Index btrees (used for indexes and also WITHOUT ROWID tables) contain ++** an arbitrary key and no data. These btrees have pKey,nKey set to the ++** key and the pData,nData,nZero fields are uninitialized. The aMem,nMem ++** fields give an array of Mem objects that are a decomposition of the key. ++** The nMem field might be zero, indicating that no decomposition is available. ++** ++** Table btrees (used for rowid tables) contain an integer rowid used as ++** the key and passed in the nKey field. The pKey field is zero. ++** pData,nData hold the content of the new entry. nZero extra zero bytes ++** are appended to the end of the content when constructing the entry. ++** The aMem,nMem fields are uninitialized for table btrees. ++** ++** Field usage summary: ++** ++** Table BTrees Index Btrees ++** ++** pKey always NULL encoded key ++** nKey the ROWID length of pKey ++** pData data not used ++** aMem not used decomposed key value ++** nMem not used entries in aMem ++** nData length of pData not used ++** nZero extra zeros after pData not used ++** ++** This object is used to pass information into sqlite3BtreeInsert(). The ++** same information used to be passed as five separate parameters. But placing ++** the information into this object helps to keep the interface more ++** organized and understandable, and it also helps the resulting code to ++** run a little faster by using fewer registers for parameter passing. ++*/ ++struct BtreePayload { ++ const void *pKey; /* Key content for indexes. NULL for tables */ ++ sqlite3_int64 nKey; /* Size of pKey for indexes. PRIMARY KEY for tabs */ ++ const void *pData; /* Data for tables. */ ++ sqlite3_value *aMem; /* First of nMem value in the unpacked pKey */ ++ u16 nMem; /* Number of aMem[] value. Might be zero */ ++ int nData; /* Size of pData. 0 if none. */ ++ int nZero; /* Extra zero data appended after pData,nData */ ++}; ++ ++SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload, ++ int flags, int seekResult); + SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes); + SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes); +-SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes); ++SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int flags); + SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*); +-SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes); +-SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize); +-SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*); +-SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, u32 *pAmt); +-SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, u32 *pAmt); +-SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize); +-SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*); ++SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags); ++SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*); ++#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC ++SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor*); ++#endif ++SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*); ++SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt); ++SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor*); ++SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor*); + + SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*); + SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*); ++SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor*); + ++#ifndef SQLITE_OMIT_INCRBLOB ++SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*); + SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*); + SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *); ++#endif + SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *); + SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion); +-SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask); +-#ifdef SQLITE_DEBUG + SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask); +-#endif + SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt); + SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void); + + #ifndef NDEBUG + SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*); + #endif ++SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor*); + + #ifndef SQLITE_OMIT_BTREECOUNT + SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *); +@@ -9582,15 +14632,19 @@ SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *); + #ifndef SQLITE_OMIT_SHARED_CACHE + SQLITE_PRIVATE void sqlite3BtreeEnter(Btree*); + SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3*); ++SQLITE_PRIVATE int sqlite3BtreeSharable(Btree*); ++SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor*); ++SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree*); + #else + # define sqlite3BtreeEnter(X) + # define sqlite3BtreeEnterAll(X) ++# define sqlite3BtreeSharable(X) 0 ++# define sqlite3BtreeEnterCursor(X) ++# define sqlite3BtreeConnectionCount(X) 1 + #endif + + #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE +-SQLITE_PRIVATE int sqlite3BtreeSharable(Btree*); + SQLITE_PRIVATE void sqlite3BtreeLeave(Btree*); +-SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor*); + SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor*); + SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3*); + #ifndef NDEBUG +@@ -9601,9 +14655,7 @@ SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*); + #endif + #else + +-# define sqlite3BtreeSharable(X) 0 + # define sqlite3BtreeLeave(X) +-# define sqlite3BtreeEnterCursor(X) + # define sqlite3BtreeLeaveCursor(X) + # define sqlite3BtreeLeaveAll(X) + +@@ -9613,7 +14665,7 @@ SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*); + #endif + + +-#endif /* _BTREE_H_ */ ++#endif /* SQLITE_BTREE_H */ + + /************** End of btree.h ***********************************************/ + /************** Continuing where we left off in sqliteInt.h ******************/ +@@ -9636,8 +14688,8 @@ SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*); + ** or VDBE. The VDBE implements an abstract machine that runs a + ** simple program to access and modify the underlying database. + */ +-#ifndef _SQLITE_VDBE_H_ +-#define _SQLITE_VDBE_H_ ++#ifndef SQLITE_VDBE_H ++#define SQLITE_VDBE_H + /* #include */ + + /* +@@ -9651,7 +14703,7 @@ typedef struct Vdbe Vdbe; + ** The names of the following types declared in vdbeInt.h are required + ** for the VdbeOp definition. + */ +-typedef struct Mem Mem; ++typedef struct sqlite3_value Mem; + typedef struct SubProgram SubProgram; + + /* +@@ -9662,25 +14714,29 @@ typedef struct SubProgram SubProgram; + struct VdbeOp { + u8 opcode; /* What operation to perform */ + signed char p4type; /* One of the P4_xxx constants for p4 */ +- u8 opflags; /* Mask of the OPFLG_* flags in opcodes.h */ +- u8 p5; /* Fifth parameter is an unsigned character */ ++ u16 p5; /* Fifth parameter is an unsigned 16-bit integer */ + int p1; /* First operand */ + int p2; /* Second parameter (often the jump destination) */ + int p3; /* The third parameter */ +- union { /* fourth parameter */ ++ union p4union { /* fourth parameter */ + int i; /* Integer value if p4type==P4_INT32 */ + void *p; /* Generic pointer */ + char *z; /* Pointer to data for string (char array) types */ + i64 *pI64; /* Used when p4type is P4_INT64 */ + double *pReal; /* Used when p4type is P4_REAL */ + FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */ ++ sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */ + CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */ + Mem *pMem; /* Used when p4type is P4_MEM */ + VTable *pVtab; /* Used when p4type is P4_VTAB */ + KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */ + int *ai; /* Used when p4type is P4_INTARRAY */ + SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */ +- int (*xAdvance)(BtCursor *, int *); ++ Table *pTab; /* Used when p4type is P4_TABLE */ ++#ifdef SQLITE_ENABLE_CURSOR_HINTS ++ Expr *pExpr; /* Used when p4type is P4_EXPR */ ++#endif ++ int (*xAdvance)(BtCursor *, int); + } p4; + #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS + char *zComment; /* Comment to improve readability */ +@@ -9690,7 +14746,8 @@ struct VdbeOp { + u64 cycles; /* Total time spent executing this instruction */ + #endif + #ifdef SQLITE_VDBE_COVERAGE +- int iSrcLine; /* Source-code line that generated this opcode */ ++ u32 iSrcLine; /* Source-code line that generated this opcode ++ ** with flags in the upper 8 bits */ + #endif + }; + typedef struct VdbeOp VdbeOp; +@@ -9704,7 +14761,7 @@ struct SubProgram { + int nOp; /* Elements in aOp[] */ + int nMem; /* Number of memory cells required */ + int nCsr; /* Number of cursors required */ +- int nOnce; /* Number of OP_Once instructions */ ++ u8 *aOnce; /* Array of OP_Once flags */ + void *token; /* id that may be used to recursive triggers */ + SubProgram *pNext; /* Next sub-program already visited */ + }; +@@ -9724,22 +14781,27 @@ typedef struct VdbeOpList VdbeOpList; + /* + ** Allowed values of VdbeOp.p4type + */ +-#define P4_NOTUSED 0 /* The P4 parameter is not used */ +-#define P4_DYNAMIC (-1) /* Pointer to a string obtained from sqliteMalloc() */ +-#define P4_STATIC (-2) /* Pointer to a static string */ +-#define P4_COLLSEQ (-4) /* P4 is a pointer to a CollSeq structure */ +-#define P4_FUNCDEF (-5) /* P4 is a pointer to a FuncDef structure */ +-#define P4_KEYINFO (-6) /* P4 is a pointer to a KeyInfo structure */ +-#define P4_MEM (-8) /* P4 is a pointer to a Mem* structure */ +-#define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */ +-#define P4_VTAB (-10) /* P4 is a pointer to an sqlite3_vtab structure */ +-#define P4_MPRINTF (-11) /* P4 is a string obtained from sqlite3_mprintf() */ +-#define P4_REAL (-12) /* P4 is a 64-bit floating point value */ +-#define P4_INT64 (-13) /* P4 is a 64-bit signed integer */ +-#define P4_INT32 (-14) /* P4 is a 32-bit signed integer */ +-#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */ +-#define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */ +-#define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */ ++#define P4_NOTUSED 0 /* The P4 parameter is not used */ ++#define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */ ++#define P4_STATIC (-1) /* Pointer to a static string */ ++#define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */ ++#define P4_INT32 (-3) /* P4 is a 32-bit signed integer */ ++#define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */ ++#define P4_ADVANCE (-5) /* P4 is a pointer to BtreeNext() or BtreePrev() */ ++#define P4_TABLE (-6) /* P4 is a pointer to a Table structure */ ++/* Above do not own any resources. Must free those below */ ++#define P4_FREE_IF_LE (-7) ++#define P4_DYNAMIC (-7) /* Pointer to memory from sqliteMalloc() */ ++#define P4_FUNCDEF (-8) /* P4 is a pointer to a FuncDef structure */ ++#define P4_KEYINFO (-9) /* P4 is a pointer to a KeyInfo structure */ ++#define P4_EXPR (-10) /* P4 is a pointer to an Expr tree */ ++#define P4_MEM (-11) /* P4 is a pointer to a Mem* structure */ ++#define P4_VTAB (-12) /* P4 is a pointer to an sqlite3_vtab structure */ ++#define P4_REAL (-13) /* P4 is a 64-bit floating point value */ ++#define P4_INT64 (-14) /* P4 is a 64-bit signed integer */ ++#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */ ++#define P4_FUNCCTX (-16) /* P4 is a pointer to an sqlite3_context object */ ++#define P4_DYNBLOB (-17) /* Pointer to memory from sqliteMalloc() */ + + /* Error message codes for OP_Halt */ + #define P5_ConstraintNotNull 1 +@@ -9767,12 +14829,11 @@ typedef struct VdbeOpList VdbeOpList; + #endif + + /* +-** The following macro converts a relative address in the p2 field +-** of a VdbeOp structure into a negative number so that +-** sqlite3VdbeAddOpList() knows that the address is relative. Calling +-** the macro again restores the address. ++** The following macro converts a label returned by sqlite3VdbeMakeLabel() ++** into an index into the Parse.aLabel[] array that contains the resolved ++** address of that label. + */ +-#define ADDR(X) (-1-(X)) ++#define ADDR(X) (~(X)) + + /* + ** The makefile scans the vdbe.c source file and creates the "opcodes.h" +@@ -9781,201 +14842,232 @@ typedef struct VdbeOpList VdbeOpList; + /************** Include opcodes.h in the middle of vdbe.h ********************/ + /************** Begin file opcodes.h *****************************************/ + /* Automatically generated. Do not edit */ +-/* See the mkopcodeh.awk script for details */ +-#define OP_Function 1 /* synopsis: r[P3]=func(r[P2@P5]) */ +-#define OP_Savepoint 2 +-#define OP_AutoCommit 3 +-#define OP_Transaction 4 +-#define OP_SorterNext 5 +-#define OP_PrevIfOpen 6 +-#define OP_NextIfOpen 7 +-#define OP_Prev 8 +-#define OP_Next 9 +-#define OP_AggStep 10 /* synopsis: accum=r[P3] step(r[P2@P5]) */ +-#define OP_Checkpoint 11 +-#define OP_JournalMode 12 +-#define OP_Vacuum 13 +-#define OP_VFilter 14 /* synopsis: iplan=r[P3] zplan='P4' */ +-#define OP_VUpdate 15 /* synopsis: data=r[P3@P2] */ +-#define OP_Goto 16 +-#define OP_Gosub 17 +-#define OP_Return 18 ++/* See the tool/mkopcodeh.tcl script for details */ ++#define OP_Savepoint 0 ++#define OP_AutoCommit 1 ++#define OP_Transaction 2 ++#define OP_SorterNext 3 /* jump */ ++#define OP_Prev 4 /* jump */ ++#define OP_Next 5 /* jump */ ++#define OP_Checkpoint 6 ++#define OP_JournalMode 7 ++#define OP_Vacuum 8 ++#define OP_VFilter 9 /* jump, synopsis: iplan=r[P3] zplan='P4' */ ++#define OP_VUpdate 10 /* synopsis: data=r[P3@P2] */ ++#define OP_Goto 11 /* jump */ ++#define OP_Gosub 12 /* jump */ ++#define OP_InitCoroutine 13 /* jump */ ++#define OP_Yield 14 /* jump */ ++#define OP_MustBeInt 15 /* jump */ ++#define OP_Jump 16 /* jump */ ++#define OP_Once 17 /* jump */ ++#define OP_If 18 /* jump */ + #define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */ +-#define OP_InitCoroutine 20 +-#define OP_EndCoroutine 21 +-#define OP_Yield 22 +-#define OP_HaltIfNull 23 /* synopsis: if r[P3]=null halt */ +-#define OP_Halt 24 +-#define OP_Integer 25 /* synopsis: r[P2]=P1 */ +-#define OP_Int64 26 /* synopsis: r[P2]=P4 */ +-#define OP_String 27 /* synopsis: r[P2]='P4' (len=P1) */ +-#define OP_Null 28 /* synopsis: r[P2..P3]=NULL */ +-#define OP_SoftNull 29 /* synopsis: r[P1]=NULL */ +-#define OP_Blob 30 /* synopsis: r[P2]=P4 (len=P1) */ +-#define OP_Variable 31 /* synopsis: r[P2]=parameter(P1,P4) */ +-#define OP_Move 32 /* synopsis: r[P2@P3]=r[P1@P3] */ +-#define OP_Copy 33 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */ +-#define OP_SCopy 34 /* synopsis: r[P2]=r[P1] */ +-#define OP_ResultRow 35 /* synopsis: output=r[P1@P2] */ +-#define OP_CollSeq 36 +-#define OP_AddImm 37 /* synopsis: r[P1]=r[P1]+P2 */ +-#define OP_MustBeInt 38 +-#define OP_RealAffinity 39 +-#define OP_Cast 40 /* synopsis: affinity(r[P1]) */ +-#define OP_Permutation 41 +-#define OP_Compare 42 /* synopsis: r[P1@P3] <-> r[P2@P3] */ +-#define OP_Jump 43 +-#define OP_Once 44 +-#define OP_If 45 +-#define OP_IfNot 46 +-#define OP_Column 47 /* synopsis: r[P3]=PX */ +-#define OP_Affinity 48 /* synopsis: affinity(r[P1@P2]) */ +-#define OP_MakeRecord 49 /* synopsis: r[P3]=mkrec(r[P1@P2]) */ +-#define OP_Count 50 /* synopsis: r[P2]=count() */ +-#define OP_ReadCookie 51 +-#define OP_SetCookie 52 +-#define OP_ReopenIdx 53 /* synopsis: root=P2 iDb=P3 */ +-#define OP_OpenRead 54 /* synopsis: root=P2 iDb=P3 */ +-#define OP_OpenWrite 55 /* synopsis: root=P2 iDb=P3 */ +-#define OP_OpenAutoindex 56 /* synopsis: nColumn=P2 */ +-#define OP_OpenEphemeral 57 /* synopsis: nColumn=P2 */ +-#define OP_SorterOpen 58 +-#define OP_SequenceTest 59 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */ +-#define OP_OpenPseudo 60 /* synopsis: P3 columns in r[P2] */ +-#define OP_Close 61 +-#define OP_SeekLT 62 /* synopsis: key=r[P3@P4] */ +-#define OP_SeekLE 63 /* synopsis: key=r[P3@P4] */ +-#define OP_SeekGE 64 /* synopsis: key=r[P3@P4] */ +-#define OP_SeekGT 65 /* synopsis: key=r[P3@P4] */ +-#define OP_Seek 66 /* synopsis: intkey=r[P2] */ +-#define OP_NoConflict 67 /* synopsis: key=r[P3@P4] */ +-#define OP_NotFound 68 /* synopsis: key=r[P3@P4] */ +-#define OP_Found 69 /* synopsis: key=r[P3@P4] */ +-#define OP_NotExists 70 /* synopsis: intkey=r[P3] */ +-#define OP_Or 71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */ +-#define OP_And 72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */ +-#define OP_Sequence 73 /* synopsis: r[P2]=cursor[P1].ctr++ */ +-#define OP_NewRowid 74 /* synopsis: r[P2]=rowid */ +-#define OP_Insert 75 /* synopsis: intkey=r[P3] data=r[P2] */ +-#define OP_IsNull 76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */ +-#define OP_NotNull 77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */ +-#define OP_Ne 78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */ +-#define OP_Eq 79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */ +-#define OP_Gt 80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */ +-#define OP_Le 81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */ +-#define OP_Lt 82 /* same as TK_LT, synopsis: if r[P1] =r[P3] goto P2 */ +-#define OP_InsertInt 84 /* synopsis: intkey=P3 data=r[P2] */ +-#define OP_BitAnd 85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */ +-#define OP_BitOr 86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */ +-#define OP_ShiftLeft 87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]< >r[P1] */ +-#define OP_Add 89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */ +-#define OP_Subtract 90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */ +-#define OP_Multiply 91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */ +-#define OP_Divide 92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */ +-#define OP_Remainder 93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */ +-#define OP_Concat 94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */ +-#define OP_Delete 95 +-#define OP_BitNot 96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */ +-#define OP_String8 97 /* same as TK_STRING, synopsis: r[P2]='P4' */ +-#define OP_ResetCount 98 +-#define OP_SorterCompare 99 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */ +-#define OP_SorterData 100 /* synopsis: r[P2]=data */ +-#define OP_RowKey 101 /* synopsis: r[P2]=key */ +-#define OP_RowData 102 /* synopsis: r[P2]=data */ +-#define OP_Rowid 103 /* synopsis: r[P2]=rowid */ +-#define OP_NullRow 104 +-#define OP_Last 105 +-#define OP_SorterSort 106 +-#define OP_Sort 107 +-#define OP_Rewind 108 +-#define OP_SorterInsert 109 +-#define OP_IdxInsert 110 /* synopsis: key=r[P2] */ +-#define OP_IdxDelete 111 /* synopsis: key=r[P2@P3] */ +-#define OP_IdxRowid 112 /* synopsis: r[P2]=rowid */ +-#define OP_IdxLE 113 /* synopsis: key=r[P3@P4] */ +-#define OP_IdxGT 114 /* synopsis: key=r[P3@P4] */ +-#define OP_IdxLT 115 /* synopsis: key=r[P3@P4] */ +-#define OP_IdxGE 116 /* synopsis: key=r[P3@P4] */ +-#define OP_Destroy 117 +-#define OP_Clear 118 +-#define OP_ResetSorter 119 +-#define OP_CreateIndex 120 /* synopsis: r[P2]=root iDb=P1 */ +-#define OP_CreateTable 121 /* synopsis: r[P2]=root iDb=P1 */ +-#define OP_ParseSchema 122 +-#define OP_LoadAnalysis 123 +-#define OP_DropTable 124 +-#define OP_DropIndex 125 +-#define OP_DropTrigger 126 +-#define OP_IntegrityCk 127 +-#define OP_RowSetAdd 128 /* synopsis: rowset(P1)=r[P2] */ +-#define OP_RowSetRead 129 /* synopsis: r[P3]=rowset(P1) */ +-#define OP_RowSetTest 130 /* synopsis: if r[P3] in rowset(P1) goto P2 */ +-#define OP_Program 131 +-#define OP_Param 132 +-#define OP_Real 133 /* same as TK_FLOAT, synopsis: r[P2]=P4 */ +-#define OP_FkCounter 134 /* synopsis: fkctr[P1]+=P2 */ +-#define OP_FkIfZero 135 /* synopsis: if fkctr[P1]==0 goto P2 */ +-#define OP_MemMax 136 /* synopsis: r[P1]=max(r[P1],r[P2]) */ +-#define OP_IfPos 137 /* synopsis: if r[P1]>0 goto P2 */ +-#define OP_IfNeg 138 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2 */ +-#define OP_IfNotZero 139 /* synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2 */ +-#define OP_DecrJumpZero 140 /* synopsis: if (--r[P1])==0 goto P2 */ +-#define OP_JumpZeroIncr 141 /* synopsis: if (r[P1]++)==0 ) goto P2 */ +-#define OP_AggFinal 142 /* synopsis: accum=r[P1] N=P2 */ +-#define OP_IncrVacuum 143 +-#define OP_Expire 144 +-#define OP_TableLock 145 /* synopsis: iDb=P1 root=P2 write=P3 */ +-#define OP_VBegin 146 +-#define OP_VCreate 147 +-#define OP_VDestroy 148 +-#define OP_VOpen 149 +-#define OP_VColumn 150 /* synopsis: r[P3]=vcolumn(P2) */ +-#define OP_VNext 151 +-#define OP_VRename 152 +-#define OP_Pagecount 153 +-#define OP_MaxPgcnt 154 +-#define OP_Init 155 /* synopsis: Start at P2 */ +-#define OP_Noop 156 +-#define OP_Explain 157 +- ++#define OP_IfNot 20 /* jump */ ++#define OP_IfNullRow 21 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */ ++#define OP_SeekLT 22 /* jump, synopsis: key=r[P3@P4] */ ++#define OP_SeekLE 23 /* jump, synopsis: key=r[P3@P4] */ ++#define OP_SeekGE 24 /* jump, synopsis: key=r[P3@P4] */ ++#define OP_SeekGT 25 /* jump, synopsis: key=r[P3@P4] */ ++#define OP_IfNoHope 26 /* jump, synopsis: key=r[P3@P4] */ ++#define OP_NoConflict 27 /* jump, synopsis: key=r[P3@P4] */ ++#define OP_NotFound 28 /* jump, synopsis: key=r[P3@P4] */ ++#define OP_Found 29 /* jump, synopsis: key=r[P3@P4] */ ++#define OP_SeekRowid 30 /* jump, synopsis: intkey=r[P3] */ ++#define OP_NotExists 31 /* jump, synopsis: intkey=r[P3] */ ++#define OP_Last 32 /* jump */ ++#define OP_IfSmaller 33 /* jump */ ++#define OP_SorterSort 34 /* jump */ ++#define OP_Sort 35 /* jump */ ++#define OP_Rewind 36 /* jump */ ++#define OP_IdxLE 37 /* jump, synopsis: key=r[P3@P4] */ ++#define OP_IdxGT 38 /* jump, synopsis: key=r[P3@P4] */ ++#define OP_IdxLT 39 /* jump, synopsis: key=r[P3@P4] */ ++#define OP_IdxGE 40 /* jump, synopsis: key=r[P3@P4] */ ++#define OP_RowSetRead 41 /* jump, synopsis: r[P3]=rowset(P1) */ ++#define OP_RowSetTest 42 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */ ++#define OP_Or 43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */ ++#define OP_And 44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */ ++#define OP_Program 45 /* jump */ ++#define OP_FkIfZero 46 /* jump, synopsis: if fkctr[P1]==0 goto P2 */ ++#define OP_IfPos 47 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */ ++#define OP_IfNotZero 48 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */ ++#define OP_DecrJumpZero 49 /* jump, synopsis: if (--r[P1])==0 goto P2 */ ++#define OP_IsNull 50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */ ++#define OP_NotNull 51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */ ++#define OP_Ne 52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */ ++#define OP_Eq 53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */ ++#define OP_Gt 54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */ ++#define OP_Le 55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */ ++#define OP_Lt 56 /* jump, same as TK_LT, synopsis: IF r[P3] =r[P1] */ ++#define OP_ElseNotEq 58 /* jump, same as TK_ESCAPE */ ++#define OP_IncrVacuum 59 /* jump */ ++#define OP_VNext 60 /* jump */ ++#define OP_Init 61 /* jump, synopsis: Start at P2 */ ++#define OP_PureFunc0 62 ++#define OP_Function0 63 /* synopsis: r[P3]=func(r[P2@P5]) */ ++#define OP_PureFunc 64 ++#define OP_Function 65 /* synopsis: r[P3]=func(r[P2@P5]) */ ++#define OP_Return 66 ++#define OP_EndCoroutine 67 ++#define OP_HaltIfNull 68 /* synopsis: if r[P3]=null halt */ ++#define OP_Halt 69 ++#define OP_Integer 70 /* synopsis: r[P2]=P1 */ ++#define OP_Int64 71 /* synopsis: r[P2]=P4 */ ++#define OP_String 72 /* synopsis: r[P2]='P4' (len=P1) */ ++#define OP_Null 73 /* synopsis: r[P2..P3]=NULL */ ++#define OP_SoftNull 74 /* synopsis: r[P1]=NULL */ ++#define OP_Blob 75 /* synopsis: r[P2]=P4 (len=P1) */ ++#define OP_Variable 76 /* synopsis: r[P2]=parameter(P1,P4) */ ++#define OP_Move 77 /* synopsis: r[P2@P3]=r[P1@P3] */ ++#define OP_Copy 78 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */ ++#define OP_SCopy 79 /* synopsis: r[P2]=r[P1] */ ++#define OP_IntCopy 80 /* synopsis: r[P2]=r[P1] */ ++#define OP_ResultRow 81 /* synopsis: output=r[P1@P2] */ ++#define OP_CollSeq 82 ++#define OP_AddImm 83 /* synopsis: r[P1]=r[P1]+P2 */ ++#define OP_RealAffinity 84 ++#define OP_Cast 85 /* synopsis: affinity(r[P1]) */ ++#define OP_Permutation 86 ++#define OP_Compare 87 /* synopsis: r[P1@P3] <-> r[P2@P3] */ ++#define OP_IsTrue 88 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */ ++#define OP_Offset 89 /* synopsis: r[P3] = sqlite_offset(P1) */ ++#define OP_Column 90 /* synopsis: r[P3]=PX */ ++#define OP_Affinity 91 /* synopsis: affinity(r[P1@P2]) */ ++#define OP_MakeRecord 92 /* synopsis: r[P3]=mkrec(r[P1@P2]) */ ++#define OP_Count 93 /* synopsis: r[P2]=count() */ ++#define OP_ReadCookie 94 ++#define OP_SetCookie 95 ++#define OP_BitAnd 96 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */ ++#define OP_BitOr 97 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */ ++#define OP_ShiftLeft 98 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]< >r[P1] */ ++#define OP_Add 100 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */ ++#define OP_Subtract 101 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */ ++#define OP_Multiply 102 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */ ++#define OP_Divide 103 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */ ++#define OP_Remainder 104 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */ ++#define OP_Concat 105 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */ ++#define OP_ReopenIdx 106 /* synopsis: root=P2 iDb=P3 */ ++#define OP_BitNot 107 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */ ++#define OP_OpenRead 108 /* synopsis: root=P2 iDb=P3 */ ++#define OP_OpenWrite 109 /* synopsis: root=P2 iDb=P3 */ ++#define OP_String8 110 /* same as TK_STRING, synopsis: r[P2]='P4' */ ++#define OP_OpenDup 111 ++#define OP_OpenAutoindex 112 /* synopsis: nColumn=P2 */ ++#define OP_OpenEphemeral 113 /* synopsis: nColumn=P2 */ ++#define OP_SorterOpen 114 ++#define OP_SequenceTest 115 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */ ++#define OP_OpenPseudo 116 /* synopsis: P3 columns in r[P2] */ ++#define OP_Close 117 ++#define OP_ColumnsUsed 118 ++#define OP_SeekHit 119 /* synopsis: seekHit=P2 */ ++#define OP_Sequence 120 /* synopsis: r[P2]=cursor[P1].ctr++ */ ++#define OP_NewRowid 121 /* synopsis: r[P2]=rowid */ ++#define OP_Insert 122 /* synopsis: intkey=r[P3] data=r[P2] */ ++#define OP_Delete 123 ++#define OP_ResetCount 124 ++#define OP_SorterCompare 125 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */ ++#define OP_SorterData 126 /* synopsis: r[P2]=data */ ++#define OP_RowData 127 /* synopsis: r[P2]=data */ ++#define OP_Rowid 128 /* synopsis: r[P2]=rowid */ ++#define OP_NullRow 129 ++#define OP_SeekEnd 130 ++#define OP_SorterInsert 131 /* synopsis: key=r[P2] */ ++#define OP_IdxInsert 132 /* synopsis: key=r[P2] */ ++#define OP_IdxDelete 133 /* synopsis: key=r[P2@P3] */ ++#define OP_DeferredSeek 134 /* synopsis: Move P3 to P1.rowid if needed */ ++#define OP_IdxRowid 135 /* synopsis: r[P2]=rowid */ ++#define OP_Destroy 136 ++#define OP_Clear 137 ++#define OP_ResetSorter 138 ++#define OP_CreateBtree 139 /* synopsis: r[P2]=root iDb=P1 flags=P3 */ ++#define OP_SqlExec 140 ++#define OP_ParseSchema 141 ++#define OP_LoadAnalysis 142 ++#define OP_DropTable 143 ++#define OP_DropIndex 144 ++#define OP_Real 145 /* same as TK_FLOAT, synopsis: r[P2]=P4 */ ++#define OP_DropTrigger 146 ++#define OP_IntegrityCk 147 ++#define OP_RowSetAdd 148 /* synopsis: rowset(P1)=r[P2] */ ++#define OP_Param 149 ++#define OP_FkCounter 150 /* synopsis: fkctr[P1]+=P2 */ ++#define OP_MemMax 151 /* synopsis: r[P1]=max(r[P1],r[P2]) */ ++#define OP_OffsetLimit 152 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */ ++#define OP_AggInverse 153 /* synopsis: accum=r[P3] inverse(r[P2@P5]) */ ++#define OP_AggStep 154 /* synopsis: accum=r[P3] step(r[P2@P5]) */ ++#define OP_AggStep1 155 /* synopsis: accum=r[P3] step(r[P2@P5]) */ ++#define OP_AggValue 156 /* synopsis: r[P3]=value N=P2 */ ++#define OP_AggFinal 157 /* synopsis: accum=r[P1] N=P2 */ ++#define OP_Expire 158 ++#define OP_TableLock 159 /* synopsis: iDb=P1 root=P2 write=P3 */ ++#define OP_VBegin 160 ++#define OP_VCreate 161 ++#define OP_VDestroy 162 ++#define OP_VOpen 163 ++#define OP_VColumn 164 /* synopsis: r[P3]=vcolumn(P2) */ ++#define OP_VRename 165 ++#define OP_Pagecount 166 ++#define OP_MaxPgcnt 167 ++#define OP_Trace 168 ++#define OP_CursorHint 169 ++#define OP_Noop 170 ++#define OP_Explain 171 ++#define OP_Abortable 172 + + /* Properties such as "out2" or "jump" that are specified in + ** comments following the "case" for each opcode in the vdbe.c + ** are encoded into bitvectors as follows: + */ +-#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */ +-#define OPFLG_IN1 0x0002 /* in1: P1 is an input */ +-#define OPFLG_IN2 0x0004 /* in2: P2 is an input */ +-#define OPFLG_IN3 0x0008 /* in3: P3 is an input */ +-#define OPFLG_OUT2 0x0010 /* out2: P2 is an output */ +-#define OPFLG_OUT3 0x0020 /* out3: P3 is an output */ ++#define OPFLG_JUMP 0x01 /* jump: P2 holds jmp target */ ++#define OPFLG_IN1 0x02 /* in1: P1 is an input */ ++#define OPFLG_IN2 0x04 /* in2: P2 is an input */ ++#define OPFLG_IN3 0x08 /* in3: P3 is an input */ ++#define OPFLG_OUT2 0x10 /* out2: P2 is an output */ ++#define OPFLG_OUT3 0x20 /* out3: P3 is an output */ + #define OPFLG_INITIALIZER {\ +-/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,\ +-/* 8 */ 0x01, 0x01, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\ +-/* 16 */ 0x01, 0x01, 0x02, 0x12, 0x01, 0x02, 0x03, 0x08,\ +-/* 24 */ 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10, 0x10,\ +-/* 32 */ 0x00, 0x00, 0x10, 0x00, 0x00, 0x02, 0x03, 0x02,\ +-/* 40 */ 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x00,\ +-/* 48 */ 0x00, 0x00, 0x10, 0x10, 0x08, 0x00, 0x00, 0x00,\ +-/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x09,\ +-/* 64 */ 0x09, 0x09, 0x04, 0x09, 0x09, 0x09, 0x09, 0x26,\ +-/* 72 */ 0x26, 0x10, 0x10, 0x00, 0x03, 0x03, 0x0b, 0x0b,\ +-/* 80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x26, 0x26, 0x26,\ +-/* 88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\ +-/* 96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\ +-/* 104 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x00,\ +-/* 112 */ 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00, 0x00,\ ++/* 0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x10,\ ++/* 8 */ 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03,\ ++/* 16 */ 0x01, 0x01, 0x03, 0x12, 0x03, 0x01, 0x09, 0x09,\ ++/* 24 */ 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,\ ++/* 32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\ ++/* 40 */ 0x01, 0x23, 0x0b, 0x26, 0x26, 0x01, 0x01, 0x03,\ ++/* 48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\ ++/* 56 */ 0x0b, 0x0b, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,\ ++/* 64 */ 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10,\ ++/* 72 */ 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10,\ ++/* 80 */ 0x10, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,\ ++/* 88 */ 0x12, 0x20, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\ ++/* 96 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\ ++/* 104 */ 0x26, 0x26, 0x00, 0x12, 0x00, 0x00, 0x10, 0x00,\ ++/* 112 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ + /* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ +-/* 128 */ 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00, 0x01,\ +-/* 136 */ 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x01,\ +-/* 144 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,\ +-/* 152 */ 0x00, 0x10, 0x10, 0x01, 0x00, 0x00,} ++/* 128 */ 0x10, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10,\ ++/* 136 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\ ++/* 144 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\ ++/* 152 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ ++/* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10,\ ++/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00,} ++ ++/* The sqlite3P2Values() routine is able to run faster if it knows ++** the value of the largest JUMP opcode. The smaller the maximum ++** JUMP opcode the better, so the mkopcodeh.tcl script that ++** generated this include file strives to group all JUMP opcodes ++** together near the beginning of the list. ++*/ ++#define SQLITE_MX_JUMP_OPCODE 61 /* Maximum JUMP opcode */ + + /************** End of opcodes.h *********************************************/ + /************** Continuing where we left off in vdbe.h ***********************/ + ++/* ++** Additional non-public SQLITE_PREPARE_* flags ++*/ ++#define SQLITE_PREPARE_SAVESQL 0x80 /* Preserve SQL text */ ++#define SQLITE_PREPARE_MASK 0x0f /* Mask of public flags */ ++ + /* + ** Prototypes for the VDBE interface. See comments on the implementation + ** for a description of what each of these routines does. +@@ -9984,24 +15076,62 @@ SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*); + SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int); + SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int); + SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int); ++SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe*,int); ++SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe*,int,const char*); ++SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...); + SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int); + SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int); ++SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int); + SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int); +-SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno); ++SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe*,int); ++#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS) ++SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N); ++SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p); ++#else ++# define sqlite3VdbeVerifyNoMallocRequired(A,B) ++# define sqlite3VdbeVerifyNoResultRow(A) ++#endif ++#if defined(SQLITE_DEBUG) ++SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int); ++#else ++# define sqlite3VdbeVerifyAbortable(A,B) ++#endif ++SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno); ++#ifndef SQLITE_OMIT_EXPLAIN ++SQLITE_PRIVATE void sqlite3VdbeExplain(Parse*,u8,const char*,...); ++SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse*); ++SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse*); ++# define ExplainQueryPlan(P) sqlite3VdbeExplain P ++# define ExplainQueryPlanPop(P) sqlite3VdbeExplainPop(P) ++# define ExplainQueryPlanParent(P) sqlite3VdbeExplainParent(P) ++#else ++# define ExplainQueryPlan(P) ++# define ExplainQueryPlanPop(P) ++# define ExplainQueryPlanParent(P) 0 ++# define sqlite3ExplainBreakpoint(A,B) /*no-op*/ ++#endif ++#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN) ++SQLITE_PRIVATE void sqlite3ExplainBreakpoint(const char*,const char*); ++#else ++# define sqlite3ExplainBreakpoint(A,B) /*no-op*/ ++#endif + SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*); ++SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8); + SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1); + SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2); + SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3); +-SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5); ++SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5); + SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr); +-SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr); ++SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr); + SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op); + SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N); ++SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type); + SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*); + SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int); + SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int); +-SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*); ++SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse*); + SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*); ++SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*); + SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*); + SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*); + SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*); +@@ -10018,7 +15148,12 @@ SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int); + SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*)); + SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*); + SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*); +-SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int); ++SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe*); ++SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8); ++#ifdef SQLITE_ENABLE_NORMALIZE ++SQLITE_PRIVATE void sqlite3VdbeAddDblquoteStr(sqlite3*,Vdbe*,const char*); ++SQLITE_PRIVATE int sqlite3VdbeUsesDoubleQuotedString(Vdbe*,const char*); ++#endif + SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*); + SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*); + SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8); +@@ -10027,11 +15162,12 @@ SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int); + SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*); + #endif + SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*); ++SQLITE_PRIVATE int sqlite3BlobCompare(const Mem*, const Mem*); + + SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*); + SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*); + SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int); +-SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **); ++SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*); + + typedef int (*RecordCompare)(int,const void*,UnpackedRecord*); + SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*); +@@ -10040,6 +15176,8 @@ SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*); + SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *); + #endif + ++SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context*); ++ + /* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on + ** each VDBE opcode. + ** +@@ -10080,23 +15218,52 @@ SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe*, const char*, ...); + ** + ** VdbeCoverageNeverTaken(v) // Previous branch is never taken + ** ++** VdbeCoverageNeverNull(v) // Previous three-way branch is only ++** // taken on the first two ways. The ++** // NULL option is not possible ++** ++** VdbeCoverageEqNe(v) // Previous OP_Jump is only interested ++** // in distingishing equal and not-equal. ++** + ** Every VDBE branch operation must be tagged with one of the macros above. + ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and + ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch() + ** routine in vdbe.c, alerting the developer to the missed tag. ++** ++** During testing, the test application will invoke ++** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback ++** routine that is invoked as each bytecode branch is taken. The callback ++** contains the sqlite3.c source line number ov the VdbeCoverage macro and ++** flags to indicate whether or not the branch was taken. The test application ++** is responsible for keeping track of this and reporting byte-code branches ++** that are never taken. ++** ++** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the ++** vdbe.c source file for additional information. + */ + #ifdef SQLITE_VDBE_COVERAGE + SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe*,int); + # define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__) + # define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__) +-# define VdbeCoverageAlwaysTaken(v) sqlite3VdbeSetLineNumber(v,2); +-# define VdbeCoverageNeverTaken(v) sqlite3VdbeSetLineNumber(v,1); ++# define VdbeCoverageAlwaysTaken(v) \ ++ sqlite3VdbeSetLineNumber(v,__LINE__|0x5000000); ++# define VdbeCoverageNeverTaken(v) \ ++ sqlite3VdbeSetLineNumber(v,__LINE__|0x6000000); ++# define VdbeCoverageNeverNull(v) \ ++ sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000); ++# define VdbeCoverageNeverNullIf(v,x) \ ++ if(x)sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000); ++# define VdbeCoverageEqNe(v) \ ++ sqlite3VdbeSetLineNumber(v,__LINE__|0x8000000); + # define VDBE_OFFSET_LINENO(x) (__LINE__+x) + #else + # define VdbeCoverage(v) + # define VdbeCoverageIf(v,x) + # define VdbeCoverageAlwaysTaken(v) + # define VdbeCoverageNeverTaken(v) ++# define VdbeCoverageNeverNull(v) ++# define VdbeCoverageNeverNullIf(v,x) ++# define VdbeCoverageEqNe(v) + # define VDBE_OFFSET_LINENO(x) 0 + #endif + +@@ -10106,8 +15273,12 @@ SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const ch + # define sqlite3VdbeScanStatus(a,b,c,d,e) + #endif + ++#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) ++SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, VdbeOp*); + #endif + ++#endif /* SQLITE_VDBE_H */ ++ + /************** End of vdbe.h ************************************************/ + /************** Continuing where we left off in sqliteInt.h ******************/ + /************** Include pager.h in the middle of sqliteInt.h *****************/ +@@ -10128,8 +15299,8 @@ SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const ch + ** at a time and provides a journal for rollback. + */ + +-#ifndef _PAGER_H_ +-#define _PAGER_H_ ++#ifndef SQLITE_PAGER_H ++#define SQLITE_PAGER_H + + /* + ** Default maximum size for persistent journal files. A negative +@@ -10182,7 +15353,11 @@ typedef struct PgHdr DbPage; + #define PAGER_LOCKINGMODE_EXCLUSIVE 1 + + /* +-** Numeric constants that encode the journalmode. ++** Numeric constants that encode the journalmode. ++** ++** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY) ++** are exposed in the API via the "PRAGMA journal_mode" command and ++** therefore cannot be changed without a compatibility break. + */ + #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */ + #define PAGER_JOURNALMODE_DELETE 0 /* Commit by deleting journal file */ +@@ -10193,22 +15368,28 @@ typedef struct PgHdr DbPage; + #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */ + + /* +-** Flags that make up the mask passed to sqlite3PagerAcquire(). ++** Flags that make up the mask passed to sqlite3PagerGet(). + */ + #define PAGER_GET_NOCONTENT 0x01 /* Do not load data from disk */ + #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */ + + /* + ** Flags for sqlite3PagerSetFlags() ++** ++** Value constraints (enforced via assert()): ++** PAGER_FULLFSYNC == SQLITE_FullFSync ++** PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync ++** PAGER_CACHE_SPILL == SQLITE_CacheSpill + */ + #define PAGER_SYNCHRONOUS_OFF 0x01 /* PRAGMA synchronous=OFF */ + #define PAGER_SYNCHRONOUS_NORMAL 0x02 /* PRAGMA synchronous=NORMAL */ + #define PAGER_SYNCHRONOUS_FULL 0x03 /* PRAGMA synchronous=FULL */ +-#define PAGER_SYNCHRONOUS_MASK 0x03 /* Mask for three values above */ +-#define PAGER_FULLFSYNC 0x04 /* PRAGMA fullfsync=ON */ +-#define PAGER_CKPT_FULLFSYNC 0x08 /* PRAGMA checkpoint_fullfsync=ON */ +-#define PAGER_CACHESPILL 0x10 /* PRAGMA cache_spill=ON */ +-#define PAGER_FLAGS_MASK 0x1c /* All above except SYNCHRONOUS */ ++#define PAGER_SYNCHRONOUS_EXTRA 0x04 /* PRAGMA synchronous=EXTRA */ ++#define PAGER_SYNCHRONOUS_MASK 0x07 /* Mask for four values above */ ++#define PAGER_FULLFSYNC 0x08 /* PRAGMA fullfsync=ON */ ++#define PAGER_CKPT_FULLFSYNC 0x10 /* PRAGMA checkpoint_fullfsync=ON */ ++#define PAGER_CACHESPILL 0x20 /* PRAGMA cache_spill=ON */ ++#define PAGER_FLAGS_MASK 0x38 /* All above except SYNCHRONOUS */ + + /* + ** The remainder of this file contains the declarations of the functions +@@ -10226,14 +15407,18 @@ SQLITE_PRIVATE int sqlite3PagerOpen( + int, + void(*)(DbPage*) + ); +-SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager); ++SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*); + SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*); + + /* Functions used to configure a Pager object. */ +-SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *); ++SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *); + SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int); ++#ifdef SQLITE_HAS_CODEC ++SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager*,Pager*); ++#endif + SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int); + SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int); ++SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager*, int); + SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64); + SQLITE_PRIVATE void sqlite3PagerShrink(Pager*); + SQLITE_PRIVATE void sqlite3PagerSetFlags(Pager*,unsigned); +@@ -10243,14 +15428,15 @@ SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*); + SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*); + SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64); + SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*); ++SQLITE_PRIVATE int sqlite3PagerFlush(Pager*); + + /* Functions used to obtain and release page references. */ +-SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag); +-#define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0) ++SQLITE_PRIVATE int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag); + SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno); + SQLITE_PRIVATE void sqlite3PagerRef(DbPage*); + SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*); + SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*); ++SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage*); + + /* Operations on page references. */ + SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*); +@@ -10273,11 +15459,22 @@ SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint); + SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager); + + #ifndef SQLITE_OMIT_WAL +-SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*); ++SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*); + SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager); + SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager); + SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen); +-SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager); ++SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*); ++# ifdef SQLITE_ENABLE_SNAPSHOT ++SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot); ++SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot); ++SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager); ++SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot); ++SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager); ++# endif ++#endif ++ ++#ifdef SQLITE_DIRECT_OVERFLOW_READ ++SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno); + #endif + + #ifdef SQLITE_ENABLE_ZIPVFS +@@ -10287,18 +15484,25 @@ SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager); + /* Functions used to query pager state and configuration. */ + SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*); + SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager*); +-SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*); ++#ifdef SQLITE_DEBUG ++SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*); ++#endif + SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*); + SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*, int); +-SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*); ++SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager*); + SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*); ++SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*); + SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*); +-SQLITE_PRIVATE int sqlite3PagerNosync(Pager*); + SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*); + SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*); + SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *); +-SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *); ++SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*); + SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *); ++#ifdef SQLITE_ENABLE_SETLK_TIMEOUT ++SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager); ++#else ++# define sqlite3PagerResetLockTimeout(X) ++#endif + + /* Functions used to truncate the database file. */ + SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno); +@@ -10324,7 +15528,7 @@ SQLITE_PRIVATE void sqlite3PagerRefdump(Pager*); + # define enable_simulated_io_errors() + #endif + +-#endif /* _PAGER_H_ */ ++#endif /* SQLITE_PAGER_H */ + + /************** End of pager.h ***********************************************/ + /************** Continuing where we left off in sqliteInt.h ******************/ +@@ -10358,7 +15562,8 @@ struct PgHdr { + sqlite3_pcache_page *pPage; /* Pcache object page handle */ + void *pData; /* Page data */ + void *pExtra; /* Extra content */ +- PgHdr *pDirty; /* Transient list of dirty pages */ ++ PCache *pCache; /* PRIVATE: Cache that owns this page */ ++ PgHdr *pDirty; /* Transient list of dirty sorted by pgno */ + Pager *pPager; /* The pager this page is part of */ + Pgno pgno; /* Page number for this page */ + #ifdef SQLITE_CHECK_PAGES +@@ -10367,25 +15572,27 @@ struct PgHdr { + u16 flags; /* PGHDR flags defined below */ + + /********************************************************************** +- ** Elements above are public. All that follows is private to pcache.c +- ** and should not be accessed by other modules. ++ ** Elements above, except pCache, are public. All that follow are ++ ** private to pcache.c and should not be accessed by other modules. ++ ** pCache is grouped with the public elements for efficiency. + */ + i16 nRef; /* Number of users of this page */ +- PCache *pCache; /* Cache that owns this page */ +- + PgHdr *pDirtyNext; /* Next element in list of dirty pages */ + PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */ ++ /* NB: pDirtyNext and pDirtyPrev are undefined if the ++ ** PgHdr object is not dirty */ + }; + + /* Bit values for PgHdr.flags */ +-#define PGHDR_DIRTY 0x002 /* Page has changed */ +-#define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before +- ** writing this page to the database */ +-#define PGHDR_NEED_READ 0x008 /* Content is unread */ +-#define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */ +-#define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */ ++#define PGHDR_CLEAN 0x001 /* Page not on the PCache.pDirty list */ ++#define PGHDR_DIRTY 0x002 /* Page is on the PCache.pDirty list */ ++#define PGHDR_WRITEABLE 0x004 /* Journaled and ready to modify */ ++#define PGHDR_NEED_SYNC 0x008 /* Fsync the rollback journal before ++ ** writing this page to the database */ ++#define PGHDR_DONT_WRITE 0x010 /* Do not write content to disk */ ++#define PGHDR_MMAP 0x020 /* This is an mmap page object */ + +-#define PGHDR_MMAP 0x040 /* This is an mmap page object */ ++#define PGHDR_WAL_APPEND 0x040 /* Appended to wal file */ + + /* Initialize and shutdown the page cache subsystem */ + SQLITE_PRIVATE int sqlite3PcacheInitialize(void); +@@ -10429,6 +15636,7 @@ SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache + SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */ + SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */ + SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */ ++SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache*); + + /* Change a page number. Used by incr-vacuum. */ + SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno); +@@ -10467,6 +15675,11 @@ SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*); + SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)); + #endif + ++#if defined(SQLITE_DEBUG) ++/* Check invariants on a PgHdr object */ ++SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr*); ++#endif ++ + /* Set and get the suggested cache-size for the specified pager-cache. + ** + ** If no global maximum is configured, then the system attempts to limit +@@ -10478,6 +15691,13 @@ SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int); + SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *); + #endif + ++/* Set or get the suggested spill-size for the specified pager-cache. ++** ++** The spill-size is the minimum number of pages in cache before the cache ++** will attempt to spill dirty pages by calling xStress. ++*/ ++SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *, int); ++ + /* Free up as much memory as possible from the page cache */ + SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*); + +@@ -10496,11 +15716,17 @@ SQLITE_PRIVATE void sqlite3PCacheSetDefault(void); + SQLITE_PRIVATE int sqlite3HeaderSizePcache(void); + SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void); + ++/* Number of dirty pages as a percentage of the configured cache size */ ++SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*); ++ ++#ifdef SQLITE_DIRECT_OVERFLOW_READ ++SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache); ++#endif ++ + #endif /* _PCACHE_H_ */ + + /************** End of pcache.h **********************************************/ + /************** Continuing where we left off in sqliteInt.h ******************/ +- + /************** Include os.h in the middle of sqliteInt.h ********************/ + /************** Begin file os.h **********************************************/ + /* +@@ -10546,8 +15772,8 @@ SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void); + ** This file contains pre-processor directives related to operating system + ** detection and/or setup. + */ +-#ifndef _OS_SETUP_H_ +-#define _OS_SETUP_H_ ++#ifndef SQLITE_OS_SETUP_H ++#define SQLITE_OS_SETUP_H + + /* + ** Figure out if we are dealing with Unix, Windows, or some other operating +@@ -10587,7 +15813,7 @@ SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void); + # endif + #endif + +-#endif /* _OS_SETUP_H_ */ ++#endif /* SQLITE_OS_SETUP_H */ + + /************** End of os_setup.h ********************************************/ + /************** Continuing where we left off in os.h *************************/ +@@ -10726,7 +15952,7 @@ SQLITE_PRIVATE int sqlite3OsInit(void); + /* + ** Functions for accessing sqlite3_file methods + */ +-SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*); ++SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*); + SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset); + SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset); + SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size); +@@ -10740,10 +15966,12 @@ SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*); + #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0 + SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id); + SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id); ++#ifndef SQLITE_OMIT_WAL + SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **); + SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int); + SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id); + SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int); ++#endif /* SQLITE_OMIT_WAL */ + SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **); + SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *); + +@@ -10763,6 +15991,7 @@ SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *); + #endif /* SQLITE_OMIT_LOAD_EXTENSION */ + SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *); + SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int); ++SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*); + SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*); + + /* +@@ -10770,7 +15999,7 @@ SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*); + ** sqlite3_malloc() to obtain space for the file-handle structure. + */ + SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*); +-SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *); ++SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *); + + #endif /* _SQLITE_OS_H_ */ + +@@ -10852,6 +16081,36 @@ SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *); + /************** End of mutex.h ***********************************************/ + /************** Continuing where we left off in sqliteInt.h ******************/ + ++/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default ++** synchronous setting to EXTRA. It is no longer supported. ++*/ ++#ifdef SQLITE_EXTRA_DURABLE ++# warning Use SQLITE_DEFAULT_SYNCHRONOUS=3 instead of SQLITE_EXTRA_DURABLE ++# define SQLITE_DEFAULT_SYNCHRONOUS 3 ++#endif ++ ++/* ++** Default synchronous levels. ++** ++** Note that (for historcal reasons) the PAGER_SYNCHRONOUS_* macros differ ++** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1. ++** ++** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS ++** OFF 1 0 ++** NORMAL 2 1 ++** FULL 3 2 ++** EXTRA 4 3 ++** ++** The "PRAGMA synchronous" statement also uses the zero-based numbers. ++** In other words, the zero-based numbers are used for all external interfaces ++** and the one-based values are used internally. ++*/ ++#ifndef SQLITE_DEFAULT_SYNCHRONOUS ++# define SQLITE_DEFAULT_SYNCHRONOUS 2 ++#endif ++#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS ++# define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS ++#endif + + /* + ** Each database file to be accessed by the system is an instance +@@ -10861,9 +16120,10 @@ SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *); + ** databases may be attached. + */ + struct Db { +- char *zName; /* Name of this database */ ++ char *zDbSName; /* Name of this database. (schema name, not filename) */ + Btree *pBt; /* The B*Tree structure for this database file */ + u8 safety_level; /* How aggressive at syncing data to disk */ ++ u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */ + Schema *pSchema; /* Pointer to database schema (possibly shared) */ + }; + +@@ -10874,7 +16134,7 @@ struct Db { + ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing. + ** In shared cache mode, a single Schema object can be shared by multiple + ** Btrees that refer to the same underlying BtShared object. +-** ++** + ** Schema objects are automatically deallocated when the last Btree that + ** references them is destroyed. The TEMP Schema is manually freed by + ** sqlite3_close(). +@@ -10899,7 +16159,7 @@ struct Schema { + }; + + /* +-** These macros can be used to test, set, or clear bits in the ++** These macros can be used to test, set, or clear bits in the + ** Db.pSchema->flags field. + */ + #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P)) +@@ -10920,6 +16180,7 @@ struct Schema { + #define DB_SchemaLoaded 0x0001 /* The schema has been loaded */ + #define DB_UnresetViews 0x0002 /* Some views have defined column names */ + #define DB_Empty 0x0004 /* The file is empty (length 0 bytes) */ ++#define DB_ResetWanted 0x0008 /* Reset the schema when nSchemaLock==0 */ + + /* + ** The number of different kinds of things that can be limited +@@ -10948,12 +16209,12 @@ struct Schema { + ** lookaside allocations are not used to construct the schema objects. + */ + struct Lookaside { ++ u32 bDisable; /* Only operate the lookaside when zero */ + u16 sz; /* Size of each buffer in bytes */ +- u8 bEnabled; /* False to disable new lookaside allocations */ + u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */ +- int nOut; /* Number of buffers currently checked out */ +- int mxOut; /* Highwater mark for nOut */ +- int anStat[3]; /* 0: hits. 1: size misses. 2: full misses */ ++ u32 nSlot; /* Number of lookaside slots allocated */ ++ u32 anStat[3]; /* 0: hits. 1: size misses. 2: full misses */ ++ LookasideSlot *pInit; /* List of buffers not previously used */ + LookasideSlot *pFree; /* List of available buffers */ + void *pStart; /* First byte of available memory space */ + void *pEnd; /* First byte past end of available space */ +@@ -10963,14 +16224,18 @@ struct LookasideSlot { + }; + + /* +-** A hash table for function definitions. ++** A hash table for built-in function definitions. (Application-defined ++** functions use a regular table table from hash.h.) + ** + ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots. +-** Collisions are on the FuncDef.pHash chain. ++** Collisions are on the FuncDef.u.pHash chain. Use the SQLITE_FUNC_HASH() ++** macro to compute a hash on the function name. + */ ++#define SQLITE_FUNC_HASH_SZ 23 + struct FuncDefHash { +- FuncDef *a[23]; /* Hash table for functions */ ++ FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */ + }; ++#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ) + + #ifdef SQLITE_USER_AUTHENTICATION + /* +@@ -11010,6 +16275,18 @@ SQLITE_PRIVATE void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**); + const char*); + #endif + ++#ifndef SQLITE_OMIT_DEPRECATED ++/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing ++** in the style of sqlite3_trace() ++*/ ++#define SQLITE_TRACE_LEGACY 0x40 /* Use the legacy xTrace */ ++#define SQLITE_TRACE_XPROFILE 0x80 /* Use the legacy xProfile */ ++#else ++#define SQLITE_TRACE_LEGACY 0 ++#define SQLITE_TRACE_XPROFILE 0 ++#endif /* SQLITE_OMIT_DEPRECATED */ ++#define SQLITE_TRACE_NONLEGACY_MASK 0x0f /* Normal flags */ ++ + + /* + ** Each database connection is an instance of the following structure. +@@ -11021,22 +16298,29 @@ struct sqlite3 { + sqlite3_mutex *mutex; /* Connection mutex */ + Db *aDb; /* All backends */ + int nDb; /* Number of backends currently in use */ +- int flags; /* Miscellaneous flags. See below */ ++ u32 mDbFlags; /* flags recording internal state */ ++ u64 flags; /* flags settable by pragmas. See below */ + i64 lastRowid; /* ROWID of most recent insert (see above) */ + i64 szMmap; /* Default mmap_size setting */ ++ u32 nSchemaLock; /* Do not reset the schema when non-zero */ + unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */ + int errCode; /* Most recent error code (SQLITE_*) */ + int errMask; /* & result codes with this before returning */ ++ int iSysErrno; /* Errno value from last system error */ + u16 dbOptFlags; /* Flags to enable/disable optimizations */ + u8 enc; /* Text encoding */ + u8 autoCommit; /* The auto-commit flag. */ + u8 temp_store; /* 1: file 2: memory 0: default */ + u8 mallocFailed; /* True if we have seen a malloc failure */ ++ u8 bBenignMalloc; /* Do not require OOMs if true */ + u8 dfltLockMode; /* Default locking-mode for attached dbs */ + signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */ + u8 suppressErr; /* Do not issue error messages if true */ + u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */ + u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */ ++ u8 mTrace; /* zero or more SQLITE_TRACE flags */ ++ u8 noSharedCache; /* True if no shared-cache backends */ ++ u8 nSqlExec; /* Number of pending OP_SqlExec opcodes */ + int nextPagesize; /* Pagesize after VACUUM if >0 */ + u32 magic; /* Magic number for detect library misuse */ + int nChange; /* Value returned by sqlite3_changes() */ +@@ -11047,8 +16331,9 @@ struct sqlite3 { + int newTnum; /* Rootpage of table being initialized */ + u8 iDb; /* Which db file is being initialized */ + u8 busy; /* TRUE if currently initializing */ +- u8 orphanTrigger; /* Last statement is orphaned TEMP trigger */ +- u8 imposterTable; /* Building an imposter table */ ++ unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */ ++ unsigned imposterTable : 1; /* Building an imposter table */ ++ unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */ + } init; + int nVdbeActive; /* Number of VDBEs currently running */ + int nVdbeRead; /* Number of active VDBEs that read or write */ +@@ -11057,16 +16342,26 @@ struct sqlite3 { + int nVDestroy; /* Number of active OP_VDestroy operations */ + int nExtension; /* Number of loaded extensions */ + void **aExtension; /* Array of shared library handles */ +- void (*xTrace)(void*,const char*); /* Trace function */ ++ int (*xTrace)(u32,void*,void*,void*); /* Trace function */ + void *pTraceArg; /* Argument to the trace function */ ++#ifndef SQLITE_OMIT_DEPRECATED + void (*xProfile)(void*,const char*,u64); /* Profiling function */ + void *pProfileArg; /* Argument to profile function */ +- void *pCommitArg; /* Argument to xCommitCallback() */ ++#endif ++ void *pCommitArg; /* Argument to xCommitCallback() */ + int (*xCommitCallback)(void*); /* Invoked at every commit. */ +- void *pRollbackArg; /* Argument to xRollbackCallback() */ ++ void *pRollbackArg; /* Argument to xRollbackCallback() */ + void (*xRollbackCallback)(void*); /* Invoked at every commit. */ + void *pUpdateArg; + void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64); ++ Parse *pParse; /* Current parse */ ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK ++ void *pPreUpdateArg; /* First argument to xPreUpdateCallback */ ++ void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */ ++ void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64 ++ ); ++ PreUpdate *pPreUpdate; /* Context for active pre-update callback */ ++#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + #ifndef SQLITE_OMIT_WAL + int (*xWalCallback)(void *, sqlite3 *, const char *, int); + void *pWalArg; +@@ -11094,9 +16389,9 @@ struct sqlite3 { + Hash aModule; /* populated by sqlite3_create_module() */ + VtabCtx *pVtabCtx; /* Context for active vtab connect/create */ + VTable **aVTrans; /* Virtual tables with open transactions */ +- VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */ ++ VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */ + #endif +- FuncDefHash aFunc; /* Hash table of connection functions */ ++ Hash aFunc; /* Hash table of connection functions */ + Hash aCollSeq; /* All collating sequences */ + BusyHandler busyHandler; /* Busy callback */ + Db aDbStatic[2]; /* Static space for the 2 default backends */ +@@ -11108,8 +16403,8 @@ struct sqlite3 { + i64 nDeferredImmCons; /* Net deferred immediate constraints */ + int *pnBytesFreed; /* If not NULL, increment this in DbFree() */ + #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY +- /* The following variables are all protected by the STATIC_MASTER +- ** mutex, not by sqlite3.mutex. They are used by code in notify.c. ++ /* The following variables are all protected by the STATIC_MASTER ++ ** mutex, not by sqlite3.mutex. They are used by code in notify.c. + ** + ** When X.pUnlockConnection==Y, that means that X is waiting for Y to + ** unlock so that it can proceed. +@@ -11137,39 +16432,64 @@ struct sqlite3 { + + /* + ** Possible values for the sqlite3.flags. +-*/ +-#define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */ +-#define SQLITE_InternChanges 0x00000002 /* Uncommitted Hash table changes */ +-#define SQLITE_FullFSync 0x00000004 /* Use full fsync on the backend */ +-#define SQLITE_CkptFullFSync 0x00000008 /* Use full fsync for checkpoint */ +-#define SQLITE_CacheSpill 0x00000010 /* OK to spill pager cache */ +-#define SQLITE_FullColNames 0x00000020 /* Show full column names on SELECT */ ++** ++** Value constraints (enforced via assert()): ++** SQLITE_FullFSync == PAGER_FULLFSYNC ++** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC ++** SQLITE_CacheSpill == PAGER_CACHE_SPILL ++*/ ++#define SQLITE_WriteSchema 0x00000001 /* OK to update SQLITE_MASTER */ ++#define SQLITE_LegacyFileFmt 0x00000002 /* Create new databases in format 1 */ ++#define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */ ++#define SQLITE_FullFSync 0x00000008 /* Use full fsync on the backend */ ++#define SQLITE_CkptFullFSync 0x00000010 /* Use full fsync for checkpoint */ ++#define SQLITE_CacheSpill 0x00000020 /* OK to spill pager cache */ + #define SQLITE_ShortColNames 0x00000040 /* Show short columns names */ + #define SQLITE_CountRows 0x00000080 /* Count rows changed by INSERT, */ + /* DELETE, or UPDATE and return */ + /* the count using a callback. */ + #define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */ + /* result set is empty */ +-#define SQLITE_SqlTrace 0x00000200 /* Debug print SQL as it executes */ +-#define SQLITE_VdbeListing 0x00000400 /* Debug listings of VDBE programs */ +-#define SQLITE_WriteSchema 0x00000800 /* OK to update SQLITE_MASTER */ +-#define SQLITE_VdbeAddopTrace 0x00001000 /* Trace sqlite3VdbeAddOp() calls */ +-#define SQLITE_IgnoreChecks 0x00002000 /* Do not enforce check constraints */ +-#define SQLITE_ReadUncommitted 0x0004000 /* For shared-cache mode */ +-#define SQLITE_LegacyFileFmt 0x00008000 /* Create new databases in format 1 */ +-#define SQLITE_RecoveryMode 0x00010000 /* Ignore schema errors */ +-#define SQLITE_ReverseOrder 0x00020000 /* Reverse unordered SELECTs */ +-#define SQLITE_RecTriggers 0x00040000 /* Enable recursive triggers */ +-#define SQLITE_ForeignKeys 0x00080000 /* Enforce foreign key constraints */ +-#define SQLITE_AutoIndex 0x00100000 /* Enable automatic indexes */ +-#define SQLITE_PreferBuiltin 0x00200000 /* Preference to built-in funcs */ +-#define SQLITE_LoadExtension 0x00400000 /* Enable load_extension */ +-#define SQLITE_EnableTrigger 0x00800000 /* True to enable triggers */ +-#define SQLITE_DeferFKs 0x01000000 /* Defer all FK constraints */ +-#define SQLITE_QueryOnly 0x02000000 /* Disable database changes */ +-#define SQLITE_VdbeEQP 0x04000000 /* Debug EXPLAIN QUERY PLAN */ +-#define SQLITE_Vacuum 0x08000000 /* Currently in a VACUUM */ ++#define SQLITE_IgnoreChecks 0x00000200 /* Do not enforce check constraints */ ++#define SQLITE_ReadUncommit 0x00000400 /* READ UNCOMMITTED in shared-cache */ ++#define SQLITE_NoCkptOnClose 0x00000800 /* No checkpoint on close()/DETACH */ ++#define SQLITE_ReverseOrder 0x00001000 /* Reverse unordered SELECTs */ ++#define SQLITE_RecTriggers 0x00002000 /* Enable recursive triggers */ ++#define SQLITE_ForeignKeys 0x00004000 /* Enforce foreign key constraints */ ++#define SQLITE_AutoIndex 0x00008000 /* Enable automatic indexes */ ++#define SQLITE_LoadExtension 0x00010000 /* Enable load_extension */ ++#define SQLITE_LoadExtFunc 0x00020000 /* Enable load_extension() SQL func */ ++#define SQLITE_EnableTrigger 0x00040000 /* True to enable triggers */ ++#define SQLITE_DeferFKs 0x00080000 /* Defer all FK constraints */ ++#define SQLITE_QueryOnly 0x00100000 /* Disable database changes */ ++#define SQLITE_CellSizeCk 0x00200000 /* Check btree cell sizes on load */ ++#define SQLITE_Fts3Tokenizer 0x00400000 /* Enable fts3_tokenizer(2) */ ++#define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/ ++#define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */ ++#define SQLITE_ResetDatabase 0x02000000 /* Reset the database */ ++#define SQLITE_LegacyAlter 0x04000000 /* Legacy ALTER TABLE behaviour */ ++#define SQLITE_NoSchemaError 0x08000000 /* Do not report schema parse errors*/ ++#define SQLITE_Defensive 0x10000000 /* Input SQL is likely hostile */ ++ ++/* Flags used only if debugging */ ++#define HI(X) ((u64)(X)<<32) ++#ifdef SQLITE_DEBUG ++#define SQLITE_SqlTrace HI(0x0001) /* Debug print SQL as it executes */ ++#define SQLITE_VdbeListing HI(0x0002) /* Debug listings of VDBE progs */ ++#define SQLITE_VdbeTrace HI(0x0004) /* True to trace VDBE execution */ ++#define SQLITE_VdbeAddopTrace HI(0x0008) /* Trace sqlite3VdbeAddOp() calls */ ++#define SQLITE_VdbeEQP HI(0x0010) /* Debug EXPLAIN QUERY PLAN */ ++#define SQLITE_ParserTrace HI(0x0020) /* PRAGMA parser_trace=ON */ ++#endif + ++/* ++** Allowed values for sqlite3.mDbFlags ++*/ ++#define DBFLAG_SchemaChange 0x0001 /* Uncommitted Hash table changes */ ++#define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */ ++#define DBFLAG_Vacuum 0x0004 /* Currently in a VACUUM */ ++#define DBFLAG_VacuumInto 0x0008 /* Currently running VACUUM INTO */ ++#define DBFLAG_SchemaKnownOk 0x0010 /* Schema is known to be valid */ + + /* + ** Bits of the sqlite3.dbOptFlags field that are used by the +@@ -11177,29 +16497,29 @@ struct sqlite3 { + ** selectively disable various optimizations. + */ + #define SQLITE_QueryFlattener 0x0001 /* Query flattening */ +-#define SQLITE_ColumnCache 0x0002 /* Column cache */ ++#define SQLITE_WindowFunc 0x0002 /* Use xInverse for window functions */ + #define SQLITE_GroupByOrder 0x0004 /* GROUPBY cover of ORDERBY */ + #define SQLITE_FactorOutConst 0x0008 /* Constant factoring */ +-/* not used 0x0010 // Was: SQLITE_IdxRealAsInt */ +-#define SQLITE_DistinctOpt 0x0020 /* DISTINCT using indexes */ +-#define SQLITE_CoverIdxScan 0x0040 /* Covering index scans */ +-#define SQLITE_OrderByIdxJoin 0x0080 /* ORDER BY of joins via index */ +-#define SQLITE_SubqCoroutine 0x0100 /* Evaluate subqueries as coroutines */ +-#define SQLITE_Transitive 0x0200 /* Transitive constraints */ +-#define SQLITE_OmitNoopJoin 0x0400 /* Omit unused tables in joins */ ++#define SQLITE_DistinctOpt 0x0010 /* DISTINCT using indexes */ ++#define SQLITE_CoverIdxScan 0x0020 /* Covering index scans */ ++#define SQLITE_OrderByIdxJoin 0x0040 /* ORDER BY of joins via index */ ++#define SQLITE_Transitive 0x0080 /* Transitive constraints */ ++#define SQLITE_OmitNoopJoin 0x0100 /* Omit unused tables in joins */ ++#define SQLITE_CountOfView 0x0200 /* The count-of-view optimization */ ++#define SQLITE_CursorHints 0x0400 /* Add OP_CursorHint opcodes */ + #define SQLITE_Stat34 0x0800 /* Use STAT3 or STAT4 data */ ++ /* TH3 expects the Stat34 ^^^^^^ value to be 0x0800. Don't change it */ ++#define SQLITE_PushDown 0x1000 /* The push-down optimization */ ++#define SQLITE_SimplifyJoin 0x2000 /* Convert LEFT JOIN to JOIN */ ++#define SQLITE_SkipScan 0x4000 /* Skip-scans */ ++#define SQLITE_PropagateConst 0x8000 /* The constant propagation opt */ + #define SQLITE_AllOpts 0xffff /* All optimizations */ + + /* + ** Macros for testing whether or not optimizations are enabled or disabled. + */ +-#ifndef SQLITE_OMIT_BUILTIN_TEST + #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0) + #define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0) +-#else +-#define OptimizationDisabled(db, mask) 0 +-#define OptimizationEnabled(db, mask) 1 +-#endif + + /* + ** Return true if it OK to factor constant expressions into the initialization +@@ -11221,28 +16541,35 @@ struct sqlite3 { + + /* + ** Each SQL function is defined by an instance of the following +-** structure. A pointer to this structure is stored in the sqlite.aFunc +-** hash table. When multiple functions have the same name, the hash table +-** points to a linked list of these structures. ++** structure. For global built-in functions (ex: substr(), max(), count()) ++** a pointer to this structure is held in the sqlite3BuiltinFunctions object. ++** For per-connection application-defined functions, a pointer to this ++** structure is held in the db->aHash hash table. ++** ++** The u.pHash field is used by the global built-ins. The u.pDestructor ++** field is used by per-connection app-def functions. + */ + struct FuncDef { +- i16 nArg; /* Number of arguments. -1 means unlimited */ +- u16 funcFlags; /* Some combination of SQLITE_FUNC_* */ ++ i8 nArg; /* Number of arguments. -1 means unlimited */ ++ u32 funcFlags; /* Some combination of SQLITE_FUNC_* */ + void *pUserData; /* User data parameter */ + FuncDef *pNext; /* Next function with same name */ +- void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */ +- void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */ +- void (*xFinalize)(sqlite3_context*); /* Aggregate finalizer */ +- char *zName; /* SQL name of the function. */ +- FuncDef *pHash; /* Next with a different name but the same hash */ +- FuncDestructor *pDestructor; /* Reference counted destructor function */ ++ void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */ ++ void (*xFinalize)(sqlite3_context*); /* Agg finalizer */ ++ void (*xValue)(sqlite3_context*); /* Current agg value */ ++ void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */ ++ const char *zName; /* SQL name of the function. */ ++ union { ++ FuncDef *pHash; /* Next with a different name but the same hash */ ++ FuncDestructor *pDestructor; /* Reference counted destructor function */ ++ } u; + }; + + /* + ** This structure encapsulates a user-function destructor callback (as + ** configured using create_function_v2()) and a reference counter. When + ** create_function_v2() is called to create a function with a destructor, +-** a single object of this type is allocated. FuncDestructor.nRef is set to ++** a single object of this type is allocated. FuncDestructor.nRef is set to + ** the number of FuncDef objects created (either 1 or 3, depending on whether + ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor + ** member of each of the new FuncDef objects is set to point to the allocated +@@ -11260,45 +16587,78 @@ struct FuncDestructor { + + /* + ** Possible values for FuncDef.flags. Note that the _LENGTH and _TYPEOF +-** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. There ++** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And ++** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There + ** are assert() statements in the code to verify this. +-*/ +-#define SQLITE_FUNC_ENCMASK 0x003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */ +-#define SQLITE_FUNC_LIKE 0x004 /* Candidate for the LIKE optimization */ +-#define SQLITE_FUNC_CASE 0x008 /* Case-sensitive LIKE-type function */ +-#define SQLITE_FUNC_EPHEM 0x010 /* Ephemeral. Delete with VDBE */ +-#define SQLITE_FUNC_NEEDCOLL 0x020 /* sqlite3GetFuncCollSeq() might be called */ +-#define SQLITE_FUNC_LENGTH 0x040 /* Built-in length() function */ +-#define SQLITE_FUNC_TYPEOF 0x080 /* Built-in typeof() function */ +-#define SQLITE_FUNC_COUNT 0x100 /* Built-in count(*) aggregate */ +-#define SQLITE_FUNC_COALESCE 0x200 /* Built-in coalesce() or ifnull() */ +-#define SQLITE_FUNC_UNLIKELY 0x400 /* Built-in unlikely() function */ +-#define SQLITE_FUNC_CONSTANT 0x800 /* Constant inputs give a constant output */ +-#define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */ ++** ++** Value constraints (enforced via assert()): ++** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg ++** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG ++** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG ++** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API ++** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API ++*/ ++#define SQLITE_FUNC_ENCMASK 0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */ ++#define SQLITE_FUNC_LIKE 0x0004 /* Candidate for the LIKE optimization */ ++#define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */ ++#define SQLITE_FUNC_EPHEM 0x0010 /* Ephemeral. Delete with VDBE */ ++#define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/ ++#define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */ ++#define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */ ++#define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */ ++#define SQLITE_FUNC_COALESCE 0x0200 /* Built-in coalesce() or ifnull() */ ++#define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */ ++#define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */ ++#define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */ ++#define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a ++ ** single query - might change over time */ ++#define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */ ++#define SQLITE_FUNC_OFFSET 0x8000 /* Built-in sqlite_offset() function */ ++#define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */ ++#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */ + + /* + ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are + ** used to create the initializers for the FuncDef structures. + ** + ** FUNCTION(zName, nArg, iArg, bNC, xFunc) +-** Used to create a scalar function definition of a function zName ++** Used to create a scalar function definition of a function zName + ** implemented by C function xFunc that accepts nArg arguments. The + ** value passed as iArg is cast to a (void*) and made available +-** as the user-data (sqlite3_user_data()) for the function. If ++** as the user-data (sqlite3_user_data()) for the function. If + ** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set. + ** + ** VFUNCTION(zName, nArg, iArg, bNC, xFunc) + ** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag. + ** ++** DFUNCTION(zName, nArg, iArg, bNC, xFunc) ++** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and ++** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions ++** and functions like sqlite_version() that can change, but not during ++** a single query. The iArg is ignored. The user-data is always set ++** to a NULL pointer. The bNC parameter is not used. ++** ++** PURE_DATE(zName, nArg, iArg, bNC, xFunc) ++** Used for "pure" date/time functions, this macro is like DFUNCTION ++** except that it does set the SQLITE_FUNC_CONSTANT flags. iArg is ++** ignored and the user-data for these functions is set to an ++** arbitrary non-NULL pointer. The bNC parameter is not used. ++** + ** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal) + ** Used to create an aggregate function definition implemented by + ** the C functions xStep and xFinal. The first four parameters + ** are interpreted in the same way as the first 4 parameters to + ** FUNCTION(). + ** ++** WFUNCTION(zName, nArg, iArg, xStep, xFinal, xValue, xInverse) ++** Used to create an aggregate function definition implemented by ++** the C functions xStep and xFinal. The first four parameters ++** are interpreted in the same way as the first 4 parameters to ++** FUNCTION(). ++** + ** LIKEFUNC(zName, nArg, pArg, flags) +-** Used to create a scalar function definition of a function zName +-** that accepts nArg arguments and is implemented by a call to C ++** Used to create a scalar function definition of a function zName ++** that accepts nArg arguments and is implemented by a call to C + ** function likeFunc. Argument pArg is cast to a (void *) and made + ** available as the function user-data (sqlite3_user_data()). The + ** FuncDef.flags variable is set to the value passed as the flags +@@ -11306,25 +16666,38 @@ struct FuncDestructor { + */ + #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \ + {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \ +- SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0} ++ SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} } + #define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \ + {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \ +- SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0} ++ SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} } ++#define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \ ++ {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8, \ ++ 0, 0, xFunc, 0, 0, 0, #zName, {0} } ++#define PURE_DATE(zName, nArg, iArg, bNC, xFunc) \ ++ {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \ ++ (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} } + #define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \ + {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\ +- SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0} ++ SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} } + #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \ +- {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \ +- pArg, 0, xFunc, 0, 0, #zName, 0, 0} ++ {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \ ++ pArg, 0, xFunc, 0, 0, 0, #zName, } + #define LIKEFUNC(zName, nArg, arg, flags) \ + {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \ +- (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0} +-#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \ ++ (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} } ++#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue) \ + {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \ +- SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0} ++ SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,0,#zName, {0}} + #define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags) \ + {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|extraFlags, \ +- SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0} ++ SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xFinal,0,#zName, {0}} ++#define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \ ++ {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \ ++ SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,xInverse,#zName, {0}} ++#define INTERNAL_FUNCTION(zName, nArg, xFunc) \ ++ {nArg, SQLITE_FUNC_INTERNAL|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \ ++ 0, 0, xFunc, 0, 0, 0, #zName, {0} } ++ + + /* + ** All current savepoints are stored in a linked list starting at +@@ -11358,6 +16731,7 @@ struct Module { + const char *zName; /* Name passed to create_module() */ + void *pAux; /* pAux passed to create_module() */ + void (*xDestroy)(void *); /* Module destructor function */ ++ Table *pEpoTab; /* Eponymous table for this module */ + }; + + /* +@@ -11365,14 +16739,12 @@ struct Module { + ** of this structure. + */ + struct Column { +- char *zName; /* Name of this column */ ++ char *zName; /* Name of this column, \000, then the type */ + Expr *pDflt; /* Default value of this column */ +- char *zDflt; /* Original text of the default value */ +- char *zType; /* Data type for this column */ + char *zColl; /* Collating sequence. If NULL, use the default */ + u8 notNull; /* An OE_ code for handling a NOT NULL constraint */ + char affinity; /* One of the SQLITE_AFF_... values */ +- u8 szEst; /* Estimated size of this column. INT==1 */ ++ u8 szEst; /* Estimated size of value in this column. sizeof(INT)==1 */ + u8 colFlags; /* Boolean properties. See COLFLAG_ defines below */ + }; + +@@ -11380,6 +16752,9 @@ struct Column { + */ + #define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */ + #define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */ ++#define COLFLAG_HASTYPE 0x0004 /* Type name follows column name */ ++#define COLFLAG_UNIQUE 0x0008 /* Column def contains "UNIQUE" or "PK" */ ++#define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */ + + /* + ** A "Collating Sequence" is defined by an instance of the following +@@ -11403,22 +16778,23 @@ struct CollSeq { + */ + #define SQLITE_SO_ASC 0 /* Sort in ascending order */ + #define SQLITE_SO_DESC 1 /* Sort in ascending order */ ++#define SQLITE_SO_UNDEFINED -1 /* No sort order specified */ + + /* + ** Column affinity types. + ** + ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and + ** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve +-** the speed a little by numbering the values consecutively. ++** the speed a little by numbering the values consecutively. + ** + ** But rather than start with 0 or 1, we begin with 'A'. That way, + ** when multiple affinity types are concatenated into a string and + ** used as the P4 operand, they will be more readable. + ** + ** Note also that the numeric types are grouped together so that testing +-** for a numeric type is a single comparison. And the NONE type is first. ++** for a numeric type is a single comparison. And the BLOB type is first. + */ +-#define SQLITE_AFF_NONE 'A' ++#define SQLITE_AFF_BLOB 'A' + #define SQLITE_AFF_TEXT 'B' + #define SQLITE_AFF_NUMERIC 'C' + #define SQLITE_AFF_INTEGER 'D' +@@ -11428,7 +16804,7 @@ struct CollSeq { + + /* + ** The SQLITE_AFF_MASK values masks off the significant bits of an +-** affinity value. ++** affinity value. + */ + #define SQLITE_AFF_MASK 0x47 + +@@ -11441,6 +16817,7 @@ struct CollSeq { + ** operator is NULL. It is added to certain comparison operators to + ** prove that the operands are always NOT NULL. + */ ++#define SQLITE_KEEPNULL 0x08 /* Used by vector == or <> */ + #define SQLITE_JUMPIFNULL 0x10 /* jumps if either operand is NULL */ + #define SQLITE_STOREP2 0x20 /* Store result in reg[P2] rather than jump */ + #define SQLITE_NULLEQ 0x80 /* NULL=NULL */ +@@ -11448,20 +16825,20 @@ struct CollSeq { + + /* + ** An object of this type is created for each virtual table present in +-** the database schema. ++** the database schema. + ** + ** If the database schema is shared, then there is one instance of this + ** structure for each database connection (sqlite3*) that uses the shared + ** schema. This is because each database connection requires its own unique +-** instance of the sqlite3_vtab* handle used to access the virtual table +-** implementation. sqlite3_vtab* handles can not be shared between +-** database connections, even when the rest of the in-memory database ++** instance of the sqlite3_vtab* handle used to access the virtual table ++** implementation. sqlite3_vtab* handles can not be shared between ++** database connections, even when the rest of the in-memory database + ** schema is shared, as the implementation often stores the database + ** connection handle passed to it via the xConnect() or xCreate() method + ** during initialization internally. This database connection handle may +-** then be used by the virtual table implementation to access real tables +-** within the database. So that they appear as part of the callers +-** transaction, these accesses need to be made via the same database ++** then be used by the virtual table implementation to access real tables ++** within the database. So that they appear as part of the callers ++** transaction, these accesses need to be made via the same database + ** connection as that used to execute SQL operations on the virtual table. + ** + ** All VTable objects that correspond to a single table in a shared +@@ -11473,19 +16850,19 @@ struct CollSeq { + ** sqlite3_vtab* handle in the compiled query. + ** + ** When an in-memory Table object is deleted (for example when the +-** schema is being reloaded for some reason), the VTable objects are not +-** deleted and the sqlite3_vtab* handles are not xDisconnect()ed ++** schema is being reloaded for some reason), the VTable objects are not ++** deleted and the sqlite3_vtab* handles are not xDisconnect()ed + ** immediately. Instead, they are moved from the Table.pVTable list to + ** another linked list headed by the sqlite3.pDisconnect member of the +-** corresponding sqlite3 structure. They are then deleted/xDisconnected ++** corresponding sqlite3 structure. They are then deleted/xDisconnected + ** next time a statement is prepared using said sqlite3*. This is done + ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes. + ** Refer to comments above function sqlite3VtabUnlockList() for an + ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect + ** list without holding the corresponding sqlite3.mutex mutex. + ** +-** The memory for objects of this type is always allocated by +-** sqlite3DbMalloc(), using the connection handle stored in VTable.db as ++** The memory for objects of this type is always allocated by ++** sqlite3DbMalloc(), using the connection handle stored in VTable.db as + ** the first argument. + */ + struct VTable { +@@ -11509,26 +16886,25 @@ struct Table { + Select *pSelect; /* NULL for tables. Points to definition if a view. */ + FKey *pFKey; /* Linked list of all foreign keys in this table */ + char *zColAff; /* String defining the affinity of each column */ +-#ifndef SQLITE_OMIT_CHECK + ExprList *pCheck; /* All CHECK constraints */ +-#endif ++ /* ... also used as column name list in a VIEW */ + int tnum; /* Root BTree page for this table */ ++ u32 nTabRef; /* Number of pointers to this Table */ ++ u32 tabFlags; /* Mask of TF_* values */ + i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */ + i16 nCol; /* Number of columns in this table */ +- u16 nRef; /* Number of pointers to this Table */ + LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */ + LogEst szTabRow; /* Estimated size of each table row in bytes */ + #ifdef SQLITE_ENABLE_COSTMULT + LogEst costMult; /* Cost multiplier for using this table */ + #endif +- u8 tabFlags; /* Mask of TF_* values */ + u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */ + #ifndef SQLITE_OMIT_ALTERTABLE + int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */ + #endif + #ifndef SQLITE_OMIT_VIRTUALTABLE + int nModuleArg; /* Number of arguments to the module */ +- char **azModuleArg; /* Text of all module args. [0] is module name */ ++ char **azModuleArg; /* 0: module 1: schema 2: vtab name 3...: args */ + VTable *pVTable; /* List of VTable objects. */ + #endif + Trigger *pTrigger; /* List of triggers stored in pSchema */ +@@ -11539,20 +16915,24 @@ struct Table { + /* + ** Allowed values for Table.tabFlags. + ** +-** TF_OOOHidden applies to virtual tables that have hidden columns that are ++** TF_OOOHidden applies to tables or view that have hidden columns that are + ** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING + ** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden, + ** the TF_OOOHidden attribute would apply in this case. Such tables require + ** special handling during INSERT processing. + */ +-#define TF_Readonly 0x01 /* Read-only system table */ +-#define TF_Ephemeral 0x02 /* An ephemeral table */ +-#define TF_HasPrimaryKey 0x04 /* Table has a primary key */ +-#define TF_Autoincrement 0x08 /* Integer primary key is autoincrement */ +-#define TF_Virtual 0x10 /* Is a virtual table */ +-#define TF_WithoutRowid 0x20 /* No rowid used. PRIMARY KEY is the key */ +-#define TF_OOOHidden 0x40 /* Out-of-Order hidden columns */ +- ++#define TF_Readonly 0x0001 /* Read-only system table */ ++#define TF_Ephemeral 0x0002 /* An ephemeral table */ ++#define TF_HasPrimaryKey 0x0004 /* Table has a primary key */ ++#define TF_Autoincrement 0x0008 /* Integer primary key is autoincrement */ ++#define TF_HasStat1 0x0010 /* nRowLogEst set from sqlite_stat1 */ ++#define TF_WithoutRowid 0x0020 /* No rowid. PRIMARY KEY is the key */ ++#define TF_NoVisibleRowid 0x0040 /* No user-visible "rowid" column */ ++#define TF_OOOHidden 0x0080 /* Out-of-Order hidden columns */ ++#define TF_StatsUsed 0x0100 /* Query planner decisions affected by ++ ** Index.aiRowLogEst[] values */ ++#define TF_HasNotNull 0x0200 /* Contains NOT NULL constraints */ ++#define TF_Shadow 0x0400 /* True for a shadow table */ + + /* + ** Test to see whether or not a table is a virtual table. This is +@@ -11560,15 +16940,32 @@ struct Table { + ** table support is omitted from the build. + */ + #ifndef SQLITE_OMIT_VIRTUALTABLE +-# define IsVirtual(X) (((X)->tabFlags & TF_Virtual)!=0) +-# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0) ++# define IsVirtual(X) ((X)->nModuleArg) + #else + # define IsVirtual(X) 0 +-# define IsHiddenColumn(X) 0 + #endif + ++/* ++** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn() ++** only works for non-virtual tables (ordinary tables and views) and is ++** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The ++** IsHiddenColumn() macro is general purpose. ++*/ ++#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS) ++# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0) ++# define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0) ++#elif !defined(SQLITE_OMIT_VIRTUALTABLE) ++# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0) ++# define IsOrdinaryHiddenColumn(X) 0 ++#else ++# define IsHiddenColumn(X) 0 ++# define IsOrdinaryHiddenColumn(X) 0 ++#endif ++ ++ + /* Does the table have a rowid */ + #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0) ++#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0) + + /* + ** Each foreign key constraint is an instance of the following structure. +@@ -11636,7 +17033,7 @@ struct FKey { + ** key is set to NULL. CASCADE means that a DELETE or UPDATE of the + ** referenced table row is propagated into the row that holds the + ** foreign key. +-** ++** + ** The following symbolic values are used to record which type + ** of action to take. + */ +@@ -11646,18 +17043,17 @@ struct FKey { + #define OE_Fail 3 /* Stop the operation but leave all prior changes */ + #define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */ + #define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */ +- +-#define OE_Restrict 6 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */ +-#define OE_SetNull 7 /* Set the foreign key value to NULL */ +-#define OE_SetDflt 8 /* Set the foreign key value to its default */ +-#define OE_Cascade 9 /* Cascade the changes */ +- +-#define OE_Default 10 /* Do whatever the default action is */ ++#define OE_Update 6 /* Process as a DO UPDATE in an upsert */ ++#define OE_Restrict 7 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */ ++#define OE_SetNull 8 /* Set the foreign key value to NULL */ ++#define OE_SetDflt 9 /* Set the foreign key value to its default */ ++#define OE_Cascade 10 /* Cascade the changes */ ++#define OE_Default 11 /* Do whatever the default action is */ + + + /* + ** An instance of the following structure is passed as the first +-** argument to sqlite3VdbeKeyCompare and is used to control the ++** argument to sqlite3VdbeKeyCompare and is used to control the + ** comparison of the two index keys. + ** + ** Note that aSortOrder[] and aColl[] have nField+1 slots. There +@@ -11667,17 +17063,16 @@ struct FKey { + struct KeyInfo { + u32 nRef; /* Number of references to this KeyInfo object */ + u8 enc; /* Text encoding - one of the SQLITE_UTF* values */ +- u16 nField; /* Number of key columns in the index */ +- u16 nXField; /* Number of columns beyond the key columns */ ++ u16 nKeyField; /* Number of key columns in the index */ ++ u16 nAllField; /* Total columns, including key plus others */ + sqlite3 *db; /* The database connection */ + u8 *aSortOrder; /* Sort order for each column. */ + CollSeq *aColl[1]; /* Collating sequence for each term of the key */ + }; + + /* +-** An instance of the following structure holds information about a +-** single index record that has already been parsed out into individual +-** values. ++** This object holds a record which has been parsed out into individual ++** fields, for the purposes of doing a comparison. + ** + ** A record is an object that contains one or more fields of data. + ** Records are used to store the content of a table row and to store +@@ -11685,20 +17080,40 @@ struct KeyInfo { + ** the OP_MakeRecord opcode of the VDBE and is disassembled by the + ** OP_Column opcode. + ** +-** This structure holds a record that has already been disassembled +-** into its constituent fields. +-** +-** The r1 and r2 member variables are only used by the optimized comparison +-** functions vdbeRecordCompareInt() and vdbeRecordCompareString(). ++** An instance of this object serves as a "key" for doing a search on ++** an index b+tree. The goal of the search is to find the entry that ++** is closed to the key described by this object. This object might hold ++** just a prefix of the key. The number of fields is given by ++** pKeyInfo->nField. ++** ++** The r1 and r2 fields are the values to return if this key is less than ++** or greater than a key in the btree, respectively. These are normally ++** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree ++** is in DESC order. ++** ++** The key comparison functions actually return default_rc when they find ++** an equals comparison. default_rc can be -1, 0, or +1. If there are ++** multiple entries in the b-tree with the same key (when only looking ++** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to ++** cause the search to find the last match, or +1 to cause the search to ++** find the first match. ++** ++** The key comparison functions will set eqSeen to true if they ever ++** get and equal results when comparing this structure to a b-tree record. ++** When default_rc!=0, the search might end up on the record immediately ++** before the first match or immediately after the last match. The ++** eqSeen field will indicate whether or not an exact match exists in the ++** b-tree. + */ + struct UnpackedRecord { + KeyInfo *pKeyInfo; /* Collation and sort-order information */ ++ Mem *aMem; /* Values */ + u16 nField; /* Number of entries in apMem[] */ + i8 default_rc; /* Comparison result if keys are equal */ + u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */ +- Mem *aMem; /* Values */ +- int r1; /* Value to return if (lhs > rhs) */ +- int r2; /* Value to return if (rhs < lhs) */ ++ i8 r1; /* Value to return if (lhs < rhs) */ ++ i8 r2; /* Value to return if (lhs > rhs) */ ++ u8 eqSeen; /* True if an equality comparison has been seen */ + }; + + +@@ -11716,7 +17131,7 @@ struct UnpackedRecord { + ** In the Table structure describing Ex1, nCol==3 because there are + ** three columns in the table. In the Index structure describing + ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed. +-** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the ++** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the + ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[]. + ** The second column to be indexed (c1) has an index of 0 in + ** Ex1.aCol[], hence Ex2.aiColumn[1]==0. +@@ -11724,9 +17139,17 @@ struct UnpackedRecord { + ** The Index.onError field determines whether or not the indexed columns + ** must be unique and what to do if they are not. When Index.onError=OE_None, + ** it means this is not a unique index. Otherwise it is a unique index +-** and the value of Index.onError indicate the which conflict resolution ++** and the value of Index.onError indicate the which conflict resolution + ** algorithm to employ whenever an attempt is made to insert a non-unique + ** element. ++** ++** While parsing a CREATE TABLE or CREATE INDEX statement in order to ++** generate VDBE code (as opposed to parsing one read from an sqlite_master ++** table as part of parsing an existing database schema), transient instances ++** of this structure may be created. In this case the Index.tnum variable is ++** used to store the address of a VDBE instruction, not a database page ++** number (it cannot - the database page is not allocated until the VDBE ++** program is executed). See convertToWithoutRowidTable() for details. + */ + struct Index { + char *zName; /* Name of this index */ +@@ -11737,19 +17160,22 @@ struct Index { + Index *pNext; /* The next index associated with the same table */ + Schema *pSchema; /* Schema containing this index */ + u8 *aSortOrder; /* for each column: True==DESC, False==ASC */ +- char **azColl; /* Array of collation sequence names for index */ ++ const char **azColl; /* Array of collation sequence names for index */ + Expr *pPartIdxWhere; /* WHERE clause for partial indices */ ++ ExprList *aColExpr; /* Column expressions */ + int tnum; /* DB Page containing root of this index */ + LogEst szIdxRow; /* Estimated average row size in bytes */ + u16 nKeyCol; /* Number of columns forming the key */ + u16 nColumn; /* Number of columns stored in the index */ + u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ +- unsigned idxType:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */ ++ unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */ + unsigned bUnordered:1; /* Use this index for == or IN queries only */ + unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */ + unsigned isResized:1; /* True if resizeIndexObject() has been called */ + unsigned isCovering:1; /* True if this is a covering index */ + unsigned noSkipScan:1; /* Do not try to use skip-scan if true */ ++ unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */ ++ unsigned bNoQuery:1; /* Do not use this index to optimize queries */ + #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + int nSample; /* Number of elements in aSample[] */ + int nSampleCol; /* Size of IndexSample.anEq[] and so on */ +@@ -11758,6 +17184,7 @@ struct Index { + tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */ + tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */ + #endif ++ Bitmask colNotIdxed; /* 0 for unindexed columns in pTab */ + }; + + /* +@@ -11766,6 +17193,7 @@ struct Index { + #define SQLITE_IDXTYPE_APPDEF 0 /* Created using CREATE INDEX */ + #define SQLITE_IDXTYPE_UNIQUE 1 /* Implements a UNIQUE constraint */ + #define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */ ++#define SQLITE_IDXTYPE_IPK 3 /* INTEGER PRIMARY KEY index */ + + /* Return true if index X is a PRIMARY KEY index */ + #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY) +@@ -11773,8 +17201,14 @@ struct Index { + /* Return true if index X is a UNIQUE index */ + #define IsUniqueIndex(X) ((X)->onError!=OE_None) + ++/* The Index.aiColumn[] values are normally positive integer. But ++** there are some negative values that have special meaning: ++*/ ++#define XN_ROWID (-1) /* Indexed column is the rowid */ ++#define XN_EXPR (-2) /* Indexed column is an expression */ ++ + /* +-** Each sample stored in the sqlite_stat3 table is represented in memory ++** Each sample stored in the sqlite_stat3 table is represented in memory + ** using a structure of this type. See documentation at the top of the + ** analyze.c source file for additional information. + */ +@@ -11786,13 +17220,21 @@ struct IndexSample { + tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */ + }; + ++/* ++** Possible values to use within the flags argument to sqlite3GetToken(). ++*/ ++#define SQLITE_TOKEN_QUOTED 0x1 /* Token is a quoted identifier. */ ++#define SQLITE_TOKEN_KEYWORD 0x2 /* Token is a keyword. */ ++ + /* + ** Each token coming out of the lexer is an instance of + ** this structure. Tokens are also used as part of an expression. + ** +-** Note if Token.z==0 then Token.dyn and Token.n are undefined and +-** may contain random values. Do not make any assumptions about Token.dyn +-** and Token.n when Token.z==0. ++** The memory that "z" points to is owned by other objects. Take care ++** that the owner of the "z" string does not deallocate the string before ++** the Token goes out of scope! Very often, the "z" points to some place ++** in the middle of the Parse.zSql text. But it might also point to a ++** static string. + */ + struct Token { + const char *z; /* Text of the token. Not NULL-terminated! */ +@@ -11869,9 +17311,9 @@ typedef int ynVar; + ** to represent the greater-than-or-equal-to operator in the expression + ** tree. + ** +-** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, ++** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, + ** or TK_STRING), then Expr.token contains the text of the SQL literal. If +-** the expression is a variable (TK_VARIABLE), then Expr.token contains the ++** the expression is a variable (TK_VARIABLE), then Expr.token contains the + ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION), + ** then Expr.token contains the name of the function. + ** +@@ -11882,7 +17324,7 @@ typedef int ynVar; + ** a CASE expression or an IN expression of the form " IN ( , ...)". + ** Expr.x.pSelect is used if the expression is a sub-select or an expression of + ** the form " IN (SELECT ...)". If the EP_xIsSelect bit is set in the +-** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is ++** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is + ** valid. + ** + ** An expression of the form ID or ID.ID refers to a column in a table. +@@ -11893,8 +17335,8 @@ typedef int ynVar; + ** value is also stored in the Expr.iAgg column in the aggregate so that + ** it can be accessed after all aggregates are computed. + ** +-** If the expression is an unbound variable marker (a question mark +-** character '?' in the original SQL) then the Expr.iTable holds the index ++** If the expression is an unbound variable marker (a question mark ++** character '?' in the original SQL) then the Expr.iTable holds the index + ** number for that variable. + ** + ** If the expression is a subquery then Expr.iColumn holds an integer +@@ -11933,7 +17375,7 @@ struct Expr { + + /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no + ** space is allocated for the fields below this point. An attempt to +- ** access them will result in a segfault or malfunction. ++ ** access them will result in a segfault or malfunction. + *********************************************************************/ + + Expr *pLeft; /* Left subnode */ +@@ -11954,26 +17396,40 @@ struct Expr { + int iTable; /* TK_COLUMN: cursor number of table holding column + ** TK_REGISTER: register number + ** TK_TRIGGER: 1 -> new, 0 -> old +- ** EP_Unlikely: 134217728 times likelihood */ ++ ** EP_Unlikely: 134217728 times likelihood ++ ** TK_SELECT: 1st register of result vector */ + ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid. +- ** TK_VARIABLE: variable number (always >= 1). */ ++ ** TK_VARIABLE: variable number (always >= 1). ++ ** TK_SELECT_COLUMN: column of the result vector */ + i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */ + i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */ + u8 op2; /* TK_REGISTER: original value of Expr.op + ** TK_COLUMN: the value of p5 for OP_Column + ** TK_AGG_FUNCTION: nesting depth */ + AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */ +- Table *pTab; /* Table for TK_COLUMN expressions. */ ++ union { ++ Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL ++ ** for a column of an index on an expression */ ++ Window *pWin; /* TK_FUNCTION: Window definition for the func */ ++ struct { /* TK_IN, TK_SELECT, and TK_EXISTS */ ++ int iAddr; /* Subroutine entry address */ ++ int regReturn; /* Register used to hold return address */ ++ } sub; ++ } y; + }; + + /* + ** The following are the meanings of bits in the Expr.flags field. ++** Value restrictions: ++** ++** EP_Agg == NC_HasAgg == SF_HasAgg ++** EP_Win == NC_HasWin + */ + #define EP_FromJoin 0x000001 /* Originates in ON/USING clause of outer join */ +-#define EP_Agg 0x000002 /* Contains one or more aggregate functions */ +-#define EP_Resolved 0x000004 /* IDs have been resolved to COLUMNs */ +-#define EP_Error 0x000008 /* Expression contains one or more errors */ +-#define EP_Distinct 0x000010 /* Aggregate function with DISTINCT keyword */ ++#define EP_Distinct 0x000002 /* Aggregate function with DISTINCT keyword */ ++#define EP_HasFunc 0x000004 /* Contains one or more functions of any kind */ ++#define EP_FixedCol 0x000008 /* TK_Column with a known fixed value */ ++#define EP_Agg 0x000010 /* Contains one or more aggregate functions */ + #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */ + #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */ + #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */ +@@ -11984,21 +17440,28 @@ struct Expr { + #define EP_Skip 0x001000 /* COLLATE, AS, or UNLIKELY */ + #define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */ + #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */ +-#define EP_Static 0x008000 /* Held in memory not obtained from malloc() */ ++#define EP_Win 0x008000 /* Contains window functions */ + #define EP_MemToken 0x010000 /* Need to sqlite3DbFree() Expr.zToken */ + #define EP_NoReduce 0x020000 /* Cannot EXPRDUP_REDUCE this Expr */ + #define EP_Unlikely 0x040000 /* unlikely() or likelihood() function */ +-#define EP_ConstFunc 0x080000 /* Node is a SQLITE_FUNC_CONSTANT function */ ++#define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */ + #define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */ + #define EP_Subquery 0x200000 /* Tree contains a TK_SELECT operator */ ++#define EP_Alias 0x400000 /* Is an alias for a result set column */ ++#define EP_Leaf 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */ ++#define EP_WinFunc 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */ ++#define EP_Subrtn 0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */ ++#define EP_Quoted 0x4000000 /* TK_ID was originally quoted */ ++#define EP_Static 0x8000000 /* Held in memory not obtained from malloc() */ + + /* +-** Combinations of two or more EP_* flags ++** The EP_Propagate mask is a set of properties that automatically propagate ++** upwards into parent nodes. + */ +-#define EP_Propagate (EP_Collate|EP_Subquery) /* Propagate these bits up tree */ ++#define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc) + + /* +-** These macros can be used to test, set, or clear bits in the ++** These macros can be used to test, set, or clear bits in the + ** Expr.flags field. + */ + #define ExprHasProperty(E,P) (((E)->flags&(P))!=0) +@@ -12017,8 +17480,8 @@ struct Expr { + #endif + + /* +-** Macros to determine the number of bytes required by a normal Expr +-** struct, an Expr struct with the EP_Reduced flag set in Expr.flags ++** Macros to determine the number of bytes required by a normal Expr ++** struct, an Expr struct with the EP_Reduced flag set in Expr.flags + ** and an Expr struct with the EP_TokenOnly flag set. + */ + #define EXPR_FULLSIZE sizeof(Expr) /* Full size */ +@@ -12026,7 +17489,7 @@ struct Expr { + #define EXPR_TOKENONLYSIZE offsetof(Expr,pLeft) /* Fewer features */ + + /* +-** Flags passed to the sqlite3ExprDup() function. See the header comment ++** Flags passed to the sqlite3ExprDup() function. See the header comment + ** above sqlite3ExprDup() for details. + */ + #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */ +@@ -12050,13 +17513,14 @@ struct Expr { + struct ExprList { + int nExpr; /* Number of expressions on the list */ + struct ExprList_item { /* For each expression in the list */ +- Expr *pExpr; /* The list of expressions */ ++ Expr *pExpr; /* The parse tree for this expression */ + char *zName; /* Token associated with this expression */ + char *zSpan; /* Original text of the expression */ + u8 sortOrder; /* 1 for DESC or 0 for ASC */ + unsigned done :1; /* A flag to indicate when processing is finished */ + unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */ + unsigned reusable :1; /* Constant expression is reusable */ ++ unsigned bSorterRef :1; /* Defer evaluation until after sorting */ + union { + struct { + u16 iOrderByCol; /* For ORDER BY, column number in result set */ +@@ -12064,18 +17528,7 @@ struct ExprList { + } x; + int iConstExprReg; /* Register in which Expr value is cached */ + } u; +- } *a; /* Alloc a power of two greater or equal to nExpr */ +-}; +- +-/* +-** An instance of this structure is used by the parser to record both +-** the parse tree for an expression and the span of input text for an +-** expression. +-*/ +-struct ExprSpan { +- Expr *pExpr; /* The expression parse tree */ +- const char *zStart; /* First character of input text */ +- const char *zEnd; /* One character past the end of input text */ ++ } a[1]; /* One slot for each expression in the list */ + }; + + /* +@@ -12101,26 +17554,6 @@ struct IdList { + int nId; /* Number of identifiers on the list */ + }; + +-/* +-** The bitmask datatype defined below is used for various optimizations. +-** +-** Changing this from a 64-bit to a 32-bit type limits the number of +-** tables in a join to 32 instead of 64. But it also reduces the size +-** of the library by 738 bytes on ix86. +-*/ +-typedef u64 Bitmask; +- +-/* +-** The number of bits in a Bitmask. "BMS" means "BitMask Size". +-*/ +-#define BMS ((int)(sizeof(Bitmask)*8)) +- +-/* +-** A bit in a Bitmask +-*/ +-#define MASKBIT(n) (((Bitmask)1)<<(n)) +-#define MASKBIT32(n) (((unsigned int)1)<<(n)) +- + /* + ** The following structure describes the FROM clause of a SELECT statement. + ** Each table or subquery in the FROM clause is a separate element of +@@ -12153,20 +17586,24 @@ struct SrcList { + int addrFillSub; /* Address of subroutine to manifest a subquery */ + int regReturn; /* Register holding return address of addrFillSub */ + int regResult; /* Registers holding results of a co-routine */ +- u8 jointype; /* Type of join between this able and the previous */ +- unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */ +- unsigned isCorrelated :1; /* True if sub-query is correlated */ +- unsigned viaCoroutine :1; /* Implemented as a co-routine */ +- unsigned isRecursive :1; /* True for recursive reference in WITH */ +-#ifndef SQLITE_OMIT_EXPLAIN +- u8 iSelectId; /* If pSelect!=0, the id of the sub-select in EQP */ +-#endif ++ struct { ++ u8 jointype; /* Type of join between this table and the previous */ ++ unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */ ++ unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */ ++ unsigned isTabFunc :1; /* True if table-valued-function syntax */ ++ unsigned isCorrelated :1; /* True if sub-query is correlated */ ++ unsigned viaCoroutine :1; /* Implemented as a co-routine */ ++ unsigned isRecursive :1; /* True for recursive reference in WITH */ ++ } fg; + int iCursor; /* The VDBE cursor number used to access this table */ + Expr *pOn; /* The ON clause of a join */ + IdList *pUsing; /* The USING clause of a join */ + Bitmask colUsed; /* Bit N (1< " clause */ +- Index *pIndex; /* Index structure corresponding to zIndex, if any */ ++ union { ++ char *zIndexedBy; /* Identifier from "INDEXED BY " clause */ ++ ExprList *pFuncArg; /* Arguments to table-valued-function */ ++ } u1; ++ Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */ + } a[1]; /* One entry for each identifier on the list */ + }; + +@@ -12185,21 +17622,28 @@ struct SrcList { + /* + ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin() + ** and the WhereInfo.wctrlFlags member. ++** ++** Value constraints (enforced via assert()): ++** WHERE_USE_LIMIT == SF_FixedLimit + */ + #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */ + #define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */ + #define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */ + #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */ +-#define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */ +-#define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */ +-#define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */ +-#define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */ +-#define WHERE_NO_AUTOINDEX 0x0080 /* Disallow automatic indexes */ +-#define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */ +-#define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */ +-#define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */ +-#define WHERE_SORTBYGROUP 0x0800 /* Support sqlite3WhereIsSorted() */ +-#define WHERE_REOPEN_IDX 0x1000 /* Try to use OP_ReopenIdx */ ++#define WHERE_ONEPASS_MULTIROW 0x0008 /* ONEPASS is ok with multiple rows */ ++#define WHERE_DUPLICATES_OK 0x0010 /* Ok to return a row more than once */ ++#define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of ++ ** the OR optimization */ ++#define WHERE_GROUPBY 0x0040 /* pOrderBy is really a GROUP BY */ ++#define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */ ++#define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */ ++#define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */ ++#define WHERE_SEEK_TABLE 0x0400 /* Do not defer seeks on main table */ ++#define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */ ++#define WHERE_SEEK_UNIQ_TABLE 0x1000 /* Do not defer seeks if unique */ ++ /* 0x2000 not currently used */ ++#define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */ ++ /* 0x8000 not currently used */ + + /* Allowed return values from sqlite3WhereIsDistinct() + */ +@@ -12217,12 +17661,12 @@ struct SrcList { + ** pEList corresponds to the result set of a SELECT and is NULL for + ** other statements. + ** +-** NameContexts can be nested. When resolving names, the inner-most ++** NameContexts can be nested. When resolving names, the inner-most + ** context is searched first. If no match is found, the next outer + ** context is checked. If there is still no match, the next context + ** is checked. This process continues until either a match is found + ** or all contexts are check. When a match is found, the nRef member of +-** the context containing the match is incremented. ++** the context containing the match is incremented. + ** + ** Each subquery gets a new NameContext. The pNext field points to the + ** NameContext in the parent query. Thus the process of scanning the +@@ -12232,37 +17676,78 @@ struct SrcList { + struct NameContext { + Parse *pParse; /* The parser */ + SrcList *pSrcList; /* One or more tables used to resolve names */ +- ExprList *pEList; /* Optional list of result-set columns */ +- AggInfo *pAggInfo; /* Information about aggregates at this level */ ++ union { ++ ExprList *pEList; /* Optional list of result-set columns */ ++ AggInfo *pAggInfo; /* Information about aggregates at this level */ ++ Upsert *pUpsert; /* ON CONFLICT clause information from an upsert */ ++ } uNC; + NameContext *pNext; /* Next outer name context. NULL for outermost */ + int nRef; /* Number of names resolved by this context */ + int nErr; /* Number of errors encountered while resolving names */ + u16 ncFlags; /* Zero or more NC_* flags defined below */ ++ Select *pWinSelect; /* SELECT statement for any window functions */ + }; + + /* + ** Allowed values for the NameContext, ncFlags field. + ** +-** Note: NC_MinMaxAgg must have the same value as SF_MinMaxAgg and +-** SQLITE_FUNC_MINMAX. +-** ++** Value constraints (all checked via assert()): ++** NC_HasAgg == SF_HasAgg == EP_Agg ++** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX ++** NC_HasWin == EP_Win ++** + */ + #define NC_AllowAgg 0x0001 /* Aggregate functions are allowed here */ +-#define NC_HasAgg 0x0002 /* One or more aggregate functions seen */ ++#define NC_PartIdx 0x0002 /* True if resolving a partial index WHERE */ + #define NC_IsCheck 0x0004 /* True if resolving names in a CHECK constraint */ + #define NC_InAggFunc 0x0008 /* True if analyzing arguments to an agg func */ +-#define NC_PartIdx 0x0010 /* True if resolving a partial index WHERE */ ++#define NC_HasAgg 0x0010 /* One or more aggregate functions seen */ ++#define NC_IdxExpr 0x0020 /* True if resolving columns of CREATE INDEX */ ++#define NC_VarSelect 0x0040 /* A correlated subquery has been seen */ ++#define NC_UEList 0x0080 /* True if uNC.pEList is used */ ++#define NC_UAggInfo 0x0100 /* True if uNC.pAggInfo is used */ ++#define NC_UUpsert 0x0200 /* True if uNC.pUpsert is used */ + #define NC_MinMaxAgg 0x1000 /* min/max aggregates seen. See note above */ ++#define NC_Complex 0x2000 /* True if a function or subquery seen */ ++#define NC_AllowWin 0x4000 /* Window functions are allowed here */ ++#define NC_HasWin 0x8000 /* One or more window functions seen */ ++ ++/* ++** An instance of the following object describes a single ON CONFLICT ++** clause in an upsert. ++** ++** The pUpsertTarget field is only set if the ON CONFLICT clause includes ++** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the ++** conflict-target clause.) The pUpsertTargetWhere is the optional ++** WHERE clause used to identify partial unique indexes. ++** ++** pUpsertSet is the list of column=expr terms of the UPDATE statement. ++** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The ++** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the ++** WHERE clause is omitted. ++*/ ++struct Upsert { ++ ExprList *pUpsertTarget; /* Optional description of conflicting index */ ++ Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */ ++ ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */ ++ Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */ ++ /* The fields above comprise the parse tree for the upsert clause. ++ ** The fields below are used to transfer information from the INSERT ++ ** processing down into the UPDATE processing while generating code. ++ ** Upsert owns the memory allocated above, but not the memory below. */ ++ Index *pUpsertIdx; /* Constraint that pUpsertTarget identifies */ ++ SrcList *pUpsertSrc; /* Table to be updated */ ++ int regData; /* First register holding array of VALUES */ ++ int iDataCur; /* Index of the data cursor */ ++ int iIdxCur; /* Index of the first index cursor */ ++}; + + /* + ** An instance of the following structure contains all information + ** needed to generate code for a single SELECT statement. + ** +-** nLimit is set to -1 if there is no LIMIT clause. nOffset is set to 0. +-** If there is a LIMIT clause, the parser sets nLimit to the value of the +-** limit and nOffset to the value of the offset (or 0 if there is not +-** offset). But later on, nLimit and nOffset become the memory locations +-** in the VDBE that record the limit and offset counters. ++** See the header comment on the computeLimitRegisters() routine for a ++** detailed description of the meaning of the iLimit and iOffset fields. + ** + ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes. + ** These addresses must be stored so that we can go back and fill in +@@ -12277,13 +17762,11 @@ struct NameContext { + struct Select { + ExprList *pEList; /* The fields of the result */ + u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */ +- u16 selFlags; /* Various SF_* values */ ++ LogEst nSelectRow; /* Estimated number of result rows */ ++ u32 selFlags; /* Various SF_* values */ + int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */ +-#if SELECTTRACE_ENABLED +- char zSelName[12]; /* Symbolic name of this SELECT use for debugging */ +-#endif ++ u32 selId; /* Unique identifier number for this SELECT */ + int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */ +- u64 nSelectRow; /* Estimated number of result rows */ + SrcList *pSrc; /* The FROM clause */ + Expr *pWhere; /* The WHERE clause */ + ExprList *pGroupBy; /* The GROUP BY clause */ +@@ -12292,36 +17775,48 @@ struct Select { + Select *pPrior; /* Prior select in a compound select statement */ + Select *pNext; /* Next select to the left in a compound */ + Expr *pLimit; /* LIMIT expression. NULL means not used. */ +- Expr *pOffset; /* OFFSET expression. NULL means not used. */ + With *pWith; /* WITH clause attached to this select. Or NULL. */ ++#ifndef SQLITE_OMIT_WINDOWFUNC ++ Window *pWin; /* List of window functions */ ++ Window *pWinDefn; /* List of named window definitions */ ++#endif + }; + + /* + ** Allowed values for Select.selFlags. The "SF" prefix stands for + ** "Select Flag". +-*/ +-#define SF_Distinct 0x0001 /* Output should be DISTINCT */ +-#define SF_Resolved 0x0002 /* Identifiers have been resolved */ +-#define SF_Aggregate 0x0004 /* Contains aggregate functions */ +-#define SF_UsesEphemeral 0x0008 /* Uses the OpenEphemeral opcode */ +-#define SF_Expanded 0x0010 /* sqlite3SelectExpand() called on this */ +-#define SF_HasTypeInfo 0x0020 /* FROM subqueries have Table metadata */ +-#define SF_Compound 0x0040 /* Part of a compound query */ +-#define SF_Values 0x0080 /* Synthesized from VALUES clause */ +-#define SF_MultiValue 0x0100 /* Single VALUES term with multiple rows */ +-#define SF_NestedFrom 0x0200 /* Part of a parenthesized FROM clause */ +-#define SF_MaybeConvert 0x0400 /* Need convertCompoundSelectToSubquery() */ +-#define SF_Recursive 0x0800 /* The recursive part of a recursive CTE */ +-#define SF_MinMaxAgg 0x1000 /* Aggregate containing min() or max() */ +-#define SF_Converted 0x2000 /* By convertCompoundSelectToSubquery() */ +- ++** ++** Value constraints (all checked via assert()) ++** SF_HasAgg == NC_HasAgg ++** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX ++** SF_FixedLimit == WHERE_USE_LIMIT ++*/ ++#define SF_Distinct 0x00001 /* Output should be DISTINCT */ ++#define SF_All 0x00002 /* Includes the ALL keyword */ ++#define SF_Resolved 0x00004 /* Identifiers have been resolved */ ++#define SF_Aggregate 0x00008 /* Contains agg functions or a GROUP BY */ ++#define SF_HasAgg 0x00010 /* Contains aggregate functions */ ++#define SF_UsesEphemeral 0x00020 /* Uses the OpenEphemeral opcode */ ++#define SF_Expanded 0x00040 /* sqlite3SelectExpand() called on this */ ++#define SF_HasTypeInfo 0x00080 /* FROM subqueries have Table metadata */ ++#define SF_Compound 0x00100 /* Part of a compound query */ ++#define SF_Values 0x00200 /* Synthesized from VALUES clause */ ++#define SF_MultiValue 0x00400 /* Single VALUES term with multiple rows */ ++#define SF_NestedFrom 0x00800 /* Part of a parenthesized FROM clause */ ++#define SF_MinMaxAgg 0x01000 /* Aggregate containing min() or max() */ ++#define SF_Recursive 0x02000 /* The recursive part of a recursive CTE */ ++#define SF_FixedLimit 0x04000 /* nSelectRow set by a constant LIMIT */ ++#define SF_MaybeConvert 0x08000 /* Need convertCompoundSelectToSubquery() */ ++#define SF_Converted 0x10000 /* By convertCompoundSelectToSubquery() */ ++#define SF_IncludeHidden 0x20000 /* Include hidden columns in output */ ++#define SF_ComplexResult 0x40000 /* Result contains subquery or function */ + + /* + ** The results of a SELECT can be distributed in several ways, as defined + ** by one of the following macros. The "SRT" prefix means "SELECT Result + ** Type". + ** +-** SRT_Union Store results as a key in a temporary index ++** SRT_Union Store results as a key in a temporary index + ** identified by pDest->iSDParm. + ** + ** SRT_Except Remove results from the temporary index pDest->iSDParm. +@@ -12345,7 +17840,7 @@ struct Select { + ** of the query. This destination implies "LIMIT 1". + ** + ** SRT_Set The result must be a single column. Store each +-** row of result as the key in table pDest->iSDParm. ++** row of result as the key in table pDest->iSDParm. + ** Apply the affinity pDest->affSdst before storing + ** results. Used to implement "IN (SELECT ...)". + ** +@@ -12405,19 +17900,19 @@ struct Select { + */ + struct SelectDest { + u8 eDest; /* How to dispose of the results. On of SRT_* above. */ +- char affSdst; /* Affinity used when eDest==SRT_Set */ + int iSDParm; /* A parameter used by the eDest disposal method */ + int iSdst; /* Base register where results are written */ + int nSdst; /* Number of registers allocated */ ++ char *zAffSdst; /* Affinity used when eDest==SRT_Set */ + ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */ + }; + + /* +-** During code generation of statements that do inserts into AUTOINCREMENT ++** During code generation of statements that do inserts into AUTOINCREMENT + ** tables, the following information is attached to the Table.u.autoInc.p + ** pointer of each autoincrement table to record some side information that + ** the code generator needs. We have to keep per-table autoincrement +-** information in case inserts are down within triggers. Triggers do not ++** information in case inserts are done within triggers. Triggers do not + ** normally coordinate their activities, but we do need to coordinate the + ** loading and saving of autoincrement information. + */ +@@ -12429,14 +17924,7 @@ struct AutoincInfo { + }; + + /* +-** Size of the column cache +-*/ +-#ifndef SQLITE_N_COLCACHE +-# define SQLITE_N_COLCACHE 10 +-#endif +- +-/* +-** At least one instance of the following structure is created for each ++** At least one instance of the following structure is created for each + ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE + ** statement. All such objects are stored in the linked list headed at + ** Parse.pTriggerPrg and deleted once statement compilation has been +@@ -12449,7 +17937,7 @@ struct AutoincInfo { + ** values for both pTrigger and orconf. + ** + ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns +-** accessed (or set to 0 for triggers fired as a result of INSERT ++** accessed (or set to 0 for triggers fired as a result of INSERT + ** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to + ** a mask of new.* columns used by the program. + */ +@@ -12490,7 +17978,7 @@ struct TriggerPrg { + ** is constant but the second part is reset at the beginning and end of + ** each recursion. + ** +-** The nTableLock and aTableLock variables are only used if the shared-cache ++** The nTableLock and aTableLock variables are only used if the shared-cache + ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are + ** used to store the set of table-locks required by the statement being + ** compiled. Function sqlite3TableLock() is used to add entries to the +@@ -12509,53 +17997,36 @@ struct Parse { + u8 mayAbort; /* True if statement may throw an ABORT exception */ + u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */ + u8 okConstFactor; /* OK to factor out constants */ +- int aTempReg[8]; /* Holding area for temporary registers */ ++ u8 disableLookaside; /* Number of times lookaside has been disabled */ ++ u8 disableVtab; /* Disable all virtual tables for this parse */ + int nRangeReg; /* Size of the temporary register block */ + int iRangeReg; /* First register in temporary register block */ + int nErr; /* Number of errors seen */ + int nTab; /* Number of previously allocated VDBE cursors */ + int nMem; /* Number of memory cells used so far */ +- int nSet; /* Number of sets used so far */ +- int nOnce; /* Number of OP_Once instructions so far */ +- int nOpAlloc; /* Number of slots allocated for Vdbe.aOp[] */ +- int iFixedOp; /* Never back out opcodes iFixedOp-1 or earlier */ +- int ckBase; /* Base register of data during check constraints */ +- int iPartIdxTab; /* Table corresponding to a partial index */ +- int iCacheLevel; /* ColCache valid when aColCache[].iLevel<=iCacheLevel */ +- int iCacheCnt; /* Counter used to generate aColCache[].lru values */ +- int nLabel; /* Number of labels used */ ++ int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */ ++ int iSelfTab; /* Table associated with an index on expr, or negative ++ ** of the base register during check-constraint eval */ ++ int nLabel; /* The *negative* of the number of labels used */ ++ int nLabelAlloc; /* Number of slots in aLabel */ + int *aLabel; /* Space to hold the labels */ +- struct yColCache { +- int iTable; /* Table cursor number */ +- i16 iColumn; /* Table column number */ +- u8 tempReg; /* iReg is a temp register that needs to be freed */ +- int iLevel; /* Nesting level */ +- int iReg; /* Reg with value of this column. 0 means none. */ +- int lru; /* Least recently used entry has the smallest value */ +- } aColCache[SQLITE_N_COLCACHE]; /* One for each column cache entry */ + ExprList *pConstExpr;/* Constant expressions */ + Token constraintName;/* Name of the constraint currently being parsed */ + yDbMask writeMask; /* Start a write transaction on these databases */ + yDbMask cookieMask; /* Bitmask of schema verified databases */ +- int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */ + int regRowid; /* Register holding rowid of CREATE TABLE entry */ + int regRoot; /* Register holding root page number for new objects */ + int nMaxArg; /* Max args passed to user function by sub-program */ +-#if SELECTTRACE_ENABLED +- int nSelect; /* Number of SELECT statements seen */ +- int nSelectIndent; /* How far to indent SELECTTRACE() output */ +-#endif ++ int nSelect; /* Number of SELECT stmts. Counter for Select.selId */ + #ifndef SQLITE_OMIT_SHARED_CACHE + int nTableLock; /* Number of locks in aTableLock */ + TableLock *aTableLock; /* Required table locks for shared-cache mode */ + #endif + AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */ +- +- /* Information used while coding trigger programs. */ + Parse *pToplevel; /* Parse structure for main program (or NULL) */ + Table *pTriggerTab; /* Table triggers are being coded for */ +- int addrCrTab; /* Address of OP_CreateTable opcode on CREATE TABLE */ +- int addrSkipPK; /* Address of instruction to skip PRIMARY KEY index */ ++ Parse *pParentParse; /* Parent parser if this parser is nested */ ++ int addrCrTab; /* Address of OP_CreateBtree opcode on CREATE TABLE */ + u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */ + u32 oldmask; /* Mask of old.* columns referenced */ + u32 newmask; /* Mask of new.* columns referenced */ +@@ -12563,36 +18034,46 @@ struct Parse { + u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */ + u8 disableTriggers; /* True to disable triggers */ + ++ /************************************************************************** ++ ** Fields above must be initialized to zero. The fields that follow, ++ ** down to the beginning of the recursive section, do not need to be ++ ** initialized as they will be set before being used. The boundary is ++ ** determined by offsetof(Parse,aTempReg). ++ **************************************************************************/ ++ ++ int aTempReg[8]; /* Holding area for temporary registers */ ++ Token sNameToken; /* Token with unqualified schema object name */ ++ + /************************************************************************ + ** Above is constant between recursions. Below is reset before and after + ** each recursion. The boundary between these two regions is determined +- ** using offsetof(Parse,nVar) so the nVar field must be the first field +- ** in the recursive region. ++ ** using offsetof(Parse,sLastToken) so the sLastToken field must be the ++ ** first field in the recursive region. + ************************************************************************/ + +- int nVar; /* Number of '?' variables seen in the SQL so far */ +- int nzVar; /* Number of available slots in azVar[] */ ++ Token sLastToken; /* The last token parsed */ ++ ynVar nVar; /* Number of '?' variables seen in the SQL so far */ + u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */ +- u8 bFreeWith; /* True if pWith should be freed with parser */ + u8 explain; /* True if the EXPLAIN flag is found on the query */ ++#if !(defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE)) ++ u8 eParseMode; /* PARSE_MODE_XXX constant */ ++#endif + #ifndef SQLITE_OMIT_VIRTUALTABLE +- u8 declareVtab; /* True if inside sqlite3_declare_vtab() */ + int nVtabLock; /* Number of virtual tables to lock */ + #endif +- int nAlias; /* Number of aliased result set columns */ + int nHeight; /* Expression tree height of current sub-select */ + #ifndef SQLITE_OMIT_EXPLAIN +- int iSelectId; /* ID of current select for EXPLAIN output */ +- int iNextSelectId; /* Next available select ID for EXPLAIN output */ ++ int addrExplain; /* Address of current OP_Explain opcode */ + #endif +- char **azVar; /* Pointers to names of parameters */ ++ VList *pVList; /* Mapping between variable names and numbers */ + Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */ + const char *zTail; /* All SQL text past the last semicolon parsed */ + Table *pNewTable; /* A table being constructed by CREATE TABLE */ ++ Index *pNewIndex; /* An index being constructed by CREATE INDEX. ++ ** Also used to hold redundant UNIQUE constraints ++ ** during a RENAME COLUMN */ + Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */ + const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */ +- Token sNameToken; /* Token with unqualified schema object name */ +- Token sLastToken; /* The last token parsed */ + #ifndef SQLITE_OMIT_VIRTUALTABLE + Token sArg; /* Complete text of a module argument */ + Table **apVtabLock; /* Pointer to virtual tables needing locking */ +@@ -12600,15 +18081,44 @@ struct Parse { + Table *pZombieTab; /* List of Table objects to delete after code gen */ + TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */ + With *pWith; /* Current WITH clause, or NULL */ ++ With *pWithToFree; /* Free this WITH object at the end of the parse */ ++#ifndef SQLITE_OMIT_ALTERTABLE ++ RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */ ++#endif + }; + ++#define PARSE_MODE_NORMAL 0 ++#define PARSE_MODE_DECLARE_VTAB 1 ++#define PARSE_MODE_RENAME_COLUMN 2 ++#define PARSE_MODE_RENAME_TABLE 3 ++ ++/* ++** Sizes and pointers of various parts of the Parse object. ++*/ ++#define PARSE_HDR_SZ offsetof(Parse,aTempReg) /* Recursive part w/o aColCache*/ ++#define PARSE_RECURSE_SZ offsetof(Parse,sLastToken) /* Recursive part */ ++#define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */ ++#define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ) /* Pointer to tail */ ++ + /* + ** Return true if currently inside an sqlite3_declare_vtab() call. + */ + #ifdef SQLITE_OMIT_VIRTUALTABLE + #define IN_DECLARE_VTAB 0 + #else +- #define IN_DECLARE_VTAB (pParse->declareVtab) ++ #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB) ++#endif ++ ++#if defined(SQLITE_OMIT_ALTERTABLE) ++ #define IN_RENAME_OBJECT 0 ++#else ++ #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME_COLUMN) ++#endif ++ ++#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE) ++ #define IN_SPECIAL_PARSE 0 ++#else ++ #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL) + #endif + + /* +@@ -12622,26 +18132,42 @@ struct AuthContext { + + /* + ** Bitfield flags for P5 value in various opcodes. +-*/ +-#define OPFLAG_NCHANGE 0x01 /* Set to update db->nChange */ ++** ++** Value constraints (enforced via assert()): ++** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH ++** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF ++** OPFLAG_BULKCSR == BTREE_BULKLOAD ++** OPFLAG_SEEKEQ == BTREE_SEEK_EQ ++** OPFLAG_FORDELETE == BTREE_FORDELETE ++** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION ++** OPFLAG_AUXDELETE == BTREE_AUXDELETE ++*/ ++#define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */ ++ /* Also used in P2 (not P5) of OP_Delete */ ++#define OPFLAG_NOCHNG 0x01 /* OP_VColumn nochange for UPDATE */ + #define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */ +-#define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */ ++#define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */ + #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */ + #define OPFLAG_APPEND 0x08 /* This is likely to be an append */ + #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */ ++#define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */ + #define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */ + #define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */ + #define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */ + #define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */ +-#define OPFLAG_P2ISREG 0x04 /* P2 to OP_Open** is a register number */ ++#define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */ ++#define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */ + #define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */ ++#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete/Insert: save cursor pos */ ++#define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */ ++#define OPFLAG_NOCHNG_MAGIC 0x6d /* OP_MakeRecord: serialtype 10 is ok */ + + /* + * Each trigger present in the database schema is stored as an instance of +- * struct Trigger. ++ * struct Trigger. + * + * Pointers to instances of struct Trigger are stored in two ways. +- * 1. In the "trigHash" hash table (part of the sqlite3* that represents the ++ * 1. In the "trigHash" hash table (part of the sqlite3* that represents the + * database). This allows Trigger structures to be retrieved by name. + * 2. All triggers associated with a single table form a linked list, using the + * pNext member of struct Trigger. A pointer to the first element of the +@@ -12667,7 +18193,7 @@ struct Trigger { + + /* + ** A trigger is either a BEFORE or an AFTER trigger. The following constants +-** determine which. ++** determine which. + ** + ** If there are multiple triggers, you might of some BEFORE and some AFTER. + ** In that cases, the constants below can be ORed together. +@@ -12677,15 +18203,15 @@ struct Trigger { + + /* + * An instance of struct TriggerStep is used to store a single SQL statement +- * that is a part of a trigger-program. ++ * that is a part of a trigger-program. + * + * Instances of struct TriggerStep are stored in a singly linked list (linked +- * using the "pNext" member) referenced by the "step_list" member of the ++ * using the "pNext" member) referenced by the "step_list" member of the + * associated struct Trigger instance. The first element of the linked list is + * the first step of the trigger-program. +- * ++ * + * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or +- * "SELECT" statement. The meanings of the other members is determined by the ++ * "SELECT" statement. The meanings of the other members is determined by the + * value of "op" as follows: + * + * (op == TK_INSERT) +@@ -12695,7 +18221,7 @@ struct Trigger { + * zTarget -> Dequoted name of the table to insert into. + * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then + * this stores values to be inserted. Otherwise NULL. +- * pIdList -> If this is an INSERT INTO ... ( ) VALUES ... ++ * pIdList -> If this is an INSERT INTO ... ( ) VALUES ... + * statement, then this stores the column-names to be + * inserted into. + * +@@ -12703,7 +18229,7 @@ struct Trigger { + * zTarget -> Dequoted name of the table to delete from. + * pWhere -> The WHERE clause of the DELETE statement if one is specified. + * Otherwise NULL. +- * ++ * + * (op == TK_UPDATE) + * zTarget -> Dequoted name of the table to update. + * pWhere -> The WHERE clause of the UPDATE statement if one is specified. +@@ -12711,7 +18237,7 @@ struct Trigger { + * pExprList -> A list of the columns to update and the expressions to update + * them to. See sqlite3Update() documentation of "pChanges" + * argument. +- * ++ * + */ + struct TriggerStep { + u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */ +@@ -12720,8 +18246,10 @@ struct TriggerStep { + Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */ + char *zTarget; /* Target table for DELETE, UPDATE, INSERT */ + Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */ +- ExprList *pExprList; /* SET clause for UPDATE. */ ++ ExprList *pExprList; /* SET clause for UPDATE */ + IdList *pIdList; /* Column names for INSERT */ ++ Upsert *pUpsert; /* Upsert clauses on an INSERT */ ++ char *zSpan; /* Original SQL text of this command */ + TriggerStep *pNext; /* Next in the link-list */ + TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */ + }; +@@ -12729,7 +18257,7 @@ struct TriggerStep { + /* + ** The following structure contains information used by the sqliteFix... + ** routines as they walk the parse tree to make database references +-** explicit. ++** explicit. + */ + typedef struct DbFixer DbFixer; + struct DbFixer { +@@ -12745,17 +18273,21 @@ struct DbFixer { + ** An objected used to accumulate the text of a string where we + ** do not necessarily know how big the string will be in the end. + */ +-struct StrAccum { ++struct sqlite3_str { + sqlite3 *db; /* Optional database for lookaside. Can be NULL */ +- char *zBase; /* A base allocation. Not from malloc. */ + char *zText; /* The string collected so far */ +- int nChar; /* Length of the string so far */ +- int nAlloc; /* Amount of space allocated in zText */ +- int mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */ +- u8 accError; /* STRACCUM_NOMEM or STRACCUM_TOOBIG */ ++ u32 nAlloc; /* Amount of space allocated in zText */ ++ u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */ ++ u32 nChar; /* Length of the string so far */ ++ u8 accError; /* SQLITE_NOMEM or SQLITE_TOOBIG */ ++ u8 printfFlags; /* SQLITE_PRINTF flags below */ + }; +-#define STRACCUM_NOMEM 1 +-#define STRACCUM_TOOBIG 2 ++#define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */ ++#define SQLITE_PRINTF_SQLFUNC 0x02 /* SQL function arguments to VXPrintf */ ++#define SQLITE_PRINTF_MALLOCED 0x04 /* True if xText is allocated space */ ++ ++#define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0) ++ + + /* + ** A pointer to this structure is used to communicate information +@@ -12766,8 +18298,15 @@ typedef struct { + char **pzErrMsg; /* Error message stored here */ + int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */ + int rc; /* Result code stored here */ ++ u32 mInitFlags; /* Flags controlling error messages */ ++ u32 nInitRow; /* Number of rows processed */ + } InitData; + ++/* ++** Allowed values for mInitFlags ++*/ ++#define INITFLAG_AlterTable 0x0001 /* This is a reparse after ALTER TABLE */ ++ + /* + ** Structure containing global configuration data for the SQLite library. + ** +@@ -12779,10 +18318,12 @@ struct Sqlite3Config { + int bFullMutex; /* True to enable full mutexing */ + int bOpenUri; /* True to interpret filenames as URIs */ + int bUseCis; /* Use covering indices for full-scans */ ++ int bSmallMalloc; /* Avoid large memory allocations if true */ + int mxStrlen; /* Maximum string length */ + int neverCorrupt; /* Database is always well-formed */ + int szLookaside; /* Default lookaside buffer size */ + int nLookaside; /* Default lookaside buffer count */ ++ int nStmtSpill; /* Stmt-journal spill-to-disk threshold */ + sqlite3_mem_methods m; /* Low-level memory allocation interface */ + sqlite3_mutex_methods mutex; /* Low-level mutex interface */ + sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */ +@@ -12791,9 +18332,6 @@ struct Sqlite3Config { + int mnReq, mxReq; /* Min and max heap requests sizes */ + sqlite3_int64 szMmap; /* mmap() space per open file */ + sqlite3_int64 mxMmap; /* Maximum value for szMmap */ +- void *pScratch; /* Scratch memory */ +- int szScratch; /* Size of each scratch buffer */ +- int nScratch; /* Number of scratch buffers */ + void *pPage; /* Page cache memory */ + int szPage; /* Size of each page in pPage[] */ + int nPage; /* Number of pages in pPage[] */ +@@ -12819,13 +18357,19 @@ struct Sqlite3Config { + /* The following callback (if not NULL) is invoked on every VDBE branch + ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE. + */ +- void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx); /* Callback */ ++ void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */ + void *pVdbeBranchArg; /* 1st argument */ + #endif +-#ifndef SQLITE_OMIT_BUILTIN_TEST ++#ifdef SQLITE_ENABLE_DESERIALIZE ++ sqlite3_int64 mxMemdbSize; /* Default max memdb size */ ++#endif ++#ifndef SQLITE_UNTESTABLE + int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */ + #endif + int bLocaltimeFault; /* True to fail localtime() calls */ ++ int bInternalFunctions; /* Internal SQL functions are visible */ ++ int iOnceResetThreshold; /* When to reset OP_Once counters */ ++ u32 szSorterRef; /* Min size in bytes to use sorter-refs */ + }; + + /* +@@ -12850,18 +18394,27 @@ struct Sqlite3Config { + ** Context pointer passed down through the tree-walk. + */ + struct Walker { ++ Parse *pParse; /* Parser context. */ + int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */ + int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */ + void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */ +- Parse *pParse; /* Parser context. */ + int walkerDepth; /* Number of subqueries */ + u8 eCode; /* A small processing code */ + union { /* Extra data for callback */ +- NameContext *pNC; /* Naming context */ +- int n; /* A counter */ +- int iCur; /* A cursor number */ +- SrcList *pSrcList; /* FROM clause */ +- struct SrcCount *pSrcCount; /* Counting column references */ ++ NameContext *pNC; /* Naming context */ ++ int n; /* A counter */ ++ int iCur; /* A cursor number */ ++ SrcList *pSrcList; /* FROM clause */ ++ struct SrcCount *pSrcCount; /* Counting column references */ ++ struct CCurHint *pCCurHint; /* Used by codeCursorHint() */ ++ int *aiCol; /* array of column indexes */ ++ struct IdxCover *pIdxCover; /* Check for index coverage */ ++ struct IdxExprTrans *pIdxTrans; /* Convert idxed expr to column */ ++ ExprList *pGroupBy; /* GROUP BY clause */ ++ Select *pSelect; /* HAVING to WHERE clause ctx */ ++ struct WindowRewrite *pRewrite; /* Window rewrite context */ ++ struct WhereConst *pConst; /* WHERE clause constants */ ++ struct RenameCtx *pRename; /* RENAME COLUMN context */ + } u; + }; + +@@ -12871,6 +18424,12 @@ SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*); + SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*); + SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*); + SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*); ++SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker*, Expr*); ++SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker*, Select*); ++SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker*, Select*); ++#ifdef SQLITE_DEBUG ++SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker*, Select*); ++#endif + + /* + ** Return code from the parse-tree walking primitives and their +@@ -12891,7 +18450,7 @@ struct With { + char *zName; /* Name of this CTE */ + ExprList *pCols; /* List of explicit column names, or NULL */ + Select *pSelect; /* The definition of this CTE */ +- const char *zErr; /* Error message for circular references */ ++ const char *zCteErr; /* Error message for circular references */ + } a[1]; + }; + +@@ -12906,6 +18465,75 @@ struct TreeView { + }; + #endif /* SQLITE_DEBUG */ + ++/* ++** This object is used in various ways, all related to window functions ++** ++** (1) A single instance of this structure is attached to the ++** the Expr.pWin field for each window function in an expression tree. ++** This object holds the information contained in the OVER clause, ++** plus additional fields used during code generation. ++** ++** (2) All window functions in a single SELECT form a linked-list ++** attached to Select.pWin. The Window.pFunc and Window.pExpr ++** fields point back to the expression that is the window function. ++** ++** (3) The terms of the WINDOW clause of a SELECT are instances of this ++** object on a linked list attached to Select.pWinDefn. ++** ++** The uses (1) and (2) are really the same Window object that just happens ++** to be accessible in two different ways. Use case (3) are separate objects. ++*/ ++struct Window { ++ char *zName; /* Name of window (may be NULL) */ ++ char *zBase; /* Name of base window for chaining (may be NULL) */ ++ ExprList *pPartition; /* PARTITION BY clause */ ++ ExprList *pOrderBy; /* ORDER BY clause */ ++ u8 eFrmType; /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */ ++ u8 eStart; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */ ++ u8 eEnd; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */ ++ u8 bImplicitFrame; /* True if frame was implicitly specified */ ++ u8 eExclude; /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */ ++ Expr *pStart; /* Expression for " PRECEDING" */ ++ Expr *pEnd; /* Expression for " FOLLOWING" */ ++ Window *pNextWin; /* Next window function belonging to this SELECT */ ++ Expr *pFilter; /* The FILTER expression */ ++ FuncDef *pFunc; /* The function */ ++ int iEphCsr; /* Partition buffer or Peer buffer */ ++ int regAccum; ++ int regResult; ++ int csrApp; /* Function cursor (used by min/max) */ ++ int regApp; /* Function register (also used by min/max) */ ++ int regPart; /* Array of registers for PARTITION BY values */ ++ Expr *pOwner; /* Expression object this window is attached to */ ++ int nBufferCol; /* Number of columns in buffer table */ ++ int iArgCol; /* Offset of first argument for this function */ ++ int regOne; /* Register containing constant value 1 */ ++ int regStartRowid; ++ int regEndRowid; ++}; ++ ++#ifndef SQLITE_OMIT_WINDOWFUNC ++SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*); ++SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p); ++SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8); ++SQLITE_PRIVATE void sqlite3WindowAttach(Parse*, Expr*, Window*); ++SQLITE_PRIVATE int sqlite3WindowCompare(Parse*, Window*, Window*); ++SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Window*); ++SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int); ++SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*); ++SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*); ++SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*); ++SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p); ++SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p); ++SQLITE_PRIVATE void sqlite3WindowFunctions(void); ++SQLITE_PRIVATE void sqlite3WindowChain(Parse*, Window*, Window*); ++SQLITE_PRIVATE Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*); ++#else ++# define sqlite3WindowDelete(a,b) ++# define sqlite3WindowFunctions() ++# define sqlite3WindowAttach(a,b,c) ++#endif ++ + /* + ** Assuming zIn points to the first byte of a UTF-8 character, + ** advance zIn to point to the first byte of the next UTF-8 character. +@@ -12923,13 +18551,33 @@ struct TreeView { + ** using sqlite3_log(). The routines also provide a convenient place + ** to set a debugger breakpoint. + */ ++SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType); + SQLITE_PRIVATE int sqlite3CorruptError(int); + SQLITE_PRIVATE int sqlite3MisuseError(int); + SQLITE_PRIVATE int sqlite3CantopenError(int); + #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__) + #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__) + #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__) ++#ifdef SQLITE_DEBUG ++SQLITE_PRIVATE int sqlite3NomemError(int); ++SQLITE_PRIVATE int sqlite3IoerrnomemError(int); ++SQLITE_PRIVATE int sqlite3CorruptPgnoError(int,Pgno); ++# define SQLITE_NOMEM_BKPT sqlite3NomemError(__LINE__) ++# define SQLITE_IOERR_NOMEM_BKPT sqlite3IoerrnomemError(__LINE__) ++# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptPgnoError(__LINE__,(P)) ++#else ++# define SQLITE_NOMEM_BKPT SQLITE_NOMEM ++# define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM ++# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptError(__LINE__) ++#endif + ++/* ++** FTS3 and FTS4 both require virtual table support ++*/ ++#if defined(SQLITE_OMIT_VIRTUALTABLE) ++# undef SQLITE_ENABLE_FTS3 ++# undef SQLITE_ENABLE_FTS4 ++#endif + + /* + ** FTS4 is really an extension for FTS3. It is enabled using the +@@ -12962,6 +18610,7 @@ SQLITE_PRIVATE int sqlite3CantopenError(int); + # define sqlite3Isdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x04) + # define sqlite3Isxdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x08) + # define sqlite3Tolower(x) (sqlite3UpperToLower[(unsigned char)(x)]) ++# define sqlite3Isquote(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x80) + #else + # define sqlite3Toupper(x) toupper((unsigned char)(x)) + # define sqlite3Isspace(x) isspace((unsigned char)(x)) +@@ -12970,14 +18619,17 @@ SQLITE_PRIVATE int sqlite3CantopenError(int); + # define sqlite3Isdigit(x) isdigit((unsigned char)(x)) + # define sqlite3Isxdigit(x) isxdigit((unsigned char)(x)) + # define sqlite3Tolower(x) tolower((unsigned char)(x)) ++# define sqlite3Isquote(x) ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`') + #endif + SQLITE_PRIVATE int sqlite3IsIdChar(u8); + + /* + ** Internal function prototypes + */ +-#define sqlite3StrICmp sqlite3_stricmp ++SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*); + SQLITE_PRIVATE int sqlite3Strlen30(const char*); ++#define sqlite3Strlen30NN(C) (strlen(C)&0x3fffffff) ++SQLITE_PRIVATE char *sqlite3ColumnType(Column*,char*); + #define sqlite3StrNICmp sqlite3_strnicmp + + SQLITE_PRIVATE int sqlite3MallocInit(void); +@@ -12986,20 +18638,23 @@ SQLITE_PRIVATE void *sqlite3Malloc(u64); + SQLITE_PRIVATE void *sqlite3MallocZero(u64); + SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, u64); + SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, u64); ++SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3*, u64); + SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*); + SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, u64); ++SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3*,const char*,const char*); + SQLITE_PRIVATE void *sqlite3Realloc(void*, u64); + SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64); + SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64); + SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*); ++SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3*, void*); + SQLITE_PRIVATE int sqlite3MallocSize(void*); + SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*); +-SQLITE_PRIVATE void *sqlite3ScratchMalloc(int); +-SQLITE_PRIVATE void sqlite3ScratchFree(void*); + SQLITE_PRIVATE void *sqlite3PageMalloc(int); + SQLITE_PRIVATE void sqlite3PageFree(void*); + SQLITE_PRIVATE void sqlite3MemSetDefault(void); ++#ifndef SQLITE_UNTESTABLE + SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void)); ++#endif + SQLITE_PRIVATE int sqlite3HeapNearlyFull(void); + + /* +@@ -13013,18 +18668,22 @@ SQLITE_PRIVATE int sqlite3HeapNearlyFull(void); + #ifdef SQLITE_USE_ALLOCA + # define sqlite3StackAllocRaw(D,N) alloca(N) + # define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N) +-# define sqlite3StackFree(D,P) ++# define sqlite3StackFree(D,P) + #else + # define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N) + # define sqlite3StackAllocZero(D,N) sqlite3DbMallocZero(D,N) + # define sqlite3StackFree(D,P) sqlite3DbFree(D,P) + #endif + +-#ifdef SQLITE_ENABLE_MEMSYS3 +-SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void); +-#endif ++/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they ++** are, disable MEMSYS3 ++*/ + #ifdef SQLITE_ENABLE_MEMSYS5 + SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void); ++#undef SQLITE_ENABLE_MEMSYS3 ++#endif ++#ifdef SQLITE_ENABLE_MEMSYS3 ++SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void); + #endif + + +@@ -13035,16 +18694,28 @@ SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int); + SQLITE_PRIVATE int sqlite3MutexInit(void); + SQLITE_PRIVATE int sqlite3MutexEnd(void); + #endif ++#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP) ++SQLITE_PRIVATE void sqlite3MemoryBarrier(void); ++#else ++# define sqlite3MemoryBarrier() ++#endif + + SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int); + SQLITE_PRIVATE void sqlite3StatusUp(int, int); + SQLITE_PRIVATE void sqlite3StatusDown(int, int); +-SQLITE_PRIVATE void sqlite3StatusSet(int, int); ++SQLITE_PRIVATE void sqlite3StatusHighwater(int, int); ++SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3*,int*); + + /* Access to mutexes used by sqlite3_status() */ + SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void); + SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void); + ++#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT) ++SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex*); ++#else ++# define sqlite3MutexWarnOnContention(x) ++#endif ++ + #ifndef SQLITE_OMIT_FLOATING_POINT + SQLITE_PRIVATE int sqlite3IsNaN(double); + #else +@@ -13061,13 +18732,8 @@ struct PrintfArguments { + sqlite3_value **apArg; /* The argument values */ + }; + +-#define SQLITE_PRINTF_INTERNAL 0x01 +-#define SQLITE_PRINTF_SQLFUNC 0x02 +-SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list); +-SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...); + SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...); + SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list); +-SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...); + #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) + SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...); + #endif +@@ -13076,19 +18742,25 @@ SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*); + #endif + + #if defined(SQLITE_DEBUG) +-SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView*,u8); +-SQLITE_PRIVATE void sqlite3TreeViewPop(TreeView*); +-SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView*, const char*, ...); +-SQLITE_PRIVATE void sqlite3TreeViewItem(TreeView*, const char*, u8); + SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView*, const Expr*, u8); ++SQLITE_PRIVATE void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*); + SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*); ++SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView*, const SrcList*); + SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8); ++SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView*, const With*, u8); ++#ifndef SQLITE_OMIT_WINDOWFUNC ++SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView*, const Window*, u8); ++SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8); ++#endif + #endif + + +-SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...); ++SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*); + SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...); +-SQLITE_PRIVATE int sqlite3Dequote(char*); ++SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3*,int); ++SQLITE_PRIVATE void sqlite3Dequote(char*); ++SQLITE_PRIVATE void sqlite3DequoteExpr(Expr*); ++SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*); + SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int); + SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **); + SQLITE_PRIVATE void sqlite3FinishCoding(Parse*); +@@ -13097,46 +18769,67 @@ SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int); + SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int); + SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int); + SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*); ++#ifdef SQLITE_DEBUG ++SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse*,int,int); ++#endif + SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int); + SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*); + SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*); +-SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*); ++SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*); ++SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse*, Expr*, Select*); + SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*); +-SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*); +-SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*); ++SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*, int); ++SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32); + SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*); + SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*); ++SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*); ++SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int); + SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int); +-SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*); ++SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*); + SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*); + SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*); ++SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index*); + SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**); + SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**); ++SQLITE_PRIVATE int sqlite3InitOne(sqlite3*, int, char**, u32); + SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int); ++#ifndef SQLITE_OMIT_VIRTUALTABLE ++SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName); ++#endif + SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*); + SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int); + SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*); +-SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int); + SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*); ++SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3*,Table*); ++SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**); ++SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*); + SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*); + SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int); + SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*); + SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index*, i16); + SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int); +-SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*); ++#if SQLITE_ENABLE_HIDDEN_COLUMNS ++SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table*, Column*); ++#else ++# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */ ++#endif ++SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*,Token*); + SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int); + SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int); + SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*); +-SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*); +-SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*); ++SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*); + SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*); + SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*); + SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*, + sqlite3_vfs**,char**,char **); ++#ifdef SQLITE_HAS_CODEC ++SQLITE_PRIVATE int sqlite3CodecQueryParameters(sqlite3*,const char*,const char*); ++#else ++# define sqlite3CodecQueryParameters(A,B,C) 0 ++#endif + SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*); +-SQLITE_PRIVATE int sqlite3CodeOnce(Parse *); + +-#ifdef SQLITE_OMIT_BUILTIN_TEST ++#ifdef SQLITE_UNTESTABLE + # define sqlite3FaultSim(X) SQLITE_OK + #else + SQLITE_PRIVATE int sqlite3FaultSim(int); +@@ -13144,19 +18837,23 @@ SQLITE_PRIVATE int sqlite3FaultSim(int); + + SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32); + SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32); ++SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec*, u32); + SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32); + SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*); + SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*); + SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*); ++#ifndef SQLITE_UNTESTABLE + SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*); ++#endif + +-SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int); +-SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*); ++SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*); ++SQLITE_PRIVATE void sqlite3RowSetDelete(void*); ++SQLITE_PRIVATE void sqlite3RowSetClear(void*); + SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64); + SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64); + SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*); + +-SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int); ++SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int); + + #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) + SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse*,Table*); +@@ -13170,6 +18867,7 @@ SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask); + SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int); + SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int); + SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*); ++SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3*, Index*); + #ifndef SQLITE_OMIT_AUTOINCREMENT + SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse); + SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse); +@@ -13177,108 +18875,132 @@ SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse); + # define sqlite3AutoincrementBegin(X) + # define sqlite3AutoincrementEnd(X) + #endif +-SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int); ++SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*); + SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*); +-SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*); ++SQLITE_PRIVATE IdList *sqlite3IdListAppend(Parse*, IdList*, Token*); + SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*); +-SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int); +-SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*); ++SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int); ++SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(Parse*, SrcList*, Token*, Token*); + SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*, + Token*, Select*, Expr*, IdList*); + SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *); ++SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*); + SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *); + SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*); + SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*); + SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*); + SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*); + SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**); +-SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*, +- Expr*, int, int); ++SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*, ++ Expr*, int, int, u8); + SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int); + SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*); + SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*, +- Expr*,ExprList*,u16,Expr*,Expr*); ++ Expr*,ExprList*,u32,Expr*); + SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*); + SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*); + SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int); + SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int); + #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) +-SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,Expr*,char*); ++SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*); + #endif +-SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*); +-SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int); ++SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*); ++SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*, ++ Upsert*); + SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int); + SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*); +-SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*); ++SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*); + SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*); + SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*); ++SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*); + SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*); + SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*); + SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*); + SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*); ++#define ONEPASS_OFF 0 /* Use of ONEPASS not allowed */ ++#define ONEPASS_SINGLE 1 /* ONEPASS valid for a single row update */ ++#define ONEPASS_MULTI 2 /* ONEPASS is valid for multiple rows */ ++SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int); + SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8); + SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int); + SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int); +-SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int); +-SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*); +-SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*); +-SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int); +-SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*); +-SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int); + SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int); ++SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int); + SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int); +-SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8); ++SQLITE_PRIVATE int sqlite3ExprCodeAtInit(Parse*, Expr*, int); + SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*); + SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int); + SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int); +-SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8); ++SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8); + #define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */ + #define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */ ++#define SQLITE_ECEL_REF 0x04 /* Use ExprList.u.x.iOrderByCol */ ++#define SQLITE_ECEL_OMITREF 0x08 /* Omit if ExprList.u.x.iOrderByCol */ + SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int); + SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int); ++SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int); + SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*); +-SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*); +-SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *); ++#define LOCATE_VIEW 0x01 ++#define LOCATE_NOERR 0x02 ++SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*); ++SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,u32 flags,struct SrcList_item *); + SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*); + SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*); + SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*); +-SQLITE_PRIVATE void sqlite3Vacuum(Parse*); +-SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*); ++SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*,Expr*); ++SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*); + SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*); +-SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*, int); ++SQLITE_PRIVATE int sqlite3ExprCompare(Parse*,Expr*, Expr*, int); ++SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr*, Expr*, int); + SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int); +-SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr*, Expr*, int); ++SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Parse*,Expr*, Expr*, int); ++SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int); + SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*); + SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*); ++SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx); + SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*); + SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*); ++#ifndef SQLITE_UNTESTABLE + SQLITE_PRIVATE void sqlite3PrngSaveState(void); + SQLITE_PRIVATE void sqlite3PrngRestoreState(void); ++#endif + SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int); + SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int); + SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb); + SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int); +-SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*); +-SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*); ++SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int); + SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*); + SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *); + SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*); ++SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*); ++SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*); + SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*); + SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*); + SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8); ++SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*); + SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int); ++#ifdef SQLITE_ENABLE_CURSOR_HINTS ++SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*); ++#endif + SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*); + SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*); + SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char); + SQLITE_PRIVATE int sqlite3IsRowid(const char*); +-SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8); +-SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*); ++SQLITE_PRIVATE void sqlite3GenerateRowDelete( ++ Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int); ++SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int); + SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int); + SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int); ++SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int); + SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int, +- u8,u8,int,int*); ++ u8,u8,int,int*,int*,Upsert*); ++#ifdef SQLITE_ENABLE_NULL_TRIM ++SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe*,Table*); ++#else ++# define sqlite3SetMakeRecordP5(A,B) ++#endif + SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int); +-SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, u8*, int*, int*); ++SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*); + SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int); + SQLITE_PRIVATE void sqlite3MultiWrite(Parse*); + SQLITE_PRIVATE void sqlite3MayAbort(Parse*); +@@ -13290,22 +19012,18 @@ SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int); + SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int); + SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*); + SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int); +-#if SELECTTRACE_ENABLED +-SQLITE_PRIVATE void sqlite3SelectSetName(Select*,const char*); +-#else +-# define sqlite3SelectSetName(A,B) +-#endif +-SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*); +-SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8); +-SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*); ++SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*); ++SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int); ++SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8); ++SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void); + SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void); +-SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void); ++SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*); + SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*); + SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*); + SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int); + + #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) +-SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int); ++SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int); + #endif + + #ifndef SQLITE_OMIT_TRIGGER +@@ -13321,15 +19039,20 @@ SQLITE_PRIVATE void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, i + SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int); + void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*); + SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*); +-SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*); +-SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*, +- Select*,u8); +-SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8); +-SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*); ++SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*, ++ const char*,const char*); ++SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*, ++ Select*,u8,Upsert*, ++ const char*,const char*); ++SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,ExprList*, Expr*, u8, ++ const char*,const char*); ++SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*, ++ const char*,const char*); + SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3*, Trigger*); + SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*); + SQLITE_PRIVATE u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int); + # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p)) ++# define sqlite3IsToplevel(p) ((p)->pToplevel==0) + #else + # define sqlite3TriggersExist(B,C,D,E,F) 0 + # define sqlite3DeleteTrigger(A,B) +@@ -13339,6 +19062,7 @@ SQLITE_PRIVATE u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Tab + # define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F) + # define sqlite3TriggerList(X, Y) 0 + # define sqlite3ParseToplevel(p) p ++# define sqlite3IsToplevel(p) 1 + # define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0 + #endif + +@@ -13368,7 +19092,9 @@ SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*); + SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8); + SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*); + SQLITE_PRIVATE int sqlite3Atoi(const char*); ++#ifndef SQLITE_OMIT_UTF16 + SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar); ++#endif + SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte); + SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**); + SQLITE_PRIVATE LogEst sqlite3LogEst(u64); +@@ -13376,7 +19102,14 @@ SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst); + #ifndef SQLITE_OMIT_VIRTUALTABLE + SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double); + #endif ++#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \ ++ defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \ ++ defined(SQLITE_EXPLAIN_ESTIMATED_ROWS) + SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst); ++#endif ++SQLITE_PRIVATE VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int); ++SQLITE_PRIVATE const char *sqlite3VListNumToName(VList*,int); ++SQLITE_PRIVATE int sqlite3VListNameToNum(VList*,const char*,int); + + /* + ** Routines to read and write variable-length integers. These used to +@@ -13402,15 +19135,17 @@ SQLITE_PRIVATE int sqlite3VarintLen(u64 v); + #define putVarint sqlite3PutVarint + + +-SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *); ++SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*); + SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int); + SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2); + SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity); ++SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table*,int); + SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr); + SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8); + SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*); + SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...); + SQLITE_PRIVATE void sqlite3Error(sqlite3*,int); ++SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int); + SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n); + SQLITE_PRIVATE u8 sqlite3HexToInt(int h); + SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **); +@@ -13419,15 +19154,23 @@ SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **); + SQLITE_PRIVATE const char *sqlite3ErrName(int); + #endif + ++#ifdef SQLITE_ENABLE_DESERIALIZE ++SQLITE_PRIVATE int sqlite3MemdbInit(void); ++#endif ++ + SQLITE_PRIVATE const char *sqlite3ErrStr(int); + SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse); + SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int); ++SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq*); + SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName); + SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr); ++SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr); ++SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,Expr*,Expr*); + SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int); + SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*); + SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*); + SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *); ++SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3*); + SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *); + SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int); + SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64); +@@ -13443,46 +19186,60 @@ SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,u8); + + SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8); + SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8); +-SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, ++SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, + void(*)(void*)); + SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*); + SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*); + SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *); ++#ifndef SQLITE_OMIT_UTF16 + SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8); ++#endif + SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **); + SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8); + #ifndef SQLITE_AMALGAMATION + SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[]; ++SQLITE_PRIVATE const char sqlite3StrBINARY[]; + SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[]; + SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[]; + SQLITE_PRIVATE const Token sqlite3IntTokens[]; + SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config; +-SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions; ++SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions; + #ifndef SQLITE_OMIT_WSD + SQLITE_PRIVATE int sqlite3PendingByte; + #endif + #endif ++#ifdef VDBE_PROFILE ++SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt; ++#endif + SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int); + SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*); + SQLITE_PRIVATE void sqlite3AlterFunctions(void); + SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*); ++SQLITE_PRIVATE void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*); + SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *); + SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...); +-SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*); +-SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int); ++SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int); ++SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int); ++SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr*); + SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*); ++SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p); + SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*); + SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*); ++SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*); + SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*); +-SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*); ++SQLITE_PRIVATE int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*); + SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*); + SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int); + SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *); + SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *); ++SQLITE_PRIVATE void *sqlite3RenameTokenMap(Parse*, void*, Token*); ++SQLITE_PRIVATE void sqlite3RenameTokenRemap(Parse*, void *pTo, void *pFrom); ++SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse*, Expr*); ++SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse*, ExprList*); + SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*); +-SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*); ++SQLITE_PRIVATE char sqlite3AffinityType(const char*, Column*); + SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*); +-SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*); ++SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*); + SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*); + SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *); + SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB); +@@ -13490,7 +19247,6 @@ SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*); + SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*); + SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int); + SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*); +-SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int); + SQLITE_PRIVATE void sqlite3SchemaClear(void *); + SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *); + SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *); +@@ -13498,43 +19254,58 @@ SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int); + SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*); + SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo*); + SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*); ++SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int); ++ + #ifdef SQLITE_DEBUG + SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*); + #endif +-SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, ++SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, + void (*)(sqlite3_context*,int,sqlite3_value **), +- void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*), ++ void (*)(sqlite3_context*,int,sqlite3_value **), ++ void (*)(sqlite3_context*), ++ void (*)(sqlite3_context*), ++ void (*)(sqlite3_context*,int,sqlite3_value **), + FuncDestructor *pDestructor + ); ++SQLITE_PRIVATE void sqlite3NoopDestructor(void*); ++SQLITE_PRIVATE void sqlite3OomFault(sqlite3*); ++SQLITE_PRIVATE void sqlite3OomClear(sqlite3*); + SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int); + SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *); + + SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int); +-SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int); +-SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*); +-SQLITE_PRIVATE void sqlite3AppendChar(StrAccum*,int,char); + SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*); +-SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*); + SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int); + SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int); + + SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *); + SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *); + ++#ifndef SQLITE_OMIT_SUBQUERY ++SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse*, Expr*); ++#else ++# define sqlite3ExprCheckIN(x,y) SQLITE_OK ++#endif ++ + #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void); +-SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*); ++SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue( ++ Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*); + SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**); + SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*); + SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**); ++SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3*, Index*, int); + #endif + + /* + ** The interface to the LEMON-generated parser + */ +-SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64)); +-SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*)); +-SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*); ++#ifndef SQLITE_AMALGAMATION ++SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64), Parse*); ++SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*)); ++#endif ++SQLITE_PRIVATE void sqlite3Parser(void*, int, Token); ++SQLITE_PRIVATE int sqlite3ParserFallback(int); + #ifdef YYTRACKMAXSTACKDEPTH + SQLITE_PRIVATE int sqlite3ParserStackPeak(void*); + #endif +@@ -13562,7 +19333,7 @@ SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char*); + # define sqlite3VtabRollback(X) + # define sqlite3VtabCommit(X) + # define sqlite3VtabInSync(db) 0 +-# define sqlite3VtabLock(X) ++# define sqlite3VtabLock(X) + # define sqlite3VtabUnlock(X) + # define sqlite3VtabUnlockList(X) + # define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK +@@ -13579,8 +19350,17 @@ SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*); + SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int); + SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*); + SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*); ++SQLITE_PRIVATE Module *sqlite3VtabCreateModule( ++ sqlite3*, ++ const char*, ++ const sqlite3_module*, ++ void*, ++ void(*)(void*) ++ ); + # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0) + #endif ++SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse*,Module*); ++SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3*,Module*); + SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*); + SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int); + SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*); +@@ -13591,11 +19371,13 @@ SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*); + SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *); + SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *); + SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*); +-SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**); + SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*); + SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int); + SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *); + SQLITE_PRIVATE void sqlite3ParserReset(Parse*); ++#ifdef SQLITE_ENABLE_NORMALIZE ++SQLITE_PRIVATE char *sqlite3Normalize(Vdbe*, const char*); ++#endif + SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*); + SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*); + SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *); +@@ -13613,12 +19395,24 @@ SQLITE_PRIVATE void sqlite3WithPush(Parse*, With*, u8); + #define sqlite3WithPush(x,y,z) + #define sqlite3WithDelete(x,y) + #endif ++#ifndef SQLITE_OMIT_UPSERT ++SQLITE_PRIVATE Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*); ++SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3*,Upsert*); ++SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3*,Upsert*); ++SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*); ++SQLITE_PRIVATE void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int); ++#else ++#define sqlite3UpsertNew(v,w,x,y,z) ((Upsert*)0) ++#define sqlite3UpsertDelete(x,y) ++#define sqlite3UpsertDup(x,y) ((Upsert*)0) ++#endif ++ + + /* Declarations for functions in fkey.c. All of these are replaced by + ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign + ** key functionality is available. If OMIT_TRIGGER is defined but + ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In +-** this case foreign keys are parsed, but no other functionality is ++** this case foreign keys are parsed, but no other functionality is + ** provided (enforcement of FK constraints requires the triggers sub-system). + */ + #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) +@@ -13634,6 +19428,7 @@ SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *); + #define sqlite3FkDropTable(a,b,c) + #define sqlite3FkOldmask(a,b) 0 + #define sqlite3FkRequired(a,b,c,d) 0 ++ #define sqlite3FkReferences(a) 0 + #endif + #ifndef SQLITE_OMIT_FOREIGN_KEY + SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *, Table*); +@@ -13652,10 +19447,10 @@ SQLITE_PRIVATE int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**); + + /* + ** The interface to the code in fault.c used for identifying "benign" +-** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST ++** malloc failures. This is only present if SQLITE_UNTESTABLE + ** is not defined. + */ +-#ifndef SQLITE_OMIT_BUILTIN_TEST ++#ifndef SQLITE_UNTESTABLE + SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void); + SQLITE_PRIVATE void sqlite3EndBenignMalloc(void); + #else +@@ -13677,21 +19472,17 @@ SQLITE_PRIVATE void sqlite3EndBenignMalloc(void); + #define IN_INDEX_NOOP_OK 0x0001 /* OK to return IN_INDEX_NOOP */ + #define IN_INDEX_MEMBERSHIP 0x0002 /* IN operator used for membership test */ + #define IN_INDEX_LOOP 0x0004 /* IN operator used as a loop */ +-SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*); ++SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*); + +-#ifdef SQLITE_ENABLE_ATOMIC_WRITE +-SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int); +-SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *); ++SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int); ++SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *); ++#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \ ++ || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) + SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *); +-SQLITE_PRIVATE int sqlite3JournalExists(sqlite3_file *p); +-#else +- #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile) +- #define sqlite3JournalExists(p) 1 + #endif + ++SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p); + SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *); +-SQLITE_PRIVATE int sqlite3MemJournalSize(void); +-SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *); + + SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p); + #if SQLITE_MAX_EXPR_DEPTH>0 +@@ -13718,11 +19509,14 @@ SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db); + #ifdef SQLITE_DEBUG + SQLITE_PRIVATE void sqlite3ParserTrace(FILE*, char *); + #endif ++#if defined(YYCOVERAGE) ++SQLITE_PRIVATE int sqlite3ParserCoverage(FILE*); ++#endif + + /* + ** If the SQLITE_ENABLE IOTRACE exists then the global variable + ** sqlite3IoTrace is a pointer to a printf-like routine used to +-** print I/O tracing messages. ++** print I/O tracing messages. + */ + #ifdef SQLITE_ENABLE_IOTRACE + # define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; } +@@ -13756,7 +19550,7 @@ SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...); + ** that allocations that might have been satisfied by lookaside are not + ** passed back to non-lookaside free() routines. Asserts such as the + ** example above are placed on the non-lookaside free() routines to verify +-** this constraint. ++** this constraint. + ** + ** All of this is no-op for a production build. It only comes into + ** play when the SQLITE_MEMDEBUG compile-time option is used. +@@ -13772,8 +19566,7 @@ SQLITE_PRIVATE int sqlite3MemdebugNoType(void*,u8); + #endif + #define MEMTYPE_HEAP 0x01 /* General heap allocations */ + #define MEMTYPE_LOOKASIDE 0x02 /* Heap that might have been lookaside */ +-#define MEMTYPE_SCRATCH 0x04 /* Scratch allocations */ +-#define MEMTYPE_PCACHE 0x08 /* Page cache allocations */ ++#define MEMTYPE_PCACHE 0x04 /* Page cache allocations */ + + /* + ** Threading interface +@@ -13783,7 +19576,24 @@ SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*); + SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**); + #endif + +-#endif /* _SQLITEINT_H_ */ ++#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST) ++SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3*); ++#endif ++#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST) ++SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*); ++#endif ++ ++SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr); ++SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr); ++SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr*, int); ++SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(Parse*,Expr*,int); ++SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse*, Expr*); ++ ++#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS ++SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt); ++#endif ++ ++#endif /* SQLITEINT_H */ + + /************** End of sqliteInt.h *******************************************/ + /************** Begin file global.c ******************************************/ +@@ -13801,6 +19611,7 @@ SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**); + ** + ** This file contains definitions of global variables and constants. + */ ++/* #include "sqliteInt.h" */ + + /* An array to map all upper-case characters into their corresponding + ** lower-case character. +@@ -13858,6 +19669,7 @@ SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = { + ** isxdigit() 0x08 + ** toupper() 0x20 + ** SQLite identifier character 0x40 ++** Quote character 0x80 + ** + ** Bit 0x20 is set if the mapped character requires translation to upper + ** case. i.e. if the character is a lower-case ASCII character. +@@ -13866,16 +19678,13 @@ SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = { + ** + ** (x & ~(map[x]&0x20)) + ** +-** Standard function tolower() is implemented using the sqlite3UpperToLower[] ++** The equivalent of tolower() is implemented using the sqlite3UpperToLower[] + ** array. tolower() is used more often than toupper() by SQLite. + ** +-** Bit 0x40 is set if the character non-alphanumeric and can be used in an ++** Bit 0x40 is set if the character is non-alphanumeric and can be used in an + ** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any + ** non-ASCII UTF character. Hence the test for whether or not a character is + ** part of an identifier is 0x46. +-** +-** SQLite's versions are identical to the standard versions assuming a +-** locale of "C". They are implemented as macros in sqliteInt.h. + */ + #ifdef SQLITE_ASCII + SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = { +@@ -13883,7 +19692,7 @@ SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = { + 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, /* 08..0f ........ */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10..17 ........ */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 18..1f ........ */ +- 0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, /* 20..27 !"#$%&' */ ++ 0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80, /* 20..27 !"#$%&' */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */ + 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 30..37 01234567 */ + 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 38..3f 89:;<=>? */ +@@ -13891,8 +19700,8 @@ SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = { + 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02, /* 40..47 @ABCDEFG */ + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48..4f HIJKLMNO */ + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50..57 PQRSTUVW */ +- 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */ +- 0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */ ++ 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */ ++ 0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */ + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 68..6f hijklmno */ + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 70..77 pqrstuvw */ + 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, /* 78..7f xyz{|}~. */ +@@ -13927,9 +19736,16 @@ SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = { + ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally + ** disabled. The default value may be changed by compiling with the + ** SQLITE_USE_URI symbol defined. ++** ++** URI filenames are enabled by default if SQLITE_HAS_CODEC is ++** enabled. + */ + #ifndef SQLITE_USE_URI +-# define SQLITE_USE_URI 0 ++# ifdef SQLITE_HAS_CODEC ++# define SQLITE_USE_URI 1 ++# else ++# define SQLITE_USE_URI 0 ++# endif + #endif + + /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the +@@ -13947,6 +19763,38 @@ SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = { + # define SQLITE_SORTER_PMASZ 250 + #endif + ++/* Statement journals spill to disk when their size exceeds the following ++** threshold (in bytes). 0 means that statement journals are created and ++** written to disk immediately (the default behavior for SQLite versions ++** before 3.12.0). -1 means always keep the entire statement journal in ++** memory. (The statement journal is also always held entirely in memory ++** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this ++** setting.) ++*/ ++#ifndef SQLITE_STMTJRNL_SPILL ++# define SQLITE_STMTJRNL_SPILL (64*1024) ++#endif ++ ++/* ++** The default lookaside-configuration, the format "SZ,N". SZ is the ++** number of bytes in each lookaside slot (should be a multiple of 8) ++** and N is the number of slots. The lookaside-configuration can be ++** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE) ++** or at run-time for an individual database connection using ++** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE); ++*/ ++#ifndef SQLITE_DEFAULT_LOOKASIDE ++# define SQLITE_DEFAULT_LOOKASIDE 1200,100 ++#endif ++ ++ ++/* The default maximum size of an in-memory database created using ++** sqlite3_deserialize() ++*/ ++#ifndef SQLITE_MEMDB_DEFAULT_MAXSIZE ++# define SQLITE_MEMDB_DEFAULT_MAXSIZE 1073741824 ++#endif ++ + /* + ** The following singleton contains the global configuration for + ** the SQLite library. +@@ -13957,10 +19805,11 @@ SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = { + SQLITE_THREADSAFE==1, /* bFullMutex */ + SQLITE_USE_URI, /* bOpenUri */ + SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */ ++ 0, /* bSmallMalloc */ + 0x7ffffffe, /* mxStrlen */ + 0, /* neverCorrupt */ +- 128, /* szLookaside */ +- 500, /* nLookaside */ ++ SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */ ++ SQLITE_STMTJRNL_SPILL, /* nStmtSpill */ + {0,0,0,0,0,0,0,0}, /* m */ + {0,0,0,0,0,0,0,0,0}, /* mutex */ + {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */ +@@ -13969,12 +19818,9 @@ SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = { + 0, 0, /* mnHeap, mxHeap */ + SQLITE_DEFAULT_MMAP_SIZE, /* szMmap */ + SQLITE_MAX_MMAP_SIZE, /* mxMmap */ +- (void*)0, /* pScratch */ +- 0, /* szScratch */ +- 0, /* nScratch */ + (void*)0, /* pPage */ + 0, /* szPage */ +- 0, /* nPage */ ++ SQLITE_DEFAULT_PCACHE_INITSZ, /* nPage */ + 0, /* mxParserStack */ + 0, /* sharedCacheEnabled */ + SQLITE_SORTER_PMASZ, /* szPma */ +@@ -13996,10 +19842,16 @@ SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = { + 0, /* xVdbeBranch */ + 0, /* pVbeBranchArg */ + #endif +-#ifndef SQLITE_OMIT_BUILTIN_TEST ++#ifdef SQLITE_ENABLE_DESERIALIZE ++ SQLITE_MEMDB_DEFAULT_MAXSIZE, /* mxMemdbSize */ ++#endif ++#ifndef SQLITE_UNTESTABLE + 0, /* xTestCallback */ + #endif +- 0 /* bLocaltimeFault */ ++ 0, /* bLocaltimeFault */ ++ 0, /* bInternalFunctions */ ++ 0x7ffffffe, /* iOnceResetThreshold */ ++ SQLITE_DEFAULT_SORTERREF_SIZE, /* szSorterRef */ + }; + + /* +@@ -14007,7 +19859,7 @@ SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = { + ** database connections. After initialization, this table is + ** read-only. + */ +-SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions; ++SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions; + + /* + ** Constant tokens for values 0 and 1. +@@ -14017,12 +19869,19 @@ SQLITE_PRIVATE const Token sqlite3IntTokens[] = { + { "1", 1 } + }; + ++#ifdef VDBE_PROFILE ++/* ++** The following performance counter can be used in place of ++** sqlite3Hwtime() for profiling. This is a no-op on standard builds. ++*/ ++SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt = 0; ++#endif + + /* + ** The value of the "pending" byte must be 0x40000000 (1 byte past the + ** 1-gibabyte boundary) in a compatible database. SQLite never uses + ** the database page that contains the pending byte. It never attempts +-** to read or write that page. The pending byte page is set assign ++** to read or write that page. The pending byte page is set aside + ** for use by the VFS layers as space for managing file locks. + ** + ** During testing, it is often desirable to move the pending byte to +@@ -14040,6 +19899,7 @@ SQLITE_PRIVATE const Token sqlite3IntTokens[] = { + SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000; + #endif + ++/* #include "opcodes.h" */ + /* + ** Properties of opcodes. The OPFLG_INITIALIZER macro is + ** created by mkopcodeh.awk during compilation. Data is obtained +@@ -14048,439 +19908,12 @@ SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000; + */ + SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER; + +-/************** End of global.c **********************************************/ +-/************** Begin file ctime.c *******************************************/ + /* +-** 2010 February 23 +-** +-** The author disclaims copyright to this source code. In place of +-** a legal notice, here is a blessing: +-** +-** May you do good and not evil. +-** May you find forgiveness for yourself and forgive others. +-** May you share freely, never taking more than you give. +-** +-************************************************************************* +-** +-** This file implements routines used to report what compile-time options +-** SQLite was built with. ++** Name of the default collating sequence + */ ++SQLITE_PRIVATE const char sqlite3StrBINARY[] = "BINARY"; + +-#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS +- +- +-/* +-** An array of names of all compile-time options. This array should +-** be sorted A-Z. +-** +-** This array looks large, but in a typical installation actually uses +-** only a handful of compile-time options, so most times this array is usually +-** rather short and uses little memory space. +-*/ +-static const char * const azCompileOpt[] = { +- +-/* These macros are provided to "stringify" the value of the define +-** for those options in which the value is meaningful. */ +-#define CTIMEOPT_VAL_(opt) #opt +-#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) +- +-#if SQLITE_32BIT_ROWID +- "32BIT_ROWID", +-#endif +-#if SQLITE_4_BYTE_ALIGNED_MALLOC +- "4_BYTE_ALIGNED_MALLOC", +-#endif +-#if SQLITE_CASE_SENSITIVE_LIKE +- "CASE_SENSITIVE_LIKE", +-#endif +-#if SQLITE_CHECK_PAGES +- "CHECK_PAGES", +-#endif +-#if SQLITE_COVERAGE_TEST +- "COVERAGE_TEST", +-#endif +-#if SQLITE_DEBUG +- "DEBUG", +-#endif +-#if SQLITE_DEFAULT_LOCKING_MODE +- "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE), +-#endif +-#if defined(SQLITE_DEFAULT_MMAP_SIZE) && !defined(SQLITE_DEFAULT_MMAP_SIZE_xc) +- "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE), +-#endif +-#if SQLITE_DISABLE_DIRSYNC +- "DISABLE_DIRSYNC", +-#endif +-#if SQLITE_DISABLE_LFS +- "DISABLE_LFS", +-#endif +-#if SQLITE_ENABLE_API_ARMOR +- "ENABLE_API_ARMOR", +-#endif +-#if SQLITE_ENABLE_ATOMIC_WRITE +- "ENABLE_ATOMIC_WRITE", +-#endif +-#if SQLITE_ENABLE_CEROD +- "ENABLE_CEROD", +-#endif +-#if SQLITE_ENABLE_COLUMN_METADATA +- "ENABLE_COLUMN_METADATA", +-#endif +-#if SQLITE_ENABLE_DBSTAT_VTAB +- "ENABLE_DBSTAT_VTAB", +-#endif +-#if SQLITE_ENABLE_EXPENSIVE_ASSERT +- "ENABLE_EXPENSIVE_ASSERT", +-#endif +-#if SQLITE_ENABLE_FTS1 +- "ENABLE_FTS1", +-#endif +-#if SQLITE_ENABLE_FTS2 +- "ENABLE_FTS2", +-#endif +-#if SQLITE_ENABLE_FTS3 +- "ENABLE_FTS3", +-#endif +-#if SQLITE_ENABLE_FTS3_PARENTHESIS +- "ENABLE_FTS3_PARENTHESIS", +-#endif +-#if SQLITE_ENABLE_FTS4 +- "ENABLE_FTS4", +-#endif +-#if SQLITE_ENABLE_ICU +- "ENABLE_ICU", +-#endif +-#if SQLITE_ENABLE_IOTRACE +- "ENABLE_IOTRACE", +-#endif +-#if SQLITE_ENABLE_LOAD_EXTENSION +- "ENABLE_LOAD_EXTENSION", +-#endif +-#if SQLITE_ENABLE_LOCKING_STYLE +- "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE), +-#endif +-#if SQLITE_ENABLE_MEMORY_MANAGEMENT +- "ENABLE_MEMORY_MANAGEMENT", +-#endif +-#if SQLITE_ENABLE_MEMSYS3 +- "ENABLE_MEMSYS3", +-#endif +-#if SQLITE_ENABLE_MEMSYS5 +- "ENABLE_MEMSYS5", +-#endif +-#if SQLITE_ENABLE_OVERSIZE_CELL_CHECK +- "ENABLE_OVERSIZE_CELL_CHECK", +-#endif +-#if SQLITE_ENABLE_RTREE +- "ENABLE_RTREE", +-#endif +-#if defined(SQLITE_ENABLE_STAT4) +- "ENABLE_STAT4", +-#elif defined(SQLITE_ENABLE_STAT3) +- "ENABLE_STAT3", +-#endif +-#if SQLITE_ENABLE_UNLOCK_NOTIFY +- "ENABLE_UNLOCK_NOTIFY", +-#endif +-#if SQLITE_ENABLE_UPDATE_DELETE_LIMIT +- "ENABLE_UPDATE_DELETE_LIMIT", +-#endif +-#if SQLITE_HAS_CODEC +- "HAS_CODEC", +-#endif +-#if HAVE_ISNAN || SQLITE_HAVE_ISNAN +- "HAVE_ISNAN", +-#endif +-#if SQLITE_HOMEGROWN_RECURSIVE_MUTEX +- "HOMEGROWN_RECURSIVE_MUTEX", +-#endif +-#if SQLITE_IGNORE_AFP_LOCK_ERRORS +- "IGNORE_AFP_LOCK_ERRORS", +-#endif +-#if SQLITE_IGNORE_FLOCK_LOCK_ERRORS +- "IGNORE_FLOCK_LOCK_ERRORS", +-#endif +-#ifdef SQLITE_INT64_TYPE +- "INT64_TYPE", +-#endif +-#if SQLITE_LOCK_TRACE +- "LOCK_TRACE", +-#endif +-#if defined(SQLITE_MAX_MMAP_SIZE) && !defined(SQLITE_MAX_MMAP_SIZE_xc) +- "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE), +-#endif +-#ifdef SQLITE_MAX_SCHEMA_RETRY +- "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY), +-#endif +-#if SQLITE_MEMDEBUG +- "MEMDEBUG", +-#endif +-#if SQLITE_MIXED_ENDIAN_64BIT_FLOAT +- "MIXED_ENDIAN_64BIT_FLOAT", +-#endif +-#if SQLITE_NO_SYNC +- "NO_SYNC", +-#endif +-#if SQLITE_OMIT_ALTERTABLE +- "OMIT_ALTERTABLE", +-#endif +-#if SQLITE_OMIT_ANALYZE +- "OMIT_ANALYZE", +-#endif +-#if SQLITE_OMIT_ATTACH +- "OMIT_ATTACH", +-#endif +-#if SQLITE_OMIT_AUTHORIZATION +- "OMIT_AUTHORIZATION", +-#endif +-#if SQLITE_OMIT_AUTOINCREMENT +- "OMIT_AUTOINCREMENT", +-#endif +-#if SQLITE_OMIT_AUTOINIT +- "OMIT_AUTOINIT", +-#endif +-#if SQLITE_OMIT_AUTOMATIC_INDEX +- "OMIT_AUTOMATIC_INDEX", +-#endif +-#if SQLITE_OMIT_AUTORESET +- "OMIT_AUTORESET", +-#endif +-#if SQLITE_OMIT_AUTOVACUUM +- "OMIT_AUTOVACUUM", +-#endif +-#if SQLITE_OMIT_BETWEEN_OPTIMIZATION +- "OMIT_BETWEEN_OPTIMIZATION", +-#endif +-#if SQLITE_OMIT_BLOB_LITERAL +- "OMIT_BLOB_LITERAL", +-#endif +-#if SQLITE_OMIT_BTREECOUNT +- "OMIT_BTREECOUNT", +-#endif +-#if SQLITE_OMIT_BUILTIN_TEST +- "OMIT_BUILTIN_TEST", +-#endif +-#if SQLITE_OMIT_CAST +- "OMIT_CAST", +-#endif +-#if SQLITE_OMIT_CHECK +- "OMIT_CHECK", +-#endif +-#if SQLITE_OMIT_COMPLETE +- "OMIT_COMPLETE", +-#endif +-#if SQLITE_OMIT_COMPOUND_SELECT +- "OMIT_COMPOUND_SELECT", +-#endif +-#if SQLITE_OMIT_CTE +- "OMIT_CTE", +-#endif +-#if SQLITE_OMIT_DATETIME_FUNCS +- "OMIT_DATETIME_FUNCS", +-#endif +-#if SQLITE_OMIT_DECLTYPE +- "OMIT_DECLTYPE", +-#endif +-#if SQLITE_OMIT_DEPRECATED +- "OMIT_DEPRECATED", +-#endif +-#if SQLITE_OMIT_DISKIO +- "OMIT_DISKIO", +-#endif +-#if SQLITE_OMIT_EXPLAIN +- "OMIT_EXPLAIN", +-#endif +-#if SQLITE_OMIT_FLAG_PRAGMAS +- "OMIT_FLAG_PRAGMAS", +-#endif +-#if SQLITE_OMIT_FLOATING_POINT +- "OMIT_FLOATING_POINT", +-#endif +-#if SQLITE_OMIT_FOREIGN_KEY +- "OMIT_FOREIGN_KEY", +-#endif +-#if SQLITE_OMIT_GET_TABLE +- "OMIT_GET_TABLE", +-#endif +-#if SQLITE_OMIT_INCRBLOB +- "OMIT_INCRBLOB", +-#endif +-#if SQLITE_OMIT_INTEGRITY_CHECK +- "OMIT_INTEGRITY_CHECK", +-#endif +-#if SQLITE_OMIT_LIKE_OPTIMIZATION +- "OMIT_LIKE_OPTIMIZATION", +-#endif +-#if SQLITE_OMIT_LOAD_EXTENSION +- "OMIT_LOAD_EXTENSION", +-#endif +-#if SQLITE_OMIT_LOCALTIME +- "OMIT_LOCALTIME", +-#endif +-#if SQLITE_OMIT_LOOKASIDE +- "OMIT_LOOKASIDE", +-#endif +-#if SQLITE_OMIT_MEMORYDB +- "OMIT_MEMORYDB", +-#endif +-#if SQLITE_OMIT_OR_OPTIMIZATION +- "OMIT_OR_OPTIMIZATION", +-#endif +-#if SQLITE_OMIT_PAGER_PRAGMAS +- "OMIT_PAGER_PRAGMAS", +-#endif +-#if SQLITE_OMIT_PRAGMA +- "OMIT_PRAGMA", +-#endif +-#if SQLITE_OMIT_PROGRESS_CALLBACK +- "OMIT_PROGRESS_CALLBACK", +-#endif +-#if SQLITE_OMIT_QUICKBALANCE +- "OMIT_QUICKBALANCE", +-#endif +-#if SQLITE_OMIT_REINDEX +- "OMIT_REINDEX", +-#endif +-#if SQLITE_OMIT_SCHEMA_PRAGMAS +- "OMIT_SCHEMA_PRAGMAS", +-#endif +-#if SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS +- "OMIT_SCHEMA_VERSION_PRAGMAS", +-#endif +-#if SQLITE_OMIT_SHARED_CACHE +- "OMIT_SHARED_CACHE", +-#endif +-#if SQLITE_OMIT_SUBQUERY +- "OMIT_SUBQUERY", +-#endif +-#if SQLITE_OMIT_TCL_VARIABLE +- "OMIT_TCL_VARIABLE", +-#endif +-#if SQLITE_OMIT_TEMPDB +- "OMIT_TEMPDB", +-#endif +-#if SQLITE_OMIT_TRACE +- "OMIT_TRACE", +-#endif +-#if SQLITE_OMIT_TRIGGER +- "OMIT_TRIGGER", +-#endif +-#if SQLITE_OMIT_TRUNCATE_OPTIMIZATION +- "OMIT_TRUNCATE_OPTIMIZATION", +-#endif +-#if SQLITE_OMIT_UTF16 +- "OMIT_UTF16", +-#endif +-#if SQLITE_OMIT_VACUUM +- "OMIT_VACUUM", +-#endif +-#if SQLITE_OMIT_VIEW +- "OMIT_VIEW", +-#endif +-#if SQLITE_OMIT_VIRTUALTABLE +- "OMIT_VIRTUALTABLE", +-#endif +-#if SQLITE_OMIT_WAL +- "OMIT_WAL", +-#endif +-#if SQLITE_OMIT_WSD +- "OMIT_WSD", +-#endif +-#if SQLITE_OMIT_XFER_OPT +- "OMIT_XFER_OPT", +-#endif +-#if SQLITE_PERFORMANCE_TRACE +- "PERFORMANCE_TRACE", +-#endif +-#if SQLITE_PROXY_DEBUG +- "PROXY_DEBUG", +-#endif +-#if SQLITE_RTREE_INT_ONLY +- "RTREE_INT_ONLY", +-#endif +-#if SQLITE_SECURE_DELETE +- "SECURE_DELETE", +-#endif +-#if SQLITE_SMALL_STACK +- "SMALL_STACK", +-#endif +-#if SQLITE_SOUNDEX +- "SOUNDEX", +-#endif +-#if SQLITE_SYSTEM_MALLOC +- "SYSTEM_MALLOC", +-#endif +-#if SQLITE_TCL +- "TCL", +-#endif +-#if defined(SQLITE_TEMP_STORE) && !defined(SQLITE_TEMP_STORE_xc) +- "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE), +-#endif +-#if SQLITE_TEST +- "TEST", +-#endif +-#if defined(SQLITE_THREADSAFE) +- "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE), +-#endif +-#if SQLITE_USE_ALLOCA +- "USE_ALLOCA", +-#endif +-#if SQLITE_USER_AUTHENTICATION +- "USER_AUTHENTICATION", +-#endif +-#if SQLITE_WIN32_MALLOC +- "WIN32_MALLOC", +-#endif +-#if SQLITE_ZERO_MALLOC +- "ZERO_MALLOC" +-#endif +-}; +- +-/* +-** Given the name of a compile-time option, return true if that option +-** was used and false if not. +-** +-** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix +-** is not required for a match. +-*/ +-SQLITE_API int SQLITE_STDCALL sqlite3_compileoption_used(const char *zOptName){ +- int i, n; +- +-#if SQLITE_ENABLE_API_ARMOR +- if( zOptName==0 ){ +- (void)SQLITE_MISUSE_BKPT; +- return 0; +- } +-#endif +- if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7; +- n = sqlite3Strlen30(zOptName); +- +- /* Since ArraySize(azCompileOpt) is normally in single digits, a +- ** linear search is adequate. No need for a binary search. */ +- for(i=0; i =0 && N aDb[] (or -1) */ ++ u8 nullRow; /* True if pointing to a row with no data */ ++ u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */ ++ u8 isTable; /* True for rowid tables. False for indexes */ + #ifdef SQLITE_DEBUG +- u8 seekOp; /* Most recent seek operation on this cursor */ +-#endif +- i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */ +- u8 nullRow; /* True if pointing to a row with no data */ +- u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */ +- Bool isEphemeral:1; /* True for an ephemeral table */ +- Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */ +- Bool isTable:1; /* True if a table requiring integer keys */ +- Bool isOrdered:1; /* True if the underlying table is BTREE_UNORDERED */ +- Pgno pgnoRoot; /* Root page of the open btree cursor */ +- sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */ +- i64 seqCount; /* Sequence counter */ +- i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ +- VdbeSorter *pSorter; /* Sorter object for OP_SorterOpen cursors */ +- +- /* Cached information about the header for the data record that the +- ** cursor is currently pointing to. Only valid if cacheStatus matches ++ u8 seekOp; /* Most recent seek operation on this cursor */ ++ u8 wrFlag; /* The wrFlag argument to sqlite3BtreeCursor() */ ++#endif ++ Bool isEphemeral:1; /* True for an ephemeral table */ ++ Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */ ++ Bool isOrdered:1; /* True if the table is not BTREE_UNORDERED */ ++ Bool seekHit:1; /* See the OP_SeekHit and OP_IfNoHope opcodes */ ++ Btree *pBtx; /* Separate file holding temporary table */ ++ i64 seqCount; /* Sequence counter */ ++ int *aAltMap; /* Mapping from table to index column numbers */ ++ ++ /* Cached OP_Column parse information is only valid if cacheStatus matches + ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of +- ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that +- ** the cache is out of date. +- ** +- ** aRow might point to (ephemeral) data for the current row, or it might +- ** be NULL. +- */ +- u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */ +- u32 payloadSize; /* Total number of bytes in the record */ +- u32 szRow; /* Byte available in aRow */ +- u32 iHdrOffset; /* Offset to next unparsed byte of the header */ +- const u8 *aRow; /* Data for the current row, if all on one page */ +- u32 *aOffset; /* Pointer to aType[nField] */ +- u32 aType[1]; /* Type values for all entries in the record */ ++ ** CACHE_STALE (0) and so setting cacheStatus=CACHE_STALE guarantees that ++ ** the cache is out of date. */ ++ u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */ ++ int seekResult; /* Result of previous sqlite3BtreeMoveto() or 0 ++ ** if there have been no prior seeks on the cursor. */ ++ /* seekResult does not distinguish between "no seeks have ever occurred ++ ** on this cursor" and "the most recent seek was an exact match". ++ ** For CURTYPE_PSEUDO, seekResult is the register holding the record */ ++ ++ /* When a new VdbeCursor is allocated, only the fields above are zeroed. ++ ** The fields that follow are uninitialized, and must be individually ++ ** initialized prior to first use. */ ++ VdbeCursor *pAltCursor; /* Associated index cursor from which to read */ ++ union { ++ BtCursor *pCursor; /* CURTYPE_BTREE or _PSEUDO. Btree cursor */ ++ sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB. Vtab cursor */ ++ VdbeSorter *pSorter; /* CURTYPE_SORTER. Sorter object */ ++ } uc; ++ KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */ ++ u32 iHdrOffset; /* Offset to next unparsed byte of the header */ ++ Pgno pgnoRoot; /* Root page of the open btree cursor */ ++ i16 nField; /* Number of fields in the header */ ++ u16 nHdrParsed; /* Number of header fields parsed so far */ ++ i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ ++ u32 *aOffset; /* Pointer to aType[nField] */ ++ const u8 *aRow; /* Data for the current row, if all on one page */ ++ u32 payloadSize; /* Total number of bytes in the record */ ++ u32 szRow; /* Byte available in aRow */ ++#ifdef SQLITE_ENABLE_COLUMN_USED_MASK ++ u64 maskUsed; /* Mask of columns used by this cursor */ ++#endif ++ + /* 2*nField extra array elements allocated for aType[], beyond the one + ** static element declared in the structure. nField total array slots for + ** aType[] and nField+1 array slots for aOffset[] */ ++ u32 aType[1]; /* Type values record decode. MUST BE LAST */ + }; +-typedef struct VdbeCursor VdbeCursor; ++ ++ ++/* ++** A value for VdbeCursor.cacheStatus that means the cache is always invalid. ++*/ ++#define CACHE_STALE 0 + + /* + ** When a sub-program is executed (OP_Program), a structure of this type +@@ -14635,44 +20099,49 @@ struct VdbeFrame { + Op *aOp; /* Program instructions for parent frame */ + i64 *anExec; /* Event counters from parent frame */ + Mem *aMem; /* Array of memory cells for parent frame */ +- u8 *aOnceFlag; /* Array of OP_Once flags for parent frame */ + VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */ ++ u8 *aOnce; /* Bitmask used by OP_Once */ + void *token; /* Copy of SubProgram.token */ + i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */ ++ AuxData *pAuxData; /* Linked list of auxdata allocations */ ++#if SQLITE_DEBUG ++ u32 iFrameMagic; /* magic number for sanity checking */ ++#endif + int nCursor; /* Number of entries in apCsr */ + int pc; /* Program Counter in parent (calling) frame */ + int nOp; /* Size of aOp array */ + int nMem; /* Number of entries in aMem */ +- int nOnceFlag; /* Number of entries in aOnceFlag */ + int nChildMem; /* Number of memory cells for child frame */ + int nChildCsr; /* Number of cursors for child frame */ + int nChange; /* Statement changes (Vdbe.nChange) */ + int nDbChange; /* Value of db->nChange */ + }; + +-#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) ++/* Magic number for sanity checking on VdbeFrame objects */ ++#define SQLITE_FRAME_MAGIC 0x879fb71e + + /* +-** A value for VdbeCursor.cacheValid that means the cache is always invalid. ++** Return a pointer to the array of registers allocated for use ++** by a VdbeFrame. + */ +-#define CACHE_STALE 0 ++#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) + + /* + ** Internally, the vdbe manipulates nearly all SQL values as Mem + ** structures. Each Mem struct may cache multiple representations (string, + ** integer etc.) of the same value. + */ +-struct Mem { ++struct sqlite3_value { + union MemValue { + double r; /* Real value used when MEM_Real is set in flags */ + i64 i; /* Integer value used when MEM_Int is set in flags */ +- int nZero; /* Used when bit MEM_Zero is set in flags */ ++ int nZero; /* Extra zero bytes when MEM_Zero and MEM_Blob set */ ++ const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */ + FuncDef *pDef; /* Used only when flags==MEM_Agg */ +- RowSet *pRowSet; /* Used only when flags==MEM_RowSet */ +- VdbeFrame *pFrame; /* Used when flags==MEM_Frame */ + } u; + u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */ + u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */ ++ u8 eSubtype; /* Subtype for this value */ + int n; /* Number of characters in string value, excluding '\0' */ + char *z; /* String or BLOB value */ + /* ShallowCopy only needs to copy the information above */ +@@ -14683,15 +20152,22 @@ struct Mem { + void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */ + #ifdef SQLITE_DEBUG + Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */ +- void *pFiller; /* So that sizeof(Mem) is a multiple of 8 */ ++ u16 mScopyFlags; /* flags value immediately after the shallow copy */ + #endif + }; + ++/* ++** Size of struct Mem not including the Mem.zMalloc member or anything that ++** follows. ++*/ ++#define MEMCELLSIZE offsetof(Mem,zMalloc) ++ + /* One or more of the following flags are set to indicate the validOK + ** representations of the value stored in the Mem struct. + ** + ** If the MEM_Null flag is set, then the value is an SQL NULL value. +-** No other flags may be set in this case. ++** For a pointer type created using sqlite3_bind_pointer() or ++** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set. + ** + ** If the MEM_Str flag is set then Mem.z points at a string representation. + ** Usually this is encoded in the same unicode encoding as the main +@@ -14699,17 +20175,17 @@ struct Mem { + ** set, then the string is nul terminated. The MEM_Int and MEM_Real + ** flags may coexist with the MEM_Str flag. + */ +-#define MEM_Null 0x0001 /* Value is NULL */ ++#define MEM_Null 0x0001 /* Value is NULL (or a pointer) */ + #define MEM_Str 0x0002 /* Value is a string */ + #define MEM_Int 0x0004 /* Value is an integer */ + #define MEM_Real 0x0008 /* Value is a real number */ + #define MEM_Blob 0x0010 /* Value is a BLOB */ + #define MEM_AffMask 0x001f /* Mask of affinity bits */ +-#define MEM_RowSet 0x0020 /* Value is a RowSet object */ +-#define MEM_Frame 0x0040 /* Value is a VdbeFrame object */ ++#define MEM_FromBind 0x0020 /* Value originates from sqlite3_bind() */ ++/* Available 0x0040 */ + #define MEM_Undefined 0x0080 /* Value is undefined */ + #define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */ +-#define MEM_TypeMask 0x01ff /* Mask of type bits */ ++#define MEM_TypeMask 0xc1df /* Mask of type bits */ + + + /* Whenever Mem contains a valid string or blob representation, one of +@@ -14717,23 +20193,36 @@ struct Mem { + ** policy for Mem.z. The MEM_Term flag tells us whether or not the + ** string is \000 or \u0000 terminated + */ +-#define MEM_Term 0x0200 /* String rep is nul terminated */ ++#define MEM_Term 0x0200 /* String in Mem.z is zero terminated */ + #define MEM_Dyn 0x0400 /* Need to call Mem.xDel() on Mem.z */ + #define MEM_Static 0x0800 /* Mem.z points to a static string */ + #define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */ + #define MEM_Agg 0x2000 /* Mem.z points to an agg function context */ + #define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */ ++#define MEM_Subtype 0x8000 /* Mem.eSubtype is valid */ + #ifdef SQLITE_OMIT_INCRBLOB + #undef MEM_Zero + #define MEM_Zero 0x0000 + #endif + ++/* Return TRUE if Mem X contains dynamically allocated content - anything ++** that needs to be deallocated to avoid a leak. ++*/ ++#define VdbeMemDynamic(X) \ ++ (((X)->flags&(MEM_Agg|MEM_Dyn))!=0) ++ + /* + ** Clear any existing type flags from a Mem and replace them with f + */ + #define MemSetTypeFlag(p, f) \ + ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f) + ++/* ++** True if Mem X is a NULL-nochng type. ++*/ ++#define MemNullNochng(X) \ ++ ((X)->flags==(MEM_Null|MEM_Zero) && (X)->n==0 && (X)->u.nZero==0) ++ + /* + ** Return true if a memory cell is not marked as invalid. This macro + ** is for use inside assert() statements only. +@@ -14750,11 +20239,11 @@ struct Mem { + ** when the VM is halted (if not before). + */ + struct AuxData { +- int iOp; /* Instruction number of OP_Function opcode */ +- int iArg; /* Index of function argument. */ ++ int iAuxOp; /* Instruction number of OP_Function opcode */ ++ int iAuxArg; /* Index of function argument. */ + void *pAux; /* Aux data pointer */ +- void (*xDelete)(void *); /* Destructor for the aux data */ +- AuxData *pNext; /* Next element in list */ ++ void (*xDeleteAux)(void*); /* Destructor for the aux data */ ++ AuxData *pNextAux; /* Next element in list */ + }; + + /* +@@ -14771,26 +20260,15 @@ struct AuxData { + ** (Mem) which are only defined there. + */ + struct sqlite3_context { +- Mem *pOut; /* The return value is stored here */ +- FuncDef *pFunc; /* Pointer to function information */ +- Mem *pMem; /* Memory cell used to store aggregate context */ +- Vdbe *pVdbe; /* The VM that owns this context */ +- int iOp; /* Instruction number of OP_Function */ +- int isError; /* Error code returned by the function. */ +- u8 skipFlag; /* Skip accumulator loading if true */ +- u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */ +-}; +- +-/* +-** An Explain object accumulates indented output which is helpful +-** in describing recursive data structures. +-*/ +-struct Explain { +- Vdbe *pVdbe; /* Attach the explanation to this Vdbe */ +- StrAccum str; /* The string being accumulated */ +- int nIndent; /* Number of elements in aIndent */ +- u16 aIndent[100]; /* Levels of indentation */ +- char zBase[100]; /* Initial space */ ++ Mem *pOut; /* The return value is stored here */ ++ FuncDef *pFunc; /* Pointer to function information */ ++ Mem *pMem; /* Memory cell used to store aggregate context */ ++ Vdbe *pVdbe; /* The VM that owns this context */ ++ int iOp; /* Instruction number of OP_Function */ ++ int isError; /* Error code returned by the function. */ ++ u8 skipFlag; /* Skip accumulator loading if true */ ++ u8 argc; /* Number of arguments */ ++ sqlite3_value *argv[1]; /* Argument set */ + }; + + /* A bitfield type for use inside of structures. Always follow with :N where +@@ -14798,6 +20276,9 @@ struct Explain { + */ + typedef unsigned bft; /* Bit Field Type */ + ++/* The ScanStatus object holds a single value for the ++** sqlite3_stmt_scanstatus() interface. ++*/ + typedef struct ScanStatus ScanStatus; + struct ScanStatus { + int addrExplain; /* OP_Explain for loop */ +@@ -14808,6 +20289,19 @@ struct ScanStatus { + char *zName; /* Name of table or index */ + }; + ++/* The DblquoteStr object holds the text of a double-quoted ++** string for a prepared statement. A linked list of these objects ++** is constructed during statement parsing and is held on Vdbe.pDblStr. ++** When computing a normalized SQL statement for an SQL statement, that ++** list is consulted for each double-quoted identifier to see if the ++** identifier should really be a string literal. ++*/ ++typedef struct DblquoteStr DblquoteStr; ++struct DblquoteStr { ++ DblquoteStr *pNextStr; /* Next string literal in the list */ ++ char z[8]; /* Dequoted value for the string */ ++}; ++ + /* + ** An instance of the virtual machine. This structure contains the complete + ** state of the virtual machine. +@@ -14817,62 +20311,69 @@ struct ScanStatus { + */ + struct Vdbe { + sqlite3 *db; /* The database connection that owns this statement */ +- Op *aOp; /* Space to hold the virtual machine's program */ +- Mem *aMem; /* The memory locations */ +- Mem **apArg; /* Arguments to currently executing user function */ +- Mem *aColName; /* Column names to return */ +- Mem *pResultSet; /* Pointer to an array of results */ ++ Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */ + Parse *pParse; /* Parsing context used to create this Vdbe */ ++ ynVar nVar; /* Number of entries in aVar[] */ ++ u32 magic; /* Magic number for sanity checking */ + int nMem; /* Number of memory locations currently allocated */ +- int nOp; /* Number of instructions in the program */ + int nCursor; /* Number of slots in apCsr[] */ +- u32 magic; /* Magic number for sanity checking */ +- char *zErrMsg; /* Error message written here */ +- Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */ +- VdbeCursor **apCsr; /* One element of this array for each open cursor */ +- Mem *aVar; /* Values for the OP_Variable opcode. */ +- char **azVar; /* Name of variables */ +- ynVar nVar; /* Number of entries in aVar[] */ +- ynVar nzVar; /* Number of entries in azVar[] */ + u32 cacheCtr; /* VdbeCursor row cache generation counter */ + int pc; /* The program counter */ + int rc; /* Value to return */ ++ int nChange; /* Number of db changes made since last reset */ ++ int iStatement; /* Statement number (or 0 if has no opened stmt) */ ++ i64 iCurrentTime; /* Value of julianday('now') for this statement */ ++ i64 nFkConstraint; /* Number of imm. FK constraints this VM */ ++ i64 nStmtDefCons; /* Number of def. constraints when stmt started */ ++ i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */ ++ Mem *aMem; /* The memory locations */ ++ Mem **apArg; /* Arguments to currently executing user function */ ++ VdbeCursor **apCsr; /* One element of this array for each open cursor */ ++ Mem *aVar; /* Values for the OP_Variable opcode. */ ++ ++ /* When allocating a new Vdbe object, all of the fields below should be ++ ** initialized to zero or NULL */ ++ ++ Op *aOp; /* Space to hold the virtual machine's program */ ++ int nOp; /* Number of instructions in the program */ ++ int nOpAlloc; /* Slots allocated for aOp[] */ ++ Mem *aColName; /* Column names to return */ ++ Mem *pResultSet; /* Pointer to an array of results */ ++ char *zErrMsg; /* Error message written here */ ++ VList *pVList; /* Name of variables */ ++#ifndef SQLITE_OMIT_TRACE ++ i64 startTime; /* Time when query started - used for profiling */ ++#endif + #ifdef SQLITE_DEBUG + int rcApp; /* errcode set by sqlite3_result_error_code() */ ++ u32 nWrite; /* Number of write operations that have occurred */ + #endif + u16 nResColumn; /* Number of columns in one row of the result set */ + u8 errorAction; /* Recovery action to do in case of an error */ + u8 minWriteFileFormat; /* Minimum file format for writable database files */ ++ u8 prepFlags; /* SQLITE_PREPARE_* flags */ ++ bft expired:2; /* 1: recompile VM immediately 2: when convenient */ + bft explain:2; /* True if EXPLAIN present on SQL command */ ++ bft doingRerun:1; /* True if rerunning after an auto-reprepare */ + bft changeCntOn:1; /* True to update the change-counter */ +- bft expired:1; /* True if the VM needs to be recompiled */ + bft runOnlyOnce:1; /* Automatically expire on reset */ + bft usesStmtJournal:1; /* True if uses a statement journal */ + bft readOnly:1; /* True for statements that do not write */ + bft bIsReader:1; /* True for statements that read */ +- bft isPrepareV2:1; /* True if prepared with prepare_v2() */ +- bft doingRerun:1; /* True if rerunning after an auto-reprepare */ +- int nChange; /* Number of db changes made since last reset */ + yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */ + yDbMask lockMask; /* Subset of btreeMask that requires a lock */ +- int iStatement; /* Statement number (or 0 if has not opened stmt) */ +- u32 aCounter[5]; /* Counters used by sqlite3_stmt_status() */ +-#ifndef SQLITE_OMIT_TRACE +- i64 startTime; /* Time when query started - used for profiling */ +-#endif +- i64 iCurrentTime; /* Value of julianday('now') for this statement */ +- i64 nFkConstraint; /* Number of imm. FK constraints this VM */ +- i64 nStmtDefCons; /* Number of def. constraints when stmt started */ +- i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */ ++ u32 aCounter[7]; /* Counters used by sqlite3_stmt_status() */ + char *zSql; /* Text of the SQL statement that generated this */ ++#ifdef SQLITE_ENABLE_NORMALIZE ++ char *zNormSql; /* Normalization of the associated SQL statement */ ++ DblquoteStr *pDblStr; /* List of double-quoted string literals */ ++#endif + void *pFree; /* Free this when deleting the vdbe */ + VdbeFrame *pFrame; /* Parent frame */ + VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */ + int nFrame; /* Number of frames in pFrame list */ + u32 expmask; /* Binding to these vars invalidates VM */ + SubProgram *pProgram; /* Linked list of all sub-programs used by VM */ +- int nOnceFlag; /* Size of array aOnceFlag[] */ +- u8 *aOnceFlag; /* Flags for OP_Once */ + AuxData *pAuxData; /* Linked list of auxdata allocations */ + #ifdef SQLITE_ENABLE_STMT_SCANSTATUS + i64 *anExec; /* Number of times each op has been executed */ +@@ -14884,32 +20385,54 @@ struct Vdbe { + /* + ** The following are allowed values for Vdbe.magic + */ +-#define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */ +-#define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */ +-#define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */ +-#define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */ ++#define VDBE_MAGIC_INIT 0x16bceaa5 /* Building a VDBE program */ ++#define VDBE_MAGIC_RUN 0x2df20da3 /* VDBE is ready to execute */ ++#define VDBE_MAGIC_HALT 0x319c2973 /* VDBE has completed execution */ ++#define VDBE_MAGIC_RESET 0x48fa9f76 /* Reset and ready to run again */ ++#define VDBE_MAGIC_DEAD 0x5606c3c8 /* The VDBE has been deallocated */ ++ ++/* ++** Structure used to store the context required by the ++** sqlite3_preupdate_*() API functions. ++*/ ++struct PreUpdate { ++ Vdbe *v; ++ VdbeCursor *pCsr; /* Cursor to read old values from */ ++ int op; /* One of SQLITE_INSERT, UPDATE, DELETE */ ++ u8 *aRecord; /* old.* database record */ ++ KeyInfo keyinfo; ++ UnpackedRecord *pUnpacked; /* Unpacked version of aRecord[] */ ++ UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */ ++ int iNewReg; /* Register for new.* values */ ++ i64 iKey1; /* First key value passed to hook */ ++ i64 iKey2; /* Second key value passed to hook */ ++ Mem *aNew; /* Array of new.* values */ ++ Table *pTab; /* Schema object being upated */ ++ Index *pPk; /* PK index if pTab is WITHOUT ROWID */ ++}; + + /* + ** Function prototypes + */ ++SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...); + SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*); + void sqliteVdbePopStack(Vdbe*,int); +-SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*); ++SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor**, int*); + SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*); +-#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) +-SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*); +-#endif + SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32); +-SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int); ++SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8); ++SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int, u32*); + SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32); + SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*); +-SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int); ++SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int); + + int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *); + SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*); + SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*); + SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*); ++#ifndef SQLITE_OMIT_EXPLAIN + SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*); ++#endif + SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*); + SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int); + SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*); +@@ -14924,46 +20447,73 @@ SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64); + #else + SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem*, double); + #endif ++SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(Mem*, void*, const char*, void(*)(void*)); + SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem*,sqlite3*,u16); + SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*); + SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int); +-SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*); ++#ifdef SQLITE_DEBUG ++SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem*); ++#endif ++SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem*); + SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*); + SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8); + SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*); + SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*); + SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*); ++SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull); + SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*); + SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*); + SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*); + SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8); +-SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*); ++SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*); + SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p); +-#define VdbeMemDynamic(X) \ +- (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0) + SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*); ++#ifndef SQLITE_OMIT_WINDOWFUNC ++SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*); ++#endif ++#ifndef SQLITE_OMIT_EXPLAIN + SQLITE_PRIVATE const char *sqlite3OpcodeName(int); ++#endif + SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); + SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n); + SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int); +-SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); ++#ifdef SQLITE_DEBUG ++SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame*); ++#endif ++SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void*); /* Destructor on Mem */ ++SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); /* Actually deletes the Frame */ + SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *); ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK ++SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int); ++#endif + SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p); + + SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *); + SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *); + SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *); + SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *); +-SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *); ++SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *); + SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *); + SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *); + SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *); + +-#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 ++#ifdef SQLITE_DEBUG ++SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*); ++SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe*); ++#else ++# define sqlite3VdbeIncrWriteCounter(V,C) ++# define sqlite3VdbeAssertAbortable(V) ++#endif ++ ++#if !defined(SQLITE_OMIT_SHARED_CACHE) + SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*); +-SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*); + #else + # define sqlite3VdbeEnter(X) ++#endif ++ ++#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 ++SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*); ++#else + # define sqlite3VdbeLeave(X) + #endif + +@@ -14978,12 +20528,14 @@ SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int); + # define sqlite3VdbeCheckFk(p,i) 0 + #endif + +-SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8); + #ifdef SQLITE_DEBUG + SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe*); + SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf); + #endif +-SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem); ++#ifndef SQLITE_OMIT_UTF16 ++SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8); ++SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem); ++#endif + + #ifndef SQLITE_OMIT_INCRBLOB + SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *); +@@ -14993,7 +20545,7 @@ SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *); + #define ExpandBlob(P) SQLITE_OK + #endif + +-#endif /* !defined(_VDBEINT_H_) */ ++#endif /* !defined(SQLITE_VDBEINT_H) */ + + /************** End of vdbeInt.h *********************************************/ + /************** Continuing where we left off in status.c *********************/ +@@ -15001,15 +20553,15 @@ SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *); + /* + ** Variables in which to record status information. + */ +-typedef struct sqlite3StatType sqlite3StatType; +-static SQLITE_WSD struct sqlite3StatType { + #if SQLITE_PTRSIZE>4 +- sqlite3_int64 nowValue[10]; /* Current value */ +- sqlite3_int64 mxValue[10]; /* Maximum value */ ++typedef sqlite3_int64 sqlite3StatValueType; + #else +- u32 nowValue[10]; /* Current value */ +- u32 mxValue[10]; /* Maximum value */ ++typedef u32 sqlite3StatValueType; + #endif ++typedef struct sqlite3StatType sqlite3StatType; ++static SQLITE_WSD struct sqlite3StatType { ++ sqlite3StatValueType nowValue[10]; /* Current value */ ++ sqlite3StatValueType mxValue[10]; /* Maximum value */ + } sqlite3Stat = { {0,}, {0,} }; + + /* +@@ -15090,25 +20642,30 @@ SQLITE_PRIVATE void sqlite3StatusDown(int op, int N){ + } + + /* +-** Set the value of a status to X. The highwater mark is adjusted if +-** necessary. The caller must hold the appropriate mutex. ++** Adjust the highwater mark if necessary. ++** The caller must hold the appropriate mutex. + */ +-SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){ ++SQLITE_PRIVATE void sqlite3StatusHighwater(int op, int X){ ++ sqlite3StatValueType newValue; + wsdStatInit; ++ assert( X>=0 ); ++ newValue = (sqlite3StatValueType)X; + assert( op>=0 && op =0 && op wsdStat.mxValue[op] ){ +- wsdStat.mxValue[op] = wsdStat.nowValue[op]; ++ assert( op==SQLITE_STATUS_MALLOC_SIZE ++ || op==SQLITE_STATUS_PAGECACHE_SIZE ++ || op==SQLITE_STATUS_PARSER_STACK ); ++ if( newValue>wsdStat.mxValue[op] ){ ++ wsdStat.mxValue[op] = newValue; + } + } + + /* + ** Query status information. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_status64( ++SQLITE_API int sqlite3_status64( + int op, + sqlite3_int64 *pCurrent, + sqlite3_int64 *pHighwater, +@@ -15133,8 +20690,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status64( + (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */ + return SQLITE_OK; + } +-SQLITE_API int SQLITE_STDCALL sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ +- sqlite3_int64 iCur, iHwtr; ++SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ ++ sqlite3_int64 iCur = 0, iHwtr = 0; + int rc; + #ifdef SQLITE_ENABLE_API_ARMOR + if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT; +@@ -15147,10 +20704,32 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status(int op, int *pCurrent, int *pHighwa + return rc; + } + ++/* ++** Return the number of LookasideSlot elements on the linked list ++*/ ++static u32 countLookasideSlots(LookasideSlot *p){ ++ u32 cnt = 0; ++ while( p ){ ++ p = p->pNext; ++ cnt++; ++ } ++ return cnt; ++} ++ ++/* ++** Count the number of slots of lookaside memory that are outstanding ++*/ ++SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){ ++ u32 nInit = countLookasideSlots(db->lookaside.pInit); ++ u32 nFree = countLookasideSlots(db->lookaside.pFree); ++ if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit; ++ return db->lookaside.nSlot - (nInit+nFree); ++} ++ + /* + ** Query status information for a single database connection + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_db_status( ++SQLITE_API int sqlite3_db_status( + sqlite3 *db, /* The database connection whose status is desired */ + int op, /* Status verb */ + int *pCurrent, /* Write current value here */ +@@ -15166,10 +20745,15 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status( + sqlite3_mutex_enter(db->mutex); + switch( op ){ + case SQLITE_DBSTATUS_LOOKASIDE_USED: { +- *pCurrent = db->lookaside.nOut; +- *pHighwater = db->lookaside.mxOut; ++ *pCurrent = sqlite3LookasideUsed(db, pHighwater); + if( resetFlag ){ +- db->lookaside.mxOut = db->lookaside.nOut; ++ LookasideSlot *p = db->lookaside.pFree; ++ if( p ){ ++ while( p->pNext ) p = p->pNext; ++ p->pNext = db->lookaside.pInit; ++ db->lookaside.pInit = db->lookaside.pFree; ++ db->lookaside.pFree = 0; ++ } + } + break; + } +@@ -15195,6 +20779,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status( + ** by all pagers associated with the given database connection. The + ** highwater mark is meaningless and is returned as zero. + */ ++ case SQLITE_DBSTATUS_CACHE_USED_SHARED: + case SQLITE_DBSTATUS_CACHE_USED: { + int totalUsed = 0; + int i; +@@ -15203,7 +20788,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status( + Btree *pBt = db->aDb[i].pBt; + if( pBt ){ + Pager *pPager = sqlite3BtreePager(pBt); +- totalUsed += sqlite3PagerMemUsed(pPager); ++ int nByte = sqlite3PagerMemUsed(pPager); ++ if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED ){ ++ nByte = nByte / sqlite3BtreeConnectionCount(pBt); ++ } ++ totalUsed += nByte; + } + } + sqlite3BtreeLeaveAll(db); +@@ -15234,10 +20823,10 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status( + + pSchema->idxHash.count + + pSchema->fkeyHash.count + ); +- nByte += sqlite3MallocSize(pSchema->tblHash.ht); +- nByte += sqlite3MallocSize(pSchema->trigHash.ht); +- nByte += sqlite3MallocSize(pSchema->idxHash.ht); +- nByte += sqlite3MallocSize(pSchema->fkeyHash.ht); ++ nByte += sqlite3_msize(pSchema->tblHash.ht); ++ nByte += sqlite3_msize(pSchema->trigHash.ht); ++ nByte += sqlite3_msize(pSchema->idxHash.ht); ++ nByte += sqlite3_msize(pSchema->fkeyHash.ht); + + for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ + sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p)); +@@ -15282,6 +20871,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status( + ** pagers the database handle is connected to. *pHighwater is always set + ** to zero. + */ ++ case SQLITE_DBSTATUS_CACHE_SPILL: ++ op = SQLITE_DBSTATUS_CACHE_WRITE+1; ++ /* Fall through into the next case */ + case SQLITE_DBSTATUS_CACHE_HIT: + case SQLITE_DBSTATUS_CACHE_MISS: + case SQLITE_DBSTATUS_CACHE_WRITE:{ +@@ -15364,63 +20956,91 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status( + ** + ** Jean Meeus + ** Astronomical Algorithms, 2nd Edition, 1998 +-** ISBM 0-943396-61-1 ++** ISBN 0-943396-61-1 + ** Willmann-Bell, Inc + ** Richmond, Virginia (USA) + */ ++/* #include "sqliteInt.h" */ + /* #include */ + /* #include */ + #include + + #ifndef SQLITE_OMIT_DATETIME_FUNCS + ++/* ++** The MSVC CRT on Windows CE may not have a localtime() function. ++** So declare a substitute. The substitute function itself is ++** defined in "os_win.c". ++*/ ++#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \ ++ (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API) ++struct tm *__cdecl localtime(const time_t *); ++#endif + + /* + ** A structure for holding a single date and time. + */ + typedef struct DateTime DateTime; + struct DateTime { +- sqlite3_int64 iJD; /* The julian day number times 86400000 */ +- int Y, M, D; /* Year, month, and day */ +- int h, m; /* Hour and minutes */ +- int tz; /* Timezone offset in minutes */ +- double s; /* Seconds */ +- char validYMD; /* True (1) if Y,M,D are valid */ +- char validHMS; /* True (1) if h,m,s are valid */ +- char validJD; /* True (1) if iJD is valid */ +- char validTZ; /* True (1) if tz is valid */ ++ sqlite3_int64 iJD; /* The julian day number times 86400000 */ ++ int Y, M, D; /* Year, month, and day */ ++ int h, m; /* Hour and minutes */ ++ int tz; /* Timezone offset in minutes */ ++ double s; /* Seconds */ ++ char validJD; /* True (1) if iJD is valid */ ++ char rawS; /* Raw numeric value stored in s */ ++ char validYMD; /* True (1) if Y,M,D are valid */ ++ char validHMS; /* True (1) if h,m,s are valid */ ++ char validTZ; /* True (1) if tz is valid */ ++ char tzSet; /* Timezone was set explicitly */ ++ char isError; /* An overflow has occurred */ + }; + + + /* +-** Convert zDate into one or more integers. Additional arguments +-** come in groups of 5 as follows: ++** Convert zDate into one or more integers according to the conversion ++** specifier zFormat. ++** ++** zFormat[] contains 4 characters for each integer converted, except for ++** the last integer which is specified by three characters. The meaning ++** of a four-character format specifiers ABCD is: ++** ++** A: number of digits to convert. Always "2" or "4". ++** B: minimum value. Always "0" or "1". ++** C: maximum value, decoded as: ++** a: 12 ++** b: 14 ++** c: 24 ++** d: 31 ++** e: 59 ++** f: 9999 ++** D: the separator character, or \000 to indicate this is the ++** last number to convert. + ** +-** N number of digits in the integer +-** min minimum allowed value of the integer +-** max maximum allowed value of the integer +-** nextC first character after the integer +-** pVal where to write the integers value. ++** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would ++** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-". ++** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates ++** the 2-digit day which is the last integer in the set. + ** +-** Conversions continue until one with nextC==0 is encountered. + ** The function returns the number of successful conversions. + */ +-static int getDigits(const char *zDate, ...){ ++static int getDigits(const char *zDate, const char *zFormat, ...){ ++ /* The aMx[] array translates the 3rd character of each format ++ ** spec into a max size: a b c d e f */ ++ static const u16 aMx[] = { 12, 14, 24, 31, 59, 9999 }; + va_list ap; +- int val; +- int N; +- int min; +- int max; +- int nextC; +- int *pVal; + int cnt = 0; +- va_start(ap, zDate); ++ char nextC; ++ va_start(ap, zFormat); + do{ +- N = va_arg(ap, int); +- min = va_arg(ap, int); +- max = va_arg(ap, int); +- nextC = va_arg(ap, int); +- pVal = va_arg(ap, int*); ++ char N = zFormat[0] - '0'; ++ char min = zFormat[1] - '0'; ++ int val = 0; ++ u16 max; ++ ++ assert( zFormat[2]>='a' && zFormat[2]<='f' ); ++ max = aMx[zFormat[2] - 'a']; ++ nextC = zFormat[3]; + val = 0; + while( N-- ){ + if( !sqlite3Isdigit(*zDate) ){ +@@ -15429,12 +21049,13 @@ static int getDigits(const char *zDate, ...){ + val = val*10 + *zDate - '0'; + zDate++; + } +- if( val max || (nextC!=0 && nextC!=*zDate) ){ ++ if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){ + goto end_getDigits; + } +- *pVal = val; ++ *va_arg(ap,int*) = val; + zDate++; + cnt++; ++ zFormat += 4; + }while( nextC ); + end_getDigits: + va_end(ap); +@@ -15475,13 +21096,14 @@ static int parseTimezone(const char *zDate, DateTime *p){ + return c!=0; + } + zDate++; +- if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){ ++ if( getDigits(zDate, "20b:20e", &nHr, &nMn)!=2 ){ + return 1; + } + zDate += 5; + p->tz = sgn*(nMn + nHr*60); + zulu_time: + while( sqlite3Isspace(*zDate) ){ zDate++; } ++ p->tzSet = 1; + return *zDate!=0; + } + +@@ -15495,13 +21117,13 @@ zulu_time: + static int parseHhMmSs(const char *zDate, DateTime *p){ + int h, m, s; + double ms = 0.0; +- if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){ ++ if( getDigits(zDate, "20c:20e", &h, &m)!=2 ){ + return 1; + } + zDate += 5; + if( *zDate==':' ){ + zDate++; +- if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){ ++ if( getDigits(zDate, "20e", &s)!=1 ){ + return 1; + } + zDate += 2; +@@ -15519,6 +21141,7 @@ static int parseHhMmSs(const char *zDate, DateTime *p){ + s = 0; + } + p->validJD = 0; ++ p->rawS = 0; + p->validHMS = 1; + p->h = h; + p->m = m; +@@ -15528,6 +21151,14 @@ static int parseHhMmSs(const char *zDate, DateTime *p){ + return 0; + } + ++/* ++** Put the DateTime object into its error state. ++*/ ++static void datetimeError(DateTime *p){ ++ memset(p, 0, sizeof(*p)); ++ p->isError = 1; ++} ++ + /* + ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume + ** that the YYYY-MM-DD is according to the Gregorian calendar. +@@ -15547,6 +21178,10 @@ static void computeJD(DateTime *p){ + M = 1; + D = 1; + } ++ if( Y<-4713 || Y>9999 || p->rawS ){ ++ datetimeError(p); ++ return; ++ } + if( M<=2 ){ + Y--; + M += 12; +@@ -15589,7 +21224,7 @@ static int parseYyyyMmDd(const char *zDate, DateTime *p){ + }else{ + neg = 0; + } +- if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){ ++ if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){ + return 1; + } + zDate += 10; +@@ -15627,6 +21262,21 @@ static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){ + } + } + ++/* ++** Input "r" is a numeric quantity which might be a julian day number, ++** or the number of seconds since 1970. If the value if r is within ++** range of a julian day number, install it as such and set validJD. ++** If the value is a valid unix timestamp, put it in p->s and set p->rawS. ++*/ ++static void setRawDateNumber(DateTime *p, double r){ ++ p->s = r; ++ p->rawS = 1; ++ if( r>=0.0 && r<5373484.5 ){ ++ p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); ++ p->validJD = 1; ++ } ++} ++ + /* + ** Attempt to parse the given string into a julian day number. Return + ** the number of errors. +@@ -15653,16 +21303,33 @@ static int parseDateOrTime( + return 0; + }else if( parseHhMmSs(zDate, p)==0 ){ + return 0; +- }else if( sqlite3StrICmp(zDate,"now")==0){ ++ }else if( sqlite3StrICmp(zDate,"now")==0 && sqlite3NotPureFunc(context) ){ + return setDateTimeToCurrent(context, p); + }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){ +- p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); +- p->validJD = 1; ++ setRawDateNumber(p, r); + return 0; + } + return 1; + } + ++/* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999. ++** Multiplying this by 86400000 gives 464269060799999 as the maximum value ++** for DateTime.iJD. ++** ++** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with ++** such a large integer literal, so we have to encode it. ++*/ ++#define INT_464269060799999 ((((i64)0x1a640)<<32)|0x1072fdff) ++ ++/* ++** Return TRUE if the given julian day number is within range. ++** ++** The input is the JulianDay times 86400000. ++*/ ++static int validJulianDay(sqlite3_int64 iJD){ ++ return iJD>=0 && iJD<=INT_464269060799999; ++} ++ + /* + ** Compute the Year, Month, and Day from the julian day number. + */ +@@ -15673,13 +21340,16 @@ static void computeYMD(DateTime *p){ + p->Y = 2000; + p->M = 1; + p->D = 1; ++ }else if( !validJulianDay(p->iJD) ){ ++ datetimeError(p); ++ return; + }else{ + Z = (int)((p->iJD + 43200000)/86400000); + A = (int)((Z - 1867216.25)/36524.25); + A = Z + 1 + A - (A/4); + B = A + 1524; + C = (int)((B - 122.1)/365.25); +- D = (36525*C)/100; ++ D = (36525*(C&32767))/100; + E = (int)((B-D)/30.6001); + X1 = (int)(30.6001*E); + p->D = B - D - X1; +@@ -15704,6 +21374,7 @@ static void computeHMS(DateTime *p){ + s -= p->h*3600; + p->m = s/60; + p->s += s - p->m*60; ++ p->rawS = 0; + p->validHMS = 1; + } + +@@ -15724,6 +21395,7 @@ static void clearYMD_HMS_TZ(DateTime *p){ + p->validTZ = 0; + } + ++#ifndef SQLITE_OMIT_LOCALTIME + /* + ** On recent Windows platforms, the localtime_s() function is available + ** as part of the "Secure CRT". It is essentially equivalent to +@@ -15742,7 +21414,6 @@ static void clearYMD_HMS_TZ(DateTime *p){ + #define HAVE_LOCALTIME_S 1 + #endif + +-#ifndef SQLITE_OMIT_LOCALTIME + /* + ** The following routine implements the rough equivalent of localtime_r() + ** using whatever operating-system specific localtime facility that +@@ -15765,14 +21436,14 @@ static int osLocaltime(time_t *t, struct tm *pTm){ + #endif + sqlite3_mutex_enter(mutex); + pX = localtime(t); +-#ifndef SQLITE_OMIT_BUILTIN_TEST ++#ifndef SQLITE_UNTESTABLE + if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0; + #endif + if( pX ) *pTm = *pX; + sqlite3_mutex_leave(mutex); + rc = pX==0; + #else +-#ifndef SQLITE_OMIT_BUILTIN_TEST ++#ifndef SQLITE_UNTESTABLE + if( sqlite3GlobalConfig.bLocaltimeFault ) return 1; + #endif + #if HAVE_LOCALTIME_R +@@ -15843,13 +21514,38 @@ static sqlite3_int64 localtimeOffset( + y.validYMD = 1; + y.validHMS = 1; + y.validJD = 0; ++ y.rawS = 0; + y.validTZ = 0; ++ y.isError = 0; + computeJD(&y); + *pRc = SQLITE_OK; + return y.iJD - x.iJD; + } + #endif /* SQLITE_OMIT_LOCALTIME */ + ++/* ++** The following table defines various date transformations of the form ++** ++** 'NNN days' ++** ++** Where NNN is an arbitrary floating-point number and "days" can be one ++** of several units of time. ++*/ ++static const struct { ++ u8 eType; /* Transformation type code */ ++ u8 nName; /* Length of th name */ ++ char *zName; /* Name of the transformation */ ++ double rLimit; /* Maximum NNN value for this transform */ ++ double rXform; /* Constant used for this transform */ ++} aXformType[] = { ++ { 0, 6, "second", 464269060800.0, 86400000.0/(24.0*60.0*60.0) }, ++ { 0, 6, "minute", 7737817680.0, 86400000.0/(24.0*60.0) }, ++ { 0, 4, "hour", 128963628.0, 86400000.0/24.0 }, ++ { 0, 3, "day", 5373485.0, 86400000.0 }, ++ { 1, 5, "month", 176546.0, 30.0*86400000.0 }, ++ { 2, 4, "year", 14713.0, 365.0*86400000.0 }, ++}; ++ + /* + ** Process a modifier to a date-time stamp. The modifiers are + ** as follows: +@@ -15874,17 +21570,15 @@ static sqlite3_int64 localtimeOffset( + ** to context pCtx. If the error is an unrecognized modifier, no error is + ** written to pCtx. + */ +-static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ ++static int parseModifier( ++ sqlite3_context *pCtx, /* Function context */ ++ const char *z, /* The text of the modifier */ ++ int n, /* Length of zMod in bytes */ ++ DateTime *p /* The date/time value to be modified */ ++){ + int rc = 1; +- int n; + double r; +- char *z, zBuf[30]; +- z = zBuf; +- for(n=0; n iJD += localtimeOffset(p, pCtx, &rc); + clearYMD_HMS_TZ(p); +@@ -15904,23 +21598,33 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ + /* + ** unixepoch + ** +- ** Treat the current value of p->iJD as the number of ++ ** Treat the current value of p->s as the number of + ** seconds since 1970. Convert to a real julian day number. + */ +- if( strcmp(z, "unixepoch")==0 && p->validJD ){ +- p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000; +- clearYMD_HMS_TZ(p); +- rc = 0; ++ if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){ ++ r = p->s*1000.0 + 210866760000000.0; ++ if( r>=0.0 && r<464269060800000.0 ){ ++ clearYMD_HMS_TZ(p); ++ p->iJD = (sqlite3_int64)r; ++ p->validJD = 1; ++ p->rawS = 0; ++ rc = 0; ++ } + } + #ifndef SQLITE_OMIT_LOCALTIME +- else if( strcmp(z, "utc")==0 ){ +- sqlite3_int64 c1; +- computeJD(p); +- c1 = localtimeOffset(p, pCtx, &rc); +- if( rc==SQLITE_OK ){ +- p->iJD -= c1; +- clearYMD_HMS_TZ(p); +- p->iJD += c1 - localtimeOffset(p, pCtx, &rc); ++ else if( sqlite3_stricmp(z, "utc")==0 && sqlite3NotPureFunc(pCtx) ){ ++ if( p->tzSet==0 ){ ++ sqlite3_int64 c1; ++ computeJD(p); ++ c1 = localtimeOffset(p, pCtx, &rc); ++ if( rc==SQLITE_OK ){ ++ p->iJD -= c1; ++ clearYMD_HMS_TZ(p); ++ p->iJD += c1 - localtimeOffset(p, pCtx, &rc); ++ } ++ p->tzSet = 1; ++ }else{ ++ rc = SQLITE_OK; + } + } + #endif +@@ -15934,7 +21638,7 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ + ** weekday N where 0==Sunday, 1==Monday, and so forth. If the + ** date is already on the appropriate weekday, this is a no-op. + */ +- if( strncmp(z, "weekday ", 8)==0 ++ if( sqlite3_strnicmp(z, "weekday ", 8)==0 + && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8) + && (n=(int)r)==r && n>=0 && r<7 ){ + sqlite3_int64 Z; +@@ -15957,23 +21661,24 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ + ** Move the date backwards to the beginning of the current day, + ** or month or year. + */ +- if( strncmp(z, "start of ", 9)!=0 ) break; ++ if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break; ++ if( !p->validJD && !p->validYMD && !p->validHMS ) break; + z += 9; + computeYMD(p); + p->validHMS = 1; + p->h = p->m = 0; + p->s = 0.0; ++ p->rawS = 0; + p->validTZ = 0; + p->validJD = 0; +- if( strcmp(z,"month")==0 ){ ++ if( sqlite3_stricmp(z,"month")==0 ){ + p->D = 1; + rc = 0; +- }else if( strcmp(z,"year")==0 ){ +- computeYMD(p); ++ }else if( sqlite3_stricmp(z,"year")==0 ){ + p->M = 1; + p->D = 1; + rc = 0; +- }else if( strcmp(z,"day")==0 ){ ++ }else if( sqlite3_stricmp(z,"day")==0 ){ + rc = 0; + } + break; +@@ -15991,6 +21696,7 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ + case '8': + case '9': { + double rRounder; ++ int i; + for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){} + if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){ + rc = 1; +@@ -16019,46 +21725,48 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ + rc = 0; + break; + } ++ ++ /* If control reaches this point, it means the transformation is ++ ** one of the forms like "+NNN days". */ + z += n; + while( sqlite3Isspace(*z) ) z++; + n = sqlite3Strlen30(z); + if( n>10 || n<3 ) break; +- if( z[n-1]=='s' ){ z[n-1] = 0; n--; } ++ if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--; + computeJD(p); +- rc = 0; ++ rc = 1; + rRounder = r<0 ? -0.5 : +0.5; +- if( n==3 && strcmp(z,"day")==0 ){ +- p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder); +- }else if( n==4 && strcmp(z,"hour")==0 ){ +- p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder); +- }else if( n==6 && strcmp(z,"minute")==0 ){ +- p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder); +- }else if( n==6 && strcmp(z,"second")==0 ){ +- p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder); +- }else if( n==5 && strcmp(z,"month")==0 ){ +- int x, y; +- computeYMD_HMS(p); +- p->M += (int)r; +- x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; +- p->Y += x; +- p->M -= x*12; +- p->validJD = 0; +- computeJD(p); +- y = (int)r; +- if( y!=r ){ +- p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder); +- } +- }else if( n==4 && strcmp(z,"year")==0 ){ +- int y = (int)r; +- computeYMD_HMS(p); +- p->Y += y; +- p->validJD = 0; +- computeJD(p); +- if( y!=r ){ +- p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder); ++ for(i=0; i -aXformType[i].rLimit && r M += (int)r; ++ x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; ++ p->Y += x; ++ p->M -= x*12; ++ p->validJD = 0; ++ r -= (int)r; ++ break; ++ } ++ case 2: { /* Special processing to add years */ ++ int y = (int)r; ++ computeYMD_HMS(p); ++ p->Y += y; ++ p->validJD = 0; ++ r -= (int)r; ++ break; ++ } ++ } ++ computeJD(p); ++ p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder); ++ rc = 0; ++ break; + } +- }else{ +- rc = 1; + } + clearYMD_HMS_TZ(p); + break; +@@ -16085,7 +21793,7 @@ static int isDate( + sqlite3_value **argv, + DateTime *p + ){ +- int i; ++ int i, n; + const unsigned char *z; + int eType; + memset(p, 0, sizeof(*p)); +@@ -16094,8 +21802,7 @@ static int isDate( + } + if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT + || eType==SQLITE_INTEGER ){ +- p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5); +- p->validJD = 1; ++ setRawDateNumber(p, sqlite3_value_double(argv[0])); + }else{ + z = sqlite3_value_text(argv[0]); + if( !z || parseDateOrTime(context, (char*)z, p) ){ +@@ -16104,8 +21811,11 @@ static int isDate( + } + for(i=1; i isError || !validJulianDay(p->iJD) ) return 1; + return 0; + } + +@@ -16268,7 +21978,7 @@ static void strftimeFunc( + sqlite3_result_error_toobig(context); + return; + }else{ +- z = sqlite3DbMallocRaw(db, (int)n); ++ z = sqlite3DbMallocRawNN(db, (int)n); + if( z==0 ){ + sqlite3_result_error_nomem(context); + return; +@@ -16404,7 +22114,6 @@ static void currentTimeFunc( + ){ + time_t t; + char *zFormat = (char *)sqlite3_user_data(context); +- sqlite3 *db; + sqlite3_int64 iT; + struct tm *pTm; + struct tm sNow; +@@ -16437,29 +22146,23 @@ static void currentTimeFunc( + ** external linkage. + */ + SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){ +- static SQLITE_WSD FuncDef aDateTimeFuncs[] = { ++ static FuncDef aDateTimeFuncs[] = { + #ifndef SQLITE_OMIT_DATETIME_FUNCS +- FUNCTION(julianday, -1, 0, 0, juliandayFunc ), +- FUNCTION(date, -1, 0, 0, dateFunc ), +- FUNCTION(time, -1, 0, 0, timeFunc ), +- FUNCTION(datetime, -1, 0, 0, datetimeFunc ), +- FUNCTION(strftime, -1, 0, 0, strftimeFunc ), +- FUNCTION(current_time, 0, 0, 0, ctimeFunc ), +- FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc), +- FUNCTION(current_date, 0, 0, 0, cdateFunc ), ++ PURE_DATE(julianday, -1, 0, 0, juliandayFunc ), ++ PURE_DATE(date, -1, 0, 0, dateFunc ), ++ PURE_DATE(time, -1, 0, 0, timeFunc ), ++ PURE_DATE(datetime, -1, 0, 0, datetimeFunc ), ++ PURE_DATE(strftime, -1, 0, 0, strftimeFunc ), ++ DFUNCTION(current_time, 0, 0, 0, ctimeFunc ), ++ DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc), ++ DFUNCTION(current_date, 0, 0, 0, cdateFunc ), + #else + STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc), + STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc), + STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc), + #endif + }; +- int i; +- FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); +- FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs); +- +- for(i=0; i pMethods ){ +- rc = pId->pMethods->xClose(pId); ++ pId->pMethods->xClose(pId); + pId->pMethods = 0; + } +- return rc; + } + SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){ + DO_OS_MALLOC_TEST(id); +@@ -16545,7 +22267,7 @@ SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){ + } + SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){ + DO_OS_MALLOC_TEST(id); +- return id->pMethods->xSync(id, flags); ++ return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK; + } + SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){ + DO_OS_MALLOC_TEST(id); +@@ -16572,11 +22294,14 @@ SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){ + ** routine has no return value since the return value would be meaningless. + */ + SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){ ++ if( id->pMethods==0 ) return SQLITE_NOTFOUND; + #ifdef SQLITE_TEST +- if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){ ++ if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ++ && op!=SQLITE_FCNTL_LOCK_TIMEOUT ++ ){ + /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite +- ** is using a regular VFS, it is called after the corresponding +- ** transaction has been committed. Injecting a fault at this point ++ ** is using a regular VFS, it is called after the corresponding ++ ** transaction has been committed. Injecting a fault at this point + ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM + ** but the transaction is committed anyway. + ** +@@ -16590,7 +22315,7 @@ SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){ + return id->pMethods->xFileControl(id, op, pArg); + } + SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){ +- (void)id->pMethods->xFileControl(id, op, pArg); ++ if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg); + } + + SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){ +@@ -16600,6 +22325,7 @@ SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){ + SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){ + return id->pMethods->xDeviceCharacteristics(id); + } ++#ifndef SQLITE_OMIT_WAL + SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){ + return id->pMethods->xShmLock(id, offset, n, flags); + } +@@ -16619,6 +22345,7 @@ SQLITE_PRIVATE int sqlite3OsShmMap( + DO_OS_MALLOC_TEST(id); + return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp); + } ++#endif /* SQLITE_OMIT_WAL */ + + #if SQLITE_MAX_MMAP_SIZE>0 + /* The real implementation of xFetch and xUnfetch */ +@@ -16645,10 +22372,10 @@ SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){ + ** VFS methods. + */ + SQLITE_PRIVATE int sqlite3OsOpen( +- sqlite3_vfs *pVfs, +- const char *zPath, +- sqlite3_file *pFile, +- int flags, ++ sqlite3_vfs *pVfs, ++ const char *zPath, ++ sqlite3_file *pFile, ++ int flags, + int *pFlagsOut + ){ + int rc; +@@ -16667,18 +22394,18 @@ SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dir + return pVfs->xDelete(pVfs, zPath, dirSync); + } + SQLITE_PRIVATE int sqlite3OsAccess( +- sqlite3_vfs *pVfs, +- const char *zPath, +- int flags, ++ sqlite3_vfs *pVfs, ++ const char *zPath, ++ int flags, + int *pResOut + ){ + DO_OS_MALLOC_TEST(0); + return pVfs->xAccess(pVfs, zPath, flags, pResOut); + } + SQLITE_PRIVATE int sqlite3OsFullPathname( +- sqlite3_vfs *pVfs, +- const char *zPath, +- int nPathOut, ++ sqlite3_vfs *pVfs, ++ const char *zPath, ++ int nPathOut, + char *zPathOut + ){ + DO_OS_MALLOC_TEST(0); +@@ -16705,6 +22432,9 @@ SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufO + SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){ + return pVfs->xSleep(pVfs, nMicro); + } ++SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs *pVfs){ ++ return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0; ++} + SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){ + int rc; + /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64() +@@ -16724,13 +22454,13 @@ SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p + } + + SQLITE_PRIVATE int sqlite3OsOpenMalloc( +- sqlite3_vfs *pVfs, +- const char *zFile, +- sqlite3_file **ppFile, ++ sqlite3_vfs *pVfs, ++ const char *zFile, ++ sqlite3_file **ppFile, + int flags, + int *pOutFlags + ){ +- int rc = SQLITE_NOMEM; ++ int rc; + sqlite3_file *pFile; + pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile); + if( pFile ){ +@@ -16740,15 +22470,15 @@ SQLITE_PRIVATE int sqlite3OsOpenMalloc( + }else{ + *ppFile = pFile; + } ++ }else{ ++ rc = SQLITE_NOMEM_BKPT; + } + return rc; + } +-SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){ +- int rc = SQLITE_OK; ++SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *pFile){ + assert( pFile ); +- rc = sqlite3OsClose(pFile); ++ sqlite3OsClose(pFile); + sqlite3_free(pFile); +- return rc; + } + + /* +@@ -16759,7 +22489,7 @@ SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){ + */ + SQLITE_PRIVATE int sqlite3OsInit(void){ + void *p = sqlite3_malloc(10); +- if( p==0 ) return SQLITE_NOMEM; ++ if( p==0 ) return SQLITE_NOMEM_BKPT; + sqlite3_free(p); + return sqlite3_os_init(); + } +@@ -16774,7 +22504,7 @@ static sqlite3_vfs * SQLITE_WSD vfsList = 0; + ** Locate a VFS by name. If no name is given, simply return the + ** first VFS on the list. + */ +-SQLITE_API sqlite3_vfs *SQLITE_STDCALL sqlite3_vfs_find(const char *zVfs){ ++SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){ + sqlite3_vfs *pVfs = 0; + #if SQLITE_THREADSAFE + sqlite3_mutex *mutex; +@@ -16820,7 +22550,7 @@ static void vfsUnlink(sqlite3_vfs *pVfs){ + ** VFS multiple times. The new VFS becomes the default if makeDflt is + ** true. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){ ++SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){ + MUTEX_LOGIC(sqlite3_mutex *mutex;) + #ifndef SQLITE_OMIT_AUTOINIT + int rc = sqlite3_initialize(); +@@ -16848,10 +22578,13 @@ SQLITE_API int SQLITE_STDCALL sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDf + /* + ** Unregister a VFS so that it is no longer accessible. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs *pVfs){ +-#if SQLITE_THREADSAFE +- sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ++SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){ ++ MUTEX_LOGIC(sqlite3_mutex *mutex;) ++#ifndef SQLITE_OMIT_AUTOINIT ++ int rc = sqlite3_initialize(); ++ if( rc ) return rc; + #endif ++ MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) + sqlite3_mutex_enter(mutex); + vfsUnlink(pVfs); + sqlite3_mutex_leave(mutex); +@@ -16886,8 +22619,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs *pVfs){ + ** during a hash table resize is a benign fault. + */ + ++/* #include "sqliteInt.h" */ + +-#ifndef SQLITE_OMIT_BUILTIN_TEST ++#ifndef SQLITE_UNTESTABLE + + /* + ** Global variables. +@@ -16945,7 +22679,7 @@ SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){ + } + } + +-#endif /* #ifndef SQLITE_OMIT_BUILTIN_TEST */ ++#endif /* #ifndef SQLITE_UNTESTABLE */ + + /************** End of fault.c ***********************************************/ + /************** Begin file mem0.c ********************************************/ +@@ -16967,6 +22701,7 @@ SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){ + ** are merely placeholders. Real drivers must be substituted using + ** sqlite3_config() before SQLite will operate. + */ ++/* #include "sqliteInt.h" */ + + /* + ** This version of the memory allocator is the default. It is +@@ -17053,6 +22788,7 @@ SQLITE_PRIVATE void sqlite3MemSetDefault(void){ + ** be necessary when compiling for Delphi, + ** for example. + */ ++/* #include "sqliteInt.h" */ + + /* + ** This version of the memory allocator is the default. It is +@@ -17068,7 +22804,9 @@ SQLITE_PRIVATE void sqlite3MemSetDefault(void){ + */ + #include + #include ++#ifdef SQLITE_MIGHT_BE_SINGLE_CORE + #include ++#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */ + static malloc_zone_t* _sqliteZone_; + #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x)) + #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x)); +@@ -17136,7 +22874,9 @@ static malloc_zone_t* _sqliteZone_; + */ + static void *sqlite3MemMalloc(int nByte){ + #ifdef SQLITE_MALLOCSIZE +- void *p = SQLITE_MALLOC( nByte ); ++ void *p; ++ testcase( ROUND8(nByte)==nByte ); ++ p = SQLITE_MALLOC( nByte ); + if( p==0 ){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte); +@@ -17145,7 +22885,7 @@ static void *sqlite3MemMalloc(int nByte){ + #else + sqlite3_int64 *p; + assert( nByte>0 ); +- nByte = ROUND8(nByte); ++ testcase( ROUND8(nByte)!=nByte ); + p = SQLITE_MALLOC( nByte+8 ); + if( p ){ + p[0] = nByte; +@@ -17183,10 +22923,11 @@ static void sqlite3MemFree(void *pPrior){ + */ + static int sqlite3MemSize(void *pPrior){ + #ifdef SQLITE_MALLOCSIZE +- return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0; ++ assert( pPrior!=0 ); ++ return (int)SQLITE_MALLOCSIZE(pPrior); + #else + sqlite3_int64 *p; +- if( pPrior==0 ) return 0; ++ assert( pPrior!=0 ); + p = (sqlite3_int64*)pPrior; + p--; + return (int)p[0]; +@@ -17258,19 +22999,10 @@ static int sqlite3MemInit(void *NotUsed){ + }else{ + /* only 1 core, use our own zone to contention over global locks, + ** e.g. we have our own dedicated locks */ +- bool success; +- malloc_zone_t* newzone = malloc_create_zone(4096, 0); +- malloc_set_zone_name(newzone, "Sqlite_Heap"); +- do{ +- success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, +- (void * volatile *)&_sqliteZone_); +- }while(!_sqliteZone_); +- if( !success ){ +- /* somebody registered a zone first */ +- malloc_destroy_zone(newzone); +- } ++ _sqliteZone_ = malloc_create_zone(4096, 0); ++ malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap"); + } +-#endif ++#endif /* defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */ + UNUSED_PARAMETER(NotUsed); + return SQLITE_OK; + } +@@ -17328,6 +23060,7 @@ SQLITE_PRIVATE void sqlite3MemSetDefault(void){ + ** This file contains implementations of the low-level memory allocation + ** routines specified in the sqlite3_mem_methods object. + */ ++/* #include "sqliteInt.h" */ + + /* + ** This version of the memory allocator is used only if the +@@ -17862,6 +23595,7 @@ SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){ + ** This version of the memory allocation subsystem is included + ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined. + */ ++/* #include "sqliteInt.h" */ + + /* + ** This version of the memory allocator is only built into the library +@@ -18314,7 +24048,7 @@ static void memsys3FreeUnsafe(void *pOld){ + */ + static int memsys3Size(void *p){ + Mem3Block *pBlock; +- if( p==0 ) return 0; ++ assert( p!=0 ); + pBlock = (Mem3Block*)p; + assert( (pBlock[-1].u.hdr.size4x&1)!=0 ); + return (pBlock[-1].u.hdr.size4x&~3)*2 - 4; +@@ -18553,7 +24287,7 @@ SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){ + ** + ** This memory allocator uses the following algorithm: + ** +-** 1. All memory allocations sizes are rounded up to a power of 2. ++** 1. All memory allocation sizes are rounded up to a power of 2. + ** + ** 2. If two adjacent free blocks are the halves of a larger block, + ** then the two blocks are coalesced into the single larger block. +@@ -18576,6 +24310,7 @@ SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){ + ** The sqlite3_status() logic tracks the maximum values of n and M so + ** that an application can, at any time, verify this constraint. + */ ++/* #include "sqliteInt.h" */ + + /* + ** This version of the memory allocator is used only when +@@ -18629,6 +24364,7 @@ static SQLITE_WSD struct Mem5Global { + */ + sqlite3_mutex *mutex; + ++#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) + /* + ** Performance statistics + */ +@@ -18640,11 +24376,12 @@ static SQLITE_WSD struct Mem5Global { + u32 maxOut; /* Maximum instantaneous currentOut */ + u32 maxCount; /* Maximum instantaneous currentCount */ + u32 maxRequest; /* Largest allocation (exclusive of internal frag) */ ++#endif + + /* + ** Lists of free blocks. aiFreelist[0] is a list of free blocks of + ** size mem5.szAtom. aiFreelist[1] holds blocks of size szAtom*2. +- ** and so forth. ++ ** aiFreelist[2] holds free blocks of size szAtom*4. And so forth. + */ + int aiFreelist[LOGMAX+1]; + +@@ -18710,9 +24447,7 @@ static void memsys5Link(int i, int iLogsize){ + } + + /* +-** If the STATIC_MEM mutex is not already held, obtain it now. The mutex +-** will already be held (obtained by code in malloc.c) if +-** sqlite3GlobalConfig.bMemStat is true. ++** Obtain or release the mutex needed to access global data structures. + */ + static void memsys5Enter(void){ + sqlite3_mutex_enter(mem5.mutex); +@@ -18722,17 +24457,15 @@ static void memsys5Leave(void){ + } + + /* +-** Return the size of an outstanding allocation, in bytes. The +-** size returned omits the 8-byte header overhead. This only +-** works for chunks that are currently checked out. ++** Return the size of an outstanding allocation, in bytes. ++** This only works for chunks that are currently checked out. + */ + static int memsys5Size(void *p){ +- int iSize = 0; +- if( p ){ +- int i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom); +- assert( i>=0 && i =0 && i 0 ); + ++ /* No more than 1GiB per allocation */ ++ if( nByte > 0x40000000 ) return 0; ++ ++#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) + /* Keep track of the maximum allocation request. Even unfulfilled + ** requests are counted */ + if( (u32)nByte>mem5.maxRequest ){ + mem5.maxRequest = nByte; + } ++#endif + +- /* Abort if the requested allocation size is larger than the largest +- ** power of two that we can represent using 32-bit signed integers. +- */ +- if( nByte > 0x40000000 ){ +- return 0; +- } + + /* Round nByte up to the next valid power of two */ +- for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz 0 ); + assert( mem5.currentOut>=(size*mem5.szAtom) ); + mem5.currentCount--; + mem5.currentOut -= size*mem5.szAtom; + assert( mem5.currentOut>0 || mem5.currentCount==0 ); + assert( mem5.currentCount>0 || mem5.currentOut==0 ); ++#endif + + mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize; + while( ALWAYS(iLogsize >iLogsize) & 1 ){ + iBuddy = iBlock - size; ++ assert( iBuddy>=0 ); + }else{ + iBuddy = iBlock + size; ++ if( iBuddy>=mem5.nBlock ) break; + } +- assert( iBuddy>=0 ); +- if( (iBuddy+(1< mem5.nBlock ) break; + if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break; + memsys5Unlink(iBuddy, iLogsize); + iLogsize++; +@@ -18926,13 +24663,11 @@ static void *memsys5Realloc(void *pPrior, int nBytes){ + if( nBytes<=nOld ){ + return pPrior; + } +- memsys5Enter(); +- p = memsys5MallocUnsafe(nBytes); ++ p = memsys5Malloc(nBytes); + if( p ){ + memcpy(p, pPrior, nOld); +- memsys5FreeUnsafe(pPrior); ++ memsys5Free(pPrior); + } +- memsys5Leave(); + return p; + } + +@@ -19119,6 +24854,7 @@ SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){ + ** + ** This file contains code that is common across all mutex implementations. + */ ++/* #include "sqliteInt.h" */ + + #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT) + /* +@@ -19127,10 +24863,197 @@ SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){ + ** allocate a mutex while the system is uninitialized. + */ + static SQLITE_WSD int mutexIsInit = 0; +-#endif /* SQLITE_DEBUG */ ++#endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */ + + + #ifndef SQLITE_MUTEX_OMIT ++ ++#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS ++/* ++** This block (enclosed by SQLITE_ENABLE_MULTITHREADED_CHECKS) contains ++** the implementation of a wrapper around the system default mutex ++** implementation (sqlite3DefaultMutex()). ++** ++** Most calls are passed directly through to the underlying default ++** mutex implementation. Except, if a mutex is configured by calling ++** sqlite3MutexWarnOnContention() on it, then if contention is ever ++** encountered within xMutexEnter() a warning is emitted via sqlite3_log(). ++** ++** This type of mutex is used as the database handle mutex when testing ++** apps that usually use SQLITE_CONFIG_MULTITHREAD mode. ++*/ ++ ++/* ++** Type for all mutexes used when SQLITE_ENABLE_MULTITHREADED_CHECKS ++** is defined. Variable CheckMutex.mutex is a pointer to the real mutex ++** allocated by the system mutex implementation. Variable iType is usually set ++** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST ++** or one of the static mutex identifiers. Or, if this is a recursive mutex ++** that has been configured using sqlite3MutexWarnOnContention(), it is ++** set to SQLITE_MUTEX_WARNONCONTENTION. ++*/ ++typedef struct CheckMutex CheckMutex; ++struct CheckMutex { ++ int iType; ++ sqlite3_mutex *mutex; ++}; ++ ++#define SQLITE_MUTEX_WARNONCONTENTION (-1) ++ ++/* ++** Pointer to real mutex methods object used by the CheckMutex ++** implementation. Set by checkMutexInit(). ++*/ ++static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods; ++ ++#ifdef SQLITE_DEBUG ++static int checkMutexHeld(sqlite3_mutex *p){ ++ return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex); ++} ++static int checkMutexNotheld(sqlite3_mutex *p){ ++ return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex); ++} ++#endif ++ ++/* ++** Initialize and deinitialize the mutex subsystem. ++*/ ++static int checkMutexInit(void){ ++ pGlobalMutexMethods = sqlite3DefaultMutex(); ++ return SQLITE_OK; ++} ++static int checkMutexEnd(void){ ++ pGlobalMutexMethods = 0; ++ return SQLITE_OK; ++} ++ ++/* ++** Allocate a mutex. ++*/ ++static sqlite3_mutex *checkMutexAlloc(int iType){ ++ static CheckMutex staticMutexes[] = { ++ {2, 0}, {3, 0}, {4, 0}, {5, 0}, ++ {6, 0}, {7, 0}, {8, 0}, {9, 0}, ++ {10, 0}, {11, 0}, {12, 0}, {13, 0} ++ }; ++ CheckMutex *p = 0; ++ ++ assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 ); ++ if( iType<2 ){ ++ p = sqlite3MallocZero(sizeof(CheckMutex)); ++ if( p==0 ) return 0; ++ p->iType = iType; ++ }else{ ++#ifdef SQLITE_ENABLE_API_ARMOR ++ if( iType-2>=ArraySize(staticMutexes) ){ ++ (void)SQLITE_MISUSE_BKPT; ++ return 0; ++ } ++#endif ++ p = &staticMutexes[iType-2]; ++ } ++ ++ if( p->mutex==0 ){ ++ p->mutex = pGlobalMutexMethods->xMutexAlloc(iType); ++ if( p->mutex==0 ){ ++ if( iType<2 ){ ++ sqlite3_free(p); ++ } ++ p = 0; ++ } ++ } ++ ++ return (sqlite3_mutex*)p; ++} ++ ++/* ++** Free a mutex. ++*/ ++static void checkMutexFree(sqlite3_mutex *p){ ++ assert( SQLITE_MUTEX_RECURSIVE<2 ); ++ assert( SQLITE_MUTEX_FAST<2 ); ++ assert( SQLITE_MUTEX_WARNONCONTENTION<2 ); ++ ++#if SQLITE_ENABLE_API_ARMOR ++ if( ((CheckMutex*)p)->iType<2 ) ++#endif ++ { ++ CheckMutex *pCheck = (CheckMutex*)p; ++ pGlobalMutexMethods->xMutexFree(pCheck->mutex); ++ sqlite3_free(pCheck); ++ } ++#ifdef SQLITE_ENABLE_API_ARMOR ++ else{ ++ (void)SQLITE_MISUSE_BKPT; ++ } ++#endif ++} ++ ++/* ++** Enter the mutex. ++*/ ++static void checkMutexEnter(sqlite3_mutex *p){ ++ CheckMutex *pCheck = (CheckMutex*)p; ++ if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){ ++ if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){ ++ return; ++ } ++ sqlite3_log(SQLITE_MISUSE, ++ "illegal multi-threaded access to database connection" ++ ); ++ } ++ pGlobalMutexMethods->xMutexEnter(pCheck->mutex); ++} ++ ++/* ++** Enter the mutex (do not block). ++*/ ++static int checkMutexTry(sqlite3_mutex *p){ ++ CheckMutex *pCheck = (CheckMutex*)p; ++ return pGlobalMutexMethods->xMutexTry(pCheck->mutex); ++} ++ ++/* ++** Leave the mutex. ++*/ ++static void checkMutexLeave(sqlite3_mutex *p){ ++ CheckMutex *pCheck = (CheckMutex*)p; ++ pGlobalMutexMethods->xMutexLeave(pCheck->mutex); ++} ++ ++sqlite3_mutex_methods const *multiThreadedCheckMutex(void){ ++ static const sqlite3_mutex_methods sMutex = { ++ checkMutexInit, ++ checkMutexEnd, ++ checkMutexAlloc, ++ checkMutexFree, ++ checkMutexEnter, ++ checkMutexTry, ++ checkMutexLeave, ++#ifdef SQLITE_DEBUG ++ checkMutexHeld, ++ checkMutexNotheld ++#else ++ 0, ++ 0 ++#endif ++ }; ++ return &sMutex; ++} ++ ++/* ++** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as ++** one on which there should be no contention. ++*/ ++SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex *p){ ++ if( sqlite3GlobalConfig.mutex.xMutexAlloc==checkMutexAlloc ){ ++ CheckMutex *pCheck = (CheckMutex*)p; ++ assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE ); ++ pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION; ++ } ++} ++#endif /* ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS */ ++ + /* + ** Initialize the mutex system. + */ +@@ -19146,15 +25069,26 @@ SQLITE_PRIVATE int sqlite3MutexInit(void){ + sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex; + + if( sqlite3GlobalConfig.bCoreMutex ){ ++#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS ++ pFrom = multiThreadedCheckMutex(); ++#else + pFrom = sqlite3DefaultMutex(); ++#endif + }else{ + pFrom = sqlite3NoopMutex(); + } +- memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc)); +- memcpy(&pTo->xMutexFree, &pFrom->xMutexFree, +- sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree)); ++ pTo->xMutexInit = pFrom->xMutexInit; ++ pTo->xMutexEnd = pFrom->xMutexEnd; ++ pTo->xMutexFree = pFrom->xMutexFree; ++ pTo->xMutexEnter = pFrom->xMutexEnter; ++ pTo->xMutexTry = pFrom->xMutexTry; ++ pTo->xMutexLeave = pFrom->xMutexLeave; ++ pTo->xMutexHeld = pFrom->xMutexHeld; ++ pTo->xMutexNotheld = pFrom->xMutexNotheld; ++ sqlite3MemoryBarrier(); + pTo->xMutexAlloc = pFrom->xMutexAlloc; + } ++ assert( sqlite3GlobalConfig.mutex.xMutexInit ); + rc = sqlite3GlobalConfig.mutex.xMutexInit(); + + #ifdef SQLITE_DEBUG +@@ -19184,11 +25118,12 @@ SQLITE_PRIVATE int sqlite3MutexEnd(void){ + /* + ** Retrieve a pointer to a static mutex or allocate a new dynamic one. + */ +-SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_mutex_alloc(int id){ ++SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){ + #ifndef SQLITE_OMIT_AUTOINIT + if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0; + if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0; + #endif ++ assert( sqlite3GlobalConfig.mutex.xMutexAlloc ); + return sqlite3GlobalConfig.mutex.xMutexAlloc(id); + } + +@@ -19197,14 +25132,16 @@ SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){ + return 0; + } + assert( GLOBAL(int, mutexIsInit) ); ++ assert( sqlite3GlobalConfig.mutex.xMutexAlloc ); + return sqlite3GlobalConfig.mutex.xMutexAlloc(id); + } + + /* + ** Free a dynamic mutex. + */ +-SQLITE_API void SQLITE_STDCALL sqlite3_mutex_free(sqlite3_mutex *p){ ++SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){ + if( p ){ ++ assert( sqlite3GlobalConfig.mutex.xMutexFree ); + sqlite3GlobalConfig.mutex.xMutexFree(p); + } + } +@@ -19213,8 +25150,9 @@ SQLITE_API void SQLITE_STDCALL sqlite3_mutex_free(sqlite3_mutex *p){ + ** Obtain the mutex p. If some other thread already has the mutex, block + ** until it can be obtained. + */ +-SQLITE_API void SQLITE_STDCALL sqlite3_mutex_enter(sqlite3_mutex *p){ ++SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){ + if( p ){ ++ assert( sqlite3GlobalConfig.mutex.xMutexEnter ); + sqlite3GlobalConfig.mutex.xMutexEnter(p); + } + } +@@ -19223,9 +25161,10 @@ SQLITE_API void SQLITE_STDCALL sqlite3_mutex_enter(sqlite3_mutex *p){ + ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another + ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_mutex_try(sqlite3_mutex *p){ ++SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){ + int rc = SQLITE_OK; + if( p ){ ++ assert( sqlite3GlobalConfig.mutex.xMutexTry ); + return sqlite3GlobalConfig.mutex.xMutexTry(p); + } + return rc; +@@ -19237,8 +25176,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_mutex_try(sqlite3_mutex *p){ + ** is not currently entered. If a NULL pointer is passed as an argument + ** this function is a no-op. + */ +-SQLITE_API void SQLITE_STDCALL sqlite3_mutex_leave(sqlite3_mutex *p){ ++SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){ + if( p ){ ++ assert( sqlite3GlobalConfig.mutex.xMutexLeave ); + sqlite3GlobalConfig.mutex.xMutexLeave(p); + } + } +@@ -19248,10 +25188,12 @@ SQLITE_API void SQLITE_STDCALL sqlite3_mutex_leave(sqlite3_mutex *p){ + ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are + ** intended for use inside assert() statements. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_mutex_held(sqlite3_mutex *p){ ++SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){ ++ assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld ); + return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p); + } +-SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex *p){ ++SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){ ++ assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld ); + return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p); + } + #endif +@@ -19287,6 +25229,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex *p){ + ** that does error checking on mutexes to make sure they are being + ** called correctly. + */ ++/* #include "sqliteInt.h" */ + + #ifndef SQLITE_MUTEX_OMIT + +@@ -19368,7 +25311,7 @@ static int debugMutexEnd(void){ return SQLITE_OK; } + ** that means that a mutex could not be allocated. + */ + static sqlite3_mutex *debugMutexAlloc(int id){ +- static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_APP3 - 1]; ++ static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1]; + sqlite3_debug_mutex *pNew = 0; + switch( id ){ + case SQLITE_MUTEX_FAST: +@@ -19490,6 +25433,7 @@ SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){ + ************************************************************************* + ** This file contains the C functions that implement mutexes for pthreads + */ ++/* #include "sqliteInt.h" */ + + /* + ** The code in this file is only used if we are compiling threadsafe +@@ -19528,9 +25472,12 @@ struct sqlite3_mutex { + #endif + }; + #if SQLITE_MUTEX_NREF +-#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 } ++# define SQLITE3_MUTEX_INITIALIZER(id) \ ++ {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0} ++#elif defined(SQLITE_ENABLE_API_ARMOR) ++# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id } + #else +-#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER } ++#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER } + #endif + + /* +@@ -19558,6 +25505,19 @@ static int pthreadMutexNotheld(sqlite3_mutex *p){ + } + #endif + ++/* ++** Try to provide a memory barrier operation, needed for initialization ++** and also for the implementation of xShmBarrier in the VFS in cases ++** where SQLite is compiled without mutexes. ++*/ ++SQLITE_PRIVATE void sqlite3MemoryBarrier(void){ ++#if defined(SQLITE_MEMORY_BARRIER) ++ SQLITE_MEMORY_BARRIER; ++#elif defined(__GNUC__) && GCC_VERSION>=4001000 ++ __sync_synchronize(); ++#endif ++} ++ + /* + ** Initialize and deinitialize the mutex subsystem. + */ +@@ -19583,6 +25543,9 @@ static int pthreadMutexEnd(void){ return SQLITE_OK; } + ** - SQLITE_MUTEX_STATIC_APP1 + **
- SQLITE_MUTEX_STATIC_APP2 + **
- SQLITE_MUTEX_STATIC_APP3 ++**
- SQLITE_MUTEX_STATIC_VFS1 ++**
- SQLITE_MUTEX_STATIC_VFS2 ++**
- SQLITE_MUTEX_STATIC_VFS3 + ** + ** + ** The first two constants cause sqlite3_mutex_alloc() to create +@@ -19611,15 +25574,18 @@ static int pthreadMutexEnd(void){ return SQLITE_OK; } + */ + static sqlite3_mutex *pthreadMutexAlloc(int iType){ + static sqlite3_mutex staticMutexes[] = { +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER ++ SQLITE3_MUTEX_INITIALIZER(2), ++ SQLITE3_MUTEX_INITIALIZER(3), ++ SQLITE3_MUTEX_INITIALIZER(4), ++ SQLITE3_MUTEX_INITIALIZER(5), ++ SQLITE3_MUTEX_INITIALIZER(6), ++ SQLITE3_MUTEX_INITIALIZER(7), ++ SQLITE3_MUTEX_INITIALIZER(8), ++ SQLITE3_MUTEX_INITIALIZER(9), ++ SQLITE3_MUTEX_INITIALIZER(10), ++ SQLITE3_MUTEX_INITIALIZER(11), ++ SQLITE3_MUTEX_INITIALIZER(12), ++ SQLITE3_MUTEX_INITIALIZER(13) + }; + sqlite3_mutex *p; + switch( iType ){ +@@ -19637,6 +25603,9 @@ static sqlite3_mutex *pthreadMutexAlloc(int iType){ + pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&p->mutex, &recursiveAttr); + pthread_mutexattr_destroy(&recursiveAttr); ++#endif ++#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR) ++ p->id = SQLITE_MUTEX_RECURSIVE; + #endif + } + break; +@@ -19645,6 +25614,9 @@ static sqlite3_mutex *pthreadMutexAlloc(int iType){ + p = sqlite3MallocZero( sizeof(*p) ); + if( p ){ + pthread_mutex_init(&p->mutex, 0); ++#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR) ++ p->id = SQLITE_MUTEX_FAST; ++#endif + } + break; + } +@@ -19660,7 +25632,7 @@ static sqlite3_mutex *pthreadMutexAlloc(int iType){ + } + } + #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR) +- if( p ) p->id = iType; ++ assert( p==0 || p->id==iType ); + #endif + return p; + } +@@ -19858,6 +25830,7 @@ SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){ + ************************************************************************* + ** This file contains the C functions that implement mutexes for Win32. + */ ++/* #include "sqliteInt.h" */ + + #if SQLITE_OS_WIN + /* +@@ -19902,8 +25875,8 @@ SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){ + */ + #ifdef SQLITE_PERFORMANCE_TRACE + +-/* +-** hwtime.h contains inline assembler code for implementing ++/* ++** hwtime.h contains inline assembler code for implementing + ** high-performance timing routines. + */ + /************** Include hwtime.h in the middle of os_common.h ****************/ +@@ -19923,8 +25896,8 @@ SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){ + ** This file contains inline asm code for retrieving "high-performance" + ** counters for x86 class CPUs. + */ +-#ifndef _HWTIME_H_ +-#define _HWTIME_H_ ++#ifndef SQLITE_HWTIME_H ++#define SQLITE_HWTIME_H + + /* + ** The following routine only works on pentium-class (or newer) processors. +@@ -19992,7 +25965,7 @@ SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); } + + #endif + +-#endif /* !defined(_HWTIME_H_) */ ++#endif /* !defined(SQLITE_HWTIME_H) */ + + /************** End of hwtime.h **********************************************/ + /************** Continuing where we left off in os_common.h ******************/ +@@ -20013,14 +25986,14 @@ static sqlite_uint64 g_elapsed; + ** of code will give us the ability to simulate a disk I/O error. This + ** is used for testing the I/O recovery logic. + */ +-#ifdef SQLITE_TEST +-SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */ +-SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */ +-SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */ +-SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */ +-SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */ +-SQLITE_API int sqlite3_diskfull_pending = 0; +-SQLITE_API int sqlite3_diskfull = 0; ++#if defined(SQLITE_TEST) ++SQLITE_API extern int sqlite3_io_error_hit; ++SQLITE_API extern int sqlite3_io_error_hardhit; ++SQLITE_API extern int sqlite3_io_error_pending; ++SQLITE_API extern int sqlite3_io_error_persist; ++SQLITE_API extern int sqlite3_io_error_benign; ++SQLITE_API extern int sqlite3_diskfull_pending; ++SQLITE_API extern int sqlite3_diskfull; + #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X) + #define SimulateIOError(CODE) \ + if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \ +@@ -20046,17 +26019,17 @@ static void local_ioerr(){ + #define SimulateIOErrorBenign(X) + #define SimulateIOError(A) + #define SimulateDiskfullError(A) +-#endif ++#endif /* defined(SQLITE_TEST) */ + + /* + ** When testing, keep a count of the number of open files. + */ +-#ifdef SQLITE_TEST +-SQLITE_API int sqlite3_open_file_count = 0; ++#if defined(SQLITE_TEST) ++SQLITE_API extern int sqlite3_open_file_count; + #define OpenCounter(X) sqlite3_open_file_count+=(X) + #else + #define OpenCounter(X) +-#endif ++#endif /* defined(SQLITE_TEST) */ + + #endif /* !defined(_OS_COMMON_H_) */ + +@@ -20082,8 +26055,8 @@ SQLITE_API int sqlite3_open_file_count = 0; + ** + ** This file contains code that is specific to Windows. + */ +-#ifndef _OS_WIN_H_ +-#define _OS_WIN_H_ ++#ifndef SQLITE_OS_WIN_H ++#define SQLITE_OS_WIN_H + + /* + ** Include the primary Windows SDK header file. +@@ -20155,7 +26128,7 @@ SQLITE_API int sqlite3_open_file_count = 0; + # define SQLITE_OS_WIN_THREADS 0 + #endif + +-#endif /* _OS_WIN_H_ */ ++#endif /* SQLITE_OS_WIN_H */ + + /************** End of os_win.h **********************************************/ + /************** Continuing where we left off in mutex_w32.c ******************/ +@@ -20176,7 +26149,7 @@ struct sqlite3_mutex { + #ifdef SQLITE_DEBUG + volatile int nRef; /* Number of enterances */ + volatile DWORD owner; /* Thread holding this mutex */ +- volatile int trace; /* True to trace changes */ ++ volatile LONG trace; /* True to trace changes */ + #endif + }; + +@@ -20188,10 +26161,10 @@ struct sqlite3_mutex { + #define SQLITE_W32_MUTEX_INITIALIZER { 0 } + + #ifdef SQLITE_DEBUG +-#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \ ++#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \ + 0L, (DWORD)0, 0 } + #else +-#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 } ++#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id } + #endif + + #ifdef SQLITE_DEBUG +@@ -20213,19 +26186,39 @@ static int winMutexNotheld(sqlite3_mutex *p){ + } + #endif + ++/* ++** Try to provide a memory barrier operation, needed for initialization ++** and also for the xShmBarrier method of the VFS in cases when SQLite is ++** compiled without mutexes (SQLITE_THREADSAFE=0). ++*/ ++SQLITE_PRIVATE void sqlite3MemoryBarrier(void){ ++#if defined(SQLITE_MEMORY_BARRIER) ++ SQLITE_MEMORY_BARRIER; ++#elif defined(__GNUC__) ++ __sync_synchronize(); ++#elif MSVC_VERSION>=1300 ++ _ReadWriteBarrier(); ++#elif defined(MemoryBarrier) ++ MemoryBarrier(); ++#endif ++} ++ + /* + ** Initialize and deinitialize the mutex subsystem. + */ + static sqlite3_mutex winMutex_staticMutexes[] = { +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER, +- SQLITE3_MUTEX_INITIALIZER ++ SQLITE3_MUTEX_INITIALIZER(2), ++ SQLITE3_MUTEX_INITIALIZER(3), ++ SQLITE3_MUTEX_INITIALIZER(4), ++ SQLITE3_MUTEX_INITIALIZER(5), ++ SQLITE3_MUTEX_INITIALIZER(6), ++ SQLITE3_MUTEX_INITIALIZER(7), ++ SQLITE3_MUTEX_INITIALIZER(8), ++ SQLITE3_MUTEX_INITIALIZER(9), ++ SQLITE3_MUTEX_INITIALIZER(10), ++ SQLITE3_MUTEX_INITIALIZER(11), ++ SQLITE3_MUTEX_INITIALIZER(12), ++ SQLITE3_MUTEX_INITIALIZER(13) + }; + + static int winMutex_isInit = 0; +@@ -20237,8 +26230,8 @@ static int winMutex_isNt = -1; /* <0 means "need to query" */ + */ + static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0; + +-SQLITE_API int SQLITE_STDCALL sqlite3_win32_is_nt(void); /* os_win.c */ +-SQLITE_API void SQLITE_STDCALL sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */ ++SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */ ++SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */ + + static int winMutexInit(void){ + /* The first to increment to 1 does actual initialization */ +@@ -20296,6 +26289,9 @@ static int winMutexEnd(void){ + **
- SQLITE_MUTEX_STATIC_APP1 + **
- SQLITE_MUTEX_STATIC_APP2 + **
- SQLITE_MUTEX_STATIC_APP3 ++**
- SQLITE_MUTEX_STATIC_VFS1 ++**
- SQLITE_MUTEX_STATIC_VFS2 ++**
- SQLITE_MUTEX_STATIC_VFS3 + ** + ** + ** The first two constants cause sqlite3_mutex_alloc() to create +@@ -20352,15 +26348,15 @@ static sqlite3_mutex *winMutexAlloc(int iType){ + } + #endif + p = &winMutex_staticMutexes[iType-2]; +- p->id = iType; + #ifdef SQLITE_DEBUG + #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC +- p->trace = 1; ++ InterlockedCompareExchange(&p->trace, 1, 0); + #endif + #endif + break; + } + } ++ assert( p==0 || p->id==iType ); + return p; + } + +@@ -20411,8 +26407,8 @@ static void winMutexEnter(sqlite3_mutex *p){ + p->owner = tid; + p->nRef++; + if( p->trace ){ +- OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n", +- tid, p, p->trace, p->nRef)); ++ OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", ++ tid, p->id, p, p->trace, p->nRef)); + } + #endif + } +@@ -20454,8 +26450,8 @@ static int winMutexTry(sqlite3_mutex *p){ + #endif + #ifdef SQLITE_DEBUG + if( p->trace ){ +- OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n", +- tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc))); ++ OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n", ++ tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc))); + } + #endif + return rc; +@@ -20483,8 +26479,8 @@ static void winMutexLeave(sqlite3_mutex *p){ + LeaveCriticalSection(&p->mutex); + #ifdef SQLITE_DEBUG + if( p->trace ){ +- OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n", +- tid, p, p->trace, p->nRef)); ++ OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", ++ tid, p->id, p, p->trace, p->nRef)); + } + #endif + } +@@ -20527,6 +26523,7 @@ SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){ + ** + ** Memory allocation functions used throughout sqlite. + */ ++/* #include "sqliteInt.h" */ + /* #include
*/ + + /* +@@ -20534,7 +26531,7 @@ SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){ + ** held by SQLite. An example of non-essential memory is memory used to + ** cache database pages that are not currently in use. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int n){ ++SQLITE_API int sqlite3_release_memory(int n){ + #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + return sqlite3PcacheReleaseMemory(n); + #else +@@ -20546,46 +26543,19 @@ SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int n){ + #endif + } + +-/* +-** An instance of the following object records the location of +-** each unused scratch buffer. +-*/ +-typedef struct ScratchFreeslot { +- struct ScratchFreeslot *pNext; /* Next unused scratch buffer */ +-} ScratchFreeslot; +- + /* + ** State information local to the memory allocation subsystem. + */ + static SQLITE_WSD struct Mem0Global { + sqlite3_mutex *mutex; /* Mutex to serialize access */ +- +- /* +- ** The alarm callback and its arguments. The mem0.mutex lock will +- ** be held while the callback is running. Recursive calls into +- ** the memory subsystem are allowed, but no new callbacks will be +- ** issued. +- */ +- sqlite3_int64 alarmThreshold; +- void (*alarmCallback)(void*, sqlite3_int64,int); +- void *alarmArg; +- +- /* +- ** Pointers to the end of sqlite3GlobalConfig.pScratch memory +- ** (so that a range test can be used to determine if an allocation +- ** being freed came from pScratch) and a pointer to the list of +- ** unused scratch allocations. +- */ +- void *pScratchEnd; +- ScratchFreeslot *pScratchFree; +- u32 nScratchFree; ++ sqlite3_int64 alarmThreshold; /* The soft heap limit */ + + /* + ** True if heap is nearly "full" where "full" is defined by the + ** sqlite3_soft_heap_limit() setting. + */ + int nearlyFull; +-} mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 }; ++} mem0 = { 0, 0, 0 }; + + #define mem0 GLOBAL(struct Mem0Global, mem0) + +@@ -20596,50 +26566,21 @@ SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){ + return mem0.mutex; + } + +-/* +-** This routine runs when the memory allocator sees that the +-** total memory allocation is about to exceed the soft heap +-** limit. +-*/ +-static void softHeapLimitEnforcer( +- void *NotUsed, +- sqlite3_int64 NotUsed2, +- int allocSize +-){ +- UNUSED_PARAMETER2(NotUsed, NotUsed2); +- sqlite3_release_memory(allocSize); +-} +- +-/* +-** Change the alarm callback +-*/ +-static int sqlite3MemoryAlarm( +- void(*xCallback)(void *pArg, sqlite3_int64 used,int N), +- void *pArg, +- sqlite3_int64 iThreshold +-){ +- sqlite3_int64 nUsed; +- sqlite3_mutex_enter(mem0.mutex); +- mem0.alarmCallback = xCallback; +- mem0.alarmArg = pArg; +- mem0.alarmThreshold = iThreshold; +- nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); +- mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed); +- sqlite3_mutex_leave(mem0.mutex); +- return SQLITE_OK; +-} +- + #ifndef SQLITE_OMIT_DEPRECATED + /* +-** Deprecated external interface. Internal/core SQLite code +-** should call sqlite3MemoryAlarm. ++** Deprecated external interface. It used to set an alarm callback ++** that was invoked when memory usage grew too large. Now it is a ++** no-op. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_memory_alarm( ++SQLITE_API int sqlite3_memory_alarm( + void(*xCallback)(void *pArg, sqlite3_int64 used,int N), + void *pArg, + sqlite3_int64 iThreshold + ){ +- return sqlite3MemoryAlarm(xCallback, pArg, iThreshold); ++ (void)xCallback; ++ (void)pArg; ++ (void)iThreshold; ++ return SQLITE_OK; + } + #endif + +@@ -20647,27 +26588,29 @@ SQLITE_API int SQLITE_STDCALL sqlite3_memory_alarm( + ** Set the soft heap-size limit for the library. Passing a zero or + ** negative value indicates no limit. + */ +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_soft_heap_limit64(sqlite3_int64 n){ ++SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){ + sqlite3_int64 priorLimit; + sqlite3_int64 excess; ++ sqlite3_int64 nUsed; + #ifndef SQLITE_OMIT_AUTOINIT + int rc = sqlite3_initialize(); + if( rc ) return -1; + #endif + sqlite3_mutex_enter(mem0.mutex); + priorLimit = mem0.alarmThreshold; +- sqlite3_mutex_leave(mem0.mutex); +- if( n<0 ) return priorLimit; +- if( n>0 ){ +- sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n); +- }else{ +- sqlite3MemoryAlarm(0, 0, 0); ++ if( n<0 ){ ++ sqlite3_mutex_leave(mem0.mutex); ++ return priorLimit; + } ++ mem0.alarmThreshold = n; ++ nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); ++ mem0.nearlyFull = (n>0 && n<=nUsed); ++ sqlite3_mutex_leave(mem0.mutex); + excess = sqlite3_memory_used() - n; + if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff)); + return priorLimit; + } +-SQLITE_API void SQLITE_STDCALL sqlite3_soft_heap_limit(int n){ ++SQLITE_API void sqlite3_soft_heap_limit(int n){ + if( n<0 ) n = 0; + sqlite3_soft_heap_limit64(n); + } +@@ -20681,36 +26624,11 @@ SQLITE_PRIVATE int sqlite3MallocInit(void){ + sqlite3MemSetDefault(); + } + memset(&mem0, 0, sizeof(mem0)); +- if( sqlite3GlobalConfig.bCoreMutex ){ +- mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); +- } +- if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100 +- && sqlite3GlobalConfig.nScratch>0 ){ +- int i, n, sz; +- ScratchFreeslot *pSlot; +- sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch); +- sqlite3GlobalConfig.szScratch = sz; +- pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch; +- n = sqlite3GlobalConfig.nScratch; +- mem0.pScratchFree = pSlot; +- mem0.nScratchFree = n; +- for(i=0; i pNext = (ScratchFreeslot*)(sz+(char*)pSlot); +- pSlot = pSlot->pNext; +- } +- pSlot->pNext = 0; +- mem0.pScratchEnd = (void*)&pSlot[1]; +- }else{ +- mem0.pScratchEnd = 0; +- sqlite3GlobalConfig.pScratch = 0; +- sqlite3GlobalConfig.szScratch = 0; +- sqlite3GlobalConfig.nScratch = 0; +- } ++ mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); + if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512 +- || sqlite3GlobalConfig.nPage<1 ){ ++ || sqlite3GlobalConfig.nPage<=0 ){ + sqlite3GlobalConfig.pPage = 0; + sqlite3GlobalConfig.szPage = 0; +- sqlite3GlobalConfig.nPage = 0; + } + rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); + if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0)); +@@ -20739,11 +26657,9 @@ SQLITE_PRIVATE void sqlite3MallocEnd(void){ + /* + ** Return the amount of memory currently checked out. + */ +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_used(void){ +- int n, mx; +- sqlite3_int64 res; +- sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0); +- res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */ ++SQLITE_API sqlite3_int64 sqlite3_memory_used(void){ ++ sqlite3_int64 res, mx; ++ sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0); + return res; + } + +@@ -20752,44 +26668,48 @@ SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_used(void){ + ** checked out since either the beginning of this process + ** or since the most recent reset. + */ +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag){ +- int n, mx; +- sqlite3_int64 res; +- sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag); +- res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */ +- return res; ++SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ ++ sqlite3_int64 res, mx; ++ sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag); ++ return mx; + } + + /* + ** Trigger the alarm + */ + static void sqlite3MallocAlarm(int nByte){ +- void (*xCallback)(void*,sqlite3_int64,int); +- sqlite3_int64 nowUsed; +- void *pArg; +- if( mem0.alarmCallback==0 ) return; +- xCallback = mem0.alarmCallback; +- nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); +- pArg = mem0.alarmArg; +- mem0.alarmCallback = 0; ++ if( mem0.alarmThreshold<=0 ) return; + sqlite3_mutex_leave(mem0.mutex); +- xCallback(pArg, nowUsed, nByte); ++ sqlite3_release_memory(nByte); + sqlite3_mutex_enter(mem0.mutex); +- mem0.alarmCallback = xCallback; +- mem0.alarmArg = pArg; + } + + /* + ** Do a memory allocation with statistics and alarms. Assume the + ** lock is already held. + */ +-static int mallocWithAlarm(int n, void **pp){ +- int nFull; ++static void mallocWithAlarm(int n, void **pp){ + void *p; ++ int nFull; + assert( sqlite3_mutex_held(mem0.mutex) ); ++ assert( n>0 ); ++ ++ /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal ++ ** implementation of malloc_good_size(), which must be called in debug ++ ** mode and specifically when the DMD "Dark Matter Detector" is enabled ++ ** or else a crash results. Hence, do not attempt to optimize out the ++ ** following xRoundup() call. */ + nFull = sqlite3GlobalConfig.m.xRoundup(n); +- sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); +- if( mem0.alarmCallback!=0 ){ ++ ++#ifdef SQLITE_MAX_MEMORY ++ if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nFull>SQLITE_MAX_MEMORY ){ ++ *pp = 0; ++ return; ++ } ++#endif ++ ++ sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n); ++ if( mem0.alarmThreshold>0 ){ + sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); + if( nUsed >= mem0.alarmThreshold - nFull ){ + mem0.nearlyFull = 1; +@@ -20800,7 +26720,7 @@ static int mallocWithAlarm(int n, void **pp){ + } + p = sqlite3GlobalConfig.m.xMalloc(nFull); + #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT +- if( p==0 && mem0.alarmCallback ){ ++ if( p==0 && mem0.alarmThreshold>0 ){ + sqlite3MallocAlarm(nFull); + p = sqlite3GlobalConfig.m.xMalloc(nFull); + } +@@ -20811,7 +26731,6 @@ static int mallocWithAlarm(int n, void **pp){ + sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1); + } + *pp = p; +- return nFull; + } + + /* +@@ -20843,124 +26762,25 @@ SQLITE_PRIVATE void *sqlite3Malloc(u64 n){ + ** First make sure the memory subsystem is initialized, then do the + ** allocation. + */ +-SQLITE_API void *SQLITE_STDCALL sqlite3_malloc(int n){ ++SQLITE_API void *sqlite3_malloc(int n){ + #ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; + #endif + return n<=0 ? 0 : sqlite3Malloc(n); + } +-SQLITE_API void *SQLITE_STDCALL sqlite3_malloc64(sqlite3_uint64 n){ ++SQLITE_API void *sqlite3_malloc64(sqlite3_uint64 n){ + #ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; + #endif + return sqlite3Malloc(n); + } + +-/* +-** Each thread may only have a single outstanding allocation from +-** xScratchMalloc(). We verify this constraint in the single-threaded +-** case by setting scratchAllocOut to 1 when an allocation +-** is outstanding clearing it when the allocation is freed. +-*/ +-#if SQLITE_THREADSAFE==0 && !defined(NDEBUG) +-static int scratchAllocOut = 0; +-#endif +- +- +-/* +-** Allocate memory that is to be used and released right away. +-** This routine is similar to alloca() in that it is not intended +-** for situations where the memory might be held long-term. This +-** routine is intended to get memory to old large transient data +-** structures that would not normally fit on the stack of an +-** embedded processor. +-*/ +-SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){ +- void *p; +- assert( n>0 ); +- +- sqlite3_mutex_enter(mem0.mutex); +- sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); +- if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ +- p = mem0.pScratchFree; +- mem0.pScratchFree = mem0.pScratchFree->pNext; +- mem0.nScratchFree--; +- sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1); +- sqlite3_mutex_leave(mem0.mutex); +- }else{ +- sqlite3_mutex_leave(mem0.mutex); +- p = sqlite3Malloc(n); +- if( sqlite3GlobalConfig.bMemstat && p ){ +- sqlite3_mutex_enter(mem0.mutex); +- sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p)); +- sqlite3_mutex_leave(mem0.mutex); +- } +- sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); +- } +- assert( sqlite3_mutex_notheld(mem0.mutex) ); +- +- +-#if SQLITE_THREADSAFE==0 && !defined(NDEBUG) +- /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch +- ** buffers per thread. +- ** +- ** This can only be checked in single-threaded mode. +- */ +- assert( scratchAllocOut==0 ); +- if( p ) scratchAllocOut++; +-#endif +- +- return p; +-} +-SQLITE_PRIVATE void sqlite3ScratchFree(void *p){ +- if( p ){ +- +-#if SQLITE_THREADSAFE==0 && !defined(NDEBUG) +- /* Verify that no more than two scratch allocation per thread +- ** is outstanding at one time. (This is only checked in the +- ** single-threaded case since checking in the multi-threaded case +- ** would be much more complicated.) */ +- assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); +- scratchAllocOut--; +-#endif +- +- if( p>=sqlite3GlobalConfig.pScratch && p pNext = mem0.pScratchFree; +- mem0.pScratchFree = pSlot; +- mem0.nScratchFree++; +- assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch ); +- sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1); +- sqlite3_mutex_leave(mem0.mutex); +- }else{ +- /* Release memory back to the heap */ +- assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); +- assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) ); +- sqlite3MemdebugSetType(p, MEMTYPE_HEAP); +- if( sqlite3GlobalConfig.bMemstat ){ +- int iSize = sqlite3MallocSize(p); +- sqlite3_mutex_enter(mem0.mutex); +- sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize); +- sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize); +- sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); +- sqlite3GlobalConfig.m.xFree(p); +- sqlite3_mutex_leave(mem0.mutex); +- }else{ +- sqlite3GlobalConfig.m.xFree(p); +- } +- } +- } +-} +- + /* + ** TRUE if p is a lookaside memory allocation from db + */ + #ifndef SQLITE_OMIT_LOOKASIDE + static int isLookaside(sqlite3 *db, void *p){ +- return p>=db->lookaside.pStart && p lookaside.pEnd; ++ return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd); + } + #else + #define isLookaside(A,B) 0 +@@ -20975,31 +26795,33 @@ SQLITE_PRIVATE int sqlite3MallocSize(void *p){ + return sqlite3GlobalConfig.m.xSize(p); + } + SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){ +- if( db==0 ){ +- assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); +- assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); +- return sqlite3MallocSize(p); +- }else{ +- assert( sqlite3_mutex_held(db->mutex) ); +- if( isLookaside(db, p) ){ +- return db->lookaside.sz; ++ assert( p!=0 ); ++ if( db==0 || !isLookaside(db,p) ){ ++#ifdef SQLITE_DEBUG ++ if( db==0 ){ ++ assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); ++ assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); + }else{ + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); + assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); +- return sqlite3GlobalConfig.m.xSize(p); + } ++#endif ++ return sqlite3GlobalConfig.m.xSize(p); ++ }else{ ++ assert( sqlite3_mutex_held(db->mutex) ); ++ return db->lookaside.sz; + } + } +-SQLITE_API sqlite3_uint64 SQLITE_STDCALL sqlite3_msize(void *p){ ++SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){ + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); +- return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p); ++ return p ? sqlite3GlobalConfig.m.xSize(p) : 0; + } + + /* + ** Free memory previously obtained from sqlite3Malloc(). + */ +-SQLITE_API void SQLITE_STDCALL sqlite3_free(void *p){ ++SQLITE_API void sqlite3_free(void *p){ + if( p==0 ) return; /* IMP: R-49053-54554 */ + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); +@@ -21024,11 +26846,12 @@ static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){ + + /* + ** Free memory that might be associated with a particular database +-** connection. ++** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op. ++** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL. + */ +-SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){ ++SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3 *db, void *p){ + assert( db==0 || sqlite3_mutex_held(db->mutex) ); +- if( p==0 ) return; ++ assert( p!=0 ); + if( db ){ + if( db->pnBytesFreed ){ + measureAllocationSize(db, p); +@@ -21036,13 +26859,12 @@ SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){ + } + if( isLookaside(db, p) ){ + LookasideSlot *pBuf = (LookasideSlot*)p; +-#if SQLITE_DEBUG ++#ifdef SQLITE_DEBUG + /* Trash all content in the buffer being freed */ + memset(p, 0xaa, db->lookaside.sz); + #endif + pBuf->pNext = db->lookaside.pFree; + db->lookaside.pFree = pBuf; +- db->lookaside.nOut--; + return; + } + } +@@ -21052,6 +26874,10 @@ SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){ + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); + sqlite3_free(p); + } ++SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){ ++ assert( db==0 || sqlite3_mutex_held(db->mutex) ); ++ if( p ) sqlite3DbFreeNN(db, p); ++} + + /* + ** Change the size of an existing memory allocation +@@ -21081,14 +26907,14 @@ SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){ + pNew = pOld; + }else if( sqlite3GlobalConfig.bMemstat ){ + sqlite3_mutex_enter(mem0.mutex); +- sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes); ++ sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes); + nDiff = nNew - nOld; +- if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= ++ if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= + mem0.alarmThreshold-nDiff ){ + sqlite3MallocAlarm(nDiff); + } + pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); +- if( pNew==0 && mem0.alarmCallback ){ ++ if( pNew==0 && mem0.alarmThreshold>0 ){ + sqlite3MallocAlarm((int)nBytes); + pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); + } +@@ -21108,14 +26934,14 @@ SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){ + ** The public interface to sqlite3Realloc. Make sure that the memory + ** subsystem is initialized prior to invoking sqliteRealloc. + */ +-SQLITE_API void *SQLITE_STDCALL sqlite3_realloc(void *pOld, int n){ ++SQLITE_API void *sqlite3_realloc(void *pOld, int n){ + #ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; + #endif + if( n<0 ) n = 0; /* IMP: R-26507-47431 */ + return sqlite3Realloc(pOld, n); + } +-SQLITE_API void *SQLITE_STDCALL sqlite3_realloc64(void *pOld, sqlite3_uint64 n){ ++SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){ + #ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; + #endif +@@ -21139,16 +26965,31 @@ SQLITE_PRIVATE void *sqlite3MallocZero(u64 n){ + ** the mallocFailed flag in the connection pointer. + */ + SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){ +- void *p = sqlite3DbMallocRaw(db, n); +- if( p ){ +- memset(p, 0, (size_t)n); +- } ++ void *p; ++ testcase( db==0 ); ++ p = sqlite3DbMallocRaw(db, n); ++ if( p ) memset(p, 0, (size_t)n); ++ return p; ++} ++ ++ ++/* Finish the work of sqlite3DbMallocRawNN for the unusual and ++** slower case when the allocation cannot be fulfilled using lookaside. ++*/ ++static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){ ++ void *p; ++ assert( db!=0 ); ++ p = sqlite3Malloc(n); ++ if( !p ) sqlite3OomFault(db); ++ sqlite3MemdebugSetType(p, ++ (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); + return p; + } + + /* +-** Allocate and zero memory. If the allocation fails, make +-** the mallocFailed flag in the connection pointer. ++** Allocate memory, either lookaside (if possible) or heap. ++** If the allocation fails, set the mallocFailed flag in ++** the connection pointer. + ** + ** If db!=0 and db->mallocFailed is true (indicating a prior malloc + ** failure on the same database connection) then always return 0. +@@ -21163,64 +27004,73 @@ SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){ + ** + ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed + ** that all prior mallocs (ex: "a") worked too. ++** ++** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is ++** not a NULL pointer. + */ + SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){ + void *p; +- assert( db==0 || sqlite3_mutex_held(db->mutex) ); +- assert( db==0 || db->pnBytesFreed==0 ); ++ if( db ) return sqlite3DbMallocRawNN(db, n); ++ p = sqlite3Malloc(n); ++ sqlite3MemdebugSetType(p, MEMTYPE_HEAP); ++ return p; ++} ++SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){ + #ifndef SQLITE_OMIT_LOOKASIDE +- if( db ){ +- LookasideSlot *pBuf; +- if( db->mallocFailed ){ +- return 0; +- } +- if( db->lookaside.bEnabled ){ +- if( n>db->lookaside.sz ){ +- db->lookaside.anStat[1]++; +- }else if( (pBuf = db->lookaside.pFree)==0 ){ +- db->lookaside.anStat[2]++; +- }else{ +- db->lookaside.pFree = pBuf->pNext; +- db->lookaside.nOut++; +- db->lookaside.anStat[0]++; +- if( db->lookaside.nOut>db->lookaside.mxOut ){ +- db->lookaside.mxOut = db->lookaside.nOut; +- } +- return (void*)pBuf; +- } ++ LookasideSlot *pBuf; ++ assert( db!=0 ); ++ assert( sqlite3_mutex_held(db->mutex) ); ++ assert( db->pnBytesFreed==0 ); ++ if( db->lookaside.bDisable==0 ){ ++ assert( db->mallocFailed==0 ); ++ if( n>db->lookaside.sz ){ ++ db->lookaside.anStat[1]++; ++ }else if( (pBuf = db->lookaside.pFree)!=0 ){ ++ db->lookaside.pFree = pBuf->pNext; ++ db->lookaside.anStat[0]++; ++ return (void*)pBuf; ++ }else if( (pBuf = db->lookaside.pInit)!=0 ){ ++ db->lookaside.pInit = pBuf->pNext; ++ db->lookaside.anStat[0]++; ++ return (void*)pBuf; ++ }else{ ++ db->lookaside.anStat[2]++; + } ++ }else if( db->mallocFailed ){ ++ return 0; + } + #else +- if( db && db->mallocFailed ){ ++ assert( db!=0 ); ++ assert( sqlite3_mutex_held(db->mutex) ); ++ assert( db->pnBytesFreed==0 ); ++ if( db->mallocFailed ){ + return 0; + } + #endif +- p = sqlite3Malloc(n); +- if( !p && db ){ +- db->mallocFailed = 1; +- } +- sqlite3MemdebugSetType(p, +- (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); +- return p; ++ return dbMallocRawFinish(db, n); + } + ++/* Forward declaration */ ++static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n); ++ + /* + ** Resize the block of memory pointed to by p to n bytes. If the + ** resize fails, set the mallocFailed flag in the connection object. + */ + SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){ +- void *pNew = 0; + assert( db!=0 ); ++ if( p==0 ) return sqlite3DbMallocRawNN(db, n); + assert( sqlite3_mutex_held(db->mutex) ); ++ if( isLookaside(db,p) && n<=db->lookaside.sz ) return p; ++ return dbReallocFinish(db, p, n); ++} ++static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){ ++ void *pNew = 0; ++ assert( db!=0 ); ++ assert( p!=0 ); + if( db->mallocFailed==0 ){ +- if( p==0 ){ +- return sqlite3DbMallocRaw(db, n); +- } + if( isLookaside(db, p) ){ +- if( n<=db->lookaside.sz ){ +- return p; +- } +- pNew = sqlite3DbMallocRaw(db, n); ++ pNew = sqlite3DbMallocRawNN(db, n); + if( pNew ){ + memcpy(pNew, p, db->lookaside.sz); + sqlite3DbFree(db, p); +@@ -21231,10 +27081,10 @@ SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){ + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); + pNew = sqlite3_realloc64(p, n); + if( !pNew ){ +- db->mallocFailed = 1; ++ sqlite3OomFault(db); + } + sqlite3MemdebugSetType(pNew, +- (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); ++ (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); + } + } + return pNew; +@@ -21266,9 +27116,8 @@ SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){ + if( z==0 ){ + return 0; + } +- n = sqlite3Strlen30(z) + 1; +- assert( (n&0x7fffffff)==n ); +- zNew = sqlite3DbMallocRaw(db, (int)n); ++ n = strlen(z) + 1; ++ zNew = sqlite3DbMallocRaw(db, n); + if( zNew ){ + memcpy(zNew, z, n); + } +@@ -21276,11 +27125,12 @@ SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){ + } + SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){ + char *zNew; ++ assert( db!=0 ); + if( z==0 ){ + return 0; + } + assert( (n&0x7fffffff)==n ); +- zNew = sqlite3DbMallocRaw(db, n+1); ++ zNew = sqlite3DbMallocRawNN(db, n+1); + if( zNew ){ + memcpy(zNew, z, (size_t)n); + zNew[n] = 0; +@@ -21289,28 +27139,68 @@ SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){ + } + + /* +-** Create a string from the zFromat argument and the va_list that follows. +-** Store the string in memory obtained from sqliteMalloc() and make *pz +-** point to that string. ++** The text between zStart and zEnd represents a phrase within a larger ++** SQL statement. Make a copy of this phrase in space obtained form ++** sqlite3DbMalloc(). Omit leading and trailing whitespace. + */ +-SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){ +- va_list ap; +- char *z; ++SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){ ++ int n; ++ while( sqlite3Isspace(zStart[0]) ) zStart++; ++ n = (int)(zEnd - zStart); ++ while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--; ++ return sqlite3DbStrNDup(db, zStart, n); ++} + +- va_start(ap, zFormat); +- z = sqlite3VMPrintf(db, zFormat, ap); +- va_end(ap); ++/* ++** Free any prior content in *pz and replace it with a copy of zNew. ++*/ ++SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){ + sqlite3DbFree(db, *pz); +- *pz = z; ++ *pz = sqlite3DbStrDup(db, zNew); ++} ++ ++/* ++** Call this routine to record the fact that an OOM (out-of-memory) error ++** has happened. This routine will set db->mallocFailed, and also ++** temporarily disable the lookaside memory allocator and interrupt ++** any running VDBEs. ++*/ ++SQLITE_PRIVATE void sqlite3OomFault(sqlite3 *db){ ++ if( db->mallocFailed==0 && db->bBenignMalloc==0 ){ ++ db->mallocFailed = 1; ++ if( db->nVdbeExec>0 ){ ++ db->u1.isInterrupted = 1; ++ } ++ db->lookaside.bDisable++; ++ if( db->pParse ){ ++ db->pParse->rc = SQLITE_NOMEM_BKPT; ++ } ++ } ++} ++ ++/* ++** This routine reactivates the memory allocator and clears the ++** db->mallocFailed flag as necessary. ++** ++** The memory allocator is not restarted if there are running ++** VDBEs. ++*/ ++SQLITE_PRIVATE void sqlite3OomClear(sqlite3 *db){ ++ if( db->mallocFailed && db->nVdbeExec==0 ){ ++ db->mallocFailed = 0; ++ db->u1.isInterrupted = 0; ++ assert( db->lookaside.bDisable>0 ); ++ db->lookaside.bDisable--; ++ } + } + + /* + ** Take actions at the end of an API call to indicate an OOM error + */ + static SQLITE_NOINLINE int apiOomError(sqlite3 *db){ +- db->mallocFailed = 0; ++ sqlite3OomClear(db); + sqlite3Error(db, SQLITE_NOMEM); +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + + /* +@@ -21322,17 +27212,16 @@ static SQLITE_NOINLINE int apiOomError(sqlite3 *db){ + ** function. However, if a malloc() failure has occurred since the previous + ** invocation SQLITE_NOMEM is returned instead. + ** +-** If the first argument, db, is not NULL and a malloc() error has occurred, +-** then the connection error-code (the value returned by sqlite3_errcode()) +-** is set to SQLITE_NOMEM. ++** If an OOM as occurred, then the connection error-code (the value ++** returned by sqlite3_errcode()) is set to SQLITE_NOMEM. + */ + SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){ +- /* If the db handle is not NULL, then we must hold the connection handle +- ** mutex here. Otherwise the read (and possible write) of db->mallocFailed ++ /* If the db handle must hold the connection handle mutex here. ++ ** Otherwise the read (and possible write) of db->mallocFailed + ** is unsafe, as is the call to sqlite3Error(). + */ +- assert( !db || sqlite3_mutex_held(db->mutex) ); +- if( db==0 ) return rc & 0xff; ++ assert( db!=0 ); ++ assert( sqlite3_mutex_held(db->mutex) ); + if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ + return apiOomError(db); + } +@@ -21343,43 +27232,42 @@ SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){ + /************** Begin file printf.c ******************************************/ + /* + ** The "printf" code that follows dates from the 1980's. It is in +-** the public domain. The original comments are included here for +-** completeness. They are very out-of-date but might be useful as +-** an historical reference. Most of the "enhancements" have been backed +-** out so that the functionality is now the same as standard printf(). ++** the public domain. + ** + ************************************************************************** + ** + ** This file contains code for a set of "printf"-like routines. These + ** routines format strings much like the printf() from the standard C + ** library, though the implementation here has enhancements to support +-** SQLlite. ++** SQLite. + */ ++/* #include "sqliteInt.h" */ + + /* + ** Conversion types fall into various categories as defined by the + ** following enumeration. + */ +-#define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */ +-#define etFLOAT 2 /* Floating point. %f */ +-#define etEXP 3 /* Exponentional notation. %e and %E */ +-#define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */ +-#define etSIZE 5 /* Return number of characters processed so far. %n */ +-#define etSTRING 6 /* Strings. %s */ +-#define etDYNSTRING 7 /* Dynamically allocated strings. %z */ +-#define etPERCENT 8 /* Percent symbol. %% */ +-#define etCHARX 9 /* Characters. %c */ ++#define etRADIX 0 /* non-decimal integer types. %x %o */ ++#define etFLOAT 1 /* Floating point. %f */ ++#define etEXP 2 /* Exponentional notation. %e and %E */ ++#define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */ ++#define etSIZE 4 /* Return number of characters processed so far. %n */ ++#define etSTRING 5 /* Strings. %s */ ++#define etDYNSTRING 6 /* Dynamically allocated strings. %z */ ++#define etPERCENT 7 /* Percent symbol. %% */ ++#define etCHARX 8 /* Characters. %c */ + /* The rest are extensions, not normally found in printf() */ +-#define etSQLESCAPE 10 /* Strings with '\'' doubled. %q */ +-#define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '', ++#define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */ ++#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '', + NULL pointers replaced by SQL NULL. %Q */ +-#define etTOKEN 12 /* a pointer to a Token structure */ +-#define etSRCLIST 13 /* a pointer to a SrcList */ +-#define etPOINTER 14 /* The %p conversion */ +-#define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */ +-#define etORDINAL 16 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ ++#define etTOKEN 11 /* a pointer to a Token structure */ ++#define etSRCLIST 12 /* a pointer to a SrcList */ ++#define etPOINTER 13 /* The %p conversion */ ++#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */ ++#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ ++#define etDECIMAL 16 /* %d or %u, but not %x, %o */ + +-#define etINVALID 0 /* Any unrecognized conversion type */ ++#define etINVALID 17 /* Any unrecognized conversion type */ + + + /* +@@ -21403,9 +27291,8 @@ typedef struct et_info { /* Information about each format field */ + /* + ** Allowed values for et_info.flags + */ +-#define FLAG_SIGNED 1 /* True if the value to convert is signed */ +-#define FLAG_INTERN 2 /* True if for internal use only */ +-#define FLAG_STRING 4 /* Allow infinity precision */ ++#define FLAG_SIGNED 1 /* True if the value to convert is signed */ ++#define FLAG_STRING 4 /* Allow infinite precision */ + + + /* +@@ -21415,7 +27302,7 @@ typedef struct et_info { /* Information about each format field */ + static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; + static const char aPrefix[] = "-x0\000X0"; + static const et_info fmtinfo[] = { +- { 'd', 10, 1, etRADIX, 0, 0 }, ++ { 'd', 10, 1, etDECIMAL, 0, 0 }, + { 's', 0, 4, etSTRING, 0, 0 }, + { 'g', 0, 1, etGENERIC, 30, 0 }, + { 'z', 0, 4, etDYNSTRING, 0, 0 }, +@@ -21424,7 +27311,7 @@ static const et_info fmtinfo[] = { + { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, + { 'c', 0, 0, etCHARX, 0, 0 }, + { 'o', 8, 0, etRADIX, 0, 2 }, +- { 'u', 10, 0, etRADIX, 0, 0 }, ++ { 'u', 10, 0, etDECIMAL, 0, 0 }, + { 'x', 16, 0, etRADIX, 16, 1 }, + { 'X', 16, 0, etRADIX, 0, 4 }, + #ifndef SQLITE_OMIT_FLOATING_POINT +@@ -21433,16 +27320,15 @@ static const et_info fmtinfo[] = { + { 'E', 0, 1, etEXP, 14, 0 }, + { 'G', 0, 1, etGENERIC, 14, 0 }, + #endif +- { 'i', 10, 1, etRADIX, 0, 0 }, ++ { 'i', 10, 1, etDECIMAL, 0, 0 }, + { 'n', 0, 0, etSIZE, 0, 0 }, + { '%', 0, 0, etPERCENT, 0, 0 }, + { 'p', 16, 0, etPOINTER, 0, 1 }, + +-/* All the rest have the FLAG_INTERN bit set and are thus for internal +-** use only */ +- { 'T', 0, 2, etTOKEN, 0, 0 }, +- { 'S', 0, 2, etSRCLIST, 0, 0 }, +- { 'r', 10, 3, etORDINAL, 0, 0 }, ++ /* All the rest are undocumented and are for internal use only */ ++ { 'T', 0, 0, etTOKEN, 0, 0 }, ++ { 'S', 0, 0, etSRCLIST, 0, 0 }, ++ { 'r', 10, 1, etORDINAL, 0, 0 }, + }; + + /* +@@ -21480,9 +27366,10 @@ static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ + ** Set the StrAccum object to an error mode. + */ + static void setStrAccumError(StrAccum *p, u8 eError){ +- assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG ); ++ assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG ); + p->accError = eError; +- p->nAlloc = 0; ++ if( p->mxAlloc ) sqlite3_str_reset(p); ++ if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError); + } + + /* +@@ -21501,6 +27388,28 @@ static char *getTextArg(PrintfArguments *p){ + return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); + } + ++/* ++** Allocate memory for a temporary buffer needed for printf rendering. ++** ++** If the requested size of the temp buffer is larger than the size ++** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error. ++** Do the size check before the memory allocation to prevent rogue ++** SQL from requesting large allocations using the precision or width ++** field of the printf() function. ++*/ ++static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){ ++ char *z; ++ if( pAccum->accError ) return 0; ++ if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){ ++ setStrAccumError(pAccum, SQLITE_TOOBIG); ++ return 0; ++ } ++ z = sqlite3DbMallocRaw(pAccum->db, n); ++ if( z==0 ){ ++ setStrAccumError(pAccum, SQLITE_NOMEM); ++ } ++ return z; ++} + + /* + ** On machines with a small stack size, you can redefine the +@@ -21514,9 +27423,8 @@ static char *getTextArg(PrintfArguments *p){ + /* + ** Render a string given by "fmt" into the StrAccum object. + */ +-SQLITE_PRIVATE void sqlite3VXPrintf( +- StrAccum *pAccum, /* Accumulate results here */ +- u32 bFlags, /* SQLITE_PRINTF_* flags */ ++SQLITE_API void sqlite3_str_vappendf( ++ sqlite3_str *pAccum, /* Accumulate results here */ + const char *fmt, /* Format string */ + va_list ap /* arguments */ + ){ +@@ -21527,17 +27435,15 @@ SQLITE_PRIVATE void sqlite3VXPrintf( + int idx; /* A general purpose loop counter */ + int width; /* Width of the current field */ + etByte flag_leftjustify; /* True if "-" flag is present */ +- etByte flag_plussign; /* True if "+" flag is present */ +- etByte flag_blanksign; /* True if " " flag is present */ ++ etByte flag_prefix; /* '+' or ' ' or 0 for prefix */ + etByte flag_alternateform; /* True if "#" flag is present */ + etByte flag_altform2; /* True if "!" flag is present */ + etByte flag_zeropad; /* True if field width constant starts with zero */ +- etByte flag_long; /* True if "l" flag is present */ +- etByte flag_longlong; /* True if the "ll" flag is present */ ++ etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */ + etByte done; /* Loop termination flag */ +- etByte xtype = 0; /* Conversion paradigm */ ++ etByte cThousand; /* Thousands separator for %d and %u */ ++ etByte xtype = etINVALID; /* Conversion paradigm */ + u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ +- u8 useIntern; /* Ok to use internal conversions (ex: %T) */ + char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ + sqlite_uint64 longvalue; /* Value for integer types */ + LONGDOUBLE_TYPE realvalue; /* Value for real types */ +@@ -21555,14 +27461,17 @@ SQLITE_PRIVATE void sqlite3VXPrintf( + PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ + char buf[etBUFSIZE]; /* Conversion buffer */ + ++ /* pAccum never starts out with an empty buffer that was obtained from ++ ** malloc(). This precondition is required by the mprintf("%z...") ++ ** optimization. */ ++ assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); ++ + bufpt = 0; +- if( bFlags ){ +- if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){ +- pArgList = va_arg(ap, PrintfArguments*); +- } +- useIntern = bFlags & SQLITE_PRINTF_INTERNAL; ++ if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){ ++ pArgList = va_arg(ap, PrintfArguments*); ++ bArgList = 1; + }else{ +- bArgList = useIntern = 0; ++ bArgList = 0; + } + for(; (c=(*fmt))!=0; ++fmt){ + if( c!='%' ){ +@@ -21572,99 +27481,124 @@ SQLITE_PRIVATE void sqlite3VXPrintf( + #else + do{ fmt++; }while( *fmt && *fmt != '%' ); + #endif +- sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt)); ++ sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt)); + if( *fmt==0 ) break; + } + if( (c=(*++fmt))==0 ){ +- sqlite3StrAccumAppend(pAccum, "%", 1); ++ sqlite3_str_append(pAccum, "%", 1); + break; + } + /* Find out what flags are present */ +- flag_leftjustify = flag_plussign = flag_blanksign = ++ flag_leftjustify = flag_prefix = cThousand = + flag_alternateform = flag_altform2 = flag_zeropad = 0; + done = 0; ++ width = 0; ++ flag_long = 0; ++ precision = -1; + do{ + switch( c ){ + case '-': flag_leftjustify = 1; break; +- case '+': flag_plussign = 1; break; +- case ' ': flag_blanksign = 1; break; ++ case '+': flag_prefix = '+'; break; ++ case ' ': flag_prefix = ' '; break; + case '#': flag_alternateform = 1; break; + case '!': flag_altform2 = 1; break; + case '0': flag_zeropad = 1; break; ++ case ',': cThousand = ','; break; + default: done = 1; break; +- } +- }while( !done && (c=(*++fmt))!=0 ); +- /* Get the field width */ +- if( c=='*' ){ +- if( bArgList ){ +- width = (int)getIntArg(pArgList); +- }else{ +- width = va_arg(ap,int); +- } +- if( width<0 ){ +- flag_leftjustify = 1; +- width = width >= -2147483647 ? -width : 0; +- } +- c = *++fmt; +- }else{ +- unsigned wx = 0; +- while( c>='0' && c<='9' ){ +- wx = wx*10 + c - '0'; +- c = *++fmt; +- } +- testcase( wx>0x7fffffff ); +- width = wx & 0x7fffffff; +- } +- +- /* Get the precision */ +- if( c=='.' ){ +- c = *++fmt; +- if( c=='*' ){ +- if( bArgList ){ +- precision = (int)getIntArg(pArgList); +- }else{ +- precision = va_arg(ap,int); ++ case 'l': { ++ flag_long = 1; ++ c = *++fmt; ++ if( c=='l' ){ ++ c = *++fmt; ++ flag_long = 2; ++ } ++ done = 1; ++ break; + } +- c = *++fmt; +- if( precision<0 ){ +- precision = precision >= -2147483647 ? -precision : -1; ++ case '1': case '2': case '3': case '4': case '5': ++ case '6': case '7': case '8': case '9': { ++ unsigned wx = c - '0'; ++ while( (c = *++fmt)>='0' && c<='9' ){ ++ wx = wx*10 + c - '0'; ++ } ++ testcase( wx>0x7fffffff ); ++ width = wx & 0x7fffffff; ++#ifdef SQLITE_PRINTF_PRECISION_LIMIT ++ if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ ++ width = SQLITE_PRINTF_PRECISION_LIMIT; ++ } ++#endif ++ if( c!='.' && c!='l' ){ ++ done = 1; ++ }else{ ++ fmt--; ++ } ++ break; + } +- }else{ +- unsigned px = 0; +- while( c>='0' && c<='9' ){ +- px = px*10 + c - '0'; ++ case '*': { ++ if( bArgList ){ ++ width = (int)getIntArg(pArgList); ++ }else{ ++ width = va_arg(ap,int); ++ } ++ if( width<0 ){ ++ flag_leftjustify = 1; ++ width = width >= -2147483647 ? -width : 0; ++ } ++#ifdef SQLITE_PRINTF_PRECISION_LIMIT ++ if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ ++ width = SQLITE_PRINTF_PRECISION_LIMIT; ++ } ++#endif ++ if( (c = fmt[1])!='.' && c!='l' ){ ++ c = *++fmt; ++ done = 1; ++ } ++ break; ++ } ++ case '.': { + c = *++fmt; ++ if( c=='*' ){ ++ if( bArgList ){ ++ precision = (int)getIntArg(pArgList); ++ }else{ ++ precision = va_arg(ap,int); ++ } ++ if( precision<0 ){ ++ precision = precision >= -2147483647 ? -precision : -1; ++ } ++ c = *++fmt; ++ }else{ ++ unsigned px = 0; ++ while( c>='0' && c<='9' ){ ++ px = px*10 + c - '0'; ++ c = *++fmt; ++ } ++ testcase( px>0x7fffffff ); ++ precision = px & 0x7fffffff; ++ } ++#ifdef SQLITE_PRINTF_PRECISION_LIMIT ++ if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){ ++ precision = SQLITE_PRINTF_PRECISION_LIMIT; ++ } ++#endif ++ if( c=='l' ){ ++ --fmt; ++ }else{ ++ done = 1; ++ } ++ break; + } +- testcase( px>0x7fffffff ); +- precision = px & 0x7fffffff; +- } +- }else{ +- precision = -1; +- } +- /* Get the conversion type modifier */ +- if( c=='l' ){ +- flag_long = 1; +- c = *++fmt; +- if( c=='l' ){ +- flag_longlong = 1; +- c = *++fmt; +- }else{ +- flag_longlong = 0; + } +- }else{ +- flag_long = flag_longlong = 0; +- } ++ }while( !done && (c=(*++fmt))!=0 ); ++ + /* Fetch the info entry for the field */ + infop = &fmtinfo[0]; + xtype = etINVALID; + for(idx=0; idx flags & FLAG_INTERN)==0 ){ +- xtype = infop->type; +- }else{ +- return; +- } ++ xtype = infop->type; + break; + } + } +@@ -21674,15 +27608,11 @@ SQLITE_PRIVATE void sqlite3VXPrintf( + ** + ** flag_alternateform TRUE if a '#' is present. + ** flag_altform2 TRUE if a '!' is present. +- ** flag_plussign TRUE if a '+' is present. ++ ** flag_prefix '+' or ' ' or zero + ** flag_leftjustify TRUE if a '-' is present or if the + ** field width was negative. + ** flag_zeropad TRUE if the width began with 0. +- ** flag_long TRUE if the letter 'l' (ell) prefixed +- ** the conversion character. +- ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed +- ** the conversion character. +- ** flag_blanksign TRUE if a ' ' is present. ++ ** flag_long 1 for "l", 2 for "ll" + ** width The specified field width. This is + ** always non-negative. Zero is the default. + ** precision The specified precision. The default +@@ -21692,19 +27622,24 @@ SQLITE_PRIVATE void sqlite3VXPrintf( + */ + switch( xtype ){ + case etPOINTER: +- flag_longlong = sizeof(char*)==sizeof(i64); +- flag_long = sizeof(char*)==sizeof(long int); ++ flag_long = sizeof(char*)==sizeof(i64) ? 2 : ++ sizeof(char*)==sizeof(long int) ? 1 : 0; + /* Fall through into the next case */ + case etORDINAL: +- case etRADIX: ++ case etRADIX: ++ cThousand = 0; ++ /* Fall through into the next case */ ++ case etDECIMAL: + if( infop->flags & FLAG_SIGNED ){ + i64 v; + if( bArgList ){ + v = getIntArg(pArgList); +- }else if( flag_longlong ){ +- v = va_arg(ap,i64); + }else if( flag_long ){ +- v = va_arg(ap,long int); ++ if( flag_long==2 ){ ++ v = va_arg(ap,i64) ; ++ }else{ ++ v = va_arg(ap,long int); ++ } + }else{ + v = va_arg(ap,int); + } +@@ -21717,17 +27652,17 @@ SQLITE_PRIVATE void sqlite3VXPrintf( + prefix = '-'; + }else{ + longvalue = v; +- if( flag_plussign ) prefix = '+'; +- else if( flag_blanksign ) prefix = ' '; +- else prefix = 0; ++ prefix = flag_prefix; + } + }else{ + if( bArgList ){ + longvalue = (u64)getIntArg(pArgList); +- }else if( flag_longlong ){ +- longvalue = va_arg(ap,u64); + }else if( flag_long ){ +- longvalue = va_arg(ap,unsigned long int); ++ if( flag_long==2 ){ ++ longvalue = va_arg(ap,u64); ++ }else{ ++ longvalue = va_arg(ap,unsigned long int); ++ } + }else{ + longvalue = va_arg(ap,unsigned int); + } +@@ -21737,16 +27672,16 @@ SQLITE_PRIVATE void sqlite3VXPrintf( + if( flag_zeropad && precision 0 ); + } + length = (int)(&zOut[nOut-1]-bufpt); +- for(idx=precision-length; idx>0; idx--){ ++ while( precision>length ){ + *(--bufpt) = '0'; /* Zero pad */ ++ length++; ++ } ++ if( cThousand ){ ++ int nn = (length - 1)/3; /* Number of "," to insert */ ++ int ix = (length - 1)%3 + 1; ++ bufpt -= nn; ++ for(idx=0; nn>0; idx++){ ++ bufpt[idx] = bufpt[idx+nn]; ++ ix--; ++ if( ix==0 ){ ++ bufpt[++idx] = cThousand; ++ nn--; ++ ix = 3; ++ } ++ } + } + if( prefix ) *(--bufpt) = prefix; /* Add sign */ + if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ +@@ -21795,9 +27745,7 @@ SQLITE_PRIVATE void sqlite3VXPrintf( + realvalue = -realvalue; + prefix = '-'; + }else{ +- if( flag_plussign ) prefix = '+'; +- else if( flag_blanksign ) prefix = ' '; +- else prefix = 0; ++ prefix = flag_prefix; + } + if( xtype==etGENERIC && precision>0 ) precision--; + testcase( precision>0xfff ); +@@ -21813,21 +27761,16 @@ SQLITE_PRIVATE void sqlite3VXPrintf( + if( realvalue>0.0 ){ + LONGDOUBLE_TYPE scale = 1.0; + while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;} +- while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; } +- while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; } ++ while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; } + while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } + realvalue /= scale; + while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } + while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } + if( exp>350 ){ +- if( prefix=='-' ){ +- bufpt = "-Inf"; +- }else if( prefix=='+' ){ +- bufpt = "+Inf"; +- }else{ +- bufpt = "Inf"; +- } +- length = sqlite3Strlen30(bufpt); ++ bufpt = buf; ++ buf[0] = prefix; ++ memcpy(buf+(prefix!=0),"Inf",4); ++ length = 3+(prefix!=0); + break; + } + } +@@ -21856,12 +27799,12 @@ SQLITE_PRIVATE void sqlite3VXPrintf( + }else{ + e2 = exp; + } +- if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){ +- bufpt = zExtra +- = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 ); +- if( bufpt==0 ){ +- setStrAccumError(pAccum, STRACCUM_NOMEM); +- return; ++ { ++ i64 szBufNeeded; /* Size of a temporary buffer needed */ ++ szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15; ++ if( szBufNeeded > etBUFSIZE ){ ++ bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded); ++ if( bufpt==0 ) return; + } + } + zOut = bufpt; +@@ -21956,43 +27899,107 @@ SQLITE_PRIVATE void sqlite3VXPrintf( + case etCHARX: + if( bArgList ){ + bufpt = getTextArg(pArgList); +- c = bufpt ? bufpt[0] : 0; ++ length = 1; ++ if( bufpt ){ ++ buf[0] = c = *(bufpt++); ++ if( (c&0xc0)==0xc0 ){ ++ while( length<4 && (bufpt[0]&0xc0)==0x80 ){ ++ buf[length++] = *(bufpt++); ++ } ++ } ++ }else{ ++ buf[0] = 0; ++ } + }else{ +- c = va_arg(ap,int); ++ unsigned int ch = va_arg(ap,unsigned int); ++ if( ch<0x00080 ){ ++ buf[0] = ch & 0xff; ++ length = 1; ++ }else if( ch<0x00800 ){ ++ buf[0] = 0xc0 + (u8)((ch>>6)&0x1f); ++ buf[1] = 0x80 + (u8)(ch & 0x3f); ++ length = 2; ++ }else if( ch<0x10000 ){ ++ buf[0] = 0xe0 + (u8)((ch>>12)&0x0f); ++ buf[1] = 0x80 + (u8)((ch>>6) & 0x3f); ++ buf[2] = 0x80 + (u8)(ch & 0x3f); ++ length = 3; ++ }else{ ++ buf[0] = 0xf0 + (u8)((ch>>18) & 0x07); ++ buf[1] = 0x80 + (u8)((ch>>12) & 0x3f); ++ buf[2] = 0x80 + (u8)((ch>>6) & 0x3f); ++ buf[3] = 0x80 + (u8)(ch & 0x3f); ++ length = 4; ++ } + } + if( precision>1 ){ + width -= precision-1; + if( width>1 && !flag_leftjustify ){ +- sqlite3AppendChar(pAccum, width-1, ' '); ++ sqlite3_str_appendchar(pAccum, width-1, ' '); + width = 0; + } +- sqlite3AppendChar(pAccum, precision-1, c); ++ while( precision-- > 1 ){ ++ sqlite3_str_append(pAccum, buf, length); ++ } + } +- length = 1; +- buf[0] = c; + bufpt = buf; +- break; ++ flag_altform2 = 1; ++ goto adjust_width_for_utf8; + case etSTRING: + case etDYNSTRING: + if( bArgList ){ + bufpt = getTextArg(pArgList); ++ xtype = etSTRING; + }else{ + bufpt = va_arg(ap,char*); + } + if( bufpt==0 ){ + bufpt = ""; +- }else if( xtype==etDYNSTRING && !bArgList ){ ++ }else if( xtype==etDYNSTRING ){ ++ if( pAccum->nChar==0 ++ && pAccum->mxAlloc ++ && width==0 ++ && precision<0 ++ && pAccum->accError==0 ++ ){ ++ /* Special optimization for sqlite3_mprintf("%z..."): ++ ** Extend an existing memory allocation rather than creating ++ ** a new one. */ ++ assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); ++ pAccum->zText = bufpt; ++ pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt); ++ pAccum->nChar = 0x7fffffff & (int)strlen(bufpt); ++ pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED; ++ length = 0; ++ break; ++ } + zExtra = bufpt; + } + if( precision>=0 ){ +- for(length=0; length 0 && z[0] ){ ++ SQLITE_SKIP_UTF8(z); ++ } ++ length = (int)(z - (unsigned char*)bufpt); ++ }else{ ++ for(length=0; length 0 ){ ++ /* Adjust width to account for extra bytes in UTF-8 characters */ ++ int ii = length - 1; ++ while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++; + } + break; +- case etSQLESCAPE: +- case etSQLESCAPE2: +- case etSQLESCAPE3: { ++ case etSQLESCAPE: /* %q: Escape ' characters */ ++ case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */ ++ case etSQLESCAPE3: { /* %w: Escape " characters */ + int i, j, k, n, isnull; + int needQuote; + char ch; +@@ -22006,18 +28013,23 @@ SQLITE_PRIVATE void sqlite3VXPrintf( + } + isnull = escarg==0; + if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); ++ /* For %q, %Q, and %w, the precision is the number of byte (or ++ ** characters if the ! flags is present) to use from the input. ++ ** Because of the extra quoting characters inserted, the number ++ ** of output characters may be larger than the precision. ++ */ + k = precision; + for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ + if( ch==q ) n++; ++ if( flag_altform2 && (ch&0xc0)==0xc0 ){ ++ while( (escarg[i+1]&0xc0)==0x80 ){ i++; } ++ } + } + needQuote = !isnull && xtype==etSQLESCAPE2; +- n += i + 1 + needQuote*2; ++ n += i + 3; + if( n>etBUFSIZE ){ +- bufpt = zExtra = sqlite3Malloc( n ); +- if( bufpt==0 ){ +- setStrAccumError(pAccum, STRACCUM_NOMEM); +- return; +- } ++ bufpt = zExtra = printfTempBuf(pAccum, n); ++ if( bufpt==0 ) return; + }else{ + bufpt = buf; + } +@@ -22031,31 +28043,34 @@ SQLITE_PRIVATE void sqlite3VXPrintf( + if( needQuote ) bufpt[j++] = q; + bufpt[j] = 0; + length = j; +- /* The precision in %q and %Q means how many input characters to +- ** consume, not the length of the output... +- ** if( precision>=0 && precision printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; ++ pToken = va_arg(ap, Token*); + assert( bArgList==0 ); + if( pToken && pToken->n ){ +- sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); ++ sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n); + } + length = width = 0; + break; + } + case etSRCLIST: { +- SrcList *pSrc = va_arg(ap, SrcList*); +- int k = va_arg(ap, int); +- struct SrcList_item *pItem = &pSrc->a[k]; ++ SrcList *pSrc; ++ int k; ++ struct SrcList_item *pItem; ++ if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; ++ pSrc = va_arg(ap, SrcList*); ++ k = va_arg(ap, int); ++ pItem = &pSrc->a[k]; + assert( bArgList==0 ); + assert( k>=0 && k nSrc ); + if( pItem->zDatabase ){ +- sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase); +- sqlite3StrAccumAppend(pAccum, ".", 1); ++ sqlite3_str_appendall(pAccum, pItem->zDatabase); ++ sqlite3_str_append(pAccum, ".", 1); + } +- sqlite3StrAccumAppendAll(pAccum, pItem->zName); ++ sqlite3_str_appendall(pAccum, pItem->zName); + length = width = 0; + break; + } +@@ -22067,15 +28082,22 @@ SQLITE_PRIVATE void sqlite3VXPrintf( + /* + ** The text of the conversion is pointed to by "bufpt" and is + ** "length" characters long. The field width is "width". Do +- ** the output. ++ ** the output. Both length and width are in bytes, not characters, ++ ** at this point. If the "!" flag was present on string conversions ++ ** indicating that width and precision should be expressed in characters, ++ ** then the values have been translated prior to reaching this point. + */ + width -= length; +- if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); +- sqlite3StrAccumAppend(pAccum, bufpt, length); +- if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); ++ if( width>0 ){ ++ if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' '); ++ sqlite3_str_append(pAccum, bufpt, length); ++ if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' '); ++ }else{ ++ sqlite3_str_append(pAccum, bufpt, length); ++ } + + if( zExtra ){ +- sqlite3_free(zExtra); ++ sqlite3DbFree(pAccum->db, zExtra); + zExtra = 0; + } + }/* End for loop over the format string */ +@@ -22092,16 +28114,15 @@ static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ + char *zNew; + assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ + if( p->accError ){ +- testcase(p->accError==STRACCUM_TOOBIG); +- testcase(p->accError==STRACCUM_NOMEM); ++ testcase(p->accError==SQLITE_TOOBIG); ++ testcase(p->accError==SQLITE_NOMEM); + return 0; + } + if( p->mxAlloc==0 ){ +- N = p->nAlloc - p->nChar - 1; +- setStrAccumError(p, STRACCUM_TOOBIG); +- return N; ++ setStrAccumError(p, SQLITE_TOOBIG); ++ return p->nAlloc - p->nChar - 1; + }else{ +- char *zOld = (p->zText==p->zBase ? 0 : p->zText); ++ char *zOld = isMalloced(p) ? p->zText : 0; + i64 szNew = p->nChar; + szNew += N + 1; + if( szNew+p->nChar<=p->mxAlloc ){ +@@ -22110,8 +28131,8 @@ static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ + szNew += p->nChar; + } + if( szNew > p->mxAlloc ){ +- sqlite3StrAccumReset(p); +- setStrAccumError(p, STRACCUM_TOOBIG); ++ sqlite3_str_reset(p); ++ setStrAccumError(p, SQLITE_TOOBIG); + return 0; + }else{ + p->nAlloc = (int)szNew; +@@ -22123,12 +28144,13 @@ static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ + } + if( zNew ){ + assert( p->zText!=0 || p->nChar==0 ); +- if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); ++ if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); + p->zText = zNew; + p->nAlloc = sqlite3DbMallocSize(p->db, zNew); ++ p->printfFlags |= SQLITE_PRINTF_MALLOCED; + }else{ +- sqlite3StrAccumReset(p); +- setStrAccumError(p, STRACCUM_NOMEM); ++ sqlite3_str_reset(p); ++ setStrAccumError(p, SQLITE_NOMEM); + return 0; + } + } +@@ -22138,7 +28160,7 @@ static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ + /* + ** Append N copies of character c to the given string buffer. + */ +-SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){ ++SQLITE_API void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){ + testcase( p->nChar + (i64)N > 0x7fffffff ); + if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ + return; +@@ -22150,9 +28172,9 @@ SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){ + ** The StrAccum "p" is not large enough to accept N new bytes of z[]. + ** So enlarge if first, then do the append. + ** +-** This is a helper routine to sqlite3StrAccumAppend() that does special-case ++** This is a helper routine to sqlite3_str_append() that does special-case + ** work (enlarging the buffer) using tail recursion, so that the +-** sqlite3StrAccumAppend() routine can use fast calling semantics. ++** sqlite3_str_append() routine can use fast calling semantics. + */ + static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ + N = sqlite3StrAccumEnlarge(p, N); +@@ -22166,14 +28188,14 @@ static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ + ** Append N bytes of text from z to the StrAccum object. Increase the + ** size of the memory allocation for StrAccum if necessary. + */ +-SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ ++SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){ + assert( z!=0 || N==0 ); + assert( p->zText!=0 || p->nChar==0 || p->accError ); + assert( N>=0 ); +- assert( p->accError==0 || p->nAlloc==0 ); ++ assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 ); + if( p->nChar+N >= p->nAlloc ){ + enlargeAndAppend(p,z,N); +- }else{ ++ }else if( N ){ + assert( p->zText ); + p->nChar += N; + memcpy(&p->zText[p->nChar-N], z, N); +@@ -22183,8 +28205,8 @@ SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ + /* + ** Append the complete text of zero-terminated string z[] to the p string. + */ +-SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){ +- sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z)); ++SQLITE_API void sqlite3_str_appendall(sqlite3_str *p, const char *z){ ++ sqlite3_str_append(p, z, sqlite3Strlen30(z)); + } + + +@@ -22193,28 +28215,79 @@ SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){ + ** Return a pointer to the resulting string. Return a NULL + ** pointer if any kind of error was encountered. + */ ++static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){ ++ char *zText; ++ assert( p->mxAlloc>0 && !isMalloced(p) ); ++ zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); ++ if( zText ){ ++ memcpy(zText, p->zText, p->nChar+1); ++ p->printfFlags |= SQLITE_PRINTF_MALLOCED; ++ }else{ ++ setStrAccumError(p, SQLITE_NOMEM); ++ } ++ p->zText = zText; ++ return zText; ++} + SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){ + if( p->zText ){ + p->zText[p->nChar] = 0; +- if( p->mxAlloc>0 && p->zText==p->zBase ){ +- p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); +- if( p->zText ){ +- memcpy(p->zText, p->zBase, p->nChar+1); +- }else{ +- setStrAccumError(p, STRACCUM_NOMEM); +- } ++ if( p->mxAlloc>0 && !isMalloced(p) ){ ++ return strAccumFinishRealloc(p); + } + } + return p->zText; + } + ++/* ++** This singleton is an sqlite3_str object that is returned if ++** sqlite3_malloc() fails to provide space for a real one. This ++** sqlite3_str object accepts no new text and always returns ++** an SQLITE_NOMEM error. ++*/ ++static sqlite3_str sqlite3OomStr = { ++ 0, 0, 0, 0, 0, SQLITE_NOMEM, 0 ++}; ++ ++/* Finalize a string created using sqlite3_str_new(). ++*/ ++SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){ ++ char *z; ++ if( p!=0 && p!=&sqlite3OomStr ){ ++ z = sqlite3StrAccumFinish(p); ++ sqlite3_free(p); ++ }else{ ++ z = 0; ++ } ++ return z; ++} ++ ++/* Return any error code associated with p */ ++SQLITE_API int sqlite3_str_errcode(sqlite3_str *p){ ++ return p ? p->accError : SQLITE_NOMEM; ++} ++ ++/* Return the current length of p in bytes */ ++SQLITE_API int sqlite3_str_length(sqlite3_str *p){ ++ return p ? p->nChar : 0; ++} ++ ++/* Return the current value for p */ ++SQLITE_API char *sqlite3_str_value(sqlite3_str *p){ ++ if( p==0 || p->nChar==0 ) return 0; ++ p->zText[p->nChar] = 0; ++ return p->zText; ++} ++ + /* + ** Reset an StrAccum string. Reclaim all malloced memory. + */ +-SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){ +- if( p->zText!=p->zBase ){ ++SQLITE_API void sqlite3_str_reset(StrAccum *p){ ++ if( isMalloced(p) ){ + sqlite3DbFree(p->db, p->zText); ++ p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; + } ++ p->nAlloc = 0; ++ p->nChar = 0; + p->zText = 0; + } + +@@ -22233,12 +28306,25 @@ SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){ + ** allocations will ever occur. + */ + SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ +- p->zText = p->zBase = zBase; ++ p->zText = zBase; + p->db = db; +- p->nChar = 0; + p->nAlloc = n; + p->mxAlloc = mx; ++ p->nChar = 0; + p->accError = 0; ++ p->printfFlags = 0; ++} ++ ++/* Allocate and initialize a new dynamic string object */ ++SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){ ++ sqlite3_str *p = sqlite3_malloc64(sizeof(*p)); ++ if( p ){ ++ sqlite3StrAccumInit(p, 0, 0, 0, ++ db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); ++ }else{ ++ p = &sqlite3OomStr; ++ } ++ return p; + } + + /* +@@ -22252,10 +28338,11 @@ SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list a + assert( db!=0 ); + sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase), + db->aLimit[SQLITE_LIMIT_LENGTH]); +- sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap); ++ acc.printfFlags = SQLITE_PRINTF_INTERNAL; ++ sqlite3_str_vappendf(&acc, zFormat, ap); + z = sqlite3StrAccumFinish(&acc); +- if( acc.accError==STRACCUM_NOMEM ){ +- db->mallocFailed = 1; ++ if( acc.accError==SQLITE_NOMEM ){ ++ sqlite3OomFault(db); + } + return z; + } +@@ -22273,29 +28360,11 @@ SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ + return z; + } + +-/* +-** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting +-** the string and before returning. This routine is intended to be used +-** to modify an existing string. For example: +-** +-** x = sqlite3MPrintf(db, x, "prefix %s suffix", x); +-** +-*/ +-SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){ +- va_list ap; +- char *z; +- va_start(ap, zFormat); +- z = sqlite3VMPrintf(db, zFormat, ap); +- va_end(ap); +- sqlite3DbFree(db, zStr); +- return z; +-} +- + /* + ** Print into memory obtained from sqlite3_malloc(). Omit the internal + ** %-conversion extensions. + */ +-SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char *zFormat, va_list ap){ ++SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){ + char *z; + char zBase[SQLITE_PRINT_BUF_SIZE]; + StrAccum acc; +@@ -22310,7 +28379,7 @@ SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char *zFormat, va_list ap + if( sqlite3_initialize() ) return 0; + #endif + sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); +- sqlite3VXPrintf(&acc, 0, zFormat, ap); ++ sqlite3_str_vappendf(&acc, zFormat, ap); + z = sqlite3StrAccumFinish(&acc); + return z; + } +@@ -22319,7 +28388,7 @@ SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char *zFormat, va_list ap + ** Print into memory obtained from sqlite3_malloc()(). Omit the internal + ** %-conversion extensions. + */ +-SQLITE_API char *SQLITE_CDECL sqlite3_mprintf(const char *zFormat, ...){ ++SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){ + va_list ap; + char *z; + #ifndef SQLITE_OMIT_AUTOINIT +@@ -22344,7 +28413,7 @@ SQLITE_API char *SQLITE_CDECL sqlite3_mprintf(const char *zFormat, ...){ + ** + ** sqlite3_vsnprintf() is the varargs version. + */ +-SQLITE_API char *SQLITE_STDCALL sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ ++SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ + StrAccum acc; + if( n<=0 ) return zBuf; + #ifdef SQLITE_ENABLE_API_ARMOR +@@ -22355,10 +28424,11 @@ SQLITE_API char *SQLITE_STDCALL sqlite3_vsnprintf(int n, char *zBuf, const char + } + #endif + sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); +- sqlite3VXPrintf(&acc, 0, zFormat, ap); +- return sqlite3StrAccumFinish(&acc); ++ sqlite3_str_vappendf(&acc, zFormat, ap); ++ zBuf[acc.nChar] = 0; ++ return zBuf; + } +-SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ ++SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ + char *z; + va_list ap; + va_start(ap,zFormat); +@@ -22375,13 +28445,18 @@ SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int n, char *zBuf, const char *zF + ** sqlite3_log() must render into a static buffer. It cannot dynamically + ** allocate memory because it might be called while the memory allocator + ** mutex is held. ++** ++** sqlite3_str_vappendf() might ask for *temporary* memory allocations for ++** certain format characters (%q) or for very large precisions or widths. ++** Care must be taken that any sqlite3_log() calls that occur while the ++** memory mutex is held do not use these mechanisms. + */ + static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ + StrAccum acc; /* String accumulator */ + char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ + + sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0); +- sqlite3VXPrintf(&acc, 0, zFormat, ap); ++ sqlite3_str_vappendf(&acc, zFormat, ap); + sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, + sqlite3StrAccumFinish(&acc)); + } +@@ -22389,7 +28464,7 @@ static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ + /* + ** Format and write a message to the log if logging is enabled. + */ +-SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...){ ++SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){ + va_list ap; /* Vararg list */ + if( sqlite3GlobalConfig.xLog ){ + va_start(ap, zFormat); +@@ -22410,30 +28485,62 @@ SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){ + char zBuf[500]; + sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); + va_start(ap,zFormat); +- sqlite3VXPrintf(&acc, 0, zFormat, ap); ++ sqlite3_str_vappendf(&acc, zFormat, ap); + va_end(ap); + sqlite3StrAccumFinish(&acc); ++#ifdef SQLITE_OS_TRACE_PROC ++ { ++ extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf); ++ SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf)); ++ } ++#else + fprintf(stdout,"%s", zBuf); + fflush(stdout); ++#endif + } + #endif + +-#ifdef SQLITE_DEBUG +-/************************************************************************* +-** Routines for implementing the "TreeView" display of hierarchical +-** data structures for debugging. ++ ++/* ++** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument ++** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats. ++*/ ++SQLITE_API void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){ ++ va_list ap; ++ va_start(ap,zFormat); ++ sqlite3_str_vappendf(p, zFormat, ap); ++ va_end(ap); ++} ++ ++/************** End of printf.c **********************************************/ ++/************** Begin file treeview.c ****************************************/ ++/* ++** 2015-06-08 ++** ++** The author disclaims copyright to this source code. In place of ++** a legal notice, here is a blessing: ++** ++** May you do good and not evil. ++** May you find forgiveness for yourself and forgive others. ++** May you share freely, never taking more than you give. + ** +-** The main entry points (coded elsewhere) are: +-** sqlite3TreeViewExpr(0, pExpr, 0); +-** sqlite3TreeViewExprList(0, pList, 0, 0); +-** sqlite3TreeViewSelect(0, pSelect, 0); +-** Insert calls to those routines while debugging in order to display +-** a diagram of Expr, ExprList, and Select objects. ++************************************************************************* + ** ++** This file contains C code to implement the TreeView debugging routines. ++** These routines print a parse tree to standard output for debugging and ++** analysis. ++** ++** The interfaces in this file is only available when compiling ++** with SQLITE_DEBUG. ++*/ ++/* #include "sqliteInt.h" */ ++#ifdef SQLITE_DEBUG ++ ++/* ++** Add a new subitem to the tree. The moreToFollow flag indicates that this ++** is not the last item in the tree. + */ +-/* Add a new subitem to the tree. The moreToFollow flag indicates that this +-** is not the last item in the tree. */ +-SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){ ++static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){ + if( p==0 ){ + p = sqlite3_malloc64( sizeof(*p) ); + if( p==0 ) return 0; +@@ -22445,15 +28552,21 @@ SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){ + if( p->iLevel bLine) ) p->bLine[p->iLevel] = moreToFollow; + return p; + } +-/* Finished with one layer of the tree */ +-SQLITE_PRIVATE void sqlite3TreeViewPop(TreeView *p){ ++ ++/* ++** Finished with one layer of the tree ++*/ ++static void sqlite3TreeViewPop(TreeView *p){ + if( p==0 ) return; + p->iLevel--; + if( p->iLevel<0 ) sqlite3_free(p); + } +-/* Generate a single line of output for the tree, with a prefix that contains +-** all the appropriate tree lines */ +-SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){ ++ ++/* ++** Generate a single line of output for the tree, with a prefix that contains ++** all the appropriate tree lines ++*/ ++static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){ + va_list ap; + int i; + StrAccum acc; +@@ -22461,36 +28574,675 @@ SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){ + sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); + if( p ){ + for(i=0; i iLevel && i bLine)-1; i++){ +- sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4); ++ sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4); + } +- sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); ++ sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); ++ } ++ if( zFormat!=0 ){ ++ va_start(ap, zFormat); ++ sqlite3_str_vappendf(&acc, zFormat, ap); ++ va_end(ap); ++ assert( acc.nChar>0 ); ++ sqlite3_str_append(&acc, "\n", 1); + } +- va_start(ap, zFormat); +- sqlite3VXPrintf(&acc, 0, zFormat, ap); +- va_end(ap); +- if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1); + sqlite3StrAccumFinish(&acc); + fprintf(stdout,"%s", zBuf); + fflush(stdout); + } +-/* Shorthand for starting a new tree item that consists of a single label */ +-SQLITE_PRIVATE void sqlite3TreeViewItem(TreeView *p, const char *zLabel, u8 moreToFollow){ +- p = sqlite3TreeViewPush(p, moreToFollow); ++ ++/* ++** Shorthand for starting a new tree item that consists of a single label ++*/ ++static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){ ++ p = sqlite3TreeViewPush(p, moreFollows); + sqlite3TreeViewLine(p, "%s", zLabel); + } +-#endif /* SQLITE_DEBUG */ + + /* +-** variable-argument wrapper around sqlite3VXPrintf(). ++** Generate a human-readable description of a WITH clause. + */ +-SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){ +- va_list ap; +- va_start(ap,zFormat); +- sqlite3VXPrintf(p, bFlags, zFormat, ap); +- va_end(ap); ++SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){ ++ int i; ++ if( pWith==0 ) return; ++ if( pWith->nCte==0 ) return; ++ if( pWith->pOuter ){ ++ sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter); ++ }else{ ++ sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith); ++ } ++ if( pWith->nCte>0 ){ ++ pView = sqlite3TreeViewPush(pView, 1); ++ for(i=0; i nCte; i++){ ++ StrAccum x; ++ char zLine[1000]; ++ const struct Cte *pCte = &pWith->a[i]; ++ sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); ++ sqlite3_str_appendf(&x, "%s", pCte->zName); ++ if( pCte->pCols && pCte->pCols->nExpr>0 ){ ++ char cSep = '('; ++ int j; ++ for(j=0; j pCols->nExpr; j++){ ++ sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zName); ++ cSep = ','; ++ } ++ sqlite3_str_appendf(&x, ")"); ++ } ++ sqlite3_str_appendf(&x, " AS"); ++ sqlite3StrAccumFinish(&x); ++ sqlite3TreeViewItem(pView, zLine, i nCte-1); ++ sqlite3TreeViewSelect(pView, pCte->pSelect, 0); ++ sqlite3TreeViewPop(pView); ++ } ++ sqlite3TreeViewPop(pView); ++ } + } + +-/************** End of printf.c **********************************************/ ++/* ++** Generate a human-readable description of a SrcList object. ++*/ ++SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){ ++ int i; ++ for(i=0; i nSrc; i++){ ++ const struct SrcList_item *pItem = &pSrc->a[i]; ++ StrAccum x; ++ char zLine[100]; ++ sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); ++ sqlite3_str_appendf(&x, "{%d,*}", pItem->iCursor); ++ if( pItem->zDatabase ){ ++ sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName); ++ }else if( pItem->zName ){ ++ sqlite3_str_appendf(&x, " %s", pItem->zName); ++ } ++ if( pItem->pTab ){ ++ sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p", ++ pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab); ++ } ++ if( pItem->zAlias ){ ++ sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias); ++ } ++ if( pItem->fg.jointype & JT_LEFT ){ ++ sqlite3_str_appendf(&x, " LEFT-JOIN"); ++ } ++ sqlite3StrAccumFinish(&x); ++ sqlite3TreeViewItem(pView, zLine, i nSrc-1); ++ if( pItem->pSelect ){ ++ sqlite3TreeViewSelect(pView, pItem->pSelect, 0); ++ } ++ if( pItem->fg.isTabFunc ){ ++ sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); ++ } ++ sqlite3TreeViewPop(pView); ++ } ++} ++ ++/* ++** Generate a human-readable description of a Select object. ++*/ ++SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){ ++ int n = 0; ++ int cnt = 0; ++ if( p==0 ){ ++ sqlite3TreeViewLine(pView, "nil-SELECT"); ++ return; ++ } ++ pView = sqlite3TreeViewPush(pView, moreToFollow); ++ if( p->pWith ){ ++ sqlite3TreeViewWith(pView, p->pWith, 1); ++ cnt = 1; ++ sqlite3TreeViewPush(pView, 1); ++ } ++ do{ ++ sqlite3TreeViewLine(pView, ++ "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d", ++ ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), ++ ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), ++ p->selId, p, p->selFlags, ++ (int)p->nSelectRow ++ ); ++ if( cnt++ ) sqlite3TreeViewPop(pView); ++ if( p->pPrior ){ ++ n = 1000; ++ }else{ ++ n = 0; ++ if( p->pSrc && p->pSrc->nSrc ) n++; ++ if( p->pWhere ) n++; ++ if( p->pGroupBy ) n++; ++ if( p->pHaving ) n++; ++ if( p->pOrderBy ) n++; ++ if( p->pLimit ) n++; ++#ifndef SQLITE_OMIT_WINDOWFUNC ++ if( p->pWin ) n++; ++ if( p->pWinDefn ) n++; ++#endif ++ } ++ sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set"); ++#ifndef SQLITE_OMIT_WINDOWFUNC ++ if( p->pWin ){ ++ Window *pX; ++ pView = sqlite3TreeViewPush(pView, (n--)>0); ++ sqlite3TreeViewLine(pView, "window-functions"); ++ for(pX=p->pWin; pX; pX=pX->pNextWin){ ++ sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0); ++ } ++ sqlite3TreeViewPop(pView); ++ } ++#endif ++ if( p->pSrc && p->pSrc->nSrc ){ ++ pView = sqlite3TreeViewPush(pView, (n--)>0); ++ sqlite3TreeViewLine(pView, "FROM"); ++ sqlite3TreeViewSrcList(pView, p->pSrc); ++ sqlite3TreeViewPop(pView); ++ } ++ if( p->pWhere ){ ++ sqlite3TreeViewItem(pView, "WHERE", (n--)>0); ++ sqlite3TreeViewExpr(pView, p->pWhere, 0); ++ sqlite3TreeViewPop(pView); ++ } ++ if( p->pGroupBy ){ ++ sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); ++ } ++ if( p->pHaving ){ ++ sqlite3TreeViewItem(pView, "HAVING", (n--)>0); ++ sqlite3TreeViewExpr(pView, p->pHaving, 0); ++ sqlite3TreeViewPop(pView); ++ } ++#ifndef SQLITE_OMIT_WINDOWFUNC ++ if( p->pWinDefn ){ ++ Window *pX; ++ sqlite3TreeViewItem(pView, "WINDOW", (n--)>0); ++ for(pX=p->pWinDefn; pX; pX=pX->pNextWin){ ++ sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0); ++ } ++ sqlite3TreeViewPop(pView); ++ } ++#endif ++ if( p->pOrderBy ){ ++ sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); ++ } ++ if( p->pLimit ){ ++ sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); ++ sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0); ++ if( p->pLimit->pRight ){ ++ sqlite3TreeViewItem(pView, "OFFSET", (n--)>0); ++ sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0); ++ sqlite3TreeViewPop(pView); ++ } ++ sqlite3TreeViewPop(pView); ++ } ++ if( p->pPrior ){ ++ const char *zOp = "UNION"; ++ switch( p->op ){ ++ case TK_ALL: zOp = "UNION ALL"; break; ++ case TK_INTERSECT: zOp = "INTERSECT"; break; ++ case TK_EXCEPT: zOp = "EXCEPT"; break; ++ } ++ sqlite3TreeViewItem(pView, zOp, 1); ++ } ++ p = p->pPrior; ++ }while( p!=0 ); ++ sqlite3TreeViewPop(pView); ++} ++ ++#ifndef SQLITE_OMIT_WINDOWFUNC ++/* ++** Generate a description of starting or stopping bounds ++*/ ++SQLITE_PRIVATE void sqlite3TreeViewBound( ++ TreeView *pView, /* View context */ ++ u8 eBound, /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */ ++ Expr *pExpr, /* Value for PRECEDING or FOLLOWING */ ++ u8 moreToFollow /* True if more to follow */ ++){ ++ switch( eBound ){ ++ case TK_UNBOUNDED: { ++ sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow); ++ sqlite3TreeViewPop(pView); ++ break; ++ } ++ case TK_CURRENT: { ++ sqlite3TreeViewItem(pView, "CURRENT", moreToFollow); ++ sqlite3TreeViewPop(pView); ++ break; ++ } ++ case TK_PRECEDING: { ++ sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow); ++ sqlite3TreeViewExpr(pView, pExpr, 0); ++ sqlite3TreeViewPop(pView); ++ break; ++ } ++ case TK_FOLLOWING: { ++ sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow); ++ sqlite3TreeViewExpr(pView, pExpr, 0); ++ sqlite3TreeViewPop(pView); ++ break; ++ } ++ } ++} ++#endif /* SQLITE_OMIT_WINDOWFUNC */ ++ ++#ifndef SQLITE_OMIT_WINDOWFUNC ++/* ++** Generate a human-readable explanation for a Window object ++*/ ++SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){ ++ int nElement = 0; ++ if( pWin->pFilter ){ ++ sqlite3TreeViewItem(pView, "FILTER", 1); ++ sqlite3TreeViewExpr(pView, pWin->pFilter, 0); ++ sqlite3TreeViewPop(pView); ++ } ++ pView = sqlite3TreeViewPush(pView, more); ++ if( pWin->zName ){ ++ sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin); ++ }else{ ++ sqlite3TreeViewLine(pView, "OVER (%p)", pWin); ++ } ++ if( pWin->zBase ) nElement++; ++ if( pWin->pOrderBy ) nElement++; ++ if( pWin->eFrmType ) nElement++; ++ if( pWin->eExclude ) nElement++; ++ if( pWin->zBase ){ ++ sqlite3TreeViewPush(pView, (--nElement)>0); ++ sqlite3TreeViewLine(pView, "window: %s", pWin->zBase); ++ sqlite3TreeViewPop(pView); ++ } ++ if( pWin->pPartition ){ ++ sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY"); ++ } ++ if( pWin->pOrderBy ){ ++ sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY"); ++ } ++ if( pWin->eFrmType ){ ++ char zBuf[30]; ++ const char *zFrmType = "ROWS"; ++ if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE"; ++ if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS"; ++ sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType, ++ pWin->bImplicitFrame ? " (implied)" : ""); ++ sqlite3TreeViewItem(pView, zBuf, (--nElement)>0); ++ sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1); ++ sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0); ++ sqlite3TreeViewPop(pView); ++ } ++ if( pWin->eExclude ){ ++ char zBuf[30]; ++ const char *zExclude; ++ switch( pWin->eExclude ){ ++ case TK_NO: zExclude = "NO OTHERS"; break; ++ case TK_CURRENT: zExclude = "CURRENT ROW"; break; ++ case TK_GROUP: zExclude = "GROUP"; break; ++ case TK_TIES: zExclude = "TIES"; break; ++ default: ++ sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude); ++ zExclude = zBuf; ++ break; ++ } ++ sqlite3TreeViewPush(pView, 0); ++ sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude); ++ sqlite3TreeViewPop(pView); ++ } ++ sqlite3TreeViewPop(pView); ++} ++#endif /* SQLITE_OMIT_WINDOWFUNC */ ++ ++#ifndef SQLITE_OMIT_WINDOWFUNC ++/* ++** Generate a human-readable explanation for a Window Function object ++*/ ++SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){ ++ pView = sqlite3TreeViewPush(pView, more); ++ sqlite3TreeViewLine(pView, "WINFUNC %s(%d)", ++ pWin->pFunc->zName, pWin->pFunc->nArg); ++ sqlite3TreeViewWindow(pView, pWin, 0); ++ sqlite3TreeViewPop(pView); ++} ++#endif /* SQLITE_OMIT_WINDOWFUNC */ ++ ++/* ++** Generate a human-readable explanation of an expression tree. ++*/ ++SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ ++ const char *zBinOp = 0; /* Binary operator */ ++ const char *zUniOp = 0; /* Unary operator */ ++ char zFlgs[60]; ++ pView = sqlite3TreeViewPush(pView, moreToFollow); ++ if( pExpr==0 ){ ++ sqlite3TreeViewLine(pView, "nil"); ++ sqlite3TreeViewPop(pView); ++ return; ++ } ++ if( pExpr->flags ){ ++ if( ExprHasProperty(pExpr, EP_FromJoin) ){ ++ sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x iRJT=%d", ++ pExpr->flags, pExpr->iRightJoinTable); ++ }else{ ++ sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags); ++ } ++ }else{ ++ zFlgs[0] = 0; ++ } ++ switch( pExpr->op ){ ++ case TK_AGG_COLUMN: { ++ sqlite3TreeViewLine(pView, "AGG{%d:%d}%s", ++ pExpr->iTable, pExpr->iColumn, zFlgs); ++ break; ++ } ++ case TK_COLUMN: { ++ if( pExpr->iTable<0 ){ ++ /* This only happens when coding check constraints */ ++ sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs); ++ }else{ ++ sqlite3TreeViewLine(pView, "{%d:%d}%s", ++ pExpr->iTable, pExpr->iColumn, zFlgs); ++ } ++ if( ExprHasProperty(pExpr, EP_FixedCol) ){ ++ sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); ++ } ++ break; ++ } ++ case TK_INTEGER: { ++ if( pExpr->flags & EP_IntValue ){ ++ sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); ++ }else{ ++ sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); ++ } ++ break; ++ } ++#ifndef SQLITE_OMIT_FLOATING_POINT ++ case TK_FLOAT: { ++ sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); ++ break; ++ } ++#endif ++ case TK_STRING: { ++ sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); ++ break; ++ } ++ case TK_NULL: { ++ sqlite3TreeViewLine(pView,"NULL"); ++ break; ++ } ++ case TK_TRUEFALSE: { ++ sqlite3TreeViewLine(pView, ++ sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE"); ++ break; ++ } ++#ifndef SQLITE_OMIT_BLOB_LITERAL ++ case TK_BLOB: { ++ sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); ++ break; ++ } ++#endif ++ case TK_VARIABLE: { ++ sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)", ++ pExpr->u.zToken, pExpr->iColumn); ++ break; ++ } ++ case TK_REGISTER: { ++ sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); ++ break; ++ } ++ case TK_ID: { ++ sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken); ++ break; ++ } ++#ifndef SQLITE_OMIT_CAST ++ case TK_CAST: { ++ /* Expressions of the form: CAST(pLeft AS token) */ ++ sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); ++ sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); ++ break; ++ } ++#endif /* SQLITE_OMIT_CAST */ ++ case TK_LT: zBinOp = "LT"; break; ++ case TK_LE: zBinOp = "LE"; break; ++ case TK_GT: zBinOp = "GT"; break; ++ case TK_GE: zBinOp = "GE"; break; ++ case TK_NE: zBinOp = "NE"; break; ++ case TK_EQ: zBinOp = "EQ"; break; ++ case TK_IS: zBinOp = "IS"; break; ++ case TK_ISNOT: zBinOp = "ISNOT"; break; ++ case TK_AND: zBinOp = "AND"; break; ++ case TK_OR: zBinOp = "OR"; break; ++ case TK_PLUS: zBinOp = "ADD"; break; ++ case TK_STAR: zBinOp = "MUL"; break; ++ case TK_MINUS: zBinOp = "SUB"; break; ++ case TK_REM: zBinOp = "REM"; break; ++ case TK_BITAND: zBinOp = "BITAND"; break; ++ case TK_BITOR: zBinOp = "BITOR"; break; ++ case TK_SLASH: zBinOp = "DIV"; break; ++ case TK_LSHIFT: zBinOp = "LSHIFT"; break; ++ case TK_RSHIFT: zBinOp = "RSHIFT"; break; ++ case TK_CONCAT: zBinOp = "CONCAT"; break; ++ case TK_DOT: zBinOp = "DOT"; break; ++ ++ case TK_UMINUS: zUniOp = "UMINUS"; break; ++ case TK_UPLUS: zUniOp = "UPLUS"; break; ++ case TK_BITNOT: zUniOp = "BITNOT"; break; ++ case TK_NOT: zUniOp = "NOT"; break; ++ case TK_ISNULL: zUniOp = "ISNULL"; break; ++ case TK_NOTNULL: zUniOp = "NOTNULL"; break; ++ ++ case TK_TRUTH: { ++ int x; ++ const char *azOp[] = { ++ "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE" ++ }; ++ assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT ); ++ assert( pExpr->pRight ); ++ assert( pExpr->pRight->op==TK_TRUEFALSE ); ++ x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight); ++ zUniOp = azOp[x]; ++ break; ++ } ++ ++ case TK_SPAN: { ++ sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken); ++ sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); ++ break; ++ } ++ ++ case TK_COLLATE: { ++ sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken); ++ sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); ++ break; ++ } ++ ++ case TK_AGG_FUNCTION: ++ case TK_FUNCTION: { ++ ExprList *pFarg; /* List of function arguments */ ++ Window *pWin; ++ if( ExprHasProperty(pExpr, EP_TokenOnly) ){ ++ pFarg = 0; ++ pWin = 0; ++ }else{ ++ pFarg = pExpr->x.pList; ++#ifndef SQLITE_OMIT_WINDOWFUNC ++ pWin = pExpr->y.pWin; ++#else ++ pWin = 0; ++#endif ++ } ++ if( pExpr->op==TK_AGG_FUNCTION ){ ++ sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q", ++ pExpr->op2, pExpr->u.zToken); ++ }else{ ++ sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken); ++ } ++ if( pFarg ){ ++ sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0); ++ } ++#ifndef SQLITE_OMIT_WINDOWFUNC ++ if( pWin ){ ++ sqlite3TreeViewWindow(pView, pWin, 0); ++ } ++#endif ++ break; ++ } ++#ifndef SQLITE_OMIT_SUBQUERY ++ case TK_EXISTS: { ++ sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags); ++ sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); ++ break; ++ } ++ case TK_SELECT: { ++ sqlite3TreeViewLine(pView, "SELECT-expr flags=0x%x", pExpr->flags); ++ sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); ++ break; ++ } ++ case TK_IN: { ++ sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags); ++ sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); ++ if( ExprHasProperty(pExpr, EP_xIsSelect) ){ ++ sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); ++ }else{ ++ sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); ++ } ++ break; ++ } ++#endif /* SQLITE_OMIT_SUBQUERY */ ++ ++ /* ++ ** x BETWEEN y AND z ++ ** ++ ** This is equivalent to ++ ** ++ ** x>=y AND x<=z ++ ** ++ ** X is stored in pExpr->pLeft. ++ ** Y is stored in pExpr->pList->a[0].pExpr. ++ ** Z is stored in pExpr->pList->a[1].pExpr. ++ */ ++ case TK_BETWEEN: { ++ Expr *pX = pExpr->pLeft; ++ Expr *pY = pExpr->x.pList->a[0].pExpr; ++ Expr *pZ = pExpr->x.pList->a[1].pExpr; ++ sqlite3TreeViewLine(pView, "BETWEEN"); ++ sqlite3TreeViewExpr(pView, pX, 1); ++ sqlite3TreeViewExpr(pView, pY, 1); ++ sqlite3TreeViewExpr(pView, pZ, 0); ++ break; ++ } ++ case TK_TRIGGER: { ++ /* If the opcode is TK_TRIGGER, then the expression is a reference ++ ** to a column in the new.* or old.* pseudo-tables available to ++ ** trigger programs. In this case Expr.iTable is set to 1 for the ++ ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn ++ ** is set to the column of the pseudo-table to read, or to -1 to ++ ** read the rowid field. ++ */ ++ sqlite3TreeViewLine(pView, "%s(%d)", ++ pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); ++ break; ++ } ++ case TK_CASE: { ++ sqlite3TreeViewLine(pView, "CASE"); ++ sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); ++ sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); ++ break; ++ } ++#ifndef SQLITE_OMIT_TRIGGER ++ case TK_RAISE: { ++ const char *zType = "unk"; ++ switch( pExpr->affinity ){ ++ case OE_Rollback: zType = "rollback"; break; ++ case OE_Abort: zType = "abort"; break; ++ case OE_Fail: zType = "fail"; break; ++ case OE_Ignore: zType = "ignore"; break; ++ } ++ sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); ++ break; ++ } ++#endif ++ case TK_MATCH: { ++ sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s", ++ pExpr->iTable, pExpr->iColumn, zFlgs); ++ sqlite3TreeViewExpr(pView, pExpr->pRight, 0); ++ break; ++ } ++ case TK_VECTOR: { ++ sqlite3TreeViewBareExprList(pView, pExpr->x.pList, "VECTOR"); ++ break; ++ } ++ case TK_SELECT_COLUMN: { ++ sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn); ++ sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0); ++ break; ++ } ++ case TK_IF_NULL_ROW: { ++ sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable); ++ sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); ++ break; ++ } ++ default: { ++ sqlite3TreeViewLine(pView, "op=%d", pExpr->op); ++ break; ++ } ++ } ++ if( zBinOp ){ ++ sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs); ++ sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); ++ sqlite3TreeViewExpr(pView, pExpr->pRight, 0); ++ }else if( zUniOp ){ ++ sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs); ++ sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); ++ } ++ sqlite3TreeViewPop(pView); ++} ++ ++ ++/* ++** Generate a human-readable explanation of an expression list. ++*/ ++SQLITE_PRIVATE void sqlite3TreeViewBareExprList( ++ TreeView *pView, ++ const ExprList *pList, ++ const char *zLabel ++){ ++ if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST"; ++ if( pList==0 ){ ++ sqlite3TreeViewLine(pView, "%s (empty)", zLabel); ++ }else{ ++ int i; ++ sqlite3TreeViewLine(pView, "%s", zLabel); ++ for(i=0; i nExpr; i++){ ++ int j = pList->a[i].u.x.iOrderByCol; ++ char *zName = pList->a[i].zName; ++ int moreToFollow = i nExpr - 1; ++ if( j || zName ){ ++ sqlite3TreeViewPush(pView, moreToFollow); ++ moreToFollow = 0; ++ sqlite3TreeViewLine(pView, 0); ++ if( zName ){ ++ fprintf(stdout, "AS %s ", zName); ++ } ++ if( j ){ ++ fprintf(stdout, "iOrderByCol=%d", j); ++ } ++ fprintf(stdout, "\n"); ++ fflush(stdout); ++ } ++ sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow); ++ if( j || zName ){ ++ sqlite3TreeViewPop(pView); ++ } ++ } ++ } ++} ++SQLITE_PRIVATE void sqlite3TreeViewExprList( ++ TreeView *pView, ++ const ExprList *pList, ++ u8 moreToFollow, ++ const char *zLabel ++){ ++ pView = sqlite3TreeViewPush(pView, moreToFollow); ++ sqlite3TreeViewBareExprList(pView, pList, zLabel); ++ sqlite3TreeViewPop(pView); ++} ++ ++#endif /* SQLITE_DEBUG */ ++ ++/************** End of treeview.c ********************************************/ + /************** Begin file random.c ******************************************/ + /* + ** 2001 September 15 +@@ -22509,6 +29261,7 @@ SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, + ** Random numbers are used by some of the database backends in order + ** to generate random integer keys for tables or random filenames. + */ ++/* #include "sqliteInt.h" */ + + + /* All threads share a single random number generator. +@@ -22523,7 +29276,7 @@ static SQLITE_WSD struct sqlite3PrngType { + /* + ** Return N random bytes. + */ +-SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *pBuf){ ++SQLITE_API void sqlite3_randomness(int N, void *pBuf){ + unsigned char t; + unsigned char *zBuf = pBuf; + +@@ -22599,7 +29352,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *pBuf){ + sqlite3_mutex_leave(mutex); + } + +-#ifndef SQLITE_OMIT_BUILTIN_TEST ++#ifndef SQLITE_UNTESTABLE + /* + ** For testing purposes, we sometimes want to preserve the state of + ** PRNG and restore the PRNG to its saved state at a later time, or +@@ -22624,7 +29377,7 @@ SQLITE_PRIVATE void sqlite3PrngRestoreState(void){ + sizeof(sqlite3Prng) + ); + } +-#endif /* SQLITE_OMIT_BUILTIN_TEST */ ++#endif /* SQLITE_UNTESTABLE */ + + /************** End of random.c **********************************************/ + /************** Begin file threads.c *****************************************/ +@@ -22655,7 +29408,9 @@ SQLITE_PRIVATE void sqlite3PrngRestoreState(void){ + ** of multiple cores can do so, while also allowing applications to stay + ** single-threaded if desired. + */ ++/* #include "sqliteInt.h" */ + #if SQLITE_OS_WIN ++/* # include "os_win.h" */ + #endif + + #if SQLITE_MAX_WORKER_THREADS>0 +@@ -22691,10 +29446,14 @@ SQLITE_PRIVATE int sqlite3ThreadCreate( + + *ppThread = 0; + p = sqlite3Malloc(sizeof(*p)); +- if( p==0 ) return SQLITE_NOMEM; ++ if( p==0 ) return SQLITE_NOMEM_BKPT; + memset(p, 0, sizeof(*p)); + p->xTask = xTask; + p->pIn = pIn; ++ /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a ++ ** function that returns SQLITE_ERROR when passed the argument 200, that ++ ** forces worker threads to run sequentially and deterministically ++ ** for testing purposes. */ + if( sqlite3FaultSim(200) ){ + rc = 1; + }else{ +@@ -22713,7 +29472,7 @@ SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ + int rc; + + assert( ppOut!=0 ); +- if( NEVER(p==0) ) return SQLITE_NOMEM; ++ if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT; + if( p->done ){ + *ppOut = p->pOut; + rc = SQLITE_OK; +@@ -22778,8 +29537,13 @@ SQLITE_PRIVATE int sqlite3ThreadCreate( + assert( xTask!=0 ); + *ppThread = 0; + p = sqlite3Malloc(sizeof(*p)); +- if( p==0 ) return SQLITE_NOMEM; +- if( sqlite3GlobalConfig.bCoreMutex==0 ){ ++ if( p==0 ) return SQLITE_NOMEM_BKPT; ++ /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a ++ ** function that returns SQLITE_ERROR when passed the argument 200, that ++ ** forces worker threads to run sequentially and deterministically ++ ** (via the sqlite3FaultSim() term of the conditional) for testing ++ ** purposes. */ ++ if( sqlite3GlobalConfig.bCoreMutex==0 || sqlite3FaultSim(200) ){ + memset(p, 0, sizeof(*p)); + }else{ + p->xTask = xTask; +@@ -22805,9 +29569,9 @@ SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ + BOOL bRc; + + assert( ppOut!=0 ); +- if( NEVER(p==0) ) return SQLITE_NOMEM; ++ if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT; + if( p->xTask==0 ){ +- assert( p->id==GetCurrentThreadId() ); ++ /* assert( p->id==GetCurrentThreadId() ); */ + rc = WAIT_OBJECT_0; + assert( p->tid==0 ); + }else{ +@@ -22853,7 +29617,7 @@ SQLITE_PRIVATE int sqlite3ThreadCreate( + assert( xTask!=0 ); + *ppThread = 0; + p = sqlite3Malloc(sizeof(*p)); +- if( p==0 ) return SQLITE_NOMEM; ++ if( p==0 ) return SQLITE_NOMEM_BKPT; + if( (SQLITE_PTR_TO_INT(p)/17)&1 ){ + p->xTask = xTask; + p->pIn = pIn; +@@ -22869,7 +29633,7 @@ SQLITE_PRIVATE int sqlite3ThreadCreate( + SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ + + assert( ppOut!=0 ); +- if( NEVER(p==0) ) return SQLITE_NOMEM; ++ if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT; + if( p->xTask ){ + *ppOut = p->xTask(p->pIn); + }else{ +@@ -22880,7 +29644,7 @@ SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ + #if defined(SQLITE_TEST) + { + void *pTstAlloc = sqlite3Malloc(10); +- if (!pTstAlloc) return SQLITE_NOMEM; ++ if (!pTstAlloc) return SQLITE_NOMEM_BKPT; + sqlite3_free(pTstAlloc); + } + #endif +@@ -22929,15 +29693,17 @@ SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ + ** 0xfe 0xff big-endian utf-16 follows + ** + */ ++/* #include "sqliteInt.h" */ + /* #include */ ++/* #include "vdbeInt.h" */ + +-#ifndef SQLITE_AMALGAMATION ++#if !defined(SQLITE_AMALGAMATION) && SQLITE_BYTEORDER==0 + /* + ** The following constant value is used by the SQLITE_BIGENDIAN and + ** SQLITE_LITTLEENDIAN macros. + */ + SQLITE_PRIVATE const int sqlite3one = 1; +-#endif /* SQLITE_AMALGAMATION */ ++#endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */ + + /* + ** This lookup table is used to help decode the first byte of +@@ -23094,11 +29860,11 @@ SQLITE_PRIVATE u32 sqlite3Utf8Read( + ** encoding, or if *pMem does not contain a string value. + */ + SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ +- int len; /* Maximum length of output string in bytes */ +- unsigned char *zOut; /* Output buffer */ +- unsigned char *zIn; /* Input iterator */ +- unsigned char *zTerm; /* End of input */ +- unsigned char *z; /* Output iterator */ ++ sqlite3_int64 len; /* Maximum length of output string in bytes */ ++ unsigned char *zOut; /* Output buffer */ ++ unsigned char *zIn; /* Input iterator */ ++ unsigned char *zTerm; /* End of input */ ++ unsigned char *z; /* Output iterator */ + unsigned int c; + + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); +@@ -23125,7 +29891,7 @@ SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desired + rc = sqlite3VdbeMemMakeWriteable(pMem); + if( rc!=SQLITE_OK ){ + assert( rc==SQLITE_NOMEM ); +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + zIn = (u8*)pMem->z; + zTerm = &zIn[pMem->n&~1]; +@@ -23147,14 +29913,14 @@ SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desired + ** nul-terminator. + */ + pMem->n &= ~1; +- len = pMem->n * 2 + 1; ++ len = 2 * (sqlite3_int64)pMem->n + 1; + }else{ + /* When converting from UTF-8 to UTF-16 the maximum growth is caused + ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16 + ** character. Two bytes are required in the output buffer for the + ** nul-terminator. + */ +- len = pMem->n * 2 + 2; ++ len = 2 * (sqlite3_int64)pMem->n + 2; + } + + /* Set zIn to point at the start of the input buffer and zTerm to point 1 +@@ -23167,7 +29933,7 @@ SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desired + zTerm = &zIn[pMem->n]; + zOut = sqlite3DbMallocRaw(pMem->db, len); + if( !zOut ){ +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + z = zOut; + +@@ -23210,7 +29976,7 @@ SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desired + + c = pMem->flags; + sqlite3VdbeMemRelease(pMem); +- pMem->flags = MEM_Str|MEM_Term|(c&MEM_AffMask); ++ pMem->flags = MEM_Str|MEM_Term|(c&(MEM_AffMask|MEM_Subtype)); + pMem->enc = desiredEnc; + pMem->z = (char*)zOut; + pMem->zMalloc = pMem->z; +@@ -23226,7 +29992,9 @@ translate_out: + #endif + return SQLITE_OK; + } ++#endif /* SQLITE_OMIT_UTF16 */ + ++#ifndef SQLITE_OMIT_UTF16 + /* + ** This routine checks for a byte-order mark at the beginning of the + ** UTF-16 string stored in *pMem. If one is present, it is removed and +@@ -23442,6 +30210,7 @@ SQLITE_PRIVATE void sqlite3UtfSelfTest(void){ + ** strings, and stuff like that. + ** + */ ++/* #include "sqliteInt.h" */ + /* #include */ + #if HAVE_ISNAN || SQLITE_HAVE_ISNAN + # include +@@ -23458,17 +30227,25 @@ SQLITE_PRIVATE void sqlite3Coverage(int x){ + #endif + + /* +-** Give a callback to the test harness that can be used to simulate faults +-** in places where it is difficult or expensive to do so purely by means +-** of inputs. ++** Calls to sqlite3FaultSim() are used to simulate a failure during testing, ++** or to bypass normal error detection during testing in order to let ++** execute proceed futher downstream. ++** ++** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The ++** sqlite3FaultSim() function only returns non-zero during testing. + ** +-** The intent of the integer argument is to let the fault simulator know +-** which of multiple sqlite3FaultSim() calls has been hit. ++** During testing, if the test harness has set a fault-sim callback using ++** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then ++** each call to sqlite3FaultSim() is relayed to that application-supplied ++** callback and the integer return value form the application-supplied ++** callback is returned by sqlite3FaultSim(). + ** +-** Return whatever integer value the test callback returns, or return +-** SQLITE_OK if no test callback is installed. ++** The integer argument to sqlite3FaultSim() is a code to identify which ++** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim() ++** should have a unique code. To prevent legacy testing applications from ++** breaking, the codes should not be changed or reused. + */ +-#ifndef SQLITE_OMIT_BUILTIN_TEST ++#ifndef SQLITE_UNTESTABLE + SQLITE_PRIVATE int sqlite3FaultSim(int iTest){ + int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback; + return xCallback ? xCallback(iTest) : SQLITE_OK; +@@ -23531,19 +30308,53 @@ SQLITE_PRIVATE int sqlite3IsNaN(double x){ + ** than 1GiB) the value returned might be less than the true string length. + */ + SQLITE_PRIVATE int sqlite3Strlen30(const char *z){ +- const char *z2 = z; + if( z==0 ) return 0; +- while( *z2 ){ z2++; } +- return 0x3fffffff & (int)(z2 - z); ++ return 0x3fffffff & (int)strlen(z); ++} ++ ++/* ++** Return the declared type of a column. Or return zDflt if the column ++** has no declared type. ++** ++** The column type is an extra string stored after the zero-terminator on ++** the column name if and only if the COLFLAG_HASTYPE flag is set. ++*/ ++SQLITE_PRIVATE char *sqlite3ColumnType(Column *pCol, char *zDflt){ ++ if( (pCol->colFlags & COLFLAG_HASTYPE)==0 ) return zDflt; ++ return pCol->zName + strlen(pCol->zName) + 1; ++} ++ ++/* ++** Helper function for sqlite3Error() - called rarely. Broken out into ++** a separate routine to avoid unnecessary register saves on entry to ++** sqlite3Error(). ++*/ ++static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){ ++ if( db->pErr ) sqlite3ValueSetNull(db->pErr); ++ sqlite3SystemError(db, err_code); + } + + /* + ** Set the current error code to err_code and clear any prior error message. ++** Also set iSysErrno (by calling sqlite3System) if the err_code indicates ++** that would be appropriate. + */ + SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){ + assert( db!=0 ); + db->errCode = err_code; +- if( db->pErr ) sqlite3ValueSetNull(db->pErr); ++ if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code); ++} ++ ++/* ++** Load the sqlite3.iSysErrno field if that is an appropriate thing ++** to do based on the SQLite error code in rc. ++*/ ++SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){ ++ if( rc==SQLITE_IOERR_NOMEM ) return; ++ rc &= 0xff; ++ if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){ ++ db->iSysErrno = sqlite3OsGetLastError(db->pVfs); ++ } + } + + /* +@@ -23570,6 +30381,7 @@ SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){ + SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){ + assert( db!=0 ); + db->errCode = err_code; ++ sqlite3SystemError(db, err_code); + if( zFormat==0 ){ + sqlite3Error(db, err_code); + }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ +@@ -23616,6 +30428,19 @@ SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ + } + } + ++/* ++** If database connection db is currently parsing SQL, then transfer ++** error code errCode to that parser if the parser has not already ++** encountered some other kind of error. ++*/ ++SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3 *db, int errCode){ ++ Parse *pParse; ++ if( db==0 || (pParse = db->pParse)==0 ) return errCode; ++ pParse->rc = errCode; ++ pParse->nErr++; ++ return errCode; ++} ++ + /* + ** Convert an SQL-style quoted string into a normal string by removing + ** the quote characters. The conversion is done in-place. If the +@@ -23629,22 +30454,17 @@ SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ + ** dequoted string, exclusive of the zero terminator, if dequoting does + ** occur. + ** +-** 2002-Feb-14: This routine is extended to remove MS-Access style ++** 2002-02-14: This routine is extended to remove MS-Access style + ** brackets from around identifiers. For example: "[a-b-c]" becomes + ** "a-b-c". + */ +-SQLITE_PRIVATE int sqlite3Dequote(char *z){ ++SQLITE_PRIVATE void sqlite3Dequote(char *z){ + char quote; + int i, j; +- if( z==0 ) return -1; ++ if( z==0 ) return; + quote = z[0]; +- switch( quote ){ +- case '\'': break; +- case '"': break; +- case '`': break; /* For MySQL compatibility */ +- case '[': quote = ']'; break; /* For MS SqlServer compatibility */ +- default: return -1; +- } ++ if( !sqlite3Isquote(quote) ) return; ++ if( quote=='[' ) quote = ']'; + for(i=1, j=0;; i++){ + assert( z[i] ); + if( z[i]==quote ){ +@@ -23659,7 +30479,19 @@ SQLITE_PRIVATE int sqlite3Dequote(char *z){ + } + } + z[j] = 0; +- return j; ++} ++SQLITE_PRIVATE void sqlite3DequoteExpr(Expr *p){ ++ assert( sqlite3Isquote(p->u.zToken[0]) ); ++ p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted; ++ sqlite3Dequote(p->u.zToken); ++} ++ ++/* ++** Generate a Token object from a string ++*/ ++SQLITE_PRIVATE void sqlite3TokenInit(Token *p, char *z){ ++ p->z = z; ++ p->n = sqlite3Strlen30(z); + } + + /* Convenient short-hand */ +@@ -23675,19 +30507,28 @@ SQLITE_PRIVATE int sqlite3Dequote(char *z){ + ** case-independent fashion, using the same definition of "case + ** independence" that SQLite uses internally when comparing identifiers. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_stricmp(const char *zLeft, const char *zRight){ +- register unsigned char *a, *b; ++SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){ + if( zLeft==0 ){ + return zRight ? -1 : 0; + }else if( zRight==0 ){ + return 1; + } ++ return sqlite3StrICmp(zLeft, zRight); ++} ++SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){ ++ unsigned char *a, *b; ++ int c; + a = (unsigned char *)zLeft; + b = (unsigned char *)zRight; +- while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } +- return UpperToLower[*a] - UpperToLower[*b]; ++ for(;;){ ++ c = (int)UpperToLower[*a] - (int)UpperToLower[*b]; ++ if( c || *a==0 ) break; ++ a++; ++ b++; ++ } ++ return c; + } +-SQLITE_API int SQLITE_STDCALL sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ ++SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ + register unsigned char *a, *b; + if( zLeft==0 ){ + return zRight ? -1 : 0; +@@ -23700,6 +30541,45 @@ SQLITE_API int SQLITE_STDCALL sqlite3_strnicmp(const char *zLeft, const char *zR + return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; + } + ++/* ++** Compute 10 to the E-th power. Examples: E==1 results in 10. ++** E==2 results in 100. E==50 results in 1.0e50. ++** ++** This routine only works for values of E between 1 and 341. ++*/ ++static LONGDOUBLE_TYPE sqlite3Pow10(int E){ ++#if defined(_MSC_VER) ++ static const LONGDOUBLE_TYPE x[] = { ++ 1.0e+001, ++ 1.0e+002, ++ 1.0e+004, ++ 1.0e+008, ++ 1.0e+016, ++ 1.0e+032, ++ 1.0e+064, ++ 1.0e+128, ++ 1.0e+256 ++ }; ++ LONGDOUBLE_TYPE r = 1.0; ++ int i; ++ assert( E>=0 && E<=307 ); ++ for(i=0; E!=0; i++, E >>=1){ ++ if( E & 1 ) r *= x[i]; ++ } ++ return r; ++#else ++ LONGDOUBLE_TYPE x = 10.0; ++ LONGDOUBLE_TYPE r = 1.0; ++ while(1){ ++ if( E & 1 ) r *= x; ++ E >>= 1; ++ if( E==0 ) break; ++ x *= x; ++ } ++ return r; ++#endif ++} ++ + /* + ** The string z[] is an text representation of a real number. + ** Convert this string to a double and write it into *pResult. +@@ -23735,7 +30615,7 @@ SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en + int eValid = 1; /* True exponent is either not used or is well-formed */ + double result; + int nDigits = 0; +- int nonNum = 0; ++ int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ + + assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); + *pResult = 0.0; /* Default return value, in case of an error */ +@@ -23748,7 +30628,7 @@ SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en + assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); + for(i=3-enc; i =zEnd ) goto do_atof_calc; + + /* if decimal point is present */ +@@ -23783,12 +30660,13 @@ SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en + z+=incr; + /* copy digits from after decimal to significand + ** (decrease exponent by d to shift decimal right) */ +- while( z =zEnd ) goto do_atof_calc; + +@@ -23796,7 +30674,12 @@ SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en + if( *z=='e' || *z=='E' ){ + z+=incr; + eValid = 0; +- if( z>=zEnd ) goto do_atof_calc; ++ ++ /* This branch is needed to avoid a (harmless) buffer overread. The ++ ** special comment alerts the mutation tester that the correct answer ++ ** is obtained even if the branch is omitted */ ++ if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/ ++ + /* get sign of exponent */ + if( *z=='-' ){ + esign = -1; +@@ -23813,9 +30696,7 @@ SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en + } + + /* skip trailing spaces */ +- if( nDigits && eValid ){ +- while( z 0 ){ +- while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10; +- }else{ +- while( !(s%10) && e>0 ) e--,s/=10; ++ /* Attempt to reduce exponent. ++ ** ++ ** Branches that are not required for the correct answer but which only ++ ** help to obtain the correct answer faster are marked with special ++ ** comments, as a hint to the mutation tester. ++ */ ++ while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/ ++ if( esign>0 ){ ++ if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/ ++ s *= 10; ++ }else{ ++ if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/ ++ s /= 10; ++ } ++ e--; + } + + /* adjust the sign of significand */ + s = sign<0 ? -s : s; + +- /* if exponent, scale significand as appropriate +- ** and store in result. */ +- if( e ){ +- LONGDOUBLE_TYPE scale = 1.0; ++ if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/ ++ result = (double)s; ++ }else{ + /* attempt to handle extremely small/large numbers better */ +- if( e>307 && e<342 ){ +- while( e%308 ) { scale *= 1.0e+1; e -= 1; } +- if( esign<0 ){ +- result = s / scale; +- result /= 1.0e+308; +- }else{ +- result = s * scale; +- result *= 1.0e+308; +- } +- }else if( e>=342 ){ +- if( esign<0 ){ +- result = 0.0*s; +- }else{ +- result = 1e308*1e308*s; /* Infinity */ ++ if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/ ++ if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/ ++ LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308); ++ if( esign<0 ){ ++ result = s / scale; ++ result /= 1.0e+308; ++ }else{ ++ result = s * scale; ++ result *= 1.0e+308; ++ } ++ }else{ assert( e>=342 ); ++ if( esign<0 ){ ++ result = 0.0*s; ++ }else{ ++#ifdef INFINITY ++ result = INFINITY*s; ++#else ++ result = 1e308*1e308*s; /* Infinity */ ++#endif ++ } + } + }else{ +- /* 1.0e+22 is the largest power of 10 than can be +- ** represented exactly. */ +- while( e%22 ) { scale *= 1.0e+1; e -= 1; } +- while( e>0 ) { scale *= 1.0e+22; e -= 22; } ++ LONGDOUBLE_TYPE scale = sqlite3Pow10(e); + if( esign<0 ){ + result = s / scale; + }else{ + result = s * scale; + } + } +- } else { +- result = (double)s; + } + } + +@@ -23883,7 +30772,7 @@ do_atof_calc: + *pResult = result; + + /* return true if number and no extra non-whitespace chracters after */ +- return z>=zEnd && nDigits>0 && eValid && nonNum==0; ++ return z==zEnd && nDigits>0 && eValid && nonNum==0; + #else + return !sqlite3Atoi64(z, pResult, length, enc); + #endif /* SQLITE_OMIT_FLOATING_POINT */ +@@ -23924,16 +30813,12 @@ static int compare2pow63(const char *zNum, int incr){ + ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This + ** routine does *not* accept hexadecimal notation. + ** +-** If the zNum value is representable as a 64-bit twos-complement +-** integer, then write that value into *pNum and return 0. +-** +-** If zNum is exactly 9223372036854775808, return 2. This special +-** case is broken out because while 9223372036854775808 cannot be a +-** signed 64-bit integer, its negative -9223372036854775808 can be. ++** Returns: + ** +-** If zNum is too big for a 64-bit integer and is not +-** 9223372036854775808 or if zNum contains any non-numeric text, +-** then return 1. ++** 0 Successful transformation. Fits in a 64-bit signed integer. ++** 1 Excess non-space text after the integer value ++** 2 Integer too large for a 64-bit signed integer or is malformed ++** 3 Special case of 9223372036854775808 + ** + ** length is the number of bytes in the string (bytes, not characters). + ** The string is not necessarily zero-terminated. The encoding is +@@ -23945,7 +30830,8 @@ SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc + int neg = 0; /* assume positive */ + int i; + int c = 0; +- int nonNum = 0; ++ int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ ++ int rc; /* Baseline return code */ + const char *zStart; + const char *zEnd = zNum + length; + assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); +@@ -23956,7 +30842,7 @@ SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc + assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); + for(i=3-enc; i ='0' && c<='9'; i+=incr){ + u = u*10 + c - '0'; + } ++ testcase( i==18*incr ); ++ testcase( i==19*incr ); ++ testcase( i==20*incr ); + if( u>LARGEST_INT64 ){ ++ /* This test and assignment is needed only to suppress UB warnings ++ ** from clang and -fsanitize=undefined. This test and assignment make ++ ** the code a little larger and slower, and no harm comes from omitting ++ ** them, but we must appaise the undefined-behavior pharisees. */ + *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; + }else if( neg ){ + *pNum = -(i64)u; + }else{ + *pNum = (i64)u; + } +- testcase( i==18 ); +- testcase( i==19 ); +- testcase( i==20 ); +- if( (c!=0 && &zNum[i] 19*incr || nonNum ){ +- /* zNum is empty or contains non-numeric text or is longer +- ** than 19 digits (thus guaranteeing that it is too large) */ +- return 1; +- }else if( i<19*incr ){ ++ rc = 0; ++ if( (i==0 && zStart==zNum) /* No digits */ ++ || nonNum /* UTF16 with high-order bytes non-zero */ ++ ){ ++ rc = 1; ++ }else if( &zNum[i] 19*incr ? 1 : compare2pow63(zNum, incr); + if( c<0 ){ + /* zNum is less than 9223372036854775808 so it fits */ + assert( u<=LARGEST_INT64 ); +- return 0; +- }else if( c>0 ){ +- /* zNum is greater than 9223372036854775808 so it overflows */ +- return 1; ++ return rc; + }else{ +- /* zNum is exactly 9223372036854775808. Fits if negative. The +- ** special case 2 overflow if positive */ +- assert( u-1==LARGEST_INT64 ); +- return neg ? 0 : 2; ++ *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; ++ if( c>0 ){ ++ /* zNum is greater than 9223372036854775808 so it overflows */ ++ return 2; ++ }else{ ++ /* zNum is exactly 9223372036854775808. Fits if negative. The ++ ** special case 2 overflow if positive */ ++ assert( u-1==LARGEST_INT64 ); ++ return neg ? rc : 3; ++ } + } + } + } +@@ -24018,14 +30922,14 @@ SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc + ** Returns: + ** + ** 0 Successful transformation. Fits in a 64-bit signed integer. +-** 1 Integer too large for a 64-bit signed integer or is malformed +-** 2 Special case of 9223372036854775808 ++** 1 Excess text after the integer value ++** 2 Integer too large for a 64-bit signed integer or is malformed ++** 3 Special case of 9223372036854775808 + */ + SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){ + #ifndef SQLITE_OMIT_HEX_INTEGER + if( z[0]=='0' + && (z[1]=='x' || z[1]=='X') +- && sqlite3Isxdigit(z[2]) + ){ + u64 u = 0; + int i, k; +@@ -24034,7 +30938,7 @@ SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){ + u = u*16 + sqlite3HexToInt(z[k]); + } + memcpy(pOut, &u, 8); +- return (z[k]==0 && k-i<=16) ? 0 : 1; ++ return (z[k]==0 && k-i<=16) ? 0 : 2; + }else + #endif /* SQLITE_OMIT_HEX_INTEGER */ + { +@@ -24081,6 +30985,7 @@ SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){ + } + } + #endif ++ if( !sqlite3Isdigit(zNum[0]) ) return 0; + while( zNum[0]=='0' ) zNum++; + for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ + v = v*10 + c; +@@ -24272,7 +31177,8 @@ SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){ + /* a: p0<<28 | p2<<14 | p4 (unmasked) */ + if (!(a&0x80)) + { +- /* we can skip these cause they were (effectively) done above in calc'ing s */ ++ /* we can skip these cause they were (effectively) done above ++ ** while calculating s */ + /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ + /* b &= (0x7f<<14)|(0x7f); */ + b = b<<7; +@@ -24493,11 +31399,8 @@ SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){ + ** 64-bit integer. + */ + SQLITE_PRIVATE int sqlite3VarintLen(u64 v){ +- int i = 0; +- do{ +- i++; +- v >>= 7; +- }while( v!=0 && ALWAYS(i<9) ); ++ int i; ++ for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); } + return i; + } + +@@ -24506,14 +31409,38 @@ SQLITE_PRIVATE int sqlite3VarintLen(u64 v){ + ** Read or write a four-byte big-endian integer value. + */ + SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){ ++#if SQLITE_BYTEORDER==4321 ++ u32 x; ++ memcpy(&x,p,4); ++ return x; ++#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 ++ u32 x; ++ memcpy(&x,p,4); ++ return __builtin_bswap32(x); ++#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 ++ u32 x; ++ memcpy(&x,p,4); ++ return _byteswap_ulong(x); ++#else + testcase( p[0]&0x80 ); + return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; ++#endif + } + SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){ ++#if SQLITE_BYTEORDER==4321 ++ memcpy(p,&v,4); ++#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 ++ u32 x = __builtin_bswap32(v); ++ memcpy(p,&x,4); ++#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 ++ u32 x = _byteswap_ulong(v); ++ memcpy(p,&x,4); ++#else + p[0] = (u8)(v>>24); + p[1] = (u8)(v>>16); + p[2] = (u8)(v>>8); + p[3] = (u8)v; ++#endif + } + + +@@ -24545,7 +31472,7 @@ SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ + char *zBlob; + int i; + +- zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1); ++ zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1); + n--; + if( zBlob ){ + for(i=0; i =5004000 && !defined(__INTEL_COMPILER) ++ return __builtin_add_overflow(*pA, iB, pA); ++#else + i64 iA = *pA; + testcase( iA==0 ); testcase( iA==1 ); + testcase( iB==-1 ); testcase( iB==0 ); +@@ -24635,8 +31565,12 @@ SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){ + } + *pA += iB; + return 0; ++#endif + } + SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){ ++#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER) ++ return __builtin_sub_overflow(*pA, iB, pA); ++#else + testcase( iB==SMALLEST_INT64+1 ); + if( iB==SMALLEST_INT64 ){ + testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); +@@ -24646,38 +31580,28 @@ SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){ + }else{ + return sqlite3AddInt64(pA, -iB); + } ++#endif + } +-#define TWOPOWER32 (((i64)1)<<32) +-#define TWOPOWER31 (((i64)1)<<31) + SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){ ++#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER) ++ return __builtin_mul_overflow(*pA, iB, pA); ++#else + i64 iA = *pA; +- i64 iA1, iA0, iB1, iB0, r; +- +- iA1 = iA/TWOPOWER32; +- iA0 = iA % TWOPOWER32; +- iB1 = iB/TWOPOWER32; +- iB0 = iB % TWOPOWER32; +- if( iA1==0 ){ +- if( iB1==0 ){ +- *pA *= iB; +- return 0; +- } +- r = iA0*iB1; +- }else if( iB1==0 ){ +- r = iA1*iB0; +- }else{ +- /* If both iA1 and iB1 are non-zero, overflow will result */ +- return 1; +- } +- testcase( r==(-TWOPOWER31)-1 ); +- testcase( r==(-TWOPOWER31) ); +- testcase( r==TWOPOWER31 ); +- testcase( r==TWOPOWER31-1 ); +- if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1; +- r *= TWOPOWER32; +- if( sqlite3AddInt64(&r, iA0*iB0) ) return 1; +- *pA = r; ++ if( iB>0 ){ ++ if( iA>LARGEST_INT64/iB ) return 1; ++ if( iA 0 ){ ++ if( iB LARGEST_INT64/-iB ) return 1; ++ } ++ } ++ *pA = iA*iB; + return 0; ++#endif + } + + /* +@@ -24761,8 +31685,14 @@ SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){ + if( x<2 ) return 0; + while( x<8 ){ y -= 10; x <<= 1; } + }else{ +- while( x>255 ){ y += 40; x >>= 4; } ++#if GCC_VERSION>=5004000 ++ int i = 60 - __builtin_clzll(x); ++ y += i*10; ++ x >>= i; ++#else ++ while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/ + while( x>15 ){ y += 10; x >>= 1; } ++#endif + } + return a[x&7] + y - 10; + } +@@ -24784,20 +31714,134 @@ SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){ + } + #endif /* SQLITE_OMIT_VIRTUALTABLE */ + ++#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \ ++ defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \ ++ defined(SQLITE_EXPLAIN_ESTIMATED_ROWS) + /* + ** Convert a LogEst into an integer. ++** ++** Note that this routine is only used when one or more of various ++** non-standard compile-time options is enabled. + */ + SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){ + u64 n; +- if( x<10 ) return 1; + n = x%10; + x /= 10; + if( n>=5 ) n -= 2; + else if( n>=1 ) n -= 1; +- if( x>=3 ){ +- return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3); +- } +- return (n+8)>>(3-x); ++#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \ ++ defined(SQLITE_EXPLAIN_ESTIMATED_ROWS) ++ if( x>60 ) return (u64)LARGEST_INT64; ++#else ++ /* If only SQLITE_ENABLE_STAT3_OR_STAT4 is on, then the largest input ++ ** possible to this routine is 310, resulting in a maximum x of 31 */ ++ assert( x<=60 ); ++#endif ++ return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x); ++} ++#endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */ ++ ++/* ++** Add a new name/number pair to a VList. This might require that the ++** VList object be reallocated, so return the new VList. If an OOM ++** error occurs, the original VList returned and the ++** db->mallocFailed flag is set. ++** ++** A VList is really just an array of integers. To destroy a VList, ++** simply pass it to sqlite3DbFree(). ++** ++** The first integer is the number of integers allocated for the whole ++** VList. The second integer is the number of integers actually used. ++** Each name/number pair is encoded by subsequent groups of 3 or more ++** integers. ++** ++** Each name/number pair starts with two integers which are the numeric ++** value for the pair and the size of the name/number pair, respectively. ++** The text name overlays one or more following integers. The text name ++** is always zero-terminated. ++** ++** Conceptually: ++** ++** struct VList { ++** int nAlloc; // Number of allocated slots ++** int nUsed; // Number of used slots ++** struct VListEntry { ++** int iValue; // Value for this entry ++** int nSlot; // Slots used by this entry ++** // ... variable name goes here ++** } a[0]; ++** } ++** ++** During code generation, pointers to the variable names within the ++** VList are taken. When that happens, nAlloc is set to zero as an ++** indication that the VList may never again be enlarged, since the ++** accompanying realloc() would invalidate the pointers. ++*/ ++SQLITE_PRIVATE VList *sqlite3VListAdd( ++ sqlite3 *db, /* The database connection used for malloc() */ ++ VList *pIn, /* The input VList. Might be NULL */ ++ const char *zName, /* Name of symbol to add */ ++ int nName, /* Bytes of text in zName */ ++ int iVal /* Value to associate with zName */ ++){ ++ int nInt; /* number of sizeof(int) objects needed for zName */ ++ char *z; /* Pointer to where zName will be stored */ ++ int i; /* Index in pIn[] where zName is stored */ ++ ++ nInt = nName/4 + 3; ++ assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */ ++ if( pIn==0 || pIn[1]+nInt > pIn[0] ){ ++ /* Enlarge the allocation */ ++ sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt; ++ VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int)); ++ if( pOut==0 ) return pIn; ++ if( pIn==0 ) pOut[1] = 2; ++ pIn = pOut; ++ pIn[0] = nAlloc; ++ } ++ i = pIn[1]; ++ pIn[i] = iVal; ++ pIn[i+1] = nInt; ++ z = (char*)&pIn[i+2]; ++ pIn[1] = i+nInt; ++ assert( pIn[1]<=pIn[0] ); ++ memcpy(z, zName, nName); ++ z[nName] = 0; ++ return pIn; ++} ++ ++/* ++** Return a pointer to the name of a variable in the given VList that ++** has the value iVal. Or return a NULL if there is no such variable in ++** the list ++*/ ++SQLITE_PRIVATE const char *sqlite3VListNumToName(VList *pIn, int iVal){ ++ int i, mx; ++ if( pIn==0 ) return 0; ++ mx = pIn[1]; ++ i = 2; ++ do{ ++ if( pIn[i]==iVal ) return (char*)&pIn[i+2]; ++ i += pIn[i+1]; ++ }while( i */ + + /* Turn bulk memory into a hash table object by initializing the +@@ -24858,8 +31903,12 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){ + static unsigned int strHash(const char *z){ + unsigned int h = 0; + unsigned char c; +- while( (c = (unsigned char)*z++)!=0 ){ +- h = (h<<3) ^ h ^ sqlite3UpperToLower[c]; ++ while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/ ++ /* Knuth multiplicative hashing. (Sorting & Searching, p. 510). ++ ** 0x9e3779b1 is 2654435761 which is the closest prime number to ++ ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */ ++ h += sqlite3UpperToLower[c]; ++ h *= 0x9e3779b1; + } + return h; + } +@@ -24939,8 +31988,9 @@ static int rehash(Hash *pH, unsigned int new_size){ + } + + /* This function (for internal use only) locates an element in an +-** hash table that matches the given key. The hash for this key is +-** also computed and returned in the *pH parameter. ++** hash table that matches the given key. If no element is found, ++** a pointer to a static null element with HashElem.data==0 is returned. ++** If pH is not NULL, then the hash for this key is written to *pH. + */ + static HashElem *findElementWithHash( + const Hash *pH, /* The pH to be searched */ +@@ -24948,10 +31998,11 @@ static HashElem *findElementWithHash( + unsigned int *pHash /* Write the hash value here */ + ){ + HashElem *elem; /* Used to loop thru the element list */ +- int count; /* Number of elements left to test */ ++ unsigned int count; /* Number of elements left to test */ + unsigned int h; /* The computed hash */ ++ static HashElem nullElement = { 0, 0, 0, 0 }; + +- if( pH->ht ){ ++ if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/ + struct _ht *pEntry; + h = strHash(pKey) % pH->htsize; + pEntry = &pH->ht[h]; +@@ -24962,7 +32013,7 @@ static HashElem *findElementWithHash( + elem = pH->first; + count = pH->count; + } +- *pHash = h; ++ if( pHash ) *pHash = h; + while( count-- ){ + assert( elem!=0 ); + if( sqlite3StrICmp(elem->pKey,pKey)==0 ){ +@@ -24970,7 +32021,7 @@ static HashElem *findElementWithHash( + } + elem = elem->next; + } +- return 0; ++ return &nullElement; + } + + /* Remove a single entry from the hash table given a pointer to that +@@ -24995,8 +32046,8 @@ static void removeElementGivenHash( + if( pEntry->chain==elem ){ + pEntry->chain = elem->next; + } ++ assert( pEntry->count>0 ); + pEntry->count--; +- assert( pEntry->count>=0 ); + } + sqlite3_free( elem ); + pH->count--; +@@ -25012,13 +32063,9 @@ static void removeElementGivenHash( + ** found, or NULL if there is no match. + */ + SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){ +- HashElem *elem; /* The element that matches key */ +- unsigned int h; /* A hash on key */ +- + assert( pH!=0 ); + assert( pKey!=0 ); +- elem = findElementWithHash(pH, pKey, &h); +- return elem ? elem->data : 0; ++ return findElementWithHash(pH, pKey, 0)->data; + } + + /* Insert an element into the hash table pH. The key is pKey +@@ -25043,7 +32090,7 @@ SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){ + assert( pH!=0 ); + assert( pKey!=0 ); + elem = findElementWithHash(pH,pKey,&h); +- if( elem ){ ++ if( elem->data ){ + void *old_data = elem->data; + if( data==0 ){ + removeElementGivenHash(pH,elem,h); +@@ -25072,172 +32119,190 @@ SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){ + /************** End of hash.c ************************************************/ + /************** Begin file opcodes.c *****************************************/ + /* Automatically generated. Do not edit */ +-/* See the mkopcodec.awk script for details. */ +-#if !defined(SQLITE_OMIT_EXPLAIN) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) ++/* See the tool/mkopcodec.tcl script for details. */ ++#if !defined(SQLITE_OMIT_EXPLAIN) \ ++ || defined(VDBE_PROFILE) \ ++ || defined(SQLITE_DEBUG) + #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG) + # define OpHelp(X) "\0" X + #else + # define OpHelp(X) + #endif + SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){ +- static const char *const azName[] = { "?", +- /* 1 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"), +- /* 2 */ "Savepoint" OpHelp(""), +- /* 3 */ "AutoCommit" OpHelp(""), +- /* 4 */ "Transaction" OpHelp(""), +- /* 5 */ "SorterNext" OpHelp(""), +- /* 6 */ "PrevIfOpen" OpHelp(""), +- /* 7 */ "NextIfOpen" OpHelp(""), +- /* 8 */ "Prev" OpHelp(""), +- /* 9 */ "Next" OpHelp(""), +- /* 10 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"), +- /* 11 */ "Checkpoint" OpHelp(""), +- /* 12 */ "JournalMode" OpHelp(""), +- /* 13 */ "Vacuum" OpHelp(""), +- /* 14 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"), +- /* 15 */ "VUpdate" OpHelp("data=r[P3@P2]"), +- /* 16 */ "Goto" OpHelp(""), +- /* 17 */ "Gosub" OpHelp(""), +- /* 18 */ "Return" OpHelp(""), +- /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"), +- /* 20 */ "InitCoroutine" OpHelp(""), +- /* 21 */ "EndCoroutine" OpHelp(""), +- /* 22 */ "Yield" OpHelp(""), +- /* 23 */ "HaltIfNull" OpHelp("if r[P3]=null halt"), +- /* 24 */ "Halt" OpHelp(""), +- /* 25 */ "Integer" OpHelp("r[P2]=P1"), +- /* 26 */ "Int64" OpHelp("r[P2]=P4"), +- /* 27 */ "String" OpHelp("r[P2]='P4' (len=P1)"), +- /* 28 */ "Null" OpHelp("r[P2..P3]=NULL"), +- /* 29 */ "SoftNull" OpHelp("r[P1]=NULL"), +- /* 30 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"), +- /* 31 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"), +- /* 32 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"), +- /* 33 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"), +- /* 34 */ "SCopy" OpHelp("r[P2]=r[P1]"), +- /* 35 */ "ResultRow" OpHelp("output=r[P1@P2]"), +- /* 36 */ "CollSeq" OpHelp(""), +- /* 37 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"), +- /* 38 */ "MustBeInt" OpHelp(""), +- /* 39 */ "RealAffinity" OpHelp(""), +- /* 40 */ "Cast" OpHelp("affinity(r[P1])"), +- /* 41 */ "Permutation" OpHelp(""), +- /* 42 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"), +- /* 43 */ "Jump" OpHelp(""), +- /* 44 */ "Once" OpHelp(""), +- /* 45 */ "If" OpHelp(""), +- /* 46 */ "IfNot" OpHelp(""), +- /* 47 */ "Column" OpHelp("r[P3]=PX"), +- /* 48 */ "Affinity" OpHelp("affinity(r[P1@P2])"), +- /* 49 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"), +- /* 50 */ "Count" OpHelp("r[P2]=count()"), +- /* 51 */ "ReadCookie" OpHelp(""), +- /* 52 */ "SetCookie" OpHelp(""), +- /* 53 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"), +- /* 54 */ "OpenRead" OpHelp("root=P2 iDb=P3"), +- /* 55 */ "OpenWrite" OpHelp("root=P2 iDb=P3"), +- /* 56 */ "OpenAutoindex" OpHelp("nColumn=P2"), +- /* 57 */ "OpenEphemeral" OpHelp("nColumn=P2"), +- /* 58 */ "SorterOpen" OpHelp(""), +- /* 59 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"), +- /* 60 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"), +- /* 61 */ "Close" OpHelp(""), +- /* 62 */ "SeekLT" OpHelp("key=r[P3@P4]"), +- /* 63 */ "SeekLE" OpHelp("key=r[P3@P4]"), +- /* 64 */ "SeekGE" OpHelp("key=r[P3@P4]"), +- /* 65 */ "SeekGT" OpHelp("key=r[P3@P4]"), +- /* 66 */ "Seek" OpHelp("intkey=r[P2]"), +- /* 67 */ "NoConflict" OpHelp("key=r[P3@P4]"), +- /* 68 */ "NotFound" OpHelp("key=r[P3@P4]"), +- /* 69 */ "Found" OpHelp("key=r[P3@P4]"), +- /* 70 */ "NotExists" OpHelp("intkey=r[P3]"), +- /* 71 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"), +- /* 72 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"), +- /* 73 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"), +- /* 74 */ "NewRowid" OpHelp("r[P2]=rowid"), +- /* 75 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"), +- /* 76 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"), +- /* 77 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"), +- /* 78 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"), +- /* 79 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"), +- /* 80 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"), +- /* 81 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"), +- /* 82 */ "Lt" OpHelp("if r[P1] =r[P3] goto P2"), +- /* 84 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"), +- /* 85 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"), +- /* 86 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"), +- /* 87 */ "ShiftLeft" OpHelp("r[P3]=r[P2]< >r[P1]"), +- /* 89 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"), +- /* 90 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"), +- /* 91 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"), +- /* 92 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"), +- /* 93 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"), +- /* 94 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"), +- /* 95 */ "Delete" OpHelp(""), +- /* 96 */ "BitNot" OpHelp("r[P1]= ~r[P1]"), +- /* 97 */ "String8" OpHelp("r[P2]='P4'"), +- /* 98 */ "ResetCount" OpHelp(""), +- /* 99 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"), +- /* 100 */ "SorterData" OpHelp("r[P2]=data"), +- /* 101 */ "RowKey" OpHelp("r[P2]=key"), +- /* 102 */ "RowData" OpHelp("r[P2]=data"), +- /* 103 */ "Rowid" OpHelp("r[P2]=rowid"), +- /* 104 */ "NullRow" OpHelp(""), +- /* 105 */ "Last" OpHelp(""), +- /* 106 */ "SorterSort" OpHelp(""), +- /* 107 */ "Sort" OpHelp(""), +- /* 108 */ "Rewind" OpHelp(""), +- /* 109 */ "SorterInsert" OpHelp(""), +- /* 110 */ "IdxInsert" OpHelp("key=r[P2]"), +- /* 111 */ "IdxDelete" OpHelp("key=r[P2@P3]"), +- /* 112 */ "IdxRowid" OpHelp("r[P2]=rowid"), +- /* 113 */ "IdxLE" OpHelp("key=r[P3@P4]"), +- /* 114 */ "IdxGT" OpHelp("key=r[P3@P4]"), +- /* 115 */ "IdxLT" OpHelp("key=r[P3@P4]"), +- /* 116 */ "IdxGE" OpHelp("key=r[P3@P4]"), +- /* 117 */ "Destroy" OpHelp(""), +- /* 118 */ "Clear" OpHelp(""), +- /* 119 */ "ResetSorter" OpHelp(""), +- /* 120 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"), +- /* 121 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"), +- /* 122 */ "ParseSchema" OpHelp(""), +- /* 123 */ "LoadAnalysis" OpHelp(""), +- /* 124 */ "DropTable" OpHelp(""), +- /* 125 */ "DropIndex" OpHelp(""), +- /* 126 */ "DropTrigger" OpHelp(""), +- /* 127 */ "IntegrityCk" OpHelp(""), +- /* 128 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"), +- /* 129 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"), +- /* 130 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"), +- /* 131 */ "Program" OpHelp(""), +- /* 132 */ "Param" OpHelp(""), +- /* 133 */ "Real" OpHelp("r[P2]=P4"), +- /* 134 */ "FkCounter" OpHelp("fkctr[P1]+=P2"), +- /* 135 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"), +- /* 136 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"), +- /* 137 */ "IfPos" OpHelp("if r[P1]>0 goto P2"), +- /* 138 */ "IfNeg" OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"), +- /* 139 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]+=P3, goto P2"), +- /* 140 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"), +- /* 141 */ "JumpZeroIncr" OpHelp("if (r[P1]++)==0 ) goto P2"), +- /* 142 */ "AggFinal" OpHelp("accum=r[P1] N=P2"), +- /* 143 */ "IncrVacuum" OpHelp(""), +- /* 144 */ "Expire" OpHelp(""), +- /* 145 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"), +- /* 146 */ "VBegin" OpHelp(""), +- /* 147 */ "VCreate" OpHelp(""), +- /* 148 */ "VDestroy" OpHelp(""), +- /* 149 */ "VOpen" OpHelp(""), +- /* 150 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"), +- /* 151 */ "VNext" OpHelp(""), +- /* 152 */ "VRename" OpHelp(""), +- /* 153 */ "Pagecount" OpHelp(""), +- /* 154 */ "MaxPgcnt" OpHelp(""), +- /* 155 */ "Init" OpHelp("Start at P2"), +- /* 156 */ "Noop" OpHelp(""), +- /* 157 */ "Explain" OpHelp(""), ++ static const char *const azName[] = { ++ /* 0 */ "Savepoint" OpHelp(""), ++ /* 1 */ "AutoCommit" OpHelp(""), ++ /* 2 */ "Transaction" OpHelp(""), ++ /* 3 */ "SorterNext" OpHelp(""), ++ /* 4 */ "Prev" OpHelp(""), ++ /* 5 */ "Next" OpHelp(""), ++ /* 6 */ "Checkpoint" OpHelp(""), ++ /* 7 */ "JournalMode" OpHelp(""), ++ /* 8 */ "Vacuum" OpHelp(""), ++ /* 9 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"), ++ /* 10 */ "VUpdate" OpHelp("data=r[P3@P2]"), ++ /* 11 */ "Goto" OpHelp(""), ++ /* 12 */ "Gosub" OpHelp(""), ++ /* 13 */ "InitCoroutine" OpHelp(""), ++ /* 14 */ "Yield" OpHelp(""), ++ /* 15 */ "MustBeInt" OpHelp(""), ++ /* 16 */ "Jump" OpHelp(""), ++ /* 17 */ "Once" OpHelp(""), ++ /* 18 */ "If" OpHelp(""), ++ /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"), ++ /* 20 */ "IfNot" OpHelp(""), ++ /* 21 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"), ++ /* 22 */ "SeekLT" OpHelp("key=r[P3@P4]"), ++ /* 23 */ "SeekLE" OpHelp("key=r[P3@P4]"), ++ /* 24 */ "SeekGE" OpHelp("key=r[P3@P4]"), ++ /* 25 */ "SeekGT" OpHelp("key=r[P3@P4]"), ++ /* 26 */ "IfNoHope" OpHelp("key=r[P3@P4]"), ++ /* 27 */ "NoConflict" OpHelp("key=r[P3@P4]"), ++ /* 28 */ "NotFound" OpHelp("key=r[P3@P4]"), ++ /* 29 */ "Found" OpHelp("key=r[P3@P4]"), ++ /* 30 */ "SeekRowid" OpHelp("intkey=r[P3]"), ++ /* 31 */ "NotExists" OpHelp("intkey=r[P3]"), ++ /* 32 */ "Last" OpHelp(""), ++ /* 33 */ "IfSmaller" OpHelp(""), ++ /* 34 */ "SorterSort" OpHelp(""), ++ /* 35 */ "Sort" OpHelp(""), ++ /* 36 */ "Rewind" OpHelp(""), ++ /* 37 */ "IdxLE" OpHelp("key=r[P3@P4]"), ++ /* 38 */ "IdxGT" OpHelp("key=r[P3@P4]"), ++ /* 39 */ "IdxLT" OpHelp("key=r[P3@P4]"), ++ /* 40 */ "IdxGE" OpHelp("key=r[P3@P4]"), ++ /* 41 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"), ++ /* 42 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"), ++ /* 43 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"), ++ /* 44 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"), ++ /* 45 */ "Program" OpHelp(""), ++ /* 46 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"), ++ /* 47 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"), ++ /* 48 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"), ++ /* 49 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"), ++ /* 50 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"), ++ /* 51 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"), ++ /* 52 */ "Ne" OpHelp("IF r[P3]!=r[P1]"), ++ /* 53 */ "Eq" OpHelp("IF r[P3]==r[P1]"), ++ /* 54 */ "Gt" OpHelp("IF r[P3]>r[P1]"), ++ /* 55 */ "Le" OpHelp("IF r[P3]<=r[P1]"), ++ /* 56 */ "Lt" OpHelp("IF r[P3] =r[P1]"), ++ /* 58 */ "ElseNotEq" OpHelp(""), ++ /* 59 */ "IncrVacuum" OpHelp(""), ++ /* 60 */ "VNext" OpHelp(""), ++ /* 61 */ "Init" OpHelp("Start at P2"), ++ /* 62 */ "PureFunc0" OpHelp(""), ++ /* 63 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"), ++ /* 64 */ "PureFunc" OpHelp(""), ++ /* 65 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"), ++ /* 66 */ "Return" OpHelp(""), ++ /* 67 */ "EndCoroutine" OpHelp(""), ++ /* 68 */ "HaltIfNull" OpHelp("if r[P3]=null halt"), ++ /* 69 */ "Halt" OpHelp(""), ++ /* 70 */ "Integer" OpHelp("r[P2]=P1"), ++ /* 71 */ "Int64" OpHelp("r[P2]=P4"), ++ /* 72 */ "String" OpHelp("r[P2]='P4' (len=P1)"), ++ /* 73 */ "Null" OpHelp("r[P2..P3]=NULL"), ++ /* 74 */ "SoftNull" OpHelp("r[P1]=NULL"), ++ /* 75 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"), ++ /* 76 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"), ++ /* 77 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"), ++ /* 78 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"), ++ /* 79 */ "SCopy" OpHelp("r[P2]=r[P1]"), ++ /* 80 */ "IntCopy" OpHelp("r[P2]=r[P1]"), ++ /* 81 */ "ResultRow" OpHelp("output=r[P1@P2]"), ++ /* 82 */ "CollSeq" OpHelp(""), ++ /* 83 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"), ++ /* 84 */ "RealAffinity" OpHelp(""), ++ /* 85 */ "Cast" OpHelp("affinity(r[P1])"), ++ /* 86 */ "Permutation" OpHelp(""), ++ /* 87 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"), ++ /* 88 */ "IsTrue" OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"), ++ /* 89 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"), ++ /* 90 */ "Column" OpHelp("r[P3]=PX"), ++ /* 91 */ "Affinity" OpHelp("affinity(r[P1@P2])"), ++ /* 92 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"), ++ /* 93 */ "Count" OpHelp("r[P2]=count()"), ++ /* 94 */ "ReadCookie" OpHelp(""), ++ /* 95 */ "SetCookie" OpHelp(""), ++ /* 96 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"), ++ /* 97 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"), ++ /* 98 */ "ShiftLeft" OpHelp("r[P3]=r[P2]< >r[P1]"), ++ /* 100 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"), ++ /* 101 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"), ++ /* 102 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"), ++ /* 103 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"), ++ /* 104 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"), ++ /* 105 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"), ++ /* 106 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"), ++ /* 107 */ "BitNot" OpHelp("r[P2]= ~r[P1]"), ++ /* 108 */ "OpenRead" OpHelp("root=P2 iDb=P3"), ++ /* 109 */ "OpenWrite" OpHelp("root=P2 iDb=P3"), ++ /* 110 */ "String8" OpHelp("r[P2]='P4'"), ++ /* 111 */ "OpenDup" OpHelp(""), ++ /* 112 */ "OpenAutoindex" OpHelp("nColumn=P2"), ++ /* 113 */ "OpenEphemeral" OpHelp("nColumn=P2"), ++ /* 114 */ "SorterOpen" OpHelp(""), ++ /* 115 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"), ++ /* 116 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"), ++ /* 117 */ "Close" OpHelp(""), ++ /* 118 */ "ColumnsUsed" OpHelp(""), ++ /* 119 */ "SeekHit" OpHelp("seekHit=P2"), ++ /* 120 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"), ++ /* 121 */ "NewRowid" OpHelp("r[P2]=rowid"), ++ /* 122 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"), ++ /* 123 */ "Delete" OpHelp(""), ++ /* 124 */ "ResetCount" OpHelp(""), ++ /* 125 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"), ++ /* 126 */ "SorterData" OpHelp("r[P2]=data"), ++ /* 127 */ "RowData" OpHelp("r[P2]=data"), ++ /* 128 */ "Rowid" OpHelp("r[P2]=rowid"), ++ /* 129 */ "NullRow" OpHelp(""), ++ /* 130 */ "SeekEnd" OpHelp(""), ++ /* 131 */ "SorterInsert" OpHelp("key=r[P2]"), ++ /* 132 */ "IdxInsert" OpHelp("key=r[P2]"), ++ /* 133 */ "IdxDelete" OpHelp("key=r[P2@P3]"), ++ /* 134 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"), ++ /* 135 */ "IdxRowid" OpHelp("r[P2]=rowid"), ++ /* 136 */ "Destroy" OpHelp(""), ++ /* 137 */ "Clear" OpHelp(""), ++ /* 138 */ "ResetSorter" OpHelp(""), ++ /* 139 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"), ++ /* 140 */ "SqlExec" OpHelp(""), ++ /* 141 */ "ParseSchema" OpHelp(""), ++ /* 142 */ "LoadAnalysis" OpHelp(""), ++ /* 143 */ "DropTable" OpHelp(""), ++ /* 144 */ "DropIndex" OpHelp(""), ++ /* 145 */ "Real" OpHelp("r[P2]=P4"), ++ /* 146 */ "DropTrigger" OpHelp(""), ++ /* 147 */ "IntegrityCk" OpHelp(""), ++ /* 148 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"), ++ /* 149 */ "Param" OpHelp(""), ++ /* 150 */ "FkCounter" OpHelp("fkctr[P1]+=P2"), ++ /* 151 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"), ++ /* 152 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"), ++ /* 153 */ "AggInverse" OpHelp("accum=r[P3] inverse(r[P2@P5])"), ++ /* 154 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"), ++ /* 155 */ "AggStep1" OpHelp("accum=r[P3] step(r[P2@P5])"), ++ /* 156 */ "AggValue" OpHelp("r[P3]=value N=P2"), ++ /* 157 */ "AggFinal" OpHelp("accum=r[P1] N=P2"), ++ /* 158 */ "Expire" OpHelp(""), ++ /* 159 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"), ++ /* 160 */ "VBegin" OpHelp(""), ++ /* 161 */ "VCreate" OpHelp(""), ++ /* 162 */ "VDestroy" OpHelp(""), ++ /* 163 */ "VOpen" OpHelp(""), ++ /* 164 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"), ++ /* 165 */ "VRename" OpHelp(""), ++ /* 166 */ "Pagecount" OpHelp(""), ++ /* 167 */ "MaxPgcnt" OpHelp(""), ++ /* 168 */ "Trace" OpHelp(""), ++ /* 169 */ "CursorHint" OpHelp(""), ++ /* 170 */ "Noop" OpHelp(""), ++ /* 171 */ "Explain" OpHelp(""), ++ /* 172 */ "Abortable" OpHelp(""), + }; + return azName[i]; + } +@@ -25290,6 +32355,7 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){ + ** * Definitions of sqlite3_vfs objects for all locking methods + ** plus implementations of sqlite3_os_init() and sqlite3_os_end(). + */ ++/* #include "sqliteInt.h" */ + #if SQLITE_OS_UNIX /* This file is used on unix only */ + + /* +@@ -25317,12 +32383,26 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){ + # endif + #endif + ++/* Use pread() and pwrite() if they are available */ ++#if defined(__APPLE__) ++# define HAVE_PREAD 1 ++# define HAVE_PWRITE 1 ++#endif ++#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64) ++# undef USE_PREAD ++# define USE_PREAD64 1 ++#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE) ++# undef USE_PREAD64 ++# define USE_PREAD 1 ++#endif ++ + /* + ** standard include files. + */ + #include + #include + #include ++#include + #include + /* #include */ + #include +@@ -25332,7 +32412,7 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){ + #endif + + #if SQLITE_ENABLE_LOCKING_STYLE +-# include ++/* # include */ + # include + # include + #endif /* SQLITE_ENABLE_LOCKING_STYLE */ +@@ -25368,12 +32448,10 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){ + #define SQLITE_FSFLAGS_IS_MSDOS 0x1 + + /* +-** If we are to be thread-safe, include the pthreads header and define +-** the SQLITE_UNIX_THREADS macro. ++** If we are to be thread-safe, include the pthreads header. + */ + #if SQLITE_THREADSAFE + /* # include */ +-# define SQLITE_UNIX_THREADS 1 + #endif + + /* +@@ -25395,6 +32473,11 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){ + */ + #define MAX_PATHNAME 512 + ++/* ++** Maximum supported symbolic links ++*/ ++#define SQLITE_MAX_SYMLINKS 100 ++ + /* Always cast the getpid() return type for compatibility with + ** kernel modules in VxWorks. */ + #define osGetpid(X) (pid_t)getpid() +@@ -25437,7 +32520,7 @@ struct unixFile { + unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ + int lastErrno; /* The unix errno from last I/O error */ + void *lockingContext; /* Locking style specific state */ +- UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ ++ UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */ + const char *zPath; /* Name of the file */ + unixShm *pShm; /* Shared memory segment information */ + int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ +@@ -25448,16 +32531,17 @@ struct unixFile { + sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ + void *pMapRegion; /* Memory mapped region */ + #endif +-#ifdef __QNXNTO__ + int sectorSize; /* Device sector size */ + int deviceCharacteristics; /* Precomputed device characteristics */ +-#endif + #if SQLITE_ENABLE_LOCKING_STYLE + int openFlags; /* The flags specified at open() */ + #endif + #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) + unsigned fsFlags; /* cached details from statfs() */ + #endif ++#ifdef SQLITE_ENABLE_SETLK_TIMEOUT ++ unsigned iBusyTimeout; /* Wait this many millisec on locks */ ++#endif + #if OS_VXWORKS + struct vxworksFileId *pId; /* Unique file ID */ + #endif +@@ -25504,8 +32588,6 @@ static pid_t randomnessPid = 0; + #define UNIXFILE_DELETE 0x20 /* Delete on close */ + #define UNIXFILE_URI 0x40 /* Filename might have query parameters */ + #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */ +-#define UNIXFILE_WARNED 0x0100 /* verifyDbFile() warnings issued */ +-#define UNIXFILE_BLOCK 0x0200 /* Next SHM lock might block */ + + /* + ** Include code that is common to all os_*.c files +@@ -25549,8 +32631,8 @@ static pid_t randomnessPid = 0; + */ + #ifdef SQLITE_PERFORMANCE_TRACE + +-/* +-** hwtime.h contains inline assembler code for implementing ++/* ++** hwtime.h contains inline assembler code for implementing + ** high-performance timing routines. + */ + /************** Include hwtime.h in the middle of os_common.h ****************/ +@@ -25570,8 +32652,8 @@ static pid_t randomnessPid = 0; + ** This file contains inline asm code for retrieving "high-performance" + ** counters for x86 class CPUs. + */ +-#ifndef _HWTIME_H_ +-#define _HWTIME_H_ ++#ifndef SQLITE_HWTIME_H ++#define SQLITE_HWTIME_H + + /* + ** The following routine only works on pentium-class (or newer) processors. +@@ -25639,7 +32721,7 @@ SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); } + + #endif + +-#endif /* !defined(_HWTIME_H_) */ ++#endif /* !defined(SQLITE_HWTIME_H) */ + + /************** End of hwtime.h **********************************************/ + /************** Continuing where we left off in os_common.h ******************/ +@@ -25660,14 +32742,14 @@ static sqlite_uint64 g_elapsed; + ** of code will give us the ability to simulate a disk I/O error. This + ** is used for testing the I/O recovery logic. + */ +-#ifdef SQLITE_TEST +-SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */ +-SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */ +-SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */ +-SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */ +-SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */ +-SQLITE_API int sqlite3_diskfull_pending = 0; +-SQLITE_API int sqlite3_diskfull = 0; ++#if defined(SQLITE_TEST) ++SQLITE_API extern int sqlite3_io_error_hit; ++SQLITE_API extern int sqlite3_io_error_hardhit; ++SQLITE_API extern int sqlite3_io_error_pending; ++SQLITE_API extern int sqlite3_io_error_persist; ++SQLITE_API extern int sqlite3_io_error_benign; ++SQLITE_API extern int sqlite3_diskfull_pending; ++SQLITE_API extern int sqlite3_diskfull; + #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X) + #define SimulateIOError(CODE) \ + if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \ +@@ -25693,17 +32775,17 @@ static void local_ioerr(){ + #define SimulateIOErrorBenign(X) + #define SimulateIOError(A) + #define SimulateDiskfullError(A) +-#endif ++#endif /* defined(SQLITE_TEST) */ + + /* + ** When testing, keep a count of the number of open files. + */ +-#ifdef SQLITE_TEST +-SQLITE_API int sqlite3_open_file_count = 0; ++#if defined(SQLITE_TEST) ++SQLITE_API extern int sqlite3_open_file_count; + #define OpenCounter(X) sqlite3_open_file_count+=(X) + #else + #define OpenCounter(X) +-#endif ++#endif /* defined(SQLITE_TEST) */ + + #endif /* !defined(_OS_COMMON_H_) */ + +@@ -25756,6 +32838,20 @@ SQLITE_API int sqlite3_open_file_count = 0; + # define lseek lseek64 + #endif + ++#ifdef __linux__ ++/* ++** Linux-specific IOCTL magic numbers used for controlling F2FS ++*/ ++#define F2FS_IOCTL_MAGIC 0xf5 ++#define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1) ++#define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2) ++#define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3) ++#define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5) ++#define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, u32) ++#define F2FS_FEATURE_ATOMIC_WRITE 0x0004 ++#endif /* __linux__ */ ++ ++ + /* + ** Different Unix systems declare open() in different ways. Same use + ** open(const char*,int,mode_t). Others use open(const char*,int,...). +@@ -25768,19 +32864,6 @@ static int posixOpen(const char *zFile, int flags, int mode){ + return open(zFile, flags, mode); + } + +-/* +-** On some systems, calls to fchown() will trigger a message in a security +-** log if they come from non-root processes. So avoid calling fchown() if +-** we are not running as root. +-*/ +-static int posixFchown(int fd, uid_t uid, gid_t gid){ +-#if OS_VXWORKS +- return 0; +-#else +- return geteuid() ? 0 : fchown(fd,uid,gid); +-#endif +-} +- + /* Forward reference */ + static int openDirectory(const char*, int*); + static int unixGetpagesize(void); +@@ -25846,7 +32929,7 @@ static struct unix_syscall { + #else + { "pread64", (sqlite3_syscall_ptr)0, 0 }, + #endif +-#define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent) ++#define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent) + + { "write", (sqlite3_syscall_ptr)write, 0 }, + #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) +@@ -25864,10 +32947,10 @@ static struct unix_syscall { + #else + { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, + #endif +-#define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\ ++#define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\ + aSyscall[13].pCurrent) + +- { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, ++ { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, + #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) + + #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE +@@ -25889,29 +32972,89 @@ static struct unix_syscall { + { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 }, + #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent) + +- { "fchown", (sqlite3_syscall_ptr)posixFchown, 0 }, ++#if defined(HAVE_FCHOWN) ++ { "fchown", (sqlite3_syscall_ptr)fchown, 0 }, ++#else ++ { "fchown", (sqlite3_syscall_ptr)0, 0 }, ++#endif + #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) + ++#if defined(HAVE_FCHOWN) ++ { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 }, ++#else ++ { "geteuid", (sqlite3_syscall_ptr)0, 0 }, ++#endif ++#define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent) ++ + #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 +- { "mmap", (sqlite3_syscall_ptr)mmap, 0 }, +-#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent) ++ { "mmap", (sqlite3_syscall_ptr)mmap, 0 }, ++#else ++ { "mmap", (sqlite3_syscall_ptr)0, 0 }, ++#endif ++#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent) + ++#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 + { "munmap", (sqlite3_syscall_ptr)munmap, 0 }, +-#define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent) ++#else ++ { "munmap", (sqlite3_syscall_ptr)0, 0 }, ++#endif ++#define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent) + +-#if HAVE_MREMAP ++#if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) + { "mremap", (sqlite3_syscall_ptr)mremap, 0 }, + #else + { "mremap", (sqlite3_syscall_ptr)0, 0 }, + #endif +-#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent) ++#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent) ++ ++#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 + { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 }, +-#define osGetpagesize ((int(*)(void))aSyscall[24].pCurrent) ++#else ++ { "getpagesize", (sqlite3_syscall_ptr)0, 0 }, ++#endif ++#define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent) ++ ++#if defined(HAVE_READLINK) ++ { "readlink", (sqlite3_syscall_ptr)readlink, 0 }, ++#else ++ { "readlink", (sqlite3_syscall_ptr)0, 0 }, ++#endif ++#define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent) + ++#if defined(HAVE_LSTAT) ++ { "lstat", (sqlite3_syscall_ptr)lstat, 0 }, ++#else ++ { "lstat", (sqlite3_syscall_ptr)0, 0 }, + #endif ++#define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent) ++ ++#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) ++# ifdef __ANDROID__ ++ { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 }, ++# else ++ { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 }, ++# endif ++#else ++ { "ioctl", (sqlite3_syscall_ptr)0, 0 }, ++#endif ++#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent) + + }; /* End of the overrideable system calls */ + ++ ++/* ++** On some systems, calls to fchown() will trigger a message in a security ++** log if they come from non-root processes. So avoid calling fchown() if ++** we are not running as root. ++*/ ++static int robustFchown(int fd, uid_t uid, gid_t gid){ ++#if defined(HAVE_FCHOWN) ++ return osGeteuid() ? 0 : osFchown(fd,uid,gid); ++#else ++ return 0; ++#endif ++} ++ + /* + ** This is the xSetSystemCall() method of sqlite3_vfs for all of the + ** "unix" VFSes. Return SQLITE_OK opon successfully updating the +@@ -26071,16 +33214,30 @@ static int robust_open(const char *z, int f, mode_t m){ + ** unixEnterMutex() + ** assert( unixMutexHeld() ); + ** unixEnterLeave() ++** ++** To prevent deadlock, the global unixBigLock must must be acquired ++** before the unixInodeInfo.pLockMutex mutex, if both are held. It is ++** OK to get the pLockMutex without holding unixBigLock first, but if ++** that happens, the unixBigLock mutex must not be acquired until after ++** pLockMutex is released. ++** ++** OK: enter(unixBigLock), enter(pLockInfo) ++** OK: enter(unixBigLock) ++** OK: enter(pLockInfo) ++** ERROR: enter(pLockInfo), enter(unixBigLock) + */ ++static sqlite3_mutex *unixBigLock = 0; + static void unixEnterMutex(void){ +- sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); ++ assert( sqlite3_mutex_notheld(unixBigLock) ); /* Not a recursive mutex */ ++ sqlite3_mutex_enter(unixBigLock); + } + static void unixLeaveMutex(void){ +- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); ++ assert( sqlite3_mutex_held(unixBigLock) ); ++ sqlite3_mutex_leave(unixBigLock); + } + #ifdef SQLITE_DEBUG + static int unixMutexHeld(void) { +- return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); ++ return sqlite3_mutex_held(unixBigLock); + } + #endif + +@@ -26196,23 +33353,12 @@ static int robust_ftruncate(int h, sqlite3_int64 sz){ + ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. + */ + static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { ++ assert( (sqliteIOErr == SQLITE_IOERR_LOCK) || ++ (sqliteIOErr == SQLITE_IOERR_UNLOCK) || ++ (sqliteIOErr == SQLITE_IOERR_RDLOCK) || ++ (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ); + switch (posixError) { +-#if 0 +- /* At one point this code was not commented out. In theory, this branch +- ** should never be hit, as this function should only be called after +- ** a locking-related function (i.e. fcntl()) has returned non-zero with +- ** the value of errno as the first argument. Since a system call has failed, +- ** errno should be non-zero. +- ** +- ** Despite this, if errno really is zero, we still don't want to return +- ** SQLITE_OK. The system call failed, and *some* SQLite error should be +- ** propagated back to the caller. Commenting this branch out means errno==0 +- ** will be handled by the "default:" case below. +- */ +- case 0: +- return SQLITE_OK; +-#endif +- ++ case EACCES: + case EAGAIN: + case ETIMEDOUT: + case EBUSY: +@@ -26222,41 +33368,9 @@ static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { + * introspection, in which it actually means what it says */ + return SQLITE_BUSY; + +- case EACCES: +- /* EACCES is like EAGAIN during locking operations, but not any other time*/ +- if( (sqliteIOErr == SQLITE_IOERR_LOCK) || +- (sqliteIOErr == SQLITE_IOERR_UNLOCK) || +- (sqliteIOErr == SQLITE_IOERR_RDLOCK) || +- (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ +- return SQLITE_BUSY; +- } +- /* else fall through */ + case EPERM: + return SQLITE_PERM; + +-#if EOPNOTSUPP!=ENOTSUP +- case EOPNOTSUPP: +- /* something went terribly awry, unless during file system support +- * introspection, in which it actually means what it says */ +-#endif +-#ifdef ENOTSUP +- case ENOTSUP: +- /* invalid fd, unless during file system support introspection, in which +- * it actually means what it says */ +-#endif +- case EIO: +- case EBADF: +- case EINVAL: +- case ENOTCONN: +- case ENODEV: +- case ENXIO: +- case ENOENT: +-#ifdef ESTALE /* ESTALE is not defined on Interix systems */ +- case ESTALE: +-#endif +- case ENOSYS: +- /* these should force the client to close the file and reconnect */ +- + default: + return sqliteIOErr; + } +@@ -26500,28 +33614,52 @@ struct unixFileId { + #if OS_VXWORKS + struct vxworksFileId *pId; /* Unique file ID for vxworks. */ + #else +- ino_t ino; /* Inode number */ ++ /* We are told that some versions of Android contain a bug that ++ ** sizes ino_t at only 32-bits instead of 64-bits. (See ++ ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c) ++ ** To work around this, always allocate 64-bits for the inode number. ++ ** On small machines that only have 32-bit inodes, this wastes 4 bytes, ++ ** but that should not be a big deal. */ ++ /* WAS: ino_t ino; */ ++ u64 ino; /* Inode number */ + #endif + }; + + /* + ** An instance of the following structure is allocated for each open +-** inode. Or, on LinuxThreads, there is one of these structures for +-** each inode opened by each thread. ++** inode. + ** + ** A single inode can have multiple file descriptors, so each unixFile + ** structure contains a pointer to an instance of this object and this + ** object keeps a count of the number of unixFile pointing to it. ++** ++** Mutex rules: ++** ++** (1) Only the pLockMutex mutex must be held in order to read or write ++** any of the locking fields: ++** nShared, nLock, eFileLock, bProcessLock, pUnused ++** ++** (2) When nRef>0, then the following fields are unchanging and can ++** be read (but not written) without holding any mutex: ++** fileId, pLockMutex ++** ++** (3) With the exceptions above, all the fields may only be read ++** or written while holding the global unixBigLock mutex. ++** ++** Deadlock prevention: The global unixBigLock mutex may not ++** be acquired while holding the pLockMutex mutex. If both unixBigLock ++** and pLockMutex are needed, then unixBigLock must be acquired first. + */ + struct unixInodeInfo { + struct unixFileId fileId; /* The lookup key */ +- int nShared; /* Number of SHARED locks held */ +- unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ +- unsigned char bProcessLock; /* An exclusive process lock is held */ ++ sqlite3_mutex *pLockMutex; /* Hold this mutex for... */ ++ int nShared; /* Number of SHARED locks held */ ++ int nLock; /* Number of outstanding file locks */ ++ unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ ++ unsigned char bProcessLock; /* An exclusive process lock is held */ ++ UnixUnusedFd *pUnused; /* Unused file descriptors to close */ + int nRef; /* Number of pointers to this structure */ + unixShmNode *pShmNode; /* Shared memory associated with this inode */ +- int nLock; /* Number of outstanding file locks */ +- UnixUnusedFd *pUnused; /* Unused file descriptors to close */ + unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ + unixInodeInfo *pPrev; /* .... doubly linked */ + #if SQLITE_ENABLE_LOCKING_STYLE +@@ -26535,12 +33673,30 @@ struct unixInodeInfo { + + /* + ** A lists of all unixInodeInfo objects. ++** ++** Must hold unixBigLock in order to read or write this variable. + */ +-static unixInodeInfo *inodeList = 0; ++static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */ ++ ++#ifdef SQLITE_DEBUG ++/* ++** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not. ++** This routine is used only within assert() to help verify correct mutex ++** usage. ++*/ ++int unixFileMutexHeld(unixFile *pFile){ ++ assert( pFile->pInode ); ++ return sqlite3_mutex_held(pFile->pInode->pLockMutex); ++} ++int unixFileMutexNotheld(unixFile *pFile){ ++ assert( pFile->pInode ); ++ return sqlite3_mutex_notheld(pFile->pInode->pLockMutex); ++} ++#endif + + /* + ** +-** This function - unixLogError_x(), is only ever called via the macro ++** This function - unixLogErrorAtLine(), is only ever called via the macro + ** unixLogError(). + ** + ** It is invoked after an error occurs in an OS function and errno has been +@@ -26642,6 +33798,7 @@ static void closePendingFds(unixFile *pFile){ + unixInodeInfo *pInode = pFile->pInode; + UnixUnusedFd *p; + UnixUnusedFd *pNext; ++ assert( unixFileMutexHeld(pFile) ); + for(p=pInode->pUnused; p; p=pNext){ + pNext = p->pNext; + robust_close(pFile, p->fd, __LINE__); +@@ -26653,17 +33810,20 @@ static void closePendingFds(unixFile *pFile){ + /* + ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). + ** +-** The mutex entered using the unixEnterMutex() function must be held +-** when this function is called. ++** The global mutex must be held when this routine is called, but the mutex ++** on the inode being deleted must NOT be held. + */ + static void releaseInodeInfo(unixFile *pFile){ + unixInodeInfo *pInode = pFile->pInode; + assert( unixMutexHeld() ); ++ assert( unixFileMutexNotheld(pFile) ); + if( ALWAYS(pInode) ){ + pInode->nRef--; + if( pInode->nRef==0 ){ + assert( pInode->pShmNode==0 ); ++ sqlite3_mutex_enter(pInode->pLockMutex); + closePendingFds(pFile); ++ sqlite3_mutex_leave(pInode->pLockMutex); + if( pInode->pPrev ){ + assert( pInode->pPrev->pNext==pInode ); + pInode->pPrev->pNext = pInode->pNext; +@@ -26675,6 +33835,7 @@ static void releaseInodeInfo(unixFile *pFile){ + assert( pInode->pNext->pPrev==pInode ); + pInode->pNext->pPrev = pInode->pPrev; + } ++ sqlite3_mutex_free(pInode->pLockMutex); + sqlite3_free(pInode); + } + } +@@ -26685,8 +33846,7 @@ static void releaseInodeInfo(unixFile *pFile){ + ** describes that file descriptor. Create a new one if necessary. The + ** return value might be uninitialized if an error occurs. + ** +-** The mutex entered using the unixEnterMutex() function must be held +-** when this function is called. ++** The global mutex must held when calling this routine. + ** + ** Return an appropriate error code. + */ +@@ -26709,7 +33869,7 @@ static int findInodeInfo( + rc = osFstat(fd, &statbuf); + if( rc!=0 ){ + storeLastErrno(pFile, errno); +-#ifdef EOVERFLOW ++#if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS) + if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; + #endif + return SQLITE_IOERR; +@@ -26745,8 +33905,9 @@ static int findInodeInfo( + #if OS_VXWORKS + fileId.pId = pFile->pId; + #else +- fileId.ino = statbuf.st_ino; ++ fileId.ino = (u64)statbuf.st_ino; + #endif ++ assert( unixMutexHeld() ); + pInode = inodeList; + while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ + pInode = pInode->pNext; +@@ -26754,11 +33915,19 @@ static int findInodeInfo( + if( pInode==0 ){ + pInode = sqlite3_malloc64( sizeof(*pInode) ); + if( pInode==0 ){ +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + memset(pInode, 0, sizeof(*pInode)); + memcpy(&pInode->fileId, &fileId, sizeof(fileId)); ++ if( sqlite3GlobalConfig.bCoreMutex ){ ++ pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); ++ if( pInode->pLockMutex==0 ){ ++ sqlite3_free(pInode); ++ return SQLITE_NOMEM_BKPT; ++ } ++ } + pInode->nRef = 1; ++ assert( unixMutexHeld() ); + pInode->pNext = inodeList; + pInode->pPrev = 0; + if( inodeList ) inodeList->pPrev = pInode; +@@ -26779,7 +33948,8 @@ static int fileHasMoved(unixFile *pFile){ + #else + struct stat buf; + return pFile->pInode!=0 && +- (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino); ++ (osStat(pFile->zPath, &buf)!=0 ++ || (u64)buf.st_ino!=pFile->pInode->fileId.ino); + #endif + } + +@@ -26796,30 +33966,25 @@ static int fileHasMoved(unixFile *pFile){ + static void verifyDbFile(unixFile *pFile){ + struct stat buf; + int rc; +- if( pFile->ctrlFlags & UNIXFILE_WARNED ){ +- /* One or more of the following warnings have already been issued. Do not +- ** repeat them so as not to clutter the error log */ +- return; +- } ++ ++ /* These verifications occurs for the main database only */ ++ if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return; ++ + rc = osFstat(pFile->h, &buf); + if( rc!=0 ){ + sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath); +- pFile->ctrlFlags |= UNIXFILE_WARNED; + return; + } +- if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){ ++ if( buf.st_nlink==0 ){ + sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath); +- pFile->ctrlFlags |= UNIXFILE_WARNED; + return; + } + if( buf.st_nlink>1 ){ + sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath); +- pFile->ctrlFlags |= UNIXFILE_WARNED; + return; + } + if( fileHasMoved(pFile) ){ + sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath); +- pFile->ctrlFlags |= UNIXFILE_WARNED; + return; + } + } +@@ -26839,7 +34004,8 @@ static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ + SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); + + assert( pFile ); +- unixEnterMutex(); /* Because pFile->pInode is shared across threads */ ++ assert( pFile->eFileLock<=SHARED_LOCK ); ++ sqlite3_mutex_enter(pFile->pInode->pLockMutex); + + /* Check if a thread in this process holds such a lock */ + if( pFile->pInode->eFileLock>SHARED_LOCK ){ +@@ -26864,13 +34030,50 @@ static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ + } + #endif + +- unixLeaveMutex(); ++ sqlite3_mutex_leave(pFile->pInode->pLockMutex); + OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); + + *pResOut = reserved; + return rc; + } + ++/* ++** Set a posix-advisory-lock. ++** ++** There are two versions of this routine. If compiled with ++** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter ++** which is a pointer to a unixFile. If the unixFile->iBusyTimeout ++** value is set, then it is the number of milliseconds to wait before ++** failing the lock. The iBusyTimeout value is always reset back to ++** zero on each call. ++** ++** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking ++** attempt to set the lock. ++*/ ++#ifndef SQLITE_ENABLE_SETLK_TIMEOUT ++# define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x) ++#else ++static int osSetPosixAdvisoryLock( ++ int h, /* The file descriptor on which to take the lock */ ++ struct flock *pLock, /* The description of the lock */ ++ unixFile *pFile /* Structure holding timeout value */ ++){ ++ int rc = osFcntl(h,F_SETLK,pLock); ++ while( rc<0 && pFile->iBusyTimeout>0 ){ ++ /* On systems that support some kind of blocking file lock with a timeout, ++ ** make appropriate changes here to invoke that blocking file lock. On ++ ** generic posix, however, there is no such API. So we simply try the ++ ** lock once every millisecond until either the timeout expires, or until ++ ** the lock is obtained. */ ++ usleep(1000); ++ rc = osFcntl(h,F_SETLK,pLock); ++ pFile->iBusyTimeout--; ++ } ++ return rc; ++} ++#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */ ++ ++ + /* + ** Attempt to set a system-lock on the file pFile. The lock is + ** described by pLock. +@@ -26893,11 +34096,9 @@ static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ + static int unixFileLock(unixFile *pFile, struct flock *pLock){ + int rc; + unixInodeInfo *pInode = pFile->pInode; +- assert( unixMutexHeld() ); + assert( pInode!=0 ); +- if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock) +- && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0) +- ){ ++ assert( sqlite3_mutex_held(pInode->pLockMutex) ); ++ if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){ + if( pInode->bProcessLock==0 ){ + struct flock lock; + assert( pInode->nLock==0 ); +@@ -26905,7 +34106,7 @@ static int unixFileLock(unixFile *pFile, struct flock *pLock){ + lock.l_start = SHARED_FIRST; + lock.l_len = SHARED_SIZE; + lock.l_type = F_WRLCK; +- rc = osFcntl(pFile->h, F_SETLK, &lock); ++ rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile); + if( rc<0 ) return rc; + pInode->bProcessLock = 1; + pInode->nLock++; +@@ -26913,7 +34114,7 @@ static int unixFileLock(unixFile *pFile, struct flock *pLock){ + rc = 0; + } + }else{ +- rc = osFcntl(pFile->h, F_SETLK, pLock); ++ rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile); + } + return rc; + } +@@ -26947,7 +34148,7 @@ static int unixLock(sqlite3_file *id, int eFileLock){ + ** lock transitions in terms of the POSIX advisory shared and exclusive + ** lock primitives (called read-locks and write-locks below, to avoid + ** confusion with SQLite lock names). The algorithms are complicated +- ** slightly in order to be compatible with windows systems simultaneously ++ ** slightly in order to be compatible with Windows95 systems simultaneously + ** accessing the same database file, in case that is ever required. + ** + ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved +@@ -26955,8 +34156,14 @@ static int unixLock(sqlite3_file *id, int eFileLock){ + ** range', a range of 510 bytes at a well known offset. + ** + ** To obtain a SHARED lock, a read-lock is obtained on the 'pending +- ** byte'. If this is successful, a random byte from the 'shared byte +- ** range' is read-locked and the lock on the 'pending byte' released. ++ ** byte'. If this is successful, 'shared byte range' is read-locked ++ ** and the lock on the 'pending byte' released. (Legacy note: When ++ ** SQLite was first developed, Windows95 systems were still very common, ++ ** and Widnows95 lacks a shared-lock capability. So on Windows95, a ++ ** single randomly selected by from the 'shared byte range' is locked. ++ ** Windows95 is now pretty much extinct, but this work-around for the ++ ** lack of shared-locks on Windows95 lives on, for backwards ++ ** compatibility.) + ** + ** A process may only obtain a RESERVED lock after it has a SHARED lock. + ** A RESERVED lock is implemented by grabbing a write-lock on the +@@ -26975,11 +34182,6 @@ static int unixLock(sqlite3_file *id, int eFileLock){ + ** range'. Since all other locks require a read-lock on one of the bytes + ** within this range, this ensures that no other locks are held on the + ** database. +- ** +- ** The reason a single byte cannot be used instead of the 'shared byte +- ** range' is that some versions of windows do not support read-locks. By +- ** locking a random byte from a range, concurrent SHARED locks may exist +- ** even if the locking primitive used is always a write-lock. + */ + int rc = SQLITE_OK; + unixFile *pFile = (unixFile*)id; +@@ -27014,8 +34216,8 @@ static int unixLock(sqlite3_file *id, int eFileLock){ + + /* This mutex is needed because pFile->pInode is shared across threads + */ +- unixEnterMutex(); + pInode = pFile->pInode; ++ sqlite3_mutex_enter(pInode->pLockMutex); + + /* If some thread using this PID has a lock via a different unixFile* + ** handle that precludes the requested lock, return BUSY. +@@ -27158,7 +34360,7 @@ static int unixLock(sqlite3_file *id, int eFileLock){ + } + + end_lock: +- unixLeaveMutex(); ++ sqlite3_mutex_leave(pInode->pLockMutex); + OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), + rc==SQLITE_OK ? "ok" : "failed")); + return rc; +@@ -27170,11 +34372,12 @@ end_lock: + */ + static void setPendingFd(unixFile *pFile){ + unixInodeInfo *pInode = pFile->pInode; +- UnixUnusedFd *p = pFile->pUnused; ++ UnixUnusedFd *p = pFile->pPreallocatedUnused; ++ assert( unixFileMutexHeld(pFile) ); + p->pNext = pInode->pUnused; + pInode->pUnused = p; + pFile->h = -1; +- pFile->pUnused = 0; ++ pFile->pPreallocatedUnused = 0; + } + + /* +@@ -27205,8 +34408,8 @@ static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ + if( pFile->eFileLock<=eFileLock ){ + return SQLITE_OK; + } +- unixEnterMutex(); + pInode = pFile->pInode; ++ sqlite3_mutex_enter(pInode->pLockMutex); + assert( pInode->nShared!=0 ); + if( pFile->eFileLock>SHARED_LOCK ){ + assert( pInode->eFileLock==pFile->eFileLock ); +@@ -27249,9 +34452,7 @@ static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ + if( unixFileLock(pFile, &lock)==(-1) ){ + tErrno = errno; + rc = SQLITE_IOERR_UNLOCK; +- if( IS_LOCK_ERROR(rc) ){ +- storeLastErrno(pFile, tErrno); +- } ++ storeLastErrno(pFile, tErrno); + goto end_unlock; + } + lock.l_type = F_RDLCK; +@@ -27273,9 +34474,7 @@ static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ + if( unixFileLock(pFile, &lock)==(-1) ){ + tErrno = errno; + rc = SQLITE_IOERR_UNLOCK; +- if( IS_LOCK_ERROR(rc) ){ +- storeLastErrno(pFile, tErrno); +- } ++ storeLastErrno(pFile, tErrno); + goto end_unlock; + } + }else +@@ -27336,14 +34535,14 @@ static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ + */ + pInode->nLock--; + assert( pInode->nLock>=0 ); +- if( pInode->nLock==0 ){ +- closePendingFds(pFile); +- } ++ if( pInode->nLock==0 ) closePendingFds(pFile); + } + + end_unlock: +- unixLeaveMutex(); +- if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; ++ sqlite3_mutex_leave(pInode->pLockMutex); ++ if( rc==SQLITE_OK ){ ++ pFile->eFileLock = eFileLock; ++ } + return rc; + } + +@@ -27403,7 +34602,7 @@ static int closeUnixFile(sqlite3_file *id){ + #endif + OSTRACE(("CLOSE %-3d\n", pFile->h)); + OpenCounter(-1); +- sqlite3_free(pFile->pUnused); ++ sqlite3_free(pFile->pPreallocatedUnused); + memset(pFile, 0, sizeof(unixFile)); + return SQLITE_OK; + } +@@ -27414,15 +34613,20 @@ static int closeUnixFile(sqlite3_file *id){ + static int unixClose(sqlite3_file *id){ + int rc = SQLITE_OK; + unixFile *pFile = (unixFile *)id; ++ unixInodeInfo *pInode = pFile->pInode; ++ ++ assert( pInode!=0 ); + verifyDbFile(pFile); + unixUnlock(id, NO_LOCK); ++ assert( unixFileMutexNotheld(pFile) ); + unixEnterMutex(); + + /* unixFile.pInode is always valid here. Otherwise, a different close + ** routine (e.g. nolockClose()) would be called instead. + */ + assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); +- if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){ ++ sqlite3_mutex_enter(pInode->pLockMutex); ++ if( pInode->nLock ){ + /* If there are outstanding locks, do not actually close the file just + ** yet because that would clear those locks. Instead, add the file + ** descriptor to pInode->pUnused list. It will be automatically closed +@@ -27430,6 +34634,7 @@ static int unixClose(sqlite3_file *id){ + */ + setPendingFd(pFile); + } ++ sqlite3_mutex_leave(pInode->pLockMutex); + releaseInodeInfo(pFile); + rc = closeUnixFile(id); + unixLeaveMutex(); +@@ -27526,17 +34731,7 @@ static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { + SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); + + assert( pFile ); +- +- /* Check if a thread in this process holds such a lock */ +- if( pFile->eFileLock>SHARED_LOCK ){ +- /* Either this connection or some other connection in the same process +- ** holds a lock on the file. No need to check further. */ +- reserved = 1; +- }else{ +- /* The lock is held if and only if the lockfile exists */ +- const char *zLockFile = (const char*)pFile->lockingContext; +- reserved = osAccess(zLockFile, 0)==0; +- } ++ reserved = osAccess((const char*)pFile->lockingContext, 0)==0; + OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); + *pResOut = reserved; + return rc; +@@ -27598,7 +34793,7 @@ static int dotlockLock(sqlite3_file *id, int eFileLock) { + rc = SQLITE_BUSY; + } else { + rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); +- if( IS_LOCK_ERROR(rc) ){ ++ if( rc!=SQLITE_BUSY ){ + storeLastErrno(pFile, tErrno); + } + } +@@ -27645,14 +34840,12 @@ static int dotlockUnlock(sqlite3_file *id, int eFileLock) { + /* To fully unlock the database, delete the lock file */ + assert( eFileLock==NO_LOCK ); + rc = osRmdir(zLockFile); +- if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile); + if( rc<0 ){ + int tErrno = errno; +- rc = 0; +- if( ENOENT != tErrno ){ ++ if( tErrno==ENOENT ){ ++ rc = SQLITE_OK; ++ }else{ + rc = SQLITE_IOERR_UNLOCK; +- } +- if( IS_LOCK_ERROR(rc) ){ + storeLastErrno(pFile, tErrno); + } + return rc; +@@ -27665,14 +34858,11 @@ static int dotlockUnlock(sqlite3_file *id, int eFileLock) { + ** Close a file. Make sure the lock has been released before closing. + */ + static int dotlockClose(sqlite3_file *id) { +- int rc = SQLITE_OK; +- if( id ){ +- unixFile *pFile = (unixFile*)id; +- dotlockUnlock(id, NO_LOCK); +- sqlite3_free(pFile->lockingContext); +- rc = closeUnixFile(id); +- } +- return rc; ++ unixFile *pFile = (unixFile*)id; ++ assert( id!=0 ); ++ dotlockUnlock(id, NO_LOCK); ++ sqlite3_free(pFile->lockingContext); ++ return closeUnixFile(id); + } + /****************** End of the dot-file lock implementation ******************* + ******************************************************************************/ +@@ -27738,10 +34928,8 @@ static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ + int tErrno = errno; + /* unlock failed with an error */ + lrc = SQLITE_IOERR_UNLOCK; +- if( IS_LOCK_ERROR(lrc) ){ +- storeLastErrno(pFile, tErrno); +- rc = lrc; +- } ++ storeLastErrno(pFile, tErrno); ++ rc = lrc; + } + } else { + int tErrno = errno; +@@ -27757,7 +34945,7 @@ static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ + OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); + + #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS +- if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ ++ if( (rc & 0xff) == SQLITE_IOERR ){ + rc = SQLITE_OK; + reserved=1; + } +@@ -27824,7 +35012,7 @@ static int flockLock(sqlite3_file *id, int eFileLock) { + OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), + rc==SQLITE_OK ? "ok" : "failed")); + #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS +- if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ ++ if( (rc & 0xff) == SQLITE_IOERR ){ + rc = SQLITE_BUSY; + } + #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ +@@ -27874,12 +35062,9 @@ static int flockUnlock(sqlite3_file *id, int eFileLock) { + ** Close a file. + */ + static int flockClose(sqlite3_file *id) { +- int rc = SQLITE_OK; +- if( id ){ +- flockUnlock(id, NO_LOCK); +- rc = closeUnixFile(id); +- } +- return rc; ++ assert( id!=0 ); ++ flockUnlock(id, NO_LOCK); ++ return closeUnixFile(id); + } + + #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ +@@ -28047,6 +35232,7 @@ static int semXClose(sqlite3_file *id) { + unixFile *pFile = (unixFile*)id; + semXUnlock(id, NO_LOCK); + assert( pFile ); ++ assert( unixFileMutexNotheld(pFile) ); + unixEnterMutex(); + releaseInodeInfo(pFile); + unixLeaveMutex(); +@@ -28161,8 +35347,7 @@ static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ + *pResOut = 1; + return SQLITE_OK; + } +- unixEnterMutex(); /* Because pFile->pInode is shared across threads */ +- ++ sqlite3_mutex_enter(pFile->pInode->pLockMutex); + /* Check if a thread in this process holds such a lock */ + if( pFile->pInode->eFileLock>SHARED_LOCK ){ + reserved = 1; +@@ -28186,7 +35371,7 @@ static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ + } + } + +- unixLeaveMutex(); ++ sqlite3_mutex_leave(pFile->pInode->pLockMutex); + OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); + + *pResOut = reserved; +@@ -28249,8 +35434,8 @@ static int afpLock(sqlite3_file *id, int eFileLock){ + + /* This mutex is needed because pFile->pInode is shared across threads + */ +- unixEnterMutex(); + pInode = pFile->pInode; ++ sqlite3_mutex_enter(pInode->pLockMutex); + + /* If some thread using this PID has a lock via a different unixFile* + ** handle that precludes the requested lock, return BUSY. +@@ -28364,7 +35549,7 @@ static int afpLock(sqlite3_file *id, int eFileLock){ + /* Can't reestablish the shared lock. Sqlite can't deal, this is + ** a critical I/O error + */ +- rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : ++ rc = ((failed & 0xff) == SQLITE_IOERR) ? failed2 : + SQLITE_IOERR_LOCK; + goto afp_end_lock; + } +@@ -28386,7 +35571,7 @@ static int afpLock(sqlite3_file *id, int eFileLock){ + } + + afp_end_lock: +- unixLeaveMutex(); ++ sqlite3_mutex_leave(pInode->pLockMutex); + OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), + rc==SQLITE_OK ? "ok" : "failed")); + return rc; +@@ -28418,8 +35603,8 @@ static int afpUnlock(sqlite3_file *id, int eFileLock) { + if( pFile->eFileLock<=eFileLock ){ + return SQLITE_OK; + } +- unixEnterMutex(); + pInode = pFile->pInode; ++ sqlite3_mutex_enter(pInode->pLockMutex); + assert( pInode->nShared!=0 ); + if( pFile->eFileLock>SHARED_LOCK ){ + assert( pInode->eFileLock==pFile->eFileLock ); +@@ -28488,14 +35673,14 @@ static int afpUnlock(sqlite3_file *id, int eFileLock) { + if( rc==SQLITE_OK ){ + pInode->nLock--; + assert( pInode->nLock>=0 ); +- if( pInode->nLock==0 ){ +- closePendingFds(pFile); +- } ++ if( pInode->nLock==0 ) closePendingFds(pFile); + } + } + +- unixLeaveMutex(); +- if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; ++ sqlite3_mutex_leave(pInode->pLockMutex); ++ if( rc==SQLITE_OK ){ ++ pFile->eFileLock = eFileLock; ++ } + return rc; + } + +@@ -28504,11 +35689,15 @@ static int afpUnlock(sqlite3_file *id, int eFileLock) { + */ + static int afpClose(sqlite3_file *id) { + int rc = SQLITE_OK; +- if( id ){ +- unixFile *pFile = (unixFile*)id; +- afpUnlock(id, NO_LOCK); +- unixEnterMutex(); +- if( pFile->pInode && pFile->pInode->nLock ){ ++ unixFile *pFile = (unixFile*)id; ++ assert( id!=0 ); ++ afpUnlock(id, NO_LOCK); ++ assert( unixFileMutexNotheld(pFile) ); ++ unixEnterMutex(); ++ if( pFile->pInode ){ ++ unixInodeInfo *pInode = pFile->pInode; ++ sqlite3_mutex_enter(pInode->pLockMutex); ++ if( pInode->nLock ){ + /* If there are outstanding locks, do not actually close the file just + ** yet because that would clear those locks. Instead, add the file + ** descriptor to pInode->aPending. It will be automatically closed when +@@ -28516,11 +35705,12 @@ static int afpClose(sqlite3_file *id) { + */ + setPendingFd(pFile); + } +- releaseInodeInfo(pFile); +- sqlite3_free(pFile->lockingContext); +- rc = closeUnixFile(id); +- unixLeaveMutex(); ++ sqlite3_mutex_leave(pInode->pLockMutex); + } ++ releaseInodeInfo(pFile); ++ sqlite3_free(pFile->lockingContext); ++ rc = closeUnixFile(id); ++ unixLeaveMutex(); + return rc; + } + +@@ -28590,7 +35780,6 @@ static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ + TIMER_START; + assert( cnt==(cnt&0x1ffff) ); + assert( id->h>2 ); +- cnt &= 0x1ffff; + do{ + #if defined(USE_PREAD) + got = osPread(id->h, pBuf, cnt, offset); +@@ -28600,13 +35789,9 @@ static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ + SimulateIOError( got = -1 ); + #else + newOffset = lseek(id->h, offset, SEEK_SET); +- SimulateIOError( newOffset-- ); +- if( newOffset!=offset ){ +- if( newOffset == -1 ){ +- storeLastErrno((unixFile*)id, errno); +- }else{ +- storeLastErrno((unixFile*)id, 0); +- } ++ SimulateIOError( newOffset = -1 ); ++ if( newOffset<0 ){ ++ storeLastErrno((unixFile*)id, errno); + return -1; + } + got = osRead(id->h, pBuf, cnt); +@@ -28650,7 +35835,7 @@ static int unixRead( + /* If this is a database file (not a journal, master-journal or temp + ** file), the bytes in the locking range should never be read or written. */ + #if 0 +- assert( pFile->pUnused==0 ++ assert( pFile->pPreallocatedUnused==0 + || offset>=PENDING_BYTE+512 + || offset+amt<=PENDING_BYTE + ); +@@ -28705,6 +35890,7 @@ static int seekAndWriteFd( + + assert( nBuf==(nBuf&0x1ffff) ); + assert( fd>2 ); ++ assert( piErrno!=0 ); + nBuf &= 0x1ffff; + TIMER_START; + +@@ -28715,11 +35901,10 @@ static int seekAndWriteFd( + #else + do{ + i64 iSeek = lseek(fd, iOff, SEEK_SET); +- SimulateIOError( iSeek-- ); +- +- if( iSeek!=iOff ){ +- if( piErrno ) *piErrno = (iSeek==-1 ? errno : 0); +- return -1; ++ SimulateIOError( iSeek = -1 ); ++ if( iSeek<0 ){ ++ rc = -1; ++ break; + } + rc = osWrite(fd, pBuf, nBuf); + }while( rc<0 && errno==EINTR ); +@@ -28728,7 +35913,7 @@ static int seekAndWriteFd( + TIMER_END; + OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED)); + +- if( rc<0 && piErrno ) *piErrno = errno; ++ if( rc<0 ) *piErrno = errno; + return rc; + } + +@@ -28763,7 +35948,7 @@ static int unixWrite( + /* If this is a database file (not a journal, master-journal or temp + ** file), the bytes in the locking range should never be read or written. */ + #if 0 +- assert( pFile->pUnused==0 ++ assert( pFile->pPreallocatedUnused==0 + || offset>=PENDING_BYTE+512 + || offset+amt<=PENDING_BYTE + ); +@@ -28791,7 +35976,7 @@ static int unixWrite( + } + #endif + +-#if SQLITE_MAX_MMAP_SIZE>0 ++#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 + /* Deal with as much of this write request as possible by transfering + ** data from the memory mapping using memcpy(). */ + if( offset mmapSize ){ +@@ -28807,8 +35992,8 @@ static int unixWrite( + } + } + #endif +- +- while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ ++ ++ while( (wrote = seekAndWrite(pFile, offset, pBuf, amt)) 0 ){ + amt -= wrote; + offset += wrote; + pBuf = &((char*)pBuf)[wrote]; +@@ -28816,7 +36001,7 @@ static int unixWrite( + SimulateIOError(( wrote=(-1), amt=1 )); + SimulateDiskfullError(( wrote=0, amt=1 )); + +- if( amt>0 ){ ++ if( amt>wrote ){ + if( wrote<0 && pFile->lastErrno!=ENOSPC ){ + /* lastErrno set by seekAndWrite */ + return SQLITE_IOERR_WRITE; +@@ -28912,10 +36097,15 @@ static int full_fsync(int fd, int fullSync, int dataOnly){ + #endif + + /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a +- ** no-op ++ ** no-op. But go ahead and call fstat() to validate the file ++ ** descriptor as we need a method to provoke a failure during ++ ** coverate testing. + */ + #ifdef SQLITE_NO_SYNC +- rc = SQLITE_OK; ++ { ++ struct stat buf; ++ rc = osFstat(fd, &buf); ++ } + #elif HAVE_FULLFSYNC + if( fullSync ){ + rc = osFcntl(fd, F_FULLFSYNC, 0); +@@ -28981,16 +36171,20 @@ static int openDirectory(const char *zFilename, int *pFd){ + char zDirname[MAX_PATHNAME+1]; + + sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); +- for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); ++ for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--); + if( ii>0 ){ + zDirname[ii] = '\0'; +- fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); +- if( fd>=0 ){ +- OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); +- } ++ }else{ ++ if( zDirname[0]!='/' ) zDirname[0] = '.'; ++ zDirname[1] = 0; ++ } ++ fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); ++ if( fd>=0 ){ ++ OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); + } + *pFd = fd; +- return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname)); ++ if( fd>=0 ) return SQLITE_OK; ++ return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname); + } + + /* +@@ -29043,10 +36237,11 @@ static int unixSync(sqlite3_file *id, int flags){ + OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, + HAVE_FULLFSYNC, isFullsync)); + rc = osOpenDirectory(pFile->zPath, &dirfd); +- if( rc==SQLITE_OK && dirfd>=0 ){ ++ if( rc==SQLITE_OK ){ + full_fsync(dirfd, 0, 0); + robust_close(pFile, dirfd, __LINE__); +- }else if( rc==SQLITE_CANTOPEN ){ ++ }else{ ++ assert( rc==SQLITE_CANTOPEN ); + rc = SQLITE_OK; + } + pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; +@@ -29165,7 +36360,7 @@ static int fcntlSizeHint(unixFile *pFile, i64 nByte){ + do{ + err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); + }while( err==EINTR ); +- if( err ) return SQLITE_IOERR_WRITE; ++ if( err && err!=EINVAL ) return SQLITE_IOERR_WRITE; + #else + /* If the OS does not have posix_fallocate(), fake it. Write a + ** single byte to the last byte in each block that falls entirely +@@ -29178,18 +36373,14 @@ static int fcntlSizeHint(unixFile *pFile, i64 nByte){ + int nWrite = 0; /* Number of bytes written by seekAndWrite */ + i64 iWrite; /* Next offset to write to */ + +- iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1; ++ iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1; + assert( iWrite>=buf.st_size ); +- assert( (iWrite/nBlk)==((buf.st_size+nBlk-1)/nBlk) ); + assert( ((iWrite+1)%nBlk)==0 ); +- for(/*no-op*/; iWrite =nSize ) iWrite = nSize - 1; + nWrite = seekAndWrite(pFile, iWrite, "", 1); + if( nWrite!=1 ) return SQLITE_IOERR_WRITE; + } +- if( nWrite==0 || (nSize%nBlk) ){ +- nWrite = seekAndWrite(pFile, nSize-1, "", 1); +- if( nWrite!=1 ) return SQLITE_IOERR_WRITE; +- } + #endif + } + } +@@ -29237,10 +36428,21 @@ static int unixGetTempname(int nBuf, char *zBuf); + static int unixFileControl(sqlite3_file *id, int op, void *pArg){ + unixFile *pFile = (unixFile*)id; + switch( op ){ +- case SQLITE_FCNTL_WAL_BLOCK: { +- /* pFile->ctrlFlags |= UNIXFILE_BLOCK; // Deferred feature */ +- return SQLITE_OK; ++#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) ++ case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: { ++ int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE); ++ return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK; ++ } ++ case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: { ++ int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE); ++ return rc ? SQLITE_IOERR_COMMIT_ATOMIC : SQLITE_OK; ++ } ++ case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: { ++ int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE); ++ return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK; + } ++#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ ++ + case SQLITE_FCNTL_LOCKSTATE: { + *(int*)pArg = pFile->eFileLock; + return SQLITE_OK; +@@ -29284,6 +36486,12 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){ + *(int*)pArg = fileHasMoved(pFile); + return SQLITE_OK; + } ++#ifdef SQLITE_ENABLE_SETLK_TIMEOUT ++ case SQLITE_FCNTL_LOCK_TIMEOUT: { ++ pFile->iBusyTimeout = *(int*)pArg; ++ return SQLITE_OK; ++ } ++#endif + #if SQLITE_MAX_MMAP_SIZE>0 + case SQLITE_FCNTL_MMAP_SIZE: { + i64 newLimit = *(i64*)pArg; +@@ -29291,6 +36499,14 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){ + if( newLimit>sqlite3GlobalConfig.mxMmap ){ + newLimit = sqlite3GlobalConfig.mxMmap; + } ++ ++ /* The value of newLimit may be eventually cast to (size_t) and passed ++ ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a ++ ** 64-bit type. */ ++ if( newLimit>0 && sizeof(size_t)<8 ){ ++ newLimit = (newLimit & 0x7FFFFFFF); ++ } ++ + *(i64*)pArg = pFile->mmapSizeMax; + if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ + pFile->mmapSizeMax = newLimit; +@@ -29324,30 +36540,41 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){ + } + + /* +-** Return the sector size in bytes of the underlying block device for +-** the specified file. This is almost always 512 bytes, but may be +-** larger for some devices. ++** If pFd->sectorSize is non-zero when this function is called, it is a ++** no-op. Otherwise, the values of pFd->sectorSize and ++** pFd->deviceCharacteristics are set according to the file-system ++** characteristics. + ** +-** SQLite code assumes this function cannot fail. It also assumes that +-** if two files are created in the same file-system directory (i.e. +-** a database and its journal file) that the sector size will be the +-** same for both. ++** There are two versions of this function. One for QNX and one for all ++** other systems. + */ +-#ifndef __QNXNTO__ +-static int unixSectorSize(sqlite3_file *NotUsed){ +- UNUSED_PARAMETER(NotUsed); +- return SQLITE_DEFAULT_SECTOR_SIZE; +-} +-#endif ++#ifndef __QNXNTO__ ++static void setDeviceCharacteristics(unixFile *pFd){ ++ assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 ); ++ if( pFd->sectorSize==0 ){ ++#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) ++ int res; ++ u32 f = 0; + +-/* +-** The following version of unixSectorSize() is optimized for QNX. +-*/ +-#ifdef __QNXNTO__ ++ /* Check for support for F2FS atomic batch writes. */ ++ res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f); ++ if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE) ){ ++ pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC; ++ } ++#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ ++ ++ /* Set the POWERSAFE_OVERWRITE flag if requested. */ ++ if( pFd->ctrlFlags & UNIXFILE_PSOW ){ ++ pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; ++ } ++ ++ pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; ++ } ++} ++#else + #include + #include +-static int unixSectorSize(sqlite3_file *id){ +- unixFile *pFile = (unixFile*)id; ++static void setDeviceCharacteristics(unixFile *pFile){ + if( pFile->sectorSize == 0 ){ + struct statvfs fsInfo; + +@@ -29355,7 +36582,7 @@ static int unixSectorSize(sqlite3_file *id){ + pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; + pFile->deviceCharacteristics = 0; + if( fstatvfs(pFile->h, &fsInfo) == -1 ) { +- return pFile->sectorSize; ++ return; + } + + if( !strcmp(fsInfo.f_basetype, "tmp") ) { +@@ -29416,9 +36643,24 @@ static int unixSectorSize(sqlite3_file *id){ + pFile->deviceCharacteristics = 0; + pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; + } +- return pFile->sectorSize; + } +-#endif /* __QNXNTO__ */ ++#endif ++ ++/* ++** Return the sector size in bytes of the underlying block device for ++** the specified file. This is almost always 512 bytes, but may be ++** larger for some devices. ++** ++** SQLite code assumes this function cannot fail. It also assumes that ++** if two files are created in the same file-system directory (i.e. ++** a database and its journal file) that the sector size will be the ++** same for both. ++*/ ++static int unixSectorSize(sqlite3_file *id){ ++ unixFile *pFd = (unixFile*)id; ++ setDeviceCharacteristics(pFd); ++ return pFd->sectorSize; ++} + + /* + ** Return the device characteristics for the file. +@@ -29434,16 +36676,9 @@ static int unixSectorSize(sqlite3_file *id){ + ** available to turn it off and URI query parameter available to turn it off. + */ + static int unixDeviceCharacteristics(sqlite3_file *id){ +- unixFile *p = (unixFile*)id; +- int rc = 0; +-#ifdef __QNXNTO__ +- if( p->sectorSize==0 ) unixSectorSize(id); +- rc = p->deviceCharacteristics; +-#endif +- if( p->ctrlFlags & UNIXFILE_PSOW ){ +- rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; +- } +- return rc; ++ unixFile *pFd = (unixFile*)id; ++ setDeviceCharacteristics(pFd); ++ return pFd->deviceCharacteristics; + } + + #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 +@@ -29490,21 +36725,22 @@ static int unixGetpagesize(void){ + ** + ** The following fields are read-only after the object is created: + ** +-** fid ++** hShm + ** zFilename + ** +-** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and ++** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and + ** unixMutexHeld() is true when reading or writing any other field + ** in this structure. + */ + struct unixShmNode { + unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ +- sqlite3_mutex *mutex; /* Mutex to access this object */ ++ sqlite3_mutex *pShmMutex; /* Mutex to access this object */ + char *zFilename; /* Name of the mmapped file */ +- int h; /* Open file descriptor */ ++ int hShm; /* Open file descriptor */ + int szRegion; /* Size of shared-memory regions */ + u16 nRegion; /* Size of array apRegion */ + u8 isReadonly; /* True if read-only */ ++ u8 isUnlocked; /* True if no DMS lock held */ + char **apRegion; /* Array of mapped shared-memory regions */ + int nRef; /* Number of unixShm objects pointing to this */ + unixShm *pFirst; /* All unixShm objects pointing to this */ +@@ -29522,16 +36758,16 @@ struct unixShmNode { + ** The following fields are initialized when this object is created and + ** are read-only thereafter: + ** +-** unixShm.pFile ++** unixShm.pShmNode + ** unixShm.id + ** +-** All other fields are read/write. The unixShm.pFile->mutex must be held +-** while accessing any read/write fields. ++** All other fields are read/write. The unixShm.pShmNode->pShmMutex must ++** be held while accessing any read/write fields. + */ + struct unixShm { + unixShmNode *pShmNode; /* The underlying unixShmNode object */ + unixShm *pNext; /* Next unixShm with the same unixShmNode */ +- u8 hasMutex; /* True if holding the unixShmNode mutex */ ++ u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */ + u8 id; /* Id of this connection within its unixShmNode */ + u16 sharedMask; /* Mask of shared locks held */ + u16 exclMask; /* Mask of exclusive locks held */ +@@ -29561,27 +36797,23 @@ static int unixShmSystemLock( + + /* Access to the unixShmNode object is serialized by the caller */ + pShmNode = pFile->pInode->pShmNode; +- assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); ++ assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) ); ++ assert( pShmNode->nRef>0 || unixMutexHeld() ); + + /* Shared locks never span more than one byte */ + assert( n==1 || lockType!=F_RDLCK ); + + /* Locks are within range */ +- assert( n>=1 && n =1 && n<=SQLITE_SHM_NLOCK ); + +- if( pShmNode->h>=0 ){ +- int lkType; ++ if( pShmNode->hShm>=0 ){ + /* Initialize the locking parameters */ +- memset(&f, 0, sizeof(f)); + f.l_type = lockType; + f.l_whence = SEEK_SET; + f.l_start = ofst; + f.l_len = n; +- +- lkType = (pFile->ctrlFlags & UNIXFILE_BLOCK)!=0 ? F_SETLKW : F_SETLK; +- rc = osFcntl(pShmNode->h, lkType, &f); ++ rc = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile); + rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; +- pFile->ctrlFlags &= ~UNIXFILE_BLOCK; + } + + /* Update the global lock state and do debug tracing */ +@@ -29648,28 +36880,91 @@ static int unixShmRegionPerMap(void){ + static void unixShmPurge(unixFile *pFd){ + unixShmNode *p = pFd->pInode->pShmNode; + assert( unixMutexHeld() ); +- if( p && p->nRef==0 ){ ++ if( p && ALWAYS(p->nRef==0) ){ + int nShmPerMap = unixShmRegionPerMap(); + int i; + assert( p->pInode==pFd->pInode ); +- sqlite3_mutex_free(p->mutex); ++ sqlite3_mutex_free(p->pShmMutex); + for(i=0; i nRegion; i+=nShmPerMap){ +- if( p->h>=0 ){ ++ if( p->hShm>=0 ){ + osMunmap(p->apRegion[i], p->szRegion); + }else{ + sqlite3_free(p->apRegion[i]); + } + } + sqlite3_free(p->apRegion); +- if( p->h>=0 ){ +- robust_close(pFd, p->h, __LINE__); +- p->h = -1; ++ if( p->hShm>=0 ){ ++ robust_close(pFd, p->hShm, __LINE__); ++ p->hShm = -1; + } + p->pInode->pShmNode = 0; + sqlite3_free(p); + } + } + ++/* ++** The DMS lock has not yet been taken on shm file pShmNode. Attempt to ++** take it now. Return SQLITE_OK if successful, or an SQLite error ++** code otherwise. ++** ++** If the DMS cannot be locked because this is a readonly_shm=1 ++** connection and no other process already holds a lock, return ++** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1. ++*/ ++static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){ ++ struct flock lock; ++ int rc = SQLITE_OK; ++ ++ /* Use F_GETLK to determine the locks other processes are holding ++ ** on the DMS byte. If it indicates that another process is holding ++ ** a SHARED lock, then this process may also take a SHARED lock ++ ** and proceed with opening the *-shm file. ++ ** ++ ** Or, if no other process is holding any lock, then this process ++ ** is the first to open it. In this case take an EXCLUSIVE lock on the ++ ** DMS byte and truncate the *-shm file to zero bytes in size. Then ++ ** downgrade to a SHARED lock on the DMS byte. ++ ** ++ ** If another process is holding an EXCLUSIVE lock on the DMS byte, ++ ** return SQLITE_BUSY to the caller (it will try again). An earlier ++ ** version of this code attempted the SHARED lock at this point. But ++ ** this introduced a subtle race condition: if the process holding ++ ** EXCLUSIVE failed just before truncating the *-shm file, then this ++ ** process might open and use the *-shm file without truncating it. ++ ** And if the *-shm file has been corrupted by a power failure or ++ ** system crash, the database itself may also become corrupt. */ ++ lock.l_whence = SEEK_SET; ++ lock.l_start = UNIX_SHM_DMS; ++ lock.l_len = 1; ++ lock.l_type = F_WRLCK; ++ if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) { ++ rc = SQLITE_IOERR_LOCK; ++ }else if( lock.l_type==F_UNLCK ){ ++ if( pShmNode->isReadonly ){ ++ pShmNode->isUnlocked = 1; ++ rc = SQLITE_READONLY_CANTINIT; ++ }else{ ++ rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1); ++ /* The first connection to attach must truncate the -shm file. We ++ ** truncate to 3 bytes (an arbitrary small number, less than the ++ ** -shm header size) rather than 0 as a system debugging aid, to ++ ** help detect if a -shm file truncation is legitimate or is the work ++ ** or a rogue process. */ ++ if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){ ++ rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename); ++ } ++ } ++ }else if( lock.l_type==F_WRLCK ){ ++ rc = SQLITE_BUSY; ++ } ++ ++ if( rc==SQLITE_OK ){ ++ assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK ); ++ rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1); ++ } ++ return rc; ++} ++ + /* + ** Open a shared-memory area associated with open database file pDbFd. + ** This particular implementation uses mmapped files. +@@ -29708,20 +37003,21 @@ static void unixShmPurge(unixFile *pFd){ + static int unixOpenSharedMemory(unixFile *pDbFd){ + struct unixShm *p = 0; /* The connection to be opened */ + struct unixShmNode *pShmNode; /* The underlying mmapped file */ +- int rc; /* Result code */ ++ int rc = SQLITE_OK; /* Result code */ + unixInodeInfo *pInode; /* The inode of fd */ +- char *zShmFilename; /* Name of the file used for SHM */ ++ char *zShm; /* Name of the file used for SHM */ + int nShmFilename; /* Size of the SHM filename in bytes */ + + /* Allocate space for the new unixShm object. */ + p = sqlite3_malloc64( sizeof(*p) ); +- if( p==0 ) return SQLITE_NOMEM; ++ if( p==0 ) return SQLITE_NOMEM_BKPT; + memset(p, 0, sizeof(*p)); + assert( pDbFd->pShm==0 ); + + /* Check to see if a unixShmNode object already exists. Reuse an existing + ** one if present. Create a new one if necessary. + */ ++ assert( unixFileMutexNotheld(pDbFd) ); + unixEnterMutex(); + pInode = pDbFd->pInode; + pShmNode = pInode->pShmNode; +@@ -29735,7 +37031,7 @@ static int unixOpenSharedMemory(unixFile *pDbFd){ + ** a new *-shm file is created, an attempt will be made to create it + ** with the same permissions. + */ +- if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){ ++ if( osFstat(pDbFd->h, &sStat) ){ + rc = SQLITE_IOERR_FSTAT; + goto shm_open_err; + } +@@ -29747,59 +37043,51 @@ static int unixOpenSharedMemory(unixFile *pDbFd){ + #endif + pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename ); + if( pShmNode==0 ){ +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + goto shm_open_err; + } + memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename); +- zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; ++ zShm = pShmNode->zFilename = (char*)&pShmNode[1]; + #ifdef SQLITE_SHM_DIRECTORY +- sqlite3_snprintf(nShmFilename, zShmFilename, ++ sqlite3_snprintf(nShmFilename, zShm, + SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", + (u32)sStat.st_ino, (u32)sStat.st_dev); + #else +- sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", zBasePath); +- sqlite3FileSuffix3(pDbFd->zPath, zShmFilename); ++ sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath); ++ sqlite3FileSuffix3(pDbFd->zPath, zShm); + #endif +- pShmNode->h = -1; ++ pShmNode->hShm = -1; + pDbFd->pInode->pShmNode = pShmNode; + pShmNode->pInode = pDbFd->pInode; +- pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); +- if( pShmNode->mutex==0 ){ +- rc = SQLITE_NOMEM; +- goto shm_open_err; ++ if( sqlite3GlobalConfig.bCoreMutex ){ ++ pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); ++ if( pShmNode->pShmMutex==0 ){ ++ rc = SQLITE_NOMEM_BKPT; ++ goto shm_open_err; ++ } + } + + if( pInode->bProcessLock==0 ){ +- int openFlags = O_RDWR | O_CREAT; +- if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ +- openFlags = O_RDONLY; +- pShmNode->isReadonly = 1; ++ if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ ++ pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT,(sStat.st_mode&0777)); + } +- pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); +- if( pShmNode->h<0 ){ +- rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); +- goto shm_open_err; ++ if( pShmNode->hShm<0 ){ ++ pShmNode->hShm = robust_open(zShm, O_RDONLY, (sStat.st_mode&0777)); ++ if( pShmNode->hShm<0 ){ ++ rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm); ++ goto shm_open_err; ++ } ++ pShmNode->isReadonly = 1; + } + + /* If this process is running as root, make sure that the SHM file + ** is owned by the same user that owns the original database. Otherwise, + ** the original owner will not be able to connect. + */ +- osFchown(pShmNode->h, sStat.st_uid, sStat.st_gid); +- +- /* Check to see if another process is holding the dead-man switch. +- ** If not, truncate the file to zero length. +- */ +- rc = SQLITE_OK; +- if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ +- if( robust_ftruncate(pShmNode->h, 0) ){ +- rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); +- } +- } +- if( rc==SQLITE_OK ){ +- rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1); +- } +- if( rc ) goto shm_open_err; ++ robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid); ++ ++ rc = unixLockSharedMemory(pDbFd, pShmNode); ++ if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err; + } + } + +@@ -29816,14 +37104,14 @@ static int unixOpenSharedMemory(unixFile *pDbFd){ + ** the cover of the unixEnterMutex() mutex and the pointer from the + ** new (struct unixShm) object to the pShmNode has been set. All that is + ** left to do is to link the new object into the linked list starting +- ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex +- ** mutex. ++ ** at pShmNode->pFirst. This must be done while holding the ++ ** pShmNode->pShmMutex. + */ +- sqlite3_mutex_enter(pShmNode->mutex); ++ sqlite3_mutex_enter(pShmNode->pShmMutex); + p->pNext = pShmNode->pFirst; + pShmNode->pFirst = p; +- sqlite3_mutex_leave(pShmNode->mutex); +- return SQLITE_OK; ++ sqlite3_mutex_leave(pShmNode->pShmMutex); ++ return rc; + + /* Jump here on any error */ + shm_open_err: +@@ -29874,11 +37162,16 @@ static int unixShmMap( + + p = pDbFd->pShm; + pShmNode = p->pShmNode; +- sqlite3_mutex_enter(pShmNode->mutex); ++ sqlite3_mutex_enter(pShmNode->pShmMutex); ++ if( pShmNode->isUnlocked ){ ++ rc = unixLockSharedMemory(pDbFd, pShmNode); ++ if( rc!=SQLITE_OK ) goto shmpage_out; ++ pShmNode->isUnlocked = 0; ++ } + assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); + assert( pShmNode->pInode==pDbFd->pInode ); +- assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); +- assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); ++ assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); ++ assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); + + /* Minimum number of regions required to be mapped. */ + nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap; +@@ -29890,12 +37183,12 @@ static int unixShmMap( + + pShmNode->szRegion = szRegion; + +- if( pShmNode->h>=0 ){ ++ if( pShmNode->hShm>=0 ){ + /* The requested region is not mapped into this processes address space. + ** Check to see if it has been allocated (i.e. if the wal-index file is + ** large enough to contain the requested region). + */ +- if( osFstat(pShmNode->h, &sStat) ){ ++ if( osFstat(pShmNode->hShm, &sStat) ){ + rc = SQLITE_IOERR_SHMSIZE; + goto shmpage_out; + } +@@ -29922,7 +37215,8 @@ static int unixShmMap( + /* Write to the last byte of each newly allocated or extended page */ + assert( (nByte % pgsz)==0 ); + for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){ +- if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, 0)!=1 ){ ++ int x = 0; ++ if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){ + const char *zFile = pShmNode->zFilename; + rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile); + goto shmpage_out; +@@ -29937,7 +37231,7 @@ static int unixShmMap( + pShmNode->apRegion, nReqRegion*sizeof(char *) + ); + if( !apNew ){ +- rc = SQLITE_IOERR_NOMEM; ++ rc = SQLITE_IOERR_NOMEM_BKPT; + goto shmpage_out; + } + pShmNode->apRegion = apNew; +@@ -29945,22 +37239,22 @@ static int unixShmMap( + int nMap = szRegion*nShmPerMap; + int i; + void *pMem; +- if( pShmNode->h>=0 ){ ++ if( pShmNode->hShm>=0 ){ + pMem = osMmap(0, nMap, + pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, +- MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion ++ MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion + ); + if( pMem==MAP_FAILED ){ + rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); + goto shmpage_out; + } + }else{ +- pMem = sqlite3_malloc64(szRegion); ++ pMem = sqlite3_malloc64(nMap); + if( pMem==0 ){ +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + goto shmpage_out; + } +- memset(pMem, 0, szRegion); ++ memset(pMem, 0, nMap); + } + + for(i=0; i isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; +- sqlite3_mutex_leave(pShmNode->mutex); ++ sqlite3_mutex_leave(pShmNode->pShmMutex); + return rc; + } + +@@ -30011,12 +37305,12 @@ static int unixShmLock( + || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) + || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); + assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); +- assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); +- assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); ++ assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); ++ assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); + + mask = (1<<(ofst+n)) - (1< 1 || mask==(1< mutex); ++ sqlite3_mutex_enter(pShmNode->pShmMutex); + if( flags & SQLITE_SHM_UNLOCK ){ + u16 allMask = 0; /* Mask of locks held by siblings */ + +@@ -30089,7 +37383,7 @@ static int unixShmLock( + } + } + } +- sqlite3_mutex_leave(pShmNode->mutex); ++ sqlite3_mutex_leave(pShmNode->pShmMutex); + OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", + p->id, osGetpid(0), p->sharedMask, p->exclMask)); + return rc; +@@ -30105,7 +37399,11 @@ static void unixShmBarrier( + sqlite3_file *fd /* Database file holding the shared memory */ + ){ + UNUSED_PARAMETER(fd); +- unixEnterMutex(); ++ sqlite3MemoryBarrier(); /* compiler-defined memory barrier */ ++ assert( fd->pMethods->xLock==nolockLock ++ || unixFileMutexNotheld((unixFile*)fd) ++ ); ++ unixEnterMutex(); /* Also mutex, for redundancy */ + unixLeaveMutex(); + } + +@@ -30135,22 +37433,23 @@ static int unixShmUnmap( + + /* Remove connection p from the set of connections associated + ** with pShmNode */ +- sqlite3_mutex_enter(pShmNode->mutex); ++ sqlite3_mutex_enter(pShmNode->pShmMutex); + for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} + *pp = p->pNext; + + /* Free the connection p */ + sqlite3_free(p); + pDbFd->pShm = 0; +- sqlite3_mutex_leave(pShmNode->mutex); ++ sqlite3_mutex_leave(pShmNode->pShmMutex); + + /* If pShmNode->nRef has reached 0, then close the underlying + ** shared-memory file, too */ ++ assert( unixFileMutexNotheld(pDbFd) ); + unixEnterMutex(); + assert( pShmNode->nRef>0 ); + pShmNode->nRef--; + if( pShmNode->nRef==0 ){ +- if( deleteFlag && pShmNode->h>=0 ){ ++ if( deleteFlag && pShmNode->hShm>=0 ){ + osUnlink(pShmNode->zFilename); + } + unixShmPurge(pDbFd); +@@ -30215,7 +37514,9 @@ static void unixRemapfile( + assert( pFd->mmapSizeActual>=pFd->mmapSize ); + assert( MAP_FAILED!=0 ); + ++#ifdef SQLITE_MMAP_READWRITE + if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE; ++#endif + + if( pOrig ){ + #if HAVE_MREMAP +@@ -30287,17 +37588,14 @@ static void unixRemapfile( + ** recreated as a result of outstanding references) or an SQLite error + ** code otherwise. + */ +-static int unixMapfile(unixFile *pFd, i64 nByte){ +- i64 nMap = nByte; +- int rc; +- ++static int unixMapfile(unixFile *pFd, i64 nMap){ + assert( nMap>=0 || pFd->nFetchOut==0 ); ++ assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); + if( pFd->nFetchOut>0 ) return SQLITE_OK; + + if( nMap<0 ){ + struct stat statbuf; /* Low-level file information */ +- rc = osFstat(pFd->h, &statbuf); +- if( rc!=SQLITE_OK ){ ++ if( osFstat(pFd->h, &statbuf) ){ + return SQLITE_IOERR_FSTAT; + } + nMap = statbuf.st_size; +@@ -30306,12 +37604,9 @@ static int unixMapfile(unixFile *pFd, i64 nByte){ + nMap = pFd->mmapSizeMax; + } + ++ assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); + if( nMap!=pFd->mmapSize ){ +- if( nMap>0 ){ +- unixRemapfile(pFd, nMap); +- }else{ +- unixUnmapfile(pFd); +- } ++ unixRemapfile(pFd, nMap); + } + + return SQLITE_OK; +@@ -30476,7 +37771,7 @@ IOMETHODS( + IOMETHODS( + nolockIoFinder, /* Finder function name */ + nolockIoMethods, /* sqlite3_io_methods object name */ +- 3, /* shared memory is disabled */ ++ 3, /* shared memory and mmap are enabled */ + nolockClose, /* xClose method */ + nolockLock, /* xLock method */ + nolockUnlock, /* xUnlock method */ +@@ -30704,17 +37999,6 @@ static int fillInUnixFile( + + assert( pNew->pInode==NULL ); + +- /* Usually the path zFilename should not be a relative pathname. The +- ** exception is when opening the proxy "conch" file in builds that +- ** include the special Apple locking styles. +- */ +-#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE +- assert( zFilename==0 || zFilename[0]=='/' +- || pVfs->pAppData==(void*)&autolockIoFinder ); +-#else +- assert( zFilename==0 || zFilename[0]=='/' ); +-#endif +- + /* No locking occurs in temporary files */ + assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 ); + +@@ -30738,7 +38022,7 @@ static int fillInUnixFile( + pNew->pId = vxworksFindFileId(zFilename); + if( pNew->pId==0 ){ + ctrlFlags |= UNIXFILE_NOLOCK; +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + } + #endif + +@@ -30794,7 +38078,7 @@ static int fillInUnixFile( + afpLockingContext *pCtx; + pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) ); + if( pCtx==0 ){ +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + }else{ + /* NB: zFilename exists and remains valid until the file is closed + ** according to requirement F11141. So we do not need to make a +@@ -30824,7 +38108,7 @@ static int fillInUnixFile( + nFilename = (int)strlen(zFilename) + 6; + zLockFile = (char *)sqlite3_malloc64(nFilename); + if( zLockFile==0 ){ +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + }else{ + sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); + } +@@ -30847,7 +38131,7 @@ static int fillInUnixFile( + if( zSemName[n]=='/' ) zSemName[n] = '_'; + pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); + if( pNew->pInode->pSem == SEM_FAILED ){ +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + pNew->pInode->aSemName[0] = '\0'; + } + } +@@ -30880,29 +38164,31 @@ static int fillInUnixFile( + */ + static const char *unixTempFileDir(void){ + static const char *azDirs[] = { +- 0, + 0, + 0, + "/var/tmp", + "/usr/tmp", + "/tmp", +- 0 /* List terminator */ ++ "." + }; +- unsigned int i; ++ unsigned int i = 0; + struct stat buf; +- const char *zDir = 0; +- +- azDirs[0] = sqlite3_temp_directory; +- if( !azDirs[1] ) azDirs[1] = getenv("SQLITE_TMPDIR"); +- if( !azDirs[2] ) azDirs[2] = getenv("TMPDIR"); +- for(i=0; i =sizeof(azDirs)/sizeof(azDirs[0]) ) break; ++ zDir = azDirs[i++]; + } +- return zDir; ++ return 0; + } + + /* +@@ -30911,38 +38197,26 @@ static const char *unixTempFileDir(void){ + ** pVfs->mxPathname bytes. + */ + static int unixGetTempname(int nBuf, char *zBuf){ +- static const unsigned char zChars[] = +- "abcdefghijklmnopqrstuvwxyz" +- "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +- "0123456789"; +- unsigned int i, j; + const char *zDir; ++ int iLimit = 0; + + /* It's odd to simulate an io-error here, but really this is just + ** using the io-error infrastructure to test that SQLite handles this + ** function failing. + */ ++ zBuf[0] = 0; + SimulateIOError( return SQLITE_IOERR ); + + zDir = unixTempFileDir(); +- if( zDir==0 ) zDir = "."; +- +- /* Check that the output buffer is large enough for the temporary file +- ** name. If it is not, return SQLITE_ERROR. +- */ +- if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){ +- return SQLITE_ERROR; +- } +- ++ if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH; + do{ +- sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); +- j = (int)strlen(zBuf); +- sqlite3_randomness(15, &zBuf[j]); +- for(i=0; i<15; i++, j++){ +- zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; +- } +- zBuf[j] = 0; +- zBuf[j+1] = 0; ++ u64 r; ++ sqlite3_randomness(sizeof(r), &r); ++ assert( nBuf>2 ); ++ zBuf[nBuf-2] = 0; ++ sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c", ++ zDir, r, 0); ++ if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR; + }while( osAccess(zBuf,0)==0 ); + return SQLITE_OK; + } +@@ -30983,6 +38257,8 @@ static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ + #if !OS_VXWORKS + struct stat sStat; /* Results of stat() call */ + ++ unixEnterMutex(); ++ + /* A stat() call may fail for various reasons. If this happens, it is + ** almost certain that an open() call on the same path will also fail. + ** For this reason, if an error occurs in the stat() call here, it is +@@ -30991,29 +38267,52 @@ static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ + ** + ** Even if a subsequent open() call does succeed, the consequences of + ** not searching for a reusable file descriptor are not dire. */ +- if( 0==osStat(zPath, &sStat) ){ ++ if( inodeList!=0 && 0==osStat(zPath, &sStat) ){ + unixInodeInfo *pInode; + +- unixEnterMutex(); + pInode = inodeList; + while( pInode && (pInode->fileId.dev!=sStat.st_dev +- || pInode->fileId.ino!=sStat.st_ino) ){ ++ || pInode->fileId.ino!=(u64)sStat.st_ino) ){ + pInode = pInode->pNext; + } + if( pInode ){ + UnixUnusedFd **pp; ++ assert( sqlite3_mutex_notheld(pInode->pLockMutex) ); ++ sqlite3_mutex_enter(pInode->pLockMutex); + for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); + pUnused = *pp; + if( pUnused ){ + *pp = pUnused->pNext; + } ++ sqlite3_mutex_leave(pInode->pLockMutex); + } +- unixLeaveMutex(); + } ++ unixLeaveMutex(); + #endif /* if !OS_VXWORKS */ + return pUnused; + } + ++/* ++** Find the mode, uid and gid of file zFile. ++*/ ++static int getFileMode( ++ const char *zFile, /* File name */ ++ mode_t *pMode, /* OUT: Permissions of zFile */ ++ uid_t *pUid, /* OUT: uid of zFile. */ ++ gid_t *pGid /* OUT: gid of zFile. */ ++){ ++ struct stat sStat; /* Output of stat() on database file */ ++ int rc = SQLITE_OK; ++ if( 0==osStat(zFile, &sStat) ){ ++ *pMode = sStat.st_mode & 0777; ++ *pUid = sStat.st_uid; ++ *pGid = sStat.st_gid; ++ }else{ ++ rc = SQLITE_IOERR_FSTAT; ++ } ++ return rc; ++} ++ + /* + ** This function is called by unixOpen() to determine the unix permissions + ** to create new files with. If no error occurs, then SQLITE_OK is returned +@@ -31049,7 +38348,6 @@ static int findCreateFileMode( + if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ + char zDb[MAX_PATHNAME+1]; /* Database file path */ + int nDb; /* Number of valid bytes in zDb */ +- struct stat sStat; /* Output of stat() on database file */ + + /* zPath is a path to a WAL or journal file. The following block derives + ** the path to the associated database file from zPath. This block handles +@@ -31064,28 +38362,29 @@ static int findCreateFileMode( + ** used by the test_multiplex.c module. + */ + nDb = sqlite3Strlen30(zPath) - 1; +-#ifdef SQLITE_ENABLE_8_3_NAMES +- while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--; +- if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK; +-#else + while( zPath[nDb]!='-' ){ +- assert( nDb>0 ); +- assert( zPath[nDb]!='\n' ); ++ /* In normal operation, the journal file name will always contain ++ ** a '-' character. However in 8+3 filename mode, or if a corrupt ++ ** rollback journal specifies a master journal with a goofy name, then ++ ** the '-' might be missing. */ ++ if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK; + nDb--; + } +-#endif + memcpy(zDb, zPath, nDb); + zDb[nDb] = '\0'; + +- if( 0==osStat(zDb, &sStat) ){ +- *pMode = sStat.st_mode & 0777; +- *pUid = sStat.st_uid; +- *pGid = sStat.st_gid; +- }else{ +- rc = SQLITE_IOERR_FSTAT; +- } ++ rc = getFileMode(zDb, pMode, pUid, pGid); + }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ + *pMode = 0600; ++ }else if( flags & SQLITE_OPEN_URI ){ ++ /* If this is a main database file and the file was opened using a URI ++ ** filename, check for the "modeof" parameter. If present, interpret ++ ** its value as a filename and try to copy the mode, uid and gid from ++ ** that file. */ ++ const char *z = sqlite3_uri_parameter(zPath, "modeof"); ++ if( z ){ ++ rc = getFileMode(z, pMode, pUid, pGid); ++ } + } + return rc; + } +@@ -31143,7 +38442,7 @@ static int unixOpen( + ** a file-descriptor on the directory too. The first time unixSync() + ** is called the directory file descriptor will be fsync()ed and close()d. + */ +- int syncDir = (isCreate && ( ++ int isNewJrnl = (isCreate && ( + eType==SQLITE_OPEN_MASTER_JOURNAL + || eType==SQLITE_OPEN_MAIN_JOURNAL + || eType==SQLITE_OPEN_WAL +@@ -31190,7 +38489,6 @@ static int unixOpen( + randomnessPid = osGetpid(0); + sqlite3_randomness(0,0); + } +- + memset(p, 0, sizeof(unixFile)); + + if( eType==SQLITE_OPEN_MAIN_DB ){ +@@ -31201,10 +38499,10 @@ static int unixOpen( + }else{ + pUnused = sqlite3_malloc64(sizeof(*pUnused)); + if( !pUnused ){ +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + } +- p->pUnused = pUnused; ++ p->pPreallocatedUnused = pUnused; + + /* Database filenames are double-zero terminated if they are not + ** URIs with parameters. Hence, they can always be passed into +@@ -31213,8 +38511,8 @@ static int unixOpen( + + }else if( !zName ){ + /* If zName is NULL, the upper layer is requesting a temp file. */ +- assert(isDelete && !syncDir); +- rc = unixGetTempname(MAX_PATHNAME+2, zTmpname); ++ assert(isDelete && !isNewJrnl); ++ rc = unixGetTempname(pVfs->mxPathname, zTmpname); + if( rc!=SQLITE_OK ){ + return rc; + } +@@ -31241,23 +38539,31 @@ static int unixOpen( + gid_t gid; /* Groupid for the file */ + rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid); + if( rc!=SQLITE_OK ){ +- assert( !p->pUnused ); ++ assert( !p->pPreallocatedUnused ); + assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); + return rc; + } + fd = robust_open(zName, openFlags, openMode); + OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); +- if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ +- /* Failed to open the file for read/write access. Try read-only. */ +- flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); +- openFlags &= ~(O_RDWR|O_CREAT); +- flags |= SQLITE_OPEN_READONLY; +- openFlags |= O_RDONLY; +- isReadonly = 1; +- fd = robust_open(zName, openFlags, openMode); ++ assert( !isExclusive || (openFlags & O_CREAT)!=0 ); ++ if( fd<0 ){ ++ if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){ ++ /* If unable to create a journal because the directory is not ++ ** writable, change the error code to indicate that. */ ++ rc = SQLITE_READONLY_DIRECTORY; ++ }else if( errno!=EISDIR && isReadWrite ){ ++ /* Failed to open the file for read/write access. Try read-only. */ ++ flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); ++ openFlags &= ~(O_RDWR|O_CREAT); ++ flags |= SQLITE_OPEN_READONLY; ++ openFlags |= O_RDONLY; ++ isReadonly = 1; ++ fd = robust_open(zName, openFlags, openMode); ++ } + } + if( fd<0 ){ +- rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); ++ int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); ++ if( rc==SQLITE_OK ) rc = rc2; + goto open_finished; + } + +@@ -31266,7 +38572,7 @@ static int unixOpen( + ** the same as the original database. + */ + if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ +- osFchown(fd, uid, gid); ++ robustFchown(fd, uid, gid); + } + } + assert( fd>=0 ); +@@ -31274,9 +38580,9 @@ static int unixOpen( + *pOutFlags = flags; + } + +- if( p->pUnused ){ +- p->pUnused->fd = fd; +- p->pUnused->flags = flags; ++ if( p->pPreallocatedUnused ){ ++ p->pPreallocatedUnused->fd = fd; ++ p->pPreallocatedUnused->flags = flags; + } + + if( isDelete ){ +@@ -31286,7 +38592,7 @@ static int unixOpen( + zPath = sqlite3_mprintf("%s", zName); + if( zPath==0 ){ + robust_close(p, fd, __LINE__); +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + #else + osUnlink(zName); +@@ -31297,9 +38603,6 @@ static int unixOpen( + p->openFlags = openFlags; + } + #endif +- +- noLock = eType!=SQLITE_OPEN_MAIN_DB; +- + + #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE + if( fstatfs(fd, &fsInfo) == -1 ){ +@@ -31318,8 +38621,9 @@ static int unixOpen( + /* Set up appropriate ctrlFlags */ + if( isDelete ) ctrlFlags |= UNIXFILE_DELETE; + if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY; ++ noLock = eType!=SQLITE_OPEN_MAIN_DB; + if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK; +- if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC; ++ if( isNewJrnl ) ctrlFlags |= UNIXFILE_DIRSYNC; + if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI; + + #if SQLITE_ENABLE_LOCKING_STYLE +@@ -31355,11 +38659,14 @@ static int unixOpen( + } + #endif + ++ assert( zPath==0 || zPath[0]=='/' ++ || eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL ++ ); + rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); + + open_finished: + if( rc!=SQLITE_OK ){ +- sqlite3_free(p->pUnused); ++ sqlite3_free(p->pPreallocatedUnused); + } + return rc; + } +@@ -31394,16 +38701,12 @@ static int unixDelete( + int fd; + rc = osOpenDirectory(zPath, &fd); + if( rc==SQLITE_OK ){ +-#if OS_VXWORKS +- if( fsync(fd)==-1 ) +-#else +- if( fsync(fd) ) +-#endif +- { ++ if( full_fsync(fd,0,0) ){ + rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); + } + robust_close(0, fd, __LINE__); +- }else if( rc==SQLITE_CANTOPEN ){ ++ }else{ ++ assert( rc==SQLITE_CANTOPEN ); + rc = SQLITE_OK; + } + } +@@ -31427,33 +38730,49 @@ static int unixAccess( + int flags, /* What do we want to learn about the zPath file? */ + int *pResOut /* Write result boolean here */ + ){ +- int amode = 0; + UNUSED_PARAMETER(NotUsed); + SimulateIOError( return SQLITE_IOERR_ACCESS; ); +- switch( flags ){ +- case SQLITE_ACCESS_EXISTS: +- amode = F_OK; +- break; +- case SQLITE_ACCESS_READWRITE: +- amode = W_OK|R_OK; +- break; +- case SQLITE_ACCESS_READ: +- amode = R_OK; +- break; ++ assert( pResOut!=0 ); + +- default: +- assert(!"Invalid flags argument"); +- } +- *pResOut = (osAccess(zPath, amode)==0); +- if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){ ++ /* The spec says there are three possible values for flags. But only ++ ** two of them are actually used */ ++ assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE ); ++ ++ if( flags==SQLITE_ACCESS_EXISTS ){ + struct stat buf; +- if( 0==osStat(zPath, &buf) && buf.st_size==0 ){ +- *pResOut = 0; +- } ++ *pResOut = (0==osStat(zPath, &buf) && buf.st_size>0); ++ }else{ ++ *pResOut = osAccess(zPath, W_OK|R_OK)==0; + } + return SQLITE_OK; + } + ++/* ++** ++*/ ++static int mkFullPathname( ++ const char *zPath, /* Input path */ ++ char *zOut, /* Output buffer */ ++ int nOut /* Allocated size of buffer zOut */ ++){ ++ int nPath = sqlite3Strlen30(zPath); ++ int iOff = 0; ++ if( zPath[0]!='/' ){ ++ if( osGetcwd(zOut, nOut-2)==0 ){ ++ return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); ++ } ++ iOff = sqlite3Strlen30(zOut); ++ zOut[iOff++] = '/'; ++ } ++ if( (iOff+nPath+1)>nOut ){ ++ /* SQLite assumes that xFullPathname() nul-terminates the output buffer ++ ** even if it returns an error. */ ++ zOut[iOff] = '\0'; ++ return SQLITE_CANTOPEN_BKPT; ++ } ++ sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath); ++ return SQLITE_OK; ++} + + /* + ** Turn a relative pathname into a full pathname. The relative path +@@ -31470,6 +38789,17 @@ static int unixFullPathname( + int nOut, /* Size of output buffer in bytes */ + char *zOut /* Output buffer */ + ){ ++#if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT) ++ return mkFullPathname(zPath, zOut, nOut); ++#else ++ int rc = SQLITE_OK; ++ int nByte; ++ int nLink = 1; /* Number of symbolic links followed so far */ ++ const char *zIn = zPath; /* Input path for each iteration of loop */ ++ char *zDel = 0; ++ ++ assert( pVfs->mxPathname==MAX_PATHNAME ); ++ UNUSED_PARAMETER(pVfs); + + /* It's odd to simulate an io-error here, but really this is just + ** using the io-error infrastructure to test that SQLite handles this +@@ -31478,21 +38808,62 @@ static int unixFullPathname( + */ + SimulateIOError( return SQLITE_ERROR ); + +- assert( pVfs->mxPathname==MAX_PATHNAME ); +- UNUSED_PARAMETER(pVfs); ++ do { + +- zOut[nOut-1] = '\0'; +- if( zPath[0]=='/' ){ +- sqlite3_snprintf(nOut, zOut, "%s", zPath); +- }else{ +- int nCwd; +- if( osGetcwd(zOut, nOut-1)==0 ){ +- return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); ++ /* Call stat() on path zIn. Set bLink to true if the path is a symbolic ++ ** link, or false otherwise. */ ++ int bLink = 0; ++ struct stat buf; ++ if( osLstat(zIn, &buf)!=0 ){ ++ if( errno!=ENOENT ){ ++ rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn); ++ } ++ }else{ ++ bLink = S_ISLNK(buf.st_mode); + } +- nCwd = (int)strlen(zOut); +- sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); +- } +- return SQLITE_OK; ++ ++ if( bLink ){ ++ if( zDel==0 ){ ++ zDel = sqlite3_malloc(nOut); ++ if( zDel==0 ) rc = SQLITE_NOMEM_BKPT; ++ }else if( ++nLink>SQLITE_MAX_SYMLINKS ){ ++ rc = SQLITE_CANTOPEN_BKPT; ++ } ++ ++ if( rc==SQLITE_OK ){ ++ nByte = osReadlink(zIn, zDel, nOut-1); ++ if( nByte<0 ){ ++ rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn); ++ }else{ ++ if( zDel[0]!='/' ){ ++ int n; ++ for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--); ++ if( nByte+n+1>nOut ){ ++ rc = SQLITE_CANTOPEN_BKPT; ++ }else{ ++ memmove(&zDel[n], zDel, nByte+1); ++ memcpy(zDel, zIn, n); ++ nByte += n; ++ } ++ } ++ zDel[nByte] = '\0'; ++ } ++ } ++ ++ zIn = zDel; ++ } ++ ++ assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' ); ++ if( rc==SQLITE_OK && zIn!=zOut ){ ++ rc = mkFullPathname(zIn, zOut, nOut); ++ } ++ if( bLink==0 ) break; ++ zIn = zOut; ++ }while( rc==SQLITE_OK ); ++ ++ sqlite3_free(zDel); ++ return rc; ++#endif /* HAVE_READLINK && HAVE_LSTAT */ + } + + +@@ -31661,11 +39032,8 @@ static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ + *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; + #else + struct timeval sNow; +- if( gettimeofday(&sNow, 0)==0 ){ +- *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; +- }else{ +- rc = SQLITE_ERROR; +- } ++ (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */ ++ *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; + #endif + + #ifdef SQLITE_TEST +@@ -31677,6 +39045,7 @@ static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ + return rc; + } + ++#ifndef SQLITE_OMIT_DEPRECATED + /* + ** Find the current time (in Universal Coordinated Time). Write the + ** current time and date as a Julian Day number into *prNow and +@@ -31690,19 +39059,21 @@ static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ + *prNow = i/86400000.0; + return rc; + } ++#else ++# define unixCurrentTime 0 ++#endif + + /* +-** We added the xGetLastError() method with the intention of providing +-** better low-level error messages when operating-system problems come up +-** during SQLite operation. But so far, none of that has been implemented +-** in the core. So this routine is never called. For now, it is merely +-** a place-holder. ++** The xGetLastError() method is designed to return a better ++** low-level error message when operating-system problems come up ++** during SQLite operation. Only the integer return code is currently ++** used. + */ + static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ + UNUSED_PARAMETER(NotUsed); + UNUSED_PARAMETER(NotUsed2); + UNUSED_PARAMETER(NotUsed3); +- return 0; ++ return errno; + } + + +@@ -31957,7 +39328,7 @@ static int proxyCreateLockPath(const char *lockPath){ + } + buf[i] = lockPath[i]; + } +- OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, osGetpid(0))); ++ OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0))); + return 0; + } + +@@ -31993,7 +39364,7 @@ static int proxyCreateUnixFile( + }else{ + pUnused = sqlite3_malloc64(sizeof(*pUnused)); + if( !pUnused ){ +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + } + if( fd<0 ){ +@@ -32026,7 +39397,7 @@ static int proxyCreateUnixFile( + + pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew)); + if( pNew==NULL ){ +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + goto end_create_proxy; + } + memset(pNew, 0, sizeof(unixFile)); +@@ -32036,7 +39407,7 @@ static int proxyCreateUnixFile( + dummyVfs.zName = "dummy"; + pUnused->fd = fd; + pUnused->flags = openFlags; +- pNew->pUnused = pUnused; ++ pNew->pPreallocatedUnused = pUnused; + + rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0); + if( rc==SQLITE_OK ){ +@@ -32369,7 +39740,7 @@ static int proxyTakeConch(unixFile *pFile){ + writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); + robust_ftruncate(conchFile->h, writeSize); + rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); +- fsync(conchFile->h); ++ full_fsync(conchFile->h,0,0); + /* If we created a new conch file (not just updated the contents of a + ** valid conch file), try to match the permissions of the database + */ +@@ -32439,7 +39810,7 @@ static int proxyTakeConch(unixFile *pFile){ + if( tempLockPath ){ + pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); + if( !pCtx->lockProxyPath ){ +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + } + } + } +@@ -32504,7 +39875,7 @@ static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ + ** the name of the original database file. */ + *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8); + if( conchPath==0 ){ +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + memcpy(conchPath, dbPath, len+1); + +@@ -32620,7 +39991,7 @@ static int proxyTransformUnixFile(unixFile *pFile, const char *path) { + + pCtx = sqlite3_malloc64( sizeof(*pCtx) ); + if( pCtx==0 ){ +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + memset(pCtx, 0, sizeof(*pCtx)); + +@@ -32656,7 +40027,7 @@ static int proxyTransformUnixFile(unixFile *pFile, const char *path) { + if( rc==SQLITE_OK ){ + pCtx->dbPath = sqlite3DbStrDup(0, dbPath); + if( pCtx->dbPath==NULL ){ +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + } + } + if( rc==SQLITE_OK ){ +@@ -32842,7 +40213,7 @@ static int proxyUnlock(sqlite3_file *id, int eFileLock) { + ** Close a file that uses proxy locks. + */ + static int proxyClose(sqlite3_file *id) { +- if( id ){ ++ if( ALWAYS(id) ){ + unixFile *pFile = (unixFile*)id; + proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; + unixFile *lockProxy = pCtx->lockProxy; +@@ -32903,7 +40274,7 @@ static int proxyClose(sqlite3_file *id) { + ** necessarily been initialized when this routine is called, and so they + ** should not be used. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){ ++SQLITE_API int sqlite3_os_init(void){ + /* + ** The following macro defines an initializer for an sqlite3_vfs object. + ** The name of the VFS is NAME. The pAppData is a pointer to a pointer +@@ -32986,12 +40357,13 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){ + + /* Double-check that the aSyscall[] array has been constructed + ** correctly. See ticket [bb3a86e890c8e96ab] */ +- assert( ArraySize(aSyscall)==25 ); ++ assert( ArraySize(aSyscall)==29 ); + + /* Register all VFSes defined in the aVfs[] array */ + for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ + sqlite3_vfs_register(&aVfs[i], i==0); + } ++ unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); + return SQLITE_OK; + } + +@@ -33002,7 +40374,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){ + ** to release dynamically allocated objects. But not on unix. + ** This routine is a no-op for unix. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){ ++SQLITE_API int sqlite3_os_end(void){ ++ unixBigLock = 0; + return SQLITE_OK; + } + +@@ -33024,6 +40397,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){ + ** + ** This file contains code that is specific to Windows. + */ ++/* #include "sqliteInt.h" */ + #if SQLITE_OS_WIN /* This file is used for Windows only */ + + /* +@@ -33068,8 +40442,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){ + */ + #ifdef SQLITE_PERFORMANCE_TRACE + +-/* +-** hwtime.h contains inline assembler code for implementing ++/* ++** hwtime.h contains inline assembler code for implementing + ** high-performance timing routines. + */ + /************** Include hwtime.h in the middle of os_common.h ****************/ +@@ -33089,8 +40463,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){ + ** This file contains inline asm code for retrieving "high-performance" + ** counters for x86 class CPUs. + */ +-#ifndef _HWTIME_H_ +-#define _HWTIME_H_ ++#ifndef SQLITE_HWTIME_H ++#define SQLITE_HWTIME_H + + /* + ** The following routine only works on pentium-class (or newer) processors. +@@ -33158,7 +40532,7 @@ SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); } + + #endif + +-#endif /* !defined(_HWTIME_H_) */ ++#endif /* !defined(SQLITE_HWTIME_H) */ + + /************** End of hwtime.h **********************************************/ + /************** Continuing where we left off in os_common.h ******************/ +@@ -33179,14 +40553,14 @@ static sqlite_uint64 g_elapsed; + ** of code will give us the ability to simulate a disk I/O error. This + ** is used for testing the I/O recovery logic. + */ +-#ifdef SQLITE_TEST +-SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */ +-SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */ +-SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */ +-SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */ +-SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */ +-SQLITE_API int sqlite3_diskfull_pending = 0; +-SQLITE_API int sqlite3_diskfull = 0; ++#if defined(SQLITE_TEST) ++SQLITE_API extern int sqlite3_io_error_hit; ++SQLITE_API extern int sqlite3_io_error_hardhit; ++SQLITE_API extern int sqlite3_io_error_pending; ++SQLITE_API extern int sqlite3_io_error_persist; ++SQLITE_API extern int sqlite3_io_error_benign; ++SQLITE_API extern int sqlite3_diskfull_pending; ++SQLITE_API extern int sqlite3_diskfull; + #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X) + #define SimulateIOError(CODE) \ + if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \ +@@ -33212,17 +40586,17 @@ static void local_ioerr(){ + #define SimulateIOErrorBenign(X) + #define SimulateIOError(A) + #define SimulateDiskfullError(A) +-#endif ++#endif /* defined(SQLITE_TEST) */ + + /* + ** When testing, keep a count of the number of open files. + */ +-#ifdef SQLITE_TEST +-SQLITE_API int sqlite3_open_file_count = 0; ++#if defined(SQLITE_TEST) ++SQLITE_API extern int sqlite3_open_file_count; + #define OpenCounter(X) sqlite3_open_file_count+=(X) + #else + #define OpenCounter(X) +-#endif ++#endif /* defined(SQLITE_TEST) */ + + #endif /* !defined(_OS_COMMON_H_) */ + +@@ -33232,6 +40606,7 @@ SQLITE_API int sqlite3_open_file_count = 0; + /* + ** Include the header file for the Windows VFS. + */ ++/* #include "os_win.h" */ + + /* + ** Compiling and using WAL mode requires several APIs that are only +@@ -33284,6 +40659,10 @@ SQLITE_API int sqlite3_open_file_count = 0; + # define NTDDI_WINBLUE 0x06030000 + #endif + ++#ifndef NTDDI_WINTHRESHOLD ++# define NTDDI_WINTHRESHOLD 0x06040000 ++#endif ++ + /* + ** Check to see if the GetVersionEx[AW] functions are deprecated on the + ** target system. GetVersionEx was first deprecated in Win8.1. +@@ -33296,6 +40675,19 @@ SQLITE_API int sqlite3_open_file_count = 0; + # endif + #endif + ++/* ++** Check to see if the CreateFileMappingA function is supported on the ++** target system. It is unavailable when using "mincore.lib" on Win10. ++** When compiling for Windows 10, always assume "mincore.lib" is in use. ++*/ ++#ifndef SQLITE_WIN32_CREATEFILEMAPPINGA ++# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD ++# define SQLITE_WIN32_CREATEFILEMAPPINGA 0 ++# else ++# define SQLITE_WIN32_CREATEFILEMAPPINGA 1 ++# endif ++#endif ++ + /* + ** This constant should already be defined (in the "WinDef.h" SDK file). + */ +@@ -33475,12 +40867,22 @@ struct winFile { + int nFetchOut; /* Number of outstanding xFetch references */ + HANDLE hMap; /* Handle for accessing memory mapping */ + void *pMapRegion; /* Area memory mapped */ +- sqlite3_int64 mmapSize; /* Usable size of mapped region */ +- sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */ ++ sqlite3_int64 mmapSize; /* Size of mapped region */ + sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ + #endif + }; + ++/* ++** The winVfsAppData structure is used for the pAppData member for all of the ++** Win32 VFS variants. ++*/ ++typedef struct winVfsAppData winVfsAppData; ++struct winVfsAppData { ++ const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */ ++ void *pAppData; /* The extra pAppData, if any. */ ++ BOOL bNoLock; /* Non-zero if locking is disabled. */ ++}; ++ + /* + ** Allowed values for winFile.ctrlFlags + */ +@@ -33495,22 +40897,6 @@ struct winFile { + # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD))) + #endif + +-/* +- * The value used with sqlite3_win32_set_directory() to specify that +- * the data directory should be changed. +- */ +-#ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE +-# define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1) +-#endif +- +-/* +- * The value used with sqlite3_win32_set_directory() to specify that +- * the temporary directory should be changed. +- */ +-#ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE +-# define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2) +-#endif +- + /* + * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the + * various Win32 API heap functions instead of our own. +@@ -33532,22 +40918,72 @@ struct winFile { + ****************************************************************************** + */ + #ifndef SQLITE_WIN32_HEAP_CREATE +-# define SQLITE_WIN32_HEAP_CREATE (TRUE) ++# define SQLITE_WIN32_HEAP_CREATE (TRUE) ++#endif ++ ++/* ++ * This is the maximum possible initial size of the Win32-specific heap, in ++ * bytes. ++ */ ++#ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE ++# define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U) ++#endif ++ ++/* ++ * This is the extra space for the initial size of the Win32-specific heap, ++ * in bytes. This value may be zero. ++ */ ++#ifndef SQLITE_WIN32_HEAP_INIT_EXTRA ++# define SQLITE_WIN32_HEAP_INIT_EXTRA (4194304) ++#endif ++ ++/* ++ * Calculate the maximum legal cache size, in pages, based on the maximum ++ * possible initial heap size and the default page size, setting aside the ++ * needed extra space. ++ */ ++#ifndef SQLITE_WIN32_MAX_CACHE_SIZE ++# define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \ ++ (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \ ++ (SQLITE_DEFAULT_PAGE_SIZE)) ++#endif ++ ++/* ++ * This is cache size used in the calculation of the initial size of the ++ * Win32-specific heap. It cannot be negative. ++ */ ++#ifndef SQLITE_WIN32_CACHE_SIZE ++# if SQLITE_DEFAULT_CACHE_SIZE>=0 ++# define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE) ++# else ++# define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE)) ++# endif ++#endif ++ ++/* ++ * Make sure that the calculated cache size, in pages, cannot cause the ++ * initial size of the Win32-specific heap to exceed the maximum amount ++ * of memory that can be specified in the call to HeapCreate. ++ */ ++#if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE ++# undef SQLITE_WIN32_CACHE_SIZE ++# define SQLITE_WIN32_CACHE_SIZE (2000) + #endif + + /* + * The initial size of the Win32-specific heap. This value may be zero. + */ + #ifndef SQLITE_WIN32_HEAP_INIT_SIZE +-# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \ +- (SQLITE_DEFAULT_PAGE_SIZE) + 4194304) ++# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \ ++ (SQLITE_DEFAULT_PAGE_SIZE) + \ ++ (SQLITE_WIN32_HEAP_INIT_EXTRA)) + #endif + + /* + * The maximum size of the Win32-specific heap. This value may be zero. + */ + #ifndef SQLITE_WIN32_HEAP_MAX_SIZE +-# define SQLITE_WIN32_HEAP_MAX_SIZE (0) ++# define SQLITE_WIN32_HEAP_MAX_SIZE (0) + #endif + + /* +@@ -33555,7 +40991,7 @@ struct winFile { + * zero for the default behavior. + */ + #ifndef SQLITE_WIN32_HEAP_FLAGS +-# define SQLITE_WIN32_HEAP_FLAGS (0) ++# define SQLITE_WIN32_HEAP_FLAGS (0) + #endif + + +@@ -33702,8 +41138,9 @@ static struct win_syscall { + #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \ + LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent) + +-#if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \ +- (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) ++#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \ ++ (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \ ++ SQLITE_WIN32_CREATEFILEMAPPINGA + { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 }, + #else + { "CreateFileMappingA", (SYSCALL)0, 0 }, +@@ -33933,8 +41370,7 @@ static struct win_syscall { + + #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent) + +-#if defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_GETVERSIONEX) && \ +- SQLITE_WIN32_GETVERSIONEX ++#if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX + { "GetVersionExA", (SYSCALL)GetVersionExA, 0 }, + #else + { "GetVersionExA", (SYSCALL)0, 0 }, +@@ -33944,7 +41380,7 @@ static struct win_syscall { + LPOSVERSIONINFOA))aSyscall[34].pCurrent) + + #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \ +- defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX ++ SQLITE_WIN32_GETVERSIONEX + { "GetVersionExW", (SYSCALL)GetVersionExW, 0 }, + #else + { "GetVersionExW", (SYSCALL)0, 0 }, +@@ -34395,7 +41831,7 @@ static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){ + ** "pnLargest" argument, if non-zero, will be used to return the size of the + ** largest committed free block in the heap, in bytes. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_win32_compact_heap(LPUINT pnLargest){ ++SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){ + int rc = SQLITE_OK; + UINT nLargest = 0; + HANDLE hHeap; +@@ -34413,7 +41849,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_win32_compact_heap(LPUINT pnLargest){ + if( lastErrno==NO_ERROR ){ + sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p", + (void*)hHeap); +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + }else{ + sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p", + osGetLastError(), (void*)hHeap); +@@ -34435,12 +41871,12 @@ SQLITE_API int SQLITE_STDCALL sqlite3_win32_compact_heap(LPUINT pnLargest){ + ** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will + ** be returned and no changes will be made to the Win32 native heap. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_win32_reset_heap(){ ++SQLITE_API int sqlite3_win32_reset_heap(){ + int rc; + MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */ + MUTEX_LOGIC( sqlite3_mutex *pMem; ) /* The memsys static mutex */ +- MUTEX_LOGIC( pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); ) +- MUTEX_LOGIC( pMem = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM); ) ++ MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) ++ MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); ) + sqlite3_mutex_enter(pMaster); + sqlite3_mutex_enter(pMem); + winMemAssertMagic(); +@@ -34480,11 +41916,17 @@ SQLITE_API int SQLITE_STDCALL sqlite3_win32_reset_heap(){ + ** (if available). + */ + +-SQLITE_API void SQLITE_STDCALL sqlite3_win32_write_debug(const char *zBuf, int nBuf){ ++SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){ + char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE]; + int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */ + if( nMin<-1 ) nMin = -1; /* all negative values become -1. */ + assert( nMin==-1 || nMin==0 || nMin 0 ){ + memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE); +@@ -34520,7 +41962,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_win32_write_debug(const char *zBuf, int n + static HANDLE sleepObj = NULL; + #endif + +-SQLITE_API void SQLITE_STDCALL sqlite3_win32_sleep(DWORD milliseconds){ ++SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){ + #if SQLITE_OS_WINRT + if ( sleepObj==NULL ){ + sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET, +@@ -34555,7 +41997,7 @@ SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){ + ** the LockFileEx() API. + */ + +-#if !defined(SQLITE_WIN32_GETVERSIONEX) || !SQLITE_WIN32_GETVERSIONEX ++#if !SQLITE_WIN32_GETVERSIONEX + # define osIsNT() (1) + #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI) + # define osIsNT() (1) +@@ -34569,14 +42011,14 @@ SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){ + ** This function determines if the machine is running a version of Windows + ** based on the NT kernel. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_win32_is_nt(void){ ++SQLITE_API int sqlite3_win32_is_nt(void){ + #if SQLITE_OS_WINRT + /* + ** NOTE: The WinRT sub-platform is always assumed to be based on the NT + ** kernel. + */ + return 1; +-#elif defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX ++#elif SQLITE_WIN32_GETVERSIONEX + if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){ + #if defined(SQLITE_WIN32_HAS_ANSI) + OSVERSIONINFOA sInfo; +@@ -34733,7 +42175,7 @@ static int winMemInit(void *pAppData){ + "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu", + osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize, + dwMaximumSize); +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + pWinMemData->bOwned = TRUE; + assert( pWinMemData->bOwned ); +@@ -34743,7 +42185,7 @@ static int winMemInit(void *pAppData){ + if( !pWinMemData->hHeap ){ + sqlite3_log(SQLITE_NOMEM, + "failed to GetProcessHeap (%lu)", osGetLastError()); +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + pWinMemData->bOwned = FALSE; + assert( !pWinMemData->bOwned ); +@@ -34810,157 +42252,254 @@ SQLITE_PRIVATE void sqlite3MemSetDefault(void){ + #endif /* SQLITE_WIN32_MALLOC */ + + /* +-** Convert a UTF-8 string to Microsoft Unicode (UTF-16?). ++** Convert a UTF-8 string to Microsoft Unicode. + ** +-** Space to hold the returned string is obtained from malloc. ++** Space to hold the returned string is obtained from sqlite3_malloc(). + */ +-static LPWSTR winUtf8ToUnicode(const char *zFilename){ ++static LPWSTR winUtf8ToUnicode(const char *zText){ + int nChar; +- LPWSTR zWideFilename; ++ LPWSTR zWideText; + +- nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0); ++ nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0); + if( nChar==0 ){ + return 0; + } +- zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) ); +- if( zWideFilename==0 ){ ++ zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) ); ++ if( zWideText==0 ){ + return 0; + } +- nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, ++ nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText, + nChar); + if( nChar==0 ){ +- sqlite3_free(zWideFilename); +- zWideFilename = 0; ++ sqlite3_free(zWideText); ++ zWideText = 0; + } +- return zWideFilename; ++ return zWideText; + } + + /* +-** Convert Microsoft Unicode to UTF-8. Space to hold the returned string is +-** obtained from sqlite3_malloc(). ++** Convert a Microsoft Unicode string to UTF-8. ++** ++** Space to hold the returned string is obtained from sqlite3_malloc(). + */ +-static char *winUnicodeToUtf8(LPCWSTR zWideFilename){ ++static char *winUnicodeToUtf8(LPCWSTR zWideText){ + int nByte; +- char *zFilename; ++ char *zText; + +- nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0); ++ nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0); + if( nByte == 0 ){ + return 0; + } +- zFilename = sqlite3MallocZero( nByte ); +- if( zFilename==0 ){ ++ zText = sqlite3MallocZero( nByte ); ++ if( zText==0 ){ + return 0; + } +- nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte, ++ nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte, + 0, 0); + if( nByte == 0 ){ +- sqlite3_free(zFilename); +- zFilename = 0; ++ sqlite3_free(zText); ++ zText = 0; + } +- return zFilename; ++ return zText; + } + + /* +-** Convert an ANSI string to Microsoft Unicode, based on the +-** current codepage settings for file apis. ++** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM ++** code page. + ** +-** Space to hold the returned string is obtained +-** from sqlite3_malloc. ++** Space to hold the returned string is obtained from sqlite3_malloc(). + */ +-static LPWSTR winMbcsToUnicode(const char *zFilename){ ++static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){ + int nByte; +- LPWSTR zMbcsFilename; +- int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP; ++ LPWSTR zMbcsText; ++ int codepage = useAnsi ? CP_ACP : CP_OEMCP; + +- nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL, ++ nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL, + 0)*sizeof(WCHAR); + if( nByte==0 ){ + return 0; + } +- zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) ); +- if( zMbcsFilename==0 ){ ++ zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) ); ++ if( zMbcsText==0 ){ + return 0; + } +- nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, ++ nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText, + nByte); + if( nByte==0 ){ +- sqlite3_free(zMbcsFilename); +- zMbcsFilename = 0; ++ sqlite3_free(zMbcsText); ++ zMbcsText = 0; + } +- return zMbcsFilename; ++ return zMbcsText; + } + + /* +-** Convert Microsoft Unicode to multi-byte character string, based on the +-** user's ANSI codepage. ++** Convert a Microsoft Unicode string to a multi-byte character string, ++** using the ANSI or OEM code page. + ** +-** Space to hold the returned string is obtained from +-** sqlite3_malloc(). ++** Space to hold the returned string is obtained from sqlite3_malloc(). + */ +-static char *winUnicodeToMbcs(LPCWSTR zWideFilename){ ++static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){ + int nByte; +- char *zFilename; +- int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP; ++ char *zText; ++ int codepage = useAnsi ? CP_ACP : CP_OEMCP; + +- nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0); ++ nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0); + if( nByte == 0 ){ + return 0; + } +- zFilename = sqlite3MallocZero( nByte ); +- if( zFilename==0 ){ ++ zText = sqlite3MallocZero( nByte ); ++ if( zText==0 ){ + return 0; + } +- nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, ++ nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText, + nByte, 0, 0); + if( nByte == 0 ){ +- sqlite3_free(zFilename); +- zFilename = 0; ++ sqlite3_free(zText); ++ zText = 0; + } +- return zFilename; ++ return zText; + } + + /* +-** Convert multibyte character string to UTF-8. Space to hold the +-** returned string is obtained from sqlite3_malloc(). ++** Convert a multi-byte character string to UTF-8. ++** ++** Space to hold the returned string is obtained from sqlite3_malloc(). + */ +-SQLITE_API char *SQLITE_STDCALL sqlite3_win32_mbcs_to_utf8(const char *zFilename){ +- char *zFilenameUtf8; ++static char *winMbcsToUtf8(const char *zText, int useAnsi){ ++ char *zTextUtf8; + LPWSTR zTmpWide; + +- zTmpWide = winMbcsToUnicode(zFilename); ++ zTmpWide = winMbcsToUnicode(zText, useAnsi); + if( zTmpWide==0 ){ + return 0; + } +- zFilenameUtf8 = winUnicodeToUtf8(zTmpWide); ++ zTextUtf8 = winUnicodeToUtf8(zTmpWide); + sqlite3_free(zTmpWide); +- return zFilenameUtf8; ++ return zTextUtf8; + } + + /* +-** Convert UTF-8 to multibyte character string. Space to hold the +-** returned string is obtained from sqlite3_malloc(). ++** Convert a UTF-8 string to a multi-byte character string. ++** ++** Space to hold the returned string is obtained from sqlite3_malloc(). + */ +-SQLITE_API char *SQLITE_STDCALL sqlite3_win32_utf8_to_mbcs(const char *zFilename){ +- char *zFilenameMbcs; ++static char *winUtf8ToMbcs(const char *zText, int useAnsi){ ++ char *zTextMbcs; + LPWSTR zTmpWide; + +- zTmpWide = winUtf8ToUnicode(zFilename); ++ zTmpWide = winUtf8ToUnicode(zText); + if( zTmpWide==0 ){ + return 0; + } +- zFilenameMbcs = winUnicodeToMbcs(zTmpWide); ++ zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi); + sqlite3_free(zTmpWide); +- return zFilenameMbcs; ++ return zTextMbcs; + } + + /* +-** This function sets the data directory or the temporary directory based on +-** the provided arguments. The type argument must be 1 in order to set the +-** data directory or 2 in order to set the temporary directory. The zValue +-** argument is the name of the directory to use. The return value will be +-** SQLITE_OK if successful. ++** This is a public wrapper for the winUtf8ToUnicode() function. ++*/ ++SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){ ++#ifdef SQLITE_ENABLE_API_ARMOR ++ if( !zText ){ ++ (void)SQLITE_MISUSE_BKPT; ++ return 0; ++ } ++#endif ++#ifndef SQLITE_OMIT_AUTOINIT ++ if( sqlite3_initialize() ) return 0; ++#endif ++ return winUtf8ToUnicode(zText); ++} ++ ++/* ++** This is a public wrapper for the winUnicodeToUtf8() function. ++*/ ++SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){ ++#ifdef SQLITE_ENABLE_API_ARMOR ++ if( !zWideText ){ ++ (void)SQLITE_MISUSE_BKPT; ++ return 0; ++ } ++#endif ++#ifndef SQLITE_OMIT_AUTOINIT ++ if( sqlite3_initialize() ) return 0; ++#endif ++ return winUnicodeToUtf8(zWideText); ++} ++ ++/* ++** This is a public wrapper for the winMbcsToUtf8() function. ++*/ ++SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zText){ ++#ifdef SQLITE_ENABLE_API_ARMOR ++ if( !zText ){ ++ (void)SQLITE_MISUSE_BKPT; ++ return 0; ++ } ++#endif ++#ifndef SQLITE_OMIT_AUTOINIT ++ if( sqlite3_initialize() ) return 0; ++#endif ++ return winMbcsToUtf8(zText, osAreFileApisANSI()); ++} ++ ++/* ++** This is a public wrapper for the winMbcsToUtf8() function. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){ ++SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){ ++#ifdef SQLITE_ENABLE_API_ARMOR ++ if( !zText ){ ++ (void)SQLITE_MISUSE_BKPT; ++ return 0; ++ } ++#endif ++#ifndef SQLITE_OMIT_AUTOINIT ++ if( sqlite3_initialize() ) return 0; ++#endif ++ return winMbcsToUtf8(zText, useAnsi); ++} ++ ++/* ++** This is a public wrapper for the winUtf8ToMbcs() function. ++*/ ++SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zText){ ++#ifdef SQLITE_ENABLE_API_ARMOR ++ if( !zText ){ ++ (void)SQLITE_MISUSE_BKPT; ++ return 0; ++ } ++#endif ++#ifndef SQLITE_OMIT_AUTOINIT ++ if( sqlite3_initialize() ) return 0; ++#endif ++ return winUtf8ToMbcs(zText, osAreFileApisANSI()); ++} ++ ++/* ++** This is a public wrapper for the winUtf8ToMbcs() function. ++*/ ++SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){ ++#ifdef SQLITE_ENABLE_API_ARMOR ++ if( !zText ){ ++ (void)SQLITE_MISUSE_BKPT; ++ return 0; ++ } ++#endif ++#ifndef SQLITE_OMIT_AUTOINIT ++ if( sqlite3_initialize() ) return 0; ++#endif ++ return winUtf8ToMbcs(zText, useAnsi); ++} ++ ++/* ++** This function is the same as sqlite3_win32_set_directory (below); however, ++** it accepts a UTF-8 string. ++*/ ++SQLITE_API int sqlite3_win32_set_directory8( ++ unsigned long type, /* Identifier for directory being set or reset */ ++ const char *zValue /* New value for directory being set or reset */ ++){ + char **ppDirectory = 0; + #ifndef SQLITE_OMIT_AUTOINIT + int rc = sqlite3_initialize(); +@@ -34976,20 +42515,53 @@ SQLITE_API int SQLITE_STDCALL sqlite3_win32_set_directory(DWORD type, LPCWSTR zV + ); + assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) ); + if( ppDirectory ){ +- char *zValueUtf8 = 0; ++ char *zCopy = 0; + if( zValue && zValue[0] ){ +- zValueUtf8 = winUnicodeToUtf8(zValue); +- if ( zValueUtf8==0 ){ +- return SQLITE_NOMEM; ++ zCopy = sqlite3_mprintf("%s", zValue); ++ if ( zCopy==0 ){ ++ return SQLITE_NOMEM_BKPT; + } + } + sqlite3_free(*ppDirectory); +- *ppDirectory = zValueUtf8; ++ *ppDirectory = zCopy; + return SQLITE_OK; + } + return SQLITE_ERROR; + } + ++/* ++** This function is the same as sqlite3_win32_set_directory (below); however, ++** it accepts a UTF-16 string. ++*/ ++SQLITE_API int sqlite3_win32_set_directory16( ++ unsigned long type, /* Identifier for directory being set or reset */ ++ const void *zValue /* New value for directory being set or reset */ ++){ ++ int rc; ++ char *zUtf8 = 0; ++ if( zValue ){ ++ zUtf8 = sqlite3_win32_unicode_to_utf8(zValue); ++ if( zUtf8==0 ) return SQLITE_NOMEM_BKPT; ++ } ++ rc = sqlite3_win32_set_directory8(type, zUtf8); ++ if( zUtf8 ) sqlite3_free(zUtf8); ++ return rc; ++} ++ ++/* ++** This function sets the data directory or the temporary directory based on ++** the provided arguments. The type argument must be 1 in order to set the ++** data directory or 2 in order to set the temporary directory. The zValue ++** argument is the name of the directory to use. The return value will be ++** SQLITE_OK if successful. ++*/ ++SQLITE_API int sqlite3_win32_set_directory( ++ unsigned long type, /* Identifier for directory being set or reset */ ++ void *zValue /* New value for directory being set or reset */ ++){ ++ return sqlite3_win32_set_directory16(type, zValue); ++} ++ + /* + ** The return value of winGetLastErrorMsg + ** is zero if the error message fits in the buffer, or non-zero +@@ -35052,7 +42624,7 @@ static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){ + if( dwLen > 0 ){ + /* allocate a buffer and convert to UTF8 */ + sqlite3BeginBenignMalloc(); +- zOut = sqlite3_win32_mbcs_to_utf8(zTemp); ++ zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI()); + sqlite3EndBenignMalloc(); + /* free the system buffer allocated by FormatMessage */ + osLocalFree(zTemp); +@@ -35194,16 +42766,17 @@ static void winLogIoerr(int nRetry, int lineno){ + } + } + +-#if SQLITE_OS_WINCE +-/************************************************************************* +-** This section contains code for WinCE only. ++/* ++** This #if does not rely on the SQLITE_OS_WINCE define because the ++** corresponding section in "date.c" cannot use it. + */ +-#if !defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API ++#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \ ++ (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API) + /* +-** The MSVC CRT on Windows CE may not have a localtime() function. So +-** create a substitute. ++** The MSVC CRT on Windows CE may not have a localtime() function. ++** So define a substitute. + */ +-/* #include */ ++/* # include */ + struct tm *__cdecl localtime(const time_t *t) + { + static struct tm y; +@@ -35227,6 +42800,10 @@ struct tm *__cdecl localtime(const time_t *t) + } + #endif + ++#if SQLITE_OS_WINCE ++/************************************************************************* ++** This section contains code for WinCE only. ++*/ + #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)] + + /* +@@ -35257,7 +42834,7 @@ static int winceCreateLock(const char *zFilename, winFile *pFile){ + zName = winUtf8ToUnicode(zFilename); + if( zName==0 ){ + /* out of memory */ +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + + /* Initialize the local lockdata */ +@@ -35682,7 +43259,12 @@ static int winClose(sqlite3_file *id){ + }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) ); + #if SQLITE_OS_WINCE + #define WINCE_DELETION_ATTEMPTS 3 +- winceDestroyLock(pFile); ++ { ++ winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData; ++ if( pAppData==NULL || !pAppData->bNoLock ){ ++ winceDestroyLock(pFile); ++ } ++ } + if( pFile->zDeleteOnClose ){ + int cnt = 0; + while( +@@ -35810,7 +43392,7 @@ static int winWrite( + "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile, + pFile->h, pBuf, amt, offset, pFile->locktype)); + +-#if SQLITE_MAX_MMAP_SIZE>0 ++#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 + /* Deal with as much of this write request as possible by transfering + ** data from the memory mapping using memcpy(). */ + if( offset mmapSize ){ +@@ -35904,6 +43486,29 @@ static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){ + winFile *pFile = (winFile*)id; /* File handle object */ + int rc = SQLITE_OK; /* Return code for this function */ + DWORD lastErrno; ++#if SQLITE_MAX_MMAP_SIZE>0 ++ sqlite3_int64 oldMmapSize; ++ if( pFile->nFetchOut>0 ){ ++ /* File truncation is a no-op if there are outstanding memory mapped ++ ** pages. This is because truncating the file means temporarily unmapping ++ ** the file, and that might delete memory out from under existing cursors. ++ ** ++ ** This can result in incremental vacuum not truncating the file, ++ ** if there is an active read cursor when the incremental vacuum occurs. ++ ** No real harm comes of this - the database file is not corrupted, ++ ** though some folks might complain that the file is bigger than it ++ ** needs to be. ++ ** ++ ** The only feasible work-around is to defer the truncation until after ++ ** all references to memory-mapped content are closed. That is doable, ++ ** but involves adding a few branches in the common write code path which ++ ** could slow down normal operations slightly. Hence, we have decided for ++ ** now to simply make trancations a no-op if there are pending reads. We ++ ** can maybe revisit this decision in the future. ++ */ ++ return SQLITE_OK; ++ } ++#endif + + assert( pFile ); + SimulateIOError(return SQLITE_IOERR_TRUNCATE); +@@ -35919,6 +43524,15 @@ static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){ + nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; + } + ++#if SQLITE_MAX_MMAP_SIZE>0 ++ if( pFile->pMapRegion ){ ++ oldMmapSize = pFile->mmapSize; ++ }else{ ++ oldMmapSize = 0; ++ } ++ winUnmapfile(pFile); ++#endif ++ + /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */ + if( winSeekFile(pFile, nByte) ){ + rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno, +@@ -35931,12 +43545,12 @@ static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){ + } + + #if SQLITE_MAX_MMAP_SIZE>0 +- /* If the file was truncated to a size smaller than the currently +- ** mapped region, reduce the effective mapping size as well. SQLite will +- ** use read() and write() to access data beyond this point from now on. +- */ +- if( pFile->pMapRegion && nByte mmapSize ){ +- pFile->mmapSize = nByte; ++ if( rc==SQLITE_OK && oldMmapSize>0 ){ ++ if( oldMmapSize>nByte ){ ++ winMapfile(pFile, -1); ++ }else{ ++ winMapfile(pFile, oldMmapSize); ++ } + } + #endif + +@@ -36223,6 +43837,12 @@ static int winLock(sqlite3_file *id, int locktype){ + return SQLITE_OK; + } + ++ /* Do not allow any kind of write-lock on a read-only database ++ */ ++ if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){ ++ return SQLITE_IOERR_LOCK; ++ } ++ + /* Make sure the locking sequence is correct + */ + assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); +@@ -36234,9 +43854,8 @@ static int winLock(sqlite3_file *id, int locktype){ + ** the PENDING_LOCK byte is temporary. + */ + newLocktype = pFile->locktype; +- if( (pFile->locktype==NO_LOCK) +- || ( (locktype==EXCLUSIVE_LOCK) +- && (pFile->locktype==RESERVED_LOCK)) ++ if( pFile->locktype==NO_LOCK ++ || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK) + ){ + int cnt = 3; + while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, +@@ -36352,7 +43971,7 @@ static int winCheckReservedLock(sqlite3_file *id, int *pResOut){ + res = 1; + OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res)); + }else{ +- res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0); ++ res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0); + if( res ){ + winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0); + } +@@ -36409,6 +44028,44 @@ static int winUnlock(sqlite3_file *id, int locktype){ + return rc; + } + ++/****************************************************************************** ++****************************** No-op Locking ********************************** ++** ++** Of the various locking implementations available, this is by far the ++** simplest: locking is ignored. No attempt is made to lock the database ++** file for reading or writing. ++** ++** This locking mode is appropriate for use on read-only databases ++** (ex: databases that are burned into CD-ROM, for example.) It can ++** also be used if the application employs some external mechanism to ++** prevent simultaneous access of the same database by two or more ++** database connections. But there is a serious risk of database ++** corruption if this locking mode is used in situations where multiple ++** database connections are accessing the same database file at the same ++** time and one or more of those connections are writing. ++*/ ++ ++static int winNolockLock(sqlite3_file *id, int locktype){ ++ UNUSED_PARAMETER(id); ++ UNUSED_PARAMETER(locktype); ++ return SQLITE_OK; ++} ++ ++static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){ ++ UNUSED_PARAMETER(id); ++ UNUSED_PARAMETER(pResOut); ++ return SQLITE_OK; ++} ++ ++static int winNolockUnlock(sqlite3_file *id, int locktype){ ++ UNUSED_PARAMETER(id); ++ UNUSED_PARAMETER(locktype); ++ return SQLITE_OK; ++} ++ ++/******************* End of the no-op lock implementation ********************* ++******************************************************************************/ ++ + /* + ** If *pArg is initially negative then this is a query. Set *pArg to + ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. +@@ -36442,7 +44099,7 @@ static int winFileControl(sqlite3_file *id, int op, void *pArg){ + OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); + return SQLITE_OK; + } +- case SQLITE_LAST_ERRNO: { ++ case SQLITE_FCNTL_LAST_ERRNO: { + *(int*)pArg = (int)pFile->lastErrno; + OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); + return SQLITE_OK; +@@ -36500,6 +44157,12 @@ static int winFileControl(sqlite3_file *id, int op, void *pArg){ + OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); + return SQLITE_OK; + } ++ case SQLITE_FCNTL_WIN32_GET_HANDLE: { ++ LPHANDLE phFile = (LPHANDLE)pArg; ++ *phFile = pFile->h; ++ OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); ++ return SQLITE_OK; ++ } + #ifdef SQLITE_TEST + case SQLITE_FCNTL_WIN32_SET_HANDLE: { + LPHANDLE phFile = (LPHANDLE)pArg; +@@ -36527,6 +44190,14 @@ static int winFileControl(sqlite3_file *id, int op, void *pArg){ + if( newLimit>sqlite3GlobalConfig.mxMmap ){ + newLimit = sqlite3GlobalConfig.mxMmap; + } ++ ++ /* The value of newLimit may be eventually cast to (SIZE_T) and passed ++ ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at ++ ** least a 64-bit type. */ ++ if( newLimit>0 && sizeof(SIZE_T)<8 ){ ++ newLimit = (newLimit & 0x7FFFFFFF); ++ } ++ + *(i64*)pArg = pFile->mmapSizeMax; + if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ + pFile->mmapSizeMax = newLimit; +@@ -36591,15 +44262,16 @@ static SYSTEM_INFO winSysInfo; + ** assert( winShmMutexHeld() ); + ** winShmLeaveMutex() + */ ++static sqlite3_mutex *winBigLock = 0; + static void winShmEnterMutex(void){ +- sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); ++ sqlite3_mutex_enter(winBigLock); + } + static void winShmLeaveMutex(void){ +- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); ++ sqlite3_mutex_leave(winBigLock); + } + #ifndef NDEBUG + static int winShmMutexHeld(void) { +- return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); ++ return sqlite3_mutex_held(winBigLock); + } + #endif + +@@ -36633,6 +44305,9 @@ struct winShmNode { + + int szRegion; /* Size of shared-memory regions */ + int nRegion; /* Size of array apRegion */ ++ u8 isReadonly; /* True if read-only */ ++ u8 isUnlocked; /* True if no DMS lock held */ ++ + struct ShmRegion { + HANDLE hMap; /* File handle from CreateFileMapping */ + void *pMap; +@@ -36687,30 +44362,30 @@ struct winShm { + /* + ** Apply advisory locks for all n bytes beginning at ofst. + */ +-#define _SHM_UNLCK 1 +-#define _SHM_RDLCK 2 +-#define _SHM_WRLCK 3 ++#define WINSHM_UNLCK 1 ++#define WINSHM_RDLCK 2 ++#define WINSHM_WRLCK 3 + static int winShmSystemLock( + winShmNode *pFile, /* Apply locks to this open shared-memory segment */ +- int lockType, /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */ ++ int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */ + int ofst, /* Offset to first byte to be locked/unlocked */ + int nByte /* Number of bytes to lock or unlock */ + ){ + int rc = 0; /* Result code form Lock/UnlockFileEx() */ + + /* Access to the winShmNode object is serialized by the caller */ +- assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 ); ++ assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) ); + + OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n", + pFile->hFile.h, lockType, ofst, nByte)); + + /* Release/Acquire the system-level lock */ +- if( lockType==_SHM_UNLCK ){ ++ if( lockType==WINSHM_UNLCK ){ + rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0); + }else{ + /* Initialize the locking parameters */ + DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY; +- if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK; ++ if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK; + rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0); + } + +@@ -36722,7 +44397,7 @@ static int winShmSystemLock( + } + + OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n", +- pFile->hFile.h, (lockType == _SHM_UNLCK) ? "winUnlockFile" : ++ pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" : + "winLockFile", pFile->lastErrno, sqlite3ErrName(rc))); + + return rc; +@@ -36780,6 +44455,37 @@ static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){ + } + } + ++/* ++** The DMS lock has not yet been taken on shm file pShmNode. Attempt to ++** take it now. Return SQLITE_OK if successful, or an SQLite error ++** code otherwise. ++** ++** If the DMS cannot be locked because this is a readonly_shm=1 ++** connection and no other process already holds a lock, return ++** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1. ++*/ ++static int winLockSharedMemory(winShmNode *pShmNode){ ++ int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1); ++ ++ if( rc==SQLITE_OK ){ ++ if( pShmNode->isReadonly ){ ++ pShmNode->isUnlocked = 1; ++ winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1); ++ return SQLITE_READONLY_CANTINIT; ++ }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){ ++ winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1); ++ return winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(), ++ "winLockSharedMemory", pShmNode->zFilename); ++ } ++ } ++ ++ if( rc==SQLITE_OK ){ ++ winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1); ++ } ++ ++ return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1); ++} ++ + /* + ** Open the shared-memory area associated with database file pDbFd. + ** +@@ -36789,9 +44495,9 @@ static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){ + */ + static int winOpenSharedMemory(winFile *pDbFd){ + struct winShm *p; /* The connection to be opened */ +- struct winShmNode *pShmNode = 0; /* The underlying mmapped file */ +- int rc; /* Result code */ +- struct winShmNode *pNew; /* Newly allocated winShmNode */ ++ winShmNode *pShmNode = 0; /* The underlying mmapped file */ ++ int rc = SQLITE_OK; /* Result code */ ++ winShmNode *pNew; /* Newly allocated winShmNode */ + int nName; /* Size of zName in bytes */ + + assert( pDbFd->pShm==0 ); /* Not previously opened */ +@@ -36800,12 +44506,12 @@ static int winOpenSharedMemory(winFile *pDbFd){ + ** allocate space for a new winShmNode and filename. + */ + p = sqlite3MallocZero( sizeof(*p) ); +- if( p==0 ) return SQLITE_IOERR_NOMEM; ++ if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT; + nName = sqlite3Strlen30(pDbFd->zPath); + pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 ); + if( pNew==0 ){ + sqlite3_free(p); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + pNew->zFilename = (char*)&pNew[1]; + sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath); +@@ -36824,42 +44530,40 @@ static int winOpenSharedMemory(winFile *pDbFd){ + if( pShmNode ){ + sqlite3_free(pNew); + }else{ ++ int inFlags = SQLITE_OPEN_WAL; ++ int outFlags = 0; ++ + pShmNode = pNew; + pNew = 0; + ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE; + pShmNode->pNext = winShmNodeList; + winShmNodeList = pShmNode; + +- pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); +- if( pShmNode->mutex==0 ){ +- rc = SQLITE_IOERR_NOMEM; +- goto shm_open_err; ++ if( sqlite3GlobalConfig.bCoreMutex ){ ++ pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); ++ if( pShmNode->mutex==0 ){ ++ rc = SQLITE_IOERR_NOMEM_BKPT; ++ goto shm_open_err; ++ } + } + +- rc = winOpen(pDbFd->pVfs, +- pShmNode->zFilename, /* Name of the file (UTF-8) */ +- (sqlite3_file*)&pShmNode->hFile, /* File handle here */ +- SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, +- 0); +- if( SQLITE_OK!=rc ){ ++ if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ ++ inFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; ++ }else{ ++ inFlags |= SQLITE_OPEN_READONLY; ++ } ++ rc = winOpen(pDbFd->pVfs, pShmNode->zFilename, ++ (sqlite3_file*)&pShmNode->hFile, ++ inFlags, &outFlags); ++ if( rc!=SQLITE_OK ){ ++ rc = winLogError(rc, osGetLastError(), "winOpenShm", ++ pShmNode->zFilename); + goto shm_open_err; + } ++ if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1; + +- /* Check to see if another process is holding the dead-man switch. +- ** If not, truncate the file to zero length. +- */ +- if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){ +- rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0); +- if( rc!=SQLITE_OK ){ +- rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(), +- "winOpenShm", pDbFd->zPath); +- } +- } +- if( rc==SQLITE_OK ){ +- winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1); +- rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1); +- } +- if( rc ) goto shm_open_err; ++ rc = winLockSharedMemory(pShmNode); ++ if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err; + } + + /* Make the new connection a child of the winShmNode */ +@@ -36882,11 +44586,11 @@ static int winOpenSharedMemory(winFile *pDbFd){ + p->pNext = pShmNode->pFirst; + pShmNode->pFirst = p; + sqlite3_mutex_leave(pShmNode->mutex); +- return SQLITE_OK; ++ return rc; + + /* Jump here on any error */ + shm_open_err: +- winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1); ++ winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1); + winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */ + sqlite3_free(p); + sqlite3_free(pNew); +@@ -36975,7 +44679,7 @@ static int winShmLock( + + /* Unlock the system-level locks */ + if( (mask & allMask)==0 ){ +- rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n); ++ rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n); + }else{ + rc = SQLITE_OK; + } +@@ -37003,7 +44707,7 @@ static int winShmLock( + /* Get shared locks at the system level, if necessary */ + if( rc==SQLITE_OK ){ + if( (allShared & mask)==0 ){ +- rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n); ++ rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n); + }else{ + rc = SQLITE_OK; + } +@@ -37028,7 +44732,7 @@ static int winShmLock( + ** also mark the local connection as being locked. + */ + if( rc==SQLITE_OK ){ +- rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n); ++ rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n); + if( rc==SQLITE_OK ){ + assert( (p->sharedMask & mask)==0 ); + p->exclMask |= mask; +@@ -37052,8 +44756,8 @@ static void winShmBarrier( + sqlite3_file *fd /* Database holding the shared memory */ + ){ + UNUSED_PARAMETER(fd); +- /* MemoryBarrier(); // does not work -- do not know why not */ +- winShmEnterMutex(); ++ sqlite3MemoryBarrier(); /* compiler-defined memory barrier */ ++ winShmEnterMutex(); /* Also mutex, for redundancy */ + winShmLeaveMutex(); + } + +@@ -37086,6 +44790,8 @@ static int winShmMap( + winFile *pDbFd = (winFile*)fd; + winShm *pShm = pDbFd->pShm; + winShmNode *pShmNode; ++ DWORD protect = PAGE_READWRITE; ++ DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ; + int rc = SQLITE_OK; + + if( !pShm ){ +@@ -37096,6 +44802,11 @@ static int winShmMap( + pShmNode = pShm->pShmNode; + + sqlite3_mutex_enter(pShmNode->mutex); ++ if( pShmNode->isUnlocked ){ ++ rc = winLockSharedMemory(pShmNode); ++ if( rc!=SQLITE_OK ) goto shmpage_out; ++ pShmNode->isUnlocked = 0; ++ } + assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); + + if( pShmNode->nRegion<=iRegion ){ +@@ -37137,26 +44848,31 @@ static int winShmMap( + pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0]) + ); + if( !apNew ){ +- rc = SQLITE_IOERR_NOMEM; ++ rc = SQLITE_IOERR_NOMEM_BKPT; + goto shmpage_out; + } + pShmNode->aRegion = apNew; + ++ if( pShmNode->isReadonly ){ ++ protect = PAGE_READONLY; ++ flags = FILE_MAP_READ; ++ } ++ + while( pShmNode->nRegion<=iRegion ){ + HANDLE hMap = NULL; /* file-mapping handle */ + void *pMap = 0; /* Mapped memory region */ + + #if SQLITE_OS_WINRT + hMap = osCreateFileMappingFromApp(pShmNode->hFile.h, +- NULL, PAGE_READWRITE, nByte, NULL ++ NULL, protect, nByte, NULL + ); + #elif defined(SQLITE_WIN32_HAS_WIDE) + hMap = osCreateFileMappingW(pShmNode->hFile.h, +- NULL, PAGE_READWRITE, 0, nByte, NULL ++ NULL, protect, 0, nByte, NULL + ); +-#elif defined(SQLITE_WIN32_HAS_ANSI) ++#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA + hMap = osCreateFileMappingA(pShmNode->hFile.h, +- NULL, PAGE_READWRITE, 0, nByte, NULL ++ NULL, protect, 0, nByte, NULL + ); + #endif + OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n", +@@ -37166,11 +44882,11 @@ static int winShmMap( + int iOffset = pShmNode->nRegion*szRegion; + int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity; + #if SQLITE_OS_WINRT +- pMap = osMapViewOfFileFromApp(hMap, FILE_MAP_WRITE | FILE_MAP_READ, ++ pMap = osMapViewOfFileFromApp(hMap, flags, + iOffset - iOffsetShift, szRegion + iOffsetShift + ); + #else +- pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ, ++ pMap = osMapViewOfFile(hMap, flags, + 0, iOffset - iOffsetShift, szRegion + iOffsetShift + ); + #endif +@@ -37201,6 +44917,7 @@ shmpage_out: + }else{ + *pp = 0; + } ++ if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; + sqlite3_mutex_leave(pShmNode->mutex); + return rc; + } +@@ -37219,9 +44936,9 @@ shmpage_out: + static int winUnmapfile(winFile *pFile){ + assert( pFile!=0 ); + OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, " +- "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n", ++ "mmapSize=%lld, mmapSizeMax=%lld\n", + osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion, +- pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax)); ++ pFile->mmapSize, pFile->mmapSizeMax)); + if( pFile->pMapRegion ){ + if( !osUnmapViewOfFile(pFile->pMapRegion) ){ + pFile->lastErrno = osGetLastError(); +@@ -37233,7 +44950,6 @@ static int winUnmapfile(winFile *pFile){ + } + pFile->pMapRegion = 0; + pFile->mmapSize = 0; +- pFile->mmapSizeActual = 0; + } + if( pFile->hMap!=NULL ){ + if( !osCloseHandle(pFile->hMap) ){ +@@ -37298,17 +45014,19 @@ static int winMapfile(winFile *pFd, sqlite3_int64 nByte){ + DWORD flags = FILE_MAP_READ; + + winUnmapfile(pFd); ++#ifdef SQLITE_MMAP_READWRITE + if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){ + protect = PAGE_READWRITE; + flags |= FILE_MAP_WRITE; + } ++#endif + #if SQLITE_OS_WINRT + pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL); + #elif defined(SQLITE_WIN32_HAS_WIDE) + pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect, + (DWORD)((nMap>>32) & 0xffffffff), + (DWORD)(nMap & 0xffffffff), NULL); +-#elif defined(SQLITE_WIN32_HAS_ANSI) ++#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA + pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect, + (DWORD)((nMap>>32) & 0xffffffff), + (DWORD)(nMap & 0xffffffff), NULL); +@@ -37342,7 +45060,6 @@ static int winMapfile(winFile *pFd, sqlite3_int64 nByte){ + } + pFd->pMapRegion = pNew; + pFd->mmapSize = nMap; +- pFd->mmapSizeActual = nMap; + } + + OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n", +@@ -37469,6 +45186,44 @@ static const sqlite3_io_methods winIoMethod = { + winUnfetch /* xUnfetch */ + }; + ++/* ++** This vector defines all the methods that can operate on an ++** sqlite3_file for win32 without performing any locking. ++*/ ++static const sqlite3_io_methods winIoNolockMethod = { ++ 3, /* iVersion */ ++ winClose, /* xClose */ ++ winRead, /* xRead */ ++ winWrite, /* xWrite */ ++ winTruncate, /* xTruncate */ ++ winSync, /* xSync */ ++ winFileSize, /* xFileSize */ ++ winNolockLock, /* xLock */ ++ winNolockUnlock, /* xUnlock */ ++ winNolockCheckReservedLock, /* xCheckReservedLock */ ++ winFileControl, /* xFileControl */ ++ winSectorSize, /* xSectorSize */ ++ winDeviceCharacteristics, /* xDeviceCharacteristics */ ++ winShmMap, /* xShmMap */ ++ winShmLock, /* xShmLock */ ++ winShmBarrier, /* xShmBarrier */ ++ winShmUnmap, /* xShmUnmap */ ++ winFetch, /* xFetch */ ++ winUnfetch /* xUnfetch */ ++}; ++ ++static winVfsAppData winAppData = { ++ &winIoMethod, /* pMethod */ ++ 0, /* pAppData */ ++ 0 /* bNoLock */ ++}; ++ ++static winVfsAppData winNolockAppData = { ++ &winIoNolockMethod, /* pMethod */ ++ 0, /* pAppData */ ++ 1 /* bNoLock */ ++}; ++ + /**************************************************************************** + **************************** sqlite3_vfs methods **************************** + ** +@@ -37489,7 +45244,7 @@ static char *winConvertToUtf8Filename(const void *zFilename){ + } + #ifdef SQLITE_WIN32_HAS_ANSI + else{ +- zConverted = sqlite3_win32_mbcs_to_utf8(zFilename); ++ zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI()); + } + #endif + /* caller will handle out of memory */ +@@ -37510,7 +45265,7 @@ static void *winConvertFromUtf8Filename(const char *zFilename){ + } + #ifdef SQLITE_WIN32_HAS_ANSI + else{ +- zConverted = sqlite3_win32_utf8_to_mbcs(zFilename); ++ zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI()); + } + #endif + /* caller will handle out of memory */ +@@ -37565,7 +45320,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){ + zBuf = sqlite3MallocZero( nBuf ); + if( !zBuf ){ + OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + + /* Figure out the effective temporary directory. First, check if one +@@ -37623,7 +45378,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){ + if( !zConverted ){ + sqlite3_free(zBuf); + OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + if( winIsDir(zConverted) ){ + sqlite3_snprintf(nMax, zBuf, "%s", zDir); +@@ -37636,7 +45391,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){ + if( !zConverted ){ + sqlite3_free(zBuf); + OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + if( cygwin_conv_path( + osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir, +@@ -37657,7 +45412,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){ + sqlite3_free(zConverted); + sqlite3_free(zBuf); + OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + sqlite3_snprintf(nMax, zBuf, "%s", zUtf8); + sqlite3_free(zUtf8); +@@ -37675,7 +45430,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){ + if( !zWidePath ){ + sqlite3_free(zBuf); + OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + if( osGetTempPathW(nMax, zWidePath)==0 ){ + sqlite3_free(zWidePath); +@@ -37693,7 +45448,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){ + sqlite3_free(zWidePath); + sqlite3_free(zBuf); + OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + } + #ifdef SQLITE_WIN32_HAS_ANSI +@@ -37703,7 +45458,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){ + if( !zMbcsPath ){ + sqlite3_free(zBuf); + OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + if( osGetTempPathA(nMax, zMbcsPath)==0 ){ + sqlite3_free(zBuf); +@@ -37711,14 +45466,14 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){ + return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(), + "winGetTempname3", 0); + } +- zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath); ++ zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI()); + if( zUtf8 ){ + sqlite3_snprintf(nMax, zBuf, "%s", zUtf8); + sqlite3_free(zUtf8); + }else{ + sqlite3_free(zBuf); + OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + } + #endif /* SQLITE_WIN32_HAS_ANSI */ +@@ -37797,11 +45552,19 @@ static int winIsDir(const void *zConverted){ + return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY); + } + ++/* forward reference */ ++static int winAccess( ++ sqlite3_vfs *pVfs, /* Not used on win32 */ ++ const char *zFilename, /* Name of file to check */ ++ int flags, /* Type of test to make on this file */ ++ int *pResOut /* OUT: Result */ ++); ++ + /* + ** Open a file. + */ + static int winOpen( +- sqlite3_vfs *pVfs, /* Used to get maximum path name length */ ++ sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */ + const char *zName, /* Name of the file (UTF-8) */ + sqlite3_file *id, /* Write the SQLite file handle here */ + int flags, /* Open mode flags */ +@@ -37816,6 +45579,7 @@ static int winOpen( + #if SQLITE_OS_WINCE + int isTemp = 0; + #endif ++ winVfsAppData *pAppData; + winFile *pFile = (winFile*)id; + void *zConverted; /* Filename in OS encoding */ + const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */ +@@ -37910,7 +45674,7 @@ static int winOpen( + if( zConverted==0 ){ + sqlite3_free(zTmpname); + OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name)); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + + if( winIsDir(zConverted) ){ +@@ -37972,37 +45736,58 @@ static int winOpen( + extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS; + extendedParameters.lpSecurityAttributes = NULL; + extendedParameters.hTemplateFile = NULL; +- while( (h = osCreateFile2((LPCWSTR)zConverted, +- dwDesiredAccess, +- dwShareMode, +- dwCreationDisposition, +- &extendedParameters))==INVALID_HANDLE_VALUE && +- winRetryIoerr(&cnt, &lastErrno) ){ +- /* Noop */ +- } ++ do{ ++ h = osCreateFile2((LPCWSTR)zConverted, ++ dwDesiredAccess, ++ dwShareMode, ++ dwCreationDisposition, ++ &extendedParameters); ++ if( h!=INVALID_HANDLE_VALUE ) break; ++ if( isReadWrite ){ ++ int rc2, isRO = 0; ++ sqlite3BeginBenignMalloc(); ++ rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO); ++ sqlite3EndBenignMalloc(); ++ if( rc2==SQLITE_OK && isRO ) break; ++ } ++ }while( winRetryIoerr(&cnt, &lastErrno) ); + #else +- while( (h = osCreateFileW((LPCWSTR)zConverted, +- dwDesiredAccess, +- dwShareMode, NULL, +- dwCreationDisposition, +- dwFlagsAndAttributes, +- NULL))==INVALID_HANDLE_VALUE && +- winRetryIoerr(&cnt, &lastErrno) ){ +- /* Noop */ +- } ++ do{ ++ h = osCreateFileW((LPCWSTR)zConverted, ++ dwDesiredAccess, ++ dwShareMode, NULL, ++ dwCreationDisposition, ++ dwFlagsAndAttributes, ++ NULL); ++ if( h!=INVALID_HANDLE_VALUE ) break; ++ if( isReadWrite ){ ++ int rc2, isRO = 0; ++ sqlite3BeginBenignMalloc(); ++ rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO); ++ sqlite3EndBenignMalloc(); ++ if( rc2==SQLITE_OK && isRO ) break; ++ } ++ }while( winRetryIoerr(&cnt, &lastErrno) ); + #endif + } + #ifdef SQLITE_WIN32_HAS_ANSI + else{ +- while( (h = osCreateFileA((LPCSTR)zConverted, +- dwDesiredAccess, +- dwShareMode, NULL, +- dwCreationDisposition, +- dwFlagsAndAttributes, +- NULL))==INVALID_HANDLE_VALUE && +- winRetryIoerr(&cnt, &lastErrno) ){ +- /* Noop */ +- } ++ do{ ++ h = osCreateFileA((LPCSTR)zConverted, ++ dwDesiredAccess, ++ dwShareMode, NULL, ++ dwCreationDisposition, ++ dwFlagsAndAttributes, ++ NULL); ++ if( h!=INVALID_HANDLE_VALUE ) break; ++ if( isReadWrite ){ ++ int rc2, isRO = 0; ++ sqlite3BeginBenignMalloc(); ++ rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO); ++ sqlite3EndBenignMalloc(); ++ if( rc2==SQLITE_OK && isRO ) break; ++ } ++ }while( winRetryIoerr(&cnt, &lastErrno) ); + } + #endif + winLogIoerr(cnt, __LINE__); +@@ -38011,8 +45796,6 @@ static int winOpen( + dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok")); + + if( h==INVALID_HANDLE_VALUE ){ +- pFile->lastErrno = lastErrno; +- winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name); + sqlite3_free(zConverted); + sqlite3_free(zTmpname); + if( isReadWrite && !isExclusive ){ +@@ -38021,6 +45804,8 @@ static int winOpen( + ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), + pOutFlags); + }else{ ++ pFile->lastErrno = lastErrno; ++ winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name); + return SQLITE_CANTOPEN_BKPT; + } + } +@@ -38037,15 +45822,20 @@ static int winOpen( + "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ? + *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok")); + ++ pAppData = (winVfsAppData*)pVfs->pAppData; ++ + #if SQLITE_OS_WINCE +- if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB +- && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK +- ){ +- osCloseHandle(h); +- sqlite3_free(zConverted); +- sqlite3_free(zTmpname); +- OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc))); +- return rc; ++ { ++ if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB ++ && ((pAppData==NULL) || !pAppData->bNoLock) ++ && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK ++ ){ ++ osCloseHandle(h); ++ sqlite3_free(zConverted); ++ sqlite3_free(zTmpname); ++ OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc))); ++ return rc; ++ } + } + if( isTemp ){ + pFile->zDeleteOnClose = zConverted; +@@ -38056,7 +45846,7 @@ static int winOpen( + } + + sqlite3_free(zTmpname); +- pFile->pMethod = &winIoMethod; ++ pFile->pMethod = pAppData ? pAppData->pMethod : &winIoMethod; + pFile->pVfs = pVfs; + pFile->h = h; + if( isReadonly ){ +@@ -38071,7 +45861,6 @@ static int winOpen( + pFile->hMap = NULL; + pFile->pMapRegion = 0; + pFile->mmapSize = 0; +- pFile->mmapSizeActual = 0; + pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap; + #endif + +@@ -38110,7 +45899,7 @@ static int winDelete( + zConverted = winConvertFromUtf8Filename(zFilename); + if( zConverted==0 ){ + OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename)); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + if( osIsNT() ){ + do { +@@ -38218,7 +46007,7 @@ static int winAccess( + zConverted = winConvertFromUtf8Filename(zFilename); + if( zConverted==0 ){ + OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename)); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + if( osIsNT() ){ + int cnt = 0; +@@ -38331,6 +46120,18 @@ static int winFullPathname( + int nFull, /* Size of output buffer in bytes */ + char *zFull /* Output buffer */ + ){ ++#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__) ++ DWORD nByte; ++ void *zConverted; ++ char *zOut; ++#endif ++ ++ /* If this path name begins with "/X:", where "X" is any alphabetic ++ ** character, discard the initial "/" from the pathname. ++ */ ++ if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){ ++ zRelative++; ++ } + + #if defined(__CYGWIN__) + SimulateIOError( return SQLITE_ERROR ); +@@ -38345,7 +46146,7 @@ static int winFullPathname( + */ + char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 ); + if( !zOut ){ +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + if( cygwin_conv_path( + (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) | +@@ -38357,7 +46158,7 @@ static int winFullPathname( + char *zUtf8 = winConvertToUtf8Filename(zOut); + if( !zUtf8 ){ + sqlite3_free(zOut); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s", + sqlite3_data_directory, winGetDirSep(), zUtf8); +@@ -38367,7 +46168,7 @@ static int winFullPathname( + }else{ + char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 ); + if( !zOut ){ +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + if( cygwin_conv_path( + (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A), +@@ -38379,7 +46180,7 @@ static int winFullPathname( + char *zUtf8 = winConvertToUtf8Filename(zOut); + if( !zUtf8 ){ + sqlite3_free(zOut); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8); + sqlite3_free(zUtf8); +@@ -38409,17 +46210,6 @@ static int winFullPathname( + #endif + + #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__) +- DWORD nByte; +- void *zConverted; +- char *zOut; +- +- /* If this path name begins with "/X:", where "X" is any alphabetic +- ** character, discard the initial "/" from the pathname. +- */ +- if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){ +- zRelative++; +- } +- + /* It's odd to simulate an io-error here, but really this is just + ** using the io-error infrastructure to test that SQLite handles this + ** function failing. This function could fail if, for example, the +@@ -38439,7 +46229,7 @@ static int winFullPathname( + } + zConverted = winConvertFromUtf8Filename(zRelative); + if( zConverted==0 ){ +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + if( osIsNT() ){ + LPWSTR zTemp; +@@ -38453,7 +46243,7 @@ static int winFullPathname( + zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) ); + if( zTemp==0 ){ + sqlite3_free(zConverted); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0); + if( nByte==0 ){ +@@ -38479,7 +46269,7 @@ static int winFullPathname( + zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) ); + if( zTemp==0 ){ + sqlite3_free(zConverted); +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0); + if( nByte==0 ){ +@@ -38489,7 +46279,7 @@ static int winFullPathname( + "winFullPathname4", zRelative); + } + sqlite3_free(zConverted); +- zOut = sqlite3_win32_mbcs_to_utf8(zTemp); ++ zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI()); + sqlite3_free(zTemp); + } + #endif +@@ -38498,7 +46288,7 @@ static int winFullPathname( + sqlite3_free(zOut); + return SQLITE_OK; + }else{ +- return SQLITE_IOERR_NOMEM; ++ return SQLITE_IOERR_NOMEM_BKPT; + } + #endif + } +@@ -38573,65 +46363,82 @@ static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){ + #define winDlClose 0 + #endif + ++/* State information for the randomness gatherer. */ ++typedef struct EntropyGatherer EntropyGatherer; ++struct EntropyGatherer { ++ unsigned char *a; /* Gather entropy into this buffer */ ++ int na; /* Size of a[] in bytes */ ++ int i; /* XOR next input into a[i] */ ++ int nXor; /* Number of XOR operations done */ ++}; ++ ++#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) ++/* Mix sz bytes of entropy into p. */ ++static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){ ++ int j, k; ++ for(j=0, k=p->i; j a[k++] ^= x[j]; ++ if( k>=p->na ) k = 0; ++ } ++ p->i = k; ++ p->nXor += sz; ++} ++#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */ + + /* + ** Write up to nBuf bytes of randomness into zBuf. + */ + static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ +- int n = 0; +- UNUSED_PARAMETER(pVfs); + #if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) +- n = nBuf; ++ UNUSED_PARAMETER(pVfs); + memset(zBuf, 0, nBuf); ++ return nBuf; + #else +- if( sizeof(SYSTEMTIME)<=nBuf-n ){ ++ EntropyGatherer e; ++ UNUSED_PARAMETER(pVfs); ++ memset(zBuf, 0, nBuf); ++ e.a = (unsigned char*)zBuf; ++ e.na = nBuf; ++ e.nXor = 0; ++ e.i = 0; ++ { + SYSTEMTIME x; + osGetSystemTime(&x); +- memcpy(&zBuf[n], &x, sizeof(x)); +- n += sizeof(x); ++ xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME)); + } +- if( sizeof(DWORD)<=nBuf-n ){ ++ { + DWORD pid = osGetCurrentProcessId(); +- memcpy(&zBuf[n], &pid, sizeof(pid)); +- n += sizeof(pid); ++ xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD)); + } + #if SQLITE_OS_WINRT +- if( sizeof(ULONGLONG)<=nBuf-n ){ ++ { + ULONGLONG cnt = osGetTickCount64(); +- memcpy(&zBuf[n], &cnt, sizeof(cnt)); +- n += sizeof(cnt); ++ xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG)); + } + #else +- if( sizeof(DWORD)<=nBuf-n ){ ++ { + DWORD cnt = osGetTickCount(); +- memcpy(&zBuf[n], &cnt, sizeof(cnt)); +- n += sizeof(cnt); ++ xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD)); + } +-#endif +- if( sizeof(LARGE_INTEGER)<=nBuf-n ){ ++#endif /* SQLITE_OS_WINRT */ ++ { + LARGE_INTEGER i; + osQueryPerformanceCounter(&i); +- memcpy(&zBuf[n], &i, sizeof(i)); +- n += sizeof(i); ++ xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER)); + } + #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID +- if( sizeof(UUID)<=nBuf-n ){ ++ { + UUID id; + memset(&id, 0, sizeof(UUID)); + osUuidCreate(&id); +- memcpy(zBuf, &id, sizeof(UUID)); +- n += sizeof(UUID); +- } +- if( sizeof(UUID)<=nBuf-n ){ +- UUID id; ++ xorMemory(&e, (unsigned char*)&id, sizeof(UUID)); + memset(&id, 0, sizeof(UUID)); + osUuidCreateSequential(&id); +- memcpy(zBuf, &id, sizeof(UUID)); +- n += sizeof(UUID); ++ xorMemory(&e, (unsigned char*)&id, sizeof(UUID)); + } +-#endif +-#endif /* defined(SQLITE_TEST) || defined(SQLITE_ZERO_PRNG_SEED) */ +- return n; ++#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */ ++ return e.nXor>nBuf ? nBuf : e.nXor; ++#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */ + } + + +@@ -38747,62 +46554,114 @@ static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){ + ** sqlite3_errmsg(), possibly making IO errors easier to debug. + */ + static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ ++ DWORD e = osGetLastError(); + UNUSED_PARAMETER(pVfs); +- return winGetLastErrorMsg(osGetLastError(), nBuf, zBuf); ++ if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf); ++ return e; + } + + /* + ** Initialize and deinitialize the operating system interface. + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){ ++SQLITE_API int sqlite3_os_init(void){ + static sqlite3_vfs winVfs = { +- 3, /* iVersion */ +- sizeof(winFile), /* szOsFile */ ++ 3, /* iVersion */ ++ sizeof(winFile), /* szOsFile */ + SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */ +- 0, /* pNext */ +- "win32", /* zName */ +- 0, /* pAppData */ +- winOpen, /* xOpen */ +- winDelete, /* xDelete */ +- winAccess, /* xAccess */ +- winFullPathname, /* xFullPathname */ +- winDlOpen, /* xDlOpen */ +- winDlError, /* xDlError */ +- winDlSym, /* xDlSym */ +- winDlClose, /* xDlClose */ +- winRandomness, /* xRandomness */ +- winSleep, /* xSleep */ +- winCurrentTime, /* xCurrentTime */ +- winGetLastError, /* xGetLastError */ +- winCurrentTimeInt64, /* xCurrentTimeInt64 */ +- winSetSystemCall, /* xSetSystemCall */ +- winGetSystemCall, /* xGetSystemCall */ +- winNextSystemCall, /* xNextSystemCall */ ++ 0, /* pNext */ ++ "win32", /* zName */ ++ &winAppData, /* pAppData */ ++ winOpen, /* xOpen */ ++ winDelete, /* xDelete */ ++ winAccess, /* xAccess */ ++ winFullPathname, /* xFullPathname */ ++ winDlOpen, /* xDlOpen */ ++ winDlError, /* xDlError */ ++ winDlSym, /* xDlSym */ ++ winDlClose, /* xDlClose */ ++ winRandomness, /* xRandomness */ ++ winSleep, /* xSleep */ ++ winCurrentTime, /* xCurrentTime */ ++ winGetLastError, /* xGetLastError */ ++ winCurrentTimeInt64, /* xCurrentTimeInt64 */ ++ winSetSystemCall, /* xSetSystemCall */ ++ winGetSystemCall, /* xGetSystemCall */ ++ winNextSystemCall, /* xNextSystemCall */ + }; + #if defined(SQLITE_WIN32_HAS_WIDE) + static sqlite3_vfs winLongPathVfs = { +- 3, /* iVersion */ +- sizeof(winFile), /* szOsFile */ ++ 3, /* iVersion */ ++ sizeof(winFile), /* szOsFile */ + SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */ +- 0, /* pNext */ +- "win32-longpath", /* zName */ +- 0, /* pAppData */ +- winOpen, /* xOpen */ +- winDelete, /* xDelete */ +- winAccess, /* xAccess */ +- winFullPathname, /* xFullPathname */ +- winDlOpen, /* xDlOpen */ +- winDlError, /* xDlError */ +- winDlSym, /* xDlSym */ +- winDlClose, /* xDlClose */ +- winRandomness, /* xRandomness */ +- winSleep, /* xSleep */ +- winCurrentTime, /* xCurrentTime */ +- winGetLastError, /* xGetLastError */ +- winCurrentTimeInt64, /* xCurrentTimeInt64 */ +- winSetSystemCall, /* xSetSystemCall */ +- winGetSystemCall, /* xGetSystemCall */ +- winNextSystemCall, /* xNextSystemCall */ ++ 0, /* pNext */ ++ "win32-longpath", /* zName */ ++ &winAppData, /* pAppData */ ++ winOpen, /* xOpen */ ++ winDelete, /* xDelete */ ++ winAccess, /* xAccess */ ++ winFullPathname, /* xFullPathname */ ++ winDlOpen, /* xDlOpen */ ++ winDlError, /* xDlError */ ++ winDlSym, /* xDlSym */ ++ winDlClose, /* xDlClose */ ++ winRandomness, /* xRandomness */ ++ winSleep, /* xSleep */ ++ winCurrentTime, /* xCurrentTime */ ++ winGetLastError, /* xGetLastError */ ++ winCurrentTimeInt64, /* xCurrentTimeInt64 */ ++ winSetSystemCall, /* xSetSystemCall */ ++ winGetSystemCall, /* xGetSystemCall */ ++ winNextSystemCall, /* xNextSystemCall */ ++ }; ++#endif ++ static sqlite3_vfs winNolockVfs = { ++ 3, /* iVersion */ ++ sizeof(winFile), /* szOsFile */ ++ SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */ ++ 0, /* pNext */ ++ "win32-none", /* zName */ ++ &winNolockAppData, /* pAppData */ ++ winOpen, /* xOpen */ ++ winDelete, /* xDelete */ ++ winAccess, /* xAccess */ ++ winFullPathname, /* xFullPathname */ ++ winDlOpen, /* xDlOpen */ ++ winDlError, /* xDlError */ ++ winDlSym, /* xDlSym */ ++ winDlClose, /* xDlClose */ ++ winRandomness, /* xRandomness */ ++ winSleep, /* xSleep */ ++ winCurrentTime, /* xCurrentTime */ ++ winGetLastError, /* xGetLastError */ ++ winCurrentTimeInt64, /* xCurrentTimeInt64 */ ++ winSetSystemCall, /* xSetSystemCall */ ++ winGetSystemCall, /* xGetSystemCall */ ++ winNextSystemCall, /* xNextSystemCall */ ++ }; ++#if defined(SQLITE_WIN32_HAS_WIDE) ++ static sqlite3_vfs winLongPathNolockVfs = { ++ 3, /* iVersion */ ++ sizeof(winFile), /* szOsFile */ ++ SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */ ++ 0, /* pNext */ ++ "win32-longpath-none", /* zName */ ++ &winNolockAppData, /* pAppData */ ++ winOpen, /* xOpen */ ++ winDelete, /* xDelete */ ++ winAccess, /* xAccess */ ++ winFullPathname, /* xFullPathname */ ++ winDlOpen, /* xDlOpen */ ++ winDlError, /* xDlError */ ++ winDlSym, /* xDlSym */ ++ winDlClose, /* xDlClose */ ++ winRandomness, /* xRandomness */ ++ winSleep, /* xSleep */ ++ winCurrentTime, /* xCurrentTime */ ++ winGetLastError, /* xGetLastError */ ++ winCurrentTimeInt64, /* xCurrentTimeInt64 */ ++ winSetSystemCall, /* xSetSystemCall */ ++ winGetSystemCall, /* xGetSystemCall */ ++ winNextSystemCall, /* xNextSystemCall */ + }; + #endif + +@@ -38826,22 +46685,663 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){ + sqlite3_vfs_register(&winLongPathVfs, 0); + #endif + ++ sqlite3_vfs_register(&winNolockVfs, 0); ++ ++#if defined(SQLITE_WIN32_HAS_WIDE) ++ sqlite3_vfs_register(&winLongPathNolockVfs, 0); ++#endif ++ ++#ifndef SQLITE_OMIT_WAL ++ winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); ++#endif ++ + return SQLITE_OK; + } + +-SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){ ++SQLITE_API int sqlite3_os_end(void){ + #if SQLITE_OS_WINRT + if( sleepObj!=NULL ){ + osCloseHandle(sleepObj); + sleepObj = NULL; + } + #endif ++ ++#ifndef SQLITE_OMIT_WAL ++ winBigLock = 0; ++#endif ++ + return SQLITE_OK; + } + + #endif /* SQLITE_OS_WIN */ + + /************** End of os_win.c **********************************************/ ++/************** Begin file memdb.c *******************************************/ ++/* ++** 2016-09-07 ++** ++** The author disclaims copyright to this source code. In place of ++** a legal notice, here is a blessing: ++** ++** May you do good and not evil. ++** May you find forgiveness for yourself and forgive others. ++** May you share freely, never taking more than you give. ++** ++****************************************************************************** ++** ++** This file implements an in-memory VFS. A database is held as a contiguous ++** block of memory. ++** ++** This file also implements interface sqlite3_serialize() and ++** sqlite3_deserialize(). ++*/ ++/* #include "sqliteInt.h" */ ++#ifdef SQLITE_ENABLE_DESERIALIZE ++ ++/* ++** Forward declaration of objects used by this utility ++*/ ++typedef struct sqlite3_vfs MemVfs; ++typedef struct MemFile MemFile; ++ ++/* Access to a lower-level VFS that (might) implement dynamic loading, ++** access to randomness, etc. ++*/ ++#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData)) ++ ++/* An open file */ ++struct MemFile { ++ sqlite3_file base; /* IO methods */ ++ sqlite3_int64 sz; /* Size of the file */ ++ sqlite3_int64 szAlloc; /* Space allocated to aData */ ++ sqlite3_int64 szMax; /* Maximum allowed size of the file */ ++ unsigned char *aData; /* content of the file */ ++ int nMmap; /* Number of memory mapped pages */ ++ unsigned mFlags; /* Flags */ ++ int eLock; /* Most recent lock against this file */ ++}; ++ ++/* ++** Methods for MemFile ++*/ ++static int memdbClose(sqlite3_file*); ++static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); ++static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst); ++static int memdbTruncate(sqlite3_file*, sqlite3_int64 size); ++static int memdbSync(sqlite3_file*, int flags); ++static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize); ++static int memdbLock(sqlite3_file*, int); ++/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */ ++static int memdbFileControl(sqlite3_file*, int op, void *pArg); ++/* static int memdbSectorSize(sqlite3_file*); // not used */ ++static int memdbDeviceCharacteristics(sqlite3_file*); ++static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp); ++static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p); ++ ++/* ++** Methods for MemVfs ++*/ ++static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); ++/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */ ++static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *); ++static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); ++static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename); ++static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg); ++static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void); ++static void memdbDlClose(sqlite3_vfs*, void*); ++static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut); ++static int memdbSleep(sqlite3_vfs*, int microseconds); ++/* static int memdbCurrentTime(sqlite3_vfs*, double*); */ ++static int memdbGetLastError(sqlite3_vfs*, int, char *); ++static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); ++ ++static sqlite3_vfs memdb_vfs = { ++ 2, /* iVersion */ ++ 0, /* szOsFile (set when registered) */ ++ 1024, /* mxPathname */ ++ 0, /* pNext */ ++ "memdb", /* zName */ ++ 0, /* pAppData (set when registered) */ ++ memdbOpen, /* xOpen */ ++ 0, /* memdbDelete, */ /* xDelete */ ++ memdbAccess, /* xAccess */ ++ memdbFullPathname, /* xFullPathname */ ++ memdbDlOpen, /* xDlOpen */ ++ memdbDlError, /* xDlError */ ++ memdbDlSym, /* xDlSym */ ++ memdbDlClose, /* xDlClose */ ++ memdbRandomness, /* xRandomness */ ++ memdbSleep, /* xSleep */ ++ 0, /* memdbCurrentTime, */ /* xCurrentTime */ ++ memdbGetLastError, /* xGetLastError */ ++ memdbCurrentTimeInt64 /* xCurrentTimeInt64 */ ++}; ++ ++static const sqlite3_io_methods memdb_io_methods = { ++ 3, /* iVersion */ ++ memdbClose, /* xClose */ ++ memdbRead, /* xRead */ ++ memdbWrite, /* xWrite */ ++ memdbTruncate, /* xTruncate */ ++ memdbSync, /* xSync */ ++ memdbFileSize, /* xFileSize */ ++ memdbLock, /* xLock */ ++ memdbLock, /* xUnlock - same as xLock in this case */ ++ 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */ ++ memdbFileControl, /* xFileControl */ ++ 0, /* memdbSectorSize,*/ /* xSectorSize */ ++ memdbDeviceCharacteristics, /* xDeviceCharacteristics */ ++ 0, /* xShmMap */ ++ 0, /* xShmLock */ ++ 0, /* xShmBarrier */ ++ 0, /* xShmUnmap */ ++ memdbFetch, /* xFetch */ ++ memdbUnfetch /* xUnfetch */ ++}; ++ ++ ++ ++/* ++** Close an memdb-file. ++** ++** The pData pointer is owned by the application, so there is nothing ++** to free. ++*/ ++static int memdbClose(sqlite3_file *pFile){ ++ MemFile *p = (MemFile *)pFile; ++ if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ) sqlite3_free(p->aData); ++ return SQLITE_OK; ++} ++ ++/* ++** Read data from an memdb-file. ++*/ ++static int memdbRead( ++ sqlite3_file *pFile, ++ void *zBuf, ++ int iAmt, ++ sqlite_int64 iOfst ++){ ++ MemFile *p = (MemFile *)pFile; ++ if( iOfst+iAmt>p->sz ){ ++ memset(zBuf, 0, iAmt); ++ if( iOfst sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst); ++ return SQLITE_IOERR_SHORT_READ; ++ } ++ memcpy(zBuf, p->aData+iOfst, iAmt); ++ return SQLITE_OK; ++} ++ ++/* ++** Try to enlarge the memory allocation to hold at least sz bytes ++*/ ++static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){ ++ unsigned char *pNew; ++ if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){ ++ return SQLITE_FULL; ++ } ++ if( newSz>p->szMax ){ ++ return SQLITE_FULL; ++ } ++ newSz *= 2; ++ if( newSz>p->szMax ) newSz = p->szMax; ++ pNew = sqlite3_realloc64(p->aData, newSz); ++ if( pNew==0 ) return SQLITE_NOMEM; ++ p->aData = pNew; ++ p->szAlloc = newSz; ++ return SQLITE_OK; ++} ++ ++/* ++** Write data to an memdb-file. ++*/ ++static int memdbWrite( ++ sqlite3_file *pFile, ++ const void *z, ++ int iAmt, ++ sqlite_int64 iOfst ++){ ++ MemFile *p = (MemFile *)pFile; ++ if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ) return SQLITE_READONLY; ++ if( iOfst+iAmt>p->sz ){ ++ int rc; ++ if( iOfst+iAmt>p->szAlloc ++ && (rc = memdbEnlarge(p, iOfst+iAmt))!=SQLITE_OK ++ ){ ++ return rc; ++ } ++ if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz); ++ p->sz = iOfst+iAmt; ++ } ++ memcpy(p->aData+iOfst, z, iAmt); ++ return SQLITE_OK; ++} ++ ++/* ++** Truncate an memdb-file. ++** ++** In rollback mode (which is always the case for memdb, as it does not ++** support WAL mode) the truncate() method is only used to reduce ++** the size of a file, never to increase the size. ++*/ ++static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){ ++ MemFile *p = (MemFile *)pFile; ++ if( NEVER(size>p->sz) ) return SQLITE_FULL; ++ p->sz = size; ++ return SQLITE_OK; ++} ++ ++/* ++** Sync an memdb-file. ++*/ ++static int memdbSync(sqlite3_file *pFile, int flags){ ++ return SQLITE_OK; ++} ++ ++/* ++** Return the current file-size of an memdb-file. ++*/ ++static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ ++ MemFile *p = (MemFile *)pFile; ++ *pSize = p->sz; ++ return SQLITE_OK; ++} ++ ++/* ++** Lock an memdb-file. ++*/ ++static int memdbLock(sqlite3_file *pFile, int eLock){ ++ MemFile *p = (MemFile *)pFile; ++ if( eLock>SQLITE_LOCK_SHARED ++ && (p->mFlags & SQLITE_DESERIALIZE_READONLY)!=0 ++ ){ ++ return SQLITE_READONLY; ++ } ++ p->eLock = eLock; ++ return SQLITE_OK; ++} ++ ++#if 0 /* Never used because memdbAccess() always returns false */ ++/* ++** Check if another file-handle holds a RESERVED lock on an memdb-file. ++*/ ++static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){ ++ *pResOut = 0; ++ return SQLITE_OK; ++} ++#endif ++ ++/* ++** File control method. For custom operations on an memdb-file. ++*/ ++static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){ ++ MemFile *p = (MemFile *)pFile; ++ int rc = SQLITE_NOTFOUND; ++ if( op==SQLITE_FCNTL_VFSNAME ){ ++ *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz); ++ rc = SQLITE_OK; ++ } ++ if( op==SQLITE_FCNTL_SIZE_LIMIT ){ ++ sqlite3_int64 iLimit = *(sqlite3_int64*)pArg; ++ if( iLimit sz ){ ++ if( iLimit<0 ){ ++ iLimit = p->szMax; ++ }else{ ++ iLimit = p->sz; ++ } ++ } ++ p->szMax = iLimit; ++ *(sqlite3_int64*)pArg = iLimit; ++ rc = SQLITE_OK; ++ } ++ return rc; ++} ++ ++#if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */ ++/* ++** Return the sector-size in bytes for an memdb-file. ++*/ ++static int memdbSectorSize(sqlite3_file *pFile){ ++ return 1024; ++} ++#endif ++ ++/* ++** Return the device characteristic flags supported by an memdb-file. ++*/ ++static int memdbDeviceCharacteristics(sqlite3_file *pFile){ ++ return SQLITE_IOCAP_ATOMIC | ++ SQLITE_IOCAP_POWERSAFE_OVERWRITE | ++ SQLITE_IOCAP_SAFE_APPEND | ++ SQLITE_IOCAP_SEQUENTIAL; ++} ++ ++/* Fetch a page of a memory-mapped file */ ++static int memdbFetch( ++ sqlite3_file *pFile, ++ sqlite3_int64 iOfst, ++ int iAmt, ++ void **pp ++){ ++ MemFile *p = (MemFile *)pFile; ++ if( iOfst+iAmt>p->sz ){ ++ *pp = 0; ++ }else{ ++ p->nMmap++; ++ *pp = (void*)(p->aData + iOfst); ++ } ++ return SQLITE_OK; ++} ++ ++/* Release a memory-mapped page */ ++static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){ ++ MemFile *p = (MemFile *)pFile; ++ p->nMmap--; ++ return SQLITE_OK; ++} ++ ++/* ++** Open an mem file handle. ++*/ ++static int memdbOpen( ++ sqlite3_vfs *pVfs, ++ const char *zName, ++ sqlite3_file *pFile, ++ int flags, ++ int *pOutFlags ++){ ++ MemFile *p = (MemFile*)pFile; ++ if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){ ++ return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags); ++ } ++ memset(p, 0, sizeof(*p)); ++ p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE; ++ assert( pOutFlags!=0 ); /* True because flags==SQLITE_OPEN_MAIN_DB */ ++ *pOutFlags = flags | SQLITE_OPEN_MEMORY; ++ p->base.pMethods = &memdb_io_methods; ++ p->szMax = sqlite3GlobalConfig.mxMemdbSize; ++ return SQLITE_OK; ++} ++ ++#if 0 /* Only used to delete rollback journals, master journals, and WAL ++ ** files, none of which exist in memdb. So this routine is never used */ ++/* ++** Delete the file located at zPath. If the dirSync argument is true, ++** ensure the file-system modifications are synced to disk before ++** returning. ++*/ ++static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ ++ return SQLITE_IOERR_DELETE; ++} ++#endif ++ ++/* ++** Test for access permissions. Return true if the requested permission ++** is available, or false otherwise. ++** ++** With memdb, no files ever exist on disk. So always return false. ++*/ ++static int memdbAccess( ++ sqlite3_vfs *pVfs, ++ const char *zPath, ++ int flags, ++ int *pResOut ++){ ++ *pResOut = 0; ++ return SQLITE_OK; ++} ++ ++/* ++** Populate buffer zOut with the full canonical pathname corresponding ++** to the pathname in zPath. zOut is guaranteed to point to a buffer ++** of at least (INST_MAX_PATHNAME+1) bytes. ++*/ ++static int memdbFullPathname( ++ sqlite3_vfs *pVfs, ++ const char *zPath, ++ int nOut, ++ char *zOut ++){ ++ sqlite3_snprintf(nOut, zOut, "%s", zPath); ++ return SQLITE_OK; ++} ++ ++/* ++** Open the dynamic library located at zPath and return a handle. ++*/ ++static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){ ++ return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath); ++} ++ ++/* ++** Populate the buffer zErrMsg (size nByte bytes) with a human readable ++** utf-8 string describing the most recent error encountered associated ++** with dynamic libraries. ++*/ ++static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ ++ ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg); ++} ++ ++/* ++** Return a pointer to the symbol zSymbol in the dynamic library pHandle. ++*/ ++static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){ ++ return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym); ++} ++ ++/* ++** Close the dynamic library handle pHandle. ++*/ ++static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){ ++ ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle); ++} ++ ++/* ++** Populate the buffer pointed to by zBufOut with nByte bytes of ++** random data. ++*/ ++static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ ++ return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut); ++} ++ ++/* ++** Sleep for nMicro microseconds. Return the number of microseconds ++** actually slept. ++*/ ++static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){ ++ return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro); ++} ++ ++#if 0 /* Never used. Modern cores only call xCurrentTimeInt64() */ ++/* ++** Return the current time as a Julian Day number in *pTimeOut. ++*/ ++static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ ++ return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut); ++} ++#endif ++ ++static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){ ++ return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b); ++} ++static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){ ++ return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p); ++} ++ ++/* ++** Translate a database connection pointer and schema name into a ++** MemFile pointer. ++*/ ++static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){ ++ MemFile *p = 0; ++ int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p); ++ if( rc ) return 0; ++ if( p->base.pMethods!=&memdb_io_methods ) return 0; ++ return p; ++} ++ ++/* ++** Return the serialization of a database ++*/ ++SQLITE_API unsigned char *sqlite3_serialize( ++ sqlite3 *db, /* The database connection */ ++ const char *zSchema, /* Which database within the connection */ ++ sqlite3_int64 *piSize, /* Write size here, if not NULL */ ++ unsigned int mFlags /* Maybe SQLITE_SERIALIZE_NOCOPY */ ++){ ++ MemFile *p; ++ int iDb; ++ Btree *pBt; ++ sqlite3_int64 sz; ++ int szPage = 0; ++ sqlite3_stmt *pStmt = 0; ++ unsigned char *pOut; ++ char *zSql; ++ int rc; ++ ++#ifdef SQLITE_ENABLE_API_ARMOR ++ if( !sqlite3SafetyCheckOk(db) ){ ++ (void)SQLITE_MISUSE_BKPT; ++ return 0; ++ } ++#endif ++ ++ if( zSchema==0 ) zSchema = db->aDb[0].zDbSName; ++ p = memdbFromDbSchema(db, zSchema); ++ iDb = sqlite3FindDbName(db, zSchema); ++ if( piSize ) *piSize = -1; ++ if( iDb<0 ) return 0; ++ if( p ){ ++ if( piSize ) *piSize = p->sz; ++ if( mFlags & SQLITE_SERIALIZE_NOCOPY ){ ++ pOut = p->aData; ++ }else{ ++ pOut = sqlite3_malloc64( p->sz ); ++ if( pOut ) memcpy(pOut, p->aData, p->sz); ++ } ++ return pOut; ++ } ++ pBt = db->aDb[iDb].pBt; ++ if( pBt==0 ) return 0; ++ szPage = sqlite3BtreeGetPageSize(pBt); ++ zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema); ++ rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM; ++ sqlite3_free(zSql); ++ if( rc ) return 0; ++ rc = sqlite3_step(pStmt); ++ if( rc!=SQLITE_ROW ){ ++ pOut = 0; ++ }else{ ++ sz = sqlite3_column_int64(pStmt, 0)*szPage; ++ if( piSize ) *piSize = sz; ++ if( mFlags & SQLITE_SERIALIZE_NOCOPY ){ ++ pOut = 0; ++ }else{ ++ pOut = sqlite3_malloc64( sz ); ++ if( pOut ){ ++ int nPage = sqlite3_column_int(pStmt, 0); ++ Pager *pPager = sqlite3BtreePager(pBt); ++ int pgno; ++ for(pgno=1; pgno<=nPage; pgno++){ ++ DbPage *pPage = 0; ++ unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1); ++ rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0); ++ if( rc==SQLITE_OK ){ ++ memcpy(pTo, sqlite3PagerGetData(pPage), szPage); ++ }else{ ++ memset(pTo, 0, szPage); ++ } ++ sqlite3PagerUnref(pPage); ++ } ++ } ++ } ++ } ++ sqlite3_finalize(pStmt); ++ return pOut; ++} ++ ++/* Convert zSchema to a MemDB and initialize its content. ++*/ ++SQLITE_API int sqlite3_deserialize( ++ sqlite3 *db, /* The database connection */ ++ const char *zSchema, /* Which DB to reopen with the deserialization */ ++ unsigned char *pData, /* The serialized database content */ ++ sqlite3_int64 szDb, /* Number bytes in the deserialization */ ++ sqlite3_int64 szBuf, /* Total size of buffer pData[] */ ++ unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */ ++){ ++ MemFile *p; ++ char *zSql; ++ sqlite3_stmt *pStmt = 0; ++ int rc; ++ int iDb; ++ ++#ifdef SQLITE_ENABLE_API_ARMOR ++ if( !sqlite3SafetyCheckOk(db) ){ ++ return SQLITE_MISUSE_BKPT; ++ } ++ if( szDb<0 ) return SQLITE_MISUSE_BKPT; ++ if( szBuf<0 ) return SQLITE_MISUSE_BKPT; ++#endif ++ ++ sqlite3_mutex_enter(db->mutex); ++ if( zSchema==0 ) zSchema = db->aDb[0].zDbSName; ++ iDb = sqlite3FindDbName(db, zSchema); ++ if( iDb<0 ){ ++ rc = SQLITE_ERROR; ++ goto end_deserialize; ++ } ++ zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema); ++ rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); ++ sqlite3_free(zSql); ++ if( rc ) goto end_deserialize; ++ db->init.iDb = (u8)iDb; ++ db->init.reopenMemdb = 1; ++ rc = sqlite3_step(pStmt); ++ db->init.reopenMemdb = 0; ++ if( rc!=SQLITE_DONE ){ ++ rc = SQLITE_ERROR; ++ goto end_deserialize; ++ } ++ p = memdbFromDbSchema(db, zSchema); ++ if( p==0 ){ ++ rc = SQLITE_ERROR; ++ }else{ ++ p->aData = pData; ++ p->sz = szDb; ++ p->szAlloc = szBuf; ++ p->szMax = szBuf; ++ if( p->szMax szMax = sqlite3GlobalConfig.mxMemdbSize; ++ } ++ p->mFlags = mFlags; ++ rc = SQLITE_OK; ++ } ++ ++end_deserialize: ++ sqlite3_finalize(pStmt); ++ sqlite3_mutex_leave(db->mutex); ++ return rc; ++} ++ ++/* ++** This routine is called when the extension is loaded. ++** Register the new VFS. ++*/ ++SQLITE_PRIVATE int sqlite3MemdbInit(void){ ++ sqlite3_vfs *pLower = sqlite3_vfs_find(0); ++ int sz = pLower->szOsFile; ++ memdb_vfs.pAppData = pLower; ++ /* In all known configurations of SQLite, the size of a default ++ ** sqlite3_file is greater than the size of a memdb sqlite3_file. ++ ** Should that ever change, remove the following NEVER() */ ++ if( NEVER(sz p->iSize || i==0 ) return 0; ++SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){ ++ assert( p!=0 ); + i--; ++ if( i>=p->iSize ) return 0; + while( p->iDivisor ){ + u32 bin = i/p->iDivisor; + i = i%p->iDivisor; +@@ -38993,6 +47495,9 @@ SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){ + return 0; + } + } ++SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){ ++ return p!=0 && sqlite3BitvecTestNotNull(p,i); ++} + + /* + ** Set the i-th bit. Return 0 on success and an error code if +@@ -39017,7 +47522,7 @@ SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){ + i = i%p->iDivisor; + if( p->u.apSub[bin]==0 ){ + p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor ); +- if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM; ++ if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT; + } + p = p->u.apSub[bin]; + } +@@ -39052,7 +47557,7 @@ bitvec_set_rehash: + int rc; + u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash)); + if( aiValues==0 ){ +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + }else{ + memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash)); + memset(p->u.apSub, 0, sizeof(p->u.apSub)); +@@ -39133,7 +47638,7 @@ SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){ + return p->iSize; + } + +-#ifndef SQLITE_OMIT_BUILTIN_TEST ++#ifndef SQLITE_UNTESTABLE + /* + ** Let V[] be an array of unsigned characters sufficient to hold + ** up to N bits. Let I be an integer between 0 and N. 0<=I pDirtyNext. ++** PCache.pDirty points to the first (newest) element in the list and ++** pDirtyTail to the last (oldest). ++** ++** The PCache.pSynced variable is used to optimize searching for a dirty ++** page to eject from the cache mid-transaction. It is better to eject ++** a page that does not require a journal sync than one that does. ++** Therefore, pSynced is maintained so that it *almost* always points ++** to either the oldest page in the pDirty/pDirtyTail list that has a ++** clear PGHDR_NEED_SYNC flag or to a page that is older than this one ++** (so that the right page to eject can be found by following pDirtyPrev ++** pointers). + */ + struct PCache { + PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */ + PgHdr *pSynced; /* Last synced page in dirty page list */ +- int nRef; /* Number of referenced pages */ ++ int nRefSum; /* Sum of ref counts over all pages */ + int szCache; /* Configured cache size */ ++ int szSpill; /* Size before spilling occurs */ + int szPage; /* Size of every page in this cache */ + int szExtra; /* Size of extra space for each page */ + u8 bPurgeable; /* True if pages are on backing store */ +@@ -39281,9 +47810,97 @@ struct PCache { + int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */ + void *pStress; /* Argument to xStress */ + sqlite3_pcache *pCache; /* Pluggable cache module */ +- PgHdr *pPage1; /* Reference to page 1 */ + }; + ++/********************************** Test and Debug Logic **********************/ ++/* ++** Debug tracing macros. Enable by by changing the "0" to "1" and ++** recompiling. ++** ++** When sqlite3PcacheTrace is 1, single line trace messages are issued. ++** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries ++** is displayed for many operations, resulting in a lot of output. ++*/ ++#if defined(SQLITE_DEBUG) && 0 ++ int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */ ++ int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */ ++# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;} ++ void pcacheDump(PCache *pCache){ ++ int N; ++ int i, j; ++ sqlite3_pcache_page *pLower; ++ PgHdr *pPg; ++ unsigned char *a; ++ ++ if( sqlite3PcacheTrace<2 ) return; ++ if( pCache->pCache==0 ) return; ++ N = sqlite3PcachePagecount(pCache); ++ if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump; ++ for(i=1; i<=N; i++){ ++ pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0); ++ if( pLower==0 ) continue; ++ pPg = (PgHdr*)pLower->pExtra; ++ printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags); ++ a = (unsigned char *)pLower->pBuf; ++ for(j=0; j<12; j++) printf("%02x", a[j]); ++ printf("\n"); ++ if( pPg->pPage==0 ){ ++ sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0); ++ } ++ } ++ } ++ #else ++# define pcacheTrace(X) ++# define pcacheDump(X) ++#endif ++ ++/* ++** Check invariants on a PgHdr entry. Return true if everything is OK. ++** Return false if any invariant is violated. ++** ++** This routine is for use inside of assert() statements only. For ++** example: ++** ++** assert( sqlite3PcachePageSanity(pPg) ); ++*/ ++#ifdef SQLITE_DEBUG ++SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){ ++ PCache *pCache; ++ assert( pPg!=0 ); ++ assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */ ++ pCache = pPg->pCache; ++ assert( pCache!=0 ); /* Every page has an associated PCache */ ++ if( pPg->flags & PGHDR_CLEAN ){ ++ assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */ ++ assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */ ++ assert( pCache->pDirtyTail!=pPg ); ++ } ++ /* WRITEABLE pages must also be DIRTY */ ++ if( pPg->flags & PGHDR_WRITEABLE ){ ++ assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */ ++ } ++ /* NEED_SYNC can be set independently of WRITEABLE. This can happen, ++ ** for example, when using the sqlite3PagerDontWrite() optimization: ++ ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK. ++ ** (2) Page X moved to freelist, WRITEABLE is cleared ++ ** (3) Page X reused, WRITEABLE is set again ++ ** If NEED_SYNC had been cleared in step 2, then it would not be reset ++ ** in step 3, and page might be written into the database without first ++ ** syncing the rollback journal, which might cause corruption on a power ++ ** loss. ++ ** ++ ** Another example is when the database page size is smaller than the ++ ** disk sector size. When any page of a sector is journalled, all pages ++ ** in that sector are marked NEED_SYNC even if they are still CLEAN, just ++ ** in case they are later modified, since all pages in the same sector ++ ** must be journalled and synced before any of those pages can be safely ++ ** written. ++ */ ++ return 1; ++} ++#endif /* SQLITE_DEBUG */ ++ ++ + /********************************** Linked List Management ********************/ + + /* Allowed values for second argument to pcacheManageDirtyList() */ +@@ -39300,17 +47917,16 @@ struct PCache { + static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){ + PCache *p = pPage->pCache; + ++ pcacheTrace(("%p.DIRTYLIST.%s %d\n", p, ++ addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT", ++ pPage->pgno)); + if( addRemove & PCACHE_DIRTYLIST_REMOVE ){ + assert( pPage->pDirtyNext || pPage==p->pDirtyTail ); + assert( pPage->pDirtyPrev || pPage==p->pDirty ); + + /* Update the PCache1.pSynced variable if necessary. */ + if( p->pSynced==pPage ){ +- PgHdr *pSynced = pPage->pDirtyPrev; +- while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){ +- pSynced = pSynced->pDirtyPrev; +- } +- p->pSynced = pSynced; ++ p->pSynced = pPage->pDirtyPrev; + } + + if( pPage->pDirtyNext ){ +@@ -39322,19 +47938,21 @@ static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){ + if( pPage->pDirtyPrev ){ + pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext; + }else{ ++ /* If there are now no dirty pages in the cache, set eCreate to 2. ++ ** This is an optimization that allows sqlite3PcacheFetch() to skip ++ ** searching for a dirty page to eject from the cache when it might ++ ** otherwise have to. */ + assert( pPage==p->pDirty ); + p->pDirty = pPage->pDirtyNext; +- if( p->pDirty==0 && p->bPurgeable ){ +- assert( p->eCreate==1 ); ++ assert( p->bPurgeable || p->eCreate==2 ); ++ if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/ ++ assert( p->bPurgeable==0 || p->eCreate==1 ); + p->eCreate = 2; + } + } +- pPage->pDirtyNext = 0; +- pPage->pDirtyPrev = 0; + } + if( addRemove & PCACHE_DIRTYLIST_ADD ){ +- assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage ); +- ++ pPage->pDirtyPrev = 0; + pPage->pDirtyNext = p->pDirty; + if( pPage->pDirtyNext ){ + assert( pPage->pDirtyNext->pDirtyPrev==0 ); +@@ -39347,10 +47965,19 @@ static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){ + } + } + p->pDirty = pPage; +- if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){ ++ ++ /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set ++ ** pSynced to point to it. Checking the NEED_SYNC flag is an ++ ** optimization, as if pSynced points to a page with the NEED_SYNC ++ ** flag set sqlite3PcacheFetchStress() searches through all newer ++ ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */ ++ if( !p->pSynced ++ && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/ ++ ){ + p->pSynced = pPage; + } + } ++ pcacheDump(p); + } + + /* +@@ -39359,18 +47986,15 @@ static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){ + */ + static void pcacheUnpin(PgHdr *p){ + if( p->pCache->bPurgeable ){ +- if( p->pgno==1 ){ +- p->pCache->pPage1 = 0; +- } ++ pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno)); + sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0); ++ pcacheDump(p->pCache); + } + } + + /* +-** Compute the number of pages of cache requested. p->szCache is the ++** Compute the number of pages of cache requested. p->szCache is the + ** cache size requested by the "PRAGMA cache_size" statement. +-** +-** + */ + static int numberOfCachePages(PCache *p){ + if( p->szCache>=0 ){ +@@ -39416,6 +48040,12 @@ SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); } + ** has already been allocated and is passed in as the p pointer. + ** The caller discovers how much space needs to be allocated by + ** calling sqlite3PcacheSize(). ++** ++** szExtra is some extra space allocated for each page. The first ++** 8 bytes of the extra space will be zeroed as the page is allocated, ++** but remaining content will be uninitialized. Though it is opaque ++** to this module, the extra space really ends up being the MemPage ++** structure in the pager. + */ + SQLITE_PRIVATE int sqlite3PcacheOpen( + int szPage, /* Size of every page */ +@@ -39428,11 +48058,14 @@ SQLITE_PRIVATE int sqlite3PcacheOpen( + memset(p, 0, sizeof(PCache)); + p->szPage = 1; + p->szExtra = szExtra; ++ assert( szExtra>=8 ); /* First 8 bytes will be zeroed */ + p->bPurgeable = bPurgeable; + p->eCreate = 2; + p->xStress = xStress; + p->pStress = pStress; + p->szCache = 100; ++ p->szSpill = 1; ++ pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable)); + return sqlite3PcacheSetPageSize(p, szPage); + } + +@@ -39441,21 +48074,21 @@ SQLITE_PRIVATE int sqlite3PcacheOpen( + ** are no outstanding page references when this function is called. + */ + SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){ +- assert( pCache->nRef==0 && pCache->pDirty==0 ); ++ assert( pCache->nRefSum==0 && pCache->pDirty==0 ); + if( pCache->szPage ){ + sqlite3_pcache *pNew; + pNew = sqlite3GlobalConfig.pcache2.xCreate( + szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)), + pCache->bPurgeable + ); +- if( pNew==0 ) return SQLITE_NOMEM; ++ if( pNew==0 ) return SQLITE_NOMEM_BKPT; + sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache)); + if( pCache->pCache ){ + sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); + } + pCache->pCache = pNew; +- pCache->pPage1 = 0; + pCache->szPage = szPage; ++ pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage)); + } + return SQLITE_OK; + } +@@ -39490,11 +48123,12 @@ SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch( + int createFlag /* If true, create page if it does not exist already */ + ){ + int eCreate; ++ sqlite3_pcache_page *pRes; + + assert( pCache!=0 ); + assert( pCache->pCache!=0 ); + assert( createFlag==3 || createFlag==0 ); +- assert( pgno>0 ); ++ assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) ); + + /* eCreate defines what to do if the page does not exist. + ** 0 Do not allocate a new page. (createFlag==0) +@@ -39507,12 +48141,15 @@ SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch( + assert( eCreate==0 || eCreate==1 || eCreate==2 ); + assert( createFlag==0 || pCache->eCreate==eCreate ); + assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) ); +- return sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate); ++ pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate); ++ pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno, ++ createFlag?" create":"",pRes)); ++ return pRes; + } + + /* + ** If the sqlite3PcacheFetch() routine is unable to allocate a new +-** page because new clean pages are available for reuse and the cache ++** page because no clean pages are available for reuse and the cache + ** size limit has been reached, then this routine can be invoked to + ** try harder to allocate a page. This routine might invoke the stress + ** callback to spill dirty pages to the journal. It will then try to +@@ -39529,36 +48166,43 @@ SQLITE_PRIVATE int sqlite3PcacheFetchStress( + PgHdr *pPg; + if( pCache->eCreate==2 ) return 0; + +- +- /* Find a dirty page to write-out and recycle. First try to find a +- ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC +- ** cleared), but if that is not possible settle for any other +- ** unreferenced dirty page. +- */ +- for(pPg=pCache->pSynced; +- pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); +- pPg=pPg->pDirtyPrev +- ); +- pCache->pSynced = pPg; +- if( !pPg ){ +- for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev); +- } +- if( pPg ){ +- int rc; ++ if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){ ++ /* Find a dirty page to write-out and recycle. First try to find a ++ ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC ++ ** cleared), but if that is not possible settle for any other ++ ** unreferenced dirty page. ++ ** ++ ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC ++ ** flag is currently referenced, then the following may leave pSynced ++ ** set incorrectly (pointing to other than the LRU page with NEED_SYNC ++ ** cleared). This is Ok, as pSynced is just an optimization. */ ++ for(pPg=pCache->pSynced; ++ pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); ++ pPg=pPg->pDirtyPrev ++ ); ++ pCache->pSynced = pPg; ++ if( !pPg ){ ++ for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev); ++ } ++ if( pPg ){ ++ int rc; + #ifdef SQLITE_LOG_CACHE_SPILL +- sqlite3_log(SQLITE_FULL, +- "spill page %d making room for %d - cache used: %d/%d", +- pPg->pgno, pgno, +- sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache), ++ sqlite3_log(SQLITE_FULL, ++ "spill page %d making room for %d - cache used: %d/%d", ++ pPg->pgno, pgno, ++ sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache), + numberOfCachePages(pCache)); + #endif +- rc = pCache->xStress(pCache->pStress, pPg); +- if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ +- return rc; ++ pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno)); ++ rc = pCache->xStress(pCache->pStress, pPg); ++ pcacheDump(pCache); ++ if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ ++ return rc; ++ } + } + } + *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2); +- return *ppPage==0 ? SQLITE_NOMEM : SQLITE_OK; ++ return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK; + } + + /* +@@ -39579,13 +48223,14 @@ static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit( + assert( pPage!=0 ); + pPgHdr = (PgHdr*)pPage->pExtra; + assert( pPgHdr->pPage==0 ); +- memset(pPgHdr, 0, sizeof(PgHdr)); ++ memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty)); + pPgHdr->pPage = pPage; + pPgHdr->pData = pPage->pBuf; + pPgHdr->pExtra = (void *)&pPgHdr[1]; +- memset(pPgHdr->pExtra, 0, pCache->szExtra); ++ memset(pPgHdr->pExtra, 0, 8); + pPgHdr->pCache = pCache; + pPgHdr->pgno = pgno; ++ pPgHdr->flags = PGHDR_CLEAN; + return sqlite3PcacheFetchFinish(pCache,pgno,pPage); + } + +@@ -39602,19 +48247,15 @@ SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish( + ){ + PgHdr *pPgHdr; + +- if( pPage==0 ) return 0; ++ assert( pPage!=0 ); + pPgHdr = (PgHdr *)pPage->pExtra; + + if( !pPgHdr->pPage ){ + return pcacheFetchFinishWithInit(pCache, pgno, pPage); + } +- if( 0==pPgHdr->nRef ){ +- pCache->nRef++; +- } ++ pCache->nRefSum++; + pPgHdr->nRef++; +- if( pgno==1 ){ +- pCache->pPage1 = pPgHdr; +- } ++ assert( sqlite3PcachePageSanity(pPgHdr) ); + return pPgHdr; + } + +@@ -39624,13 +48265,11 @@ SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish( + */ + SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){ + assert( p->nRef>0 ); +- p->nRef--; +- if( p->nRef==0 ){ +- p->pCache->nRef--; +- if( (p->flags&PGHDR_DIRTY)==0 ){ ++ p->pCache->nRefSum--; ++ if( (--p->nRef)==0 ){ ++ if( p->flags&PGHDR_CLEAN ){ + pcacheUnpin(p); +- }else if( p->pDirtyPrev!=0 ){ +- /* Move the page to the head of the dirty list. */ ++ }else{ + pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); + } + } +@@ -39641,7 +48280,9 @@ SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){ + */ + SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){ + assert(p->nRef>0); ++ assert( sqlite3PcachePageSanity(p) ); + p->nRef++; ++ p->pCache->nRefSum++; + } + + /* +@@ -39651,13 +48292,11 @@ SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){ + */ + SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){ + assert( p->nRef==1 ); ++ assert( sqlite3PcachePageSanity(p) ); + if( p->flags&PGHDR_DIRTY ){ + pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); + } +- p->pCache->nRef--; +- if( p->pgno==1 ){ +- p->pCache->pPage1 = 0; +- } ++ p->pCache->nRefSum--; + sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1); + } + +@@ -39666,11 +48305,17 @@ SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){ + ** make it so. + */ + SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){ +- p->flags &= ~PGHDR_DONT_WRITE; + assert( p->nRef>0 ); +- if( 0==(p->flags & PGHDR_DIRTY) ){ +- p->flags |= PGHDR_DIRTY; +- pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD); ++ assert( sqlite3PcachePageSanity(p) ); ++ if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/ ++ p->flags &= ~PGHDR_DONT_WRITE; ++ if( p->flags & PGHDR_CLEAN ){ ++ p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN); ++ pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno)); ++ assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY ); ++ pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD); ++ } ++ assert( sqlite3PcachePageSanity(p) ); + } + } + +@@ -39679,12 +48324,16 @@ SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){ + ** make it so. + */ + SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){ +- if( (p->flags & PGHDR_DIRTY) ){ +- pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); +- p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC); +- if( p->nRef==0 ){ +- pcacheUnpin(p); +- } ++ assert( sqlite3PcachePageSanity(p) ); ++ assert( (p->flags & PGHDR_DIRTY)!=0 ); ++ assert( (p->flags & PGHDR_CLEAN)==0 ); ++ pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); ++ p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE); ++ p->flags |= PGHDR_CLEAN; ++ pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno)); ++ assert( sqlite3PcachePageSanity(p) ); ++ if( p->nRef==0 ){ ++ pcacheUnpin(p); + } + } + +@@ -39693,11 +48342,24 @@ SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){ + */ + SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){ + PgHdr *p; ++ pcacheTrace(("%p.CLEAN-ALL\n",pCache)); + while( (p = pCache->pDirty)!=0 ){ + sqlite3PcacheMakeClean(p); + } + } + ++/* ++** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages. ++*/ ++SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache *pCache){ ++ PgHdr *p; ++ pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache)); ++ for(p=pCache->pDirty; p; p=p->pDirtyNext){ ++ p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE); ++ } ++ pCache->pSynced = pCache->pDirtyTail; ++} ++ + /* + ** Clear the PGHDR_NEED_SYNC flag from all dirty pages. + */ +@@ -39716,6 +48378,8 @@ SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){ + PCache *pCache = p->pCache; + assert( p->nRef>0 ); + assert( newPgno>0 ); ++ assert( sqlite3PcachePageSanity(p) ); ++ pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno)); + sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno); + p->pgno = newPgno; + if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){ +@@ -39736,6 +48400,7 @@ SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){ + if( pCache->pCache ){ + PgHdr *p; + PgHdr *pNext; ++ pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno)); + for(p=pCache->pDirty; p; p=pNext){ + pNext = p->pDirtyNext; + /* This routine never gets call with a positive pgno except right +@@ -39743,14 +48408,19 @@ SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){ + ** it must be that pgno==0. + */ + assert( p->pgno>0 ); +- if( ALWAYS(p->pgno>pgno) ){ ++ if( p->pgno>pgno ){ + assert( p->flags&PGHDR_DIRTY ); + sqlite3PcacheMakeClean(p); + } + } +- if( pgno==0 && pCache->pPage1 ){ +- memset(pCache->pPage1->pData, 0, pCache->szPage); +- pgno = 1; ++ if( pgno==0 && pCache->nRefSum ){ ++ sqlite3_pcache_page *pPage1; ++ pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0); ++ if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because ++ ** pCache->nRefSum>0 */ ++ memset(pPage1->pBuf, 0, pCache->szPage); ++ pgno = 1; ++ } + } + sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1); + } +@@ -39761,6 +48431,7 @@ SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){ + */ + SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){ + assert( pCache->pCache!=0 ); ++ pcacheTrace(("%p.CLOSE\n",pCache)); + sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); + } + +@@ -39773,29 +48444,31 @@ SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){ + + /* + ** Merge two lists of pages connected by pDirty and in pgno order. +-** Do not both fixing the pDirtyPrev pointers. ++** Do not bother fixing the pDirtyPrev pointers. + */ + static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){ + PgHdr result, *pTail; + pTail = &result; +- while( pA && pB ){ ++ assert( pA!=0 && pB!=0 ); ++ for(;;){ + if( pA->pgno pgno ){ + pTail->pDirty = pA; + pTail = pA; + pA = pA->pDirty; ++ if( pA==0 ){ ++ pTail->pDirty = pB; ++ break; ++ } + }else{ + pTail->pDirty = pB; + pTail = pB; + pB = pB->pDirty; ++ if( pB==0 ){ ++ pTail->pDirty = pA; ++ break; ++ } + } + } +- if( pA ){ +- pTail->pDirty = pA; +- }else if( pB ){ +- pTail->pDirty = pB; +- }else{ +- pTail->pDirty = 0; +- } + return result.pDirty; + } + +@@ -39836,7 +48509,8 @@ static PgHdr *pcacheSortDirtyList(PgHdr *pIn){ + } + p = a[0]; + for(i=1; i nRef; ++ return pCache->nRefSum; + } + + /* +@@ -39893,6 +48570,25 @@ SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){ + numberOfCachePages(pCache)); + } + ++/* ++** Set the suggested cache-spill value. Make no changes if if the ++** argument is zero. Return the effective cache-spill size, which will ++** be the larger of the szSpill and szCache. ++*/ ++SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){ ++ int res; ++ assert( p->pCache!=0 ); ++ if( mxPage ){ ++ if( mxPage<0 ){ ++ mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra)); ++ } ++ p->szSpill = mxPage; ++ } ++ res = numberOfCachePages(p); ++ if( res szSpill ) res = p->szSpill; ++ return res; ++} ++ + /* + ** Free up as much memory as possible from the page cache. + */ +@@ -39907,6 +48603,26 @@ SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){ + */ + SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); } + ++/* ++** Return the number of dirty pages currently in the cache, as a percentage ++** of the configured cache size. ++*/ ++SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){ ++ PgHdr *pDirty; ++ int nDirty = 0; ++ int nCache = numberOfCachePages(pCache); ++ for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++; ++ return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0; ++} ++ ++#ifdef SQLITE_DIRECT_OVERFLOW_READ ++/* ++** Return true if there are one or more dirty pages in the cache. Else false. ++*/ ++SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache){ ++ return (pCache->pDirty!=0); ++} ++#endif + + #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) + /* +@@ -39941,14 +48657,113 @@ SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHd + ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features. + ** If the default page cache implementation is overridden, then neither of + ** these two features are available. ++** ++** A Page cache line looks like this: ++** ++** ------------------------------------------------------------- ++** | database page content | PgHdr1 | MemPage | PgHdr | ++** ------------------------------------------------------------- ++** ++** The database page content is up front (so that buffer overreads tend to ++** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage ++** is the extension added by the btree.c module containing information such ++** as the database page number and how that database page is used. PgHdr ++** is added by the pcache.c layer and contains information used to keep track ++** of which pages are "dirty". PgHdr1 is an extension added by this ++** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page. ++** PgHdr1 contains information needed to look up a page by its page number. ++** The superclass sqlite3_pcache_page.pBuf points to the start of the ++** database page content and sqlite3_pcache_page.pExtra points to PgHdr. ++** ++** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at ++** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The ++** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this ++** size can vary according to architecture, compile-time options, and ++** SQLite library version number. ++** ++** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained ++** using a separate memory allocation from the database page content. This ++** seeks to overcome the "clownshoe" problem (also called "internal ++** fragmentation" in academic literature) of allocating a few bytes more ++** than a power of two with the memory allocator rounding up to the next ++** power of two, and leaving the rounded-up space unused. ++** ++** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates ++** with this module. Information is passed back and forth as PgHdr1 pointers. ++** ++** The pcache.c and pager.c modules deal pointers to PgHdr objects. ++** The btree.c module deals with pointers to MemPage objects. ++** ++** SOURCE OF PAGE CACHE MEMORY: ++** ++** Memory for a page might come from any of three sources: ++** ++** (1) The general-purpose memory allocator - sqlite3Malloc() ++** (2) Global page-cache memory provided using sqlite3_config() with ++** SQLITE_CONFIG_PAGECACHE. ++** (3) PCache-local bulk allocation. ++** ++** The third case is a chunk of heap memory (defaulting to 100 pages worth) ++** that is allocated when the page cache is created. The size of the local ++** bulk allocation can be adjusted using ++** ++** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N). ++** ++** If N is positive, then N pages worth of memory are allocated using a single ++** sqlite3Malloc() call and that memory is used for the first N pages allocated. ++** Or if N is negative, then -1024*N bytes of memory are allocated and used ++** for as many pages as can be accomodated. ++** ++** Only one of (2) or (3) can be used. Once the memory available to (2) or ++** (3) is exhausted, subsequent allocations fail over to the general-purpose ++** memory allocator (1). ++** ++** Earlier versions of SQLite used only methods (1) and (2). But experiments ++** show that method (3) with N==100 provides about a 5% performance boost for ++** common workloads. + */ +- ++/* #include "sqliteInt.h" */ + + typedef struct PCache1 PCache1; + typedef struct PgHdr1 PgHdr1; + typedef struct PgFreeslot PgFreeslot; + typedef struct PGroup PGroup; + ++/* ++** Each cache entry is represented by an instance of the following ++** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of ++** PgHdr1.pCache->szPage bytes is allocated directly before this structure ++** in memory. ++** ++** Note: Variables isBulkLocal and isAnchor were once type "u8". That works, ++** but causes a 2-byte gap in the structure for most architectures (since ++** pointers must be either 4 or 8-byte aligned). As this structure is located ++** in memory directly after the associated page data, if the database is ++** corrupt, code at the b-tree layer may overread the page buffer and ++** read part of this structure before the corruption is detected. This ++** can cause a valgrind error if the unitialized gap is accessed. Using u16 ++** ensures there is no such gap, and therefore no bytes of unitialized memory ++** in the structure. ++*/ ++struct PgHdr1 { ++ sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */ ++ unsigned int iKey; /* Key value (page number) */ ++ u16 isBulkLocal; /* This page from bulk local storage */ ++ u16 isAnchor; /* This is the PGroup.lru element */ ++ PgHdr1 *pNext; /* Next in hash table chain */ ++ PCache1 *pCache; /* Cache that currently owns this page */ ++ PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */ ++ PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */ ++ /* NB: pLruPrev is only valid if pLruNext!=0 */ ++}; ++ ++/* ++** A page is pinned if it is not on the LRU list. To be "pinned" means ++** that the page is in active use and must not be deallocated. ++*/ ++#define PAGE_IS_PINNED(p) ((p)->pLruNext==0) ++#define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0) ++ + /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set + ** of one or more PCaches that are able to recycle each other's unpinned + ** pages when they are under memory pressure. A PGroup is an instance of +@@ -39976,8 +48791,8 @@ struct PGroup { + unsigned int nMaxPage; /* Sum of nMax for purgeable caches */ + unsigned int nMinPage; /* Sum of nMin for purgeable caches */ + unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */ +- unsigned int nCurrentPage; /* Number of purgeable pages allocated */ +- PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */ ++ unsigned int nPurgeable; /* Number of purgeable pages allocated */ ++ PgHdr1 lru; /* The beginning and end of the LRU list */ + }; + + /* Each page cache is an instance of the following object. Every +@@ -39990,18 +48805,22 @@ struct PGroup { + */ + struct PCache1 { + /* Cache configuration parameters. Page size (szPage) and the purgeable +- ** flag (bPurgeable) are set when the cache is created. nMax may be ++ ** flag (bPurgeable) and the pnPurgeable pointer are all set when the ++ ** cache is created and are never changed thereafter. nMax may be + ** modified at any time by a call to the pcache1Cachesize() method. + ** The PGroup mutex must be held when accessing nMax. + */ + PGroup *pGroup; /* PGroup this cache belongs to */ +- int szPage; /* Size of allocated pages in bytes */ +- int szExtra; /* Size of extra space in bytes */ ++ unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */ ++ int szPage; /* Size of database content section */ ++ int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */ ++ int szAlloc; /* Total size of one pcache line */ + int bPurgeable; /* True if cache is purgeable */ + unsigned int nMin; /* Minimum number of pages reserved */ + unsigned int nMax; /* Configured "cache_size" value */ + unsigned int n90pct; /* nMax*9/10 */ + unsigned int iMaxKey; /* Largest key seen since xTruncate() */ ++ unsigned int nPurgeableDummy; /* pnPurgeable points here when not used*/ + + /* Hash table of all pages. The following variables may only be accessed + ** when the accessor is holding the PGroup mutex. +@@ -40010,27 +48829,13 @@ struct PCache1 { + unsigned int nPage; /* Total number of pages in apHash */ + unsigned int nHash; /* Number of slots in apHash[] */ + PgHdr1 **apHash; /* Hash table for fast lookup by key */ ++ PgHdr1 *pFree; /* List of unused pcache-local pages */ ++ void *pBulk; /* Bulk memory used by pcache-local */ + }; + + /* +-** Each cache entry is represented by an instance of the following +-** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of +-** PgHdr1.pCache->szPage bytes is allocated directly before this structure +-** in memory. +-*/ +-struct PgHdr1 { +- sqlite3_pcache_page page; +- unsigned int iKey; /* Key value (page number) */ +- u8 isPinned; /* Page in use, not on the LRU list */ +- PgHdr1 *pNext; /* Next in hash table chain */ +- PCache1 *pCache; /* Cache that currently owns this page */ +- PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */ +- PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */ +-}; +- +-/* +-** Free slots in the allocator used to divide up the buffer provided using +-** the SQLITE_CONFIG_PAGECACHE mechanism. ++** Free slots in the allocator used to divide up the global page cache ++** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism. + */ + struct PgFreeslot { + PgFreeslot *pNext; /* Next free slot */ +@@ -40048,10 +48853,12 @@ static SQLITE_WSD struct PCacheGlobal { + ** The nFreeSlot and pFree values do require mutex protection. + */ + int isInit; /* True if initialized */ ++ int separateCache; /* Use a new PGroup for each PCache */ ++ int nInitPage; /* Initial bulk allocation size */ + int szSlot; /* Size of each free slot */ + int nSlot; /* The number of pcache slots */ + int nReserve; /* Try to keep nFreeSlot above this */ +- void *pStart, *pEnd; /* Bounds of pagecache malloc range */ ++ void *pStart, *pEnd; /* Bounds of global page cache memory */ + /* Above requires no mutex. Use mutex below for variable that follow. */ + sqlite3_mutex *mutex; /* Mutex for accessing the following: */ + PgFreeslot *pFree; /* Free page blocks */ +@@ -40073,12 +48880,20 @@ static SQLITE_WSD struct PCacheGlobal { + /* + ** Macros to enter and leave the PCache LRU mutex. + */ +-#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex) +-#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex) ++#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0 ++# define pcache1EnterMutex(X) assert((X)->mutex==0) ++# define pcache1LeaveMutex(X) assert((X)->mutex==0) ++# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0 ++#else ++# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex) ++# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex) ++# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1 ++#endif + + /******************************************************************************/ + /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/ + ++ + /* + ** This function is called during initialization if a static buffer is + ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE +@@ -40091,6 +48906,8 @@ static SQLITE_WSD struct PCacheGlobal { + SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){ + if( pcache1.isInit ){ + PgFreeslot *p; ++ if( pBuf==0 ) sz = n = 0; ++ if( n==0 ) sz = 0; + sz = ROUNDDOWN8(sz); + pcache1.szSlot = sz; + pcache1.nSlot = pcache1.nFreeSlot = n; +@@ -40108,6 +48925,44 @@ SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){ + } + } + ++/* ++** Try to initialize the pCache->pFree and pCache->pBulk fields. Return ++** true if pCache->pFree ends up containing one or more free pages. ++*/ ++static int pcache1InitBulk(PCache1 *pCache){ ++ i64 szBulk; ++ char *zBulk; ++ if( pcache1.nInitPage==0 ) return 0; ++ /* Do not bother with a bulk allocation if the cache size very small */ ++ if( pCache->nMax<3 ) return 0; ++ sqlite3BeginBenignMalloc(); ++ if( pcache1.nInitPage>0 ){ ++ szBulk = pCache->szAlloc * (i64)pcache1.nInitPage; ++ }else{ ++ szBulk = -1024 * (i64)pcache1.nInitPage; ++ } ++ if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){ ++ szBulk = pCache->szAlloc*(i64)pCache->nMax; ++ } ++ zBulk = pCache->pBulk = sqlite3Malloc( szBulk ); ++ sqlite3EndBenignMalloc(); ++ if( zBulk ){ ++ int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc; ++ do{ ++ PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage]; ++ pX->page.pBuf = zBulk; ++ pX->page.pExtra = &pX[1]; ++ pX->isBulkLocal = 1; ++ pX->isAnchor = 0; ++ pX->pNext = pCache->pFree; ++ pX->pLruPrev = 0; /* Initializing this saves a valgrind error */ ++ pCache->pFree = pX; ++ zBulk += pCache->szAlloc; ++ }while( --nBulk ); ++ } ++ return pCache->pFree!=0; ++} ++ + /* + ** Malloc function used within this file to allocate space from the buffer + ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no +@@ -40128,7 +48983,7 @@ static void *pcache1Alloc(int nByte){ + pcache1.nFreeSlot--; + pcache1.bUnderPressure = pcache1.nFreeSlot =0 ); +- sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); ++ sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte); + sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1); + } + sqlite3_mutex_leave(pcache1.mutex); +@@ -40142,7 +48997,7 @@ static void *pcache1Alloc(int nByte){ + if( p ){ + int sz = sqlite3MallocSize(p); + sqlite3_mutex_enter(pcache1.mutex); +- sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); ++ sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte); + sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz); + sqlite3_mutex_leave(pcache1.mutex); + } +@@ -40155,10 +49010,9 @@ static void *pcache1Alloc(int nByte){ + /* + ** Free an allocated buffer obtained from pcache1Alloc(). + */ +-static int pcache1Free(void *p){ +- int nFreed = 0; +- if( p==0 ) return 0; +- if( p>=pcache1.pStart && p pGroup->mutex) ); +- pcache1LeaveMutex(pCache->pGroup); ++ if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){ ++ p = pCache->pFree; ++ pCache->pFree = p->pNext; ++ p->pNext = 0; ++ }else{ ++#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT ++ /* The group mutex must be released before pcache1Alloc() is called. This ++ ** is because it might call sqlite3_release_memory(), which assumes that ++ ** this mutex is not held. */ ++ assert( pcache1.separateCache==0 ); ++ assert( pCache->pGroup==&pcache1.grp ); ++ pcache1LeaveMutex(pCache->pGroup); ++#endif ++ if( benignMalloc ){ sqlite3BeginBenignMalloc(); } + #ifdef SQLITE_PCACHE_SEPARATE_HEADER +- pPg = pcache1Alloc(pCache->szPage); +- p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra); +- if( !pPg || !p ){ +- pcache1Free(pPg); +- sqlite3_free(p); +- pPg = 0; +- } ++ pPg = pcache1Alloc(pCache->szPage); ++ p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra); ++ if( !pPg || !p ){ ++ pcache1Free(pPg); ++ sqlite3_free(p); ++ pPg = 0; ++ } + #else +- pPg = pcache1Alloc(ROUND8(sizeof(PgHdr1)) + pCache->szPage + pCache->szExtra); +- p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage]; ++ pPg = pcache1Alloc(pCache->szAlloc); ++ p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage]; + #endif +- pcache1EnterMutex(pCache->pGroup); +- +- if( pPg ){ ++ if( benignMalloc ){ sqlite3EndBenignMalloc(); } ++#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT ++ pcache1EnterMutex(pCache->pGroup); ++#endif ++ if( pPg==0 ) return 0; + p->page.pBuf = pPg; + p->page.pExtra = &p[1]; +- if( pCache->bPurgeable ){ +- pCache->pGroup->nCurrentPage++; +- } +- return p; ++ p->isBulkLocal = 0; ++ p->isAnchor = 0; + } +- return 0; ++ (*pCache->pnPurgeable)++; ++ return p; + } + + /* + ** Free a page object allocated by pcache1AllocPage(). +-** +-** The pointer is allowed to be NULL, which is prudent. But it turns out +-** that the current implementation happens to never call this routine +-** with a NULL pointer, so we mark the NULL test with ALWAYS(). + */ + static void pcache1FreePage(PgHdr1 *p){ +- if( ALWAYS(p) ){ +- PCache1 *pCache = p->pCache; +- assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) ); ++ PCache1 *pCache; ++ assert( p!=0 ); ++ pCache = p->pCache; ++ assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) ); ++ if( p->isBulkLocal ){ ++ p->pNext = pCache->pFree; ++ pCache->pFree = p; ++ }else{ + pcache1Free(p->page.pBuf); + #ifdef SQLITE_PCACHE_SEPARATE_HEADER + sqlite3_free(p); + #endif +- if( pCache->bPurgeable ){ +- pCache->pGroup->nCurrentPage--; +- } + } ++ (*pCache->pnPurgeable)--; + } + + /* +@@ -40265,6 +49131,7 @@ static void pcache1FreePage(PgHdr1 *p){ + ** exists, this function falls back to sqlite3Malloc(). + */ + SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){ ++ assert( sz<=65536+8 ); /* These allocations are never very large */ + return pcache1Alloc(sz); + } + +@@ -40350,41 +49217,32 @@ static void pcache1ResizeHash(PCache1 *p){ + ** + ** The PGroup mutex must be held when this function is called. + */ +-static void pcache1PinPage(PgHdr1 *pPage){ +- PCache1 *pCache; +- PGroup *pGroup; +- ++static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){ + assert( pPage!=0 ); +- assert( pPage->isPinned==0 ); +- pCache = pPage->pCache; +- pGroup = pCache->pGroup; +- assert( pPage->pLruNext || pPage==pGroup->pLruTail ); +- assert( pPage->pLruPrev || pPage==pGroup->pLruHead ); +- assert( sqlite3_mutex_held(pGroup->mutex) ); +- if( pPage->pLruPrev ){ +- pPage->pLruPrev->pLruNext = pPage->pLruNext; +- }else{ +- pGroup->pLruHead = pPage->pLruNext; +- } +- if( pPage->pLruNext ){ +- pPage->pLruNext->pLruPrev = pPage->pLruPrev; +- }else{ +- pGroup->pLruTail = pPage->pLruPrev; +- } ++ assert( PAGE_IS_UNPINNED(pPage) ); ++ assert( pPage->pLruNext ); ++ assert( pPage->pLruPrev ); ++ assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) ); ++ pPage->pLruPrev->pLruNext = pPage->pLruNext; ++ pPage->pLruNext->pLruPrev = pPage->pLruPrev; + pPage->pLruNext = 0; +- pPage->pLruPrev = 0; +- pPage->isPinned = 1; +- pCache->nRecyclable--; ++ /* pPage->pLruPrev = 0; ++ ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */ ++ assert( pPage->isAnchor==0 ); ++ assert( pPage->pCache->pGroup->lru.isAnchor==1 ); ++ pPage->pCache->nRecyclable--; ++ return pPage; + } + + + /* + ** Remove the page supplied as an argument from the hash table + ** (PCache1.apHash structure) that it is currently stored in. ++** Also free the page if freePage is true. + ** + ** The PGroup mutex must be held when this function is called. + */ +-static void pcache1RemoveFromHash(PgHdr1 *pPage){ ++static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){ + unsigned int h; + PCache1 *pCache = pPage->pCache; + PgHdr1 **pp; +@@ -40395,21 +49253,28 @@ static void pcache1RemoveFromHash(PgHdr1 *pPage){ + *pp = (*pp)->pNext; + + pCache->nPage--; ++ if( freeFlag ) pcache1FreePage(pPage); + } + + /* + ** If there are currently more than nMaxPage pages allocated, try + ** to recycle pages to reduce the number allocated to nMaxPage. + */ +-static void pcache1EnforceMaxPage(PGroup *pGroup){ ++static void pcache1EnforceMaxPage(PCache1 *pCache){ ++ PGroup *pGroup = pCache->pGroup; ++ PgHdr1 *p; + assert( sqlite3_mutex_held(pGroup->mutex) ); +- while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){ +- PgHdr1 *p = pGroup->pLruTail; ++ while( pGroup->nPurgeable>pGroup->nMaxPage ++ && (p=pGroup->lru.pLruPrev)->isAnchor==0 ++ ){ + assert( p->pCache->pGroup==pGroup ); +- assert( p->isPinned==0 ); ++ assert( PAGE_IS_UNPINNED(p) ); + pcache1PinPage(p); +- pcache1RemoveFromHash(p); +- pcache1FreePage(p); ++ pcache1RemoveFromHash(p, 1); ++ } ++ if( pCache->nPage==0 && pCache->pBulk ){ ++ sqlite3_free(pCache->pBulk); ++ pCache->pBulk = pCache->pFree = 0; + } + } + +@@ -40424,25 +49289,45 @@ static void pcache1TruncateUnsafe( + PCache1 *pCache, /* The cache to truncate */ + unsigned int iLimit /* Drop pages with this pgno or larger */ + ){ +- TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */ +- unsigned int h; ++ TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */ ++ unsigned int h, iStop; + assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); +- for(h=0; h nHash; h++){ +- PgHdr1 **pp = &pCache->apHash[h]; ++ assert( pCache->iMaxKey >= iLimit ); ++ assert( pCache->nHash > 0 ); ++ if( pCache->iMaxKey - iLimit < pCache->nHash ){ ++ /* If we are just shaving the last few pages off the end of the ++ ** cache, then there is no point in scanning the entire hash table. ++ ** Only scan those hash slots that might contain pages that need to ++ ** be removed. */ ++ h = iLimit % pCache->nHash; ++ iStop = pCache->iMaxKey % pCache->nHash; ++ TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */ ++ }else{ ++ /* This is the general case where many pages are being removed. ++ ** It is necessary to scan the entire hash table */ ++ h = pCache->nHash/2; ++ iStop = h - 1; ++ } ++ for(;;){ ++ PgHdr1 **pp; + PgHdr1 *pPage; ++ assert( h nHash ); ++ pp = &pCache->apHash[h]; + while( (pPage = *pp)!=0 ){ + if( pPage->iKey>=iLimit ){ + pCache->nPage--; + *pp = pPage->pNext; +- if( !pPage->isPinned ) pcache1PinPage(pPage); ++ if( PAGE_IS_UNPINNED(pPage) ) pcache1PinPage(pPage); + pcache1FreePage(pPage); + }else{ + pp = &pPage->pNext; +- TESTONLY( nPage++; ) ++ TESTONLY( if( nPage>=0 ) nPage++; ) + } + } ++ if( h==iStop ) break; ++ h = (h+1) % pCache->nHash; + } +- assert( pCache->nPage==nPage ); ++ assert( nPage<0 || pCache->nPage==(unsigned)nPage ); + } + + /******************************************************************************/ +@@ -40455,9 +49340,44 @@ static int pcache1Init(void *NotUsed){ + UNUSED_PARAMETER(NotUsed); + assert( pcache1.isInit==0 ); + memset(&pcache1, 0, sizeof(pcache1)); ++ ++ ++ /* ++ ** The pcache1.separateCache variable is true if each PCache has its own ++ ** private PGroup (mode-1). pcache1.separateCache is false if the single ++ ** PGroup in pcache1.grp is used for all page caches (mode-2). ++ ** ++ ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT ++ ** ++ ** * Use a unified cache in single-threaded applications that have ++ ** configured a start-time buffer for use as page-cache memory using ++ ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL ++ ** pBuf argument. ++ ** ++ ** * Otherwise use separate caches (mode-1) ++ */ ++#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) ++ pcache1.separateCache = 0; ++#elif SQLITE_THREADSAFE ++ pcache1.separateCache = sqlite3GlobalConfig.pPage==0 ++ || sqlite3GlobalConfig.bCoreMutex>0; ++#else ++ pcache1.separateCache = sqlite3GlobalConfig.pPage==0; ++#endif ++ ++#if SQLITE_THREADSAFE + if( sqlite3GlobalConfig.bCoreMutex ){ +- pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU); +- pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM); ++ pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU); ++ pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM); ++ } ++#endif ++ if( pcache1.separateCache ++ && sqlite3GlobalConfig.nPage!=0 ++ && sqlite3GlobalConfig.pPage==0 ++ ){ ++ pcache1.nInitPage = sqlite3GlobalConfig.nPage; ++ }else{ ++ pcache1.nInitPage = 0; + } + pcache1.grp.mxPinned = 10; + pcache1.isInit = 1; +@@ -40488,39 +49408,26 @@ static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){ + PGroup *pGroup; /* The group the new page cache will belong to */ + int sz; /* Bytes of memory required to allocate the new cache */ + +- /* +- ** The separateCache variable is true if each PCache has its own private +- ** PGroup. In other words, separateCache is true for mode (1) where no +- ** mutexing is required. +- ** +- ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT +- ** +- ** * Always use a unified cache in single-threaded applications +- ** +- ** * Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off) +- ** use separate caches (mode-1) +- */ +-#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0 +- const int separateCache = 0; +-#else +- int separateCache = sqlite3GlobalConfig.bCoreMutex>0; +-#endif +- + assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 ); + assert( szExtra < 300 ); + +- sz = sizeof(PCache1) + sizeof(PGroup)*separateCache; ++ sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache; + pCache = (PCache1 *)sqlite3MallocZero(sz); + if( pCache ){ +- if( separateCache ){ ++ if( pcache1.separateCache ){ + pGroup = (PGroup*)&pCache[1]; + pGroup->mxPinned = 10; + }else{ + pGroup = &pcache1.grp; + } ++ if( pGroup->lru.isAnchor==0 ){ ++ pGroup->lru.isAnchor = 1; ++ pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru; ++ } + pCache->pGroup = pGroup; + pCache->szPage = szPage; + pCache->szExtra = szExtra; ++ pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1)); + pCache->bPurgeable = (bPurgeable ? 1 : 0); + pcache1EnterMutex(pGroup); + pcache1ResizeHash(pCache); +@@ -40528,6 +49435,9 @@ static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){ + pCache->nMin = 10; + pGroup->nMinPage += pCache->nMin; + pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; ++ pCache->pnPurgeable = &pGroup->nPurgeable; ++ }else{ ++ pCache->pnPurgeable = &pCache->nPurgeableDummy; + } + pcache1LeaveMutex(pGroup); + if( pCache->nHash==0 ){ +@@ -40552,7 +49462,7 @@ static void pcache1Cachesize(sqlite3_pcache *p, int nMax){ + pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; + pCache->nMax = nMax; + pCache->n90pct = pCache->nMax*9/10; +- pcache1EnforceMaxPage(pGroup); ++ pcache1EnforceMaxPage(pCache); + pcache1LeaveMutex(pGroup); + } + } +@@ -40570,7 +49480,7 @@ static void pcache1Shrink(sqlite3_pcache *p){ + pcache1EnterMutex(pGroup); + savedMaxPage = pGroup->nMaxPage; + pGroup->nMaxPage = 0; +- pcache1EnforceMaxPage(pGroup); ++ pcache1EnforceMaxPage(pCache); + pGroup->nMaxPage = savedMaxPage; + pcache1LeaveMutex(pGroup); + } +@@ -40623,30 +49533,21 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( + assert( pCache->nHash>0 && pCache->apHash ); + + /* Step 4. Try to recycle a page. */ +- if( pCache->bPurgeable && pGroup->pLruTail && ( +- (pCache->nPage+1>=pCache->nMax) +- || pGroup->nCurrentPage>=pGroup->nMaxPage +- || pcache1UnderMemoryPressure(pCache) +- )){ ++ if( pCache->bPurgeable ++ && !pGroup->lru.pLruPrev->isAnchor ++ && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache)) ++ ){ + PCache1 *pOther; +- pPage = pGroup->pLruTail; +- assert( pPage->isPinned==0 ); +- pcache1RemoveFromHash(pPage); ++ pPage = pGroup->lru.pLruPrev; ++ assert( PAGE_IS_UNPINNED(pPage) ); ++ pcache1RemoveFromHash(pPage, 0); + pcache1PinPage(pPage); + pOther = pPage->pCache; +- +- /* We want to verify that szPage and szExtra are the same for pOther +- ** and pCache. Assert that we can verify this by comparing sums. */ +- assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 ); +- assert( pCache->szExtra<512 ); +- assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 ); +- assert( pOther->szExtra<512 ); +- +- if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){ ++ if( pOther->szAlloc != pCache->szAlloc ){ + pcache1FreePage(pPage); + pPage = 0; + }else{ +- pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable); ++ pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable); + } + } + +@@ -40654,9 +49555,7 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( + ** attempt to allocate a new one. + */ + if( !pPage ){ +- if( createFlag==1 ) sqlite3BeginBenignMalloc(); +- pPage = pcache1AllocPage(pCache); +- if( createFlag==1 ) sqlite3EndBenignMalloc(); ++ pPage = pcache1AllocPage(pCache, createFlag==1); + } + + if( pPage ){ +@@ -40665,9 +49564,9 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( + pPage->iKey = iKey; + pPage->pNext = pCache->apHash[h]; + pPage->pCache = pCache; +- pPage->pLruPrev = 0; + pPage->pLruNext = 0; +- pPage->isPinned = 1; ++ /* pPage->pLruPrev = 0; ++ ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */ + *(void **)pPage->page.pExtra = 0; + pCache->apHash[h] = pPage; + if( iKey>pCache->iMaxKey ){ +@@ -40730,8 +49629,13 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( + ** proceed to step 5. + ** + ** 5. Otherwise, allocate and return a new page buffer. ++** ++** There are two versions of this routine. pcache1FetchWithMutex() is ++** the general case. pcache1FetchNoMutex() is a faster implementation for ++** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper ++** invokes the appropriate routine. + */ +-static sqlite3_pcache_page *pcache1Fetch( ++static PgHdr1 *pcache1FetchNoMutex( + sqlite3_pcache *p, + unsigned int iKey, + int createFlag +@@ -40739,28 +49643,66 @@ static sqlite3_pcache_page *pcache1Fetch( + PCache1 *pCache = (PCache1 *)p; + PgHdr1 *pPage = 0; + +- assert( offsetof(PgHdr1,page)==0 ); +- assert( pCache->bPurgeable || createFlag!=1 ); +- assert( pCache->bPurgeable || pCache->nMin==0 ); +- assert( pCache->bPurgeable==0 || pCache->nMin==10 ); +- assert( pCache->nMin==0 || pCache->bPurgeable ); +- assert( pCache->nHash>0 ); +- pcache1EnterMutex(pCache->pGroup); +- + /* Step 1: Search the hash table for an existing entry. */ + pPage = pCache->apHash[iKey % pCache->nHash]; + while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; } + +- /* Step 2: Abort if no existing page is found and createFlag is 0 */ ++ /* Step 2: If the page was found in the hash table, then return it. ++ ** If the page was not in the hash table and createFlag is 0, abort. ++ ** Otherwise (page not in hash and createFlag!=0) continue with ++ ** subsequent steps to try to create the page. */ + if( pPage ){ +- if( !pPage->isPinned ) pcache1PinPage(pPage); ++ if( PAGE_IS_UNPINNED(pPage) ){ ++ return pcache1PinPage(pPage); ++ }else{ ++ return pPage; ++ } + }else if( createFlag ){ + /* Steps 3, 4, and 5 implemented by this subroutine */ +- pPage = pcache1FetchStage2(pCache, iKey, createFlag); ++ return pcache1FetchStage2(pCache, iKey, createFlag); ++ }else{ ++ return 0; + } ++} ++#if PCACHE1_MIGHT_USE_GROUP_MUTEX ++static PgHdr1 *pcache1FetchWithMutex( ++ sqlite3_pcache *p, ++ unsigned int iKey, ++ int createFlag ++){ ++ PCache1 *pCache = (PCache1 *)p; ++ PgHdr1 *pPage; ++ ++ pcache1EnterMutex(pCache->pGroup); ++ pPage = pcache1FetchNoMutex(p, iKey, createFlag); + assert( pPage==0 || pCache->iMaxKey>=iKey ); + pcache1LeaveMutex(pCache->pGroup); +- return (sqlite3_pcache_page*)pPage; ++ return pPage; ++} ++#endif ++static sqlite3_pcache_page *pcache1Fetch( ++ sqlite3_pcache *p, ++ unsigned int iKey, ++ int createFlag ++){ ++#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG) ++ PCache1 *pCache = (PCache1 *)p; ++#endif ++ ++ assert( offsetof(PgHdr1,page)==0 ); ++ assert( pCache->bPurgeable || createFlag!=1 ); ++ assert( pCache->bPurgeable || pCache->nMin==0 ); ++ assert( pCache->bPurgeable==0 || pCache->nMin==10 ); ++ assert( pCache->nMin==0 || pCache->bPurgeable ); ++ assert( pCache->nHash>0 ); ++#if PCACHE1_MIGHT_USE_GROUP_MUTEX ++ if( pCache->pGroup->mutex ){ ++ return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag); ++ }else ++#endif ++ { ++ return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag); ++ } + } + + +@@ -40784,25 +49726,18 @@ static void pcache1Unpin( + /* It is an error to call this function if the page is already + ** part of the PGroup LRU list. + */ +- assert( pPage->pLruPrev==0 && pPage->pLruNext==0 ); +- assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage ); +- assert( pPage->isPinned==1 ); ++ assert( pPage->pLruNext==0 ); ++ assert( PAGE_IS_PINNED(pPage) ); + +- if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){ +- pcache1RemoveFromHash(pPage); +- pcache1FreePage(pPage); ++ if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){ ++ pcache1RemoveFromHash(pPage, 1); + }else{ + /* Add the page to the PGroup LRU list. */ +- if( pGroup->pLruHead ){ +- pGroup->pLruHead->pLruPrev = pPage; +- pPage->pLruNext = pGroup->pLruHead; +- pGroup->pLruHead = pPage; +- }else{ +- pGroup->pLruTail = pPage; +- pGroup->pLruHead = pPage; +- } ++ PgHdr1 **ppFirst = &pGroup->lru.pLruNext; ++ pPage->pLruPrev = &pGroup->lru; ++ (pPage->pLruNext = *ppFirst)->pLruPrev = pPage; ++ *ppFirst = pPage; + pCache->nRecyclable++; +- pPage->isPinned = 0; + } + + pcache1LeaveMutex(pCache->pGroup); +@@ -40871,14 +49806,15 @@ static void pcache1Destroy(sqlite3_pcache *p){ + PGroup *pGroup = pCache->pGroup; + assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) ); + pcache1EnterMutex(pGroup); +- pcache1TruncateUnsafe(pCache, 0); ++ if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0); + assert( pGroup->nMaxPage >= pCache->nMax ); + pGroup->nMaxPage -= pCache->nMax; + assert( pGroup->nMinPage >= pCache->nMin ); + pGroup->nMinPage -= pCache->nMin; + pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; +- pcache1EnforceMaxPage(pGroup); ++ pcache1EnforceMaxPage(pCache); + pcache1LeaveMutex(pGroup); ++ sqlite3_free(pCache->pBulk); + sqlite3_free(pCache->apHash); + sqlite3_free(pCache); + } +@@ -40934,18 +49870,20 @@ SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){ + int nFree = 0; + assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); + assert( sqlite3_mutex_notheld(pcache1.mutex) ); +- if( pcache1.pStart==0 ){ ++ if( sqlite3GlobalConfig.pPage==0 ){ + PgHdr1 *p; + pcache1EnterMutex(&pcache1.grp); +- while( (nReq<0 || nFree isAnchor==0 ++ ){ + nFree += pcache1MemSize(p->page.pBuf); + #ifdef SQLITE_PCACHE_SEPARATE_HEADER + nFree += sqlite3MemSize(p); + #endif +- assert( p->isPinned==0 ); ++ assert( PAGE_IS_UNPINNED(p) ); + pcache1PinPage(p); +- pcache1RemoveFromHash(p); +- pcache1FreePage(p); ++ pcache1RemoveFromHash(p, 1); + } + pcache1LeaveMutex(&pcache1.grp); + } +@@ -40966,11 +49904,11 @@ SQLITE_PRIVATE void sqlite3PcacheStats( + ){ + PgHdr1 *p; + int nRecyclable = 0; +- for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){ +- assert( p->isPinned==0 ); ++ for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){ ++ assert( PAGE_IS_UNPINNED(p) ); + nRecyclable++; + } +- *pnCurrent = pcache1.grp.nCurrentPage; ++ *pnCurrent = pcache1.grp.nPurgeable; + *pnMax = (int)pcache1.grp.nMaxPage; + *pnMin = (int)pcache1.grp.nMinPage; + *pnRecyclable = nRecyclable; +@@ -41038,9 +49976,11 @@ SQLITE_PRIVATE void sqlite3PcacheStats( + ** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST + ** primitives are constant time. The cost of DESTROY is O(N). + ** +-** There is an added cost of O(N) when switching between TEST and +-** SMALLEST primitives. ++** TEST and SMALLEST may not be used by the same RowSet. This used to ++** be possible, but the feature was not used, so it was removed in order ++** to simplify the code. + */ ++/* #include "sqliteInt.h" */ + + + /* +@@ -41103,30 +50043,23 @@ struct RowSet { + #define ROWSET_NEXT 0x02 /* True if sqlite3RowSetNext() has been called */ + + /* +-** Turn bulk memory into a RowSet object. N bytes of memory +-** are available at pSpace. The db pointer is used as a memory context +-** for any subsequent allocations that need to occur. +-** Return a pointer to the new RowSet object. +-** +-** It must be the case that N is sufficient to make a Rowset. If not +-** an assertion fault occurs. +-** +-** If N is larger than the minimum, use the surplus as an initial +-** allocation of entries available to be filled. ++** Allocate a RowSet object. Return NULL if a memory allocation ++** error occurs. + */ +-SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){ +- RowSet *p; +- assert( N >= ROUND8(sizeof(*p)) ); +- p = pSpace; +- p->pChunk = 0; +- p->db = db; +- p->pEntry = 0; +- p->pLast = 0; +- p->pForest = 0; +- p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p); +- p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry)); +- p->rsFlags = ROWSET_SORTED; +- p->iBatch = 0; ++SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db){ ++ RowSet *p = sqlite3DbMallocRawNN(db, sizeof(*p)); ++ if( p ){ ++ int N = sqlite3DbMallocSize(db, p); ++ p->pChunk = 0; ++ p->db = db; ++ p->pEntry = 0; ++ p->pLast = 0; ++ p->pForest = 0; ++ p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p); ++ p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry)); ++ p->rsFlags = ROWSET_SORTED; ++ p->iBatch = 0; ++ } + return p; + } + +@@ -41135,7 +50068,8 @@ SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int + ** the RowSet has allocated over its lifetime. This routine is + ** the destructor for the RowSet. + */ +-SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){ ++SQLITE_PRIVATE void sqlite3RowSetClear(void *pArg){ ++ RowSet *p = (RowSet*)pArg; + struct RowSetChunk *pChunk, *pNextChunk; + for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){ + pNextChunk = pChunk->pNextChunk; +@@ -41149,6 +50083,16 @@ SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){ + p->rsFlags = ROWSET_SORTED; + } + ++/* ++** Deallocate all chunks from a RowSet. This frees all memory that ++** the RowSet has allocated over its lifetime. This routine is ++** the destructor for the RowSet. ++*/ ++SQLITE_PRIVATE void sqlite3RowSetDelete(void *pArg){ ++ sqlite3RowSetClear(pArg); ++ sqlite3DbFree(((RowSet*)pArg)->db, pArg); ++} ++ + /* + ** Allocate a new RowSetEntry object that is associated with the + ** given RowSet. Return a pointer to the new and completely uninitialized +@@ -41159,9 +50103,11 @@ SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){ + */ + static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){ + assert( p!=0 ); +- if( p->nFresh==0 ){ ++ if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/ ++ /* We could allocate a fresh RowSetEntry each time one is needed, but it ++ ** is more efficient to pull a preallocated entry from the pool */ + struct RowSetChunk *pNew; +- pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew)); ++ pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew)); + if( pNew==0 ){ + return 0; + } +@@ -41193,7 +50139,9 @@ SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){ + pEntry->pRight = 0; + pLast = p->pLast; + if( pLast ){ +- if( (p->rsFlags & ROWSET_SORTED)!=0 && rowid<=pLast->v ){ ++ if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/ ++ /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags ++ ** where possible */ + p->rsFlags &= ~ROWSET_SORTED; + } + pLast->pRight = pEntry; +@@ -41217,28 +50165,26 @@ static struct RowSetEntry *rowSetEntryMerge( + struct RowSetEntry *pTail; + + pTail = &head; +- while( pA && pB ){ ++ assert( pA!=0 && pB!=0 ); ++ for(;;){ + assert( pA->pRight==0 || pA->v<=pA->pRight->v ); + assert( pB->pRight==0 || pB->v<=pB->pRight->v ); +- if( pA->v v ){ +- pTail->pRight = pA; ++ if( pA->v<=pB->v ){ ++ if( pA->v v ) pTail = pTail->pRight = pA; + pA = pA->pRight; +- pTail = pTail->pRight; +- }else if( pB->v v ){ +- pTail->pRight = pB; +- pB = pB->pRight; +- pTail = pTail->pRight; ++ if( pA==0 ){ ++ pTail->pRight = pB; ++ break; ++ } + }else{ +- pA = pA->pRight; ++ pTail = pTail->pRight = pB; ++ pB = pB->pRight; ++ if( pB==0 ){ ++ pTail->pRight = pA; ++ break; ++ } + } + } +- if( pA ){ +- assert( pA->pRight==0 || pA->v<=pA->pRight->v ); +- pTail->pRight = pA; +- }else{ +- assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v ); +- pTail->pRight = pB; +- } + return head.pRight; + } + +@@ -41261,9 +50207,10 @@ static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){ + aBucket[i] = pIn; + pIn = pNext; + } +- pIn = 0; +- for(i=0; i 1 ){ /*OPTIMIZATION-IF-TRUE*/ ++ /* This branch causes a *balanced* tree to be generated. A valid tree ++ ** is still generated without this branch, but the tree is wildly ++ ** unbalanced and inefficient. */ ++ pLeft = rowSetNDeepTree(ppList, iDepth-1); ++ p = *ppList; ++ if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/ ++ /* It is safe to always return here, but the resulting tree ++ ** would be unbalanced */ ++ return pLeft; ++ } ++ p->pLeft = pLeft; ++ *ppList = p->pRight; ++ p->pRight = rowSetNDeepTree(ppList, iDepth-1); ++ }else{ + p = *ppList; + *ppList = p->pRight; + p->pLeft = p->pRight = 0; +- return p; + } +- pLeft = rowSetNDeepTree(ppList, iDepth-1); +- p = *ppList; +- if( p==0 ){ +- return pLeft; +- } +- p->pLeft = pLeft; +- *ppList = p->pRight; +- p->pRight = rowSetNDeepTree(ppList, iDepth-1); + return p; + } + +@@ -41358,59 +50311,37 @@ static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){ + return p; + } + +-/* +-** Take all the entries on p->pEntry and on the trees in p->pForest and +-** sort them all together into one big ordered list on p->pEntry. +-** +-** This routine should only be called once in the life of a RowSet. +-*/ +-static void rowSetToList(RowSet *p){ +- +- /* This routine is called only once */ +- assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 ); +- +- if( (p->rsFlags & ROWSET_SORTED)==0 ){ +- p->pEntry = rowSetEntrySort(p->pEntry); +- } +- +- /* While this module could theoretically support it, sqlite3RowSetNext() +- ** is never called after sqlite3RowSetText() for the same RowSet. So +- ** there is never a forest to deal with. Should this change, simply +- ** remove the assert() and the #if 0. */ +- assert( p->pForest==0 ); +-#if 0 +- while( p->pForest ){ +- struct RowSetEntry *pTree = p->pForest->pLeft; +- if( pTree ){ +- struct RowSetEntry *pHead, *pTail; +- rowSetTreeToList(pTree, &pHead, &pTail); +- p->pEntry = rowSetEntryMerge(p->pEntry, pHead); +- } +- p->pForest = p->pForest->pRight; +- } +-#endif +- p->rsFlags |= ROWSET_NEXT; /* Verify this routine is never called again */ +-} +- + /* + ** Extract the smallest element from the RowSet. + ** Write the element into *pRowid. Return 1 on success. Return + ** 0 if the RowSet is already empty. + ** + ** After this routine has been called, the sqlite3RowSetInsert() +-** routine may not be called again. ++** routine may not be called again. ++** ++** This routine may not be called after sqlite3RowSetTest() has ++** been used. Older versions of RowSet allowed that, but as the ++** capability was not used by the code generator, it was removed ++** for code economy. + */ + SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){ + assert( p!=0 ); ++ assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */ + + /* Merge the forest into a single sorted list on first call */ +- if( (p->rsFlags & ROWSET_NEXT)==0 ) rowSetToList(p); ++ if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/ ++ if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/ ++ p->pEntry = rowSetEntrySort(p->pEntry); ++ } ++ p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT; ++ } + + /* Return the next entry on the list */ + if( p->pEntry ){ + *pRowid = p->pEntry->v; + p->pEntry = p->pEntry->pRight; +- if( p->pEntry==0 ){ ++ if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/ ++ /* Free memory immediately, rather than waiting on sqlite3_finalize() */ + sqlite3RowSetClear(p); + } + return 1; +@@ -41433,13 +50364,15 @@ SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 + /* This routine is never called after sqlite3RowSetNext() */ + assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 ); + +- /* Sort entries into the forest on the first test of a new batch ++ /* Sort entries into the forest on the first test of a new batch. ++ ** To save unnecessary work, only do this when the batch number changes. + */ +- if( iBatch!=pRowSet->iBatch ){ ++ if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/ + p = pRowSet->pEntry; + if( p ){ + struct RowSetEntry **ppPrevTree = &pRowSet->pForest; +- if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ ++ if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/ ++ /* Only sort the current set of entiries if they need it */ + p = rowSetEntrySort(p); + } + for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){ +@@ -41510,6 +50443,7 @@ SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 + ** another is writing. + */ + #ifndef SQLITE_OMIT_DISKIO ++/* #include "sqliteInt.h" */ + /************** Include wal.h in the middle of pager.c ***********************/ + /************** Begin file wal.h *********************************************/ + /* +@@ -41528,20 +50462,21 @@ SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 + ** the implementation of each function in log.c for further details. + */ + +-#ifndef _WAL_H_ +-#define _WAL_H_ ++#ifndef SQLITE_WAL_H ++#define SQLITE_WAL_H + ++/* #include "sqliteInt.h" */ + +-/* Additional values that can be added to the sync_flags argument of +-** sqlite3WalFrames(): ++/* Macros for extracting appropriate sync flags for either transaction ++** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)): + */ +-#define WAL_SYNC_TRANSACTIONS 0x20 /* Sync at the end of each transaction */ +-#define SQLITE_SYNC_MASK 0x13 /* Mask off the SQLITE_SYNC_* values */ ++#define WAL_SYNC_FLAGS(X) ((X)&0x03) ++#define CKPT_SYNC_FLAGS(X) (((X)>>2)&0x03) + + #ifdef SQLITE_OMIT_WAL + # define sqlite3WalOpen(x,y,z) 0 + # define sqlite3WalLimit(x,y) +-# define sqlite3WalClose(w,x,y,z) 0 ++# define sqlite3WalClose(v,w,x,y,z) 0 + # define sqlite3WalBeginReadTransaction(y,z) 0 + # define sqlite3WalEndReadTransaction(z) + # define sqlite3WalDbsize(y) 0 +@@ -41551,12 +50486,13 @@ SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 + # define sqlite3WalSavepoint(y,z) + # define sqlite3WalSavepointUndo(y,z) 0 + # define sqlite3WalFrames(u,v,w,x,y,z) 0 +-# define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0 ++# define sqlite3WalCheckpoint(q,r,s,t,u,v,w,x,y,z) 0 + # define sqlite3WalCallback(z) 0 + # define sqlite3WalExclusiveMode(y,z) 0 + # define sqlite3WalHeapMemory(z) 0 + # define sqlite3WalFramesize(z) 0 + # define sqlite3WalFindFrame(x,y,z) 0 ++# define sqlite3WalFile(x) 0 + #else + + #define WAL_SAVEPOINT_NDATA 4 +@@ -41568,7 +50504,7 @@ typedef struct Wal Wal; + + /* Open and close a connection to a write-ahead log. */ + SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**); +-SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *); ++SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *); + + /* Set the limiting size of a WAL file. */ + SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64); +@@ -41611,6 +50547,7 @@ SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int); + /* Copy pages from the log to the database file */ + SQLITE_PRIVATE int sqlite3WalCheckpoint( + Wal *pWal, /* Write-ahead log connection */ ++ sqlite3 *db, /* Check this handle's interrupt flag */ + int eMode, /* One of PASSIVE, FULL and RESTART */ + int (*xBusy)(void*), /* Function to call when busy */ + void *pBusyArg, /* Context argument for xBusyHandler */ +@@ -41639,6 +50576,14 @@ SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op); + */ + SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal); + ++#ifdef SQLITE_ENABLE_SNAPSHOT ++SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot); ++SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot); ++SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal); ++SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot); ++SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal); ++#endif ++ + #ifdef SQLITE_ENABLE_ZIPVFS + /* If the WAL file is not empty, return the number of bytes of content + ** stored in each frame (i.e. the db page-size when the WAL was created). +@@ -41646,8 +50591,11 @@ SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal); + SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal); + #endif + ++/* Return the sqlite3_file object for the WAL file */ ++SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal); ++ + #endif /* ifndef SQLITE_OMIT_WAL */ +-#endif /* _WAL_H_ */ ++#endif /* SQLITE_WAL_H */ + + /************** End of wal.h *************************************************/ + /************** Continuing where we left off in pager.c **********************/ +@@ -41758,8 +50706,8 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ + ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file + ** struct as its argument. + */ +-#define PAGERID(p) ((int)(p->fd)) +-#define FILEHANDLEID(fd) ((int)fd) ++#define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd)) ++#define FILEHANDLEID(fd) (SQLITE_PTR_TO_INT(fd)) + + /* + ** The Pager.eState variable stores the current 'state' of a pager. A +@@ -42058,6 +51006,7 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ + */ + #define MAX_SECTOR_SIZE 0x10000 + ++ + /* + ** An instance of the following structure is allocated for each active + ** savepoint and statement transaction in the system. All such structures +@@ -42086,9 +51035,9 @@ struct PagerSavepoint { + /* + ** Bits of the Pager.doNotSpill flag. See further description below. + */ +-#define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */ +-#define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */ +-#define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */ ++#define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */ ++#define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */ ++#define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */ + + /* + ** An open page cache is an instance of struct Pager. A description of +@@ -42170,11 +51119,11 @@ struct PagerSavepoint { + ** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF + ** case is a user preference. + ** +-** If the SPILLFLAG_NOSYNC bit is set, writing to the database from pagerStress() +-** is permitted, but syncing the journal file is not. This flag is set +-** by sqlite3PagerWrite() when the file-system sector-size is larger than +-** the database page-size in order to prevent a journal sync from happening +-** in between the journalling of two pages on the same sector. ++** If the SPILLFLAG_NOSYNC bit is set, writing to the database from ++** pagerStress() is permitted, but syncing the journal file is not. ++** This flag is set by sqlite3PagerWrite() when the file-system sector-size ++** is larger than the database page-size in order to prevent a journal sync ++** from happening in between the journalling of two pages on the same sector. + ** + ** subjInMemory + ** +@@ -42245,6 +51194,18 @@ struct PagerSavepoint { + ** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode + ** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX + ** sub-codes. ++** ++** syncFlags, walSyncFlags ++** ++** syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03). ++** syncFlags is used for rollback mode. walSyncFlags is used for WAL mode ++** and contains the flags used to sync the checkpoint operations in the ++** lower two bits, and sync flags used for transaction commits in the WAL ++** file in bits 0x04 and 0x08. In other words, to get the correct sync flags ++** for checkpoint operations, use (walSyncFlags&0x03) and to get the correct ++** sync flags for transaction commit, use ((walSyncFlags>>2)&0x03). Note ++** that with synchronous=NORMAL in WAL mode, transaction commit is not synced ++** meaning that the 0x04 and 0x08 bits are both zero. + */ + struct Pager { + sqlite3_vfs *pVfs; /* OS functions to use for IO */ +@@ -42253,9 +51214,9 @@ struct Pager { + u8 useJournal; /* Use a rollback journal on this file */ + u8 noSync; /* Do not sync the journal if true */ + u8 fullSync; /* Do extra syncs of the journal for robustness */ +- u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */ +- u8 walSyncFlags; /* SYNC_NORMAL or SYNC_FULL for wal writes */ ++ u8 extraSync; /* sync directory after journal delete */ + u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */ ++ u8 walSyncFlags; /* See description above */ + u8 tempFile; /* zFilename is a temporary or immutable file */ + u8 noLock; /* Do not lock (except in WAL mode) */ + u8 readOnly; /* True for a read-only database */ +@@ -42277,7 +51238,7 @@ struct Pager { + u8 doNotSpill; /* Do not spill the cache when non-zero */ + u8 subjInMemory; /* True to use in-memory sub-journals */ + u8 bUseFetch; /* True to use xFetch() */ +- u8 hasBeenUsed; /* True if any content previously read from this pager*/ ++ u8 hasHeldSharedLock; /* True if a shared lock has ever been held */ + Pgno dbSize; /* Number of pages in the database */ + Pgno dbOrigSize; /* dbSize before the current transaction */ + Pgno dbFileSize; /* Number of pages in the database file */ +@@ -42316,11 +51277,12 @@ struct Pager { + char *zJournal; /* Name of the journal file */ + int (*xBusyHandler)(void*); /* Function to call when busy */ + void *pBusyHandlerArg; /* Context argument for xBusyHandler */ +- int aStat[3]; /* Total cache hits, misses and writes */ ++ int aStat[4]; /* Total cache hits, misses, writes, spills */ + #ifdef SQLITE_TEST + int nRead; /* Database pages read */ + #endif + void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */ ++ int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */ + #ifdef SQLITE_HAS_CODEC + void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ + void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */ +@@ -42343,6 +51305,7 @@ struct Pager { + #define PAGER_STAT_HIT 0 + #define PAGER_STAT_MISS 1 + #define PAGER_STAT_WRITE 2 ++#define PAGER_STAT_SPILL 3 + + /* + ** The following global variables hold counters used for +@@ -42438,16 +51401,37 @@ static const unsigned char aJournalMagic[] = { + ** + ** if( pPager->jfd->pMethods ){ ... + */ +-#define isOpen(pFd) ((pFd)->pMethods) ++#define isOpen(pFd) ((pFd)->pMethods!=0) + ++#ifdef SQLITE_DIRECT_OVERFLOW_READ + /* +-** Return true if this pager uses a write-ahead log instead of the usual +-** rollback journal. Otherwise false. ++** Return true if page pgno can be read directly from the database file ++** by the b-tree layer. This is the case if: ++** ++** * the database file is open, ++** * there are no dirty pages in the cache, and ++** * the desired page is not currently in the wal file. + */ ++SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){ ++ if( pPager->fd->pMethods==0 ) return 0; ++ if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0; ++#ifdef SQLITE_HAS_CODEC ++ if( pPager->xCodec!=0 ) return 0; ++#endif + #ifndef SQLITE_OMIT_WAL +-static int pagerUseWal(Pager *pPager){ +- return (pPager->pWal!=0); ++ if( pPager->pWal ){ ++ u32 iRead = 0; ++ int rc; ++ rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead); ++ return (rc==SQLITE_OK && iRead==0); ++ } ++#endif ++ return 1; + } ++#endif ++ ++#ifndef SQLITE_OMIT_WAL ++# define pagerUseWal(x) ((x)->pWal!=0) + #else + # define pagerUseWal(x) 0 + # define pagerRollbackWal(x) 0 +@@ -42500,6 +51484,7 @@ static int assert_pager_state(Pager *p){ + ** state. + */ + if( MEMDB ){ ++ assert( !isOpen(p->fd) ); + assert( p->noSync ); + assert( p->journalMode==PAGER_JOURNALMODE_OFF + || p->journalMode==PAGER_JOURNALMODE_MEMORY +@@ -42566,6 +51551,7 @@ static int assert_pager_state(Pager *p){ + assert( isOpen(p->jfd) + || p->journalMode==PAGER_JOURNALMODE_OFF + || p->journalMode==PAGER_JOURNALMODE_WAL ++ || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC) + ); + assert( pPager->dbOrigSize<=pPager->dbHintSize ); + break; +@@ -42577,6 +51563,7 @@ static int assert_pager_state(Pager *p){ + assert( isOpen(p->jfd) + || p->journalMode==PAGER_JOURNALMODE_OFF + || p->journalMode==PAGER_JOURNALMODE_WAL ++ || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC) + ); + break; + +@@ -42586,7 +51573,7 @@ static int assert_pager_state(Pager *p){ + ** back to OPEN state. + */ + assert( pPager->errCode!=SQLITE_OK ); +- assert( sqlite3PcacheRefCount(pPager->pPCache)>0 ); ++ assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile ); + break; + } + +@@ -42602,8 +51589,12 @@ static int assert_pager_state(Pager *p){ + ** to "print *pPager" in gdb: + ** + ** (gdb) printf "%s", print_pager_state(pPager) ++** ++** This routine has external linkage in order to suppress compiler warnings ++** about an unused function. It is enclosed within SQLITE_DEBUG and so does ++** not appear in normal builds. + */ +-static char *print_pager_state(Pager *p){ ++char *print_pager_state(Pager *p){ + static char zRet[1024]; + + sqlite3_snprintf(1024, zRet, +@@ -42645,6 +51636,33 @@ static char *print_pager_state(Pager *p){ + } + #endif + ++/* Forward references to the various page getters */ ++static int getPageNormal(Pager*,Pgno,DbPage**,int); ++static int getPageError(Pager*,Pgno,DbPage**,int); ++#if SQLITE_MAX_MMAP_SIZE>0 ++static int getPageMMap(Pager*,Pgno,DbPage**,int); ++#endif ++ ++/* ++** Set the Pager.xGet method for the appropriate routine used to fetch ++** content from the pager. ++*/ ++static void setGetterMethod(Pager *pPager){ ++ if( pPager->errCode ){ ++ pPager->xGet = getPageError; ++#if SQLITE_MAX_MMAP_SIZE>0 ++ }else if( USEFETCH(pPager) ++#ifdef SQLITE_HAS_CODEC ++ && pPager->xCodec==0 ++#endif ++ ){ ++ pPager->xGet = getPageMMap; ++#endif /* SQLITE_MAX_MMAP_SIZE>0 */ ++ }else{ ++ pPager->xGet = getPageNormal; ++ } ++} ++ + /* + ** Return true if it is necessary to write page *pPg into the sub-journal. + ** A page needs to be written into the sub-journal if there exists one +@@ -42661,19 +51679,21 @@ static int subjRequiresPage(PgHdr *pPg){ + int i; + for(i=0; i nSavepoint; i++){ + p = &pPager->aSavepoint[i]; +- if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){ ++ if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){ + return 1; + } + } + return 0; + } + ++#ifdef SQLITE_DEBUG + /* + ** Return true if the page is already in the journal file. + */ + static int pageInJournal(Pager *pPager, PgHdr *pPg){ + return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno); + } ++#endif + + /* + ** Read a 32-bit integer from the given file descriptor. Store the integer +@@ -42758,34 +51778,47 @@ static int pagerLockDb(Pager *pPager, int eLock){ + } + + /* +-** This function determines whether or not the atomic-write optimization +-** can be used with this pager. The optimization can be used if: ++** This function determines whether or not the atomic-write or ++** atomic-batch-write optimizations can be used with this pager. The ++** atomic-write optimization can be used if: + ** + ** (a) the value returned by OsDeviceCharacteristics() indicates that + ** a database page may be written atomically, and + ** (b) the value returned by OsSectorSize() is less than or equal + ** to the page size. + ** +-** The optimization is also always enabled for temporary files. It is +-** an error to call this function if pPager is opened on an in-memory +-** database. ++** If it can be used, then the value returned is the size of the journal ++** file when it contains rollback data for exactly one page. + ** +-** If the optimization cannot be used, 0 is returned. If it can be used, +-** then the value returned is the size of the journal file when it +-** contains rollback data for exactly one page. ++** The atomic-batch-write optimization can be used if OsDeviceCharacteristics() ++** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is ++** returned in this case. ++** ++** If neither optimization can be used, 0 is returned. + */ +-#ifdef SQLITE_ENABLE_ATOMIC_WRITE + static int jrnlBufferSize(Pager *pPager){ + assert( !MEMDB ); +- if( !pPager->tempFile ){ +- int dc; /* Device characteristics */ +- int nSector; /* Sector size */ +- int szPage; /* Page size */ + +- assert( isOpen(pPager->fd) ); +- dc = sqlite3OsDeviceCharacteristics(pPager->fd); +- nSector = pPager->sectorSize; +- szPage = pPager->pageSize; ++#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \ ++ || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) ++ int dc; /* Device characteristics */ ++ ++ assert( isOpen(pPager->fd) ); ++ dc = sqlite3OsDeviceCharacteristics(pPager->fd); ++#else ++ UNUSED_PARAMETER(pPager); ++#endif ++ ++#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE ++ if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){ ++ return -1; ++ } ++#endif ++ ++#ifdef SQLITE_ENABLE_ATOMIC_WRITE ++ { ++ int nSector = pPager->sectorSize; ++ int szPage = pPager->pageSize; + + assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); + assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); +@@ -42795,9 +51828,11 @@ static int jrnlBufferSize(Pager *pPager){ + } + + return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager); +-} + #endif + ++ return 0; ++} ++ + /* + ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking + ** on the cache using a hash function. This is used for testing +@@ -42879,6 +51914,7 @@ static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){ + || szJ<16 + || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len)) + || len>=nMaster ++ || len>szJ-16 + || len==0 + || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum)) + || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8)) +@@ -42956,6 +51992,7 @@ static i64 journalHdrOffset(Pager *pPager){ + static int zeroJournalHdr(Pager *pPager, int doTruncate){ + int rc = SQLITE_OK; /* Return code */ + assert( isOpen(pPager->jfd) ); ++ assert( !sqlite3JournalIsInMemory(pPager->jfd) ); + if( pPager->journalOff ){ + const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */ + +@@ -43285,7 +52322,8 @@ static int writeMasterJournal(Pager *pPager, const char *zMaster){ + || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4))) + || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster))) + || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum))) +- || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8))) ++ || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, ++ iHdrOff+4+nMaster+8))) + ){ + return rc; + } +@@ -43322,7 +52360,6 @@ static void pager_reset(Pager *pPager){ + ** Return the pPager->iDataVersion value + */ + SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager *pPager){ +- assert( pPager->eState>PAGER_OPEN ); + return pPager->iDataVersion; + } + +@@ -43336,7 +52373,7 @@ static void releaseAllSavepoints(Pager *pPager){ + for(ii=0; ii nSavepoint; ii++){ + sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint); + } +- if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){ ++ if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){ + sqlite3OsClose(pPager->sjfd); + } + sqlite3_free(pPager->aSavepoint); +@@ -43442,13 +52479,18 @@ static void pager_unlock(Pager *pPager){ + ** it can safely move back to PAGER_OPEN state. This happens in both + ** normal and exclusive-locking mode. + */ ++ assert( pPager->errCode==SQLITE_OK || !MEMDB ); + if( pPager->errCode ){ +- assert( !MEMDB ); +- pager_reset(pPager); +- pPager->changeCountDone = pPager->tempFile; +- pPager->eState = PAGER_OPEN; +- pPager->errCode = SQLITE_OK; ++ if( pPager->tempFile==0 ){ ++ pager_reset(pPager); ++ pPager->changeCountDone = 0; ++ pPager->eState = PAGER_OPEN; ++ }else{ ++ pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER); ++ } + if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0); ++ pPager->errCode = SQLITE_OK; ++ setGetterMethod(pPager); + } + + pPager->journalOff = 0; +@@ -43486,12 +52528,36 @@ static int pager_error(Pager *pPager, int rc){ + if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){ + pPager->errCode = rc; + pPager->eState = PAGER_ERROR; ++ setGetterMethod(pPager); + } + return rc; + } + + static int pager_truncate(Pager *pPager, Pgno nPage); + ++/* ++** The write transaction open on pPager is being committed (bCommit==1) ++** or rolled back (bCommit==0). ++** ++** Return TRUE if and only if all dirty pages should be flushed to disk. ++** ++** Rules: ++** ++** * For non-TEMP databases, always sync to disk. This is necessary ++** for transactions to be durable. ++** ++** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing ++** file has been created already (via a spill on pagerStress()) and ++** when the number of dirty pages in memory exceeds 25% of the total ++** cache size. ++*/ ++static int pagerFlushOnCommit(Pager *pPager, int bCommit){ ++ if( pPager->tempFile==0 ) return 1; ++ if( !bCommit ) return 0; ++ if( !isOpen(pPager->fd) ) return 0; ++ return (sqlite3PCachePercentDirty(pPager->pPCache)>=25); ++} ++ + /* + ** This routine ends a transaction. A transaction is usually ended by + ** either a COMMIT or a ROLLBACK operation. This routine may be called +@@ -43569,13 +52635,15 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ + } + + releaseAllSavepoints(pPager); +- assert( isOpen(pPager->jfd) || pPager->pInJournal==0 ); ++ assert( isOpen(pPager->jfd) || pPager->pInJournal==0 ++ || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC) ++ ); + if( isOpen(pPager->jfd) ){ + assert( !pagerUseWal(pPager) ); + + /* Finalize the journal file. */ +- if( sqlite3IsMemJournal(pPager->jfd) ){ +- assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); ++ if( sqlite3JournalIsInMemory(pPager->jfd) ){ ++ /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */ + sqlite3OsClose(pPager->jfd); + }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){ + if( pPager->journalOff==0 ){ +@@ -43595,22 +52663,23 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ + }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST + || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL) + ){ +- rc = zeroJournalHdr(pPager, hasMaster); ++ rc = zeroJournalHdr(pPager, hasMaster||pPager->tempFile); + pPager->journalOff = 0; + }else{ + /* This branch may be executed with Pager.journalMode==MEMORY if + ** a hot-journal was just rolled back. In this case the journal + ** file should be closed and deleted. If this connection writes to +- ** the database file, it will do so using an in-memory journal. ++ ** the database file, it will do so using an in-memory journal. + */ +- int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd)); ++ int bDelete = !pPager->tempFile; ++ assert( sqlite3JournalIsInMemory(pPager->jfd)==0 ); + assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE + || pPager->journalMode==PAGER_JOURNALMODE_MEMORY + || pPager->journalMode==PAGER_JOURNALMODE_WAL + ); + sqlite3OsClose(pPager->jfd); + if( bDelete ){ +- rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); ++ rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync); + } + } + } +@@ -43629,8 +52698,14 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ + sqlite3BitvecDestroy(pPager->pInJournal); + pPager->pInJournal = 0; + pPager->nRec = 0; +- sqlite3PcacheCleanAll(pPager->pPCache); +- sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize); ++ if( rc==SQLITE_OK ){ ++ if( MEMDB || pagerFlushOnCommit(pPager, bCommit) ){ ++ sqlite3PcacheCleanAll(pPager->pPCache); ++ }else{ ++ sqlite3PcacheClearWritable(pPager->pPCache); ++ } ++ sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize); ++ } + + if( pagerUseWal(pPager) ){ + /* Drop the WAL write-lock, if any. Also, if the connection was in +@@ -43650,7 +52725,7 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ + rc = pager_truncate(pPager, pPager->dbSize); + } + +- if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){ ++ if( rc==SQLITE_OK && bCommit ){ + rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0); + if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; + } +@@ -43743,6 +52818,20 @@ static void pagerReportSize(Pager *pPager){ + # define pagerReportSize(X) /* No-op if we do not support a codec */ + #endif + ++#ifdef SQLITE_HAS_CODEC ++/* ++** Make sure the number of reserved bits is the same in the destination ++** pager as it is in the source. This comes up when a VACUUM changes the ++** number of reserved bits to the "optimal" amount. ++*/ ++SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager *pDest, Pager *pSrc){ ++ if( pDest->nReserve!=pSrc->nReserve ){ ++ pDest->nReserve = pSrc->nReserve; ++ pagerReportSize(pDest); ++ } ++} ++#endif ++ + /* + ** Read a single page from either the journal file (if isMainJrnl==1) or + ** from the sub-journal (if isMainJrnl==0) and playback that page. +@@ -43794,6 +52883,11 @@ static int pager_playback_one_page( + char *aData; /* Temporary storage for the page */ + sqlite3_file *jfd; /* The file descriptor for the journal file */ + int isSynced; /* True if journal page is synced */ ++#ifdef SQLITE_HAS_CODEC ++ /* The jrnlEnc flag is true if Journal pages should be passed through ++ ** the codec. It is false for pure in-memory journals. */ ++ const int jrnlEnc = (isMainJrnl || pPager->subjInMemory==0); ++#endif + + assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */ + assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */ +@@ -43845,7 +52939,7 @@ static int pager_playback_one_page( + } + } + +- /* If this page has already been played by before during the current ++ /* If this page has already been played back before during the current + ** rollback, then don't bother to play it back again. + */ + if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){ +@@ -43900,7 +52994,7 @@ static int pager_playback_one_page( + pPg = sqlite3PagerLookup(pPager, pgno); + } + assert( pPg || !MEMDB ); +- assert( pPager->eState!=PAGER_OPEN || pPg==0 ); ++ assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile ); + PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n", + PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData), + (isMainJrnl?"main-journal":"sub-journal") +@@ -43917,14 +53011,34 @@ static int pager_playback_one_page( + i64 ofst = (pgno-1)*(i64)pPager->pageSize; + testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 ); + assert( !pagerUseWal(pPager) ); ++ ++ /* Write the data read from the journal back into the database file. ++ ** This is usually safe even for an encrypted database - as the data ++ ** was encrypted before it was written to the journal file. The exception ++ ** is if the data was just read from an in-memory sub-journal. In that ++ ** case it must be encrypted here before it is copied into the database ++ ** file. */ ++#ifdef SQLITE_HAS_CODEC ++ if( !jrnlEnc ){ ++ CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT, aData); ++ rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst); ++ CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT); ++ }else ++#endif + rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst); ++ + if( pgno>pPager->dbFileSize ){ + pPager->dbFileSize = pgno; + } + if( pPager->pBackup ){ +- CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM); ++#ifdef SQLITE_HAS_CODEC ++ if( jrnlEnc ){ ++ CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT); ++ sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData); ++ CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT,aData); ++ }else ++#endif + sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData); +- CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData); + } + }else if( !isMainJrnl && pPg==0 ){ + /* If this is a rollback of a savepoint and data was not written to +@@ -43946,11 +53060,10 @@ static int pager_playback_one_page( + assert( isSavepnt ); + assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 ); + pPager->doNotSpill |= SPILLFLAG_ROLLBACK; +- rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1); ++ rc = sqlite3PagerGet(pPager, pgno, &pPg, 1); + assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 ); + pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK; + if( rc!=SQLITE_OK ) return rc; +- pPg->flags &= ~PGHDR_NEED_READ; + sqlite3PcacheMakeDirty(pPg); + } + if( pPg ){ +@@ -43964,29 +53077,10 @@ static int pager_playback_one_page( + pData = pPg->pData; + memcpy(pData, (u8*)aData, pPager->pageSize); + pPager->xReiniter(pPg); +- if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){ +- /* If the contents of this page were just restored from the main +- ** journal file, then its content must be as they were when the +- ** transaction was first opened. In this case we can mark the page +- ** as clean, since there will be no need to write it out to the +- ** database. +- ** +- ** There is one exception to this rule. If the page is being rolled +- ** back as part of a savepoint (or statement) rollback from an +- ** unsynced portion of the main journal file, then it is not safe +- ** to mark the page as clean. This is because marking the page as +- ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is +- ** already in the journal file (recorded in Pager.pInJournal) and +- ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to +- ** again within this transaction, it will be marked as dirty but +- ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially +- ** be written out into the database file before its journal file +- ** segment is synced. If a crash occurs during or following this, +- ** database corruption may ensue. +- */ +- assert( !pagerUseWal(pPager) ); +- sqlite3PcacheMakeClean(pPg); +- } ++ /* It used to be that sqlite3PcacheMakeClean(pPg) was called here. But ++ ** that call was dangerous and had no detectable benefit since the cache ++ ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so ++ ** has been removed. */ + pager_set_pagehash(pPg); + + /* If this was page 1, then restore the value of Pager.dbFileVers. +@@ -43996,7 +53090,9 @@ static int pager_playback_one_page( + } + + /* Decode the page just read from disk */ +- CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM); ++#if SQLITE_HAS_CODEC ++ if( jrnlEnc ){ CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM_BKPT); } ++#endif + sqlite3PcacheRelease(pPg); + } + return rc; +@@ -44062,7 +53158,7 @@ static int pager_delmaster(Pager *pPager, const char *zMaster){ + pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2); + pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile); + if( !pMaster ){ +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + }else{ + const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL); + rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0); +@@ -44079,7 +53175,7 @@ static int pager_delmaster(Pager *pPager, const char *zMaster){ + nMasterPtr = pVfs->mxPathname+1; + zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 1); + if( !zMasterJournal ){ +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + goto delmaster_out; + } + zMasterPtr = &zMasterJournal[nMasterJournal+1]; +@@ -44309,6 +53405,7 @@ static int pager_playback(Pager *pPager, int isHot){ + char *zMaster = 0; /* Name of master journal file if any */ + int needPagerReset; /* True to reset page prior to first page rollback */ + int nPlayback = 0; /* Total number of pages restored from journal */ ++ u32 savedPageSize = pPager->pageSize; + + /* Figure out how many records are in the journal. Abort early if + ** the journal is empty. +@@ -44327,7 +53424,7 @@ static int pager_playback(Pager *pPager, int isHot){ + ** TODO: Technically the following is an error because it assumes that + ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that + ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c, +- ** mxPathname is 512, which is the same as the minimum allowable value ++ ** mxPathname is 512, which is the same as the minimum allowable value + ** for pageSize. + */ + zMaster = pPager->pTmpSpace; +@@ -44438,15 +53535,16 @@ static int pager_playback(Pager *pPager, int isHot){ + assert( 0 ); + + end_playback: ++ if( rc==SQLITE_OK ){ ++ rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1); ++ } + /* Following a rollback, the database file should be back in its original + ** state prior to the start of the transaction, so invoke the + ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the + ** assertion that the transaction counter was modified. + */ + #ifdef SQLITE_DEBUG +- if( pPager->fd->pMethods ){ +- sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0); +- } ++ sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0); + #endif + + /* If this playback is happening automatically as a result of an IO or +@@ -44496,7 +53594,8 @@ end_playback: + + + /* +-** Read the content for page pPg out of the database file and into ++** Read the content for page pPg out of the database file (or out of ++** the WAL if that is where the most recent copy if found) into + ** pPg->pData. A shared lock or greater must be held on the database + ** file before this function is called. + ** +@@ -44506,30 +53605,33 @@ end_playback: + ** If an IO error occurs, then the IO error is returned to the caller. + ** Otherwise, SQLITE_OK is returned. + */ +-static int readDbPage(PgHdr *pPg, u32 iFrame){ ++static int readDbPage(PgHdr *pPg){ + Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */ +- Pgno pgno = pPg->pgno; /* Page number to read */ + int rc = SQLITE_OK; /* Return code */ +- int pgsz = pPager->pageSize; /* Number of bytes to read */ ++ ++#ifndef SQLITE_OMIT_WAL ++ u32 iFrame = 0; /* Frame of WAL containing pgno */ + + assert( pPager->eState>=PAGER_READER && !MEMDB ); + assert( isOpen(pPager->fd) ); + +-#ifndef SQLITE_OMIT_WAL ++ if( pagerUseWal(pPager) ){ ++ rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame); ++ if( rc ) return rc; ++ } + if( iFrame ){ +- /* Try to pull the page from the write-ahead log. */ +- rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData); ++ rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData); + }else + #endif + { +- i64 iOffset = (pgno-1)*(i64)pPager->pageSize; +- rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset); ++ i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize; ++ rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset); + if( rc==SQLITE_IOERR_SHORT_READ ){ + rc = SQLITE_OK; + } + } + +- if( pgno==1 ){ ++ if( pPg->pgno==1 ){ + if( rc ){ + /* If the read is unsuccessful, set the dbFileVers[] to something + ** that will never be a valid file version. dbFileVers[] is a copy +@@ -44549,13 +53651,13 @@ static int readDbPage(PgHdr *pPg, u32 iFrame){ + memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers)); + } + } +- CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM); ++ CODEC1(pPager, pPg->pData, pPg->pgno, 3, rc = SQLITE_NOMEM_BKPT); + + PAGER_INCR(sqlite3_pager_readdb_count); + PAGER_INCR(pPager->nRead); +- IOTRACE(("PGIN %p %d\n", pPager, pgno)); ++ IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno)); + PAGERTRACE(("FETCH %d page %d hash(%08x)\n", +- PAGERID(pPager), pgno, pager_pagehash(pPg))); ++ PAGERID(pPager), pPg->pgno, pager_pagehash(pPg))); + + return rc; + } +@@ -44606,11 +53708,7 @@ static int pagerUndoCallback(void *pCtx, Pgno iPg){ + if( sqlite3PcachePageRefcount(pPg)==1 ){ + sqlite3PcacheDrop(pPg); + }else{ +- u32 iFrame = 0; +- rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame); +- if( rc==SQLITE_OK ){ +- rc = readDbPage(pPg, iFrame); +- } ++ rc = readDbPage(pPg); + if( rc==SQLITE_OK ){ + pPager->xReiniter(pPg); + } +@@ -44777,22 +53875,20 @@ static int pagerPagecount(Pager *pPager, Pgno *pnPage){ + */ + assert( pPager->eState==PAGER_OPEN ); + assert( pPager->eLock>=SHARED_LOCK ); ++ assert( isOpen(pPager->fd) ); ++ assert( pPager->tempFile==0 ); + nPage = sqlite3WalDbsize(pPager->pWal); + +- /* If the database size was not available from the WAL sub-system, +- ** determine it based on the size of the database file. If the size +- ** of the database file is not an integer multiple of the page-size, +- ** round down to the nearest page. Except, any file larger than 0 +- ** bytes in size is considered to contain at least one page. ++ /* If the number of pages in the database is not available from the ++ ** WAL sub-system, determine the page count based on the size of ++ ** the database file. If the size of the database file is not an ++ ** integer multiple of the page-size, round up the result. + */ +- if( nPage==0 ){ ++ if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){ + i64 n = 0; /* Size of db file in bytes */ +- assert( isOpen(pPager->fd) || pPager->tempFile ); +- if( isOpen(pPager->fd) ){ +- int rc = sqlite3OsFileSize(pPager->fd, &n); +- if( rc!=SQLITE_OK ){ +- return rc; +- } ++ int rc = sqlite3OsFileSize(pPager->fd, &n); ++ if( rc!=SQLITE_OK ){ ++ return rc; + } + nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize); + } +@@ -44835,23 +53931,21 @@ static int pagerOpenWalIfPresent(Pager *pPager){ + + if( !pPager->tempFile ){ + int isWal; /* True if WAL file exists */ +- Pgno nPage; /* Size of the database file */ +- +- rc = pagerPagecount(pPager, &nPage); +- if( rc ) return rc; +- if( nPage==0 ){ +- rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0); +- if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK; +- isWal = 0; +- }else{ +- rc = sqlite3OsAccess( +- pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal +- ); +- } ++ rc = sqlite3OsAccess( ++ pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal ++ ); + if( rc==SQLITE_OK ){ + if( isWal ){ +- testcase( sqlite3PcachePagecount(pPager->pPCache)==0 ); +- rc = sqlite3PagerOpenWal(pPager, 0); ++ Pgno nPage; /* Size of the database file */ ++ ++ rc = pagerPagecount(pPager, &nPage); ++ if( rc ) return rc; ++ if( nPage==0 ){ ++ rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0); ++ }else{ ++ testcase( sqlite3PcachePagecount(pPager->pPCache)==0 ); ++ rc = sqlite3PagerOpenWal(pPager, 0); ++ } + }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){ + pPager->journalMode = PAGER_JOURNALMODE_DELETE; + } +@@ -44910,7 +54004,7 @@ static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){ + if( pSavepoint ){ + pDone = sqlite3BitvecCreate(pSavepoint->nOrig); + if( !pDone ){ +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + } + +@@ -45006,12 +54100,21 @@ static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){ + } + + /* +-** Change the maximum number of in-memory pages that are allowed. ++** Change the maximum number of in-memory pages that are allowed ++** before attempting to recycle clean and unused pages. + */ + SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){ + sqlite3PcacheSetCachesize(pPager->pPCache, mxPage); + } + ++/* ++** Change the maximum number of in-memory pages that are allowed ++** before attempting to spill pages to journal. ++*/ ++SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager *pPager, int mxPage){ ++ return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage); ++} ++ + /* + ** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap. + */ +@@ -45022,6 +54125,7 @@ static void pagerFixMaplimit(Pager *pPager){ + sqlite3_int64 sz; + sz = pPager->szMmap; + pPager->bUseFetch = (sz>0); ++ setGetterMethod(pPager); + sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz); + } + #endif +@@ -45048,7 +54152,7 @@ SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){ + ** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness + ** of the database to damage due to OS crashes or power failures by + ** changing the number of syncs()s when writing the journals. +-** There are three levels: ++** There are four levels: + ** + ** OFF sqlite3OsSync() is never called. This is the default + ** for temporary and transient files. +@@ -45068,6 +54172,10 @@ SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){ + ** assurance that the journal will not be corrupted to the + ** point of causing damage to the database during rollback. + ** ++** EXTRA This is like FULL except that is also syncs the directory ++** that contains the rollback journal after the rollback ++** journal is unlinked. ++** + ** The above is for a rollback-journal mode. For WAL mode, OFF continues + ** to mean that no syncs ever occur. NORMAL means that the WAL is synced + ** prior to the start of checkpoint and that the database file is synced +@@ -45075,7 +54183,8 @@ SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){ + ** was written back into the database. But no sync operations occur for + ** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL + ** file is synced following each commit operation, in addition to the +-** syncs associated with NORMAL. ++** syncs associated with NORMAL. There is no difference between FULL ++** and EXTRA for WAL mode. + ** + ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The + ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync +@@ -45094,25 +54203,28 @@ SQLITE_PRIVATE void sqlite3PagerSetFlags( + unsigned pgFlags /* Various flags */ + ){ + unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK; +- assert( level>=1 && level<=3 ); +- pPager->noSync = (level==1 || pPager->tempFile) ?1:0; +- pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0; ++ if( pPager->tempFile ){ ++ pPager->noSync = 1; ++ pPager->fullSync = 0; ++ pPager->extraSync = 0; ++ }else{ ++ pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0; ++ pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0; ++ pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0; ++ } + if( pPager->noSync ){ + pPager->syncFlags = 0; +- pPager->ckptSyncFlags = 0; + }else if( pgFlags & PAGER_FULLFSYNC ){ + pPager->syncFlags = SQLITE_SYNC_FULL; +- pPager->ckptSyncFlags = SQLITE_SYNC_FULL; +- }else if( pgFlags & PAGER_CKPT_FULLFSYNC ){ +- pPager->syncFlags = SQLITE_SYNC_NORMAL; +- pPager->ckptSyncFlags = SQLITE_SYNC_FULL; + }else{ + pPager->syncFlags = SQLITE_SYNC_NORMAL; +- pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL; + } +- pPager->walSyncFlags = pPager->syncFlags; ++ pPager->walSyncFlags = (pPager->syncFlags<<2); + if( pPager->fullSync ){ +- pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS; ++ pPager->walSyncFlags |= pPager->syncFlags; ++ } ++ if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){ ++ pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2); + } + if( pgFlags & PAGER_CACHESPILL ){ + pPager->doNotSpill &= ~SPILLFLAG_OFF; +@@ -45185,20 +54297,18 @@ static int pagerOpentemp( + ** retried. If it returns zero, then the SQLITE_BUSY error is + ** returned to the caller of the pager API function. + */ +-SQLITE_PRIVATE void sqlite3PagerSetBusyhandler( ++SQLITE_PRIVATE void sqlite3PagerSetBusyHandler( + Pager *pPager, /* Pager object */ + int (*xBusyHandler)(void *), /* Pointer to busy-handler function */ + void *pBusyHandlerArg /* Argument to pass to xBusyHandler */ + ){ ++ void **ap; + pPager->xBusyHandler = xBusyHandler; + pPager->pBusyHandlerArg = pBusyHandlerArg; +- +- if( isOpen(pPager->fd) ){ +- void **ap = (void **)&pPager->xBusyHandler; +- assert( ((int(*)(void *))(ap[0]))==xBusyHandler ); +- assert( ap[1]==pBusyHandlerArg ); +- sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap); +- } ++ ap = (void **)&pPager->xBusyHandler; ++ assert( ((int(*)(void *))(ap[0]))==xBusyHandler ); ++ assert( ap[1]==pBusyHandlerArg ); ++ sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap); + } + + /* +@@ -45257,8 +54367,14 @@ SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nR + rc = sqlite3OsFileSize(pPager->fd, &nByte); + } + if( rc==SQLITE_OK ){ +- pNew = (char *)sqlite3PageMalloc(pageSize); +- if( !pNew ) rc = SQLITE_NOMEM; ++ /* 8 bytes of zeroed overrun space is sufficient so that the b-tree ++ * cell header parser will never run off the end of the allocation */ ++ pNew = (char *)sqlite3PageMalloc(pageSize+8); ++ if( !pNew ){ ++ rc = SQLITE_NOMEM_BKPT; ++ }else{ ++ memset(pNew+pageSize, 0, 8); ++ } + } + + if( rc==SQLITE_OK ){ +@@ -45310,7 +54426,10 @@ SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ + pPager->mxPgno = mxPage; + } + assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */ +- assert( pPager->mxPgno>=pPager->dbSize ); /* OP_MaxPgcnt enforces this */ ++ /* assert( pPager->mxPgno>=pPager->dbSize ); */ ++ /* OP_MaxPgcnt ensures that the parameter passed to this function is not ++ ** less than the total number of valid pages in the database. But this ++ ** may be less than Pager.dbSize, and so the assert() above is not valid */ + return pPager->mxPgno; + } + +@@ -45507,6 +54626,7 @@ static int pagerSyncHotJournal(Pager *pPager){ + return rc; + } + ++#if SQLITE_MAX_MMAP_SIZE>0 + /* + ** Obtain a reference to a memory mapped page object for page number pgno. + ** The new object will use the pointer pData, obtained from xFetch(). +@@ -45529,12 +54649,13 @@ static int pagerAcquireMapPage( + *ppPage = p = pPager->pMmapFreelist; + pPager->pMmapFreelist = p->pDirty; + p->pDirty = 0; +- memset(p->pExtra, 0, pPager->nExtra); ++ assert( pPager->nExtra>=8 ); ++ memset(p->pExtra, 0, 8); + }else{ + *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra); + if( p==0 ){ + sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData); +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + p->pExtra = (void *)&p[1]; + p->flags = PGHDR_MMAP; +@@ -45554,6 +54675,7 @@ static int pagerAcquireMapPage( + + return SQLITE_OK; + } ++#endif + + /* + ** Release a reference to page pPg. pPg must have been returned by an +@@ -45581,6 +54703,30 @@ static void pagerFreeMapHdrs(Pager *pPager){ + } + } + ++/* Verify that the database file has not be deleted or renamed out from ++** under the pager. Return SQLITE_OK if the database is still where it ought ++** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error ++** code from sqlite3OsAccess()) if the database has gone missing. ++*/ ++static int databaseIsUnmoved(Pager *pPager){ ++ int bHasMoved = 0; ++ int rc; ++ ++ if( pPager->tempFile ) return SQLITE_OK; ++ if( pPager->dbSize==0 ) return SQLITE_OK; ++ assert( pPager->zFilename && pPager->zFilename[0] ); ++ rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved); ++ if( rc==SQLITE_NOTFOUND ){ ++ /* If the HAS_MOVED file-control is unimplemented, assume that the file ++ ** has not been moved. That is the historical behavior of SQLite: prior to ++ ** version 3.8.3, it never checked */ ++ rc = SQLITE_OK; ++ }else if( rc==SQLITE_OK && bHasMoved ){ ++ rc = SQLITE_READONLY_DBMOVED; ++ } ++ return rc; ++} ++ + + /* + ** Shutdown the page cache. Free all memory and close all files. +@@ -45596,9 +54742,9 @@ static void pagerFreeMapHdrs(Pager *pPager){ + ** a hot journal may be left in the filesystem but no error is returned + ** to the caller. + */ +-SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){ +- u8 *pTmp = (u8 *)pPager->pTmpSpace; +- ++SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){ ++ u8 *pTmp = (u8*)pPager->pTmpSpace; ++ assert( db || pagerUseWal(pPager)==0 ); + assert( assert_pager_state(pPager) ); + disable_simulated_io_errors(); + sqlite3BeginBenignMalloc(); +@@ -45606,8 +54752,17 @@ SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){ + /* pPager->errCode = 0; */ + pPager->exclusiveMode = 0; + #ifndef SQLITE_OMIT_WAL +- sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp); +- pPager->pWal = 0; ++ { ++ u8 *a = 0; ++ assert( db || pPager->pWal==0 ); ++ if( db && 0==(db->flags & SQLITE_NoCkptOnClose) ++ && SQLITE_OK==databaseIsUnmoved(pPager) ++ ){ ++ a = pTmp; ++ } ++ sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a); ++ pPager->pWal = 0; ++ } + #endif + pager_reset(pPager); + if( MEMDB ){ +@@ -45848,8 +55003,9 @@ static int pager_write_pagelist(Pager *pPager, PgHdr *pList){ + + /* This function is only called for rollback pagers in WRITER_DBMOD state. */ + assert( !pagerUseWal(pPager) ); +- assert( pPager->eState==PAGER_WRITER_DBMOD ); ++ assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD ); + assert( pPager->eLock==EXCLUSIVE_LOCK ); ++ assert( isOpen(pPager->fd) || pList->pDirty==0 ); + + /* If the file is a temp-file has not yet been opened, open it now. It + ** is not possible for rc to be other than SQLITE_OK if this branch +@@ -45892,7 +55048,7 @@ static int pager_write_pagelist(Pager *pPager, PgHdr *pList){ + if( pList->pgno==1 ) pager_write_changecounter(pList); + + /* Encode the database */ +- CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData); ++ CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM_BKPT, pData); + + /* Write out the page data. */ + rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset); +@@ -45937,19 +55093,20 @@ static int pager_write_pagelist(Pager *pPager, PgHdr *pList){ + static int openSubJournal(Pager *pPager){ + int rc = SQLITE_OK; + if( !isOpen(pPager->sjfd) ){ ++ const int flags = SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_READWRITE ++ | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE ++ | SQLITE_OPEN_DELETEONCLOSE; ++ int nStmtSpill = sqlite3Config.nStmtSpill; + if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){ +- sqlite3MemJournalOpen(pPager->sjfd); +- }else{ +- rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL); ++ nStmtSpill = -1; + } ++ rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill); + } + return rc; + } + + /* + ** Append a record of the current state of page pPg to the sub-journal. +-** It is the callers responsibility to use subjRequiresPage() to check +-** that it is really required before calling this function. + ** + ** If successful, set the bit corresponding to pPg->pgno in the bitvecs + ** for all open savepoints before returning. +@@ -45980,8 +55137,13 @@ static int subjournalPage(PgHdr *pPg){ + void *pData = pPg->pData; + i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize); + char *pData2; +- +- CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2); ++ ++#if SQLITE_HAS_CODEC ++ if( !pPager->subjInMemory ){ ++ CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2); ++ }else ++#endif ++ pData2 = pData; + PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno)); + rc = write32bits(pPager->sjfd, offset, pPg->pgno); + if( rc==SQLITE_OK ){ +@@ -45996,6 +55158,13 @@ static int subjournalPage(PgHdr *pPg){ + } + return rc; + } ++static int subjournalPageIfRequired(PgHdr *pPg){ ++ if( subjRequiresPage(pPg) ){ ++ return subjournalPage(pPg); ++ }else{ ++ return SQLITE_OK; ++ } ++} + + /* + ** This function is called by the pcache layer when it has reached some +@@ -46050,16 +55219,22 @@ static int pagerStress(void *p, PgHdr *pPg){ + return SQLITE_OK; + } + ++ pPager->aStat[PAGER_STAT_SPILL]++; + pPg->pDirty = 0; + if( pagerUseWal(pPager) ){ + /* Write a single frame for this page to the log. */ +- if( subjRequiresPage(pPg) ){ +- rc = subjournalPage(pPg); +- } ++ rc = subjournalPageIfRequired(pPg); + if( rc==SQLITE_OK ){ + rc = pagerWalFrames(pPager, pPg, 0, 0); + } + }else{ ++ ++#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE ++ if( pPager->tempFile==0 ){ ++ rc = sqlite3JournalCreate(pPager->jfd); ++ if( rc!=SQLITE_OK ) return pager_error(pPager, rc); ++ } ++#endif + + /* Sync the journal file if required. */ + if( pPg->flags&PGHDR_NEED_SYNC +@@ -46068,39 +55243,6 @@ static int pagerStress(void *p, PgHdr *pPg){ + rc = syncJournal(pPager, 1); + } + +- /* If the page number of this page is larger than the current size of +- ** the database image, it may need to be written to the sub-journal. +- ** This is because the call to pager_write_pagelist() below will not +- ** actually write data to the file in this case. +- ** +- ** Consider the following sequence of events: +- ** +- ** BEGIN; +- ** +- ** +- ** SAVEPOINT sp; +- ** +- ** pagerStress(page X) +- ** ROLLBACK TO sp; +- ** +- ** If (X>Y), then when pagerStress is called page X will not be written +- ** out to the database file, but will be dropped from the cache. Then, +- ** following the "ROLLBACK TO sp" statement, reading page X will read +- ** data from the database file. This will be the copy of page X as it +- ** was when the transaction started, not as it was when "SAVEPOINT sp" +- ** was executed. +- ** +- ** The solution is to write the current data for page X into the +- ** sub-journal file now (if it is not already there), so that it will +- ** be restored to its current value when the "ROLLBACK TO sp" is +- ** executed. +- */ +- if( NEVER( +- rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg) +- ) ){ +- rc = subjournalPage(pPg); +- } +- + /* Write the contents of the page out to the database file. */ + if( rc==SQLITE_OK ){ + assert( (pPg->flags&PGHDR_NEED_SYNC)==0 ); +@@ -46117,6 +55259,25 @@ static int pagerStress(void *p, PgHdr *pPg){ + return pager_error(pPager, rc); + } + ++/* ++** Flush all unreferenced dirty pages to disk. ++*/ ++SQLITE_PRIVATE int sqlite3PagerFlush(Pager *pPager){ ++ int rc = pPager->errCode; ++ if( !MEMDB ){ ++ PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache); ++ assert( assert_pager_state(pPager) ); ++ while( rc==SQLITE_OK && pList ){ ++ PgHdr *pNext = pList->pDirty; ++ if( pList->nRef==0 ){ ++ rc = pagerStress((void*)pPager, pList); ++ } ++ pList = pNext; ++ } ++ } ++ ++ return rc; ++} + + /* + ** Allocate and initialize a new Pager object and put a pointer to it +@@ -46132,7 +55293,9 @@ static int pagerStress(void *p, PgHdr *pPg){ + ** + ** The nExtra parameter specifies the number of bytes of space allocated + ** along with each page reference. This space is available to the user +-** via the sqlite3PagerGetExtra() API. ++** via the sqlite3PagerGetExtra() API. When a new page is allocated, the ++** first 8 bytes of this space are zeroed but the remainder is uninitialized. ++** (The extra space is used by btree as the MemPage object.) + ** + ** The flags argument is used to specify properties that affect the + ** operation of the pager. It should be passed some bitwise combination +@@ -46162,6 +55325,11 @@ SQLITE_PRIVATE int sqlite3PagerOpen( + int rc = SQLITE_OK; /* Return code */ + int tempFile = 0; /* True for temp files (incl. in-memory files) */ + int memDb = 0; /* True if this is an in-memory file */ ++#ifdef SQLITE_ENABLE_DESERIALIZE ++ int memJM = 0; /* Memory journal mode */ ++#else ++# define memJM 0 ++#endif + int readOnly = 0; /* True if this is a read-only file */ + int journalFileSize; /* Bytes to allocate for each journal fd */ + char *zPathname = 0; /* Full path to database file */ +@@ -46173,18 +55341,8 @@ SQLITE_PRIVATE int sqlite3PagerOpen( + int nUri = 0; /* Number of bytes of URI args at *zUri */ + + /* Figure out how much space is required for each journal file-handle +- ** (there are two of them, the main journal and the sub-journal). This +- ** is the maximum space required for an in-memory journal file handle +- ** and a regular journal file-handle. Note that a "regular journal-handle" +- ** may be a wrapper capable of caching the first portion of the journal +- ** file in memory to implement the atomic-write optimization (see +- ** source file journal.c). +- */ +- if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){ +- journalFileSize = ROUND8(sqlite3JournalSize(pVfs)); +- }else{ +- journalFileSize = ROUND8(sqlite3MemJournalSize()); +- } ++ ** (there are two of them, the main journal and the sub-journal). */ ++ journalFileSize = ROUND8(sqlite3JournalSize(pVfs)); + + /* Set the output variable to NULL in case an error occurs. */ + *ppPager = 0; +@@ -46194,7 +55352,7 @@ SQLITE_PRIVATE int sqlite3PagerOpen( + memDb = 1; + if( zFilename && zFilename[0] ){ + zPathname = sqlite3DbStrDup(0, zFilename); +- if( zPathname==0 ) return SQLITE_NOMEM; ++ if( zPathname==0 ) return SQLITE_NOMEM_BKPT; + nPathname = sqlite3Strlen30(zPathname); + zFilename = 0; + } +@@ -46210,7 +55368,7 @@ SQLITE_PRIVATE int sqlite3PagerOpen( + nPathname = pVfs->mxPathname+1; + zPathname = sqlite3DbMallocRaw(0, nPathname*2); + if( zPathname==0 ){ +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */ + rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname); +@@ -46263,7 +55421,7 @@ SQLITE_PRIVATE int sqlite3PagerOpen( + assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) ); + if( !pPtr ){ + sqlite3DbFree(0, zPathname); +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + pPager = (Pager*)(pPtr); + pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager))); +@@ -46299,7 +55457,10 @@ SQLITE_PRIVATE int sqlite3PagerOpen( + int fout = 0; /* VFS flags returned by xOpen() */ + rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout); + assert( !memDb ); +- readOnly = (fout&SQLITE_OPEN_READONLY); ++#ifdef SQLITE_ENABLE_DESERIALIZE ++ memJM = (fout&SQLITE_OPEN_MEMORY)!=0; ++#endif ++ readOnly = (fout&SQLITE_OPEN_READONLY)!=0; + + /* If the file was successfully opened for read/write access, + ** choose a default page size in case we have to create the +@@ -46356,7 +55517,7 @@ SQLITE_PRIVATE int sqlite3PagerOpen( + act_like_temp_file: + tempFile = 1; + pPager->eState = PAGER_READER; /* Pretend we already have a lock */ +- pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE locking mode */ ++ pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */ + pPager->noLock = 1; /* Do no locking */ + readOnly = (vfsFlags&SQLITE_OPEN_READONLY); + } +@@ -46372,10 +55533,10 @@ act_like_temp_file: + + /* Initialize the PCache object. */ + if( rc==SQLITE_OK ){ +- assert( nExtra<1000 ); + nExtra = ROUND8(nExtra); ++ assert( nExtra>=8 && nExtra<1000 ); + rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb, +- !memDb?pagerStress:0, (void *)pPager, pPager->pPCache); ++ !memDb?pagerStress:0, (void *)pPager, pPager->pPCache); + } + + /* If an error occurred above, free the Pager structure and close the file. +@@ -46412,14 +55573,14 @@ act_like_temp_file: + pPager->noSync = pPager->tempFile; + if( pPager->noSync ){ + assert( pPager->fullSync==0 ); ++ assert( pPager->extraSync==0 ); + assert( pPager->syncFlags==0 ); + assert( pPager->walSyncFlags==0 ); +- assert( pPager->ckptSyncFlags==0 ); + }else{ + pPager->fullSync = 1; ++ pPager->extraSync = 0; + pPager->syncFlags = SQLITE_SYNC_NORMAL; +- pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS; +- pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL; ++ pPager->walSyncFlags = SQLITE_SYNC_NORMAL | (SQLITE_SYNC_NORMAL<<2); + } + /* pPager->pFirst = 0; */ + /* pPager->pFirstSynced = 0; */ +@@ -46430,12 +55591,13 @@ act_like_temp_file: + setSectorSize(pPager); + if( !useJournal ){ + pPager->journalMode = PAGER_JOURNALMODE_OFF; +- }else if( memDb ){ ++ }else if( memDb || memJM ){ + pPager->journalMode = PAGER_JOURNALMODE_MEMORY; + } + /* pPager->xBusyHandler = 0; */ + /* pPager->pBusyHandlerArg = 0; */ + pPager->xReiniter = xReinit; ++ setGetterMethod(pPager); + /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ + /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */ + +@@ -46444,30 +55606,6 @@ act_like_temp_file: + } + + +-/* Verify that the database file has not be deleted or renamed out from +-** under the pager. Return SQLITE_OK if the database is still were it ought +-** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error +-** code from sqlite3OsAccess()) if the database has gone missing. +-*/ +-static int databaseIsUnmoved(Pager *pPager){ +- int bHasMoved = 0; +- int rc; +- +- if( pPager->tempFile ) return SQLITE_OK; +- if( pPager->dbSize==0 ) return SQLITE_OK; +- assert( pPager->zFilename && pPager->zFilename[0] ); +- rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved); +- if( rc==SQLITE_NOTFOUND ){ +- /* If the HAS_MOVED file-control is unimplemented, assume that the file +- ** has not been moved. That is the historical behavior of SQLite: prior to +- ** version 3.8.3, it never checked */ +- rc = SQLITE_OK; +- }else if( rc==SQLITE_OK && bHasMoved ){ +- rc = SQLITE_READONLY_DBMOVED; +- } +- return rc; +-} +- + + /* + ** This function is called after transitioning from PAGER_UNLOCK to +@@ -46533,6 +55671,7 @@ static int hasHotJournal(Pager *pPager, int *pExists){ + if( rc==SQLITE_OK && !locked ){ + Pgno nPage; /* Number of pages in database file */ + ++ assert( pPager->tempFile==0 ); + rc = pagerPagecount(pPager, &nPage); + if( rc==SQLITE_OK ){ + /* If the database is zero pages in size, that means that either (1) the +@@ -46594,7 +55733,7 @@ static int hasHotJournal(Pager *pPager, int *pExists){ + + /* + ** This function is called to obtain a shared lock on the database file. +-** It is illegal to call sqlite3PagerAcquire() until after this function ++** It is illegal to call sqlite3PagerGet() until after this function + ** has been successfully called. If a shared-lock is already held when + ** this function is called, it is a no-op. + ** +@@ -46625,17 +55764,17 @@ SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){ + /* This routine is only called from b-tree and only when there are no + ** outstanding pages. This implies that the pager state should either + ** be OPEN or READER. READER is only possible if the pager is or was in +- ** exclusive access mode. +- */ ++ ** exclusive access mode. */ + assert( sqlite3PcacheRefCount(pPager->pPCache)==0 ); + assert( assert_pager_state(pPager) ); + assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER ); +- if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; } ++ assert( pPager->errCode==SQLITE_OK ); + + if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){ + int bHotJournal = 1; /* True if there exists a hot journal-file */ + + assert( !MEMDB ); ++ assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK ); + + rc = pager_wait_on_lock(pPager, SHARED_LOCK); + if( rc!=SQLITE_OK ){ +@@ -46721,7 +55860,7 @@ SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){ + assert( rc==SQLITE_OK ); + rc = pagerSyncHotJournal(pPager); + if( rc==SQLITE_OK ){ +- rc = pager_playback(pPager, 1); ++ rc = pager_playback(pPager, !pPager->tempFile); + pPager->eState = PAGER_OPEN; + } + }else if( !pPager->exclusiveMode ){ +@@ -46755,14 +55894,14 @@ SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){ + ); + } + +- if( !pPager->tempFile && pPager->hasBeenUsed ){ ++ if( !pPager->tempFile && pPager->hasHeldSharedLock ){ + /* The shared-lock has just been acquired then check to + ** see if the database has been modified. If the database has changed, +- ** flush the cache. The pPager->hasBeenUsed flag prevents this from ++ ** flush the cache. The hasHeldSharedLock flag prevents this from + ** occurring on the very first access to a file, in order to save a + ** single unnecessary sqlite3OsRead() call at the start-up. + ** +- ** Database changes is detected by looking at 15 bytes beginning ++ ** Database changes are detected by looking at 15 bytes beginning + ** at offset 24 into the file. The first 4 of these 16 bytes are + ** a 32-bit counter that is incremented with each change. The + ** other bytes change randomly with each file change when +@@ -46772,19 +55911,14 @@ SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){ + ** detected. The chance of an undetected change is so small that + ** it can be neglected. + */ +- Pgno nPage = 0; + char dbFileVers[sizeof(pPager->dbFileVers)]; + +- rc = pagerPagecount(pPager, &nPage); +- if( rc ) goto failed; +- +- if( nPage>0 ){ +- IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); +- rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); +- if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){ ++ IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); ++ rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); ++ if( rc!=SQLITE_OK ){ ++ if( rc!=SQLITE_IOERR_SHORT_READ ){ + goto failed; + } +- }else{ + memset(dbFileVers, 0, sizeof(dbFileVers)); + } + +@@ -46817,7 +55951,7 @@ SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){ + rc = pagerBeginReadTransaction(pPager); + } + +- if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){ ++ if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){ + rc = pagerPagecount(pPager, &pPager->dbSize); + } + +@@ -46828,6 +55962,7 @@ SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){ + assert( pPager->eState==PAGER_OPEN ); + }else{ + pPager->eState = PAGER_READER; ++ pPager->hasHeldSharedLock = 1; + } + return rc; + } +@@ -46841,16 +55976,24 @@ SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){ + ** nothing to rollback, so this routine is a no-op. + */ + static void pagerUnlockIfUnused(Pager *pPager){ +- if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){ ++ if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){ ++ assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */ + pagerUnlockAndRollback(pPager); + } + } + + /* +-** Acquire a reference to page number pgno in pager pPager (a page +-** reference has type DbPage*). If the requested reference is ++** The page getter methods each try to acquire a reference to a ++** page with page number pgno. If the requested reference is + ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned. + ** ++** There are different implementations of the getter method depending ++** on the current state of the pager. ++** ++** getPageNormal() -- The normal getter ++** getPageError() -- Used if the pager is in an error state ++** getPageMmap() -- Used if memory-mapped I/O is enabled ++** + ** If the requested page is already in the cache, it is returned. + ** Otherwise, a new page object is allocated and populated with data + ** read from the database file. In some cases, the pcache module may +@@ -46862,14 +56005,14 @@ static void pagerUnlockIfUnused(Pager *pPager){ + ** already in the cache when this function is called, then the extra + ** data is left as it was when the page object was last used. + ** +-** If the database image is smaller than the requested page or if a +-** non-zero value is passed as the noContent parameter and the ++** If the database image is smaller than the requested page or if ++** the flags parameter contains the PAGER_GET_NOCONTENT bit and the + ** requested page is not already stored in the cache, then no + ** actual disk read occurs. In this case the memory image of the + ** page is initialized to all zeros. + ** +-** If noContent is true, it means that we do not care about the contents +-** of the page. This occurs in two scenarios: ++** If PAGER_GET_NOCONTENT is true, it means that we do not care about ++** the contents of the page. This occurs in two scenarios: + ** + ** a) When reading a free-list leaf page from the database, and + ** +@@ -46877,8 +56020,8 @@ static void pagerUnlockIfUnused(Pager *pPager){ + ** a new page into the cache to be filled with the data read + ** from the savepoint journal. + ** +-** If noContent is true, then the data returned is zeroed instead of +-** being read from the database. Additionally, the bits corresponding ++** If PAGER_GET_NOCONTENT is true, then the data returned is zeroed instead ++** of being read from the database. Additionally, the bits corresponding + ** to pgno in Pager.pInJournal (bitvec of pages already written to the + ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open + ** savepoints are set. This means if the page is made writable at any +@@ -46896,97 +56039,40 @@ static void pagerUnlockIfUnused(Pager *pPager){ + ** Since Lookup() never goes to disk, it never has to deal with locks + ** or journal files. + */ +-SQLITE_PRIVATE int sqlite3PagerAcquire( ++static int getPageNormal( + Pager *pPager, /* The pager open on the database file */ + Pgno pgno, /* Page number to fetch */ + DbPage **ppPage, /* Write a pointer to the page here */ + int flags /* PAGER_GET_XXX flags */ + ){ + int rc = SQLITE_OK; +- PgHdr *pPg = 0; +- u32 iFrame = 0; /* Frame to read from WAL file */ +- const int noContent = (flags & PAGER_GET_NOCONTENT); +- +- /* It is acceptable to use a read-only (mmap) page for any page except +- ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY +- ** flag was specified by the caller. And so long as the db is not a +- ** temporary or in-memory database. */ +- const int bMmapOk = (pgno!=1 && USEFETCH(pPager) +- && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY)) +-#ifdef SQLITE_HAS_CODEC +- && pPager->xCodec==0 +-#endif +- ); ++ PgHdr *pPg; ++ u8 noContent; /* True if PAGER_GET_NOCONTENT is set */ ++ sqlite3_pcache_page *pBase; + ++ assert( pPager->errCode==SQLITE_OK ); + assert( pPager->eState>=PAGER_READER ); + assert( assert_pager_state(pPager) ); +- assert( noContent==0 || bMmapOk==0 ); +- +- if( pgno==0 ){ +- return SQLITE_CORRUPT_BKPT; +- } +- pPager->hasBeenUsed = 1; +- +- /* If the pager is in the error state, return an error immediately. +- ** Otherwise, request the page from the PCache layer. */ +- if( pPager->errCode!=SQLITE_OK ){ +- rc = pPager->errCode; +- }else{ +- if( bMmapOk && pagerUseWal(pPager) ){ +- rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame); +- if( rc!=SQLITE_OK ) goto pager_acquire_err; +- } +- +- if( bMmapOk && iFrame==0 ){ +- void *pData = 0; +- +- rc = sqlite3OsFetch(pPager->fd, +- (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData +- ); +- +- if( rc==SQLITE_OK && pData ){ +- if( pPager->eState>PAGER_READER ){ +- pPg = sqlite3PagerLookup(pPager, pgno); +- } +- if( pPg==0 ){ +- rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg); +- }else{ +- sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData); +- } +- if( pPg ){ +- assert( rc==SQLITE_OK ); +- *ppPage = pPg; +- return SQLITE_OK; +- } +- } +- if( rc!=SQLITE_OK ){ +- goto pager_acquire_err; +- } +- } ++ assert( pPager->hasHeldSharedLock==1 ); + +- { +- sqlite3_pcache_page *pBase; +- pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3); +- if( pBase==0 ){ +- rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase); +- if( rc!=SQLITE_OK ) goto pager_acquire_err; +- } +- pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase); +- if( pPg==0 ) rc = SQLITE_NOMEM; +- } +- } +- +- if( rc!=SQLITE_OK ){ +- /* Either the call to sqlite3PcacheFetch() returned an error or the +- ** pager was already in the error-state when this function was called. +- ** Set pPg to 0 and jump to the exception handler. */ ++ if( pgno==0 ) return SQLITE_CORRUPT_BKPT; ++ pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3); ++ if( pBase==0 ){ + pPg = 0; +- goto pager_acquire_err; ++ rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase); ++ if( rc!=SQLITE_OK ) goto pager_acquire_err; ++ if( pBase==0 ){ ++ rc = SQLITE_NOMEM_BKPT; ++ goto pager_acquire_err; ++ } + } +- assert( (*ppPage)->pgno==pgno ); +- assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 ); ++ pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase); ++ assert( pPg==(*ppPage) ); ++ assert( pPg->pgno==pgno ); ++ assert( pPg->pPager==pPager || pPg->pPager==0 ); + +- if( (*ppPage)->pPager && !noContent ){ ++ noContent = (flags & PAGER_GET_NOCONTENT)!=0; ++ if( pPg->pPager && !noContent ){ + /* In this case the pcache already contains an initialized copy of + ** the page. Return without further ado. */ + assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) ); +@@ -46995,19 +56081,20 @@ SQLITE_PRIVATE int sqlite3PagerAcquire( + + }else{ + /* The pager cache has created a new page. Its content needs to +- ** be initialized. */ +- +- pPg = *ppPage; +- pPg->pPager = pPager; +- +- /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page +- ** number greater than this, or the unused locking-page, is requested. */ ++ ** be initialized. But first some error checks: ++ ** ++ ** (1) The maximum page number is 2^31 ++ ** (2) Never try to fetch the locking page ++ */ + if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){ + rc = SQLITE_CORRUPT_BKPT; + goto pager_acquire_err; + } + +- if( MEMDB || pPager->dbSize fd) ){ ++ pPg->pPager = pPager; ++ ++ assert( !isOpen(pPager->fd) || !MEMDB ); ++ if( !isOpen(pPager->fd) || pPager->dbSize pPager->mxPgno ){ + rc = SQLITE_FULL; + goto pager_acquire_err; +@@ -47031,20 +56118,15 @@ SQLITE_PRIVATE int sqlite3PagerAcquire( + memset(pPg->pData, 0, pPager->pageSize); + IOTRACE(("ZERO %p %d\n", pPager, pgno)); + }else{ +- if( pagerUseWal(pPager) && bMmapOk==0 ){ +- rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame); +- if( rc!=SQLITE_OK ) goto pager_acquire_err; +- } + assert( pPg->pPager==pPager ); + pPager->aStat[PAGER_STAT_MISS]++; +- rc = readDbPage(pPg, iFrame); ++ rc = readDbPage(pPg); + if( rc!=SQLITE_OK ){ + goto pager_acquire_err; + } + } + pager_set_pagehash(pPg); + } +- + return SQLITE_OK; + + pager_acquire_err: +@@ -47053,11 +56135,109 @@ pager_acquire_err: + sqlite3PcacheDrop(pPg); + } + pagerUnlockIfUnused(pPager); +- + *ppPage = 0; + return rc; + } + ++#if SQLITE_MAX_MMAP_SIZE>0 ++/* The page getter for when memory-mapped I/O is enabled */ ++static int getPageMMap( ++ Pager *pPager, /* The pager open on the database file */ ++ Pgno pgno, /* Page number to fetch */ ++ DbPage **ppPage, /* Write a pointer to the page here */ ++ int flags /* PAGER_GET_XXX flags */ ++){ ++ int rc = SQLITE_OK; ++ PgHdr *pPg = 0; ++ u32 iFrame = 0; /* Frame to read from WAL file */ ++ ++ /* It is acceptable to use a read-only (mmap) page for any page except ++ ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY ++ ** flag was specified by the caller. And so long as the db is not a ++ ** temporary or in-memory database. */ ++ const int bMmapOk = (pgno>1 ++ && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY)) ++ ); ++ ++ assert( USEFETCH(pPager) ); ++#ifdef SQLITE_HAS_CODEC ++ assert( pPager->xCodec==0 ); ++#endif ++ ++ /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here ++ ** allows the compiler optimizer to reuse the results of the "pgno>1" ++ ** test in the previous statement, and avoid testing pgno==0 in the ++ ** common case where pgno is large. */ ++ if( pgno<=1 && pgno==0 ){ ++ return SQLITE_CORRUPT_BKPT; ++ } ++ assert( pPager->eState>=PAGER_READER ); ++ assert( assert_pager_state(pPager) ); ++ assert( pPager->hasHeldSharedLock==1 ); ++ assert( pPager->errCode==SQLITE_OK ); ++ ++ if( bMmapOk && pagerUseWal(pPager) ){ ++ rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame); ++ if( rc!=SQLITE_OK ){ ++ *ppPage = 0; ++ return rc; ++ } ++ } ++ if( bMmapOk && iFrame==0 ){ ++ void *pData = 0; ++ rc = sqlite3OsFetch(pPager->fd, ++ (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData ++ ); ++ if( rc==SQLITE_OK && pData ){ ++ if( pPager->eState>PAGER_READER || pPager->tempFile ){ ++ pPg = sqlite3PagerLookup(pPager, pgno); ++ } ++ if( pPg==0 ){ ++ rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg); ++ }else{ ++ sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData); ++ } ++ if( pPg ){ ++ assert( rc==SQLITE_OK ); ++ *ppPage = pPg; ++ return SQLITE_OK; ++ } ++ } ++ if( rc!=SQLITE_OK ){ ++ *ppPage = 0; ++ return rc; ++ } ++ } ++ return getPageNormal(pPager, pgno, ppPage, flags); ++} ++#endif /* SQLITE_MAX_MMAP_SIZE>0 */ ++ ++/* The page getter method for when the pager is an error state */ ++static int getPageError( ++ Pager *pPager, /* The pager open on the database file */ ++ Pgno pgno, /* Page number to fetch */ ++ DbPage **ppPage, /* Write a pointer to the page here */ ++ int flags /* PAGER_GET_XXX flags */ ++){ ++ UNUSED_PARAMETER(pgno); ++ UNUSED_PARAMETER(flags); ++ assert( pPager->errCode!=SQLITE_OK ); ++ *ppPage = 0; ++ return pPager->errCode; ++} ++ ++ ++/* Dispatch all page fetch requests to the appropriate getter method. ++*/ ++SQLITE_PRIVATE int sqlite3PagerGet( ++ Pager *pPager, /* The pager open on the database file */ ++ Pgno pgno, /* Page number to fetch */ ++ DbPage **ppPage, /* Write a pointer to the page here */ ++ int flags /* PAGER_GET_XXX flags */ ++){ ++ return pPager->xGet(pPager, pgno, ppPage, flags); ++} ++ + /* + ** Acquire a page if it is already in the in-memory cache. Do + ** not read the page from disk. Return a pointer to the page, +@@ -47075,32 +56255,48 @@ SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){ + assert( pgno!=0 ); + assert( pPager->pPCache!=0 ); + pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0); +- assert( pPage==0 || pPager->hasBeenUsed ); ++ assert( pPage==0 || pPager->hasHeldSharedLock ); ++ if( pPage==0 ) return 0; + return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage); + } + + /* + ** Release a page reference. + ** +-** If the number of references to the page drop to zero, then the +-** page is added to the LRU list. When all references to all pages +-** are released, a rollback occurs and the lock on the database is +-** removed. ++** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be ++** used if we know that the page being released is not the last page. ++** The btree layer always holds page1 open until the end, so these first ++** to routines can be used to release any page other than BtShared.pPage1. ++** ++** Use sqlite3PagerUnrefPageOne() to release page1. This latter routine ++** checks the total number of outstanding pages and if the number of ++** pages reaches zero it drops the database lock. + */ + SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){ +- Pager *pPager; ++ TESTONLY( Pager *pPager = pPg->pPager; ) + assert( pPg!=0 ); +- pPager = pPg->pPager; + if( pPg->flags & PGHDR_MMAP ){ ++ assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */ + pagerReleaseMapPage(pPg); + }else{ + sqlite3PcacheRelease(pPg); + } +- pagerUnlockIfUnused(pPager); ++ /* Do not use this routine to release the last reference to page1 */ ++ assert( sqlite3PcacheRefCount(pPager->pPCache)>0 ); + } + SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){ + if( pPg ) sqlite3PagerUnrefNotNull(pPg); + } ++SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage *pPg){ ++ Pager *pPager; ++ assert( pPg!=0 ); ++ assert( pPg->pgno==1 ); ++ assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */ ++ pPager = pPg->pPager; ++ sqlite3PagerResetLockTimeout(pPager); ++ sqlite3PcacheRelease(pPg); ++ pagerUnlockIfUnused(pPager); ++} + + /* + ** This function is called at the start of every write transaction. +@@ -47140,7 +56336,7 @@ static int pager_open_journal(Pager *pPager){ + if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ + pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize); + if( pPager->pInJournal==0 ){ +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + + /* Open the journal file if it is not already open. */ +@@ -47148,24 +56344,24 @@ static int pager_open_journal(Pager *pPager){ + if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){ + sqlite3MemJournalOpen(pPager->jfd); + }else{ +- const int flags = /* VFS flags to open journal file */ +- SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| +- (pPager->tempFile ? +- (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL): +- (SQLITE_OPEN_MAIN_JOURNAL) +- ); ++ int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; ++ int nSpill; + ++ if( pPager->tempFile ){ ++ flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL); ++ nSpill = sqlite3Config.nStmtSpill; ++ }else{ ++ flags |= SQLITE_OPEN_MAIN_JOURNAL; ++ nSpill = jrnlBufferSize(pPager); ++ } ++ + /* Verify that the database still has the same name as it did when + ** it was originally opened. */ + rc = databaseIsUnmoved(pPager); + if( rc==SQLITE_OK ){ +-#ifdef SQLITE_ENABLE_ATOMIC_WRITE +- rc = sqlite3JournalOpen( +- pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager) ++ rc = sqlite3JournalOpen ( ++ pVfs, pPager->zJournal, pPager->jfd, flags, nSpill + ); +-#else +- rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0); +-#endif + } + } + assert( rc!=SQLITE_OK || isOpen(pPager->jfd) ); +@@ -47232,7 +56428,7 @@ SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory + if( rc!=SQLITE_OK ){ + return rc; + } +- sqlite3WalExclusiveMode(pPager->pWal, 1); ++ (void)sqlite3WalExclusiveMode(pPager->pWal, 1); + } + + /* Grab the write lock on the log file. If successful, upgrade to +@@ -47279,6 +56475,59 @@ SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory + return rc; + } + ++/* ++** Write page pPg onto the end of the rollback journal. ++*/ ++static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){ ++ Pager *pPager = pPg->pPager; ++ int rc; ++ u32 cksum; ++ char *pData2; ++ i64 iOff = pPager->journalOff; ++ ++ /* We should never write to the journal file the page that ++ ** contains the database locks. The following assert verifies ++ ** that we do not. */ ++ assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); ++ ++ assert( pPager->journalHdr<=pPager->journalOff ); ++ CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2); ++ cksum = pager_cksum(pPager, (u8*)pData2); ++ ++ /* Even if an IO or diskfull error occurs while journalling the ++ ** page in the block above, set the need-sync flag for the page. ++ ** Otherwise, when the transaction is rolled back, the logic in ++ ** playback_one_page() will think that the page needs to be restored ++ ** in the database file. And if an IO error occurs while doing so, ++ ** then corruption may follow. ++ */ ++ pPg->flags |= PGHDR_NEED_SYNC; ++ ++ rc = write32bits(pPager->jfd, iOff, pPg->pgno); ++ if( rc!=SQLITE_OK ) return rc; ++ rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4); ++ if( rc!=SQLITE_OK ) return rc; ++ rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum); ++ if( rc!=SQLITE_OK ) return rc; ++ ++ IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, ++ pPager->journalOff, pPager->pageSize)); ++ PAGER_INCR(sqlite3_pager_writej_count); ++ PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n", ++ PAGERID(pPager), pPg->pgno, ++ ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg))); ++ ++ pPager->journalOff += 8 + pPager->pageSize; ++ pPager->nRec++; ++ assert( pPager->pInJournal!=0 ); ++ rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); ++ testcase( rc==SQLITE_NOMEM ); ++ assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); ++ rc |= addToSavepointBitvecs(pPager, pPg->pgno); ++ assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); ++ return rc; ++} ++ + /* + ** Mark a single data page as writeable. The page is written into the + ** main journal or sub-journal as required. If the page is written into +@@ -47289,7 +56538,6 @@ SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory + static int pager_write(PgHdr *pPg){ + Pager *pPager = pPg->pPager; + int rc = SQLITE_OK; +- int inJournal; + + /* This routine is not called unless a write-transaction has already + ** been started. The journal file may or may not be open at this point. +@@ -47302,7 +56550,6 @@ static int pager_write(PgHdr *pPg){ + assert( assert_pager_state(pPager) ); + assert( pPager->errCode==0 ); + assert( pPager->readOnly==0 ); +- + CHECK_PAGE(pPg); + + /* The journal file needs to be opened. Higher level routines have already +@@ -47321,91 +56568,48 @@ static int pager_write(PgHdr *pPg){ + assert( pPager->eState>=PAGER_WRITER_CACHEMOD ); + assert( assert_pager_state(pPager) ); + +- /* Mark the page as dirty. If the page has already been written +- ** to the journal then we can return right away. +- */ ++ /* Mark the page that is about to be modified as dirty. */ + sqlite3PcacheMakeDirty(pPg); +- inJournal = pageInJournal(pPager, pPg); +- if( inJournal && (pPager->nSavepoint==0 || !subjRequiresPage(pPg)) ){ +- assert( !pagerUseWal(pPager) ); +- }else{ +- +- /* The transaction journal now exists and we have a RESERVED or an +- ** EXCLUSIVE lock on the main database file. Write the current page to +- ** the transaction journal if it is not there already. +- */ +- if( !inJournal && !pagerUseWal(pPager) ){ +- assert( pagerUseWal(pPager)==0 ); +- if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){ +- u32 cksum; +- char *pData2; +- i64 iOff = pPager->journalOff; +- +- /* We should never write to the journal file the page that +- ** contains the database locks. The following assert verifies +- ** that we do not. */ +- assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); +- +- assert( pPager->journalHdr<=pPager->journalOff ); +- CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2); +- cksum = pager_cksum(pPager, (u8*)pData2); +- +- /* Even if an IO or diskfull error occurs while journalling the +- ** page in the block above, set the need-sync flag for the page. +- ** Otherwise, when the transaction is rolled back, the logic in +- ** playback_one_page() will think that the page needs to be restored +- ** in the database file. And if an IO error occurs while doing so, +- ** then corruption may follow. +- */ +- pPg->flags |= PGHDR_NEED_SYNC; + +- rc = write32bits(pPager->jfd, iOff, pPg->pgno); +- if( rc!=SQLITE_OK ) return rc; +- rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4); +- if( rc!=SQLITE_OK ) return rc; +- rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum); +- if( rc!=SQLITE_OK ) return rc; +- +- IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, +- pPager->journalOff, pPager->pageSize)); +- PAGER_INCR(sqlite3_pager_writej_count); +- PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n", +- PAGERID(pPager), pPg->pgno, +- ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg))); +- +- pPager->journalOff += 8 + pPager->pageSize; +- pPager->nRec++; +- assert( pPager->pInJournal!=0 ); +- rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); +- testcase( rc==SQLITE_NOMEM ); +- assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); +- rc |= addToSavepointBitvecs(pPager, pPg->pgno); +- if( rc!=SQLITE_OK ){ +- assert( rc==SQLITE_NOMEM ); +- return rc; +- } +- }else{ +- if( pPager->eState!=PAGER_WRITER_DBMOD ){ +- pPg->flags |= PGHDR_NEED_SYNC; +- } +- PAGERTRACE(("APPEND %d page %d needSync=%d\n", +- PAGERID(pPager), pPg->pgno, +- ((pPg->flags&PGHDR_NEED_SYNC)?1:0))); ++ /* If a rollback journal is in use, them make sure the page that is about ++ ** to change is in the rollback journal, or if the page is a new page off ++ ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC. ++ */ ++ assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) ); ++ if( pPager->pInJournal!=0 ++ && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0 ++ ){ ++ assert( pagerUseWal(pPager)==0 ); ++ if( pPg->pgno<=pPager->dbOrigSize ){ ++ rc = pagerAddPageToRollbackJournal(pPg); ++ if( rc!=SQLITE_OK ){ ++ return rc; + } +- } +- +- /* If the statement journal is open and the page is not in it, +- ** then write the current page to the statement journal. Note that +- ** the statement journal format differs from the standard journal format +- ** in that it omits the checksums and the header. +- */ +- if( pPager->nSavepoint>0 && subjRequiresPage(pPg) ){ +- rc = subjournalPage(pPg); ++ }else{ ++ if( pPager->eState!=PAGER_WRITER_DBMOD ){ ++ pPg->flags |= PGHDR_NEED_SYNC; ++ } ++ PAGERTRACE(("APPEND %d page %d needSync=%d\n", ++ PAGERID(pPager), pPg->pgno, ++ ((pPg->flags&PGHDR_NEED_SYNC)?1:0))); + } + } + +- /* Update the database size and return. ++ /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list ++ ** and before writing the page into the rollback journal. Wait until now, ++ ** after the page has been successfully journalled, before setting the ++ ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified. ++ */ ++ pPg->flags |= PGHDR_WRITEABLE; ++ ++ /* If the statement journal is open and the page is not in it, ++ ** then write the page into the statement journal. + */ ++ if( pPager->nSavepoint>0 ){ ++ rc = subjournalPageIfRequired(pPg); ++ } ++ ++ /* Update the database size and return. */ + if( pPager->dbSize pgno ){ + pPager->dbSize = pPg->pgno; + } +@@ -47420,17 +56624,17 @@ static int pager_write(PgHdr *pPg){ + ** a write. + ** + ** Usually, the sector size is less than or equal to the page size, in which +-** case pages can be individually written. This routine only runs in the exceptional +-** case where the page size is smaller than the sector size. ++** case pages can be individually written. This routine only runs in the ++** exceptional case where the page size is smaller than the sector size. + */ + static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){ +- int rc = SQLITE_OK; /* Return code */ +- Pgno nPageCount; /* Total number of pages in database file */ +- Pgno pg1; /* First page of the sector pPg is located on. */ +- int nPage = 0; /* Number of pages starting at pg1 to journal */ +- int ii; /* Loop counter */ +- int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */ +- Pager *pPager = pPg->pPager; /* The pager that owns pPg */ ++ int rc = SQLITE_OK; /* Return code */ ++ Pgno nPageCount; /* Total number of pages in database file */ ++ Pgno pg1; /* First page of the sector pPg is located on. */ ++ int nPage = 0; /* Number of pages starting at pg1 to journal */ ++ int ii; /* Loop counter */ ++ int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */ ++ Pager *pPager = pPg->pPager; /* The pager that owns pPg */ + Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize); + + /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow +@@ -47464,7 +56668,7 @@ static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){ + PgHdr *pPage; + if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){ + if( pg!=PAGER_MJ_PGNO(pPager) ){ +- rc = sqlite3PagerGet(pPager, pg, &pPage); ++ rc = sqlite3PagerGet(pPager, pg, &pPage, 0); + if( rc==SQLITE_OK ){ + rc = pager_write(pPage); + if( pPage->flags&PGHDR_NEED_SYNC ){ +@@ -47518,11 +56722,17 @@ static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){ + ** as appropriate. Otherwise, SQLITE_OK. + */ + SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){ ++ Pager *pPager = pPg->pPager; + assert( (pPg->flags & PGHDR_MMAP)==0 ); +- assert( pPg->pPager->eState>=PAGER_WRITER_LOCKED ); +- assert( pPg->pPager->eState!=PAGER_ERROR ); +- assert( assert_pager_state(pPg->pPager) ); +- if( pPg->pPager->sectorSize > (u32)pPg->pPager->pageSize ){ ++ assert( pPager->eState>=PAGER_WRITER_LOCKED ); ++ assert( assert_pager_state(pPager) ); ++ if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){ ++ if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg); ++ return SQLITE_OK; ++ }else if( pPager->errCode ){ ++ return pPager->errCode; ++ }else if( pPager->sectorSize > (u32)pPager->pageSize ){ ++ assert( pPager->tempFile==0 ); + return pagerWriteLargeSector(pPg); + }else{ + return pager_write(pPg); +@@ -47536,7 +56746,7 @@ SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){ + */ + #ifndef NDEBUG + SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){ +- return pPg->flags&PGHDR_DIRTY; ++ return pPg->flags & PGHDR_WRITEABLE; + } + #endif + +@@ -47553,13 +56763,21 @@ SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){ + ** + ** Tests show that this optimization can quadruple the speed of large + ** DELETE operations. ++** ++** This optimization cannot be used with a temp-file, as the page may ++** have been dirty at the start of the transaction. In that case, if ++** memory pressure forces page pPg out of the cache, the data does need ++** to be written out to disk so that it may be read back in if the ++** current transaction is rolled back. + */ + SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){ + Pager *pPager = pPg->pPager; +- if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){ ++ if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){ + PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager))); + IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno)) + pPg->flags |= PGHDR_DONT_WRITE; ++ pPg->flags &= ~PGHDR_WRITEABLE; ++ testcase( pPg->flags & PGHDR_NEED_SYNC ); + pager_set_pagehash(pPg); + } + } +@@ -47618,7 +56836,7 @@ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){ + assert( !pPager->tempFile && isOpen(pPager->fd) ); + + /* Open page 1 of the file for writing. */ +- rc = sqlite3PagerGet(pPager, 1, &pPgHdr); ++ rc = sqlite3PagerGet(pPager, 1, &pPgHdr, 0); + assert( pPgHdr==0 || rc==SQLITE_OK ); + + /* If page one was fetched successfully, and this function is not +@@ -47638,7 +56856,7 @@ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){ + if( DIRECT_MODE ){ + const void *zBuf; + assert( pPager->dbFileSize>0 ); +- CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf); ++ CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM_BKPT, zBuf); + if( rc==SQLITE_OK ){ + rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); + pPager->aStat[PAGER_STAT_WRITE]++; +@@ -47671,12 +56889,9 @@ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){ + */ + SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){ + int rc = SQLITE_OK; +- +- if( isOpen(pPager->fd) ){ +- void *pArg = (void*)zMaster; +- rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg); +- if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; +- } ++ void *pArg = (void*)zMaster; ++ rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg); ++ if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; + if( rc==SQLITE_OK && !pPager->noSync ){ + assert( !MEMDB ); + rc = sqlite3OsSync(pPager->fd, pPager->syncFlags); +@@ -47696,14 +56911,17 @@ SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){ + ** returned. + */ + SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){ +- int rc = SQLITE_OK; +- assert( pPager->eState==PAGER_WRITER_CACHEMOD +- || pPager->eState==PAGER_WRITER_DBMOD +- || pPager->eState==PAGER_WRITER_LOCKED +- ); ++ int rc = pPager->errCode; + assert( assert_pager_state(pPager) ); +- if( 0==pagerUseWal(pPager) ){ +- rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); ++ if( rc==SQLITE_OK ){ ++ assert( pPager->eState==PAGER_WRITER_CACHEMOD ++ || pPager->eState==PAGER_WRITER_DBMOD ++ || pPager->eState==PAGER_WRITER_LOCKED ++ ); ++ assert( assert_pager_state(pPager) ); ++ if( 0==pagerUseWal(pPager) ){ ++ rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); ++ } + } + return rc; + } +@@ -47751,26 +56969,31 @@ SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne( + /* If a prior error occurred, report that error again. */ + if( NEVER(pPager->errCode) ) return pPager->errCode; + ++ /* Provide the ability to easily simulate an I/O error during testing */ ++ if( sqlite3FaultSim(400) ) return SQLITE_IOERR; ++ + PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", + pPager->zFilename, zMaster, pPager->dbSize)); + + /* If no database changes have been made, return early. */ + if( pPager->eState tempFile ); ++ assert( isOpen(pPager->fd) || pPager->tempFile ); ++ if( 0==pagerFlushOnCommit(pPager, 1) ){ + /* If this is an in-memory db, or no pages have been written to, or this + ** function has already been called, it is mostly a no-op. However, any +- ** backup in progress needs to be restarted. +- */ ++ ** backup in progress needs to be restarted. */ + sqlite3BackupRestart(pPager->pBackup); + }else{ ++ PgHdr *pList; + if( pagerUseWal(pPager) ){ +- PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache); + PgHdr *pPageOne = 0; ++ pList = sqlite3PcacheDirtyList(pPager->pPCache); + if( pList==0 ){ + /* Must have at least one page for the WAL commit flag. + ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */ +- rc = sqlite3PagerGet(pPager, 1, &pPageOne); ++ rc = sqlite3PagerGet(pPager, 1, &pPageOne, 0); + pList = pPageOne; + pList->pDirty = 0; + } +@@ -47783,6 +57006,21 @@ SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne( + sqlite3PcacheCleanAll(pPager->pPCache); + } + }else{ ++ /* The bBatch boolean is true if the batch-atomic-write commit method ++ ** should be used. No rollback journal is created if batch-atomic-write ++ ** is enabled. ++ */ ++#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE ++ sqlite3_file *fd = pPager->fd; ++ int bBatch = zMaster==0 /* An SQLITE_IOCAP_BATCH_ATOMIC commit */ ++ && (sqlite3OsDeviceCharacteristics(fd) & SQLITE_IOCAP_BATCH_ATOMIC) ++ && !pPager->noSync ++ && sqlite3JournalIsInMemory(pPager->jfd); ++#else ++# define bBatch 0 ++#endif ++ ++#ifdef SQLITE_ENABLE_ATOMIC_WRITE + /* The following block updates the change-counter. Exactly how it + ** does this depends on whether or not the atomic-update optimization + ** was enabled at compile time, and if this transaction meets the +@@ -47806,33 +57044,41 @@ SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne( + ** in 'direct' mode. In this case the journal file will never be + ** created for this transaction. + */ +- #ifdef SQLITE_ENABLE_ATOMIC_WRITE +- PgHdr *pPg; +- assert( isOpen(pPager->jfd) +- || pPager->journalMode==PAGER_JOURNALMODE_OFF +- || pPager->journalMode==PAGER_JOURNALMODE_WAL +- ); +- if( !zMaster && isOpen(pPager->jfd) +- && pPager->journalOff==jrnlBufferSize(pPager) +- && pPager->dbSize>=pPager->dbOrigSize +- && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty) +- ){ +- /* Update the db file change counter via the direct-write method. The +- ** following call will modify the in-memory representation of page 1 +- ** to include the updated change counter and then write page 1 +- ** directly to the database file. Because of the atomic-write +- ** property of the host file-system, this is safe. +- */ +- rc = pager_incr_changecounter(pPager, 1); +- }else{ +- rc = sqlite3JournalCreate(pPager->jfd); +- if( rc==SQLITE_OK ){ +- rc = pager_incr_changecounter(pPager, 0); ++ if( bBatch==0 ){ ++ PgHdr *pPg; ++ assert( isOpen(pPager->jfd) ++ || pPager->journalMode==PAGER_JOURNALMODE_OFF ++ || pPager->journalMode==PAGER_JOURNALMODE_WAL ++ ); ++ if( !zMaster && isOpen(pPager->jfd) ++ && pPager->journalOff==jrnlBufferSize(pPager) ++ && pPager->dbSize>=pPager->dbOrigSize ++ && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty) ++ ){ ++ /* Update the db file change counter via the direct-write method. The ++ ** following call will modify the in-memory representation of page 1 ++ ** to include the updated change counter and then write page 1 ++ ** directly to the database file. Because of the atomic-write ++ ** property of the host file-system, this is safe. ++ */ ++ rc = pager_incr_changecounter(pPager, 1); ++ }else{ ++ rc = sqlite3JournalCreate(pPager->jfd); ++ if( rc==SQLITE_OK ){ ++ rc = pager_incr_changecounter(pPager, 0); ++ } + } + } +- #else ++#else /* SQLITE_ENABLE_ATOMIC_WRITE */ ++#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE ++ if( zMaster ){ ++ rc = sqlite3JournalCreate(pPager->jfd); ++ if( rc!=SQLITE_OK ) goto commit_phase_one_exit; ++ assert( bBatch==0 ); ++ } ++#endif + rc = pager_incr_changecounter(pPager, 0); +- #endif ++#endif /* !SQLITE_ENABLE_ATOMIC_WRITE */ + if( rc!=SQLITE_OK ) goto commit_phase_one_exit; + + /* Write the master journal name into the journal file. If a master +@@ -47855,8 +57101,37 @@ SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne( + */ + rc = syncJournal(pPager, 0); + if( rc!=SQLITE_OK ) goto commit_phase_one_exit; +- +- rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache)); ++ ++ pList = sqlite3PcacheDirtyList(pPager->pPCache); ++#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE ++ if( bBatch ){ ++ rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0); ++ if( rc==SQLITE_OK ){ ++ rc = pager_write_pagelist(pPager, pList); ++ if( rc==SQLITE_OK ){ ++ rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0); ++ } ++ if( rc!=SQLITE_OK ){ ++ sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0); ++ } ++ } ++ ++ if( (rc&0xFF)==SQLITE_IOERR && rc!=SQLITE_IOERR_NOMEM ){ ++ rc = sqlite3JournalCreate(pPager->jfd); ++ if( rc!=SQLITE_OK ){ ++ sqlite3OsClose(pPager->jfd); ++ goto commit_phase_one_exit; ++ } ++ bBatch = 0; ++ }else{ ++ sqlite3OsClose(pPager->jfd); ++ } ++ } ++#endif /* SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ ++ ++ if( bBatch==0 ){ ++ rc = pager_write_pagelist(pPager, pList); ++ } + if( rc!=SQLITE_OK ){ + assert( rc!=SQLITE_IOERR_BLOCKED ); + goto commit_phase_one_exit; +@@ -48000,6 +57275,7 @@ SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){ + */ + pPager->errCode = SQLITE_ABORT; + pPager->eState = PAGER_ERROR; ++ setGetterMethod(pPager); + return rc; + } + }else{ +@@ -48026,12 +57302,14 @@ SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){ + return pPager->readOnly; + } + ++#ifdef SQLITE_DEBUG + /* +-** Return the number of references to the pager. ++** Return the sum of the reference counts for all pages held by pPager. + */ + SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){ + return sqlite3PcacheRefCount(pPager->pPCache); + } ++#endif + + /* + ** Return the approximate number of bytes of memory currently +@@ -48074,8 +57352,12 @@ SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){ + #endif + + /* +-** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or +-** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the ++** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE, ++** or _WRITE+1. The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation ++** of SQLITE_DBSTATUS_CACHE_SPILL. The _SPILL case is not contiguous because ++** it was added later. ++** ++** Before returning, *pnVal is incremented by the + ** current cache hit or miss count, according to the value of eStat. If the + ** reset parameter is non-zero, the cache hit or miss count is zeroed before + ** returning. +@@ -48085,23 +57367,26 @@ SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, i + assert( eStat==SQLITE_DBSTATUS_CACHE_HIT + || eStat==SQLITE_DBSTATUS_CACHE_MISS + || eStat==SQLITE_DBSTATUS_CACHE_WRITE ++ || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1 + ); + + assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS ); + assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE ); +- assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 ); ++ assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 ++ && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 ); + +- *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT]; ++ eStat -= SQLITE_DBSTATUS_CACHE_HIT; ++ *pnVal += pPager->aStat[eStat]; + if( reset ){ +- pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0; ++ pPager->aStat[eStat] = 0; + } + } + + /* +-** Return true if this is an in-memory pager. ++** Return true if this is an in-memory or temp-file backed pager. + */ + SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){ +- return MEMDB; ++ return pPager->tempFile; + } + + /* +@@ -48114,54 +57399,62 @@ SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){ + ** occurs while opening the sub-journal file, then an IO error code is + ** returned. Otherwise, SQLITE_OK. + */ +-SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){ ++static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){ + int rc = SQLITE_OK; /* Return code */ + int nCurrent = pPager->nSavepoint; /* Current number of savepoints */ ++ int ii; /* Iterator variable */ ++ PagerSavepoint *aNew; /* New Pager.aSavepoint array */ + + assert( pPager->eState>=PAGER_WRITER_LOCKED ); + assert( assert_pager_state(pPager) ); ++ assert( nSavepoint>nCurrent && pPager->useJournal ); + +- if( nSavepoint>nCurrent && pPager->useJournal ){ +- int ii; /* Iterator variable */ +- PagerSavepoint *aNew; /* New Pager.aSavepoint array */ ++ /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM ++ ** if the allocation fails. Otherwise, zero the new portion in case a ++ ** malloc failure occurs while populating it in the for(...) loop below. ++ */ ++ aNew = (PagerSavepoint *)sqlite3Realloc( ++ pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint ++ ); ++ if( !aNew ){ ++ return SQLITE_NOMEM_BKPT; ++ } ++ memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint)); ++ pPager->aSavepoint = aNew; + +- /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM +- ** if the allocation fails. Otherwise, zero the new portion in case a +- ** malloc failure occurs while populating it in the for(...) loop below. +- */ +- aNew = (PagerSavepoint *)sqlite3Realloc( +- pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint +- ); +- if( !aNew ){ +- return SQLITE_NOMEM; ++ /* Populate the PagerSavepoint structures just allocated. */ ++ for(ii=nCurrent; ii dbSize; ++ if( isOpen(pPager->jfd) && pPager->journalOff>0 ){ ++ aNew[ii].iOffset = pPager->journalOff; ++ }else{ ++ aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager); + } +- memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint)); +- pPager->aSavepoint = aNew; +- +- /* Populate the PagerSavepoint structures just allocated. */ +- for(ii=nCurrent; ii dbSize; +- if( isOpen(pPager->jfd) && pPager->journalOff>0 ){ +- aNew[ii].iOffset = pPager->journalOff; +- }else{ +- aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager); +- } +- aNew[ii].iSubRec = pPager->nSubRec; +- aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize); +- if( !aNew[ii].pInSavepoint ){ +- return SQLITE_NOMEM; +- } +- if( pagerUseWal(pPager) ){ +- sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData); +- } +- pPager->nSavepoint = ii+1; ++ aNew[ii].iSubRec = pPager->nSubRec; ++ aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize); ++ if( !aNew[ii].pInSavepoint ){ ++ return SQLITE_NOMEM_BKPT; ++ } ++ if( pagerUseWal(pPager) ){ ++ sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData); + } +- assert( pPager->nSavepoint==nSavepoint ); +- assertTruncateConstraint(pPager); ++ pPager->nSavepoint = ii+1; + } +- ++ assert( pPager->nSavepoint==nSavepoint ); ++ assertTruncateConstraint(pPager); + return rc; + } ++SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){ ++ assert( pPager->eState>=PAGER_WRITER_LOCKED ); ++ assert( assert_pager_state(pPager) ); ++ ++ if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){ ++ return pagerOpenSavepoint(pPager, nSavepoint); ++ }else{ ++ return SQLITE_OK; ++ } ++} ++ + + /* + ** This function is called to rollback or release (commit) a savepoint. +@@ -48194,7 +57487,11 @@ SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){ + ** savepoint. If no errors occur, SQLITE_OK is returned. + */ + SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ +- int rc = pPager->errCode; /* Return code */ ++ int rc = pPager->errCode; ++ ++#ifdef SQLITE_ENABLE_ZIPVFS ++ if( op==SAVEPOINT_RELEASE ) rc = SQLITE_OK; ++#endif + + assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); + assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK ); +@@ -48218,7 +57515,7 @@ SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ + if( op==SAVEPOINT_RELEASE ){ + if( nNew==0 && isOpen(pPager->sjfd) ){ + /* Only truncate if it is an in-memory sub-journal. */ +- if( sqlite3IsMemJournal(pPager->sjfd) ){ ++ if( sqlite3JournalIsInMemory(pPager->sjfd) ){ + rc = sqlite3OsTruncate(pPager->sjfd, 0); + assert( rc==SQLITE_OK ); + } +@@ -48235,6 +57532,21 @@ SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ + rc = pagerPlaybackSavepoint(pPager, pSavepoint); + assert(rc!=SQLITE_DONE); + } ++ ++#ifdef SQLITE_ENABLE_ZIPVFS ++ /* If the cache has been modified but the savepoint cannot be rolled ++ ** back journal_mode=off, put the pager in the error state. This way, ++ ** if the VFS used by this pager includes ZipVFS, the entire transaction ++ ** can be rolled back at the ZipVFS level. */ ++ else if( ++ pPager->journalMode==PAGER_JOURNALMODE_OFF ++ && pPager->eState>=PAGER_WRITER_CACHEMOD ++ ){ ++ pPager->errCode = SQLITE_ABORT; ++ pPager->eState = PAGER_ERROR; ++ setGetterMethod(pPager); ++ } ++#endif + } + + return rc; +@@ -48257,7 +57569,7 @@ SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager, int nullIfMemDb){ + /* + ** Return the VFS structure for the pager. + */ +-SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){ ++SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){ + return pPager->pVfs; + } + +@@ -48270,19 +57582,33 @@ SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){ + return pPager->fd; + } + ++#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + /* +-** Return the full pathname of the journal file. ++** Reset the lock timeout for pager. + */ +-SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){ +- return pPager->zJournal; ++SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager){ ++ int x = 0; ++ sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_LOCK_TIMEOUT, &x); + } ++#endif + + /* +-** Return true if fsync() calls are disabled for this pager. Return FALSE +-** if fsync()s are executed normally. ++** Return the file handle for the journal file (if it exists). ++** This will be either the rollback journal or the WAL file. + */ +-SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){ +- return pPager->noSync; ++SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){ ++#if SQLITE_OMIT_WAL ++ return pPager->jfd; ++#else ++ return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd; ++#endif ++} ++ ++/* ++** Return the full pathname of the journal file. ++*/ ++SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){ ++ return pPager->zJournal; + } + + #ifdef SQLITE_HAS_CODEC +@@ -48296,11 +57622,16 @@ SQLITE_PRIVATE void sqlite3PagerSetCodec( + void (*xCodecFree)(void*), + void *pCodec + ){ +- if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec); ++ if( pPager->xCodecFree ){ ++ pPager->xCodecFree(pPager->pCodec); ++ }else{ ++ pager_reset(pPager); ++ } + pPager->xCodec = pPager->memDb ? 0 : xCodec; + pPager->xCodecSizeChng = xCodecSizeChng; + pPager->xCodecFree = xCodecFree; + pPager->pCodec = pCodec; ++ setGetterMethod(pPager); + pagerReportSize(pPager); + } + SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){ +@@ -48369,7 +57700,8 @@ SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, i + /* In order to be able to rollback, an in-memory database must journal + ** the page we are moving from. + */ +- if( MEMDB ){ ++ assert( pPager->tempFile || !MEMDB ); ++ if( pPager->tempFile ){ + rc = sqlite3PagerWrite(pPg); + if( rc ) return rc; + } +@@ -48392,9 +57724,8 @@ SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, i + ** one or more savepoint bitvecs. This is the reason this function + ** may return SQLITE_NOMEM. + */ +- if( pPg->flags&PGHDR_DIRTY +- && subjRequiresPage(pPg) +- && SQLITE_OK!=(rc = subjournalPage(pPg)) ++ if( (pPg->flags & PGHDR_DIRTY)!=0 ++ && SQLITE_OK!=(rc = subjournalPageIfRequired(pPg)) + ){ + return rc; + } +@@ -48424,10 +57755,14 @@ SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, i + */ + pPg->flags &= ~PGHDR_NEED_SYNC; + pPgOld = sqlite3PagerLookup(pPager, pgno); +- assert( !pPgOld || pPgOld->nRef==1 ); ++ assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB ); + if( pPgOld ){ ++ if( pPgOld->nRef>1 ){ ++ sqlite3PagerUnrefNotNull(pPgOld); ++ return SQLITE_CORRUPT_BKPT; ++ } + pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC); +- if( MEMDB ){ ++ if( pPager->tempFile ){ + /* Do not discard pages from an in-memory database since we might + ** need to rollback later. Just move the page out of the way. */ + sqlite3PcacheMove(pPgOld, pPager->dbSize+1); +@@ -48444,8 +57779,7 @@ SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, i + ** to exist, in case the transaction needs to roll back. Use pPgOld + ** as the original page since it has already been allocated. + */ +- if( MEMDB ){ +- assert( pPgOld ); ++ if( pPager->tempFile && pPgOld ){ + sqlite3PcacheMove(pPgOld, origPgno); + sqlite3PagerUnrefNotNull(pPgOld); + } +@@ -48466,7 +57800,7 @@ SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, i + ** the journal file twice, but that is not a problem. + */ + PgHdr *pPgHdr; +- rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr); ++ rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr, 0); + if( rc!=SQLITE_OK ){ + if( needSyncPgno<=pPager->dbOrigSize ){ + assert( pPager->pTmpSpace!=0 ); +@@ -48557,13 +57891,6 @@ SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){ + SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){ + u8 eOld = pPager->journalMode; /* Prior journalmode */ + +-#ifdef SQLITE_DEBUG +- /* The print_pager_state() routine is intended to be used by the debugger +- ** only. We invoke it once here to suppress a compiler warning. */ +- print_pager_state(pPager); +-#endif +- +- + /* The eMode parameter is always valid */ + assert( eMode==PAGER_JOURNALMODE_DELETE + || eMode==PAGER_JOURNALMODE_TRUNCATE +@@ -48697,10 +58024,12 @@ SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){ + ** Unless this is an in-memory or temporary database, clear the pager cache. + */ + SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){ +- if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager); ++ assert( MEMDB==0 || pPager->tempFile ); ++ if( pPager->tempFile==0 ) pager_reset(pPager); + } + #endif + ++ + #ifndef SQLITE_OMIT_WAL + /* + ** This function is called when the user invokes "PRAGMA wal_checkpoint", +@@ -48709,15 +58038,22 @@ SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){ + ** + ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. + */ +-SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){ ++SQLITE_PRIVATE int sqlite3PagerCheckpoint( ++ Pager *pPager, /* Checkpoint on this pager */ ++ sqlite3 *db, /* Db handle used to check for interrupts */ ++ int eMode, /* Type of checkpoint */ ++ int *pnLog, /* OUT: Final number of frames in log */ ++ int *pnCkpt /* OUT: Final number of checkpointed frames */ ++){ + int rc = SQLITE_OK; + if( pPager->pWal ){ +- rc = sqlite3WalCheckpoint(pPager->pWal, eMode, ++ rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode, + (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler), + pPager->pBusyHandlerArg, +- pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace, ++ pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace, + pnLog, pnCkpt + ); ++ sqlite3PagerResetLockTimeout(pPager); + } + return rc; + } +@@ -48732,6 +58068,7 @@ SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){ + */ + SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){ + const sqlite3_io_methods *pMethods = pPager->fd->pMethods; ++ if( pPager->noLock ) return 0; + return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap); + } + +@@ -48843,7 +58180,7 @@ SQLITE_PRIVATE int sqlite3PagerOpenWal( + ** error (SQLITE_BUSY) is returned and the log connection is not closed. + ** If successful, the EXCLUSIVE lock is not released before returning. + */ +-SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){ ++SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){ + int rc = SQLITE_OK; + + assert( pPager->journalMode==PAGER_JOURNALMODE_WAL ); +@@ -48871,15 +58208,90 @@ SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){ + if( rc==SQLITE_OK && pPager->pWal ){ + rc = pagerExclusiveLock(pPager); + if( rc==SQLITE_OK ){ +- rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, ++ rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, + pPager->pageSize, (u8*)pPager->pTmpSpace); + pPager->pWal = 0; + pagerFixMaplimit(pPager); ++ if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK); + } + } + return rc; + } + ++#ifdef SQLITE_ENABLE_SNAPSHOT ++/* ++** If this is a WAL database, obtain a snapshot handle for the snapshot ++** currently open. Otherwise, return an error. ++*/ ++SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){ ++ int rc = SQLITE_ERROR; ++ if( pPager->pWal ){ ++ rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot); ++ } ++ return rc; ++} ++ ++/* ++** If this is a WAL database, store a pointer to pSnapshot. Next time a ++** read transaction is opened, attempt to read from the snapshot it ++** identifies. If this is not a WAL database, return an error. ++*/ ++SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){ ++ int rc = SQLITE_OK; ++ if( pPager->pWal ){ ++ sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot); ++ }else{ ++ rc = SQLITE_ERROR; ++ } ++ return rc; ++} ++ ++/* ++** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this ++** is not a WAL database, return an error. ++*/ ++SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager){ ++ int rc; ++ if( pPager->pWal ){ ++ rc = sqlite3WalSnapshotRecover(pPager->pWal); ++ }else{ ++ rc = SQLITE_ERROR; ++ } ++ return rc; ++} ++ ++/* ++** The caller currently has a read transaction open on the database. ++** If this is not a WAL database, SQLITE_ERROR is returned. Otherwise, ++** this function takes a SHARED lock on the CHECKPOINTER slot and then ++** checks if the snapshot passed as the second argument is still ++** available. If so, SQLITE_OK is returned. ++** ++** If the snapshot is not available, SQLITE_ERROR is returned. Or, if ++** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error ++** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER ++** lock is released before returning. ++*/ ++SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot){ ++ int rc; ++ if( pPager->pWal ){ ++ rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot); ++ }else{ ++ rc = SQLITE_ERROR; ++ } ++ return rc; ++} ++ ++/* ++** Release a lock obtained by an earlier successful call to ++** sqlite3PagerSnapshotCheck(). ++*/ ++SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager){ ++ assert( pPager->pWal ); ++ sqlite3WalSnapshotUnlock(pPager->pWal); ++} ++ ++#endif /* SQLITE_ENABLE_SNAPSHOT */ + #endif /* !SQLITE_OMIT_WAL */ + + #ifdef SQLITE_ENABLE_ZIPVFS +@@ -48896,7 +58308,6 @@ SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){ + } + #endif + +- + #endif /* SQLITE_OMIT_DISKIO */ + + /************** End of pager.c ***********************************************/ +@@ -49035,6 +58446,10 @@ SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){ + ** on a network filesystem. All users of the database must be able to + ** share memory. + ** ++** In the default unix and windows implementation, the wal-index is a mmapped ++** file whose name is the database name with a "-shm" suffix added. For that ++** reason, the wal-index is sometimes called the "shm" file. ++** + ** The wal-index is transient. After a crash, the wal-index can (and should + ** be) reconstructed from the original WAL file. In fact, the VFS is required + ** to either truncate or zero the header of the wal-index when the last +@@ -49145,6 +58560,7 @@ SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){ + */ + #ifndef SQLITE_OMIT_WAL + ++/* #include "wal.h" */ + + /* + ** Trace output macros +@@ -49156,6 +58572,18 @@ SQLITE_PRIVATE int sqlite3WalTrace = 0; + # define WALTRACE(X) + #endif + ++/* ++** WAL mode depends on atomic aligned 32-bit loads and stores in a few ++** places. The following macros try to make this explicit. ++*/ ++#if GCC_VESRION>=5004000 ++# define AtomicLoad(PTR) __atomic_load_n((PTR),__ATOMIC_RELAXED) ++# define AtomicStore(PTR,VAL) __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED) ++#else ++# define AtomicLoad(PTR) (*(PTR)) ++# define AtomicStore(PTR,VAL) (*(PTR) = (VAL)) ++#endif ++ + /* + ** The maximum (and only) versions of the wal and wal-index formats + ** that may be interpreted by this version of SQLite. +@@ -49173,8 +58601,18 @@ SQLITE_PRIVATE int sqlite3WalTrace = 0; + #define WALINDEX_MAX_VERSION 3007000 + + /* +-** Indices of various locking bytes. WAL_NREADER is the number +-** of available reader locks and should be at least 3. ++** Index numbers for various locking bytes. WAL_NREADER is the number ++** of available reader locks and should be at least 3. The default ++** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5. ++** ++** Technically, the various VFSes are free to implement these locks however ++** they see fit. However, compatibility is encouraged so that VFSes can ++** interoperate. The standard implemention used on both unix and windows ++** is for the index number to indicate a byte offset into the ++** WalCkptInfo.aLock[] array in the wal-index header. In other words, all ++** locks are on the shm file. The WALINDEX_LOCK_OFFSET constant (which ++** should be 120) is the location in the shm file for the first locking ++** byte. + */ + #define WAL_WRITE_LOCK 0 + #define WAL_ALL_BUT_WRITE 1 +@@ -49194,7 +58632,10 @@ typedef struct WalCkptInfo WalCkptInfo; + ** The following object holds a copy of the wal-index header content. + ** + ** The actual header in the wal-index consists of two copies of this +-** object. ++** object followed by one instance of the WalCkptInfo object. ++** For all versions of SQLite through 3.10.0 and probably beyond, ++** the locking bytes (WalCkptInfo.aLock) start at offset 120 and ++** the total header size is 136 bytes. + ** + ** The szPage value can be any power of 2 between 512 and 32768, inclusive. + ** Or it can be 1 to represent a 65536-byte page. The latter case was +@@ -49227,6 +58668,16 @@ struct WalIndexHdr { + ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from + ** mxFrame back to zero when the WAL is reset. + ** ++** nBackfillAttempted is the largest value of nBackfill that a checkpoint ++** has attempted to achieve. Normally nBackfill==nBackfillAtempted, however ++** the nBackfillAttempted is set before any backfilling is done and the ++** nBackfill is only set after all backfilling completes. So if a checkpoint ++** crashes, nBackfillAttempted might be larger than nBackfill. The ++** WalIndexHdr.mxFrame must never be less than nBackfillAttempted. ++** ++** The aLock[] field is a set of bytes used for locking. These bytes should ++** never be read or written. ++** + ** There is one entry in aReadMark[] for each reader lock. If a reader + ** holds read-lock K, then the value in aReadMark[K] is no greater than + ** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff) +@@ -49266,6 +58717,9 @@ struct WalIndexHdr { + struct WalCkptInfo { + u32 nBackfill; /* Number of WAL frames backfilled into DB */ + u32 aReadMark[WAL_NREADER]; /* Reader marks */ ++ u8 aLock[SQLITE_SHM_NLOCK]; /* Reserved space for locks */ ++ u32 nBackfillAttempted; /* WAL frames perhaps written, or maybe not */ ++ u32 notUsed0; /* Available for future enhancements */ + }; + #define READMARK_NOT_USED 0xffffffff + +@@ -49275,15 +58729,13 @@ struct WalCkptInfo { + ** only support mandatory file-locks, we do not read or write data + ** from the region of the file on which locks are applied. + */ +-#define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo)) +-#define WALINDEX_LOCK_RESERVED 16 +-#define WALINDEX_HDR_SIZE (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED) ++#define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock)) ++#define WALINDEX_HDR_SIZE (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo)) + + /* Size of header before each frame in wal */ + #define WAL_FRAME_HDRSIZE 24 + + /* Size of write ahead log header, including checksum. */ +-/* #define WAL_HDRSIZE 24 */ + #define WAL_HDRSIZE 32 + + /* WAL magic value. Either this value, or the same value with the least +@@ -49329,12 +58781,18 @@ struct Wal { + u8 truncateOnCommit; /* True to truncate WAL file on commit */ + u8 syncHeader; /* Fsync the WAL header if true */ + u8 padToSectorBoundary; /* Pad transactions out to the next sector */ ++ u8 bShmUnreliable; /* SHM content is read-only and unreliable */ + WalIndexHdr hdr; /* Wal-index header for current transaction */ ++ u32 minFrame; /* Ignore wal frames before this one */ ++ u32 iReCksum; /* On commit, recalculate checksums from here */ + const char *zWalName; /* Name of WAL file */ + u32 nCkpt; /* Checkpoint sequence counter in the wal-header */ + #ifdef SQLITE_DEBUG + u8 lockError; /* True if a locking error has occurred */ + #endif ++#ifdef SQLITE_ENABLE_SNAPSHOT ++ WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */ ++#endif + }; + + /* +@@ -49413,21 +58871,30 @@ struct WalIterator { + ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are + ** numbered from zero. + ** ++** If the wal-index is currently smaller the iPage pages then the size ++** of the wal-index might be increased, but only if it is safe to do ++** so. It is safe to enlarge the wal-index if pWal->writeLock is true ++** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE. ++** + ** If this call is successful, *ppPage is set to point to the wal-index + ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs, + ** then an SQLite error code is returned and *ppPage is set to 0. + */ +-static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){ ++static SQLITE_NOINLINE int walIndexPageRealloc( ++ Wal *pWal, /* The WAL context */ ++ int iPage, /* The page we seek */ ++ volatile u32 **ppPage /* Write the page pointer here */ ++){ + int rc = SQLITE_OK; + + /* Enlarge the pWal->apWiData[] array if required */ + if( pWal->nWiData<=iPage ){ +- int nByte = sizeof(u32*)*(iPage+1); ++ sqlite3_int64 nByte = sizeof(u32*)*(iPage+1); + volatile u32 **apNew; + apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte); + if( !apNew ){ + *ppPage = 0; +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + memset((void*)&apNew[pWal->nWiData], 0, + sizeof(u32*)*(iPage+1-pWal->nWiData)); +@@ -49436,16 +58903,19 @@ static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){ + } + + /* Request a pointer to the required page from the VFS */ +- if( pWal->apWiData[iPage]==0 ){ +- if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){ +- pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ); +- if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM; +- }else{ +- rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, +- pWal->writeLock, (void volatile **)&pWal->apWiData[iPage] +- ); ++ assert( pWal->apWiData[iPage]==0 ); ++ if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){ ++ pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ); ++ if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT; ++ }else{ ++ rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, ++ pWal->writeLock, (void volatile **)&pWal->apWiData[iPage] ++ ); ++ assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 ); ++ testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK ); ++ if( (rc&0xff)==SQLITE_READONLY ){ ++ pWal->readOnly |= WAL_SHM_RDONLY; + if( rc==SQLITE_READONLY ){ +- pWal->readOnly |= WAL_SHM_RDONLY; + rc = SQLITE_OK; + } + } +@@ -49455,6 +58925,16 @@ static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){ + assert( iPage==0 || *ppPage || rc!=SQLITE_OK ); + return rc; + } ++static int walIndexPage( ++ Wal *pWal, /* The WAL context */ ++ int iPage, /* The page we seek */ ++ volatile u32 **ppPage /* Write the page pointer here */ ++){ ++ if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){ ++ return walIndexPageRealloc(pWal, iPage, ppPage); ++ } ++ return SQLITE_OK; ++} + + /* + ** Return a pointer to the WalCkptInfo structure in the wal-index. +@@ -49513,6 +58993,7 @@ static void walChecksumBytes( + + assert( nByte>=8 ); + assert( (nByte&0x00000007)==0 ); ++ assert( nByte<=65536 ); + + if( nativeCksum ){ + do { +@@ -49550,9 +59031,9 @@ static void walIndexWriteHdr(Wal *pWal){ + pWal->hdr.isInit = 1; + pWal->hdr.iVersion = WALINDEX_MAX_VERSION; + walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum); +- memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr)); ++ memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr)); + walShmBarrier(pWal); +- memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr)); ++ memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr)); + } + + /* +@@ -49580,14 +59061,18 @@ static void walEncodeFrame( + assert( WAL_FRAME_HDRSIZE==24 ); + sqlite3Put4byte(&aFrame[0], iPage); + sqlite3Put4byte(&aFrame[4], nTruncate); +- memcpy(&aFrame[8], pWal->hdr.aSalt, 8); ++ if( pWal->iReCksum==0 ){ ++ memcpy(&aFrame[8], pWal->hdr.aSalt, 8); + +- nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN); +- walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum); +- walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum); ++ nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN); ++ walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum); ++ walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum); + +- sqlite3Put4byte(&aFrame[16], aCksum[0]); +- sqlite3Put4byte(&aFrame[20], aCksum[1]); ++ sqlite3Put4byte(&aFrame[16], aCksum[0]); ++ sqlite3Put4byte(&aFrame[20], aCksum[1]); ++ }else{ ++ memset(&aFrame[8], 0, 16); ++ } + } + + /* +@@ -49690,10 +59175,9 @@ static void walUnlockShared(Wal *pWal, int lockIdx){ + SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED); + WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx))); + } +-static int walLockExclusive(Wal *pWal, int lockIdx, int n, int fBlock){ ++static int walLockExclusive(Wal *pWal, int lockIdx, int n){ + int rc; + if( pWal->exclusiveMode ) return SQLITE_OK; +- if( fBlock ) sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_WAL_BLOCK, 0); + rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n, + SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE); + WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal, +@@ -49723,48 +59207,51 @@ static int walNextHash(int iPriorHash){ + return (iPriorHash+1)&(HASHTABLE_NSLOT-1); + } + ++/* ++** An instance of the WalHashLoc object is used to describe the location ++** of a page hash table in the wal-index. This becomes the return value ++** from walHashGet(). ++*/ ++typedef struct WalHashLoc WalHashLoc; ++struct WalHashLoc { ++ volatile ht_slot *aHash; /* Start of the wal-index hash table */ ++ volatile u32 *aPgno; /* aPgno[1] is the page of first frame indexed */ ++ u32 iZero; /* One less than the frame number of first indexed*/ ++}; ++ + /* + ** Return pointers to the hash table and page number array stored on + ** page iHash of the wal-index. The wal-index is broken into 32KB pages + ** numbered starting from 0. + ** +-** Set output variable *paHash to point to the start of the hash table +-** in the wal-index file. Set *piZero to one less than the frame ++** Set output variable pLoc->aHash to point to the start of the hash table ++** in the wal-index file. Set pLoc->iZero to one less than the frame + ** number of the first frame indexed by this hash table. If a + ** slot in the hash table is set to N, it refers to frame number +-** (*piZero+N) in the log. ++** (pLoc->iZero+N) in the log. + ** +-** Finally, set *paPgno so that *paPgno[1] is the page number of the +-** first frame indexed by the hash table, frame (*piZero+1). ++** Finally, set pLoc->aPgno so that pLoc->aPgno[1] is the page number of the ++** first frame indexed by the hash table, frame (pLoc->iZero+1). + */ + static int walHashGet( + Wal *pWal, /* WAL handle */ + int iHash, /* Find the iHash'th table */ +- volatile ht_slot **paHash, /* OUT: Pointer to hash index */ +- volatile u32 **paPgno, /* OUT: Pointer to page number array */ +- u32 *piZero /* OUT: Frame associated with *paPgno[0] */ ++ WalHashLoc *pLoc /* OUT: Hash table location */ + ){ + int rc; /* Return code */ +- volatile u32 *aPgno; + +- rc = walIndexPage(pWal, iHash, &aPgno); ++ rc = walIndexPage(pWal, iHash, &pLoc->aPgno); + assert( rc==SQLITE_OK || iHash>0 ); + + if( rc==SQLITE_OK ){ +- u32 iZero; +- volatile ht_slot *aHash; +- +- aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE]; ++ pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE]; + if( iHash==0 ){ +- aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)]; +- iZero = 0; ++ pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)]; ++ pLoc->iZero = 0; + }else{ +- iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE; ++ pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE; + } +- +- *paPgno = &aPgno[-1]; +- *paHash = aHash; +- *piZero = iZero; ++ pLoc->aPgno = &pLoc->aPgno[-1]; + } + return rc; + } +@@ -49810,12 +59297,11 @@ static u32 walFramePgno(Wal *pWal, u32 iFrame){ + ** actually needed. + */ + static void walCleanupHash(Wal *pWal){ +- volatile ht_slot *aHash = 0; /* Pointer to hash table to clear */ +- volatile u32 *aPgno = 0; /* Page number array for hash table */ +- u32 iZero = 0; /* frame == (aHash[x]+iZero) */ ++ WalHashLoc sLoc; /* Hash table location */ + int iLimit = 0; /* Zero values greater than this */ + int nByte; /* Number of bytes to zero in aPgno[] */ + int i; /* Used to iterate through aHash[] */ ++ int rc; /* Return code form walHashGet() */ + + assert( pWal->writeLock ); + testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 ); +@@ -49826,41 +59312,42 @@ static void walCleanupHash(Wal *pWal){ + + /* Obtain pointers to the hash-table and page-number array containing + ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed +- ** that the page said hash-table and array reside on is already mapped. ++ ** that the page said hash-table and array reside on is already mapped.(1) + */ + assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) ); + assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] ); +- walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero); ++ rc = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc); ++ if( NEVER(rc) ) return; /* Defense-in-depth, in case (1) above is wrong */ + + /* Zero all hash-table entries that correspond to frame numbers greater + ** than pWal->hdr.mxFrame. + */ +- iLimit = pWal->hdr.mxFrame - iZero; ++ iLimit = pWal->hdr.mxFrame - sLoc.iZero; + assert( iLimit>0 ); + for(i=0; i iLimit ){ +- aHash[i] = 0; ++ if( sLoc.aHash[i]>iLimit ){ ++ sLoc.aHash[i] = 0; + } + } + + /* Zero the entries in the aPgno array that correspond to frames with + ** frame numbers greater than pWal->hdr.mxFrame. + */ +- nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]); +- memset((void *)&aPgno[iLimit+1], 0, nByte); ++ nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit+1]); ++ memset((void *)&sLoc.aPgno[iLimit+1], 0, nByte); + + #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT + /* Verify that the every entry in the mapping region is still reachable + ** via the hash table even after the cleanup. + */ + if( iLimit ){ +- int i; /* Loop counter */ ++ int j; /* Loop counter */ + int iKey; /* Hash key */ +- for(i=1; i<=iLimit; i++){ +- for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){ +- if( aHash[iKey]==i ) break; ++ for(j=1; j<=iLimit; j++){ ++ for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){ ++ if( sLoc.aHash[iKey]==j ) break; + } +- assert( aHash[iKey]==i ); ++ assert( sLoc.aHash[iKey]==j ); + } + } + #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */ +@@ -49873,11 +59360,9 @@ static void walCleanupHash(Wal *pWal){ + */ + static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ + int rc; /* Return code */ +- u32 iZero = 0; /* One less than frame number of aPgno[1] */ +- volatile u32 *aPgno = 0; /* Page number array */ +- volatile ht_slot *aHash = 0; /* Hash table */ ++ WalHashLoc sLoc; /* Wal-index hash table location */ + +- rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero); ++ rc = walHashGet(pWal, walFramePage(iFrame), &sLoc); + + /* Assuming the wal-index file was successfully mapped, populate the + ** page number array and hash table entry. +@@ -49887,15 +59372,16 @@ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ + int idx; /* Value to write to hash-table slot */ + int nCollide; /* Number of hash collisions */ + +- idx = iFrame - iZero; ++ idx = iFrame - sLoc.iZero; + assert( idx <= HASHTABLE_NSLOT/2 + 1 ); + + /* If this is the first entry to be added to this hash-table, zero the + ** entire hash table and aPgno[] array before proceeding. + */ + if( idx==1 ){ +- int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]); +- memset((void*)&aPgno[1], 0, nByte); ++ int nByte = (int)((u8 *)&sLoc.aHash[HASHTABLE_NSLOT] ++ - (u8 *)&sLoc.aPgno[1]); ++ memset((void*)&sLoc.aPgno[1], 0, nByte); + } + + /* If the entry in aPgno[] is already set, then the previous writer +@@ -49904,18 +59390,18 @@ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ + ** Remove the remnants of that writers uncommitted transaction from + ** the hash-table before writing any new entries. + */ +- if( aPgno[idx] ){ ++ if( sLoc.aPgno[idx] ){ + walCleanupHash(pWal); +- assert( !aPgno[idx] ); ++ assert( !sLoc.aPgno[idx] ); + } + + /* Write the aPgno[] array entry and the hash-table slot. */ + nCollide = idx; +- for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){ ++ for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){ + if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT; + } +- aPgno[idx] = iPage; +- aHash[iKey] = (ht_slot)idx; ++ sLoc.aPgno[idx] = iPage; ++ sLoc.aHash[iKey] = (ht_slot)idx; + + #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT + /* Verify that the number of entries in the hash table exactly equals +@@ -49924,7 +59410,7 @@ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ + { + int i; /* Loop counter */ + int nEntry = 0; /* Number of entries in the hash table */ +- for(i=0; i writeLock ); + iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock; +- nLock = SQLITE_SHM_NLOCK - iLock; +- rc = walLockExclusive(pWal, iLock, nLock, 0); ++ rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock); ++ if( rc==SQLITE_OK ){ ++ rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1); ++ if( rc!=SQLITE_OK ){ ++ walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock); ++ } ++ } + if( rc ){ + return rc; + } ++ + WALTRACE(("WAL%p: recovery begin...\n", pWal)); + + memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); +@@ -50051,7 +59544,7 @@ static int walIndexRecover(Wal *pWal){ + szFrame = szPage + WAL_FRAME_HDRSIZE; + aFrame = (u8 *)sqlite3_malloc64(szFrame); + if( !aFrame ){ +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + goto recovery_error; + } + aData = &aFrame[WAL_FRAME_HDRSIZE]; +@@ -50100,6 +59593,7 @@ finished: + */ + pInfo = walCkptInfo(pWal); + pInfo->nBackfill = 0; ++ pInfo->nBackfillAttempted = pWal->hdr.mxFrame; + pInfo->aReadMark[0] = 0; + for(i=1; i aReadMark[i] = READMARK_NOT_USED; + if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame; +@@ -50119,7 +59613,8 @@ finished: + + recovery_error: + WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok")); +- walUnlockExclusive(pWal, iLock, nLock); ++ walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock); ++ walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1); + return rc; + } + +@@ -50127,13 +59622,14 @@ recovery_error: + ** Close an open wal-index. + */ + static void walIndexClose(Wal *pWal, int isDelete){ +- if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){ ++ if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){ + int i; + for(i=0; i nWiData; i++){ + sqlite3_free((void *)pWal->apWiData[i]); + pWal->apWiData[i] = 0; + } +- }else{ ++ } ++ if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){ + sqlite3OsShmUnmap(pWal->pDbFd, isDelete); + } + } +@@ -50171,7 +59667,11 @@ SQLITE_PRIVATE int sqlite3WalOpen( + /* In the amalgamation, the os_unix.c and os_win.c source files come before + ** this source file. Verify that the #defines of the locking byte offsets + ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value. ++ ** For that matter, if the lock offset ever changes from its initial design ++ ** value of 120, we need to know that so there is an assert() to check it. + */ ++ assert( 120==WALINDEX_LOCK_OFFSET ); ++ assert( 136==WALINDEX_HDR_SIZE ); + #ifdef WIN_SHM_BASE + assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET ); + #endif +@@ -50184,7 +59684,7 @@ SQLITE_PRIVATE int sqlite3WalOpen( + *ppWal = 0; + pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile); + if( !pRet ){ +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + + pRet->pVfs = pVfs; +@@ -50362,7 +59862,7 @@ static void walMergesort( + int nMerge = 0; /* Number of elements in list aMerge */ + ht_slot *aMerge = 0; /* List to be merged */ + int iList; /* Index into input list */ +- int iSub = 0; /* Index into aSub array */ ++ u32 iSub = 0; /* Index into aSub array */ + struct Sublist aSub[13]; /* Array of sub-lists */ + + memset(aSub, 0, sizeof(aSub)); +@@ -50373,7 +59873,9 @@ static void walMergesort( + nMerge = 1; + aMerge = &aList[iList]; + for(iSub=0; iList & (1< aList && p->nList<=(1< aList==&aList[iList&~((2< aList, p->nList, &aMerge, &nMerge, aBuffer); +@@ -50384,7 +59886,9 @@ static void walMergesort( + + for(iSub++; iSub nList<=(1< aList==&aList[nList&~((2< aList, p->nList, &aMerge, &nMerge, aBuffer); +@@ -50412,8 +59916,9 @@ static void walIteratorFree(WalIterator *p){ + + /* + ** Construct a WalInterator object that can be used to loop over all +-** pages in the WAL in ascending order. The caller must hold the checkpoint +-** lock. ++** pages in the WAL following frame nBackfill in ascending order. Frames ++** nBackfill or earlier may be included - excluding them is an optimization ++** only. The caller must hold the checkpoint lock. + ** + ** On success, make *pp point to the newly allocated WalInterator object + ** return SQLITE_OK. Otherwise, return an error code. If this routine +@@ -50422,11 +59927,11 @@ static void walIteratorFree(WalIterator *p){ + ** The calling routine should invoke walIteratorFree() to destroy the + ** WalIterator object when it has finished with it. + */ +-static int walIteratorInit(Wal *pWal, WalIterator **pp){ ++static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){ + WalIterator *p; /* Return value */ + int nSegment; /* Number of segments to merge */ + u32 iLast; /* Last frame in log */ +- int nByte; /* Number of bytes to allocate */ ++ sqlite3_int64 nByte; /* Number of bytes to allocate */ + int i; /* Iterator variable */ + ht_slot *aTmp; /* Temp space used by merge-sort */ + int rc = SQLITE_OK; /* Return Code */ +@@ -50444,7 +59949,7 @@ static int walIteratorInit(Wal *pWal, WalIterator **pp){ + + iLast*sizeof(ht_slot); + p = (WalIterator *)sqlite3_malloc64(nByte); + if( !p ){ +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + memset(p, 0, nByte); + p->nSegment = nSegment; +@@ -50456,43 +59961,42 @@ static int walIteratorInit(Wal *pWal, WalIterator **pp){ + sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast) + ); + if( !aTmp ){ +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + } + +- for(i=0; rc==SQLITE_OK && i aSegment[p->nSegment])[iZero]; +- iZero++; ++ aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero]; ++ sLoc.iZero++; + + for(j=0; j aSegment[i].iZero = iZero; ++ walMergesort((u32 *)sLoc.aPgno, aTmp, aIndex, &nEntry); ++ p->aSegment[i].iZero = sLoc.iZero; + p->aSegment[i].nEntry = nEntry; + p->aSegment[i].aIndex = aIndex; +- p->aSegment[i].aPgno = (u32 *)aPgno; ++ p->aSegment[i].aPgno = (u32 *)sLoc.aPgno; + } + } + sqlite3_free(aTmp); + + if( rc!=SQLITE_OK ){ + walIteratorFree(p); ++ p = 0; + } + *pp = p; + return rc; +@@ -50513,7 +60017,7 @@ static int walBusyLock( + ){ + int rc; + do { +- rc = walLockExclusive(pWal, lockIdx, n, 0); ++ rc = walLockExclusive(pWal, lockIdx, n); + }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) ); + return rc; + } +@@ -50553,6 +60057,7 @@ static void walRestartHdr(Wal *pWal, u32 salt1){ + memcpy(&pWal->hdr.aSalt[1], &salt1, 4); + walIndexWriteHdr(pWal); + pInfo->nBackfill = 0; ++ pInfo->nBackfillAttempted = 0; + pInfo->aReadMark[1] = 0; + for(i=2; i aReadMark[i] = READMARK_NOT_USED; + assert( pInfo->aReadMark[0]==0 ); +@@ -50591,6 +60096,7 @@ static void walRestartHdr(Wal *pWal, u32 salt1){ + */ + static int walCheckpoint( + Wal *pWal, /* Wal connection */ ++ sqlite3 *db, /* Check for interrupts on this handle */ + int eMode, /* One of PASSIVE, FULL or RESTART */ + int (*xBusy)(void*), /* Function to call when busy */ + void *pBusyArg, /* Context argument for xBusyHandler */ +@@ -50613,13 +60119,6 @@ static int walCheckpoint( + pInfo = walCkptInfo(pWal); + if( pInfo->nBackfill hdr.mxFrame ){ + +- /* Allocate the iterator */ +- rc = walIteratorInit(pWal, &pIter); +- if( rc!=SQLITE_OK ){ +- return rc; +- } +- assert( pIter ); +- + /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked + ** in the SQLITE_CHECKPOINT_PASSIVE mode. */ + assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 ); +@@ -50656,22 +60155,28 @@ static int walCheckpoint( + } + } + +- if( pInfo->nBackfill nBackfill nBackfill, &pIter); ++ assert( rc==SQLITE_OK || pIter==0 ); ++ } ++ ++ if( pIter + && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK + ){ +- i64 nSize; /* Current size of database file */ + u32 nBackfill = pInfo->nBackfill; + ++ pInfo->nBackfillAttempted = mxSafeFrame; ++ + /* Sync the WAL to disk */ +- if( sync_flags ){ +- rc = sqlite3OsSync(pWal->pWalFd, sync_flags); +- } ++ rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags)); + + /* If the database may grow as a result of this checkpoint, hint + ** about the eventual size of the db file to the VFS layer. + */ + if( rc==SQLITE_OK ){ + i64 nReq = ((i64)mxPage * szPage); ++ i64 nSize; /* Current size of database file */ + rc = sqlite3OsFileSize(pWal->pDbFd, &nSize); + if( rc==SQLITE_OK && nSize pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq); +@@ -50683,6 +60188,10 @@ static int walCheckpoint( + while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){ + i64 iOffset; + assert( walFramePgno(pWal, iFrame)==iDbpage ); ++ if( db->u1.isInterrupted ){ ++ rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT; ++ break; ++ } + if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){ + continue; + } +@@ -50702,8 +60211,8 @@ static int walCheckpoint( + i64 szDb = pWal->hdr.nPage*(i64)szPage; + testcase( IS_BIG_INT(szDb) ); + rc = sqlite3OsTruncate(pWal->pDbFd, szDb); +- if( rc==SQLITE_OK && sync_flags ){ +- rc = sqlite3OsSync(pWal->pDbFd, sync_flags); ++ if( rc==SQLITE_OK ){ ++ rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags)); + } + } + if( rc==SQLITE_OK ){ +@@ -50787,6 +60296,7 @@ static void walLimitSize(Wal *pWal, i64 nMax){ + */ + SQLITE_PRIVATE int sqlite3WalClose( + Wal *pWal, /* Wal to close */ ++ sqlite3 *db, /* For interrupt flag */ + int sync_flags, /* Flags to pass to OsSync() (or 0) */ + int nBuf, + u8 *zBuf /* Buffer of at least nBuf bytes */ +@@ -50803,13 +60313,14 @@ SQLITE_PRIVATE int sqlite3WalClose( + ** + ** The EXCLUSIVE lock is not released before returning. + */ +- rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE); +- if( rc==SQLITE_OK ){ ++ if( zBuf!=0 ++ && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE)) ++ ){ + if( pWal->exclusiveMode==WAL_NORMAL_MODE ){ + pWal->exclusiveMode = WAL_EXCLUSIVE_MODE; + } +- rc = sqlite3WalCheckpoint( +- pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0 ++ rc = sqlite3WalCheckpoint(pWal, db, ++ SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0 + ); + if( rc==SQLITE_OK ){ + int bPersist = -1; +@@ -50910,6 +60421,12 @@ static int walIndexTryHdr(Wal *pWal, int *pChanged){ + return 0; + } + ++/* ++** This is the value that walTryBeginRead returns when it needs to ++** be retried. ++*/ ++#define WAL_RETRY (-1) ++ + /* + ** Read the wal-index header from the wal-index and into pWal->hdr. + ** If the wal-header appears to be corrupt, try to reconstruct the +@@ -50933,9 +60450,29 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){ + assert( pChanged ); + rc = walIndexPage(pWal, 0, &page0); + if( rc!=SQLITE_OK ){ +- return rc; +- }; +- assert( page0 || pWal->writeLock==0 ); ++ assert( rc!=SQLITE_READONLY ); /* READONLY changed to OK in walIndexPage */ ++ if( rc==SQLITE_READONLY_CANTINIT ){ ++ /* The SQLITE_READONLY_CANTINIT return means that the shared-memory ++ ** was openable but is not writable, and this thread is unable to ++ ** confirm that another write-capable connection has the shared-memory ++ ** open, and hence the content of the shared-memory is unreliable, ++ ** since the shared-memory might be inconsistent with the WAL file ++ ** and there is no writer on hand to fix it. */ ++ assert( page0==0 ); ++ assert( pWal->writeLock==0 ); ++ assert( pWal->readOnly & WAL_SHM_RDONLY ); ++ pWal->bShmUnreliable = 1; ++ pWal->exclusiveMode = WAL_HEAPMEMORY_MODE; ++ *pChanged = 1; ++ }else{ ++ return rc; /* Any other non-OK return is just an error */ ++ } ++ }else{ ++ /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock ++ ** is zero, which prevents the SHM from growing */ ++ testcase( page0!=0 ); ++ } ++ assert( page0!=0 || pWal->writeLock==0 ); + + /* If the first page of the wal-index has been mapped, try to read the + ** wal-index header immediately, without holding any lock. This usually +@@ -50949,12 +60486,12 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){ + */ + assert( badHdr==0 || pWal->writeLock==0 ); + if( badHdr ){ +- if( pWal->readOnly & WAL_SHM_RDONLY ){ ++ if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){ + if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){ + walUnlockShared(pWal, WAL_WRITE_LOCK); + rc = SQLITE_READONLY_RECOVERY; + } +- }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1, 1)) ){ ++ }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){ + pWal->writeLock = 1; + if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){ + badHdr = walIndexTryHdr(pWal, pChanged); +@@ -50979,15 +60516,193 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){ + if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){ + rc = SQLITE_CANTOPEN_BKPT; + } ++ if( pWal->bShmUnreliable ){ ++ if( rc!=SQLITE_OK ){ ++ walIndexClose(pWal, 0); ++ pWal->bShmUnreliable = 0; ++ assert( pWal->nWiData>0 && pWal->apWiData[0]==0 ); ++ /* walIndexRecover() might have returned SHORT_READ if a concurrent ++ ** writer truncated the WAL out from under it. If that happens, it ++ ** indicates that a writer has fixed the SHM file for us, so retry */ ++ if( rc==SQLITE_IOERR_SHORT_READ ) rc = WAL_RETRY; ++ } ++ pWal->exclusiveMode = WAL_NORMAL_MODE; ++ } + + return rc; + } + + /* +-** This is the value that walTryBeginRead returns when it needs to +-** be retried. ++** Open a transaction in a connection where the shared-memory is read-only ++** and where we cannot verify that there is a separate write-capable connection ++** on hand to keep the shared-memory up-to-date with the WAL file. ++** ++** This can happen, for example, when the shared-memory is implemented by ++** memory-mapping a *-shm file, where a prior writer has shut down and ++** left the *-shm file on disk, and now the present connection is trying ++** to use that database but lacks write permission on the *-shm file. ++** Other scenarios are also possible, depending on the VFS implementation. ++** ++** Precondition: ++** ++** The *-wal file has been read and an appropriate wal-index has been ++** constructed in pWal->apWiData[] using heap memory instead of shared ++** memory. ++** ++** If this function returns SQLITE_OK, then the read transaction has ++** been successfully opened. In this case output variable (*pChanged) ++** is set to true before returning if the caller should discard the ++** contents of the page cache before proceeding. Or, if it returns ++** WAL_RETRY, then the heap memory wal-index has been discarded and ++** the caller should retry opening the read transaction from the ++** beginning (including attempting to map the *-shm file). ++** ++** If an error occurs, an SQLite error code is returned. + */ +-#define WAL_RETRY (-1) ++static int walBeginShmUnreliable(Wal *pWal, int *pChanged){ ++ i64 szWal; /* Size of wal file on disk in bytes */ ++ i64 iOffset; /* Current offset when reading wal file */ ++ u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */ ++ u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */ ++ int szFrame; /* Number of bytes in buffer aFrame[] */ ++ u8 *aData; /* Pointer to data part of aFrame buffer */ ++ volatile void *pDummy; /* Dummy argument for xShmMap */ ++ int rc; /* Return code */ ++ u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */ ++ ++ assert( pWal->bShmUnreliable ); ++ assert( pWal->readOnly & WAL_SHM_RDONLY ); ++ assert( pWal->nWiData>0 && pWal->apWiData[0] ); ++ ++ /* Take WAL_READ_LOCK(0). This has the effect of preventing any ++ ** writers from running a checkpoint, but does not stop them ++ ** from running recovery. */ ++ rc = walLockShared(pWal, WAL_READ_LOCK(0)); ++ if( rc!=SQLITE_OK ){ ++ if( rc==SQLITE_BUSY ) rc = WAL_RETRY; ++ goto begin_unreliable_shm_out; ++ } ++ pWal->readLock = 0; ++ ++ /* Check to see if a separate writer has attached to the shared-memory area, ++ ** thus making the shared-memory "reliable" again. Do this by invoking ++ ** the xShmMap() routine of the VFS and looking to see if the return ++ ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT. ++ ** ++ ** If the shared-memory is now "reliable" return WAL_RETRY, which will ++ ** cause the heap-memory WAL-index to be discarded and the actual ++ ** shared memory to be used in its place. ++ ** ++ ** This step is important because, even though this connection is holding ++ ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might ++ ** have already checkpointed the WAL file and, while the current ++ ** is active, wrap the WAL and start overwriting frames that this ++ ** process wants to use. ++ ** ++ ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has ++ ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY ++ ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations, ++ ** even if some external agent does a "chmod" to make the shared-memory ++ ** writable by us, until sqlite3OsShmUnmap() has been called. ++ ** This is a requirement on the VFS implementation. ++ */ ++ rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy); ++ assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */ ++ if( rc!=SQLITE_READONLY_CANTINIT ){ ++ rc = (rc==SQLITE_READONLY ? WAL_RETRY : rc); ++ goto begin_unreliable_shm_out; ++ } ++ ++ /* We reach this point only if the real shared-memory is still unreliable. ++ ** Assume the in-memory WAL-index substitute is correct and load it ++ ** into pWal->hdr. ++ */ ++ memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr)); ++ ++ /* Make sure some writer hasn't come in and changed the WAL file out ++ ** from under us, then disconnected, while we were not looking. ++ */ ++ rc = sqlite3OsFileSize(pWal->pWalFd, &szWal); ++ if( rc!=SQLITE_OK ){ ++ goto begin_unreliable_shm_out; ++ } ++ if( szWal hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY); ++ goto begin_unreliable_shm_out; ++ } ++ ++ /* Check the salt keys at the start of the wal file still match. */ ++ rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0); ++ if( rc!=SQLITE_OK ){ ++ goto begin_unreliable_shm_out; ++ } ++ if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){ ++ /* Some writer has wrapped the WAL file while we were not looking. ++ ** Return WAL_RETRY which will cause the in-memory WAL-index to be ++ ** rebuilt. */ ++ rc = WAL_RETRY; ++ goto begin_unreliable_shm_out; ++ } ++ ++ /* Allocate a buffer to read frames into */ ++ szFrame = pWal->hdr.szPage + WAL_FRAME_HDRSIZE; ++ aFrame = (u8 *)sqlite3_malloc64(szFrame); ++ if( aFrame==0 ){ ++ rc = SQLITE_NOMEM_BKPT; ++ goto begin_unreliable_shm_out; ++ } ++ aData = &aFrame[WAL_FRAME_HDRSIZE]; ++ ++ /* Check to see if a complete transaction has been appended to the ++ ** wal file since the heap-memory wal-index was created. If so, the ++ ** heap-memory wal-index is discarded and WAL_RETRY returned to ++ ** the caller. */ ++ aSaveCksum[0] = pWal->hdr.aFrameCksum[0]; ++ aSaveCksum[1] = pWal->hdr.aFrameCksum[1]; ++ for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->hdr.szPage); ++ iOffset+szFrame<=szWal; ++ iOffset+=szFrame ++ ){ ++ u32 pgno; /* Database page number for frame */ ++ u32 nTruncate; /* dbsize field from frame header */ ++ ++ /* Read and decode the next log frame. */ ++ rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset); ++ if( rc!=SQLITE_OK ) break; ++ if( !walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame) ) break; ++ ++ /* If nTruncate is non-zero, then a complete transaction has been ++ ** appended to this wal file. Set rc to WAL_RETRY and break out of ++ ** the loop. */ ++ if( nTruncate ){ ++ rc = WAL_RETRY; ++ break; ++ } ++ } ++ pWal->hdr.aFrameCksum[0] = aSaveCksum[0]; ++ pWal->hdr.aFrameCksum[1] = aSaveCksum[1]; ++ ++ begin_unreliable_shm_out: ++ sqlite3_free(aFrame); ++ if( rc!=SQLITE_OK ){ ++ int i; ++ for(i=0; i nWiData; i++){ ++ sqlite3_free((void*)pWal->apWiData[i]); ++ pWal->apWiData[i] = 0; ++ } ++ pWal->bShmUnreliable = 0; ++ sqlite3WalEndReadTransaction(pWal); ++ *pChanged = 1; ++ } ++ return rc; ++} + + /* + ** Attempt to start a read transaction. This might fail due to a race or +@@ -51003,7 +60718,7 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){ + ** checkpointed. If useWal==0 then this routine calls walIndexReadHdr() + ** to make a copy of the wal-index header into pWal->hdr. If the + ** wal-index header has changed, *pChanged is set to 1 (as an indication +-** to the caller that the local paget cache is obsolete and needs to be ++** to the caller that the local page cache is obsolete and needs to be + ** flushed.) When useWal==1, the wal-index header is assumed to already + ** be loaded and the pChanged parameter is unused. + ** +@@ -51045,9 +60760,13 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ + int mxI; /* Index of largest aReadMark[] value */ + int i; /* Loop counter */ + int rc = SQLITE_OK; /* Return code */ ++ u32 mxFrame; /* Wal frame to lock to */ + + assert( pWal->readLock<0 ); /* Not currently locked */ + ++ /* useWal may only be set for read/write connections */ ++ assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 ); ++ + /* Take steps to avoid spinning forever if there is a protocol error. + ** + ** Circumstances that cause a RETRY should only last for the briefest +@@ -51076,7 +60795,10 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ + } + + if( !useWal ){ +- rc = walIndexReadHdr(pWal, pChanged); ++ assert( rc==SQLITE_OK ); ++ if( pWal->bShmUnreliable==0 ){ ++ rc = walIndexReadHdr(pWal, pChanged); ++ } + if( rc==SQLITE_BUSY ){ + /* If there is not a recovery running in another thread or process + ** then convert BUSY errors to WAL_RETRY. If recovery is known to +@@ -51105,10 +60827,19 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ + if( rc!=SQLITE_OK ){ + return rc; + } ++ else if( pWal->bShmUnreliable ){ ++ return walBeginShmUnreliable(pWal, pChanged); ++ } + } + ++ assert( pWal->nWiData>0 ); ++ assert( pWal->apWiData[0]!=0 ); + pInfo = walCkptInfo(pWal); +- if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){ ++ if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ++#ifdef SQLITE_ENABLE_SNAPSHOT ++ && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0) ++#endif ++ ){ + /* The WAL has been completely backfilled (or it is empty). + ** and can be safely ignored. + */ +@@ -51146,73 +60877,167 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ + */ + mxReadMark = 0; + mxI = 0; ++ mxFrame = pWal->hdr.mxFrame; ++#ifdef SQLITE_ENABLE_SNAPSHOT ++ if( pWal->pSnapshot && pWal->pSnapshot->mxFrame pSnapshot->mxFrame; ++ } ++#endif + for(i=1; i aReadMark[i]; +- if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){ ++ u32 thisMark = AtomicLoad(pInfo->aReadMark+i); ++ if( mxReadMark<=thisMark && thisMark<=mxFrame ){ + assert( thisMark!=READMARK_NOT_USED ); + mxReadMark = thisMark; + mxI = i; + } + } +- /* There was once an "if" here. The extra "{" is to preserve indentation. */ +- { +- if( (pWal->readOnly & WAL_SHM_RDONLY)==0 +- && (mxReadMark hdr.mxFrame || mxI==0) +- ){ +- for(i=1; i aReadMark[i] = pWal->hdr.mxFrame; +- mxI = i; +- walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); +- break; +- }else if( rc!=SQLITE_BUSY ){ +- return rc; +- } ++ if( (pWal->readOnly & WAL_SHM_RDONLY)==0 ++ && (mxReadMark aReadMark+i,mxFrame); ++ mxI = i; ++ walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); ++ break; ++ }else if( rc!=SQLITE_BUSY ){ ++ return rc; + } + } +- if( mxI==0 ){ +- assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 ); +- return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK; +- } ++ } ++ if( mxI==0 ){ ++ assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 ); ++ return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT; ++ } + +- rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); +- if( rc ){ +- return rc==SQLITE_BUSY ? WAL_RETRY : rc; +- } +- /* Now that the read-lock has been obtained, check that neither the +- ** value in the aReadMark[] array or the contents of the wal-index +- ** header have changed. +- ** +- ** It is necessary to check that the wal-index header did not change +- ** between the time it was read and when the shared-lock was obtained +- ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility +- ** that the log file may have been wrapped by a writer, or that frames +- ** that occur later in the log than pWal->hdr.mxFrame may have been +- ** copied into the database by a checkpointer. If either of these things +- ** happened, then reading the database with the current value of +- ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry +- ** instead. +- ** +- ** This does not guarantee that the copy of the wal-index header is up to +- ** date before proceeding. That would not be possible without somehow +- ** blocking writers. It only guarantees that a dangerous checkpoint or +- ** log-wrap (either of which would require an exclusive lock on +- ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid. +- */ +- walShmBarrier(pWal); +- if( pInfo->aReadMark[mxI]!=mxReadMark +- || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) +- ){ +- walUnlockShared(pWal, WAL_READ_LOCK(mxI)); +- return WAL_RETRY; +- }else{ +- assert( mxReadMark<=pWal->hdr.mxFrame ); +- pWal->readLock = (i16)mxI; ++ rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); ++ if( rc ){ ++ return rc==SQLITE_BUSY ? WAL_RETRY : rc; ++ } ++ /* Now that the read-lock has been obtained, check that neither the ++ ** value in the aReadMark[] array or the contents of the wal-index ++ ** header have changed. ++ ** ++ ** It is necessary to check that the wal-index header did not change ++ ** between the time it was read and when the shared-lock was obtained ++ ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility ++ ** that the log file may have been wrapped by a writer, or that frames ++ ** that occur later in the log than pWal->hdr.mxFrame may have been ++ ** copied into the database by a checkpointer. If either of these things ++ ** happened, then reading the database with the current value of ++ ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry ++ ** instead. ++ ** ++ ** Before checking that the live wal-index header has not changed ++ ** since it was read, set Wal.minFrame to the first frame in the wal ++ ** file that has not yet been checkpointed. This client will not need ++ ** to read any frames earlier than minFrame from the wal file - they ++ ** can be safely read directly from the database file. ++ ** ++ ** Because a ShmBarrier() call is made between taking the copy of ++ ** nBackfill and checking that the wal-header in shared-memory still ++ ** matches the one cached in pWal->hdr, it is guaranteed that the ++ ** checkpointer that set nBackfill was not working with a wal-index ++ ** header newer than that cached in pWal->hdr. If it were, that could ++ ** cause a problem. The checkpointer could omit to checkpoint ++ ** a version of page X that lies before pWal->minFrame (call that version ++ ** A) on the basis that there is a newer version (version B) of the same ++ ** page later in the wal file. But if version B happens to like past ++ ** frame pWal->hdr.mxFrame - then the client would incorrectly assume ++ ** that it can read version A from the database file. However, since ++ ** we can guarantee that the checkpointer that set nBackfill could not ++ ** see any pages past pWal->hdr.mxFrame, this problem does not come up. ++ */ ++ pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1; ++ walShmBarrier(pWal); ++ if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark ++ || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ++ ){ ++ walUnlockShared(pWal, WAL_READ_LOCK(mxI)); ++ return WAL_RETRY; ++ }else{ ++ assert( mxReadMark<=pWal->hdr.mxFrame ); ++ pWal->readLock = (i16)mxI; ++ } ++ return rc; ++} ++ ++#ifdef SQLITE_ENABLE_SNAPSHOT ++/* ++** Attempt to reduce the value of the WalCkptInfo.nBackfillAttempted ++** variable so that older snapshots can be accessed. To do this, loop ++** through all wal frames from nBackfillAttempted to (nBackfill+1), ++** comparing their content to the corresponding page with the database ++** file, if any. Set nBackfillAttempted to the frame number of the ++** first frame for which the wal file content matches the db file. ++** ++** This is only really safe if the file-system is such that any page ++** writes made by earlier checkpointers were atomic operations, which ++** is not always true. It is also possible that nBackfillAttempted ++** may be left set to a value larger than expected, if a wal frame ++** contains content that duplicate of an earlier version of the same ++** page. ++** ++** SQLITE_OK is returned if successful, or an SQLite error code if an ++** error occurs. It is not an error if nBackfillAttempted cannot be ++** decreased at all. ++*/ ++SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal){ ++ int rc; ++ ++ assert( pWal->readLock>=0 ); ++ rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1); ++ if( rc==SQLITE_OK ){ ++ volatile WalCkptInfo *pInfo = walCkptInfo(pWal); ++ int szPage = (int)pWal->szPage; ++ i64 szDb; /* Size of db file in bytes */ ++ ++ rc = sqlite3OsFileSize(pWal->pDbFd, &szDb); ++ if( rc==SQLITE_OK ){ ++ void *pBuf1 = sqlite3_malloc(szPage); ++ void *pBuf2 = sqlite3_malloc(szPage); ++ if( pBuf1==0 || pBuf2==0 ){ ++ rc = SQLITE_NOMEM; ++ }else{ ++ u32 i = pInfo->nBackfillAttempted; ++ for(i=pInfo->nBackfillAttempted; i>pInfo->nBackfill; i--){ ++ WalHashLoc sLoc; /* Hash table location */ ++ u32 pgno; /* Page number in db file */ ++ i64 iDbOff; /* Offset of db file entry */ ++ i64 iWalOff; /* Offset of wal file entry */ ++ ++ rc = walHashGet(pWal, walFramePage(i), &sLoc); ++ if( rc!=SQLITE_OK ) break; ++ pgno = sLoc.aPgno[i-sLoc.iZero]; ++ iDbOff = (i64)(pgno-1) * szPage; ++ ++ if( iDbOff+szPage<=szDb ){ ++ iWalOff = walFrameOffset(i, szPage) + WAL_FRAME_HDRSIZE; ++ rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff); ++ ++ if( rc==SQLITE_OK ){ ++ rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff); ++ } ++ ++ if( rc!=SQLITE_OK || 0==memcmp(pBuf1, pBuf2, szPage) ){ ++ break; ++ } ++ } ++ ++ pInfo->nBackfillAttempted = i-1; ++ } ++ } ++ ++ sqlite3_free(pBuf1); ++ sqlite3_free(pBuf2); + } ++ walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); + } ++ + return rc; + } ++#endif /* SQLITE_ENABLE_SNAPSHOT */ + + /* + ** Begin a read transaction on the database. +@@ -51225,13 +61050,21 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ + ** + ** If the database contents have changes since the previous read + ** transaction, then *pChanged is set to 1 before returning. The +-** Pager layer will use this to know that is cache is stale and ++** Pager layer will use this to know that its cache is stale and + ** needs to be flushed. + */ + SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ + int rc; /* Return code */ + int cnt = 0; /* Number of TryBeginRead attempts */ + ++#ifdef SQLITE_ENABLE_SNAPSHOT ++ int bChanged = 0; ++ WalIndexHdr *pSnapshot = pWal->pSnapshot; ++ if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ ++ bChanged = 1; ++ } ++#endif ++ + do{ + rc = walTryBeginRead(pWal, pChanged, 0, ++cnt); + }while( rc==WAL_RETRY ); +@@ -51239,6 +61072,71 @@ SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ + testcase( (rc&0xff)==SQLITE_IOERR ); + testcase( rc==SQLITE_PROTOCOL ); + testcase( rc==SQLITE_OK ); ++ ++#ifdef SQLITE_ENABLE_SNAPSHOT ++ if( rc==SQLITE_OK ){ ++ if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ ++ /* At this point the client has a lock on an aReadMark[] slot holding ++ ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr ++ ** is populated with the wal-index header corresponding to the head ++ ** of the wal file. Verify that pSnapshot is still valid before ++ ** continuing. Reasons why pSnapshot might no longer be valid: ++ ** ++ ** (1) The WAL file has been reset since the snapshot was taken. ++ ** In this case, the salt will have changed. ++ ** ++ ** (2) A checkpoint as been attempted that wrote frames past ++ ** pSnapshot->mxFrame into the database file. Note that the ++ ** checkpoint need not have completed for this to cause problems. ++ */ ++ volatile WalCkptInfo *pInfo = walCkptInfo(pWal); ++ ++ assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 ); ++ assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame ); ++ ++ /* It is possible that there is a checkpointer thread running ++ ** concurrent with this code. If this is the case, it may be that the ++ ** checkpointer has already determined that it will checkpoint ++ ** snapshot X, where X is later in the wal file than pSnapshot, but ++ ** has not yet set the pInfo->nBackfillAttempted variable to indicate ++ ** its intent. To avoid the race condition this leads to, ensure that ++ ** there is no checkpointer process by taking a shared CKPT lock ++ ** before checking pInfo->nBackfillAttempted. ++ ** ++ ** TODO: Does the aReadMark[] lock prevent a checkpointer from doing ++ ** this already? ++ */ ++ rc = walLockShared(pWal, WAL_CKPT_LOCK); ++ ++ if( rc==SQLITE_OK ){ ++ /* Check that the wal file has not been wrapped. Assuming that it has ++ ** not, also check that no checkpointer has attempted to checkpoint any ++ ** frames beyond pSnapshot->mxFrame. If either of these conditions are ++ ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr ++ ** with *pSnapshot and set *pChanged as appropriate for opening the ++ ** snapshot. */ ++ if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt)) ++ && pSnapshot->mxFrame>=pInfo->nBackfillAttempted ++ ){ ++ assert( pWal->readLock>0 ); ++ memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr)); ++ *pChanged = bChanged; ++ }else{ ++ rc = SQLITE_ERROR_SNAPSHOT; ++ } ++ ++ /* Release the shared CKPT lock obtained above. */ ++ walUnlockShared(pWal, WAL_CKPT_LOCK); ++ pWal->minFrame = 1; ++ } ++ ++ ++ if( rc!=SQLITE_OK ){ ++ sqlite3WalEndReadTransaction(pWal); ++ } ++ } ++ } ++#endif + return rc; + } + +@@ -51270,6 +61168,7 @@ SQLITE_PRIVATE int sqlite3WalFindFrame( + u32 iRead = 0; /* If !=0, WAL frame to return data from */ + u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */ + int iHash; /* Used to loop through N hash tables */ ++ int iMinHash; + + /* This routine is only be called from within a read transaction. */ + assert( pWal->readLock>=0 || pWal->lockError ); +@@ -51280,7 +61179,7 @@ SQLITE_PRIVATE int sqlite3WalFindFrame( + ** then the WAL is ignored by the reader so return early, as if the + ** WAL were empty. + */ +- if( iLast==0 || pWal->readLock==0 ){ ++ if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){ + *piRead = 0; + return SQLITE_OK; + } +@@ -51310,22 +61209,22 @@ SQLITE_PRIVATE int sqlite3WalFindFrame( + ** This condition filters out entries that were added to the hash + ** table after the current read-transaction had started. + */ +- for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){ +- volatile ht_slot *aHash; /* Pointer to hash table */ +- volatile u32 *aPgno; /* Pointer to array of page numbers */ +- u32 iZero; /* Frame number corresponding to aPgno[0] */ ++ iMinHash = walFramePage(pWal->minFrame); ++ for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){ ++ WalHashLoc sLoc; /* Hash table location */ + int iKey; /* Hash slot index */ + int nCollide; /* Number of hash collisions remaining */ + int rc; /* Error code */ + +- rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero); ++ rc = walHashGet(pWal, iHash, &sLoc); + if( rc!=SQLITE_OK ){ + return rc; + } + nCollide = HASHTABLE_NSLOT; +- for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){ +- u32 iFrame = aHash[iKey] + iZero; +- if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){ ++ for(iKey=walHash(pgno); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){ ++ u32 iFrame = sLoc.aHash[iKey] + sLoc.iZero; ++ if( iFrame<=iLast && iFrame>=pWal->minFrame ++ && sLoc.aPgno[sLoc.aHash[iKey]]==pgno ){ + assert( iFrame>iRead || CORRUPT_DB ); + iRead = iFrame; + } +@@ -51333,6 +61232,7 @@ SQLITE_PRIVATE int sqlite3WalFindFrame( + return SQLITE_CORRUPT_BKPT; + } + } ++ if( iRead ) break; + } + + #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT +@@ -51342,7 +61242,8 @@ SQLITE_PRIVATE int sqlite3WalFindFrame( + { + u32 iRead2 = 0; + u32 iTest; +- for(iTest=iLast; iTest>0; iTest--){ ++ assert( pWal->bShmUnreliable || pWal->minFrame>0 ); ++ for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){ + if( walFramePgno(pWal, iTest)==pgno ){ + iRead2 = iTest; + break; +@@ -51408,6 +61309,7 @@ SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){ + /* Cannot start a write transaction without first holding a read + ** transaction. */ + assert( pWal->readLock>=0 ); ++ assert( pWal->writeLock==0 && pWal->iReCksum==0 ); + + if( pWal->readOnly ){ + return SQLITE_READONLY; +@@ -51416,7 +61318,7 @@ SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){ + /* Only one writer allowed at a time. Get the write lock. Return + ** SQLITE_BUSY if unable. + */ +- rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1, 0); ++ rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); + if( rc ){ + return rc; + } +@@ -51443,6 +61345,7 @@ SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){ + if( pWal->writeLock ){ + walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); + pWal->writeLock = 0; ++ pWal->iReCksum = 0; + pWal->truncateOnCommit = 0; + } + return SQLITE_OK; +@@ -51561,7 +61464,7 @@ static int walRestartLog(Wal *pWal){ + if( pInfo->nBackfill>0 ){ + u32 salt1; + sqlite3_randomness(4, &salt1); +- rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1, 0); ++ rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1); + if( rc==SQLITE_OK ){ + /* If all readers are using WAL_READ_LOCK(0) (in other words if no + ** readers are currently using the WAL), then the transactions +@@ -51628,8 +61531,8 @@ static int walWriteToLog( + iOffset += iFirstAmt; + iAmt -= iFirstAmt; + pContent = (void*)(iFirstAmt + (char*)pContent); +- assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) ); +- rc = sqlite3OsSync(p->pFd, p->syncFlags & SQLITE_SYNC_MASK); ++ assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 ); ++ rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags)); + if( iAmt==0 || rc ) return rc; + } + rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset); +@@ -51649,7 +61552,7 @@ static int walWriteOneFrame( + void *pData; /* Data actually written */ + u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */ + #if defined(SQLITE_HAS_CODEC) +- if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM; ++ if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM_BKPT; + #else + pData = pPage->pData; + #endif +@@ -51661,6 +61564,59 @@ static int walWriteOneFrame( + return rc; + } + ++/* ++** This function is called as part of committing a transaction within which ++** one or more frames have been overwritten. It updates the checksums for ++** all frames written to the wal file by the current transaction starting ++** with the earliest to have been overwritten. ++** ++** SQLITE_OK is returned if successful, or an SQLite error code otherwise. ++*/ ++static int walRewriteChecksums(Wal *pWal, u32 iLast){ ++ const int szPage = pWal->szPage;/* Database page size */ ++ int rc = SQLITE_OK; /* Return code */ ++ u8 *aBuf; /* Buffer to load data from wal file into */ ++ u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */ ++ u32 iRead; /* Next frame to read from wal file */ ++ i64 iCksumOff; ++ ++ aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE); ++ if( aBuf==0 ) return SQLITE_NOMEM_BKPT; ++ ++ /* Find the checksum values to use as input for the recalculating the ++ ** first checksum. If the first frame is frame 1 (implying that the current ++ ** transaction restarted the wal file), these values must be read from the ++ ** wal-file header. Otherwise, read them from the frame header of the ++ ** previous frame. */ ++ assert( pWal->iReCksum>0 ); ++ if( pWal->iReCksum==1 ){ ++ iCksumOff = 24; ++ }else{ ++ iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16; ++ } ++ rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff); ++ pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf); ++ pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]); ++ ++ iRead = pWal->iReCksum; ++ pWal->iReCksum = 0; ++ for(; rc==SQLITE_OK && iRead<=iLast; iRead++){ ++ i64 iOff = walFrameOffset(iRead, szPage); ++ rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff); ++ if( rc==SQLITE_OK ){ ++ u32 iPgno, nDbSize; ++ iPgno = sqlite3Get4byte(aBuf); ++ nDbSize = sqlite3Get4byte(&aBuf[4]); ++ ++ walEncodeFrame(pWal, iPgno, nDbSize, &aBuf[WAL_FRAME_HDRSIZE], aFrame); ++ rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff); ++ } ++ } ++ ++ sqlite3_free(aBuf); ++ return rc; ++} ++ + /* + ** Write a set of frames to the log. The caller must hold the write-lock + ** on the log file (obtained using sqlite3WalBeginWriteTransaction()). +@@ -51681,6 +61637,8 @@ SQLITE_PRIVATE int sqlite3WalFrames( + int szFrame; /* The size of a single frame */ + i64 iOffset; /* Next byte to write in WAL file */ + WalWriter w; /* The writer */ ++ u32 iFirst = 0; /* First frame that may be overwritten */ ++ WalIndexHdr *pLive; /* Pointer to shared header */ + + assert( pList ); + assert( pWal->writeLock ); +@@ -51696,6 +61654,11 @@ SQLITE_PRIVATE int sqlite3WalFrames( + } + #endif + ++ pLive = (WalIndexHdr*)walIndexHdr(pWal); ++ if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){ ++ iFirst = pLive->mxFrame+1; ++ } ++ + /* See if it is possible to write these frames into the start of the + ** log file, instead of appending to it at pWal->hdr.mxFrame. + */ +@@ -51739,10 +61702,10 @@ SQLITE_PRIVATE int sqlite3WalFrames( + ** an out-of-order write following a WAL restart could result in + ** database corruption. See the ticket: + ** +- ** http://localhost:591/sqlite/info/ff5be73dee ++ ** https://sqlite.org/src/info/ff5be73dee + */ +- if( pWal->syncHeader && sync_flags ){ +- rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK); ++ if( pWal->syncHeader ){ ++ rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags)); + if( rc ) return rc; + } + } +@@ -51760,6 +61723,33 @@ SQLITE_PRIVATE int sqlite3WalFrames( + /* Write all frames into the log file exactly once */ + for(p=pList; p; p=p->pDirty){ + int nDbSize; /* 0 normally. Positive == commit flag */ ++ ++ /* Check if this page has already been written into the wal file by ++ ** the current transaction. If so, overwrite the existing frame and ++ ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that ++ ** checksums must be recomputed when the transaction is committed. */ ++ if( iFirst && (p->pDirty || isCommit==0) ){ ++ u32 iWrite = 0; ++ VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite); ++ assert( rc==SQLITE_OK || iWrite==0 ); ++ if( iWrite>=iFirst ){ ++ i64 iOff = walFrameOffset(iWrite, szPage) + WAL_FRAME_HDRSIZE; ++ void *pData; ++ if( pWal->iReCksum==0 || iWrite iReCksum ){ ++ pWal->iReCksum = iWrite; ++ } ++#if defined(SQLITE_HAS_CODEC) ++ if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM; ++#else ++ pData = p->pData; ++#endif ++ rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff); ++ if( rc ) return rc; ++ p->flags &= ~PGHDR_WAL_APPEND; ++ continue; ++ } ++ } ++ + iFrame++; + assert( iOffset==walFrameOffset(iFrame, szPage) ); + nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0; +@@ -51767,6 +61757,13 @@ SQLITE_PRIVATE int sqlite3WalFrames( + if( rc ) return rc; + pLast = p; + iOffset += szFrame; ++ p->flags |= PGHDR_WAL_APPEND; ++ } ++ ++ /* Recalculate checksums within the wal file if required. */ ++ if( isCommit && pWal->iReCksum ){ ++ rc = walRewriteChecksums(pWal, iFrame); ++ if( rc ) return rc; + } + + /* If this is the end of a transaction, then we might need to pad +@@ -51783,18 +61780,23 @@ SQLITE_PRIVATE int sqlite3WalFrames( + ** sector boundary is synced; the part of the last frame that extends + ** past the sector boundary is written after the sync. + */ +- if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){ ++ if( isCommit && WAL_SYNC_FLAGS(sync_flags)!=0 ){ ++ int bSync = 1; + if( pWal->padToSectorBoundary ){ + int sectorSize = sqlite3SectorSize(pWal->pWalFd); + w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize; ++ bSync = (w.iSyncPoint==iOffset); ++ testcase( bSync ); + while( iOffset hdr.mxFrame; + for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){ ++ if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue; + iFrame++; + rc = walIndexAppend(pWal, iFrame, p->pgno); + } +@@ -51860,6 +61863,7 @@ SQLITE_PRIVATE int sqlite3WalFrames( + */ + SQLITE_PRIVATE int sqlite3WalCheckpoint( + Wal *pWal, /* Wal connection */ ++ sqlite3 *db, /* Check this handle's interrupt flag */ + int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */ + int (*xBusy)(void*), /* Function to call when busy */ + void *pBusyArg, /* Context argument for xBusyHandler */ +@@ -51886,7 +61890,7 @@ SQLITE_PRIVATE int sqlite3WalCheckpoint( + + /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive + ** "checkpoint" lock on the database file. */ +- rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1, 0); ++ rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1); + if( rc ){ + /* EVIDENCE-OF: R-10421-19736 If any other process is running a + ** checkpoint operation at the same time, the lock cannot be obtained and +@@ -51930,10 +61934,11 @@ SQLITE_PRIVATE int sqlite3WalCheckpoint( + + /* Copy data from the log to the database file. */ + if( rc==SQLITE_OK ){ ++ + if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){ + rc = SQLITE_CORRUPT_BKPT; + }else{ +- rc = walCheckpoint(pWal, eMode2, xBusy2, pBusyArg, sync_flags, zBuf); ++ rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags, zBuf); + } + + /* If no error occurred, set the output variables. */ +@@ -52014,24 +62019,24 @@ SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){ + assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) ); + + if( op==0 ){ +- if( pWal->exclusiveMode ){ +- pWal->exclusiveMode = 0; ++ if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){ ++ pWal->exclusiveMode = WAL_NORMAL_MODE; + if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){ +- pWal->exclusiveMode = 1; ++ pWal->exclusiveMode = WAL_EXCLUSIVE_MODE; + } +- rc = pWal->exclusiveMode==0; ++ rc = pWal->exclusiveMode==WAL_NORMAL_MODE; + }else{ + /* Already in locking_mode=NORMAL */ + rc = 0; + } + }else if( op>0 ){ +- assert( pWal->exclusiveMode==0 ); ++ assert( pWal->exclusiveMode==WAL_NORMAL_MODE ); + assert( pWal->readLock>=0 ); + walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); +- pWal->exclusiveMode = 1; ++ pWal->exclusiveMode = WAL_EXCLUSIVE_MODE; + rc = 1; + }else{ +- rc = pWal->exclusiveMode==0; ++ rc = pWal->exclusiveMode==WAL_NORMAL_MODE; + } + return rc; + } +@@ -52045,6 +62050,94 @@ SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){ + return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ); + } + ++#ifdef SQLITE_ENABLE_SNAPSHOT ++/* Create a snapshot object. The content of a snapshot is opaque to ++** every other subsystem, so the WAL module can put whatever it needs ++** in the object. ++*/ ++SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){ ++ int rc = SQLITE_OK; ++ WalIndexHdr *pRet; ++ static const u32 aZero[4] = { 0, 0, 0, 0 }; ++ ++ assert( pWal->readLock>=0 && pWal->writeLock==0 ); ++ ++ if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){ ++ *ppSnapshot = 0; ++ return SQLITE_ERROR; ++ } ++ pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr)); ++ if( pRet==0 ){ ++ rc = SQLITE_NOMEM_BKPT; ++ }else{ ++ memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr)); ++ *ppSnapshot = (sqlite3_snapshot*)pRet; ++ } ++ ++ return rc; ++} ++ ++/* Try to open on pSnapshot when the next read-transaction starts ++*/ ++SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot){ ++ pWal->pSnapshot = (WalIndexHdr*)pSnapshot; ++} ++ ++/* ++** Return a +ve value if snapshot p1 is newer than p2. A -ve value if ++** p1 is older than p2 and zero if p1 and p2 are the same snapshot. ++*/ ++SQLITE_API int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){ ++ WalIndexHdr *pHdr1 = (WalIndexHdr*)p1; ++ WalIndexHdr *pHdr2 = (WalIndexHdr*)p2; ++ ++ /* aSalt[0] is a copy of the value stored in the wal file header. It ++ ** is incremented each time the wal file is restarted. */ ++ if( pHdr1->aSalt[0] aSalt[0] ) return -1; ++ if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1; ++ if( pHdr1->mxFrame mxFrame ) return -1; ++ if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1; ++ return 0; ++} ++ ++/* ++** The caller currently has a read transaction open on the database. ++** This function takes a SHARED lock on the CHECKPOINTER slot and then ++** checks if the snapshot passed as the second argument is still ++** available. If so, SQLITE_OK is returned. ++** ++** If the snapshot is not available, SQLITE_ERROR is returned. Or, if ++** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error ++** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER ++** lock is released before returning. ++*/ ++SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){ ++ int rc; ++ rc = walLockShared(pWal, WAL_CKPT_LOCK); ++ if( rc==SQLITE_OK ){ ++ WalIndexHdr *pNew = (WalIndexHdr*)pSnapshot; ++ if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt)) ++ || pNew->mxFrame nBackfillAttempted ++ ){ ++ rc = SQLITE_ERROR_SNAPSHOT; ++ walUnlockShared(pWal, WAL_CKPT_LOCK); ++ } ++ } ++ return rc; ++} ++ ++/* ++** Release a lock obtained by an earlier successful call to ++** sqlite3WalSnapshotCheck(). ++*/ ++SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal){ ++ assert( pWal ); ++ walUnlockShared(pWal, WAL_CKPT_LOCK); ++} ++ ++ ++#endif /* SQLITE_ENABLE_SNAPSHOT */ ++ + #ifdef SQLITE_ENABLE_ZIPVFS + /* + ** If the argument is not NULL, it points to a Wal object that holds a +@@ -52057,6 +62150,12 @@ SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){ + } + #endif + ++/* Return the sqlite3_file object for the WAL file ++*/ ++SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal){ ++ return pWal->pWalFd; ++} ++ + #endif /* #ifndef SQLITE_OMIT_WAL */ + + /************** End of wal.c *************************************************/ +@@ -52295,6 +62394,7 @@ SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){ + ** 4 Number of leaf pointers on this page + ** * zero or more pages numbers of leaves + */ ++/* #include "sqliteInt.h" */ + + + /* The following value is the maximum cell size assuming a maximum page +@@ -52312,6 +62412,7 @@ SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){ + /* Forward declarations */ + typedef struct MemPage MemPage; + typedef struct BtLock BtLock; ++typedef struct CellInfo CellInfo; + + /* + ** This is a magic string that appears at the beginning of every +@@ -52339,53 +62440,49 @@ typedef struct BtLock BtLock; + #define PTF_LEAF 0x08 + + /* +-** As each page of the file is loaded into memory, an instance of the following +-** structure is appended and initialized to zero. This structure stores +-** information about the page that is decoded from the raw file page. ++** An instance of this object stores information about each a single database ++** page that has been loaded into memory. The information in this object ++** is derived from the raw on-disk page content. + ** +-** The pParent field points back to the parent page. This allows us to +-** walk up the BTree from any leaf to the root. Care must be taken to +-** unref() the parent page pointer when this page is no longer referenced. +-** The pageDestructor() routine handles that chore. ++** As each database page is loaded into memory, the pager allocats an ++** instance of this object and zeros the first 8 bytes. (This is the ++** "extra" information associated with each page of the pager.) + ** + ** Access to all fields of this structure is controlled by the mutex + ** stored in MemPage.pBt->mutex. + */ + struct MemPage { + u8 isInit; /* True if previously initialized. MUST BE FIRST! */ +- u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ ++ u8 bBusy; /* Prevent endless loops on corrupt database files */ + u8 intKey; /* True if table b-trees. False for index b-trees */ + u8 intKeyLeaf; /* True if the leaf of an intKey table */ +- u8 noPayload; /* True if internal intKey page (thus w/o data) */ ++ Pgno pgno; /* Page number for this page */ ++ /* Only the first 8 bytes (above) are zeroed by pager.c when a new page ++ ** is allocated. All fields that follow must be initialized before use */ + u8 leaf; /* True if a leaf page */ + u8 hdrOffset; /* 100 for page 1. 0 otherwise */ + u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ + u8 max1bytePayload; /* min(maxLocal,127) */ +- u8 bBusy; /* Prevent endless loops on corrupt database files */ ++ u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ + u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */ + u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */ + u16 cellOffset; /* Index in aData of first cell pointer */ +- u16 nFree; /* Number of free bytes on the page */ ++ int nFree; /* Number of free bytes on the page. -1 for unknown */ + u16 nCell; /* Number of cells on this page, local and ovfl */ + u16 maskPage; /* Mask for page offset */ +- u16 aiOvfl[5]; /* Insert the i-th overflow cell before the aiOvfl-th ++ u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th + ** non-overflow cell */ +- u8 *apOvfl[5]; /* Pointers to the body of overflow cells */ ++ u8 *apOvfl[4]; /* Pointers to the body of overflow cells */ + BtShared *pBt; /* Pointer to BtShared that this page is part of */ + u8 *aData; /* Pointer to disk image of the page data */ + u8 *aDataEnd; /* One byte past the end of usable data */ + u8 *aCellIdx; /* The cell index area */ ++ u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */ + DbPage *pDbPage; /* Pager page handle */ +- Pgno pgno; /* Page number for this page */ ++ u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */ ++ void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */ + }; + +-/* +-** The in-memory image of a disk page has the auxiliary information appended +-** to the end. EXTRA_SIZE is the number of bytes of space needed to hold +-** that extra information. +-*/ +-#define EXTRA_SIZE sizeof(MemPage) +- + /* + ** A linked list of the following structures is stored at BtShared.pLock. + ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor +@@ -52431,6 +62528,7 @@ struct Btree { + u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */ + u8 sharable; /* True if we can share pBt with another db */ + u8 locked; /* True if db currently has pBt locked */ ++ u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */ + int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */ + int nBackup; /* Number of backup operations reading this btree */ + u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */ +@@ -52531,23 +62629,23 @@ struct BtShared { + #define BTS_READ_ONLY 0x0001 /* Underlying file is readonly */ + #define BTS_PAGESIZE_FIXED 0x0002 /* Page size can no longer be changed */ + #define BTS_SECURE_DELETE 0x0004 /* PRAGMA secure_delete is enabled */ +-#define BTS_INITIALLY_EMPTY 0x0008 /* Database was empty at trans start */ +-#define BTS_NO_WAL 0x0010 /* Do not open write-ahead-log files */ +-#define BTS_EXCLUSIVE 0x0020 /* pWriter has an exclusive lock */ +-#define BTS_PENDING 0x0040 /* Waiting for read-locks to clear */ ++#define BTS_OVERWRITE 0x0008 /* Overwrite deleted content with zeros */ ++#define BTS_FAST_SECURE 0x000c /* Combination of the previous two */ ++#define BTS_INITIALLY_EMPTY 0x0010 /* Database was empty at trans start */ ++#define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */ ++#define BTS_EXCLUSIVE 0x0040 /* pWriter has an exclusive lock */ ++#define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */ + + /* + ** An instance of the following structure is used to hold information + ** about a cell. The parseCellPtr() function fills in this structure + ** based on information extract from the raw disk page. + */ +-typedef struct CellInfo CellInfo; + struct CellInfo { + i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */ + u8 *pPayload; /* Pointer to the start of payload */ + u32 nPayload; /* Bytes of payload */ + u16 nLocal; /* Amount of payload held locally, not on overflow */ +- u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */ + u16 nSize; /* Size of the cell content on the main b-tree page */ + }; + +@@ -52577,29 +62675,43 @@ struct CellInfo { + ** found at self->pBt->mutex. + ** + ** skipNext meaning: +-** eState==SKIPNEXT && skipNext>0: Next sqlite3BtreeNext() is no-op. +-** eState==SKIPNEXT && skipNext<0: Next sqlite3BtreePrevious() is no-op. +-** eState==FAULT: Cursor fault with skipNext as error code. ++** The meaning of skipNext depends on the value of eState: ++** ++** eState Meaning of skipNext ++** VALID skipNext is meaningless and is ignored ++** INVALID skipNext is meaningless and is ignored ++** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and ++** sqlite3BtreePrevious() is no-op if skipNext<0. ++** REQUIRESEEK restoreCursorPosition() restores the cursor to ++** eState=SKIPNEXT if skipNext!=0 ++** FAULT skipNext holds the cursor fault error code. + */ + struct BtCursor { ++ u8 eState; /* One of the CURSOR_XXX constants (see below) */ ++ u8 curFlags; /* zero or more BTCF_* flags defined below */ ++ u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */ ++ u8 hints; /* As configured by CursorSetHints() */ ++ int skipNext; /* Prev() is noop if negative. Next() is noop if positive. ++ ** Error code if eState==CURSOR_FAULT */ + Btree *pBtree; /* The Btree to which this cursor belongs */ +- BtShared *pBt; /* The BtShared this cursor points to */ +- BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ +- struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */ + Pgno *aOverflow; /* Cache of overflow page locations */ ++ void *pKey; /* Saved key that was cursor last known position */ ++ /* All fields above are zeroed when the cursor is allocated. See ++ ** sqlite3BtreeCursorZero(). Fields that follow must be manually ++ ** initialized. */ ++#define BTCURSOR_FIRST_UNINIT pBt /* Name of first uninitialized field */ ++ BtShared *pBt; /* The BtShared this cursor points to */ ++ BtCursor *pNext; /* Forms a linked list of all cursors */ + CellInfo info; /* A parse of the cell we are pointing at */ + i64 nKey; /* Size of pKey, or last integer key */ +- void *pKey; /* Saved key that was cursor last known position */ + Pgno pgnoRoot; /* The root page of this tree */ +- int nOvflAlloc; /* Allocated size of aOverflow[] array */ +- int skipNext; /* Prev() is noop if negative. Next() is noop if positive. +- ** Error code if eState==CURSOR_FAULT */ +- u8 curFlags; /* zero or more BTCF_* flags defined below */ +- u8 eState; /* One of the CURSOR_XXX constants (see below) */ +- u8 hints; /* As configured by CursorSetHints() */ +- i16 iPage; /* Index of current page in apPage */ +- u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */ +- MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */ ++ i8 iPage; /* Index of current page in apPage */ ++ u8 curIntKey; /* Value of apPage[0]->intKey */ ++ u16 ix; /* Current index for apPage[iPage] */ ++ u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */ ++ struct KeyInfo *pKeyInfo; /* Arg passed to comparison function */ ++ MemPage *pPage; /* Current page */ ++ MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */ + }; + + /* +@@ -52610,6 +62722,7 @@ struct BtCursor { + #define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */ + #define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */ + #define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */ ++#define BTCF_Multiple 0x20 /* Maybe another cursor on the same btree */ + + /* + ** Potential values for BtCursor.eState. +@@ -52641,8 +62754,8 @@ struct BtCursor { + ** Do nothing else with this cursor. Any attempt to use the cursor + ** should return the error code stored in BtCursor.skipNext + */ +-#define CURSOR_INVALID 0 +-#define CURSOR_VALID 1 ++#define CURSOR_VALID 0 ++#define CURSOR_INVALID 1 + #define CURSOR_SKIPNEXT 2 + #define CURSOR_REQUIRESEEK 3 + #define CURSOR_FAULT 4 +@@ -52752,6 +62865,7 @@ struct IntegrityCk { + const char *zPfx; /* Error message prefix */ + int v1, v2; /* Values for up to two %d fields in zPfx */ + StrAccum errMsg; /* Accumulate the error message text here */ ++ u32 *heap; /* Min-heap used for analyzing cell coverage */ + }; + + /* +@@ -52762,6 +62876,21 @@ struct IntegrityCk { + #define get4byte sqlite3Get4byte + #define put4byte sqlite3Put4byte + ++/* ++** get2byteAligned(), unlike get2byte(), requires that its argument point to a ++** two-byte aligned address. get2bytea() is only used for accessing the ++** cell addresses in a btree header. ++*/ ++#if SQLITE_BYTEORDER==4321 ++# define get2byteAligned(x) (*(u16*)(x)) ++#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000 ++# define get2byteAligned(x) __builtin_bswap16(*(u16*)(x)) ++#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 ++# define get2byteAligned(x) _byteswap_ushort(*(u16*)(x)) ++#else ++# define get2byteAligned(x) ((x)[0]<<8 | (x)[1]) ++#endif ++ + /************** End of btreeInt.h ********************************************/ + /************** Continuing where we left off in btmutex.c ********************/ + #ifndef SQLITE_OMIT_SHARED_CACHE +@@ -52917,21 +63046,6 @@ SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){ + #endif + + +-#ifndef SQLITE_OMIT_INCRBLOB +-/* +-** Enter and leave a mutex on a Btree given a cursor owned by that +-** Btree. These entry points are used by incremental I/O and can be +-** omitted if that module is not used. +-*/ +-SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){ +- sqlite3BtreeEnter(pCur->pBtree); +-} +-SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){ +- sqlite3BtreeLeave(pCur->pBtree); +-} +-#endif /* SQLITE_OMIT_INCRBLOB */ +- +- + /* + ** Enter the mutex on every Btree associated with a database + ** connection. This is needed (for example) prior to parsing +@@ -52946,16 +63060,24 @@ SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){ + ** two or more btrees in common both try to lock all their btrees + ** at the same instant. + */ +-SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){ ++static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){ + int i; ++ int skipOk = 1; + Btree *p; + assert( sqlite3_mutex_held(db->mutex) ); + for(i=0; i nDb; i++){ + p = db->aDb[i].pBt; +- if( p ) sqlite3BtreeEnter(p); ++ if( p && p->sharable ){ ++ sqlite3BtreeEnter(p); ++ skipOk = 0; ++ } + } ++ db->noSharedCache = skipOk; + } +-SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){ ++SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){ ++ if( db->noSharedCache==0 ) btreeEnterAll(db); ++} ++static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){ + int i; + Btree *p; + assert( sqlite3_mutex_held(db->mutex) ); +@@ -52964,13 +63086,8 @@ SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){ + if( p ) sqlite3BtreeLeave(p); + } + } +- +-/* +-** Return true if a particular Btree requires a lock. Return FALSE if +-** no lock is ever required since it is not sharable. +-*/ +-SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){ +- return p->sharable; ++SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){ ++ if( db->noSharedCache==0 ) btreeLeaveAll(db); + } + + #ifndef NDEBUG +@@ -53046,6 +63163,25 @@ SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){ + } + } + #endif /* if SQLITE_THREADSAFE */ ++ ++#ifndef SQLITE_OMIT_INCRBLOB ++/* ++** Enter a mutex on a Btree given a cursor owned by that Btree. ++** ++** These entry points are used by incremental I/O only. Enter() is required ++** any time OMIT_SHARED_CACHE is not defined, regardless of whether or not ++** the build is threadsafe. Leave() is only required by threadsafe builds. ++*/ ++SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){ ++ sqlite3BtreeEnter(pCur->pBtree); ++} ++# if SQLITE_THREADSAFE ++SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){ ++ sqlite3BtreeLeave(pCur->pBtree); ++} ++# endif ++#endif /* ifndef SQLITE_OMIT_INCRBLOB */ ++ + #endif /* ifndef SQLITE_OMIT_SHARED_CACHE */ + + /************** End of btmutex.c *********************************************/ +@@ -53065,6 +63201,7 @@ SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){ + ** See the header comment on "btreeInt.h" for additional information. + ** Including a description of file format and an overview of operation. + */ ++/* #include "btreeInt.h" */ + + /* + ** The header string that appears at the beginning of every +@@ -53137,7 +63274,7 @@ static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; + ** The shared cache setting effects only future calls to + ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2(). + */ +-SQLITE_API int SQLITE_STDCALL sqlite3_enable_shared_cache(int enable){ ++SQLITE_API int sqlite3_enable_shared_cache(int enable){ + sqlite3GlobalConfig.sharedCacheEnabled = enable; + return SQLITE_OK; + } +@@ -53163,6 +63300,34 @@ SQLITE_API int SQLITE_STDCALL sqlite3_enable_shared_cache(int enable){ + #define hasReadConflicts(a, b) 0 + #endif + ++/* ++** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single ++** (MemPage*) as an argument. The (MemPage*) must not be NULL. ++** ++** If SQLITE_DEBUG is not defined, then this macro is equivalent to ++** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message ++** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented ++** with the page number and filename associated with the (MemPage*). ++*/ ++#ifdef SQLITE_DEBUG ++int corruptPageError(int lineno, MemPage *p){ ++ char *zMsg; ++ sqlite3BeginBenignMalloc(); ++ zMsg = sqlite3_mprintf("database corruption page %d of %s", ++ (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0) ++ ); ++ sqlite3EndBenignMalloc(); ++ if( zMsg ){ ++ sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg); ++ } ++ sqlite3_free(zMsg); ++ return SQLITE_CORRUPT_BKPT; ++} ++# define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage) ++#else ++# define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno) ++#endif ++ + #ifndef SQLITE_OMIT_SHARED_CACHE + + #ifdef SQLITE_DEBUG +@@ -53203,7 +63368,7 @@ static int hasSharedCacheTableLock( + ** Return true immediately. + */ + if( (pBtree->sharable==0) +- || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted)) ++ || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit)) + ){ + return 1; + } +@@ -53280,7 +63445,7 @@ static int hasReadConflicts(Btree *pBtree, Pgno iRoot){ + for(p=pBtree->pBt->pCursor; p; p=p->pNext){ + if( p->pgnoRoot==iRoot + && p->pBtree!=pBtree +- && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted) ++ && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit) + ){ + return 1; + } +@@ -53302,7 +63467,7 @@ static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){ + assert( sqlite3BtreeHoldsMutex(p) ); + assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); + assert( p->db!=0 ); +- assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 ); ++ assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 ); + + /* If requesting a write-lock, then the Btree must have an open write + ** transaction on this file. And, obviously, for this to be so there +@@ -53380,7 +63545,7 @@ static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){ + ** obtain a read-lock using this function. The only read-lock obtained + ** by a connection in read-uncommitted mode is on the sqlite_master + ** table, and that lock is obtained in BtreeBeginTrans(). */ +- assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK ); ++ assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK ); + + /* This function should only be called on a sharable b-tree after it + ** has been determined that no other b-tree holds a conflicting lock. */ +@@ -53401,7 +63566,7 @@ static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){ + if( !pLock ){ + pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); + if( !pLock ){ +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + pLock->iTable = iTable; + pLock->pBtree = p; +@@ -53490,7 +63655,9 @@ static void downgradeAllSharedCacheTableLocks(Btree *p){ + + #endif /* SQLITE_OMIT_SHARED_CACHE */ + +-static void releasePage(MemPage *pPage); /* Forward reference */ ++static void releasePage(MemPage *pPage); /* Forward reference */ ++static void releasePageOne(MemPage *pPage); /* Forward reference */ ++static void releasePageNotNull(MemPage *pPage); /* Forward reference */ + + /* + ***** This routine is used inside of assert() only **** +@@ -53501,6 +63668,19 @@ static void releasePage(MemPage *pPage); /* Forward reference */ + static int cursorHoldsMutex(BtCursor *p){ + return sqlite3_mutex_held(p->pBt->mutex); + } ++ ++/* Verify that the cursor and the BtShared agree about what is the current ++** database connetion. This is important in shared-cache mode. If the database ++** connection pointers get out-of-sync, it is possible for routines like ++** btreeInitPage() to reference an stale connection pointer that references a ++** a connection that has already closed. This routine is used inside assert() ++** statements only and for the purpose of double-checking that the btree code ++** does keep the database connection pointers up-to-date. ++*/ ++static int cursorOwnsBtShared(BtCursor *p){ ++ assert( cursorHoldsMutex(p) ); ++ return (p->pBtree->db==p->pBt->db); ++} + #endif + + /* +@@ -53537,24 +63717,27 @@ static void invalidateAllOverflowCache(BtShared *pBt){ + */ + static void invalidateIncrblobCursors( + Btree *pBtree, /* The database file to check */ ++ Pgno pgnoRoot, /* The table that might be changing */ + i64 iRow, /* The rowid that might be changing */ + int isClearTable /* True if all rows are being deleted */ + ){ + BtCursor *p; +- BtShared *pBt = pBtree->pBt; ++ if( pBtree->hasIncrblobCur==0 ) return; + assert( sqlite3BtreeHoldsMutex(pBtree) ); +- for(p=pBt->pCursor; p; p=p->pNext){ +- if( (p->curFlags & BTCF_Incrblob)!=0 +- && (isClearTable || p->info.nKey==iRow) +- ){ +- p->eState = CURSOR_INVALID; ++ pBtree->hasIncrblobCur = 0; ++ for(p=pBtree->pBt->pCursor; p; p=p->pNext){ ++ if( (p->curFlags & BTCF_Incrblob)!=0 ){ ++ pBtree->hasIncrblobCur = 1; ++ if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){ ++ p->eState = CURSOR_INVALID; ++ } + } + } + } + + #else + /* Stub function when INCRBLOB is omitted */ +- #define invalidateIncrblobCursors(x,y,z) ++ #define invalidateIncrblobCursors(w,x,y,z) + #endif /* SQLITE_OMIT_INCRBLOB */ + + /* +@@ -53598,7 +63781,7 @@ static int btreeSetHasContent(BtShared *pBt, Pgno pgno){ + assert( pgno<=pBt->nPage ); + pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage); + if( !pBt->pHasContent ){ +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + } + } + if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){ +@@ -53633,13 +63816,62 @@ static void btreeClearHasContent(BtShared *pBt){ + */ + static void btreeReleaseAllCursorPages(BtCursor *pCur){ + int i; +- for(i=0; i<=pCur->iPage; i++){ +- releasePage(pCur->apPage[i]); +- pCur->apPage[i] = 0; ++ if( pCur->iPage>=0 ){ ++ for(i=0; i iPage; i++){ ++ releasePageNotNull(pCur->apPage[i]); ++ } ++ releasePageNotNull(pCur->pPage); ++ pCur->iPage = -1; + } +- pCur->iPage = -1; + } + ++/* ++** The cursor passed as the only argument must point to a valid entry ++** when this function is called (i.e. have eState==CURSOR_VALID). This ++** function saves the current cursor key in variables pCur->nKey and ++** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error ++** code otherwise. ++** ++** If the cursor is open on an intkey table, then the integer key ++** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to ++** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is ++** set to point to a malloced buffer pCur->nKey bytes in size containing ++** the key. ++*/ ++static int saveCursorKey(BtCursor *pCur){ ++ int rc = SQLITE_OK; ++ assert( CURSOR_VALID==pCur->eState ); ++ assert( 0==pCur->pKey ); ++ assert( cursorHoldsMutex(pCur) ); ++ ++ if( pCur->curIntKey ){ ++ /* Only the rowid is required for a table btree */ ++ pCur->nKey = sqlite3BtreeIntegerKey(pCur); ++ }else{ ++ /* For an index btree, save the complete key content. It is possible ++ ** that the current key is corrupt. In that case, it is possible that ++ ** the sqlite3VdbeRecordUnpack() function may overread the buffer by ++ ** up to the size of 1 varint plus 1 8-byte value when the cursor ++ ** position is restored. Hence the 17 bytes of padding allocated ++ ** below. */ ++ void *pKey; ++ pCur->nKey = sqlite3BtreePayloadSize(pCur); ++ pKey = sqlite3Malloc( pCur->nKey + 9 + 8 ); ++ if( pKey ){ ++ rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey); ++ if( rc==SQLITE_OK ){ ++ memset(((u8*)pKey)+pCur->nKey, 0, 9+8); ++ pCur->pKey = pKey; ++ }else{ ++ sqlite3_free(pKey); ++ } ++ }else{ ++ rc = SQLITE_NOMEM_BKPT; ++ } ++ } ++ assert( !pCur->curIntKey || !pCur->pKey ); ++ return rc; ++} + + /* + ** Save the current cursor position in the variables BtCursor.nKey +@@ -53660,36 +63892,14 @@ static int saveCursorPosition(BtCursor *pCur){ + }else{ + pCur->skipNext = 0; + } +- rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); +- assert( rc==SQLITE_OK ); /* KeySize() cannot fail */ +- +- /* If this is an intKey table, then the above call to BtreeKeySize() +- ** stores the integer key in pCur->nKey. In this case this value is +- ** all that is required. Otherwise, if pCur is not open on an intKey +- ** table, then malloc space for and store the pCur->nKey bytes of key +- ** data. +- */ +- if( 0==pCur->apPage[0]->intKey ){ +- void *pKey = sqlite3Malloc( pCur->nKey ); +- if( pKey ){ +- rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey); +- if( rc==SQLITE_OK ){ +- pCur->pKey = pKey; +- }else{ +- sqlite3_free(pKey); +- } +- }else{ +- rc = SQLITE_NOMEM; +- } +- } +- assert( !pCur->apPage[0]->intKey || !pCur->pKey ); + ++ rc = saveCursorKey(pCur); + if( rc==SQLITE_OK ){ + btreeReleaseAllCursorPages(pCur); + pCur->eState = CURSOR_REQUIRESEEK; + } + +- invalidateOverflowCache(pCur); ++ pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast); + return rc; + } + +@@ -53704,6 +63914,15 @@ static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*); + ** routine is called just before cursor pExcept is used to modify the + ** table, for example in BtreeDelete() or BtreeInsert(). + ** ++** If there are two or more cursors on the same btree, then all such ++** cursors should have their BTCF_Multiple flag set. The btreeCursor() ++** routine enforces that rule. This routine only needs to be called in ++** the uncommon case when pExpect has the BTCF_Multiple flag set. ++** ++** If pExpect!=NULL and if no other cursors are found on the same root-page, ++** then the BTCF_Multiple flag on pExpect is cleared, to avoid another ++** pointless call to this routine. ++** + ** Implementation note: This routine merely checks to see if any cursors + ** need to be saved. It calls out to saveCursorsOnList() in the (unusual) + ** event that cursors are in need to being saved. +@@ -53715,7 +63934,9 @@ static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ + for(p=pBt->pCursor; p; p=p->pNext){ + if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break; + } +- return p ? saveCursorsOnList(p, iRoot, pExcept) : SQLITE_OK; ++ if( p ) return saveCursorsOnList(p, iRoot, pExcept); ++ if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple; ++ return SQLITE_OK; + } + + /* This helper routine to saveAllCursors does the actual work of saving +@@ -53736,7 +63957,7 @@ static int SQLITE_NOINLINE saveCursorsOnList( + return rc; + } + }else{ +- testcase( p->iPage>0 ); ++ testcase( p->iPage>=0 ); + btreeReleaseAllCursorPages(p); + } + } +@@ -53769,26 +63990,24 @@ static int btreeMoveto( + ){ + int rc; /* Status code */ + UnpackedRecord *pIdxKey; /* Unpacked index key */ +- char aSpace[200]; /* Temp space for pIdxKey - to avoid a malloc */ +- char *pFree = 0; + + if( pKey ){ ++ KeyInfo *pKeyInfo = pCur->pKeyInfo; + assert( nKey==(i64)(int)nKey ); +- pIdxKey = sqlite3VdbeAllocUnpackedRecord( +- pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree +- ); +- if( pIdxKey==0 ) return SQLITE_NOMEM; +- sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey); +- if( pIdxKey->nField==0 ){ +- sqlite3DbFree(pCur->pKeyInfo->db, pFree); +- return SQLITE_CORRUPT_BKPT; ++ pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo); ++ if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT; ++ sqlite3VdbeRecordUnpack(pKeyInfo, (int)nKey, pKey, pIdxKey); ++ if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){ ++ rc = SQLITE_CORRUPT_BKPT; ++ goto moveto_done; + } + }else{ + pIdxKey = 0; + } + rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes); +- if( pFree ){ +- sqlite3DbFree(pCur->pKeyInfo->db, pFree); ++moveto_done: ++ if( pIdxKey ){ ++ sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey); + } + return rc; + } +@@ -53802,19 +64021,23 @@ static int btreeMoveto( + */ + static int btreeRestoreCursorPosition(BtCursor *pCur){ + int rc; +- int skipNext; +- assert( cursorHoldsMutex(pCur) ); ++ int skipNext = 0; ++ assert( cursorOwnsBtShared(pCur) ); + assert( pCur->eState>=CURSOR_REQUIRESEEK ); + if( pCur->eState==CURSOR_FAULT ){ + return pCur->skipNext; + } + pCur->eState = CURSOR_INVALID; +- rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext); ++ if( sqlite3FaultSim(410) ){ ++ rc = SQLITE_IOERR; ++ }else{ ++ rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext); ++ } + if( rc==SQLITE_OK ){ + sqlite3_free(pCur->pKey); + pCur->pKey = 0; + assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); +- pCur->skipNext |= skipNext; ++ if( skipNext ) pCur->skipNext = skipNext; + if( pCur->skipNext && pCur->eState==CURSOR_VALID ){ + pCur->eState = CURSOR_SKIPNEXT; + } +@@ -53840,7 +64063,22 @@ static int btreeRestoreCursorPosition(BtCursor *pCur){ + ** back to where it ought to be if this routine returns true. + */ + SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){ +- return pCur->eState!=CURSOR_VALID; ++ assert( EIGHT_BYTE_ALIGNMENT(pCur) ++ || pCur==sqlite3BtreeFakeValidCursor() ); ++ assert( offsetof(BtCursor, eState)==0 ); ++ assert( sizeof(pCur->eState)==1 ); ++ return CURSOR_VALID != *(u8*)pCur; ++} ++ ++/* ++** Return a pointer to a fake BtCursor object that will always answer ++** false to the sqlite3BtreeCursorHasMoved() routine above. The fake ++** cursor returned must not be used with any other Btree interface. ++*/ ++SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void){ ++ static u8 fakeCursor = CURSOR_VALID; ++ assert( offsetof(BtCursor, eState)==0 ); ++ return (BtCursor*)&fakeCursor; + } + + /* +@@ -53869,12 +64107,31 @@ SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow) + if( pCur->eState!=CURSOR_VALID ){ + *pDifferentRow = 1; + }else{ +- assert( pCur->skipNext==0 ); + *pDifferentRow = 0; + } + return SQLITE_OK; + } + ++#ifdef SQLITE_ENABLE_CURSOR_HINTS ++/* ++** Provide hints to the cursor. The particular hint given (and the type ++** and number of the varargs parameters) is determined by the eHintType ++** parameter. See the definitions of the BTREE_HINT_* macros for details. ++*/ ++SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){ ++ /* Used only by system that substitute their own storage engine */ ++} ++#endif ++ ++/* ++** Provide flag hints to the cursor. ++*/ ++SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){ ++ assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 ); ++ pCur->hints = x; ++} ++ ++ + #ifndef SQLITE_OMIT_AUTOVACUUM + /* + ** Given a page number of a regular database page, return the page +@@ -53928,11 +64185,18 @@ static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){ + return; + } + iPtrmap = PTRMAP_PAGENO(pBt, key); +- rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); ++ rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0); + if( rc!=SQLITE_OK ){ + *pRC = rc; + return; + } ++ if( ((char*)sqlite3PagerGetExtra(pDbPage))[0]!=0 ){ ++ /* The first byte of the extra data is the MemPage.isInit byte. ++ ** If that byte is set, it means this page is also being used ++ ** as a btree page. */ ++ *pRC = SQLITE_CORRUPT_BKPT; ++ goto ptrmap_exit; ++ } + offset = PTRMAP_PTROFFSET(iPtrmap, key); + if( offset<0 ){ + *pRC = SQLITE_CORRUPT_BKPT; +@@ -53971,7 +64235,7 @@ static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ + assert( sqlite3_mutex_held(pBt->mutex) ); + + iPtrmap = PTRMAP_PAGENO(pBt, key); +- rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); ++ rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0); + if( rc!=0 ){ + return rc; + } +@@ -53988,14 +64252,14 @@ static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ + if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); + + sqlite3PagerUnref(pDbPage); +- if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; ++ if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap); + return SQLITE_OK; + } + + #else /* if defined SQLITE_OMIT_AUTOVACUUM */ + #define ptrmapPut(w,x,y,z,rc) + #define ptrmapGet(w,x,y,z) SQLITE_OK +- #define ptrmapPutOvflPtr(x, y, rc) ++ #define ptrmapPutOvflPtr(x, y, z, rc) + #endif + + /* +@@ -54003,39 +64267,85 @@ static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ + ** the page, 1 means the second cell, and so forth) return a pointer + ** to the cell content. + ** ++** findCellPastPtr() does the same except it skips past the initial ++** 4-byte child pointer found on interior pages, if there is one. ++** + ** This routine works only for pages that do not contain overflow cells. + */ + #define findCell(P,I) \ +- ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)]))) +-#define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I))))) ++ ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)]))) ++#define findCellPastPtr(P,I) \ ++ ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)]))) + + + /* +-** This a more complex version of findCell() that works for +-** pages that do contain overflow cells. ++** This is common tail processing for btreeParseCellPtr() and ++** btreeParseCellPtrIndex() for the case when the cell does not fit entirely ++** on a single B-tree page. Make necessary adjustments to the CellInfo ++** structure. + */ +-static u8 *findOverflowCell(MemPage *pPage, int iCell){ +- int i; +- assert( sqlite3_mutex_held(pPage->pBt->mutex) ); +- for(i=pPage->nOverflow-1; i>=0; i--){ +- int k; +- k = pPage->aiOvfl[i]; +- if( k<=iCell ){ +- if( k==iCell ){ +- return pPage->apOvfl[i]; +- } +- iCell--; +- } ++static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow( ++ MemPage *pPage, /* Page containing the cell */ ++ u8 *pCell, /* Pointer to the cell text. */ ++ CellInfo *pInfo /* Fill in this structure */ ++){ ++ /* If the payload will not fit completely on the local page, we have ++ ** to decide how much to store locally and how much to spill onto ++ ** overflow pages. The strategy is to minimize the amount of unused ++ ** space on overflow pages while keeping the amount of local storage ++ ** in between minLocal and maxLocal. ++ ** ++ ** Warning: changing the way overflow payload is distributed in any ++ ** way will result in an incompatible file format. ++ */ ++ int minLocal; /* Minimum amount of payload held locally */ ++ int maxLocal; /* Maximum amount of payload held locally */ ++ int surplus; /* Overflow payload available for local storage */ ++ ++ minLocal = pPage->minLocal; ++ maxLocal = pPage->maxLocal; ++ surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4); ++ testcase( surplus==maxLocal ); ++ testcase( surplus==maxLocal+1 ); ++ if( surplus <= maxLocal ){ ++ pInfo->nLocal = (u16)surplus; ++ }else{ ++ pInfo->nLocal = (u16)minLocal; + } +- return findCell(pPage, iCell); ++ pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4; + } + + /* +-** Parse a cell content block and fill in the CellInfo structure. There +-** are two versions of this function. btreeParseCell() takes a +-** cell index as the second argument and btreeParseCellPtr() +-** takes a pointer to the body of the cell as its second argument. ++** The following routines are implementations of the MemPage.xParseCell() ++** method. ++** ++** Parse a cell content block and fill in the CellInfo structure. ++** ++** btreeParseCellPtr() => table btree leaf nodes ++** btreeParseCellNoPayload() => table btree internal nodes ++** btreeParseCellPtrIndex() => index btree nodes ++** ++** There is also a wrapper function btreeParseCell() that works for ++** all MemPage types and that references the cell by index rather than ++** by pointer. + */ ++static void btreeParseCellPtrNoPayload( ++ MemPage *pPage, /* Page containing the cell */ ++ u8 *pCell, /* Pointer to the cell text. */ ++ CellInfo *pInfo /* Fill in this structure */ ++){ ++ assert( sqlite3_mutex_held(pPage->pBt->mutex) ); ++ assert( pPage->leaf==0 ); ++ assert( pPage->childPtrSize==4 ); ++#ifndef SQLITE_DEBUG ++ UNUSED_PARAMETER(pPage); ++#endif ++ pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey); ++ pInfo->nPayload = 0; ++ pInfo->nLocal = 0; ++ pInfo->pPayload = 0; ++ return; ++} + static void btreeParseCellPtr( + MemPage *pPage, /* Page containing the cell */ + u8 *pCell, /* Pointer to the cell text. */ +@@ -54043,26 +64353,52 @@ static void btreeParseCellPtr( + ){ + u8 *pIter; /* For scanning through pCell */ + u32 nPayload; /* Number of bytes of cell payload */ ++ u64 iKey; /* Extracted Key value */ + + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( pPage->leaf==0 || pPage->leaf==1 ); +- if( pPage->intKeyLeaf ){ +- assert( pPage->childPtrSize==0 ); +- pIter = pCell + getVarint32(pCell, nPayload); +- pIter += getVarint(pIter, (u64*)&pInfo->nKey); +- }else if( pPage->noPayload ){ +- assert( pPage->childPtrSize==4 ); +- pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey); +- pInfo->nPayload = 0; +- pInfo->nLocal = 0; +- pInfo->iOverflow = 0; +- pInfo->pPayload = 0; +- return; +- }else{ +- pIter = pCell + pPage->childPtrSize; +- pIter += getVarint32(pIter, nPayload); +- pInfo->nKey = nPayload; ++ assert( pPage->intKeyLeaf ); ++ assert( pPage->childPtrSize==0 ); ++ pIter = pCell; ++ ++ /* The next block of code is equivalent to: ++ ** ++ ** pIter += getVarint32(pIter, nPayload); ++ ** ++ ** The code is inlined to avoid a function call. ++ */ ++ nPayload = *pIter; ++ if( nPayload>=0x80 ){ ++ u8 *pEnd = &pIter[8]; ++ nPayload &= 0x7f; ++ do{ ++ nPayload = (nPayload<<7) | (*++pIter & 0x7f); ++ }while( (*pIter)>=0x80 && pIter nKey); ++ ** ++ ** The code is inlined to avoid a function call. ++ */ ++ iKey = *pIter; ++ if( iKey>=0x80 ){ ++ u8 *pEnd = &pIter[7]; ++ iKey &= 0x7f; ++ while(1){ ++ iKey = (iKey<<7) | (*++pIter & 0x7f); ++ if( (*pIter)<0x80 ) break; ++ if( pIter>=pEnd ){ ++ iKey = (iKey<<8) | *++pIter; ++ break; ++ } ++ } + } ++ pIter++; ++ ++ pInfo->nKey = *(i64*)&iKey; + pInfo->nPayload = nPayload; + pInfo->pPayload = pIter; + testcase( nPayload==pPage->maxLocal ); +@@ -54074,33 +64410,45 @@ static void btreeParseCellPtr( + pInfo->nSize = nPayload + (u16)(pIter - pCell); + if( pInfo->nSize<4 ) pInfo->nSize = 4; + pInfo->nLocal = (u16)nPayload; +- pInfo->iOverflow = 0; + }else{ +- /* If the payload will not fit completely on the local page, we have +- ** to decide how much to store locally and how much to spill onto +- ** overflow pages. The strategy is to minimize the amount of unused +- ** space on overflow pages while keeping the amount of local storage +- ** in between minLocal and maxLocal. +- ** +- ** Warning: changing the way overflow payload is distributed in any +- ** way will result in an incompatible file format. +- */ +- int minLocal; /* Minimum amount of payload held locally */ +- int maxLocal; /* Maximum amount of payload held locally */ +- int surplus; /* Overflow payload available for local storage */ ++ btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo); ++ } ++} ++static void btreeParseCellPtrIndex( ++ MemPage *pPage, /* Page containing the cell */ ++ u8 *pCell, /* Pointer to the cell text. */ ++ CellInfo *pInfo /* Fill in this structure */ ++){ ++ u8 *pIter; /* For scanning through pCell */ ++ u32 nPayload; /* Number of bytes of cell payload */ + +- minLocal = pPage->minLocal; +- maxLocal = pPage->maxLocal; +- surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); +- testcase( surplus==maxLocal ); +- testcase( surplus==maxLocal+1 ); +- if( surplus <= maxLocal ){ +- pInfo->nLocal = (u16)surplus; +- }else{ +- pInfo->nLocal = (u16)minLocal; +- } +- pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell); +- pInfo->nSize = pInfo->iOverflow + 4; ++ assert( sqlite3_mutex_held(pPage->pBt->mutex) ); ++ assert( pPage->leaf==0 || pPage->leaf==1 ); ++ assert( pPage->intKeyLeaf==0 ); ++ pIter = pCell + pPage->childPtrSize; ++ nPayload = *pIter; ++ if( nPayload>=0x80 ){ ++ u8 *pEnd = &pIter[8]; ++ nPayload &= 0x7f; ++ do{ ++ nPayload = (nPayload<<7) | (*++pIter & 0x7f); ++ }while( *(pIter)>=0x80 && pIter nKey = nPayload; ++ pInfo->nPayload = nPayload; ++ pInfo->pPayload = pIter; ++ testcase( nPayload==pPage->maxLocal ); ++ testcase( nPayload==pPage->maxLocal+1 ); ++ if( nPayload<=pPage->maxLocal ){ ++ /* This is the (easy) common case where the entire payload fits ++ ** on the local page. No overflow is required. ++ */ ++ pInfo->nSize = nPayload + (u16)(pIter - pCell); ++ if( pInfo->nSize<4 ) pInfo->nSize = 4; ++ pInfo->nLocal = (u16)nPayload; ++ }else{ ++ btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo); + } + } + static void btreeParseCell( +@@ -54108,14 +64456,20 @@ static void btreeParseCell( + int iCell, /* The cell index. First cell is 0 */ + CellInfo *pInfo /* Fill in this structure */ + ){ +- btreeParseCellPtr(pPage, findCell(pPage, iCell), pInfo); ++ pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo); + } + + /* ++** The following routines are implementations of the MemPage.xCellSize ++** method. ++** + ** Compute the total number of bytes that a Cell needs in the cell + ** data area of the btree-page. The return number includes the cell + ** data header and the local payload, but not any overflow page or + ** the space used by the cell pointer. ++** ++** cellSizePtrNoPayload() => table internal nodes ++** cellSizePtr() => all index nodes & table leaf nodes + */ + static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ + u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */ +@@ -54128,18 +64482,12 @@ static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ + ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of + ** this function verifies that this invariant is not violated. */ + CellInfo debuginfo; +- btreeParseCellPtr(pPage, pCell, &debuginfo); ++ pPage->xParseCell(pPage, pCell, &debuginfo); + #endif + +- if( pPage->noPayload ){ +- pEnd = &pIter[9]; +- while( (*pIter++)&0x80 && pIter childPtrSize==4 ); +- return (u16)(pIter - pCell); +- } + nSize = *pIter; + if( nSize>=0x80 ){ +- pEnd = &pIter[9]; ++ pEnd = &pIter[8]; + nSize &= 0x7f; + do{ + nSize = (nSize<<7) | (*++pIter & 0x7f); +@@ -54171,28 +64519,57 @@ static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ + assert( nSize==debuginfo.nSize || CORRUPT_DB ); + return (u16)nSize; + } ++static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){ ++ u8 *pIter = pCell + 4; /* For looping over bytes of pCell */ ++ u8 *pEnd; /* End mark for a varint */ ++ ++#ifdef SQLITE_DEBUG ++ /* The value returned by this function should always be the same as ++ ** the (CellInfo.nSize) value found by doing a full parse of the ++ ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of ++ ** this function verifies that this invariant is not violated. */ ++ CellInfo debuginfo; ++ pPage->xParseCell(pPage, pCell, &debuginfo); ++#else ++ UNUSED_PARAMETER(pPage); ++#endif ++ ++ assert( pPage->childPtrSize==4 ); ++ pEnd = pIter + 9; ++ while( (*pIter++)&0x80 && pIter xCellSize(pPage, findCell(pPage, iCell)); + } + #endif + + #ifndef SQLITE_OMIT_AUTOVACUUM + /* +-** If the cell pCell, part of page pPage contains a pointer +-** to an overflow page, insert an entry into the pointer-map +-** for the overflow page. ++** The cell pCell is currently part of page pSrc but will ultimately be part ++** of pPage. (pSrc and pPager are often the same.) If pCell contains a ++** pointer to an overflow page, insert an entry into the pointer-map for ++** the overflow page that will be valid after pCell has been moved to pPage. + */ +-static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){ ++static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){ + CellInfo info; + if( *pRC ) return; + assert( pCell!=0 ); +- btreeParseCellPtr(pPage, pCell, &info); +- if( info.iOverflow ){ +- Pgno ovfl = get4byte(&pCell[info.iOverflow]); ++ pPage->xParseCell(pPage, pCell, &info); ++ if( info.nLocal aDataEnd, pCell, pCell+info.nLocal) ){ ++ testcase( pSrc!=pPage ); ++ *pRC = SQLITE_CORRUPT_BKPT; ++ return; ++ } ++ ovfl = get4byte(&pCell[info.nSize-4]); + ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC); + } + } +@@ -54200,17 +64577,18 @@ static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){ + + + /* +-** Defragment the page given. All Cells are moved to the +-** end of the page and all free space is collected into one +-** big FreeBlk that occurs in between the header and cell +-** pointer array and the cell content area. ++** Defragment the page given. This routine reorganizes cells within the ++** page so that there are no free-blocks on the free-block list. ++** ++** Parameter nMaxFrag is the maximum amount of fragmented space that may be ++** present in the page after this routine returns. + ** + ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a + ** b-tree page so that there are no freeblocks or fragment bytes, all + ** unused bytes are contained in the unallocated space region, and all + ** cells are packed tightly at the end of the page. + */ +-static int defragmentPage(MemPage *pPage){ ++static int defragmentPage(MemPage *pPage, int nMaxFrag){ + int i; /* Loop counter */ + int pc; /* Address of the i-th cell */ + int hdr; /* Offset to the page header */ +@@ -54225,7 +64603,6 @@ static int defragmentPage(MemPage *pPage){ + int iCellFirst; /* First allowable cell index */ + int iCellLast; /* Last possible cell index */ + +- + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + assert( pPage->pBt!=0 ); + assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); +@@ -54236,10 +64613,54 @@ static int defragmentPage(MemPage *pPage){ + hdr = pPage->hdrOffset; + cellOffset = pPage->cellOffset; + nCell = pPage->nCell; +- assert( nCell==get2byte(&data[hdr+3]) ); ++ assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB ); ++ iCellFirst = cellOffset + 2*nCell; + usableSize = pPage->pBt->usableSize; ++ ++ /* This block handles pages with two or fewer free blocks and nMaxFrag ++ ** or fewer fragmented bytes. In this case it is faster to move the ++ ** two (or one) blocks of cells using memmove() and add the required ++ ** offsets to each pointer in the cell-pointer array than it is to ++ ** reconstruct the entire page. */ ++ if( (int)data[hdr+7]<=nMaxFrag ){ ++ int iFree = get2byte(&data[hdr+1]); ++ if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage); ++ if( iFree ){ ++ int iFree2 = get2byte(&data[iFree]); ++ if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage); ++ if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){ ++ u8 *pEnd = &data[cellOffset + nCell*2]; ++ u8 *pAddr; ++ int sz2 = 0; ++ int sz = get2byte(&data[iFree+2]); ++ int top = get2byte(&data[hdr+5]); ++ if( top>=iFree ){ ++ return SQLITE_CORRUPT_PAGE(pPage); ++ } ++ if( iFree2 ){ ++ if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage); ++ sz2 = get2byte(&data[iFree2+2]); ++ if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage); ++ memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz)); ++ sz += sz2; ++ }else if( iFree+sz>usableSize ){ ++ return SQLITE_CORRUPT_PAGE(pPage); ++ } ++ ++ cbrk = top+sz; ++ assert( cbrk+(iFree-top) <= usableSize ); ++ memmove(&data[cbrk], &data[top], iFree-top); ++ for(pAddr=&data[cellOffset]; pAddr iCellLast ){ +- return SQLITE_CORRUPT_BKPT; ++ return SQLITE_CORRUPT_PAGE(pPage); + } +-#endif + assert( pc>=iCellFirst && pc<=iCellLast ); +- size = cellSizePtr(pPage, &src[pc]); ++ size = pPage->xCellSize(pPage, &src[pc]); + cbrk -= size; +-#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) +- if( cbrk usableSize ){ +- return SQLITE_CORRUPT_BKPT; ++ return SQLITE_CORRUPT_PAGE(pPage); + } +-#endif + assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); + testcase( cbrk+size==usableSize ); + testcase( pc+size==usableSize ); +@@ -54281,16 +64694,19 @@ static int defragmentPage(MemPage *pPage){ + } + memcpy(&data[cbrk], &src[pc], size); + } ++ data[hdr+7] = 0; ++ ++ defragment_out: ++ assert( pPage->nFree>=0 ); ++ if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){ ++ return SQLITE_CORRUPT_PAGE(pPage); ++ } + assert( cbrk>=iCellFirst ); + put2byte(&data[hdr+5], cbrk); + data[hdr+1] = 0; + data[hdr+2] = 0; +- data[hdr+7] = 0; + memset(&data[iCellFirst], 0, cbrk-iCellFirst); + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); +- if( cbrk-iCellFirst!=pPage->nFree ){ +- return SQLITE_CORRUPT_BKPT; +- } + return SQLITE_OK; + } + +@@ -54304,56 +64720,62 @@ static int defragmentPage(MemPage *pPage){ + ** This function may detect corruption within pPg. If corruption is + ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned. + ** +-** If a slot of at least nByte bytes is found but cannot be used because +-** there are already at least 60 fragmented bytes on the page, return NULL. +-** In this case, if pbDefrag parameter is not NULL, set *pbDefrag to true. +-*/ +-static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc, int *pbDefrag){ +- const int hdr = pPg->hdrOffset; +- u8 * const aData = pPg->aData; +- int iAddr; +- int pc; +- int usableSize = pPg->pBt->usableSize; +- +- for(iAddr=hdr+1; (pc = get2byte(&aData[iAddr]))>0; iAddr=pc){ +- int size; /* Size of the free slot */ +- /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of +- ** increasing offset. */ +- if( pc>usableSize-4 || pc hdrOffset; /* Offset to page header */ ++ u8 * const aData = pPg->aData; /* Page data */ ++ int iAddr = hdr + 1; /* Address of ptr to pc */ ++ int pc = get2byte(&aData[iAddr]); /* Address of a free slot */ ++ int x; /* Excess size of the slot */ ++ int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */ ++ int size; /* Size of the free slot */ ++ ++ assert( pc>0 ); ++ while( pc<=maxPC ){ + /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each + ** freeblock form a big-endian integer which is the size of the freeblock + ** in bytes, including the 4-byte header. */ + size = get2byte(&aData[pc+2]); +- if( size>=nByte ){ +- int x = size - nByte; ++ if( (x = size - nByte)>=0 ){ + testcase( x==4 ); + testcase( x==3 ); + if( x<4 ){ + /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total + ** number of bytes in fragments may not exceed 60. */ +- if( aData[hdr+7]>=60 ){ +- if( pbDefrag ) *pbDefrag = 1; +- return 0; +- } ++ if( aData[hdr+7]>57 ) return 0; ++ + /* Remove the slot from the free-list. Update the number of + ** fragmented bytes within the page. */ + memcpy(&aData[iAddr], &aData[pc], 2); + aData[hdr+7] += (u8)x; +- }else if( size+pc > usableSize ){ +- *pRc = SQLITE_CORRUPT_BKPT; ++ }else if( x+pc > maxPC ){ ++ /* This slot extends off the end of the usable part of the page */ ++ *pRc = SQLITE_CORRUPT_PAGE(pPg); + return 0; + }else{ + /* The slot remains on the free-list. Reduce its size to account +- ** for the portion used by the new allocation. */ ++ ** for the portion used by the new allocation. */ + put2byte(&aData[pc+2], x); + } + return &aData[pc + x]; + } ++ iAddr = pc; ++ pc = get2byte(&aData[pc]); ++ if( pc<=iAddr+size ){ ++ if( pc ){ ++ /* The next slot in the chain is not past the end of the current slot */ ++ *pRc = SQLITE_CORRUPT_PAGE(pPg); ++ } ++ return 0; ++ } ++ } ++ if( pc>maxPC+nByte-4 ){ ++ /* The free slot chain extends off the end of the page */ ++ *pRc = SQLITE_CORRUPT_PAGE(pPg); + } +- + return 0; + } + +@@ -54393,25 +64815,31 @@ static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ + ** then the cell content offset of an empty page wants to be 65536. + ** However, that integer is too large to be stored in a 2-byte unsigned + ** integer, so a value of 0 is used in its place. */ +- top = get2byteNotZero(&data[hdr+5]); +- if( gap>top ) return SQLITE_CORRUPT_BKPT; ++ top = get2byte(&data[hdr+5]); ++ assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */ ++ if( gap>top ){ ++ if( top==0 && pPage->pBt->usableSize==65536 ){ ++ top = 65536; ++ }else{ ++ return SQLITE_CORRUPT_PAGE(pPage); ++ } ++ } + +- /* If there is enough space between gap and top for one more cell pointer +- ** array entry offset, and if the freelist is not empty, then search the +- ** freelist looking for a free slot big enough to satisfy the request. ++ /* If there is enough space between gap and top for one more cell pointer, ++ ** and if the freelist is not empty, then search the ++ ** freelist looking for a slot big enough to satisfy the request. + */ + testcase( gap+2==top ); + testcase( gap+1==top ); + testcase( gap==top ); +- if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){ +- int bDefrag = 0; +- u8 *pSpace = pageFindSlot(pPage, nByte, &rc, &bDefrag); +- if( rc ) return rc; +- if( bDefrag ) goto defragment_page; ++ if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){ ++ u8 *pSpace = pageFindSlot(pPage, nByte, &rc); + if( pSpace ){ + assert( pSpace>=data && (pSpace - data)<65536 ); + *pIdx = (int)(pSpace - data); + return SQLITE_OK; ++ }else if( rc ){ ++ return rc; + } + } + +@@ -54420,17 +64848,17 @@ static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ + */ + testcase( gap+2+nByte==top ); + if( gap+2+nByte>top ){ +- defragment_page: + assert( pPage->nCell>0 || CORRUPT_DB ); +- rc = defragmentPage(pPage); ++ assert( pPage->nFree>=0 ); ++ rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte))); + if( rc ) return rc; + top = get2byteNotZero(&data[hdr+5]); +- assert( gap+nByte<=top ); ++ assert( gap+2+nByte<=top ); + } + + + /* Allocate memory from the gap in between the cell pointer array +- ** and the cell content area. The btreeInitPage() call has already ++ ** and the cell content area. The btreeComputeFreeSpace() call has already + ** validated the freelist. Given that the freelist is valid, there + ** is no way that the allocation can extend off the end of the page. + ** The assert() below verifies the previous sentence. +@@ -54449,7 +64877,7 @@ static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ + ** + ** Adjacent freeblocks are coalesced. + ** +-** Note that even though the freeblock list was checked by btreeInitPage(), ++** Even though the freeblock list was checked by btreeComputeFreeSpace(), + ** that routine will not detect overlap between cells or freeblocks. Nor + ** does it detect cells or freeblocks that encrouch into the reserved bytes + ** at the end of the page. So do additional corruption checks inside this +@@ -54461,23 +64889,17 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ + u8 hdr; /* Page header size. 0 or 100 */ + u8 nFrag = 0; /* Reduction in fragmentation */ + u16 iOrigSize = iSize; /* Original value of iSize */ +- u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */ ++ u16 x; /* Offset to cell content area */ + u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */ + unsigned char *data = pPage->aData; /* Page content */ + + assert( pPage->pBt!=0 ); + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); +- assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); ++ assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); + assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( iSize>=4 ); /* Minimum cell size is 4 */ +- assert( iStart<=iLast ); +- +- /* Overwrite deleted information with zeros when the secure_delete +- ** option is enabled */ +- if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){ +- memset(&data[iStart], 0, iSize); +- } ++ assert( iStart<=pPage->pBt->usableSize-4 ); + + /* The list of freeblocks must be in ascending order. Find the + ** spot on the list where iStart should be inserted. +@@ -54487,23 +64909,31 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ + if( data[iPtr+1]==0 && data[iPtr]==0 ){ + iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */ + }else{ +- while( (iFreeBlk = get2byte(&data[iPtr]))>0 && iFreeBlk iLast ) return SQLITE_CORRUPT_BKPT; ++ if( iFreeBlk>pPage->pBt->usableSize-4 ){ ++ return SQLITE_CORRUPT_PAGE(pPage); ++ } + assert( iFreeBlk>iPtr || iFreeBlk==0 ); + + /* At this point: + ** iFreeBlk: First freeblock after iStart, or zero if none +- ** iPtr: The address of a pointer iFreeBlk ++ ** iPtr: The address of a pointer to iFreeBlk + ** + ** Check to see if iFreeBlk should be coalesced onto the end of iStart. + */ + if( iFreeBlk && iEnd+3>=iFreeBlk ){ + nFrag = iFreeBlk - iEnd; +- if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT; ++ if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage); + iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]); ++ if( iEnd > pPage->pBt->usableSize ){ ++ return SQLITE_CORRUPT_PAGE(pPage); ++ } + iSize = iEnd - iStart; + iFreeBlk = get2byte(&data[iFreeBlk]); + } +@@ -54515,28 +64945,34 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ + if( iPtr>hdr+1 ){ + int iPtrEnd = iPtr + get2byte(&data[iPtr+2]); + if( iPtrEnd+3>=iStart ){ +- if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT; ++ if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage); + nFrag += iStart - iPtrEnd; + iSize = iEnd - iPtr; + iStart = iPtr; + } + } +- if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_BKPT; ++ if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage); + data[hdr+7] -= nFrag; + } +- if( iStart==get2byte(&data[hdr+5]) ){ ++ x = get2byte(&data[hdr+5]); ++ if( iStart<=x ){ + /* The new freeblock is at the beginning of the cell content area, + ** so just extend the cell content area rather than create another + ** freelist entry */ +- if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_BKPT; ++ if( iStart pBt->btsFlags & BTS_FAST_SECURE ){ ++ /* Overwrite deleted information with zeros when the secure_delete ++ ** option is enabled */ ++ memset(&data[iStart], 0, iSize); ++ } ++ put2byte(&data[iStart], iFreeBlk); ++ put2byte(&data[iStart+2], iSize); + pPage->nFree += iOrigSize; + return SQLITE_OK; + } +@@ -54561,172 +64997,219 @@ static int decodeFlags(MemPage *pPage, int flagByte){ + pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); + flagByte &= ~PTF_LEAF; + pPage->childPtrSize = 4-4*pPage->leaf; ++ pPage->xCellSize = cellSizePtr; + pBt = pPage->pBt; + if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ +- /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior +- ** table b-tree page. */ ++ /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an ++ ** interior table b-tree page. */ + assert( (PTF_LEAFDATA|PTF_INTKEY)==5 ); +- /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf +- ** table b-tree page. */ ++ /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a ++ ** leaf table b-tree page. */ + assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 ); + pPage->intKey = 1; +- pPage->intKeyLeaf = pPage->leaf; +- pPage->noPayload = !pPage->leaf; ++ if( pPage->leaf ){ ++ pPage->intKeyLeaf = 1; ++ pPage->xParseCell = btreeParseCellPtr; ++ }else{ ++ pPage->intKeyLeaf = 0; ++ pPage->xCellSize = cellSizePtrNoPayload; ++ pPage->xParseCell = btreeParseCellPtrNoPayload; ++ } + pPage->maxLocal = pBt->maxLeaf; + pPage->minLocal = pBt->minLeaf; + }else if( flagByte==PTF_ZERODATA ){ +- /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior +- ** index b-tree page. */ ++ /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an ++ ** interior index b-tree page. */ + assert( (PTF_ZERODATA)==2 ); +- /* EVIDENCE-OF: R-16571-11615 A value of 10 means the page is a leaf +- ** index b-tree page. */ ++ /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a ++ ** leaf index b-tree page. */ + assert( (PTF_ZERODATA|PTF_LEAF)==10 ); + pPage->intKey = 0; + pPage->intKeyLeaf = 0; +- pPage->noPayload = 0; ++ pPage->xParseCell = btreeParseCellPtrIndex; + pPage->maxLocal = pBt->maxLocal; + pPage->minLocal = pBt->minLocal; + }else{ + /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is + ** an error. */ +- return SQLITE_CORRUPT_BKPT; ++ return SQLITE_CORRUPT_PAGE(pPage); + } + pPage->max1bytePayload = pBt->max1bytePayload; + return SQLITE_OK; + } + + /* +-** Initialize the auxiliary information for a disk block. +-** +-** Return SQLITE_OK on success. If we see that the page does +-** not contain a well-formed database page, then return +-** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not +-** guarantee that the page is well-formed. It only shows that +-** we failed to detect any corruption. ++** Compute the amount of freespace on the page. In other words, fill ++** in the pPage->nFree field. + */ +-static int btreeInitPage(MemPage *pPage){ ++static int btreeComputeFreeSpace(MemPage *pPage){ ++ int pc; /* Address of a freeblock within pPage->aData[] */ ++ u8 hdr; /* Offset to beginning of page header */ ++ u8 *data; /* Equal to pPage->aData */ ++ int usableSize; /* Amount of usable space on each page */ ++ int nFree; /* Number of unused bytes on the page */ ++ int top; /* First byte of the cell content area */ ++ int iCellFirst; /* First allowable cell or freeblock offset */ ++ int iCellLast; /* Last possible cell or freeblock offset */ + + assert( pPage->pBt!=0 ); ++ assert( pPage->pBt->db!=0 ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); + assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); + assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); ++ assert( pPage->isInit==1 ); ++ assert( pPage->nFree<0 ); + +- if( !pPage->isInit ){ +- u16 pc; /* Address of a freeblock within pPage->aData[] */ +- u8 hdr; /* Offset to beginning of page header */ +- u8 *data; /* Equal to pPage->aData */ +- BtShared *pBt; /* The main btree structure */ +- int usableSize; /* Amount of usable space on each page */ +- u16 cellOffset; /* Offset from start of page to first cell pointer */ +- int nFree; /* Number of unused bytes on the page */ +- int top; /* First byte of the cell content area */ +- int iCellFirst; /* First allowable cell or freeblock offset */ +- int iCellLast; /* Last possible cell or freeblock offset */ +- +- pBt = pPage->pBt; +- +- hdr = pPage->hdrOffset; +- data = pPage->aData; +- /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating +- ** the b-tree page type. */ +- if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; +- assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); +- pPage->maskPage = (u16)(pBt->pageSize - 1); +- pPage->nOverflow = 0; +- usableSize = pBt->usableSize; +- pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize; +- pPage->aDataEnd = &data[usableSize]; +- pPage->aCellIdx = &data[cellOffset]; +- /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates +- ** the start of the cell content area. A zero value for this integer is +- ** interpreted as 65536. */ +- top = get2byteNotZero(&data[hdr+5]); +- /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the +- ** number of cells on the page. */ +- pPage->nCell = get2byte(&data[hdr+3]); +- if( pPage->nCell>MX_CELL(pBt) ){ +- /* To many cells for a single page. The page must be corrupt */ +- return SQLITE_CORRUPT_BKPT; +- } +- testcase( pPage->nCell==MX_CELL(pBt) ); +- /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only +- ** possible for a root page of a table that contains no rows) then the +- ** offset to the cell content area will equal the page size minus the +- ** bytes of reserved space. */ +- assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB ); +- +- /* A malformed database page might cause us to read past the end +- ** of page when parsing a cell. +- ** +- ** The following block of code checks early to see if a cell extends +- ** past the end of a page boundary and causes SQLITE_CORRUPT to be +- ** returned if it does. +- */ +- iCellFirst = cellOffset + 2*pPage->nCell; +- iCellLast = usableSize - 4; +-#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) +- { +- int i; /* Index into the cell pointer array */ +- int sz; /* Size of a cell */ +- +- if( !pPage->leaf ) iCellLast--; +- for(i=0; i nCell; i++){ +- pc = get2byte(&data[cellOffset+i*2]); +- testcase( pc==iCellFirst ); +- testcase( pc==iCellLast ); +- if( pc iCellLast ){ +- return SQLITE_CORRUPT_BKPT; +- } +- sz = cellSizePtr(pPage, &data[pc]); +- testcase( pc+sz==usableSize ); +- if( pc+sz>usableSize ){ +- return SQLITE_CORRUPT_BKPT; +- } +- } +- if( !pPage->leaf ) iCellLast++; +- } +-#endif ++ usableSize = pPage->pBt->usableSize; ++ hdr = pPage->hdrOffset; ++ data = pPage->aData; ++ /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates ++ ** the start of the cell content area. A zero value for this integer is ++ ** interpreted as 65536. */ ++ top = get2byteNotZero(&data[hdr+5]); ++ iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell; ++ iCellLast = usableSize - 4; + +- /* Compute the total free space on the page +- ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the +- ** start of the first freeblock on the page, or is zero if there are no +- ** freeblocks. */ +- pc = get2byte(&data[hdr+1]); +- nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */ +- while( pc>0 ){ +- u16 next, size; +- if( pc iCellLast ){ +- /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will +- ** always be at least one cell before the first freeblock. +- ** +- ** Or, the freeblock is off the end of the page +- */ +- return SQLITE_CORRUPT_BKPT; ++ /* Compute the total free space on the page ++ ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the ++ ** start of the first freeblock on the page, or is zero if there are no ++ ** freeblocks. */ ++ pc = get2byte(&data[hdr+1]); ++ nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */ ++ if( pc>0 ){ ++ u32 next, size; ++ if( pc iCellLast ){ ++ /* Freeblock off the end of the page */ ++ return SQLITE_CORRUPT_PAGE(pPage); + } + next = get2byte(&data[pc]); + size = get2byte(&data[pc+2]); +- if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){ +- /* Free blocks must be in ascending order. And the last byte of +- ** the free-block must lie on the database page. */ +- return SQLITE_CORRUPT_BKPT; +- } + nFree = nFree + size; ++ if( next<=pc+size+3 ) break; + pc = next; + } ++ if( next>0 ){ ++ /* Freeblock not in ascending order */ ++ return SQLITE_CORRUPT_PAGE(pPage); ++ } ++ if( pc+size>(unsigned int)usableSize ){ ++ /* Last freeblock extends past page end */ ++ return SQLITE_CORRUPT_PAGE(pPage); ++ } ++ } + +- /* At this point, nFree contains the sum of the offset to the start +- ** of the cell-content area plus the number of free bytes within +- ** the cell-content area. If this is greater than the usable-size +- ** of the page, then the page must be corrupted. This check also +- ** serves to verify that the offset to the start of the cell-content +- ** area, according to the page header, lies within the page. +- */ +- if( nFree>usableSize ){ +- return SQLITE_CORRUPT_BKPT; ++ /* At this point, nFree contains the sum of the offset to the start ++ ** of the cell-content area plus the number of free bytes within ++ ** the cell-content area. If this is greater than the usable-size ++ ** of the page, then the page must be corrupted. This check also ++ ** serves to verify that the offset to the start of the cell-content ++ ** area, according to the page header, lies within the page. ++ */ ++ if( nFree>usableSize ){ ++ return SQLITE_CORRUPT_PAGE(pPage); ++ } ++ pPage->nFree = (u16)(nFree - iCellFirst); ++ return SQLITE_OK; ++} ++ ++/* ++** Do additional sanity check after btreeInitPage() if ++** PRAGMA cell_size_check=ON ++*/ ++static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){ ++ int iCellFirst; /* First allowable cell or freeblock offset */ ++ int iCellLast; /* Last possible cell or freeblock offset */ ++ int i; /* Index into the cell pointer array */ ++ int sz; /* Size of a cell */ ++ int pc; /* Address of a freeblock within pPage->aData[] */ ++ u8 *data; /* Equal to pPage->aData */ ++ int usableSize; /* Maximum usable space on the page */ ++ int cellOffset; /* Start of cell content area */ ++ ++ iCellFirst = pPage->cellOffset + 2*pPage->nCell; ++ usableSize = pPage->pBt->usableSize; ++ iCellLast = usableSize - 4; ++ data = pPage->aData; ++ cellOffset = pPage->cellOffset; ++ if( !pPage->leaf ) iCellLast--; ++ for(i=0; i nCell; i++){ ++ pc = get2byteAligned(&data[cellOffset+i*2]); ++ testcase( pc==iCellFirst ); ++ testcase( pc==iCellLast ); ++ if( pc iCellLast ){ ++ return SQLITE_CORRUPT_PAGE(pPage); + } +- pPage->nFree = (u16)(nFree - iCellFirst); +- pPage->isInit = 1; ++ sz = pPage->xCellSize(pPage, &data[pc]); ++ testcase( pc+sz==usableSize ); ++ if( pc+sz>usableSize ){ ++ return SQLITE_CORRUPT_PAGE(pPage); ++ } ++ } ++ return SQLITE_OK; ++} ++ ++/* ++** Initialize the auxiliary information for a disk block. ++** ++** Return SQLITE_OK on success. If we see that the page does ++** not contain a well-formed database page, then return ++** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not ++** guarantee that the page is well-formed. It only shows that ++** we failed to detect any corruption. ++*/ ++static int btreeInitPage(MemPage *pPage){ ++ u8 *data; /* Equal to pPage->aData */ ++ BtShared *pBt; /* The main btree structure */ ++ ++ assert( pPage->pBt!=0 ); ++ assert( pPage->pBt->db!=0 ); ++ assert( sqlite3_mutex_held(pPage->pBt->mutex) ); ++ assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); ++ assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); ++ assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); ++ assert( pPage->isInit==0 ); ++ ++ pBt = pPage->pBt; ++ data = pPage->aData + pPage->hdrOffset; ++ /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating ++ ** the b-tree page type. */ ++ if( decodeFlags(pPage, data[0]) ){ ++ return SQLITE_CORRUPT_PAGE(pPage); ++ } ++ assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); ++ pPage->maskPage = (u16)(pBt->pageSize - 1); ++ pPage->nOverflow = 0; ++ pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize; ++ pPage->aCellIdx = data + pPage->childPtrSize + 8; ++ pPage->aDataEnd = pPage->aData + pBt->usableSize; ++ pPage->aDataOfst = pPage->aData + pPage->childPtrSize; ++ /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the ++ ** number of cells on the page. */ ++ pPage->nCell = get2byte(&data[3]); ++ if( pPage->nCell>MX_CELL(pBt) ){ ++ /* To many cells for a single page. The page must be corrupt */ ++ return SQLITE_CORRUPT_PAGE(pPage); ++ } ++ testcase( pPage->nCell==MX_CELL(pBt) ); ++ /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only ++ ** possible for a root page of a table that contains no rows) then the ++ ** offset to the cell content area will equal the page size minus the ++ ** bytes of reserved space. */ ++ assert( pPage->nCell>0 ++ || get2byteNotZero(&data[5])==(int)pBt->usableSize ++ || CORRUPT_DB ); ++ pPage->nFree = -1; /* Indicate that this value is yet uncomputed */ ++ pPage->isInit = 1; ++ if( pBt->db->flags & SQLITE_CellSizeCk ){ ++ return btreeCellSizeCheck(pPage); + } + return SQLITE_OK; + } +@@ -54746,7 +65229,7 @@ static void zeroPage(MemPage *pPage, int flags){ + assert( sqlite3PagerGetData(pPage->pDbPage) == data ); + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + assert( sqlite3_mutex_held(pBt->mutex) ); +- if( pBt->btsFlags & BTS_SECURE_DELETE ){ ++ if( pBt->btsFlags & BTS_FAST_SECURE ){ + memset(&data[hdr], 0, pBt->usableSize - hdr); + } + data[hdr] = (char)flags; +@@ -54759,6 +65242,7 @@ static void zeroPage(MemPage *pPage, int flags){ + pPage->cellOffset = first; + pPage->aDataEnd = &data[pBt->usableSize]; + pPage->aCellIdx = &data[first]; ++ pPage->aDataOfst = &data[pPage->childPtrSize]; + pPage->nOverflow = 0; + assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); + pPage->maskPage = (u16)(pBt->pageSize - 1); +@@ -54773,20 +65257,23 @@ static void zeroPage(MemPage *pPage, int flags){ + */ + static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){ + MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); +- pPage->aData = sqlite3PagerGetData(pDbPage); +- pPage->pDbPage = pDbPage; +- pPage->pBt = pBt; +- pPage->pgno = pgno; +- pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; ++ if( pgno!=pPage->pgno ){ ++ pPage->aData = sqlite3PagerGetData(pDbPage); ++ pPage->pDbPage = pDbPage; ++ pPage->pBt = pBt; ++ pPage->pgno = pgno; ++ pPage->hdrOffset = pgno==1 ? 100 : 0; ++ } ++ assert( pPage->aData==sqlite3PagerGetData(pDbPage) ); + return pPage; + } + + /* + ** Get a page from the pager. Initialize the MemPage.pBt and +-** MemPage.aData elements if needed. ++** MemPage.aData elements if needed. See also: btreeGetUnusedPage(). + ** +-** If the noContent flag is set, it means that we do not care about +-** the content of the page at this time. So do not go to the disk ++** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care ++** about the content of the page at this time. So do not go to the disk + ** to fetch the content. Just fill in the content with zeros for now. + ** If in the future we call sqlite3PagerWrite() on this page, that + ** means we have started to be concerned about content and the disk +@@ -54803,7 +65290,7 @@ static int btreeGetPage( + + assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY ); + assert( sqlite3_mutex_held(pBt->mutex) ); +- rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, flags); ++ rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags); + if( rc ) return rc; + *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt); + return SQLITE_OK; +@@ -54833,40 +65320,71 @@ static Pgno btreePagecount(BtShared *pBt){ + } + SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){ + assert( sqlite3BtreeHoldsMutex(p) ); +- assert( ((p->pBt->nPage)&0x8000000)==0 ); ++ assert( ((p->pBt->nPage)&0x80000000)==0 ); + return btreePagecount(p->pBt); + } + + /* +-** Get a page from the pager and initialize it. This routine is just a +-** convenience wrapper around separate calls to btreeGetPage() and +-** btreeInitPage(). ++** Get a page from the pager and initialize it. ++** ++** If pCur!=0 then the page is being fetched as part of a moveToChild() ++** call. Do additional sanity checking on the page in this case. ++** And if the fetch fails, this routine must decrement pCur->iPage. ++** ++** The page is fetched as read-write unless pCur is not NULL and is ++** a read-only cursor. + ** +-** If an error occurs, then the value *ppPage is set to is undefined. It ++** If an error occurs, then *ppPage is undefined. It + ** may remain unchanged, or it may be set to an invalid value. + */ + static int getAndInitPage( + BtShared *pBt, /* The database file */ + Pgno pgno, /* Number of the page to get */ + MemPage **ppPage, /* Write the page pointer here */ +- int bReadonly /* PAGER_GET_READONLY or 0 */ ++ BtCursor *pCur, /* Cursor to receive the page, or NULL */ ++ int bReadOnly /* True for a read-only page */ + ){ + int rc; ++ DbPage *pDbPage; + assert( sqlite3_mutex_held(pBt->mutex) ); +- assert( bReadonly==PAGER_GET_READONLY || bReadonly==0 ); ++ assert( pCur==0 || ppPage==&pCur->pPage ); ++ assert( pCur==0 || bReadOnly==pCur->curPagerFlags ); ++ assert( pCur==0 || pCur->iPage>0 ); + + if( pgno>btreePagecount(pBt) ){ + rc = SQLITE_CORRUPT_BKPT; +- }else{ +- rc = btreeGetPage(pBt, pgno, ppPage, bReadonly); +- if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ +- rc = btreeInitPage(*ppPage); +- if( rc!=SQLITE_OK ){ +- releasePage(*ppPage); +- } ++ goto getAndInitPage_error1; ++ } ++ rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly); ++ if( rc ){ ++ goto getAndInitPage_error1; ++ } ++ *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); ++ if( (*ppPage)->isInit==0 ){ ++ btreePageFromDbPage(pDbPage, pgno, pBt); ++ rc = btreeInitPage(*ppPage); ++ if( rc!=SQLITE_OK ){ ++ goto getAndInitPage_error2; + } + } ++ assert( (*ppPage)->pgno==pgno ); ++ assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) ); ++ ++ /* If obtaining a child page for a cursor, we must verify that the page is ++ ** compatible with the root page. */ ++ if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){ ++ rc = SQLITE_CORRUPT_PGNO(pgno); ++ goto getAndInitPage_error2; ++ } ++ return SQLITE_OK; + ++getAndInitPage_error2: ++ releasePage(*ppPage); ++getAndInitPage_error1: ++ if( pCur ){ ++ pCur->iPage--; ++ pCur->pPage = pCur->apPage[pCur->iPage]; ++ } + testcase( pgno==0 ); + assert( pgno!=0 || rc==SQLITE_CORRUPT ); + return rc; +@@ -54875,19 +65393,62 @@ static int getAndInitPage( + /* + ** Release a MemPage. This should be called once for each prior + ** call to btreeGetPage. ++** ++** Page1 is a special case and must be released using releasePageOne(). + */ ++static void releasePageNotNull(MemPage *pPage){ ++ assert( pPage->aData ); ++ assert( pPage->pBt ); ++ assert( pPage->pDbPage!=0 ); ++ assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); ++ assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); ++ assert( sqlite3_mutex_held(pPage->pBt->mutex) ); ++ sqlite3PagerUnrefNotNull(pPage->pDbPage); ++} + static void releasePage(MemPage *pPage){ +- if( pPage ){ +- assert( pPage->aData ); +- assert( pPage->pBt ); +- assert( pPage->pDbPage!=0 ); +- assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); +- assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); +- assert( sqlite3_mutex_held(pPage->pBt->mutex) ); +- sqlite3PagerUnrefNotNull(pPage->pDbPage); ++ if( pPage ) releasePageNotNull(pPage); ++} ++static void releasePageOne(MemPage *pPage){ ++ assert( pPage!=0 ); ++ assert( pPage->aData ); ++ assert( pPage->pBt ); ++ assert( pPage->pDbPage!=0 ); ++ assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); ++ assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); ++ assert( sqlite3_mutex_held(pPage->pBt->mutex) ); ++ sqlite3PagerUnrefPageOne(pPage->pDbPage); ++} ++ ++/* ++** Get an unused page. ++** ++** This works just like btreeGetPage() with the addition: ++** ++** * If the page is already in use for some other purpose, immediately ++** release it and return an SQLITE_CURRUPT error. ++** * Make sure the isInit flag is clear ++*/ ++static int btreeGetUnusedPage( ++ BtShared *pBt, /* The btree */ ++ Pgno pgno, /* Number of the page to fetch */ ++ MemPage **ppPage, /* Return the page in this parameter */ ++ int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */ ++){ ++ int rc = btreeGetPage(pBt, pgno, ppPage, flags); ++ if( rc==SQLITE_OK ){ ++ if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ ++ releasePage(*ppPage); ++ *ppPage = 0; ++ return SQLITE_CORRUPT_BKPT; ++ } ++ (*ppPage)->isInit = 0; ++ }else{ ++ *ppPage = 0; + } ++ return rc; + } + ++ + /* + ** During a rollback, when the pager reloads information into the cache + ** so that the cache is restored to its original state at the start of +@@ -54922,7 +65483,8 @@ static int btreeInvokeBusyHandler(void *pArg){ + BtShared *pBt = (BtShared*)pArg; + assert( pBt->db ); + assert( sqlite3_mutex_held(pBt->db->mutex) ); +- return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); ++ return sqlite3InvokeBusyHandler(&pBt->db->busyHandler, ++ sqlite3PagerFile(pBt->pPager)); + } + + /* +@@ -54994,7 +65556,7 @@ SQLITE_PRIVATE int sqlite3BtreeOpen( + } + p = sqlite3MallocZero(sizeof(Btree)); + if( !p ){ +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + p->inTrans = TRANS_NONE; + p->db = db; +@@ -55018,7 +65580,7 @@ SQLITE_PRIVATE int sqlite3BtreeOpen( + p->sharable = 1; + if( !zFullPathname ){ + sqlite3_free(p); +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } + if( isMemdb ){ + memcpy(zFullPathname, zFilename, nFilename); +@@ -55086,11 +65648,11 @@ SQLITE_PRIVATE int sqlite3BtreeOpen( + + pBt = sqlite3MallocZero( sizeof(*pBt) ); + if( pBt==0 ){ +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + goto btree_open_out; + } + rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, +- EXTRA_SIZE, flags, vfsFlags, pageReinit); ++ sizeof(MemPage), flags, vfsFlags, pageReinit); + if( rc==SQLITE_OK ){ + sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap); + rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); +@@ -55100,14 +65662,16 @@ SQLITE_PRIVATE int sqlite3BtreeOpen( + } + pBt->openFlags = (u8)flags; + pBt->db = db; +- sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt); ++ sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt); + p->pBt = pBt; + + pBt->pCursor = 0; + pBt->pPage1 = 0; + if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY; +-#ifdef SQLITE_SECURE_DELETE ++#if defined(SQLITE_SECURE_DELETE) + pBt->btsFlags |= BTS_SECURE_DELETE; ++#elif defined(SQLITE_FAST_SECURE_DELETE) ++ pBt->btsFlags |= BTS_OVERWRITE; + #endif + /* EVIDENCE-OF: R-51873-39618 The page size for a database file is + ** determined by the 2-byte integer located at an offset of 16 bytes from +@@ -55148,15 +65712,14 @@ SQLITE_PRIVATE int sqlite3BtreeOpen( + #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) + /* Add the new BtShared object to the linked list sharable BtShareds. + */ ++ pBt->nRef = 1; + if( p->sharable ){ + MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) +- pBt->nRef = 1; + MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);) + if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ + pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); + if( pBt->mutex==0 ){ +- rc = SQLITE_NOMEM; +- db->mallocFailed = 0; ++ rc = SQLITE_NOMEM_BKPT; + goto btree_open_out; + } + } +@@ -55179,12 +65742,12 @@ SQLITE_PRIVATE int sqlite3BtreeOpen( + for(i=0; i nDb; i++){ + if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ + while( pSib->pPrev ){ pSib = pSib->pPrev; } +- if( p->pBt pBt ){ ++ if( (uptr)p->pBt<(uptr)pSib->pBt ){ + p->pNext = pSib; + p->pPrev = 0; + pSib->pPrev = p; + }else{ +- while( pSib->pNext && pSib->pNext->pBt pBt ){ ++ while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){ + pSib = pSib->pNext; + } + p->pNext = pSib->pNext; +@@ -55204,12 +65767,14 @@ SQLITE_PRIVATE int sqlite3BtreeOpen( + btree_open_out: + if( rc!=SQLITE_OK ){ + if( pBt && pBt->pPager ){ +- sqlite3PagerClose(pBt->pPager); ++ sqlite3PagerClose(pBt->pPager, 0); + } + sqlite3_free(pBt); + sqlite3_free(p); + *ppBtree = 0; + }else{ ++ sqlite3_file *pFile; ++ + /* If the B-Tree was successfully opened, set the pager-cache size to the + ** default value. Except, when opening on an existing shared pager-cache, + ** do not change the pager-cache size. +@@ -55217,11 +65782,17 @@ btree_open_out: + if( sqlite3BtreeSchema(p, 0, 0)==0 ){ + sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE); + } ++ ++ pFile = sqlite3PagerFile(pBt->pPager); ++ if( pFile->pMethods ){ ++ sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db); ++ } + } + if( mutexOpen ){ + assert( sqlite3_mutex_held(mutexOpen) ); + sqlite3_mutex_leave(mutexOpen); + } ++ assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 ); + return rc; + } + +@@ -55345,7 +65916,7 @@ SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){ + ** Clean out and delete the BtShared object. + */ + assert( !pBt->pCursor ); +- sqlite3PagerClose(pBt->pPager); ++ sqlite3PagerClose(pBt->pPager, p->db); + if( pBt->xFreeSchema && pBt->pSchema ){ + pBt->xFreeSchema(pBt->pSchema); + } +@@ -55366,19 +65937,11 @@ SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){ + } + + /* +-** Change the limit on the number of pages allowed in the cache. +-** +-** The maximum number of cache pages is set to the absolute +-** value of mxPage. If mxPage is negative, the pager will +-** operate asynchronously - it will not stop to do fsync()s +-** to insure data is written to the disk surface before +-** continuing. Transactions still work if synchronous is off, +-** and the database cannot be corrupted if this program +-** crashes. But if the operating system crashes or there is +-** an abrupt power failure when synchronous is off, the database +-** could be left in an inconsistent and unrecoverable state. +-** Synchronous is on by default so database corruption is not +-** normally a worry. ++** Change the "soft" limit on the number of pages in the cache. ++** Unused and unmodified pages will be recycled when the number of ++** pages in the cache exceeds this soft limit. But the size of the ++** cache is allowed to grow larger than this limit if it contains ++** dirty pages or pages still in active use. + */ + SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ + BtShared *pBt = p->pBt; +@@ -55389,6 +65952,26 @@ SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ + return SQLITE_OK; + } + ++/* ++** Change the "spill" limit on the number of pages in the cache. ++** If the number of pages exceeds this limit during a write transaction, ++** the pager might attempt to "spill" pages to the journal early in ++** order to free up memory. ++** ++** The value returned is the current spill size. If zero is passed ++** as an argument, no changes are made to the spill size setting, so ++** using mxPage of 0 is a way to query the current spill size. ++*/ ++SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){ ++ BtShared *pBt = p->pBt; ++ int res; ++ assert( sqlite3_mutex_held(p->db->mutex) ); ++ sqlite3BtreeEnter(p); ++ res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage); ++ sqlite3BtreeLeave(p); ++ return res; ++} ++ + #if SQLITE_MAX_MMAP_SIZE>0 + /* + ** Change the limit on the amount of the database file that may be +@@ -55426,21 +66009,6 @@ SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags( + } + #endif + +-/* +-** Return TRUE if the given btree is set to safety level 1. In other +-** words, return TRUE if no sync() occurs on the disk files. +-*/ +-SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){ +- BtShared *pBt = p->pBt; +- int rc; +- assert( sqlite3_mutex_held(p->db->mutex) ); +- sqlite3BtreeEnter(p); +- assert( pBt && pBt->pPager ); +- rc = sqlite3PagerNosync(pBt->pPager); +- sqlite3BtreeLeave(p); +- return rc; +-} +- + /* + ** Change the default pages size and the number of reserved bytes per page. + ** Or, if the page size has already been fixed, return SQLITE_READONLY +@@ -55551,19 +66119,34 @@ SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ + } + + /* +-** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1. If newFlag is -1, +-** then make no changes. Always return the value of the BTS_SECURE_DELETE +-** setting after the change. ++** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags: ++** ++** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared ++** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared ++** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set ++** newFlag==(-1) No changes ++** ++** This routine acts as a query if newFlag is less than zero ++** ++** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but ++** freelist leaf pages are not written back to the database. Thus in-page ++** deleted content is cleared, but freelist deleted content is not. ++** ++** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition ++** that freelist leaf pages are written back into the database, increasing ++** the amount of disk I/O. + */ + SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){ + int b; + if( p==0 ) return 0; + sqlite3BtreeEnter(p); ++ assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 ); ++ assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) ); + if( newFlag>=0 ){ +- p->pBt->btsFlags &= ~BTS_SECURE_DELETE; +- if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE; +- } +- b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0; ++ p->pBt->btsFlags &= ~BTS_FAST_SECURE; ++ p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag; ++ } ++ b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE; + sqlite3BtreeLeave(p); + return b; + } +@@ -55614,6 +66197,36 @@ SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){ + #endif + } + ++/* ++** If the user has not set the safety-level for this database connection ++** using "PRAGMA synchronous", and if the safety-level is not already ++** set to the value passed to this function as the second parameter, ++** set it so. ++*/ ++#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \ ++ && !defined(SQLITE_OMIT_WAL) ++static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){ ++ sqlite3 *db; ++ Db *pDb; ++ if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){ ++ while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; } ++ if( pDb->bSyncSet==0 ++ && pDb->safety_level!=safety_level ++ && pDb!=&db->aDb[1] ++ ){ ++ pDb->safety_level = safety_level; ++ sqlite3PagerSetFlags(pBt->pPager, ++ pDb->safety_level | (db->flags & PAGER_FLAGS_MASK)); ++ } ++ } ++} ++#else ++# define setDefaultSyncFlag(pBt,safety_level) ++#endif ++ ++/* Forward declaration */ ++static int newDatabase(BtShared*); ++ + + /* + ** Get a reference to pPage1 of the database file. This will +@@ -55627,9 +66240,9 @@ SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){ + static int lockBtree(BtShared *pBt){ + int rc; /* Result code from subfunctions */ + MemPage *pPage1; /* Page 1 of the database file */ +- int nPage; /* Number of pages in the database */ +- int nPageFile = 0; /* Number of pages in the database file */ +- int nPageHeader; /* Number of pages in the database according to hdr */ ++ u32 nPage; /* Number of pages in the database */ ++ u32 nPageFile = 0; /* Number of pages in the database file */ ++ u32 nPageHeader; /* Number of pages in the database according to hdr */ + + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( pBt->pPage1==0 ); +@@ -55642,10 +66255,13 @@ static int lockBtree(BtShared *pBt){ + ** a valid database file. + */ + nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData); +- sqlite3PagerPagecount(pBt->pPager, &nPageFile); ++ sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile); + if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){ + nPage = nPageFile; + } ++ if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){ ++ nPage = 0; ++ } + if( nPage>0 ){ + u32 pageSize; + u32 usableSize; +@@ -55686,11 +66302,16 @@ static int lockBtree(BtShared *pBt){ + rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen); + if( rc!=SQLITE_OK ){ + goto page1_init_failed; +- }else if( isOpen==0 ){ +- releasePage(pPage1); +- return SQLITE_OK; ++ }else{ ++ setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1); ++ if( isOpen==0 ){ ++ releasePageOne(pPage1); ++ return SQLITE_OK; ++ } + } + rc = SQLITE_NOTADB; ++ }else{ ++ setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1); + } + #endif + +@@ -55715,6 +66336,7 @@ static int lockBtree(BtShared *pBt){ + ){ + goto page1_init_failed; + } ++ pBt->btsFlags |= BTS_PAGESIZE_FIXED; + assert( (pageSize & 7)==0 ); + /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte + ** integer at offset 20 is the number of bytes of space at the end of +@@ -55731,7 +66353,7 @@ static int lockBtree(BtShared *pBt){ + ** zero and return SQLITE_OK. The caller will call this function + ** again with the correct page-size. + */ +- releasePage(pPage1); ++ releasePageOne(pPage1); + pBt->usableSize = usableSize; + pBt->pageSize = pageSize; + freeTempSpace(pBt); +@@ -55739,7 +66361,7 @@ static int lockBtree(BtShared *pBt){ + pageSize-usableSize); + return rc; + } +- if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){ ++ if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){ + rc = SQLITE_CORRUPT_BKPT; + goto page1_init_failed; + } +@@ -55785,7 +66407,7 @@ static int lockBtree(BtShared *pBt){ + return SQLITE_OK; + + page1_init_failed: +- releasePage(pPage1); ++ releasePageOne(pPage1); + pBt->pPage1 = 0; + return rc; + } +@@ -55830,7 +66452,7 @@ static void unlockBtreeIfUnused(BtShared *pBt){ + assert( pPage1->aData ); + assert( sqlite3PagerRefcount(pBt->pPager)==1 ); + pBt->pPage1 = 0; +- releasePage(pPage1); ++ releasePageOne(pPage1); + } + } + +@@ -55927,8 +66549,7 @@ SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){ + ** when A already has a read lock, we encourage A to give up and let B + ** proceed. + */ +-SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ +- sqlite3 *pBlock = 0; ++SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){ + BtShared *pBt = p->pBt; + int rc = SQLITE_OK; + +@@ -55944,6 +66565,12 @@ SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ + } + assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 ); + ++ if( (p->db->flags & SQLITE_ResetDatabase) ++ && sqlite3PagerIsreadonly(pBt->pPager)==0 ++ ){ ++ pBt->btsFlags &= ~BTS_READ_ONLY; ++ } ++ + /* Write transactions are not possible on a read-only database */ + if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){ + rc = SQLITE_READONLY; +@@ -55951,27 +66578,30 @@ SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ + } + + #ifndef SQLITE_OMIT_SHARED_CACHE +- /* If another database handle has already opened a write transaction +- ** on this shared-btree structure and a second write transaction is +- ** requested, return SQLITE_LOCKED. +- */ +- if( (wrflag && pBt->inTransaction==TRANS_WRITE) +- || (pBt->btsFlags & BTS_PENDING)!=0 +- ){ +- pBlock = pBt->pWriter->db; +- }else if( wrflag>1 ){ +- BtLock *pIter; +- for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ +- if( pIter->pBtree!=p ){ +- pBlock = pIter->pBtree->db; +- break; ++ { ++ sqlite3 *pBlock = 0; ++ /* If another database handle has already opened a write transaction ++ ** on this shared-btree structure and a second write transaction is ++ ** requested, return SQLITE_LOCKED. ++ */ ++ if( (wrflag && pBt->inTransaction==TRANS_WRITE) ++ || (pBt->btsFlags & BTS_PENDING)!=0 ++ ){ ++ pBlock = pBt->pWriter->db; ++ }else if( wrflag>1 ){ ++ BtLock *pIter; ++ for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ ++ if( pIter->pBtree!=p ){ ++ pBlock = pIter->pBtree->db; ++ break; ++ } + } + } +- } +- if( pBlock ){ +- sqlite3ConnectionBlocked(p->db, pBlock); +- rc = SQLITE_LOCKED_SHAREDCACHE; +- goto trans_begun; ++ if( pBlock ){ ++ sqlite3ConnectionBlocked(p->db, pBlock); ++ rc = SQLITE_LOCKED_SHAREDCACHE; ++ goto trans_begun; ++ } + } + #endif + +@@ -56000,6 +66630,11 @@ SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ + rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db)); + if( rc==SQLITE_OK ){ + rc = newDatabase(pBt); ++ }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){ ++ /* if there was no transaction opened when this function was ++ ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error ++ ** code to SQLITE_BUSY. */ ++ rc = SQLITE_BUSY; + } + } + } +@@ -56009,6 +66644,7 @@ SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ + } + }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && + btreeInvokeBusyHandler(pBt) ); ++ sqlite3PagerResetLockTimeout(pBt->pPager); + + if( rc==SQLITE_OK ){ + if( p->inTrans==TRANS_NONE ){ +@@ -56050,14 +66686,18 @@ SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ + } + } + +- + trans_begun: +- if( rc==SQLITE_OK && wrflag ){ +- /* This call makes sure that the pager has the correct number of +- ** open savepoints. If the second parameter is greater than 0 and +- ** the sub-journal is not already open, then it will be opened here. +- */ +- rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); ++ if( rc==SQLITE_OK ){ ++ if( pSchemaVersion ){ ++ *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]); ++ } ++ if( wrflag ){ ++ /* This call makes sure that the pager has the correct number of ++ ** open savepoints. If the second parameter is greater than 0 and ++ ** the sub-journal is not already open, then it will be opened here. ++ */ ++ rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); ++ } + } + + btreeIntegrity(p); +@@ -56077,20 +66717,17 @@ static int setChildPtrmaps(MemPage *pPage){ + int nCell; /* Number of cells in page pPage */ + int rc; /* Return code */ + BtShared *pBt = pPage->pBt; +- u8 isInitOrig = pPage->isInit; + Pgno pgno = pPage->pgno; + + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); +- rc = btreeInitPage(pPage); +- if( rc!=SQLITE_OK ){ +- goto set_child_ptrmaps_out; +- } ++ rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage); ++ if( rc!=SQLITE_OK ) return rc; + nCell = pPage->nCell; + + for(i=0; i leaf ){ + Pgno childPgno = get4byte(pCell); +@@ -56103,8 +66740,6 @@ static int setChildPtrmaps(MemPage *pPage){ + ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); + } + +-set_child_ptrmaps_out: +- pPage->isInit = isInitOrig; + return rc; + } + +@@ -56128,28 +66763,31 @@ static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ + if( eType==PTRMAP_OVERFLOW2 ){ + /* The pointer is always the first 4 bytes of the page in this case. */ + if( get4byte(pPage->aData)!=iFrom ){ +- return SQLITE_CORRUPT_BKPT; ++ return SQLITE_CORRUPT_PAGE(pPage); + } + put4byte(pPage->aData, iTo); + }else{ +- u8 isInitOrig = pPage->isInit; + int i; + int nCell; ++ int rc; + +- btreeInitPage(pPage); ++ rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage); ++ if( rc ) return rc; + nCell = pPage->nCell; + + for(i=0; i aData+pPage->maskPage +- && iFrom==get4byte(&pCell[info.iOverflow]) +- ){ +- put4byte(&pCell[info.iOverflow], iTo); +- break; ++ pPage->xParseCell(pPage, pCell, &info); ++ if( info.nLocal pPage->aData+pPage->pBt->usableSize ){ ++ return SQLITE_CORRUPT_PAGE(pPage); ++ } ++ if( iFrom==get4byte(pCell+info.nSize-4) ){ ++ put4byte(pCell+info.nSize-4, iTo); ++ break; ++ } + } + }else{ + if( get4byte(pCell)==iFrom ){ +@@ -56162,12 +66800,10 @@ static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ + if( i==nCell ){ + if( eType!=PTRMAP_BTREE || + get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ +- return SQLITE_CORRUPT_BKPT; ++ return SQLITE_CORRUPT_PAGE(pPage); + } + put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); + } +- +- pPage->isInit = isInitOrig; + } + return SQLITE_OK; + } +@@ -56199,6 +66835,7 @@ static int relocatePage( + eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( pDbPage->pBt==pBt ); ++ if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT; + + /* Move page iDbPage from its current location to page number iFreePage */ + TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", +@@ -56442,7 +67079,7 @@ SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){ + static int autoVacuumCommit(BtShared *pBt){ + int rc = SQLITE_OK; + Pager *pPager = pBt->pPager; +- VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) ); ++ VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); ) + + assert( sqlite3_mutex_held(pBt->mutex) ); + invalidateAllOverflowCache(pBt); +@@ -56684,7 +67321,6 @@ SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int wr + if( pBtree ){ + sqlite3BtreeEnter(pBtree); + for(p=pBtree->pBt->pCursor; p; p=p->pNext){ +- int i; + if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){ + if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){ + rc = saveCursorPosition(p); +@@ -56698,10 +67334,7 @@ SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int wr + p->eState = CURSOR_FAULT; + p->skipNext = errCode; + } +- for(i=0; i<=p->iPage; i++){ +- releasePage(p->apPage[i]); +- p->apPage[i] = 0; +- } ++ btreeReleaseAllCursorPages(p); + } + sqlite3BtreeLeave(pBtree); + } +@@ -56758,7 +67391,7 @@ SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){ + if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage); + testcase( pBt->nPage!=nPage ); + pBt->nPage = nPage; +- releasePage(pPage1); ++ releasePageOne(pPage1); + } + assert( countValidCursors(pBt, 1)==0 ); + pBt->inTransaction = TRANS_READ; +@@ -56826,7 +67459,12 @@ SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ + assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); + assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) ); + sqlite3BtreeEnter(p); +- rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); ++ if( op==SAVEPOINT_ROLLBACK ){ ++ rc = saveAllCursors(pBt, 0, 0); ++ } ++ if( rc==SQLITE_OK ){ ++ rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); ++ } + if( rc==SQLITE_OK ){ + if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){ + pBt->nPage = 0; +@@ -56851,13 +67489,13 @@ SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ + ** on the database already. If a write-cursor is requested, then + ** the caller is assumed to have an open write transaction. + ** +-** If wrFlag==0, then the cursor can only be used for reading. +-** If wrFlag==1, then the cursor can be used for reading or for +-** writing if other conditions for writing are also met. These +-** are the conditions that must be met in order for writing to +-** be allowed: ++** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only ++** be used for reading. If the BTREE_WRCSR bit is set, then the cursor ++** can be used for reading or for writing if other conditions for writing ++** are also met. These are the conditions that must be met in order ++** for writing to be allowed: + ** +-** 1: The cursor must have been opened with wrFlag==1 ++** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR + ** + ** 2: Other database connections that share the same pager cache + ** but which are not in the READ_UNCOMMITTED state may not have +@@ -56869,6 +67507,16 @@ SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ + ** + ** 4: There must be an active transaction. + ** ++** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR ++** is set. If FORDELETE is set, that is a hint to the implementation that ++** this cursor will only be used to seek to and delete entries of an index ++** as part of a larger DELETE statement. The FORDELETE hint is not used by ++** this implementation. But in a hypothetical alternative storage engine ++** in which index entries are automatically deleted when corresponding table ++** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE ++** operations on this cursor can be no-ops and all READ operations can ++** return a null row (2-bytes: 0x01 0x00). ++** + ** No checking is done to make sure that page iTable really is the + ** root page of a b-tree. If it is not, then the cursor acquired + ** will not work correctly. +@@ -56884,28 +67532,30 @@ static int btreeCursor( + BtCursor *pCur /* Space for new cursor */ + ){ + BtShared *pBt = p->pBt; /* Shared b-tree handle */ ++ BtCursor *pX; /* Looping over other all cursors */ + + assert( sqlite3BtreeHoldsMutex(p) ); +- assert( wrFlag==0 || wrFlag==1 ); ++ assert( wrFlag==0 ++ || wrFlag==BTREE_WRCSR ++ || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE) ++ ); + + /* The following assert statements verify that if this is a sharable + ** b-tree database, the connection is holding the required table locks, + ** and that no other connection has any open cursor that conflicts with + ** this lock. */ +- assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) ); ++ assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1)) ); + assert( wrFlag==0 || !hasReadConflicts(p, iTable) ); + + /* Assert that the caller has opened the required transaction. */ + assert( p->inTrans>TRANS_NONE ); + assert( wrFlag==0 || p->inTrans==TRANS_WRITE ); + assert( pBt->pPage1 && pBt->pPage1->aData ); ++ assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 ); + +- if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){ +- return SQLITE_READONLY; +- } + if( wrFlag ){ + allocateTempSpace(pBt); +- if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM; ++ if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT; + } + if( iTable==1 && btreePagecount(pBt)==0 ){ + assert( wrFlag==0 ); +@@ -56919,12 +67569,17 @@ static int btreeCursor( + pCur->pKeyInfo = pKeyInfo; + pCur->pBtree = p; + pCur->pBt = pBt; +- assert( wrFlag==0 || wrFlag==BTCF_WriteFlag ); +- pCur->curFlags = wrFlag; +- pCur->pNext = pBt->pCursor; +- if( pCur->pNext ){ +- pCur->pNext->pPrev = pCur; ++ pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0; ++ pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY; ++ /* If there are two or more cursors on the same btree, then all such ++ ** cursors *must* have the BTCF_Multiple flag set. */ ++ for(pX=pBt->pCursor; pX; pX=pX->pNext){ ++ if( pX->pgnoRoot==(Pgno)iTable ){ ++ pX->curFlags |= BTCF_Multiple; ++ pCur->curFlags |= BTCF_Multiple; ++ } + } ++ pCur->pNext = pBt->pCursor; + pBt->pCursor = pCur; + pCur->eState = CURSOR_INVALID; + return SQLITE_OK; +@@ -56937,9 +67592,13 @@ SQLITE_PRIVATE int sqlite3BtreeCursor( + BtCursor *pCur /* Write new cursor here */ + ){ + int rc; +- sqlite3BtreeEnter(p); +- rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); +- sqlite3BtreeLeave(p); ++ if( iTable<1 ){ ++ rc = SQLITE_CORRUPT_BKPT; ++ }else{ ++ sqlite3BtreeEnter(p); ++ rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); ++ sqlite3BtreeLeave(p); ++ } + return rc; + } + +@@ -56964,7 +67623,7 @@ SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){ + ** of run-time by skipping the initialization of those elements. + */ + SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){ +- memset(p, 0, offsetof(BtCursor, iPage)); ++ memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT)); + } + + /* +@@ -56974,25 +67633,27 @@ SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){ + SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){ + Btree *pBtree = pCur->pBtree; + if( pBtree ){ +- int i; + BtShared *pBt = pCur->pBt; + sqlite3BtreeEnter(pBtree); +- sqlite3BtreeClearCursor(pCur); +- if( pCur->pPrev ){ +- pCur->pPrev->pNext = pCur->pNext; +- }else{ ++ assert( pBt->pCursor!=0 ); ++ if( pBt->pCursor==pCur ){ + pBt->pCursor = pCur->pNext; ++ }else{ ++ BtCursor *pPrev = pBt->pCursor; ++ do{ ++ if( pPrev->pNext==pCur ){ ++ pPrev->pNext = pCur->pNext; ++ break; ++ } ++ pPrev = pPrev->pNext; ++ }while( ALWAYS(pPrev) ); + } +- if( pCur->pNext ){ +- pCur->pNext->pPrev = pCur->pPrev; +- } +- for(i=0; i<=pCur->iPage; i++){ +- releasePage(pCur->apPage[i]); +- } ++ btreeReleaseAllCursorPages(pCur); + unlockBtreeIfUnused(pBt); + sqlite3_free(pCur->aOverflow); +- /* sqlite3_free(pCur); */ ++ sqlite3_free(pCur->pKey); + sqlite3BtreeLeave(pBtree); ++ pCur->pBtree = 0; + } + return SQLITE_OK; + } +@@ -57004,47 +67665,33 @@ SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){ + ** + ** BtCursor.info is a cache of the information in the current cell. + ** Using this cache reduces the number of calls to btreeParseCell(). +-** +-** 2007-06-25: There is a bug in some versions of MSVC that cause the +-** compiler to crash when getCellInfo() is implemented as a macro. +-** But there is a measureable speed advantage to using the macro on gcc +-** (when less compiler optimizations like -Os or -O0 are used and the +-** compiler is not doing aggressive inlining.) So we use a real function +-** for MSVC and a macro for everything else. Ticket #2457. + */ + #ifndef NDEBUG ++ static int cellInfoEqual(CellInfo *a, CellInfo *b){ ++ if( a->nKey!=b->nKey ) return 0; ++ if( a->pPayload!=b->pPayload ) return 0; ++ if( a->nPayload!=b->nPayload ) return 0; ++ if( a->nLocal!=b->nLocal ) return 0; ++ if( a->nSize!=b->nSize ) return 0; ++ return 1; ++ } + static void assertCellInfo(BtCursor *pCur){ + CellInfo info; +- int iPage = pCur->iPage; + memset(&info, 0, sizeof(info)); +- btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info); +- assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 ); ++ btreeParseCell(pCur->pPage, pCur->ix, &info); ++ assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) ); + } + #else + #define assertCellInfo(x) + #endif +-#ifdef _MSC_VER +- /* Use a real function in MSVC to work around bugs in that compiler. */ +- static void getCellInfo(BtCursor *pCur){ +- if( pCur->info.nSize==0 ){ +- int iPage = pCur->iPage; +- btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); +- pCur->curFlags |= BTCF_ValidNKey; +- }else{ +- assertCellInfo(pCur); +- } +- } +-#else /* if not _MSC_VER */ +- /* Use a macro in all other compilers so that the function is inlined */ +-#define getCellInfo(pCur) \ +- if( pCur->info.nSize==0 ){ \ +- int iPage = pCur->iPage; \ +- btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \ +- pCur->curFlags |= BTCF_ValidNKey; \ +- }else{ \ +- assertCellInfo(pCur); \ ++static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){ ++ if( pCur->info.nSize==0 ){ ++ pCur->curFlags |= BTCF_ValidNKey; ++ btreeParseCell(pCur->pPage,pCur->ix,&pCur->info); ++ }else{ ++ assertCellInfo(pCur); + } +-#endif /* _MSC_VER */ ++} + + #ifndef NDEBUG /* The next routine used only within assert() statements */ + /* +@@ -57056,48 +67703,72 @@ SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){ + return pCur && pCur->eState==CURSOR_VALID; + } + #endif /* NDEBUG */ ++SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){ ++ assert( pCur!=0 ); ++ return pCur->eState==CURSOR_VALID; ++} + + /* +-** Set *pSize to the size of the buffer needed to hold the value of +-** the key for the current entry. If the cursor is not pointing +-** to a valid entry, *pSize is set to 0. +-** +-** For a table with the INTKEY flag set, this routine returns the key +-** itself, not the number of bytes in the key. +-** +-** The caller must position the cursor prior to invoking this routine. +-** +-** This routine cannot fail. It always returns SQLITE_OK. ++** Return the value of the integer key or "rowid" for a table btree. ++** This routine is only valid for a cursor that is pointing into a ++** ordinary table btree. If the cursor points to an index btree or ++** is invalid, the result of this routine is undefined. + */ +-SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ ++SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor *pCur){ + assert( cursorHoldsMutex(pCur) ); + assert( pCur->eState==CURSOR_VALID ); ++ assert( pCur->curIntKey ); + getCellInfo(pCur); +- *pSize = pCur->info.nKey; +- return SQLITE_OK; ++ return pCur->info.nKey; ++} ++ ++#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC ++/* ++** Return the offset into the database file for the start of the ++** payload to which the cursor is pointing. ++*/ ++SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor *pCur){ ++ assert( cursorHoldsMutex(pCur) ); ++ assert( pCur->eState==CURSOR_VALID ); ++ getCellInfo(pCur); ++ return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) + ++ (i64)(pCur->info.pPayload - pCur->pPage->aData); + } ++#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */ + + /* +-** Set *pSize to the number of bytes of data in the entry the +-** cursor currently points to. ++** Return the number of bytes of payload for the entry that pCur is ++** currently pointing to. For table btrees, this will be the amount ++** of data. For index btrees, this will be the size of the key. + ** + ** The caller must guarantee that the cursor is pointing to a non-NULL + ** valid entry. In other words, the calling procedure must guarantee + ** that the cursor has Cursor.eState==CURSOR_VALID. +-** +-** Failure is not possible. This function always returns SQLITE_OK. +-** It might just as well be a procedure (returning void) but we continue +-** to return an integer result code for historical reasons. + */ +-SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ ++SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor *pCur){ + assert( cursorHoldsMutex(pCur) ); + assert( pCur->eState==CURSOR_VALID ); +- assert( pCur->iPage>=0 ); +- assert( pCur->iPage apPage[pCur->iPage]->intKeyLeaf==1 ); + getCellInfo(pCur); +- *pSize = pCur->info.nPayload; +- return SQLITE_OK; ++ return pCur->info.nPayload; ++} ++ ++/* ++** Return an upper bound on the size of any record for the table ++** that the cursor is pointing into. ++** ++** This is an optimization. Everything will still work if this ++** routine always returns 2147483647 (which is the largest record ++** that SQLite can handle) or more. But returning a smaller value might ++** prevent large memory allocations when trying to interpret a ++** corrupt datrabase. ++** ++** The current implementation merely returns the size of the underlying ++** database file. ++*/ ++SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){ ++ assert( cursorHoldsMutex(pCur) ); ++ assert( pCur->eState==CURSOR_VALID ); ++ return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage; + } + + /* +@@ -57215,7 +67886,6 @@ static int copyPayload( + ** + ** 0: The operation is a read. Populate the overflow cache. + ** 1: The operation is a write. Populate the overflow cache. +-** 2: The operation is a read. Do not populate the overflow cache. + ** + ** A total of "amt" bytes are read or written beginning at "offset". + ** Data is read to or from the buffer pBuf. +@@ -57223,13 +67893,13 @@ static int copyPayload( + ** The content being read or written might appear on the main page + ** or be scattered out on multiple overflow pages. + ** +-** If the current cursor entry uses one or more overflow pages and the +-** eOp argument is not 2, this function may allocate space for and lazily +-** populates the overflow page-list cache array (BtCursor.aOverflow). ++** If the current cursor entry uses one or more overflow pages ++** this function may allocate space for and lazily populate ++** the overflow page-list cache array (BtCursor.aOverflow). + ** Subsequent calls use this cache to make seeking to the supplied offset + ** more efficient. + ** +-** Once an overflow page-list cache has been allocated, it may be ++** Once an overflow page-list cache has been allocated, it must be + ** invalidated if some other cursor writes to the same table, or if + ** the cursor is moved to a different row. Additionally, in auto-vacuum + ** mode, the following events may invalidate an overflow page-list cache. +@@ -57248,29 +67918,30 @@ static int accessPayload( + unsigned char *aPayload; + int rc = SQLITE_OK; + int iIdx = 0; +- MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */ ++ MemPage *pPage = pCur->pPage; /* Btree page of current entry */ + BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ + #ifdef SQLITE_DIRECT_OVERFLOW_READ +- unsigned char * const pBufStart = pBuf; +- int bEnd; /* True if reading to end of data */ ++ unsigned char * const pBufStart = pBuf; /* Start of original out buffer */ + #endif + + assert( pPage ); ++ assert( eOp==0 || eOp==1 ); + assert( pCur->eState==CURSOR_VALID ); +- assert( pCur->aiIdx[pCur->iPage] nCell ); ++ assert( pCur->ix nCell ); + assert( cursorHoldsMutex(pCur) ); +- assert( eOp!=2 || offset==0 ); /* Always start from beginning for eOp==2 */ + + getCellInfo(pCur); + aPayload = pCur->info.pPayload; +-#ifdef SQLITE_DIRECT_OVERFLOW_READ +- bEnd = offset+amt==pCur->info.nPayload; +-#endif + assert( offset+amt <= pCur->info.nPayload ); + +- if( &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ){ +- /* Trying to read or write past the end of the data is an error */ +- return SQLITE_CORRUPT_BKPT; ++ assert( aPayload > pPage->aData ); ++ if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){ ++ /* Trying to read or write past the end of the data is an error. The ++ ** conditional above is really: ++ ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ++ ** but is recast into its current form to avoid integer overflow problems ++ */ ++ return SQLITE_CORRUPT_PAGE(pPage); + } + + /* Check if data must be read/written to/from the btree page itself. */ +@@ -57279,7 +67950,7 @@ static int accessPayload( + if( a+offset>pCur->info.nLocal ){ + a = pCur->info.nLocal - offset; + } +- rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage); ++ rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); + offset = 0; + pBuf += a; + amt -= a; +@@ -57295,51 +67966,47 @@ static int accessPayload( + nextPage = get4byte(&aPayload[pCur->info.nLocal]); + + /* If the BtCursor.aOverflow[] has not been allocated, allocate it now. +- ** Except, do not allocate aOverflow[] for eOp==2. + ** + ** The aOverflow[] array is sized at one entry for each overflow page + ** in the overflow chain. The page number of the first overflow page is + ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array + ** means "not yet known" (the cache is lazily populated). + */ +- if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){ ++ if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){ + int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; +- if( nOvfl>pCur->nOvflAlloc ){ ++ if( pCur->aOverflow==0 ++ || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow) ++ ){ + Pgno *aNew = (Pgno*)sqlite3Realloc( + pCur->aOverflow, nOvfl*2*sizeof(Pgno) + ); + if( aNew==0 ){ +- rc = SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + }else{ +- pCur->nOvflAlloc = nOvfl*2; + pCur->aOverflow = aNew; + } + } +- if( rc==SQLITE_OK ){ +- memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno)); +- pCur->curFlags |= BTCF_ValidOvfl; ++ memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno)); ++ pCur->curFlags |= BTCF_ValidOvfl; ++ }else{ ++ /* If the overflow page-list cache has been allocated and the ++ ** entry for the first required overflow page is valid, skip ++ ** directly to it. ++ */ ++ if( pCur->aOverflow[offset/ovflSize] ){ ++ iIdx = (offset/ovflSize); ++ nextPage = pCur->aOverflow[iIdx]; ++ offset = (offset%ovflSize); + } + } + +- /* If the overflow page-list cache has been allocated and the +- ** entry for the first required overflow page is valid, skip +- ** directly to it. +- */ +- if( (pCur->curFlags & BTCF_ValidOvfl)!=0 +- && pCur->aOverflow[offset/ovflSize] +- ){ +- iIdx = (offset/ovflSize); +- nextPage = pCur->aOverflow[iIdx]; +- offset = (offset%ovflSize); +- } +- +- for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ +- ++ assert( rc==SQLITE_OK && amt>0 ); ++ while( nextPage ){ + /* If required, populate the overflow page-list cache. */ +- if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){ +- assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); +- pCur->aOverflow[iIdx] = nextPage; +- } ++ assert( pCur->aOverflow[iIdx]==0 ++ || pCur->aOverflow[iIdx]==nextPage ++ || CORRUPT_DB ); ++ pCur->aOverflow[iIdx] = nextPage; + + if( offset>=ovflSize ){ + /* The only reason to read this page is to obtain the page +@@ -57347,11 +68014,7 @@ static int accessPayload( + ** data is not required. So first try to lookup the overflow + ** page-list cache, if any, then fall back to the getOverflowPage() + ** function. +- ** +- ** Note that the aOverflow[] array must be allocated because eOp!=2 +- ** here. If eOp==2, then offset==0 and this branch is never taken. + */ +- assert( eOp!=2 ); + assert( pCur->curFlags & BTCF_ValidOvfl ); + assert( pCur->pBtree->db==pBt->db ); + if( pCur->aOverflow[iIdx+1] ){ +@@ -57364,9 +68027,6 @@ static int accessPayload( + /* Need to read this page properly. It contains some of the + ** range of data that is being read (eOp==0) or written (eOp!=0). + */ +-#ifdef SQLITE_DIRECT_OVERFLOW_READ +- sqlite3_file *fd; +-#endif + int a = amt; + if( a + offset > ovflSize ){ + a = ovflSize - offset; +@@ -57377,27 +68037,24 @@ static int accessPayload( + ** + ** 1) this is a read operation, and + ** 2) data is required from the start of this overflow page, and +- ** 3) the database is file-backed, and +- ** 4) there is no open write-transaction, and +- ** 5) the database is not a WAL database, +- ** 6) all data from the page is being read. +- ** 7) at least 4 bytes have already been read into the output buffer ++ ** 3) there are no dirty pages in the page-cache ++ ** 4) the database is file-backed, and ++ ** 5) the page is not in the WAL file ++ ** 6) at least 4 bytes have already been read into the output buffer + ** + ** then data can be read directly from the database file into the + ** output buffer, bypassing the page-cache altogether. This speeds + ** up loading large records that span many overflow pages. + */ +- if( (eOp&0x01)==0 /* (1) */ ++ if( eOp==0 /* (1) */ + && offset==0 /* (2) */ +- && (bEnd || a==ovflSize) /* (6) */ +- && pBt->inTransaction==TRANS_READ /* (4) */ +- && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */ +- && pBt->pPage1->aData[19]==0x01 /* (5) */ +- && &pBuf[-4]>=pBufStart /* (7) */ ++ && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */ ++ && &pBuf[-4]>=pBufStart /* (6) */ + ){ ++ sqlite3_file *fd = sqlite3PagerFile(pBt->pPager); + u8 aSave[4]; + u8 *aWrite = &pBuf[-4]; +- assert( aWrite>=pBufStart ); /* hence (7) */ ++ assert( aWrite>=pBufStart ); /* due to (6) */ + memcpy(aSave, aWrite, 4); + rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1)); + nextPage = get4byte(aWrite); +@@ -57407,77 +68064,87 @@ static int accessPayload( + + { + DbPage *pDbPage; +- rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage, +- ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0) ++ rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage, ++ (eOp==0 ? PAGER_GET_READONLY : 0) + ); + if( rc==SQLITE_OK ){ + aPayload = sqlite3PagerGetData(pDbPage); + nextPage = get4byte(aPayload); +- rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage); ++ rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); + sqlite3PagerUnref(pDbPage); + offset = 0; + } + } + amt -= a; ++ if( amt==0 ) return rc; + pBuf += a; + } ++ if( rc ) break; ++ iIdx++; + } + } + + if( rc==SQLITE_OK && amt>0 ){ +- return SQLITE_CORRUPT_BKPT; ++ /* Overflow chain ends prematurely */ ++ return SQLITE_CORRUPT_PAGE(pPage); + } + return rc; + } + + /* +-** Read part of the key associated with cursor pCur. Exactly +-** "amt" bytes will be transferred into pBuf[]. The transfer ++** Read part of the payload for the row at which that cursor pCur is currently ++** pointing. "amt" bytes will be transferred into pBuf[]. The transfer + ** begins at "offset". + ** +-** The caller must ensure that pCur is pointing to a valid row +-** in the table. ++** pCur can be pointing to either a table or an index b-tree. ++** If pointing to a table btree, then the content section is read. If ++** pCur is pointing to an index b-tree then the key section is read. ++** ++** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing ++** to a valid row in the table. For sqlite3BtreePayloadChecked(), the ++** cursor might be invalid or might need to be restored before being read. + ** + ** Return SQLITE_OK on success or an error code if anything goes + ** wrong. An error is returned if "offset+amt" is larger than + ** the available payload. + */ +-SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ ++SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ + assert( cursorHoldsMutex(pCur) ); + assert( pCur->eState==CURSOR_VALID ); +- assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); +- assert( pCur->aiIdx[pCur->iPage] apPage[pCur->iPage]->nCell ); ++ assert( pCur->iPage>=0 && pCur->pPage ); ++ assert( pCur->ix pPage->nCell ); + return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); + } + + /* +-** Read part of the data associated with cursor pCur. Exactly +-** "amt" bytes will be transfered into pBuf[]. The transfer +-** begins at "offset". +-** +-** Return SQLITE_OK on success or an error code if anything goes +-** wrong. An error is returned if "offset+amt" is larger than +-** the available payload. ++** This variant of sqlite3BtreePayload() works even if the cursor has not ++** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read() ++** interface. + */ +-SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ +- int rc; +- + #ifndef SQLITE_OMIT_INCRBLOB ++static SQLITE_NOINLINE int accessPayloadChecked( ++ BtCursor *pCur, ++ u32 offset, ++ u32 amt, ++ void *pBuf ++){ ++ int rc; + if ( pCur->eState==CURSOR_INVALID ){ + return SQLITE_ABORT; + } +-#endif +- +- assert( cursorHoldsMutex(pCur) ); +- rc = restoreCursorPosition(pCur); +- if( rc==SQLITE_OK ){ +- assert( pCur->eState==CURSOR_VALID ); +- assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); +- assert( pCur->aiIdx[pCur->iPage] apPage[pCur->iPage]->nCell ); +- rc = accessPayload(pCur, offset, amt, pBuf, 0); ++ assert( cursorOwnsBtShared(pCur) ); ++ rc = btreeRestoreCursorPosition(pCur); ++ return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0); ++} ++SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ ++ if( pCur->eState==CURSOR_VALID ){ ++ assert( cursorOwnsBtShared(pCur) ); ++ return accessPayload(pCur, offset, amt, pBuf, 0); ++ }else{ ++ return accessPayloadChecked(pCur, offset, amt, pBuf); + } +- return rc; + } ++#endif /* SQLITE_OMIT_INCRBLOB */ + + /* + ** Return a pointer to payload information from the entry that the +@@ -57502,18 +68169,23 @@ static const void *fetchPayload( + BtCursor *pCur, /* Cursor pointing to entry to read from */ + u32 *pAmt /* Write the number of available bytes here */ + ){ +- u32 amt; +- assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]); ++ int amt; ++ assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage); + assert( pCur->eState==CURSOR_VALID ); + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); +- assert( cursorHoldsMutex(pCur) ); +- assert( pCur->aiIdx[pCur->iPage] apPage[pCur->iPage]->nCell ); ++ assert( cursorOwnsBtShared(pCur) ); ++ assert( pCur->ix pPage->nCell ); + assert( pCur->info.nSize>0 ); +- assert( pCur->info.pPayload>pCur->apPage[pCur->iPage]->aData || CORRUPT_DB ); +- assert( pCur->info.pPayload apPage[pCur->iPage]->aDataEnd ||CORRUPT_DB); +- amt = (int)(pCur->apPage[pCur->iPage]->aDataEnd - pCur->info.pPayload); +- if( pCur->info.nLocal info.nLocal; +- *pAmt = amt; ++ assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB ); ++ assert( pCur->info.pPayload pPage->aDataEnd ||CORRUPT_DB); ++ amt = pCur->info.nLocal; ++ if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){ ++ /* There is too little space on the page for the expected amount ++ ** of local content. Database must be corrupt. */ ++ assert( CORRUPT_DB ); ++ amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload)); ++ } ++ *pAmt = (u32)amt; + return (void*)pCur->info.pPayload; + } + +@@ -57532,10 +68204,7 @@ static const void *fetchPayload( + ** These routines is used to get quick access to key and data + ** in the common case where no overflow pages are used. + */ +-SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){ +- return fetchPayload(pCur, pAmt); +-} +-SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){ ++SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){ + return fetchPayload(pCur, pAmt); + } + +@@ -57550,34 +68219,25 @@ SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){ + ** vice-versa). + */ + static int moveToChild(BtCursor *pCur, u32 newPgno){ +- int rc; +- int i = pCur->iPage; +- MemPage *pNewPage; + BtShared *pBt = pCur->pBt; + +- assert( cursorHoldsMutex(pCur) ); ++ assert( cursorOwnsBtShared(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + assert( pCur->iPage iPage>=0 ); + if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){ + return SQLITE_CORRUPT_BKPT; + } +- rc = getAndInitPage(pBt, newPgno, &pNewPage, +- (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0); +- if( rc ) return rc; +- pCur->apPage[i+1] = pNewPage; +- pCur->aiIdx[i+1] = 0; +- pCur->iPage++; +- + pCur->info.nSize = 0; + pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); +- if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){ +- return SQLITE_CORRUPT_BKPT; +- } +- return SQLITE_OK; ++ pCur->aiIdx[pCur->iPage] = pCur->ix; ++ pCur->apPage[pCur->iPage] = pCur->pPage; ++ pCur->ix = 0; ++ pCur->iPage++; ++ return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags); + } + +-#if SQLITE_DEBUG ++#ifdef SQLITE_DEBUG + /* + ** Page pParent is an internal (non-leaf) tree page. This function + ** asserts that page number iChild is the left-child if the iIdx'th +@@ -57608,21 +68268,23 @@ static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){ + ** the largest cell index. + */ + static void moveToParent(BtCursor *pCur){ +- assert( cursorHoldsMutex(pCur) ); ++ MemPage *pLeaf; ++ assert( cursorOwnsBtShared(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + assert( pCur->iPage>0 ); +- assert( pCur->apPage[pCur->iPage] ); ++ assert( pCur->pPage ); + assertParentIndex( + pCur->apPage[pCur->iPage-1], + pCur->aiIdx[pCur->iPage-1], +- pCur->apPage[pCur->iPage]->pgno ++ pCur->pPage->pgno + ); + testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell ); +- +- releasePage(pCur->apPage[pCur->iPage]); +- pCur->iPage--; + pCur->info.nSize = 0; + pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); ++ pCur->ix = pCur->aiIdx[pCur->iPage-1]; ++ pLeaf = pCur->pPage; ++ pCur->pPage = pCur->apPage[--pCur->iPage]; ++ releasePageNotNull(pLeaf); + } + + /* +@@ -57634,9 +68296,9 @@ static void moveToParent(BtCursor *pCur){ + ** single child page. This can only happen with the table rooted at page 1. + ** + ** If the b-tree structure is empty, the cursor state is set to +-** CURSOR_INVALID. Otherwise, the cursor is set to point to the first +-** cell located on the root (or virtual root) page and the cursor state +-** is set to CURSOR_VALID. ++** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise, ++** the cursor is set to point to the first cell located on the root ++** (or virtual root) page and the cursor state is set to CURSOR_VALID. + ** + ** If this function returns successfully, it may be assumed that the + ** page-header flags indicate that the [virtual] root-page is the expected +@@ -57650,33 +68312,44 @@ static int moveToRoot(BtCursor *pCur){ + MemPage *pRoot; + int rc = SQLITE_OK; + +- assert( cursorHoldsMutex(pCur) ); ++ assert( cursorOwnsBtShared(pCur) ); + assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); + assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); + assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); +- if( pCur->eState>=CURSOR_REQUIRESEEK ){ +- if( pCur->eState==CURSOR_FAULT ){ +- assert( pCur->skipNext!=SQLITE_OK ); +- return pCur->skipNext; +- } +- sqlite3BtreeClearCursor(pCur); +- } ++ assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 ); ++ assert( pCur->pgnoRoot>0 || pCur->iPage<0 ); + + if( pCur->iPage>=0 ){ +- while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]); ++ if( pCur->iPage ){ ++ releasePageNotNull(pCur->pPage); ++ while( --pCur->iPage ){ ++ releasePageNotNull(pCur->apPage[pCur->iPage]); ++ } ++ pCur->pPage = pCur->apPage[0]; ++ goto skip_init; ++ } + }else if( pCur->pgnoRoot==0 ){ + pCur->eState = CURSOR_INVALID; +- return SQLITE_OK; ++ return SQLITE_EMPTY; + }else{ +- rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0], +- (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0); ++ assert( pCur->iPage==(-1) ); ++ if( pCur->eState>=CURSOR_REQUIRESEEK ){ ++ if( pCur->eState==CURSOR_FAULT ){ ++ assert( pCur->skipNext!=SQLITE_OK ); ++ return pCur->skipNext; ++ } ++ sqlite3BtreeClearCursor(pCur); ++ } ++ rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage, ++ 0, pCur->curPagerFlags); + if( rc!=SQLITE_OK ){ + pCur->eState = CURSOR_INVALID; + return rc; + } + pCur->iPage = 0; ++ pCur->curIntKey = pCur->pPage->intKey; + } +- pRoot = pCur->apPage[0]; ++ pRoot = pCur->pPage; + assert( pRoot->pgno==pCur->pgnoRoot ); + + /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor +@@ -57691,13 +68364,15 @@ static int moveToRoot(BtCursor *pCur){ + ** (or the freelist). */ + assert( pRoot->intKey==1 || pRoot->intKey==0 ); + if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){ +- return SQLITE_CORRUPT_BKPT; ++ return SQLITE_CORRUPT_PAGE(pCur->pPage); + } + +- pCur->aiIdx[0] = 0; ++skip_init: ++ pCur->ix = 0; + pCur->info.nSize = 0; + pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl); + ++ pRoot = pCur->pPage; + if( pRoot->nCell>0 ){ + pCur->eState = CURSOR_VALID; + }else if( !pRoot->leaf ){ +@@ -57708,6 +68383,7 @@ static int moveToRoot(BtCursor *pCur){ + rc = moveToChild(pCur, subpage); + }else{ + pCur->eState = CURSOR_INVALID; ++ rc = SQLITE_EMPTY; + } + return rc; + } +@@ -57724,11 +68400,11 @@ static int moveToLeftmost(BtCursor *pCur){ + int rc = SQLITE_OK; + MemPage *pPage; + +- assert( cursorHoldsMutex(pCur) ); ++ assert( cursorOwnsBtShared(pCur) ); + assert( pCur->eState==CURSOR_VALID ); +- while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ +- assert( pCur->aiIdx[pCur->iPage] nCell ); +- pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage])); ++ while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ ++ assert( pCur->ix nCell ); ++ pgno = get4byte(findCell(pPage, pCur->ix)); + rc = moveToChild(pCur, pgno); + } + return rc; +@@ -57749,15 +68425,15 @@ static int moveToRightmost(BtCursor *pCur){ + int rc = SQLITE_OK; + MemPage *pPage = 0; + +- assert( cursorHoldsMutex(pCur) ); ++ assert( cursorOwnsBtShared(pCur) ); + assert( pCur->eState==CURSOR_VALID ); +- while( !(pPage = pCur->apPage[pCur->iPage])->leaf ){ ++ while( !(pPage = pCur->pPage)->leaf ){ + pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); +- pCur->aiIdx[pCur->iPage] = pPage->nCell; ++ pCur->ix = pPage->nCell; + rc = moveToChild(pCur, pgno); + if( rc ) return rc; + } +- pCur->aiIdx[pCur->iPage] = pPage->nCell-1; ++ pCur->ix = pPage->nCell-1; + assert( pCur->info.nSize==0 ); + assert( (pCur->curFlags & BTCF_ValidNKey)==0 ); + return SQLITE_OK; +@@ -57770,18 +68446,17 @@ static int moveToRightmost(BtCursor *pCur){ + SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ + int rc; + +- assert( cursorHoldsMutex(pCur) ); ++ assert( cursorOwnsBtShared(pCur) ); + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + rc = moveToRoot(pCur); + if( rc==SQLITE_OK ){ +- if( pCur->eState==CURSOR_INVALID ){ +- assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); +- *pRes = 1; +- }else{ +- assert( pCur->apPage[pCur->iPage]->nCell>0 ); +- *pRes = 0; +- rc = moveToLeftmost(pCur); +- } ++ assert( pCur->pPage->nCell>0 ); ++ *pRes = 0; ++ rc = moveToLeftmost(pCur); ++ }else if( rc==SQLITE_EMPTY ){ ++ assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 ); ++ *pRes = 1; ++ rc = SQLITE_OK; + } + return rc; + } +@@ -57793,7 +68468,7 @@ SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ + SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ + int rc; + +- assert( cursorHoldsMutex(pCur) ); ++ assert( cursorOwnsBtShared(pCur) ); + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + + /* If the cursor already points to the last entry, this is a no-op. */ +@@ -57805,28 +68480,26 @@ SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ + for(ii=0; ii iPage; ii++){ + assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell ); + } +- assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 ); +- assert( pCur->apPage[pCur->iPage]->leaf ); ++ assert( pCur->ix==pCur->pPage->nCell-1 ); ++ assert( pCur->pPage->leaf ); + #endif + return SQLITE_OK; + } + + rc = moveToRoot(pCur); + if( rc==SQLITE_OK ){ +- if( CURSOR_INVALID==pCur->eState ){ +- assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); +- *pRes = 1; ++ assert( pCur->eState==CURSOR_VALID ); ++ *pRes = 0; ++ rc = moveToRightmost(pCur); ++ if( rc==SQLITE_OK ){ ++ pCur->curFlags |= BTCF_AtLast; + }else{ +- assert( pCur->eState==CURSOR_VALID ); +- *pRes = 0; +- rc = moveToRightmost(pCur); +- if( rc==SQLITE_OK ){ +- pCur->curFlags |= BTCF_AtLast; +- }else{ +- pCur->curFlags &= ~BTCF_AtLast; +- } +- ++ pCur->curFlags &= ~BTCF_AtLast; + } ++ }else if( rc==SQLITE_EMPTY ){ ++ assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 ); ++ *pRes = 1; ++ rc = SQLITE_OK; + } + return rc; + } +@@ -57858,6 +68531,8 @@ SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ + ** *pRes>0 The cursor is left pointing at an entry that + ** is larger than intKey/pIdxKey. + ** ++** For index tables, the pIdxKey->eqSeen field is set to 1 if there ++** exists an entry in the table that exactly matches pIdxKey. + */ + SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked( + BtCursor *pCur, /* The cursor to be moved */ +@@ -57869,23 +68544,44 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked( + int rc; + RecordCompare xRecordCompare; + +- assert( cursorHoldsMutex(pCur) ); ++ assert( cursorOwnsBtShared(pCur) ); + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + assert( pRes ); + assert( (pIdxKey==0)==(pCur->pKeyInfo==0) ); ++ assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) ); + + /* If the cursor is already positioned at the point we are trying + ** to move to, then just return without doing any work */ +- if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 +- && pCur->apPage[0]->intKey ++ if( pIdxKey==0 ++ && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 + ){ + if( pCur->info.nKey==intKey ){ + *pRes = 0; + return SQLITE_OK; + } +- if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey info.nKey curFlags & BTCF_AtLast)!=0 ){ ++ *pRes = -1; ++ return SQLITE_OK; ++ } ++ /* If the requested key is one more than the previous key, then ++ ** try to get there using sqlite3BtreeNext() rather than a full ++ ** binary search. This is an optimization only. The correct answer ++ ** is still obtained without this case, only a little more slowely */ ++ if( pCur->info.nKey+1==intKey ){ ++ *pRes = 0; ++ rc = sqlite3BtreeNext(pCur, 0); ++ if( rc==SQLITE_OK ){ ++ getCellInfo(pCur); ++ if( pCur->info.nKey==intKey ){ ++ return SQLITE_OK; ++ } ++ }else if( rc==SQLITE_DONE ){ ++ rc = SQLITE_OK; ++ }else{ ++ return rc; ++ } ++ } + } + } + +@@ -57902,21 +68598,23 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked( + + rc = moveToRoot(pCur); + if( rc ){ ++ if( rc==SQLITE_EMPTY ){ ++ assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 ); ++ *pRes = -1; ++ return SQLITE_OK; ++ } + return rc; + } +- assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] ); +- assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit ); +- assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 ); +- if( pCur->eState==CURSOR_INVALID ){ +- *pRes = -1; +- assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); +- return SQLITE_OK; +- } +- assert( pCur->apPage[0]->intKey || pIdxKey ); ++ assert( pCur->pPage ); ++ assert( pCur->pPage->isInit ); ++ assert( pCur->eState==CURSOR_VALID ); ++ assert( pCur->pPage->nCell > 0 ); ++ assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey ); ++ assert( pCur->curIntKey || pIdxKey ); + for(;;){ + int lwr, upr, idx, c; + Pgno chldPg; +- MemPage *pPage = pCur->apPage[pCur->iPage]; ++ MemPage *pPage = pCur->pPage; + u8 *pCell; /* Pointer to current cell in pPage */ + + /* pPage->nCell must be greater than zero. If this is the root-page +@@ -57931,14 +68629,16 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked( + upr = pPage->nCell-1; + assert( biasRight==0 || biasRight==1 ); + idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */ +- pCur->aiIdx[pCur->iPage] = (u16)idx; ++ pCur->ix = (u16)idx; + if( xRecordCompare==0 ){ + for(;;){ + i64 nCellKey; +- pCell = findCell(pPage, idx) + pPage->childPtrSize; ++ pCell = findCellPastPtr(pPage, idx); + if( pPage->intKeyLeaf ){ + while( 0x80 <= *(pCell++) ){ +- if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT; ++ if( pCell>=pPage->aDataEnd ){ ++ return SQLITE_CORRUPT_PAGE(pPage); ++ } + } + } + getVarint(pCell, (u64*)&nCellKey); +@@ -57950,16 +68650,16 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked( + if( lwr>upr ){ c = +1; break; } + }else{ + assert( nCellKey==intKey ); +- pCur->curFlags |= BTCF_ValidNKey; +- pCur->info.nKey = nCellKey; +- pCur->aiIdx[pCur->iPage] = (u16)idx; ++ pCur->ix = (u16)idx; + if( !pPage->leaf ){ + lwr = idx; + goto moveto_next_layer; + }else{ ++ pCur->curFlags |= BTCF_ValidNKey; ++ pCur->info.nKey = nCellKey; ++ pCur->info.nSize = 0; + *pRes = 0; +- rc = SQLITE_OK; +- goto moveto_finish; ++ return SQLITE_OK; + } + } + assert( lwr+upr>=0 ); +@@ -57967,8 +68667,8 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked( + } + }else{ + for(;;){ +- int nCell; +- pCell = findCell(pPage, idx) + pPage->childPtrSize; ++ int nCell; /* Size of the pCell cell in bytes */ ++ pCell = findCellPastPtr(pPage, idx); + + /* The maximum supported page-size is 65536 bytes. This means that + ** the maximum number of record bytes stored on an index B-Tree +@@ -57996,23 +68696,37 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked( + /* The record flows over onto one or more overflow pages. In + ** this case the whole cell needs to be parsed, a buffer allocated + ** and accessPayload() used to retrieve the record into the +- ** buffer before VdbeRecordCompare() can be called. */ ++ ** buffer before VdbeRecordCompare() can be called. ++ ** ++ ** If the record is corrupt, the xRecordCompare routine may read ++ ** up to two varints past the end of the buffer. An extra 18 ++ ** bytes of padding is allocated at the end of the buffer in ++ ** case this happens. */ + void *pCellKey; + u8 * const pCellBody = pCell - pPage->childPtrSize; +- btreeParseCellPtr(pPage, pCellBody, &pCur->info); ++ pPage->xParseCell(pPage, pCellBody, &pCur->info); + nCell = (int)pCur->info.nKey; +- pCellKey = sqlite3Malloc( nCell ); ++ testcase( nCell<0 ); /* True if key size is 2^32 or more */ ++ testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */ ++ testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */ ++ testcase( nCell==2 ); /* Minimum legal index key size */ ++ if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){ ++ rc = SQLITE_CORRUPT_PAGE(pPage); ++ goto moveto_finish; ++ } ++ pCellKey = sqlite3Malloc( nCell+18 ); + if( pCellKey==0 ){ +- rc = SQLITE_NOMEM; ++ rc = SQLITE_NOMEM_BKPT; + goto moveto_finish; + } +- pCur->aiIdx[pCur->iPage] = (u16)idx; +- rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2); ++ pCur->ix = (u16)idx; ++ rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0); ++ pCur->curFlags &= ~BTCF_ValidOvfl; + if( rc ){ + sqlite3_free(pCellKey); + goto moveto_finish; + } +- c = xRecordCompare(nCell, pCellKey, pIdxKey); ++ c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey); + sqlite3_free(pCellKey); + } + assert( +@@ -58027,8 +68741,8 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked( + assert( c==0 ); + *pRes = 0; + rc = SQLITE_OK; +- pCur->aiIdx[pCur->iPage] = (u16)idx; +- if( pIdxKey->errCode ) rc = SQLITE_CORRUPT; ++ pCur->ix = (u16)idx; ++ if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT; + goto moveto_finish; + } + if( lwr>upr ) break; +@@ -58039,8 +68753,8 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked( + assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) ); + assert( pPage->isInit ); + if( pPage->leaf ){ +- assert( pCur->aiIdx[pCur->iPage] apPage[pCur->iPage]->nCell ); +- pCur->aiIdx[pCur->iPage] = (u16)idx; ++ assert( pCur->ix pPage->nCell ); ++ pCur->ix = (u16)idx; + *pRes = c; + rc = SQLITE_OK; + goto moveto_finish; +@@ -58051,13 +68765,13 @@ moveto_next_layer: + }else{ + chldPg = get4byte(findCell(pPage, lwr)); + } +- pCur->aiIdx[pCur->iPage] = (u16)lwr; ++ pCur->ix = (u16)lwr; + rc = moveToChild(pCur, chldPg); + if( rc ) break; + } + moveto_finish: + pCur->info.nSize = 0; +- pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); ++ assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); + return rc; + } + +@@ -58078,10 +68792,37 @@ SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){ + } + + /* +-** Advance the cursor to the next entry in the database. If +-** successful then set *pRes=0. If the cursor +-** was already pointing to the last entry in the database before +-** this routine was called, then set *pRes=1. ++** Return an estimate for the number of rows in the table that pCur is ++** pointing to. Return a negative number if no estimate is currently ++** available. ++*/ ++SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor *pCur){ ++ i64 n; ++ u8 i; ++ ++ assert( cursorOwnsBtShared(pCur) ); ++ assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); ++ ++ /* Currently this interface is only called by the OP_IfSmaller ++ ** opcode, and it that case the cursor will always be valid and ++ ** will always point to a leaf node. */ ++ if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1; ++ if( NEVER(pCur->pPage->leaf==0) ) return -1; ++ ++ n = pCur->pPage->nCell; ++ for(i=0; i iPage; i++){ ++ n *= pCur->apPage[i]->nCell; ++ } ++ return n; ++} ++ ++/* ++** Advance the cursor to the next entry in the database. ++** Return value: ++** ++** SQLITE_OK success ++** SQLITE_DONE cursor is already pointing at the last element ++** otherwise some kind of error occurred + ** + ** The main entry point is sqlite3BtreeNext(). That routine is optimized + ** for the common case of merely incrementing the cell counter BtCursor.aiIdx +@@ -58089,23 +68830,18 @@ SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){ + ** routine is called when it is necessary to move to a different page or + ** to restore the cursor. + ** +-** The calling function will set *pRes to 0 or 1. The initial *pRes value +-** will be 1 if the cursor being stepped corresponds to an SQL index and +-** if this routine could have been skipped if that SQL index had been +-** a unique index. Otherwise the caller will have set *pRes to zero. +-** Zero is the common case. The btree implementation is free to use the +-** initial *pRes value as a hint to improve performance, but the current +-** SQLite btree implementation does not. (Note that the comdb2 btree +-** implementation does use this hint, however.) ++** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the ++** cursor corresponds to an SQL index and this routine could have been ++** skipped if the SQL index had been a unique index. The F argument ++** is a hint to the implement. SQLite btree implementation does not use ++** this hint, but COMDB2 does. + */ +-static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){ ++static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){ + int rc; + int idx; + MemPage *pPage; + +- assert( cursorHoldsMutex(pCur) ); +- assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); +- assert( *pRes==0 ); ++ assert( cursorOwnsBtShared(pCur) ); + if( pCur->eState!=CURSOR_VALID ){ + assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); + rc = restoreCursorPosition(pCur); +@@ -58113,23 +68849,26 @@ static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){ + return rc; + } + if( CURSOR_INVALID==pCur->eState ){ +- *pRes = 1; +- return SQLITE_OK; ++ return SQLITE_DONE; + } +- if( pCur->skipNext ){ +- assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT ); ++ if( pCur->eState==CURSOR_SKIPNEXT ){ + pCur->eState = CURSOR_VALID; +- if( pCur->skipNext>0 ){ +- pCur->skipNext = 0; +- return SQLITE_OK; +- } +- pCur->skipNext = 0; ++ if( pCur->skipNext>0 ) return SQLITE_OK; + } + } + +- pPage = pCur->apPage[pCur->iPage]; +- idx = ++pCur->aiIdx[pCur->iPage]; +- assert( pPage->isInit ); ++ pPage = pCur->pPage; ++ idx = ++pCur->ix; ++ if( !pPage->isInit ){ ++ /* The only known way for this to happen is for there to be a ++ ** recursive SQL function that does a DELETE operation as part of a ++ ** SELECT which deletes content out from under an active cursor ++ ** in a corrupt database file where the table being DELETE-ed from ++ ** has pages in common with the table being queried. See TH3 ++ ** module cov1/btree78.test testcase 220 (2018-06-08) for an ++ ** example. */ ++ return SQLITE_CORRUPT_BKPT; ++ } + + /* If the database file is corrupt, it is possible for the value of idx + ** to be invalid here. This can only occur if a second cursor modifies +@@ -58146,15 +68885,14 @@ static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){ + } + do{ + if( pCur->iPage==0 ){ +- *pRes = 1; + pCur->eState = CURSOR_INVALID; +- return SQLITE_OK; ++ return SQLITE_DONE; + } + moveToParent(pCur); +- pPage = pCur->apPage[pCur->iPage]; +- }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell ); ++ pPage = pCur->pPage; ++ }while( pCur->ix>=pPage->nCell ); + if( pPage->intKey ){ +- return sqlite3BtreeNext(pCur, pRes); ++ return sqlite3BtreeNext(pCur, 0); + }else{ + return SQLITE_OK; + } +@@ -58165,20 +68903,18 @@ static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){ + return moveToLeftmost(pCur); + } + } +-SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ ++SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int flags){ + MemPage *pPage; +- assert( cursorHoldsMutex(pCur) ); +- assert( pRes!=0 ); +- assert( *pRes==0 || *pRes==1 ); +- assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); ++ UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */ ++ assert( cursorOwnsBtShared(pCur) ); ++ assert( flags==0 || flags==1 ); + pCur->info.nSize = 0; + pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); +- *pRes = 0; +- if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur, pRes); +- pPage = pCur->apPage[pCur->iPage]; +- if( (++pCur->aiIdx[pCur->iPage])>=pPage->nCell ){ +- pCur->aiIdx[pCur->iPage]--; +- return btreeNext(pCur, pRes); ++ if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur); ++ pPage = pCur->pPage; ++ if( (++pCur->ix)>=pPage->nCell ){ ++ pCur->ix--; ++ return btreeNext(pCur); + } + if( pPage->leaf ){ + return SQLITE_OK; +@@ -58188,10 +68924,12 @@ SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ + } + + /* +-** Step the cursor to the back to the previous entry in the database. If +-** successful then set *pRes=0. If the cursor +-** was already pointing to the first entry in the database before +-** this routine was called, then set *pRes=1. ++** Step the cursor to the back to the previous entry in the database. ++** Return values: ++** ++** SQLITE_OK success ++** SQLITE_DONE the cursor is already on the first element of the table ++** otherwise some kind of error occurred + ** + ** The main entry point is sqlite3BtreePrevious(). That routine is optimized + ** for the common case of merely decrementing the cell counter BtCursor.aiIdx +@@ -58199,23 +68937,17 @@ SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ + ** helper routine is called when it is necessary to move to a different page + ** or to restore the cursor. + ** +-** The calling function will set *pRes to 0 or 1. The initial *pRes value +-** will be 1 if the cursor being stepped corresponds to an SQL index and +-** if this routine could have been skipped if that SQL index had been +-** a unique index. Otherwise the caller will have set *pRes to zero. +-** Zero is the common case. The btree implementation is free to use the +-** initial *pRes value as a hint to improve performance, but the current +-** SQLite btree implementation does not. (Note that the comdb2 btree +-** implementation does use this hint, however.) ++** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then ++** the cursor corresponds to an SQL index and this routine could have been ++** skipped if the SQL index had been a unique index. The F argument is a ++** hint to the implement. The native SQLite btree implementation does not ++** use this hint, but COMDB2 does. + */ +-static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur, int *pRes){ ++static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){ + int rc; + MemPage *pPage; + +- assert( cursorHoldsMutex(pCur) ); +- assert( pRes!=0 ); +- assert( *pRes==0 ); +- assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); ++ assert( cursorOwnsBtShared(pCur) ); + assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 ); + assert( pCur->info.nSize==0 ); + if( pCur->eState!=CURSOR_VALID ){ +@@ -58224,64 +68956,55 @@ static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur, int *pRes){ + return rc; + } + if( CURSOR_INVALID==pCur->eState ){ +- *pRes = 1; +- return SQLITE_OK; ++ return SQLITE_DONE; + } +- if( pCur->skipNext ){ +- assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT ); ++ if( CURSOR_SKIPNEXT==pCur->eState ){ + pCur->eState = CURSOR_VALID; +- if( pCur->skipNext<0 ){ +- pCur->skipNext = 0; +- return SQLITE_OK; +- } +- pCur->skipNext = 0; ++ if( pCur->skipNext<0 ) return SQLITE_OK; + } + } + +- pPage = pCur->apPage[pCur->iPage]; ++ pPage = pCur->pPage; + assert( pPage->isInit ); + if( !pPage->leaf ){ +- int idx = pCur->aiIdx[pCur->iPage]; ++ int idx = pCur->ix; + rc = moveToChild(pCur, get4byte(findCell(pPage, idx))); + if( rc ) return rc; + rc = moveToRightmost(pCur); + }else{ +- while( pCur->aiIdx[pCur->iPage]==0 ){ ++ while( pCur->ix==0 ){ + if( pCur->iPage==0 ){ + pCur->eState = CURSOR_INVALID; +- *pRes = 1; +- return SQLITE_OK; ++ return SQLITE_DONE; + } + moveToParent(pCur); + } + assert( pCur->info.nSize==0 ); +- assert( (pCur->curFlags & (BTCF_ValidNKey|BTCF_ValidOvfl))==0 ); ++ assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 ); + +- pCur->aiIdx[pCur->iPage]--; +- pPage = pCur->apPage[pCur->iPage]; ++ pCur->ix--; ++ pPage = pCur->pPage; + if( pPage->intKey && !pPage->leaf ){ +- rc = sqlite3BtreePrevious(pCur, pRes); ++ rc = sqlite3BtreePrevious(pCur, 0); + }else{ + rc = SQLITE_OK; + } + } + return rc; + } +-SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ +- assert( cursorHoldsMutex(pCur) ); +- assert( pRes!=0 ); +- assert( *pRes==0 || *pRes==1 ); +- assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); +- *pRes = 0; ++SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int flags){ ++ assert( cursorOwnsBtShared(pCur) ); ++ assert( flags==0 || flags==1 ); ++ UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */ + pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey); + pCur->info.nSize = 0; + if( pCur->eState!=CURSOR_VALID +- || pCur->aiIdx[pCur->iPage]==0 +- || pCur->apPage[pCur->iPage]->leaf==0 ++ || pCur->ix==0 ++ || pCur->pPage->leaf==0 + ){ +- return btreePrevious(pCur, pRes); ++ return btreePrevious(pCur); + } +- pCur->aiIdx[pCur->iPage]--; ++ pCur->ix--; + return SQLITE_OK; + } + +@@ -58294,8 +69017,7 @@ SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ + ** sqlite3PagerUnref() on the new page when it is done. + ** + ** SQLITE_OK is returned on success. Any other return value indicates +-** an error. *ppPage and *pPgno are undefined in the event of an error. +-** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. ++** an error. *ppPage is set to NULL in the event of an error. + ** + ** If the "nearby" parameter is not 0, then an effort is made to + ** locate a page close to the page number "nearby". This can be used in an +@@ -58338,6 +69060,7 @@ static int allocateBtreePage( + /* There are pages on the freelist. Reuse one of those pages. */ + Pgno iTrunk; + u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ ++ u32 nSearch = 0; /* Count of the number of search attempts */ + + /* If eMode==BTALLOC_EXACT and a query of the pointer-map + ** shows that the page 'nearby' is somewhere on the free-list, then +@@ -58386,10 +69109,10 @@ static int allocateBtreePage( + iTrunk = get4byte(&pPage1->aData[32]); + } + testcase( iTrunk==mxPage ); +- if( iTrunk>mxPage ){ +- rc = SQLITE_CORRUPT_BKPT; ++ if( iTrunk>mxPage || nSearch++ > n ){ ++ rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1); + }else{ +- rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); ++ rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0); + } + if( rc ){ + pTrunk = 0; +@@ -58416,7 +69139,7 @@ static int allocateBtreePage( + TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); + }else if( k>(u32)(pBt->usableSize/4 - 2) ){ + /* Value of k is out of range. Database corruption */ +- rc = SQLITE_CORRUPT_BKPT; ++ rc = SQLITE_CORRUPT_PGNO(iTrunk); + goto end_allocate_page; + #ifndef SQLITE_OMIT_AUTOVACUUM + }else if( searchList +@@ -58450,11 +69173,11 @@ static int allocateBtreePage( + MemPage *pNewTrunk; + Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); + if( iNewTrunk>mxPage ){ +- rc = SQLITE_CORRUPT_BKPT; ++ rc = SQLITE_CORRUPT_PGNO(iTrunk); + goto end_allocate_page; + } + testcase( iNewTrunk==mxPage ); +- rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); ++ rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0); + if( rc!=SQLITE_OK ){ + goto end_allocate_page; + } +@@ -58515,7 +69238,7 @@ static int allocateBtreePage( + iPage = get4byte(&aData[8+closest*4]); + testcase( iPage==mxPage ); + if( iPage>mxPage ){ +- rc = SQLITE_CORRUPT_BKPT; ++ rc = SQLITE_CORRUPT_PGNO(iTrunk); + goto end_allocate_page; + } + testcase( iPage==mxPage ); +@@ -58534,11 +69257,12 @@ static int allocateBtreePage( + } + put4byte(&aData[4], k-1); + noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0; +- rc = btreeGetPage(pBt, *pPgno, ppPage, noContent); ++ rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent); + if( rc==SQLITE_OK ){ + rc = sqlite3PagerWrite((*ppPage)->pDbPage); + if( rc!=SQLITE_OK ){ + releasePage(*ppPage); ++ *ppPage = 0; + } + } + searchList = 0; +@@ -58582,7 +69306,7 @@ static int allocateBtreePage( + MemPage *pPg = 0; + TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage)); + assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) ); +- rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent); ++ rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent); + if( rc==SQLITE_OK ){ + rc = sqlite3PagerWrite(pPg->pDbPage); + releasePage(pPg); +@@ -58596,31 +69320,23 @@ static int allocateBtreePage( + *pPgno = pBt->nPage; + + assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); +- rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent); ++ rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent); + if( rc ) return rc; + rc = sqlite3PagerWrite((*ppPage)->pDbPage); + if( rc!=SQLITE_OK ){ + releasePage(*ppPage); ++ *ppPage = 0; + } + TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); + } + +- assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); ++ assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) ); + + end_allocate_page: + releasePage(pTrunk); + releasePage(pPrevTrunk); +- if( rc==SQLITE_OK ){ +- if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ +- releasePage(*ppPage); +- *ppPage = 0; +- return SQLITE_CORRUPT_BKPT; +- } +- (*ppPage)->isInit = 0; +- }else{ +- *ppPage = 0; +- } +- assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) ); ++ assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 ); ++ assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 ); + return rc; + } + +@@ -58642,12 +69358,15 @@ static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ + MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */ + MemPage *pPage; /* Page being freed. May be NULL. */ + int rc; /* Return Code */ +- int nFree; /* Initial number of pages on free-list */ ++ u32 nFree; /* Initial number of pages on free-list */ + + assert( sqlite3_mutex_held(pBt->mutex) ); +- assert( iPage>1 ); ++ assert( CORRUPT_DB || iPage>1 ); + assert( !pMemPage || pMemPage->pgno==iPage ); + ++ if( iPage<2 || iPage>pBt->nPage ){ ++ return SQLITE_CORRUPT_BKPT; ++ } + if( pMemPage ){ + pPage = pMemPage; + sqlite3PagerRef(pPage->pDbPage); +@@ -58770,36 +69489,39 @@ static void freePage(MemPage *pPage, int *pRC){ + } + + /* +-** Free any overflow pages associated with the given Cell. Write the +-** local Cell size (the number of bytes on the original page, omitting +-** overflow) into *pnSize. ++** Free any overflow pages associated with the given Cell. Store ++** size information about the cell in pInfo. + */ + static int clearCell( + MemPage *pPage, /* The page that contains the Cell */ + unsigned char *pCell, /* First byte of the Cell */ +- u16 *pnSize /* Write the size of the Cell here */ ++ CellInfo *pInfo /* Size information about the cell */ + ){ +- BtShared *pBt = pPage->pBt; +- CellInfo info; ++ BtShared *pBt; + Pgno ovflPgno; + int rc; + int nOvfl; + u32 ovflPageSize; + + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); +- btreeParseCellPtr(pPage, pCell, &info); +- *pnSize = info.nSize; +- if( info.iOverflow==0 ){ ++ pPage->xParseCell(pPage, pCell, pInfo); ++ if( pInfo->nLocal==pInfo->nPayload ){ + return SQLITE_OK; /* No overflow pages. Return without doing anything */ + } +- if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){ +- return SQLITE_CORRUPT_BKPT; /* Cell extends past end of page */ ++ testcase( pCell + pInfo->nSize == pPage->aDataEnd ); ++ testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd ); ++ if( pCell + pInfo->nSize > pPage->aDataEnd ){ ++ /* Cell extends past end of page */ ++ return SQLITE_CORRUPT_PAGE(pPage); + } +- ovflPgno = get4byte(&pCell[info.iOverflow]); ++ ovflPgno = get4byte(pCell + pInfo->nSize - 4); ++ pBt = pPage->pBt; + assert( pBt->usableSize > 4 ); + ovflPageSize = pBt->usableSize - 4; +- nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; +- assert( ovflPgno==0 || nOvfl>0 ); ++ nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize; ++ assert( nOvfl>0 || ++ (CORRUPT_DB && (pInfo->nPayload + ovflPageSize) pBt; +- Pgno pgnoOvfl = 0; ++ BtShared *pBt; ++ Pgno pgnoOvfl; + int nHeader; + + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + + /* pPage is not necessarily writeable since pCell might be auxiliary + ** buffer space that is separate from the pPage buffer area */ +- assert( pCell aData || pCell>=&pPage->aData[pBt->pageSize] ++ assert( pCell aData || pCell>=&pPage->aData[pPage->pBt->pageSize] + || sqlite3PagerIswriteable(pPage->pDbPage) ); + + /* Fill in the header. */ + nHeader = pPage->childPtrSize; +- nPayload = nData + nZero; +- if( pPage->intKeyLeaf ){ ++ if( pPage->intKey ){ ++ nPayload = pX->nData + pX->nZero; ++ pSrc = pX->pData; ++ nSrc = pX->nData; ++ assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */ + nHeader += putVarint32(&pCell[nHeader], nPayload); ++ nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey); + }else{ +- assert( nData==0 ); +- assert( nZero==0 ); ++ assert( pX->nKey<=0x7fffffff && pX->pKey!=0 ); ++ nSrc = nPayload = (int)pX->nKey; ++ pSrc = pX->pKey; ++ nHeader += putVarint32(&pCell[nHeader], nPayload); + } +- nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); + +- /* Fill in the payload size */ +- if( pPage->intKey ){ +- pSrc = pData; +- nSrc = nData; +- nData = 0; +- }else{ +- if( NEVER(nKey>0x7fffffff || pKey==0) ){ +- return SQLITE_CORRUPT_BKPT; +- } +- nPayload = (int)nKey; +- pSrc = pKey; +- nSrc = (int)nKey; +- } ++ /* Fill in the payload */ ++ pPayload = &pCell[nHeader]; + if( nPayload<=pPage->maxLocal ){ ++ /* This is the common case where everything fits on the btree page ++ ** and no overflow pages are required. */ + n = nHeader + nPayload; + testcase( n==3 ); + testcase( n==4 ); + if( n<4 ) n = 4; + *pnSize = n; +- spaceLeft = nPayload; +- pPrior = pCell; +- }else{ +- int mn = pPage->minLocal; +- n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4); +- testcase( n==pPage->maxLocal ); +- testcase( n==pPage->maxLocal+1 ); +- if( n > pPage->maxLocal ) n = mn; +- spaceLeft = n; +- *pnSize = n + nHeader + 4; +- pPrior = &pCell[nHeader+n]; ++ assert( nSrc<=nPayload ); ++ testcase( nSrc minLocal; ++ n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4); ++ testcase( n==pPage->maxLocal ); ++ testcase( n==pPage->maxLocal+1 ); ++ if( n > pPage->maxLocal ) n = mn; ++ spaceLeft = n; ++ *pnSize = n + nHeader + 4; ++ pPrior = &pCell[nHeader+n]; ++ pToRelease = 0; ++ pgnoOvfl = 0; ++ pBt = pPage->pBt; + + /* At this point variables should be set as follows: + ** +@@ -58936,21 +69659,47 @@ static int fillInCell( + ** Use a call to btreeParseCellPtr() to verify that the values above + ** were computed correctly. + */ +-#if SQLITE_DEBUG ++#ifdef SQLITE_DEBUG + { + CellInfo info; +- btreeParseCellPtr(pPage, pCell, &info); +- assert( nHeader=(int)(info.pPayload - pCell) ); +- assert( info.nKey==nKey ); ++ pPage->xParseCell(pPage, pCell, &info); ++ assert( nHeader==(int)(info.pPayload - pCell) ); ++ assert( info.nKey==pX->nKey ); + assert( *pnSize == info.nSize ); + assert( spaceLeft == info.nLocal ); +- assert( pPrior == &pCell[info.iOverflow] ); + } + #endif + + /* Write the payload into the local Cell and any extra into overflow pages */ +- while( nPayload>0 ){ ++ while( 1 ){ ++ n = nPayload; ++ if( n>spaceLeft ) n = spaceLeft; ++ ++ /* If pToRelease is not zero than pPayload points into the data area ++ ** of pToRelease. Make sure pToRelease is still writeable. */ ++ assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); ++ ++ /* If pPayload is part of the data area of pPage, then make sure pPage ++ ** is still writeable */ ++ assert( pPayload aData || pPayload>=&pPage->aData[pBt->pageSize] ++ || sqlite3PagerIswriteable(pPage->pDbPage) ); ++ ++ if( nSrc>=n ){ ++ memcpy(pPayload, pSrc, n); ++ }else if( nSrc>0 ){ ++ n = nSrc; ++ memcpy(pPayload, pSrc, n); ++ }else{ ++ memset(pPayload, 0, n); ++ } ++ nPayload -= n; ++ if( nPayload<=0 ) break; ++ pPayload += n; ++ pSrc += n; ++ nSrc -= n; ++ spaceLeft -= n; + if( spaceLeft==0 ){ ++ MemPage *pOvfl = 0; + #ifndef SQLITE_OMIT_AUTOVACUUM + Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ + if( pBt->autoVacuum ){ +@@ -59003,34 +69752,6 @@ static int fillInCell( + pPayload = &pOvfl->aData[4]; + spaceLeft = pBt->usableSize - 4; + } +- n = nPayload; +- if( n>spaceLeft ) n = spaceLeft; +- +- /* If pToRelease is not zero than pPayload points into the data area +- ** of pToRelease. Make sure pToRelease is still writeable. */ +- assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); +- +- /* If pPayload is part of the data area of pPage, then make sure pPage +- ** is still writeable */ +- assert( pPayload aData || pPayload>=&pPage->aData[pBt->pageSize] +- || sqlite3PagerIswriteable(pPage->pDbPage) ); +- +- if( nSrc>0 ){ +- if( n>nSrc ) n = nSrc; +- assert( pSrc ); +- memcpy(pPayload, pSrc, n); +- }else{ +- memset(pPayload, 0, n); +- } +- nPayload -= n; +- pPayload += n; +- pSrc += n; +- nSrc -= n; +- spaceLeft -= n; +- if( nSrc==0 ){ +- nSrc = nData; +- pSrc = pData; +- } + } + releasePage(pToRelease); + return SQLITE_OK; +@@ -59052,18 +69773,18 @@ static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){ + int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */ + + if( *pRC ) return; +- + assert( idx>=0 && idx nCell ); +- assert( sz==cellSize(pPage, idx) ); ++ assert( CORRUPT_DB || sz==cellSize(pPage, idx) ); + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); ++ assert( pPage->nFree>=0 ); + data = pPage->aData; + ptr = &pPage->aCellIdx[2*idx]; + pc = get2byte(ptr); + hdr = pPage->hdrOffset; + testcase( pc==get2byte(&data[hdr+5]) ); + testcase( pc+sz==pPage->pBt->usableSize ); +- if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){ ++ if( pc+sz > pPage->pBt->usableSize ){ + *pRC = SQLITE_CORRUPT_BKPT; + return; + } +@@ -59097,6 +69818,8 @@ static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){ + ** in pTemp or the original pCell) and also record its index. + ** Allocating a new entry in pPage->aCell[] implies that + ** pPage->nOverflow is incremented. ++** ++** *pRC must be SQLITE_OK when this routine is called. + */ + static void insertCell( + MemPage *pPage, /* Page into which we are copying */ +@@ -59109,13 +69832,10 @@ static void insertCell( + ){ + int idx = 0; /* Where to write new cell content in data[] */ + int j; /* Loop counter */ +- int end; /* First byte past the last cell pointer in data[] */ +- int ins; /* Index in data[] where new cell pointer is inserted */ +- int cellOffset; /* Address of first cell pointer in data[] */ + u8 *data; /* The content of the whole page */ ++ u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */ + +- if( *pRC ) return; +- ++ assert( *pRC==SQLITE_OK ); + assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); + assert( MX_CELL(pPage->pBt)<=10921 ); + assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB ); +@@ -59127,7 +69847,8 @@ static void insertCell( + ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size + ** might be less than 8 (leaf-size + pointer) on the interior node. Hence + ** the term after the || in the following assert(). */ +- assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) ); ++ assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) ); ++ assert( pPage->nFree>=0 ); + if( pPage->nOverflow || sz+2>pPage->nFree ){ + if( pTemp ){ + memcpy(pTemp, pCell, sz); +@@ -59137,9 +69858,20 @@ static void insertCell( + put4byte(pCell, iChild); + } + j = pPage->nOverflow++; +- assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) ); ++ /* Comparison against ArraySize-1 since we hold back one extra slot ++ ** as a contingency. In other words, never need more than 3 overflow ++ ** slots but 4 are allocated, just to be safe. */ ++ assert( j < ArraySize(pPage->apOvfl)-1 ); + pPage->apOvfl[j] = pCell; + pPage->aiOvfl[j] = (u16)i; ++ ++ /* When multiple overflows occur, they are always sequential and in ++ ** sorted order. This invariants arise because multiple overflows can ++ ** only occur when inserting divider cells into the parent page during ++ ** balancing, and the dividers are adjacent and sorted. ++ */ ++ assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */ ++ assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */ + }else{ + int rc = sqlite3PagerWrite(pPage->pDbPage); + if( rc!=SQLITE_OK ){ +@@ -59148,35 +69880,171 @@ static void insertCell( + } + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + data = pPage->aData; +- cellOffset = pPage->cellOffset; +- end = cellOffset + 2*pPage->nCell; +- ins = cellOffset + 2*i; ++ assert( &data[pPage->cellOffset]==pPage->aCellIdx ); + rc = allocateSpace(pPage, sz, &idx); + if( rc ){ *pRC = rc; return; } +- /* The allocateSpace() routine guarantees the following two properties +- ** if it returns success */ +- assert( idx >= end+2 ); ++ /* The allocateSpace() routine guarantees the following properties ++ ** if it returns successfully */ ++ assert( idx >= 0 ); ++ assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB ); + assert( idx+sz <= (int)pPage->pBt->usableSize ); +- pPage->nCell++; + pPage->nFree -= (u16)(2 + sz); +- memcpy(&data[idx], pCell, sz); + if( iChild ){ ++ /* In a corrupt database where an entry in the cell index section of ++ ** a btree page has a value of 3 or less, the pCell value might point ++ ** as many as 4 bytes in front of the start of the aData buffer for ++ ** the source page. Make sure this does not cause problems by not ++ ** reading the first 4 bytes */ ++ memcpy(&data[idx+4], pCell+4, sz-4); + put4byte(&data[idx], iChild); ++ }else{ ++ memcpy(&data[idx], pCell, sz); + } +- memmove(&data[ins+2], &data[ins], end-ins); +- put2byte(&data[ins], idx); +- put2byte(&data[pPage->hdrOffset+3], pPage->nCell); ++ pIns = pPage->aCellIdx + i*2; ++ memmove(pIns+2, pIns, 2*(pPage->nCell - i)); ++ put2byte(pIns, idx); ++ pPage->nCell++; ++ /* increment the cell count */ ++ if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++; ++ assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB ); + #ifndef SQLITE_OMIT_AUTOVACUUM + if( pPage->pBt->autoVacuum ){ + /* The cell may contain a pointer to an overflow page. If so, write + ** the entry for the overflow page into the pointer map. + */ +- ptrmapPutOvflPtr(pPage, pCell, pRC); ++ ptrmapPutOvflPtr(pPage, pPage, pCell, pRC); + } + #endif + } + } + ++/* ++** The following parameters determine how many adjacent pages get involved ++** in a balancing operation. NN is the number of neighbors on either side ++** of the page that participate in the balancing operation. NB is the ++** total number of pages that participate, including the target page and ++** NN neighbors on either side. ++** ++** The minimum value of NN is 1 (of course). Increasing NN above 1 ++** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance ++** in exchange for a larger degradation in INSERT and UPDATE performance. ++** The value of NN appears to give the best results overall. ++** ++** (Later:) The description above makes it seem as if these values are ++** tunable - as if you could change them and recompile and it would all work. ++** But that is unlikely. NB has been 3 since the inception of SQLite and ++** we have never tested any other value. ++*/ ++#define NN 1 /* Number of neighbors on either side of pPage */ ++#define NB 3 /* (NN*2+1): Total pages involved in the balance */ ++ ++/* ++** A CellArray object contains a cache of pointers and sizes for a ++** consecutive sequence of cells that might be held on multiple pages. ++** ++** The cells in this array are the divider cell or cells from the pParent ++** page plus up to three child pages. There are a total of nCell cells. ++** ++** pRef is a pointer to one of the pages that contributes cells. This is ++** used to access information such as MemPage.intKey and MemPage.pBt->pageSize ++** which should be common to all pages that contribute cells to this array. ++** ++** apCell[] and szCell[] hold, respectively, pointers to the start of each ++** cell and the size of each cell. Some of the apCell[] pointers might refer ++** to overflow cells. In other words, some apCel[] pointers might not point ++** to content area of the pages. ++** ++** A szCell[] of zero means the size of that cell has not yet been computed. ++** ++** The cells come from as many as four different pages: ++** ++** ----------- ++** | Parent | ++** ----------- ++** / | \ ++** / | \ ++** --------- --------- --------- ++** |Child-1| |Child-2| |Child-3| ++** --------- --------- --------- ++** ++** The order of cells is in the array is for an index btree is: ++** ++** 1. All cells from Child-1 in order ++** 2. The first divider cell from Parent ++** 3. All cells from Child-2 in order ++** 4. The second divider cell from Parent ++** 5. All cells from Child-3 in order ++** ++** For a table-btree (with rowids) the items 2 and 4 are empty because ++** content exists only in leaves and there are no divider cells. ++** ++** For an index btree, the apEnd[] array holds pointer to the end of page ++** for Child-1, the Parent, Child-2, the Parent (again), and Child-3, ++** respectively. The ixNx[] array holds the number of cells contained in ++** each of these 5 stages, and all stages to the left. Hence: ++** ++** ixNx[0] = Number of cells in Child-1. ++** ixNx[1] = Number of cells in Child-1 plus 1 for first divider. ++** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider. ++** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells ++** ixNx[4] = Total number of cells. ++** ++** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2] ++** are used and they point to the leaf pages only, and the ixNx value are: ++** ++** ixNx[0] = Number of cells in Child-1. ++** ixNx[1] = Number of cells in Child-1 and Child-2. ++** ixNx[2] = Total number of cells. ++** ++** Sometimes when deleting, a child page can have zero cells. In those ++** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[] ++** entries, shift down. The end result is that each ixNx[] entry should ++** be larger than the previous ++*/ ++typedef struct CellArray CellArray; ++struct CellArray { ++ int nCell; /* Number of cells in apCell[] */ ++ MemPage *pRef; /* Reference page */ ++ u8 **apCell; /* All cells begin balanced */ ++ u16 *szCell; /* Local size of all cells in apCell[] */ ++ u8 *apEnd[NB*2]; /* MemPage.aDataEnd values */ ++ int ixNx[NB*2]; /* Index of at which we move to the next apEnd[] */ ++}; ++ ++/* ++** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been ++** computed. ++*/ ++static void populateCellCache(CellArray *p, int idx, int N){ ++ assert( idx>=0 && idx+N<=p->nCell ); ++ while( N>0 ){ ++ assert( p->apCell[idx]!=0 ); ++ if( p->szCell[idx]==0 ){ ++ p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]); ++ }else{ ++ assert( CORRUPT_DB || ++ p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) ); ++ } ++ idx++; ++ N--; ++ } ++} ++ ++/* ++** Return the size of the Nth element of the cell array ++*/ ++static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){ ++ assert( N>=0 && N nCell ); ++ assert( p->szCell[N]==0 ); ++ p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]); ++ return p->szCell[N]; ++} ++static u16 cachedCellSize(CellArray *p, int N){ ++ assert( N>=0 && N nCell ); ++ if( p->szCell[N] ) return p->szCell[N]; ++ return computeCellSize(p, N); ++} ++ + /* + ** Array apCell[] contains pointers to nCell b-tree page cells. The + ** szCell[] array contains the size in bytes of each cell. This function +@@ -59190,35 +70058,60 @@ static void insertCell( + ** The MemPage.nFree field is invalidated by this function. It is the + ** responsibility of the caller to set it correctly. + */ +-static void rebuildPage( +- MemPage *pPg, /* Edit this page */ ++static int rebuildPage( ++ CellArray *pCArray, /* Content to be added to page pPg */ ++ int iFirst, /* First cell in pCArray to use */ + int nCell, /* Final number of cells on page */ +- u8 **apCell, /* Array of cells */ +- u16 *szCell /* Array of cell sizes */ ++ MemPage *pPg /* The page to be reconstructed */ + ){ + const int hdr = pPg->hdrOffset; /* Offset of header on pPg */ + u8 * const aData = pPg->aData; /* Pointer to data for pPg */ + const int usableSize = pPg->pBt->usableSize; + u8 * const pEnd = &aData[usableSize]; +- int i; ++ int i = iFirst; /* Which cell to copy from pCArray*/ ++ u32 j; /* Start of cell content area */ ++ int iEnd = i+nCell; /* Loop terminator */ + u8 *pCellptr = pPg->aCellIdx; + u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); + u8 *pData; ++ int k; /* Current slot in pCArray->apEnd[] */ ++ u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */ ++ ++ assert( i (u32)usableSize) ){ j = 0; } ++ memcpy(&pTmp[j], &aData[j], usableSize - j); + +- i = get2byte(&aData[hdr+5]); +- memcpy(&pTmp[i], &aData[i], usableSize - i); ++ for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k apEnd[k]; + + pData = pEnd; +- for(i=0; i aData && pCell apCell[i]; ++ u16 sz = pCArray->szCell[i]; ++ assert( sz>0 ); ++ if( SQLITE_WITHIN(pCell,aData,pEnd) ){ ++ if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPT; + pCell = &pTmp[pCell - aData]; ++ }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd ++ && (uptr)(pCell)<(uptr)pSrcEnd ++ ){ ++ return SQLITE_CORRUPT_BKPT; + } +- pData -= szCell[i]; +- memcpy(pData, pCell, szCell[i]); ++ ++ pData -= sz; + put2byte(pCellptr, (pData - aData)); + pCellptr += 2; +- assert( szCell[i]==cellSizePtr(pPg, pCell) ); ++ if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT; ++ memcpy(pData, pCell, sz); ++ assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB ); ++ testcase( sz!=pPg->xCellSize(pPg,pCell) ); ++ i++; ++ if( i>=iEnd ) break; ++ if( pCArray->ixNx[k]<=i ){ ++ k++; ++ pSrcEnd = pCArray->apEnd[k]; ++ } + } + + /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ +@@ -59229,15 +70122,15 @@ static void rebuildPage( + put2byte(&aData[hdr+3], pPg->nCell); + put2byte(&aData[hdr+5], pData - aData); + aData[hdr+7] = 0x00; ++ return SQLITE_OK; + } + + /* +-** Array apCell[] contains nCell pointers to b-tree cells. Array szCell +-** contains the size in bytes of each such cell. This function attempts to +-** add the cells stored in the array to page pPg. If it cannot (because +-** the page needs to be defragmented before the cells will fit), non-zero +-** is returned. Otherwise, if the cells are added successfully, zero is +-** returned. ++** The pCArray objects contains pointers to b-tree cells and the cell sizes. ++** This function attempts to add the cells stored in the array to page pPg. ++** If it cannot (because the page needs to be defragmented before the cells ++** will fit), non-zero is returned. Otherwise, if the cells are added ++** successfully, zero is returned. + ** + ** Argument pCellptr points to the first entry in the cell-pointer array + ** (part of page pPg) to populate. After cell apCell[0] is written to the +@@ -59259,61 +70152,90 @@ static void rebuildPage( + static int pageInsertArray( + MemPage *pPg, /* Page to add cells to */ + u8 *pBegin, /* End of cell-pointer array */ +- u8 **ppData, /* IN/OUT: Page content -area pointer */ ++ u8 **ppData, /* IN/OUT: Page content-area pointer */ + u8 *pCellptr, /* Pointer to cell-pointer area */ ++ int iFirst, /* Index of first cell to add */ + int nCell, /* Number of cells to add to pPg */ +- u8 **apCell, /* Array of cells */ +- u16 *szCell /* Array of cell sizes */ +-){ +- int i; +- u8 *aData = pPg->aData; +- u8 *pData = *ppData; +- const int bFreelist = aData[1] || aData[2]; ++ CellArray *pCArray /* Array of cells */ ++){ ++ int i = iFirst; /* Loop counter - cell index to insert */ ++ u8 *aData = pPg->aData; /* Complete page */ ++ u8 *pData = *ppData; /* Content area. A subset of aData[] */ ++ int iEnd = iFirst + nCell; /* End of loop. One past last cell to ins */ ++ int k; /* Current slot in pCArray->apEnd[] */ ++ u8 *pEnd; /* Maximum extent of cell data */ + assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */ +- for(i=0; i ixNx[k]<=i && ALWAYS(k apEnd[k]; ++ while( 1 /*Exit by break*/ ){ ++ int sz, rc; + u8 *pSlot; +- if( bFreelist==0 || (pSlot = pageFindSlot(pPg, sz, &rc, 0))==0 ){ ++ sz = cachedCellSize(pCArray, i); ++ if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){ ++ if( (pData - pBegin) apCell[i] will never overlap on a well-formed ++ ** database. But they might for a corrupt database. Hence use memmove() ++ ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */ ++ assert( (pSlot+sz)<=pCArray->apCell[i] ++ || pSlot>=(pCArray->apCell[i]+sz) ++ || CORRUPT_DB ); ++ if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd ++ && (uptr)(pCArray->apCell[i])<(uptr)pEnd ++ ){ ++ assert( CORRUPT_DB ); ++ (void)SQLITE_CORRUPT_BKPT; ++ return 1; ++ } ++ memmove(pSlot, pCArray->apCell[i], sz); + put2byte(pCellptr, (pSlot - aData)); + pCellptr += 2; ++ i++; ++ if( i>=iEnd ) break; ++ if( pCArray->ixNx[k]<=i ){ ++ k++; ++ pEnd = pCArray->apEnd[k]; ++ } + } + *ppData = pData; + return 0; + } + + /* +-** Array apCell[] contains nCell pointers to b-tree cells. Array szCell +-** contains the size in bytes of each such cell. This function adds the +-** space associated with each cell in the array that is currently stored +-** within the body of pPg to the pPg free-list. The cell-pointers and other +-** fields of the page are not updated. ++** The pCArray object contains pointers to b-tree cells and their sizes. ++** ++** This function adds the space associated with each cell in the array ++** that is currently stored within the body of pPg to the pPg free-list. ++** The cell-pointers and other fields of the page are not updated. + ** + ** This function returns the total number of cells added to the free-list. + */ + static int pageFreeArray( + MemPage *pPg, /* Page to edit */ ++ int iFirst, /* First cell to delete */ + int nCell, /* Cells to delete */ +- u8 **apCell, /* Array of cells */ +- u16 *szCell /* Array of cell sizes */ ++ CellArray *pCArray /* Array of cells */ + ){ + u8 * const aData = pPg->aData; + u8 * const pEnd = &aData[pPg->pBt->usableSize]; + u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize]; + int nRet = 0; + int i; ++ int iEnd = iFirst + nCell; + u8 *pFree = 0; + int szFree = 0; + +- for(i=0; i =pStart && pCell apCell[i]; ++ if( SQLITE_WITHIN(pCell, pStart, pEnd) ){ ++ int sz; ++ /* No need to use cachedCellSize() here. The sizes of all cells that ++ ** are to be freed have already been computing while deciding which ++ ** cells need freeing */ ++ sz = pCArray->szCell[i]; assert( sz>0 ); + if( pFree!=(pCell + sz) ){ + if( pFree ){ + assert( pFree>aData && (pFree - aData)<65536 ); +@@ -59337,9 +70259,9 @@ static int pageFreeArray( + } + + /* +-** apCell[] and szCell[] contains pointers to and sizes of all cells in the +-** pages being balanced. The current page, pPg, has pPg->nCell cells starting +-** with apCell[iOld]. After balancing, this page should hold nNew cells ++** pCArray contains pointers to and sizes of all cells in the page being ++** balanced. The current page, pPg, has pPg->nCell cells starting with ++** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells + ** starting at apCell[iNew]. + ** + ** This routine makes the necessary adjustments to pPg so that it contains +@@ -59348,13 +70270,12 @@ static int pageFreeArray( + ** The pPg->nFree field is invalid when this function returns. It is the + ** responsibility of the caller to set it correctly. + */ +-static void editPage( ++static int editPage( + MemPage *pPg, /* Edit this page */ + int iOld, /* Index of first cell currently on page */ + int iNew, /* Index of new first cell on page */ + int nNew, /* Final number of cells on page */ +- u8 **apCell, /* Array of cells */ +- u16 *szCell /* Array of cell sizes */ ++ CellArray *pCArray /* Array of cells and sizes */ + ){ + u8 * const aData = pPg->aData; + const int hdr = pPg->hdrOffset; +@@ -59372,17 +70293,17 @@ static void editPage( + #endif + + /* Remove cells from the start and end of the page */ ++ assert( nCell>=0 ); + if( iOld nCell ) return SQLITE_CORRUPT_BKPT; + memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); + nCell -= nShift; + } + if( iNewEnd < iOldEnd ){ +- nCell -= pageFreeArray( +- pPg, iOldEnd-iNewEnd, &apCell[iNewEnd], &szCell[iNewEnd] +- ); ++ int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray); ++ assert( nCell>=nTail ); ++ nCell -= nTail; + } + + pData = &aData[get2byteNotZero(&aData[hdr+5])]; +@@ -59392,11 +70313,12 @@ static void editPage( + if( iNew =0 ); + pCellptr = pPg->aCellIdx; + memmove(&pCellptr[nAdd*2], pCellptr, nCell*2); + if( pageInsertArray( + pPg, pBegin, &pData, pCellptr, +- nAdd, &apCell[iNew], &szCell[iNew] ++ iNew, nAdd, pCArray + ) ) goto editpage_fail; + nCell += nAdd; + } +@@ -59406,20 +70328,23 @@ static void editPage( + int iCell = (iOld + pPg->aiOvfl[i]) - iNew; + if( iCell>=0 && iCell aCellIdx[iCell * 2]; +- memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); ++ if( nCell>iCell ){ ++ memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); ++ } + nCell++; + if( pageInsertArray( + pPg, pBegin, &pData, pCellptr, +- 1, &apCell[iCell + iNew], &szCell[iCell + iNew] ++ iCell+iNew, 1, pCArray + ) ) goto editpage_fail; + } + } + + /* Append cells to the end of the page */ ++ assert( nCell>=0 ); + pCellptr = &pPg->aCellIdx[nCell*2]; + if( pageInsertArray( + pPg, pBegin, &pData, pCellptr, +- nNew-nCell, &apCell[iNew+nCell], &szCell[iNew+nCell] ++ iNew+nCell, nNew-nCell, pCArray + ) ) goto editpage_fail; + + pPg->nCell = nNew; +@@ -59430,36 +70355,23 @@ static void editPage( + + #ifdef SQLITE_DEBUG + for(i=0; i aCellIdx[i*2]); +- if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){ ++ u8 *pCell = pCArray->apCell[i+iNew]; ++ int iOff = get2byteAligned(&pPg->aCellIdx[i*2]); ++ if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){ + pCell = &pTmp[pCell - aData]; + } +- assert( 0==memcmp(pCell, &aData[iOff], szCell[i+iNew]) ); ++ assert( 0==memcmp(pCell, &aData[iOff], ++ pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) ); + } + #endif + +- return; ++ return SQLITE_OK; + editpage_fail: + /* Unable to edit this page. Rebuild it from scratch instead. */ +- rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]); ++ populateCellCache(pCArray, iNew, nNew); ++ return rebuildPage(pCArray, iNew, nNew, pPg); + } + +-/* +-** The following parameters determine how many adjacent pages get involved +-** in a balancing operation. NN is the number of neighbors on either side +-** of the page that participate in the balancing operation. NB is the +-** total number of pages that participate, including the target page and +-** NN neighbors on either side. +-** +-** The minimum value of NN is 1 (of course). Increasing NN above 1 +-** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance +-** in exchange for a larger degradation in INSERT and UPDATE performance. +-** The value of NN appears to give the best results overall. +-*/ +-#define NN 1 /* Number of neighbors on either side of pPage */ +-#define NB (NN*2+1) /* Total pages involved in the balance */ +- + + #ifndef SQLITE_OMIT_QUICKBALANCE + /* +@@ -59494,9 +70406,10 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( sqlite3PagerIswriteable(pParent->pDbPage) ); + assert( pPage->nOverflow==1 ); +- +- /* This error condition is now caught prior to reaching this function */ +- if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT; ++ ++ if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */ ++ assert( pPage->nFree>=0 ); ++ assert( pParent->nFree>=0 ); + + /* Allocate a new page. This page will become the right-sibling of + ** pPage. Make the parent page writable, so that the new divider cell +@@ -59508,13 +70421,24 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ + + u8 *pOut = &pSpace[4]; + u8 *pCell = pPage->apOvfl[0]; +- u16 szCell = cellSizePtr(pPage, pCell); ++ u16 szCell = pPage->xCellSize(pPage, pCell); + u8 *pStop; ++ CellArray b; + + assert( sqlite3PagerIswriteable(pNew->pDbPage) ); +- assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); ++ assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); + zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); +- rebuildPage(pNew, 1, &pCell, &szCell); ++ b.nCell = 1; ++ b.pRef = pPage; ++ b.apCell = &pCell; ++ b.szCell = &szCell; ++ b.apEnd[0] = pPage->aDataEnd; ++ b.ixNx[0] = 2; ++ rc = rebuildPage(&b, 0, 1, pNew); ++ if( NEVER(rc) ){ ++ releasePage(pNew); ++ return rc; ++ } + pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell; + + /* If this is an auto-vacuum database, update the pointer map +@@ -59529,7 +70453,7 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ + if( ISAUTOVACUUM ){ + ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc); + if( szCell>pNew->minLocal ){ +- ptrmapPutOvflPtr(pNew, pCell, &rc); ++ ptrmapPutOvflPtr(pNew, pNew, pCell, &rc); + } + } + +@@ -59553,8 +70477,10 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ + while( ((*(pOut++) = *(pCell++))&0x80) && pCell nCell, pSpace, (int)(pOut-pSpace), +- 0, pPage->pgno, &rc); ++ if( rc==SQLITE_OK ){ ++ insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace), ++ 0, pPage->pgno, &rc); ++ } + + /* Set the right-child pointer of pParent to point to the new page. */ + put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); +@@ -59587,9 +70513,9 @@ static int ptrmapCheckPages(MemPage **apPage, int nPage){ + u8 *z; + + z = findCell(pPage, j); +- btreeParseCellPtr(pPage, z, &info); +- if( info.iOverflow ){ +- Pgno ovfl = get4byte(&z[info.iOverflow]); ++ pPage->xParseCell(pPage, z, &info); ++ if( info.nLocal pgno && e==PTRMAP_OVERFLOW1 ); + } +@@ -59653,6 +70579,7 @@ static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){ + */ + pTo->isInit = 0; + rc = btreeInitPage(pTo); ++ if( rc==SQLITE_OK ) rc = btreeComputeFreeSpace(pTo); + if( rc!=SQLITE_OK ){ + *pRC = rc; + return; +@@ -59707,9 +70634,6 @@ static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){ + ** If aOvflSpace is set to a null pointer, this function returns + ** SQLITE_NOMEM. + */ +-#if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM) +-#pragma optimize("", off) +-#endif + static int balance_nonroot( + MemPage *pParent, /* Parent page of siblings being balanced */ + int iParentIdx, /* Index of "the page" in pParent */ +@@ -59718,7 +70642,6 @@ static int balance_nonroot( + int bBulk /* True if this call is part of a bulk load */ + ){ + BtShared *pBt; /* The whole database */ +- int nCell = 0; /* Number of cells in apCell[] */ + int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ + int nNew = 0; /* Number of pages in apNew[] */ + int nOld; /* Number of pages in apOld[] */ +@@ -59729,7 +70652,6 @@ static int balance_nonroot( + int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ + int usableSpace; /* Bytes in pPage beyond the header */ + int pageFlags; /* Value of pPage->aData[0] */ +- int subtotal; /* Subtotal of bytes in cells on one page */ + int iSpace1 = 0; /* First unused byte of aSpace1[] */ + int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ + int szScratch; /* Size of scratch memory requested */ +@@ -59737,27 +70659,24 @@ static int balance_nonroot( + MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ + u8 *pRight; /* Location in parent of right-sibling pointer */ + u8 *apDiv[NB-1]; /* Divider cells in pParent */ +- int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ +- int cntOld[NB+2]; /* Old index in aCell[] after i-th page */ ++ int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */ ++ int cntOld[NB+2]; /* Old index in b.apCell[] */ + int szNew[NB+2]; /* Combined size of cells placed on i-th page */ +- u8 **apCell = 0; /* All cells begin balanced */ +- u16 *szCell; /* Local size of all cells in apCell[] */ + u8 *aSpace1; /* Space for copies of dividers cells */ + Pgno pgno; /* Temp var to store a page number in */ + u8 abDone[NB+2]; /* True after i'th new page is populated */ + Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */ + Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */ + u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */ ++ CellArray b; /* Parsed information on cells being balanced */ + + memset(abDone, 0, sizeof(abDone)); ++ b.nCell = 0; ++ b.apCell = 0; + pBt = pParent->pBt; + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( sqlite3PagerIswriteable(pParent->pDbPage) ); + +-#if 0 +- TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); +-#endif +- + /* At this point pParent may have at most one overflow cell. And if + ** this overflow cell is present, it must be the cell with + ** index iParentIdx. This scenario comes about when this function +@@ -59767,8 +70686,9 @@ static int balance_nonroot( + assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx ); + + if( !aOvflSpace ){ +- return SQLITE_NOMEM; ++ return SQLITE_NOMEM_BKPT; + } ++ assert( pParent->nFree>=0 ); + + /* Find the sibling pages to balance. Also locate the cells in pParent + ** that divide the siblings. An attempt is made to find NN siblings on +@@ -59803,23 +70723,29 @@ static int balance_nonroot( + } + pgno = get4byte(pRight); + while( 1 ){ +- rc = getAndInitPage(pBt, pgno, &apOld[i], 0); ++ rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0); + if( rc ){ + memset(apOld, 0, (i+1)*sizeof(MemPage*)); + goto balance_cleanup; + } +- nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; ++ if( apOld[i]->nFree<0 ){ ++ rc = btreeComputeFreeSpace(apOld[i]); ++ if( rc ){ ++ memset(apOld, 0, (i)*sizeof(MemPage*)); ++ goto balance_cleanup; ++ } ++ } + if( (i--)==0 ) break; + +- if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){ ++ if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){ + apDiv[i] = pParent->apOvfl[0]; + pgno = get4byte(apDiv[i]); +- szNew[i] = cellSizePtr(pParent, apDiv[i]); ++ szNew[i] = pParent->xCellSize(pParent, apDiv[i]); + pParent->nOverflow = 0; + }else{ + apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow); + pgno = get4byte(apDiv[i]); +- szNew[i] = cellSizePtr(pParent, apDiv[i]); ++ szNew[i] = pParent->xCellSize(pParent, apDiv[i]); + + /* Drop the cell from the parent page. apDiv[i] still points to + ** the cell within the parent, even though it has been dropped. +@@ -59833,7 +70759,7 @@ static int balance_nonroot( + ** In this case, temporarily copy the cell into the aOvflSpace[] + ** buffer. It will be copied out again as soon as the aSpace[] buffer + ** is allocated. */ +- if( pBt->btsFlags & BTS_SECURE_DELETE ){ ++ if( pBt->btsFlags & BTS_FAST_SECURE ){ + int iOff; + + iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData); +@@ -59852,136 +70778,215 @@ static int balance_nonroot( + + /* Make nMaxCells a multiple of 4 in order to preserve 8-byte + ** alignment */ ++ nMaxCells = nOld*(MX_CELL(pBt) + ArraySize(pParent->apOvfl)); + nMaxCells = (nMaxCells + 3)&~3; + + /* + ** Allocate space for memory structures + */ + szScratch = +- nMaxCells*sizeof(u8*) /* apCell */ +- + nMaxCells*sizeof(u16) /* szCell */ ++ nMaxCells*sizeof(u8*) /* b.apCell */ ++ + nMaxCells*sizeof(u16) /* b.szCell */ + + pBt->pageSize; /* aSpace1 */ + +- /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer +- ** that is more than 6 times the database page size. */ +- assert( szScratch<=6*(int)pBt->pageSize ); +- apCell = sqlite3ScratchMalloc( szScratch ); +- if( apCell==0 ){ +- rc = SQLITE_NOMEM; ++ assert( szScratch<=7*(int)pBt->pageSize ); ++ b.apCell = sqlite3StackAllocRaw(0, szScratch ); ++ if( b.apCell==0 ){ ++ rc = SQLITE_NOMEM_BKPT; + goto balance_cleanup; + } +- szCell = (u16*)&apCell[nMaxCells]; +- aSpace1 = (u8*)&szCell[nMaxCells]; ++ b.szCell = (u16*)&b.apCell[nMaxCells]; ++ aSpace1 = (u8*)&b.szCell[nMaxCells]; + assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); + + /* + ** Load pointers to all cells on sibling pages and the divider cells +- ** into the local apCell[] array. Make copies of the divider cells ++ ** into the local b.apCell[] array. Make copies of the divider cells + ** into space obtained from aSpace1[]. The divider cells have already + ** been removed from pParent. + ** + ** If the siblings are on leaf pages, then the child pointers of the + ** divider cells are stripped from the cells before they are copied +- ** into aSpace1[]. In this way, all cells in apCell[] are without ++ ** into aSpace1[]. In this way, all cells in b.apCell[] are without + ** child pointers. If siblings are not leaves, then all cell in +- ** apCell[] include child pointers. Either way, all cells in apCell[] ++ ** b.apCell[] include child pointers. Either way, all cells in b.apCell[] + ** are alike. + ** + ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. + ** leafData: 1 if pPage holds key+data and pParent holds only keys. + */ +- leafCorrection = apOld[0]->leaf*4; +- leafData = apOld[0]->intKeyLeaf; ++ b.pRef = apOld[0]; ++ leafCorrection = b.pRef->leaf*4; ++ leafData = b.pRef->intKeyLeaf; + for(i=0; i nCell; ++ u8 *aData = pOld->aData; ++ u16 maskPage = pOld->maskPage; ++ u8 *piCell = aData + pOld->cellOffset; ++ u8 *piEnd; ++ ++ /* Verify that all sibling pages are of the same "type" (table-leaf, ++ ** table-interior, index-leaf, or index-interior). ++ */ ++ if( pOld->aData[0]!=apOld[0]->aData[0] ){ ++ rc = SQLITE_CORRUPT_BKPT; ++ goto balance_cleanup; ++ } + +- limit = pOld->nCell+pOld->nOverflow; ++ /* Load b.apCell[] with pointers to all cells in pOld. If pOld ++ ** contains overflow cells, include them in the b.apCell[] array ++ ** in the correct spot. ++ ** ++ ** Note that when there are multiple overflow cells, it is always the ++ ** case that they are sequential and adjacent. This invariant arises ++ ** because multiple overflows can only occurs when inserting divider ++ ** cells into a parent on a prior balance, and divider cells are always ++ ** adjacent and are inserted in order. There is an assert() tagged ++ ** with "NOTE 1" in the overflow cell insertion loop to prove this ++ ** invariant. ++ ** ++ ** This must be done in advance. Once the balance starts, the cell ++ ** offset section of the btree page will be overwritten and we will no ++ ** long be able to find the cells if a pointer to each cell is not saved ++ ** first. ++ */ ++ memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow)); + if( pOld->nOverflow>0 ){ ++ limit = pOld->aiOvfl[0]; + for(j=0; j aData; +- u16 maskPage = pOld->maskPage; +- u16 cellOffset = pOld->cellOffset; +- for(j=0; j nOverflow; k++){ ++ assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */ ++ b.apCell[b.nCell] = pOld->apOvfl[k]; ++ b.nCell++; + } +- } +- cntOld[i] = nCell; ++ } ++ piEnd = aData + pOld->cellOffset + 2*pOld->nCell; ++ while( piCell