cjson
.github
workflows CI.yml ci-fuzz.yml
CONTRIBUTING.md
fuzzing
inputs test1 test10 test11 test2 test3 test3.bu test3.uf test3.uu test4 test5 test6 test7 test8 test9
.gitignore CMakeLists.txt afl-prepare-linux.sh afl.c afl.sh cjson_read_fuzzer.c fuzz_main.c json.dict ossfuzz.sh
library_config cJSONConfig.cmake.in cJSONConfigVersion.cmake.in libcjson.pc.in libcjson_utils.pc.in uninstall.cmake
tests
inputs test1 test1.expected test10 test10.expected test11 test11.expected test2 test2.expected test3 test3.expected test4 test4.expected test5 test5.expected test6 test7 test7.expected test8 test8.expected test9 test9.expected
json-patch-tests .editorconfig .gitignore .npmignore README.md cjson-utils-tests.json package.json spec_tests.json tests.json
unity
auto colour_prompt.rb colour_reporter.rb generate_config.yml generate_module.rb generate_test_runner.rb parse_output.rb stylize_as_junit.rb test_file_filter.rb type_sanitizer.rb unity_test_summary.py unity_test_summary.rb unity_to_junit.py
docs ThrowTheSwitchCodingStandard.md UnityAssertionsCheatSheetSuitableforPrintingandPossiblyFraming.pdf UnityAssertionsReference.md UnityConfigurationGuide.md UnityGettingStartedGuide.md UnityHelperScriptsGuide.md license.txt
examples
example_1
src ProductionCode.c ProductionCode.h ProductionCode2.c ProductionCode2.h
makefile readme.txt
example_2
src ProductionCode.c ProductionCode.h ProductionCode2.c ProductionCode2.h
makefile readme.txt
example_3
helper UnityHelper.c UnityHelper.h
src ProductionCode.c ProductionCode.h ProductionCode2.c ProductionCode2.h
rakefile.rb rakefile_helper.rb readme.txt target_gcc_32.yml
unity_config.h
extras
eclipse error_parsers.txt
fixture
src unity_fixture.c unity_fixture.h unity_fixture_internals.h unity_fixture_malloc_overrides.h
rakefile.rb rakefile_helper.rb readme.txt
release build.info version.info
src unity.c unity.h unity_internals.h
.gitattributes .gitignore .travis.yml README.md
CMakeLists.txt cjson_add.c common.h compare_tests.c json_patch_tests.c minify_tests.c misc_tests.c misc_utils_tests.c old_utils_tests.c parse_array.c parse_examples.c parse_hex4.c parse_number.c parse_object.c parse_string.c parse_value.c parse_with_opts.c print_array.c print_number.c print_object.c print_string.c print_value.c readme_examples.c unity_setup.c
.editorconfig .gitattributes .gitignore .travis.yml CHANGELOG.md CMakeLists.txt CONTRIBUTORS.md LICENSE Makefile README.md SECURITY.md appveyor.yml cJSON.c cJSON.h cJSON_Utils.c cJSON_Utils.h test.c valgrind.supp
curl
.circleci config.yml
.github
ISSUE_TEMPLATE bug_report.yml config.yml docs.yml
scripts cleancmd.pl cmp-config.pl cmp-pkg-config.sh codespell-ignore.words codespell.sh distfiles.sh pyspelling.words pyspelling.yaml randcurl.pl requirements-docs.txt requirements-proselint.txt requirements.txt shellcheck-ci.sh shellcheck.sh spellcheck.curl trimmarkdownheader.pl typos.sh typos.toml verify-examples.pl verify-synopsis.pl yamlcheck.sh yamlcheck.yaml
workflows appveyor-status.yml checkdocs.yml checksrc.yml checkurls.yml codeql.yml configure-vs-cmake.yml curl-for-win.yml distcheck.yml fuzz.yml http3-linux.yml label.yml linux-old.yml linux.yml macos.yml non-native.yml windows.yml
CODEOWNERS CONTRIBUTING.md FUNDING.yml dependabot.yml labeler.yml lock.yml stale.yml
CMake CurlSymbolHiding.cmake CurlTests.c FindBrotli.cmake FindCares.cmake FindGSS.cmake FindGnuTLS.cmake FindLDAP.cmake FindLibbacktrace.cmake FindLibgsasl.cmake FindLibidn2.cmake FindLibpsl.cmake FindLibssh.cmake FindLibssh2.cmake FindLibuv.cmake FindMbedTLS.cmake FindNGHTTP2.cmake FindNGHTTP3.cmake FindNGTCP2.cmake FindNettle.cmake FindQuiche.cmake FindRustls.cmake FindWolfSSL.cmake FindZstd.cmake Macros.cmake OtherTests.cmake PickyWarnings.cmake Utilities.cmake cmake_uninstall.in.cmake curl-config.in.cmake unix-cache.cmake win32-cache.cmake
LICENSES BSD-4-Clause-UC.txt ISC.txt curl.txt
docs
cmdline-opts .gitignore CMakeLists.txt MANPAGE.md Makefile.am Makefile.inc _AUTHORS.md _BUGS.md _DESCRIPTION.md _ENVIRONMENT.md _EXITCODES.md _FILES.md _GLOBBING.md _NAME.md _OPTIONS.md _OUTPUT.md _PROGRESS.md _PROTOCOLS.md _PROXYPREFIX.md _SEEALSO.md _SYNOPSIS.md _URL.md _VARIABLES.md _VERSION.md _WWW.md abstract-unix-socket.md alt-svc.md anyauth.md append.md aws-sigv4.md basic.md ca-native.md cacert.md capath.md cert-status.md cert-type.md cert.md ciphers.md compressed-ssh.md compressed.md config.md connect-timeout.md connect-to.md continue-at.md cookie-jar.md cookie.md create-dirs.md create-file-mode.md crlf.md crlfile.md curves.md data-ascii.md data-binary.md data-raw.md data-urlencode.md data.md delegation.md digest.md disable-eprt.md disable-epsv.md disable.md disallow-username-in-url.md dns-interface.md dns-ipv4-addr.md dns-ipv6-addr.md dns-servers.md doh-cert-status.md doh-insecure.md doh-url.md dump-ca-embed.md dump-header.md ech.md egd-file.md engine.md etag-compare.md etag-save.md expect100-timeout.md fail-early.md fail-with-body.md fail.md false-start.md follow.md form-escape.md form-string.md form.md ftp-account.md ftp-alternative-to-user.md ftp-create-dirs.md ftp-method.md ftp-pasv.md ftp-port.md ftp-pret.md ftp-skip-pasv-ip.md ftp-ssl-ccc-mode.md ftp-ssl-ccc.md ftp-ssl-control.md get.md globoff.md happy-eyeballs-timeout-ms.md haproxy-clientip.md haproxy-protocol.md head.md header.md help.md hostpubmd5.md hostpubsha256.md hsts.md http0.9.md http1.0.md http1.1.md http2-prior-knowledge.md http2.md http3-only.md http3.md ignore-content-length.md insecure.md interface.md ip-tos.md ipfs-gateway.md ipv4.md ipv6.md json.md junk-session-cookies.md keepalive-cnt.md keepalive-time.md key-type.md key.md knownhosts.md krb.md libcurl.md limit-rate.md list-only.md local-port.md location-trusted.md location.md login-options.md mail-auth.md mail-from.md mail-rcpt-allowfails.md mail-rcpt.md mainpage.idx manual.md max-filesize.md max-redirs.md max-time.md metalink.md mptcp.md negotiate.md netrc-file.md netrc-optional.md netrc.md next.md no-alpn.md no-buffer.md no-clobber.md no-keepalive.md no-npn.md no-progress-meter.md no-sessionid.md noproxy.md ntlm-wb.md ntlm.md oauth2-bearer.md out-null.md output-dir.md output.md parallel-immediate.md parallel-max-host.md parallel-max.md parallel.md pass.md path-as-is.md pinnedpubkey.md post301.md post302.md post303.md preproxy.md progress-bar.md proto-default.md proto-redir.md proto.md proxy-anyauth.md proxy-basic.md proxy-ca-native.md proxy-cacert.md proxy-capath.md proxy-cert-type.md proxy-cert.md proxy-ciphers.md proxy-crlfile.md proxy-digest.md proxy-header.md proxy-http2.md proxy-insecure.md proxy-key-type.md proxy-key.md proxy-negotiate.md proxy-ntlm.md proxy-pass.md proxy-pinnedpubkey.md proxy-service-name.md proxy-ssl-allow-beast.md proxy-ssl-auto-client-cert.md proxy-tls13-ciphers.md proxy-tlsauthtype.md proxy-tlspassword.md proxy-tlsuser.md proxy-tlsv1.md proxy-user.md proxy.md proxy1.0.md proxytunnel.md pubkey.md quote.md random-file.md range.md rate.md raw.md referer.md remote-header-name.md remote-name-all.md remote-name.md remote-time.md remove-on-error.md request-target.md request.md resolve.md retry-all-errors.md retry-connrefused.md retry-delay.md retry-max-time.md retry.md sasl-authzid.md sasl-ir.md service-name.md show-error.md show-headers.md sigalgs.md silent.md skip-existing.md socks4.md socks4a.md socks5-basic.md socks5-gssapi-nec.md socks5-gssapi-service.md socks5-gssapi.md socks5-hostname.md socks5.md speed-limit.md speed-time.md ssl-allow-beast.md ssl-auto-client-cert.md ssl-no-revoke.md ssl-reqd.md ssl-revoke-best-effort.md ssl-sessions.md ssl.md sslv2.md sslv3.md stderr.md styled-output.md suppress-connect-headers.md tcp-fastopen.md tcp-nodelay.md telnet-option.md tftp-blksize.md tftp-no-options.md time-cond.md tls-earlydata.md tls-max.md tls13-ciphers.md tlsauthtype.md tlspassword.md tlsuser.md tlsv1.0.md tlsv1.1.md tlsv1.2.md tlsv1.3.md tlsv1.md tr-encoding.md trace-ascii.md trace-config.md trace-ids.md trace-time.md trace.md unix-socket.md upload-file.md upload-flags.md url-query.md url.md use-ascii.md user-agent.md user.md variable.md verbose.md version.md vlan-priority.md write-out.md xattr.md
examples .checksrc .gitignore 10-at-a-time.c CMakeLists.txt Makefile.am Makefile.example Makefile.inc README.md adddocsref.pl address-scope.c altsvc.c anyauthput.c block_ip.c cacertinmem.c certinfo.c chkspeed.c connect-to.c cookie_interface.c crawler.c debug.c default-scheme.c ephiperfifo.c evhiperfifo.c externalsocket.c fileupload.c ftp-delete.c ftp-wildcard.c ftpget.c ftpgetinfo.c ftpgetresp.c ftpsget.c ftpupload.c ftpuploadfrommem.c ftpuploadresume.c getinfo.c getinmemory.c getredirect.c getreferrer.c ghiper.c headerapi.c hiperfifo.c hsts-preload.c htmltidy.c htmltitle.cpp http-options.c http-post.c http2-download.c http2-pushinmemory.c http2-serverpush.c http2-upload.c http3-present.c http3.c httpcustomheader.c httpput-postfields.c httpput.c https.c imap-append.c imap-authzid.c imap-copy.c imap-create.c imap-delete.c imap-examine.c imap-fetch.c imap-list.c imap-lsub.c imap-multi.c imap-noop.c imap-search.c imap-ssl.c imap-store.c imap-tls.c interface.c ipv6.c keepalive.c localport.c log_failed_transfers.c maxconnects.c multi-app.c multi-debugcallback.c multi-double.c multi-event.c multi-formadd.c multi-legacy.c multi-post.c multi-single.c multi-uv.c netrc.c parseurl.c persistent.c pop3-authzid.c pop3-dele.c pop3-list.c pop3-multi.c pop3-noop.c pop3-retr.c pop3-ssl.c pop3-stat.c pop3-tls.c pop3-top.c pop3-uidl.c post-callback.c postinmemory.c postit2-formadd.c postit2.c progressfunc.c protofeats.c range.c resolve.c rtsp-options.c sendrecv.c sepheaders.c sessioninfo.c sftpget.c sftpuploadresume.c shared-connection-cache.c simple.c simplepost.c simplessl.c smooth-gtk-thread.c smtp-authzid.c smtp-expn.c smtp-mail.c smtp-mime.c smtp-multi.c smtp-ssl.c smtp-tls.c smtp-vrfy.c sslbackend.c synctime.c threaded.c unixsocket.c url2file.c urlapi.c usercertinmem.c version-check.pl websocket-cb.c websocket-updown.c websocket.c xmlstream.c
internals BUFQ.md BUFREF.md CHECKSRC.md CLIENT-READERS.md CLIENT-WRITERS.md CODE_STYLE.md CONNECTION-FILTERS.md CREDENTIALS.md CURLX.md DYNBUF.md HASH.md LLIST.md MID.md MQTT.md MULTI-EV.md NEW-PROTOCOL.md PEERS.md PORTING.md RATELIMITS.md README.md SCORECARD.md SPLAY.md STRPARSE.md THRDPOOL-AND-QUEUE.md TIME-KEEPING.md TLS-SESSIONS.md UINT_SETS.md WEBSOCKET.md
libcurl
opts CMakeLists.txt CURLINFO_ACTIVESOCKET.md CURLINFO_APPCONNECT_TIME.md CURLINFO_APPCONNECT_TIME_T.md CURLINFO_CAINFO.md CURLINFO_CAPATH.md CURLINFO_CERTINFO.md CURLINFO_CONDITION_UNMET.md CURLINFO_CONNECT_TIME.md CURLINFO_CONNECT_TIME_T.md CURLINFO_CONN_ID.md CURLINFO_CONTENT_LENGTH_DOWNLOAD.md CURLINFO_CONTENT_LENGTH_DOWNLOAD_T.md CURLINFO_CONTENT_LENGTH_UPLOAD.md CURLINFO_CONTENT_LENGTH_UPLOAD_T.md CURLINFO_CONTENT_TYPE.md CURLINFO_COOKIELIST.md CURLINFO_EARLYDATA_SENT_T.md CURLINFO_EFFECTIVE_METHOD.md CURLINFO_EFFECTIVE_URL.md CURLINFO_FILETIME.md CURLINFO_FILETIME_T.md CURLINFO_FTP_ENTRY_PATH.md CURLINFO_HEADER_SIZE.md CURLINFO_HTTPAUTH_AVAIL.md CURLINFO_HTTPAUTH_USED.md CURLINFO_HTTP_CONNECTCODE.md CURLINFO_HTTP_VERSION.md CURLINFO_LASTSOCKET.md CURLINFO_LOCAL_IP.md CURLINFO_LOCAL_PORT.md CURLINFO_NAMELOOKUP_TIME.md CURLINFO_NAMELOOKUP_TIME_T.md CURLINFO_NUM_CONNECTS.md CURLINFO_OS_ERRNO.md CURLINFO_POSTTRANSFER_TIME_T.md CURLINFO_PRETRANSFER_TIME.md CURLINFO_PRETRANSFER_TIME_T.md CURLINFO_PRIMARY_IP.md CURLINFO_PRIMARY_PORT.md CURLINFO_PRIVATE.md CURLINFO_PROTOCOL.md CURLINFO_PROXYAUTH_AVAIL.md CURLINFO_PROXYAUTH_USED.md CURLINFO_PROXY_ERROR.md CURLINFO_PROXY_SSL_VERIFYRESULT.md CURLINFO_QUEUE_TIME_T.md CURLINFO_REDIRECT_COUNT.md CURLINFO_REDIRECT_TIME.md CURLINFO_REDIRECT_TIME_T.md CURLINFO_REDIRECT_URL.md CURLINFO_REFERER.md CURLINFO_REQUEST_SIZE.md CURLINFO_RESPONSE_CODE.md CURLINFO_RETRY_AFTER.md CURLINFO_RTSP_CLIENT_CSEQ.md CURLINFO_RTSP_CSEQ_RECV.md CURLINFO_RTSP_SERVER_CSEQ.md CURLINFO_RTSP_SESSION_ID.md CURLINFO_SCHEME.md CURLINFO_SIZE_DELIVERED.md CURLINFO_SIZE_DOWNLOAD.md CURLINFO_SIZE_DOWNLOAD_T.md CURLINFO_SIZE_UPLOAD.md CURLINFO_SIZE_UPLOAD_T.md CURLINFO_SPEED_DOWNLOAD.md CURLINFO_SPEED_DOWNLOAD_T.md CURLINFO_SPEED_UPLOAD.md CURLINFO_SPEED_UPLOAD_T.md CURLINFO_SSL_ENGINES.md CURLINFO_SSL_VERIFYRESULT.md CURLINFO_STARTTRANSFER_TIME.md CURLINFO_STARTTRANSFER_TIME_T.md CURLINFO_TLS_SESSION.md CURLINFO_TLS_SSL_PTR.md CURLINFO_TOTAL_TIME.md CURLINFO_TOTAL_TIME_T.md CURLINFO_USED_PROXY.md CURLINFO_XFER_ID.md CURLMINFO_XFERS_ADDED.md CURLMINFO_XFERS_CURRENT.md CURLMINFO_XFERS_DONE.md CURLMINFO_XFERS_PENDING.md CURLMINFO_XFERS_RUNNING.md CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE.md CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE.md CURLMOPT_MAXCONNECTS.md CURLMOPT_MAX_CONCURRENT_STREAMS.md CURLMOPT_MAX_HOST_CONNECTIONS.md CURLMOPT_MAX_PIPELINE_LENGTH.md CURLMOPT_MAX_TOTAL_CONNECTIONS.md CURLMOPT_NETWORK_CHANGED.md CURLMOPT_NOTIFYDATA.md CURLMOPT_NOTIFYFUNCTION.md CURLMOPT_PIPELINING.md CURLMOPT_PIPELINING_SERVER_BL.md CURLMOPT_PIPELINING_SITE_BL.md CURLMOPT_PUSHDATA.md CURLMOPT_PUSHFUNCTION.md CURLMOPT_QUICK_EXIT.md CURLMOPT_RESOLVE_THREADS_MAX.md CURLMOPT_SOCKETDATA.md CURLMOPT_SOCKETFUNCTION.md CURLMOPT_TIMERDATA.md CURLMOPT_TIMERFUNCTION.md CURLOPT_ABSTRACT_UNIX_SOCKET.md CURLOPT_ACCEPTTIMEOUT_MS.md CURLOPT_ACCEPT_ENCODING.md CURLOPT_ADDRESS_SCOPE.md CURLOPT_ALTSVC.md CURLOPT_ALTSVC_CTRL.md CURLOPT_APPEND.md CURLOPT_AUTOREFERER.md CURLOPT_AWS_SIGV4.md CURLOPT_BUFFERSIZE.md CURLOPT_CAINFO.md CURLOPT_CAINFO_BLOB.md CURLOPT_CAPATH.md CURLOPT_CA_CACHE_TIMEOUT.md CURLOPT_CERTINFO.md CURLOPT_CHUNK_BGN_FUNCTION.md CURLOPT_CHUNK_DATA.md CURLOPT_CHUNK_END_FUNCTION.md CURLOPT_CLOSESOCKETDATA.md CURLOPT_CLOSESOCKETFUNCTION.md CURLOPT_CONNECTTIMEOUT.md CURLOPT_CONNECTTIMEOUT_MS.md CURLOPT_CONNECT_ONLY.md CURLOPT_CONNECT_TO.md CURLOPT_CONV_FROM_NETWORK_FUNCTION.md CURLOPT_CONV_FROM_UTF8_FUNCTION.md CURLOPT_CONV_TO_NETWORK_FUNCTION.md CURLOPT_COOKIE.md CURLOPT_COOKIEFILE.md CURLOPT_COOKIEJAR.md CURLOPT_COOKIELIST.md CURLOPT_COOKIESESSION.md CURLOPT_COPYPOSTFIELDS.md CURLOPT_CRLF.md CURLOPT_CRLFILE.md CURLOPT_CURLU.md CURLOPT_CUSTOMREQUEST.md CURLOPT_DEBUGDATA.md CURLOPT_DEBUGFUNCTION.md CURLOPT_DEFAULT_PROTOCOL.md CURLOPT_DIRLISTONLY.md CURLOPT_DISALLOW_USERNAME_IN_URL.md CURLOPT_DNS_CACHE_TIMEOUT.md CURLOPT_DNS_INTERFACE.md CURLOPT_DNS_LOCAL_IP4.md CURLOPT_DNS_LOCAL_IP6.md CURLOPT_DNS_SERVERS.md CURLOPT_DNS_SHUFFLE_ADDRESSES.md CURLOPT_DNS_USE_GLOBAL_CACHE.md CURLOPT_DOH_SSL_VERIFYHOST.md CURLOPT_DOH_SSL_VERIFYPEER.md CURLOPT_DOH_SSL_VERIFYSTATUS.md CURLOPT_DOH_URL.md CURLOPT_ECH.md CURLOPT_EGDSOCKET.md CURLOPT_ERRORBUFFER.md CURLOPT_EXPECT_100_TIMEOUT_MS.md CURLOPT_FAILONERROR.md CURLOPT_FILETIME.md CURLOPT_FNMATCH_DATA.md CURLOPT_FNMATCH_FUNCTION.md CURLOPT_FOLLOWLOCATION.md CURLOPT_FORBID_REUSE.md CURLOPT_FRESH_CONNECT.md CURLOPT_FTPPORT.md CURLOPT_FTPSSLAUTH.md CURLOPT_FTP_ACCOUNT.md CURLOPT_FTP_ALTERNATIVE_TO_USER.md CURLOPT_FTP_CREATE_MISSING_DIRS.md CURLOPT_FTP_FILEMETHOD.md CURLOPT_FTP_SKIP_PASV_IP.md CURLOPT_FTP_SSL_CCC.md CURLOPT_FTP_USE_EPRT.md CURLOPT_FTP_USE_EPSV.md CURLOPT_FTP_USE_PRET.md CURLOPT_GSSAPI_DELEGATION.md CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS.md CURLOPT_HAPROXYPROTOCOL.md CURLOPT_HAPROXY_CLIENT_IP.md CURLOPT_HEADER.md CURLOPT_HEADERDATA.md CURLOPT_HEADERFUNCTION.md CURLOPT_HEADEROPT.md CURLOPT_HSTS.md CURLOPT_HSTSREADDATA.md CURLOPT_HSTSREADFUNCTION.md CURLOPT_HSTSWRITEDATA.md CURLOPT_HSTSWRITEFUNCTION.md CURLOPT_HSTS_CTRL.md CURLOPT_HTTP09_ALLOWED.md CURLOPT_HTTP200ALIASES.md CURLOPT_HTTPAUTH.md CURLOPT_HTTPGET.md CURLOPT_HTTPHEADER.md CURLOPT_HTTPPOST.md CURLOPT_HTTPPROXYTUNNEL.md CURLOPT_HTTP_CONTENT_DECODING.md CURLOPT_HTTP_TRANSFER_DECODING.md CURLOPT_HTTP_VERSION.md CURLOPT_IGNORE_CONTENT_LENGTH.md CURLOPT_INFILESIZE.md CURLOPT_INFILESIZE_LARGE.md CURLOPT_INTERFACE.md CURLOPT_INTERLEAVEDATA.md CURLOPT_INTERLEAVEFUNCTION.md CURLOPT_IOCTLDATA.md CURLOPT_IOCTLFUNCTION.md CURLOPT_IPRESOLVE.md CURLOPT_ISSUERCERT.md CURLOPT_ISSUERCERT_BLOB.md CURLOPT_KEEP_SENDING_ON_ERROR.md CURLOPT_KEYPASSWD.md CURLOPT_KRBLEVEL.md CURLOPT_LOCALPORT.md CURLOPT_LOCALPORTRANGE.md CURLOPT_LOGIN_OPTIONS.md CURLOPT_LOW_SPEED_LIMIT.md CURLOPT_LOW_SPEED_TIME.md CURLOPT_MAIL_AUTH.md CURLOPT_MAIL_FROM.md CURLOPT_MAIL_RCPT.md CURLOPT_MAIL_RCPT_ALLOWFAILS.md CURLOPT_MAXAGE_CONN.md CURLOPT_MAXCONNECTS.md CURLOPT_MAXFILESIZE.md CURLOPT_MAXFILESIZE_LARGE.md CURLOPT_MAXLIFETIME_CONN.md CURLOPT_MAXREDIRS.md CURLOPT_MAX_RECV_SPEED_LARGE.md CURLOPT_MAX_SEND_SPEED_LARGE.md CURLOPT_MIMEPOST.md CURLOPT_MIME_OPTIONS.md CURLOPT_NETRC.md CURLOPT_NETRC_FILE.md CURLOPT_NEW_DIRECTORY_PERMS.md CURLOPT_NEW_FILE_PERMS.md CURLOPT_NOBODY.md CURLOPT_NOPROGRESS.md CURLOPT_NOPROXY.md CURLOPT_NOSIGNAL.md CURLOPT_OPENSOCKETDATA.md CURLOPT_OPENSOCKETFUNCTION.md CURLOPT_PASSWORD.md CURLOPT_PATH_AS_IS.md CURLOPT_PINNEDPUBLICKEY.md CURLOPT_PIPEWAIT.md CURLOPT_PORT.md CURLOPT_POST.md CURLOPT_POSTFIELDS.md CURLOPT_POSTFIELDSIZE.md CURLOPT_POSTFIELDSIZE_LARGE.md CURLOPT_POSTQUOTE.md CURLOPT_POSTREDIR.md CURLOPT_PREQUOTE.md CURLOPT_PREREQDATA.md CURLOPT_PREREQFUNCTION.md CURLOPT_PRE_PROXY.md CURLOPT_PRIVATE.md CURLOPT_PROGRESSDATA.md CURLOPT_PROGRESSFUNCTION.md CURLOPT_PROTOCOLS.md CURLOPT_PROTOCOLS_STR.md CURLOPT_PROXY.md CURLOPT_PROXYAUTH.md CURLOPT_PROXYHEADER.md CURLOPT_PROXYPASSWORD.md CURLOPT_PROXYPORT.md CURLOPT_PROXYTYPE.md CURLOPT_PROXYUSERNAME.md CURLOPT_PROXYUSERPWD.md CURLOPT_PROXY_CAINFO.md CURLOPT_PROXY_CAINFO_BLOB.md CURLOPT_PROXY_CAPATH.md CURLOPT_PROXY_CRLFILE.md CURLOPT_PROXY_ISSUERCERT.md CURLOPT_PROXY_ISSUERCERT_BLOB.md CURLOPT_PROXY_KEYPASSWD.md CURLOPT_PROXY_PINNEDPUBLICKEY.md CURLOPT_PROXY_SERVICE_NAME.md CURLOPT_PROXY_SSLCERT.md CURLOPT_PROXY_SSLCERTTYPE.md CURLOPT_PROXY_SSLCERT_BLOB.md CURLOPT_PROXY_SSLKEY.md CURLOPT_PROXY_SSLKEYTYPE.md CURLOPT_PROXY_SSLKEY_BLOB.md CURLOPT_PROXY_SSLVERSION.md CURLOPT_PROXY_SSL_CIPHER_LIST.md CURLOPT_PROXY_SSL_OPTIONS.md CURLOPT_PROXY_SSL_VERIFYHOST.md CURLOPT_PROXY_SSL_VERIFYPEER.md CURLOPT_PROXY_TLS13_CIPHERS.md CURLOPT_PROXY_TLSAUTH_PASSWORD.md CURLOPT_PROXY_TLSAUTH_TYPE.md CURLOPT_PROXY_TLSAUTH_USERNAME.md CURLOPT_PROXY_TRANSFER_MODE.md CURLOPT_PUT.md CURLOPT_QUICK_EXIT.md CURLOPT_QUOTE.md CURLOPT_RANDOM_FILE.md CURLOPT_RANGE.md CURLOPT_READDATA.md CURLOPT_READFUNCTION.md CURLOPT_REDIR_PROTOCOLS.md CURLOPT_REDIR_PROTOCOLS_STR.md CURLOPT_REFERER.md CURLOPT_REQUEST_TARGET.md CURLOPT_RESOLVE.md CURLOPT_RESOLVER_START_DATA.md CURLOPT_RESOLVER_START_FUNCTION.md CURLOPT_RESUME_FROM.md CURLOPT_RESUME_FROM_LARGE.md CURLOPT_RTSP_CLIENT_CSEQ.md CURLOPT_RTSP_REQUEST.md CURLOPT_RTSP_SERVER_CSEQ.md CURLOPT_RTSP_SESSION_ID.md CURLOPT_RTSP_STREAM_URI.md CURLOPT_RTSP_TRANSPORT.md CURLOPT_SASL_AUTHZID.md CURLOPT_SASL_IR.md CURLOPT_SEEKDATA.md CURLOPT_SEEKFUNCTION.md CURLOPT_SERVER_RESPONSE_TIMEOUT.md CURLOPT_SERVER_RESPONSE_TIMEOUT_MS.md CURLOPT_SERVICE_NAME.md CURLOPT_SHARE.md CURLOPT_SOCKOPTDATA.md CURLOPT_SOCKOPTFUNCTION.md CURLOPT_SOCKS5_AUTH.md CURLOPT_SOCKS5_GSSAPI_NEC.md CURLOPT_SOCKS5_GSSAPI_SERVICE.md CURLOPT_SSH_AUTH_TYPES.md CURLOPT_SSH_COMPRESSION.md CURLOPT_SSH_HOSTKEYDATA.md CURLOPT_SSH_HOSTKEYFUNCTION.md CURLOPT_SSH_HOST_PUBLIC_KEY_MD5.md CURLOPT_SSH_HOST_PUBLIC_KEY_SHA256.md CURLOPT_SSH_KEYDATA.md CURLOPT_SSH_KEYFUNCTION.md CURLOPT_SSH_KNOWNHOSTS.md CURLOPT_SSH_PRIVATE_KEYFILE.md CURLOPT_SSH_PUBLIC_KEYFILE.md CURLOPT_SSLCERT.md CURLOPT_SSLCERTTYPE.md CURLOPT_SSLCERT_BLOB.md CURLOPT_SSLENGINE.md CURLOPT_SSLENGINE_DEFAULT.md CURLOPT_SSLKEY.md CURLOPT_SSLKEYTYPE.md CURLOPT_SSLKEY_BLOB.md CURLOPT_SSLVERSION.md CURLOPT_SSL_CIPHER_LIST.md CURLOPT_SSL_CTX_DATA.md CURLOPT_SSL_CTX_FUNCTION.md CURLOPT_SSL_EC_CURVES.md CURLOPT_SSL_ENABLE_ALPN.md CURLOPT_SSL_ENABLE_NPN.md CURLOPT_SSL_FALSESTART.md CURLOPT_SSL_OPTIONS.md CURLOPT_SSL_SESSIONID_CACHE.md CURLOPT_SSL_SIGNATURE_ALGORITHMS.md CURLOPT_SSL_VERIFYHOST.md CURLOPT_SSL_VERIFYPEER.md CURLOPT_SSL_VERIFYSTATUS.md CURLOPT_STDERR.md CURLOPT_STREAM_DEPENDS.md CURLOPT_STREAM_DEPENDS_E.md CURLOPT_STREAM_WEIGHT.md CURLOPT_SUPPRESS_CONNECT_HEADERS.md CURLOPT_TCP_FASTOPEN.md CURLOPT_TCP_KEEPALIVE.md CURLOPT_TCP_KEEPCNT.md CURLOPT_TCP_KEEPIDLE.md CURLOPT_TCP_KEEPINTVL.md CURLOPT_TCP_NODELAY.md CURLOPT_TELNETOPTIONS.md CURLOPT_TFTP_BLKSIZE.md CURLOPT_TFTP_NO_OPTIONS.md CURLOPT_TIMECONDITION.md CURLOPT_TIMEOUT.md CURLOPT_TIMEOUT_MS.md CURLOPT_TIMEVALUE.md CURLOPT_TIMEVALUE_LARGE.md CURLOPT_TLS13_CIPHERS.md CURLOPT_TLSAUTH_PASSWORD.md CURLOPT_TLSAUTH_TYPE.md CURLOPT_TLSAUTH_USERNAME.md CURLOPT_TRAILERDATA.md CURLOPT_TRAILERFUNCTION.md CURLOPT_TRANSFERTEXT.md CURLOPT_TRANSFER_ENCODING.md CURLOPT_UNIX_SOCKET_PATH.md CURLOPT_UNRESTRICTED_AUTH.md CURLOPT_UPKEEP_INTERVAL_MS.md CURLOPT_UPLOAD.md CURLOPT_UPLOAD_BUFFERSIZE.md CURLOPT_UPLOAD_FLAGS.md CURLOPT_URL.md CURLOPT_USERAGENT.md CURLOPT_USERNAME.md CURLOPT_USERPWD.md CURLOPT_USE_SSL.md CURLOPT_VERBOSE.md CURLOPT_WILDCARDMATCH.md CURLOPT_WRITEDATA.md CURLOPT_WRITEFUNCTION.md CURLOPT_WS_OPTIONS.md CURLOPT_XFERINFODATA.md CURLOPT_XFERINFOFUNCTION.md CURLOPT_XOAUTH2_BEARER.md CURLSHOPT_LOCKFUNC.md CURLSHOPT_SHARE.md CURLSHOPT_UNLOCKFUNC.md CURLSHOPT_UNSHARE.md CURLSHOPT_USERDATA.md Makefile.am Makefile.inc
.gitignore ABI.md CMakeLists.txt Makefile.am Makefile.inc curl_easy_cleanup.md curl_easy_duphandle.md curl_easy_escape.md curl_easy_getinfo.md curl_easy_header.md curl_easy_init.md curl_easy_nextheader.md curl_easy_option_by_id.md curl_easy_option_by_name.md curl_easy_option_next.md curl_easy_pause.md curl_easy_perform.md curl_easy_recv.md curl_easy_reset.md curl_easy_send.md curl_easy_setopt.md curl_easy_ssls_export.md curl_easy_ssls_import.md curl_easy_strerror.md curl_easy_unescape.md curl_easy_upkeep.md curl_escape.md curl_formadd.md curl_formfree.md curl_formget.md curl_free.md curl_getdate.md curl_getenv.md curl_global_cleanup.md curl_global_init.md curl_global_init_mem.md curl_global_sslset.md curl_global_trace.md curl_mime_addpart.md curl_mime_data.md curl_mime_data_cb.md curl_mime_encoder.md curl_mime_filedata.md curl_mime_filename.md curl_mime_free.md curl_mime_headers.md curl_mime_init.md curl_mime_name.md curl_mime_subparts.md curl_mime_type.md curl_mprintf.md curl_multi_add_handle.md curl_multi_assign.md curl_multi_cleanup.md curl_multi_fdset.md curl_multi_get_handles.md curl_multi_get_offt.md curl_multi_info_read.md curl_multi_init.md curl_multi_notify_disable.md curl_multi_notify_enable.md curl_multi_perform.md curl_multi_poll.md curl_multi_remove_handle.md curl_multi_setopt.md curl_multi_socket.md curl_multi_socket_action.md curl_multi_socket_all.md curl_multi_strerror.md curl_multi_timeout.md curl_multi_wait.md curl_multi_waitfds.md curl_multi_wakeup.md curl_pushheader_byname.md curl_pushheader_bynum.md curl_share_cleanup.md curl_share_init.md curl_share_setopt.md curl_share_strerror.md curl_slist_append.md curl_slist_free_all.md curl_strequal.md curl_strnequal.md curl_unescape.md curl_url.md curl_url_cleanup.md curl_url_dup.md curl_url_get.md curl_url_set.md curl_url_strerror.md curl_version.md curl_version_info.md curl_ws_meta.md curl_ws_recv.md curl_ws_send.md curl_ws_start_frame.md libcurl-easy.md libcurl-env-dbg.md libcurl-env.md libcurl-errors.md libcurl-multi.md libcurl-security.md libcurl-share.md libcurl-thread.md libcurl-tutorial.md libcurl-url.md libcurl-ws.md libcurl.m4 libcurl.md mksymbolsmanpage.pl symbols-in-versions symbols.pl
tests CI.md FILEFORMAT.md HTTP.md TEST-SUITE.md
.gitignore ALTSVC.md BINDINGS.md BUG-BOUNTY.md BUGS.md CIPHERS-TLS12.md CIPHERS.md CMakeLists.txt CODE_OF_CONDUCT.md CODE_REVIEW.md CONTRIBUTE.md CURL-DISABLE.md CURLDOWN.md DEPRECATE.md DISTROS.md EARLY-RELEASE.md ECH.md EXPERIMENTAL.md FAQ.md FEATURES.md GOVERNANCE.md HELP-US.md HISTORY.md HSTS.md HTTP-COOKIES.md HTTP3.md HTTPSRR.md INFRASTRUCTURE.md INSTALL-CMAKE.md INSTALL.md INTERNALS.md IPFS.md KNOWN_BUGS.md KNOWN_RISKS.md MAIL-ETIQUETTE.md MANUAL.md Makefile.am README.md RELEASE-PROCEDURE.md ROADMAP.md RUSTLS.md SECURITY-ADVISORY.md SPONSORS.md SSL-PROBLEMS.md SSLCERTS.md THANKS THANKS-filter TODO.md TheArtOfHttpScripting.md URL-SYNTAX.md VERIFY.md VERSIONS.md VULN-DISCLOSURE-POLICY.md curl-config.md mk-ca-bundle.md options-in-versions runtests.md testcurl.md wcurl.md
include
curl Makefile.am curl.h curlver.h easy.h header.h mprintf.h multi.h options.h stdcheaders.h system.h typecheck-gcc.h urlapi.h websockets.h
Makefile.am README.md
lib
curlx base64.c base64.h basename.c basename.h dynbuf.c dynbuf.h fopen.c fopen.h inet_ntop.c inet_ntop.h inet_pton.c inet_pton.h multibyte.c multibyte.h nonblock.c nonblock.h snprintf.c snprintf.h strcopy.c strcopy.h strdup.c strdup.h strerr.c strerr.h strparse.c strparse.h timediff.c timediff.h timeval.c timeval.h version_win32.c version_win32.h wait.c wait.h warnless.c warnless.h winapi.c winapi.h
vauth cleartext.c cram.c digest.c digest.h digest_sspi.c gsasl.c krb5_gssapi.c krb5_sspi.c ntlm.c ntlm_sspi.c oauth2.c spnego_gssapi.c spnego_sspi.c vauth.c vauth.h
vquic curl_ngtcp2.c curl_ngtcp2.h curl_quiche.c curl_quiche.h vquic-tls.c vquic-tls.h vquic.c vquic.h vquic_int.h
vssh libssh.c libssh2.c ssh.h vssh.c vssh.h
vtls apple.c apple.h cipher_suite.c cipher_suite.h gtls.c gtls.h hostcheck.c hostcheck.h keylog.c keylog.h mbedtls.c mbedtls.h openssl.c openssl.h rustls.c rustls.h schannel.c schannel.h schannel_int.h schannel_verify.c vtls.c vtls.h vtls_int.h vtls_scache.c vtls_scache.h vtls_spack.c vtls_spack.h wolfssl.c wolfssl.h x509asn1.c x509asn1.h
.gitignore CMakeLists.txt Makefile.am Makefile.inc Makefile.soname altsvc.c altsvc.h amigaos.c amigaos.h arpa_telnet.h asyn-ares.c asyn-base.c asyn-thrdd.c asyn.h bufq.c bufq.h bufref.c bufref.h cf-dns.c cf-dns.h cf-h1-proxy.c cf-h1-proxy.h cf-h2-proxy.c cf-h2-proxy.h cf-haproxy.c cf-haproxy.h cf-https-connect.c cf-https-connect.h cf-ip-happy.c cf-ip-happy.h cf-socket.c cf-socket.h cfilters.c cfilters.h config-mac.h config-os400.h config-riscos.h config-win32.h conncache.c conncache.h connect.c connect.h content_encoding.c content_encoding.h cookie.c cookie.h creds.c creds.h cshutdn.c cshutdn.h curl_addrinfo.c curl_addrinfo.h curl_config-cmake.h.in curl_ctype.h curl_endian.c curl_endian.h curl_fnmatch.c curl_fnmatch.h curl_fopen.c curl_fopen.h curl_get_line.c curl_get_line.h curl_gethostname.c curl_gethostname.h curl_gssapi.c curl_gssapi.h curl_hmac.h curl_ldap.h curl_md4.h curl_md5.h curl_memrchr.c curl_memrchr.h curl_ntlm_core.c curl_ntlm_core.h curl_printf.h curl_range.c curl_range.h curl_sasl.c curl_sasl.h curl_setup.h curl_sha256.h curl_sha512_256.c curl_sha512_256.h curl_share.c curl_share.h curl_sspi.c curl_sspi.h curl_threads.c curl_threads.h curl_trc.c curl_trc.h cw-out.c cw-out.h cw-pause.c cw-pause.h dict.c dict.h dllmain.c dnscache.c dnscache.h doh.c doh.h dynhds.c dynhds.h easy.c easy_lock.h easygetopt.c easyif.h easyoptions.c easyoptions.h escape.c escape.h fake_addrinfo.c fake_addrinfo.h file.c file.h fileinfo.c fileinfo.h formdata.c formdata.h ftp-int.h ftp.c ftp.h ftplistparser.c ftplistparser.h functypes.h getenv.c getinfo.c getinfo.h gopher.c gopher.h hash.c hash.h headers.c headers.h hmac.c hostip.c hostip.h hostip4.c hostip6.c hsts.c hsts.h http.c http.h http1.c http1.h http2.c http2.h http_aws_sigv4.c http_aws_sigv4.h http_chunks.c http_chunks.h http_digest.c http_digest.h http_negotiate.c http_negotiate.h http_ntlm.c http_ntlm.h http_proxy.c http_proxy.h httpsrr.c httpsrr.h idn.c idn.h if2ip.c if2ip.h imap.c imap.h ldap.c libcurl.def libcurl.rc libcurl.vers.in llist.c llist.h macos.c macos.h md4.c md5.c memdebug.c mime.c mime.h mprintf.c mqtt.c mqtt.h multi.c multi_ev.c multi_ev.h multi_ntfy.c multi_ntfy.h multihandle.h multiif.h netrc.c netrc.h noproxy.c noproxy.h openldap.c optiontable.pl parsedate.c parsedate.h peer.c peer.h pingpong.c pingpong.h pop3.c pop3.h progress.c progress.h protocol.c protocol.h psl.c psl.h rand.c rand.h ratelimit.c ratelimit.h request.c request.h rtsp.c rtsp.h select.c select.h sendf.c sendf.h setopt.c setopt.h setup-os400.h setup-vms.h setup-win32.h sha256.c sigpipe.h slist.c slist.h smb.c smb.h smtp.c smtp.h sockaddr.h socketpair.c socketpair.h socks.c socks.h socks_gssapi.c socks_sspi.c splay.c splay.h strcase.c strcase.h strequal.c strerror.c strerror.h system_win32.c system_win32.h telnet.c telnet.h tftp.c tftp.h thrdpool.c thrdpool.h thrdqueue.c thrdqueue.h transfer.c transfer.h uint-bset.c uint-bset.h uint-hash.c uint-hash.h uint-spbset.c uint-spbset.h uint-table.c uint-table.h url.c url.h urlapi-int.h urlapi.c urldata.h version.c ws.c ws.h
m4 .gitignore curl-amissl.m4 curl-apple-sectrust.m4 curl-compilers.m4 curl-confopts.m4 curl-functions.m4 curl-gnutls.m4 curl-mbedtls.m4 curl-openssl.m4 curl-override.m4 curl-reentrant.m4 curl-rustls.m4 curl-schannel.m4 curl-sysconfig.m4 curl-wolfssl.m4 xc-am-iface.m4 xc-cc-check.m4 xc-lt-iface.m4 xc-val-flgs.m4 zz40-xc-ovr.m4 zz50-xc-ovr.m4
projects
OS400
rpg-examples HEADERAPI HTTPPOST INMEMORY SIMPLE1 SIMPLE2 SMTPSRCMBR
.checksrc README.OS400 ccsidcurl.c ccsidcurl.h config400.default curl.cmd curl.inc.in curlcl.c curlmain.c initscript.sh make-docs.sh make-include.sh make-lib.sh make-src.sh make-tests.sh makefile.sh os400sys.c os400sys.h
Windows
tmpl .gitattributes README.txt curl-all.sln curl.sln curl.vcxproj curl.vcxproj.filters libcurl.sln libcurl.vcxproj libcurl.vcxproj.filters
.gitignore README.md generate.bat
vms Makefile.am backup_gnv_curl_src.com build_curl-config_script.com build_gnv_curl.com build_gnv_curl_pcsi_desc.com build_gnv_curl_pcsi_text.com build_gnv_curl_release_notes.com build_libcurl_pc.com build_vms.com clean_gnv_curl.com compare_curl_source.com config_h.com curl_crtl_init.c curl_gnv_build_steps.txt curl_release_note_start.txt curl_startup.com curlmsg.h curlmsg.msg curlmsg.sdl curlmsg_vms.h generate_config_vms_h_curl.com generate_vax_transfer.com gnv_conftest.c_first gnv_curl_configure.sh gnv_libcurl_symbols.opt gnv_link_curl.com macro32_exactcase.patch make_gnv_curl_install.sh make_pcsi_curl_kit_name.com pcsi_gnv_curl_file_list.txt pcsi_product_gnv_curl.com readme report_openssl_version.c setup_gnv_curl_build.com stage_curl_install.com vms_eco_level.h
Makefile.am README.md
scripts .checksrc CMakeLists.txt Makefile.am badwords badwords-all badwords.txt cd2cd cd2nroff cdall checksrc-all.pl checksrc.pl cmakelint.sh completion.pl contributors.sh contrithanks.sh coverage.sh delta dmaketgz extract-unit-protos firefox-db2pem.sh installcheck.sh maketgz managen mdlinkcheck mk-ca-bundle.pl mk-unity.pl nroff2cd perlcheck.sh pythonlint.sh randdisable release-notes.pl release-tools.sh schemetable.c singleuse.pl spacecheck.pl top-complexity top-length verify-release wcurl
src
toolx tool_time.c tool_time.h
.checksrc .gitignore CMakeLists.txt Makefile.am Makefile.inc config2setopts.c config2setopts.h curl.rc curlinfo.c mk-file-embed.pl mkhelp.pl slist_wc.c slist_wc.h terminal.c terminal.h tool_cb_dbg.c tool_cb_dbg.h tool_cb_hdr.c tool_cb_hdr.h tool_cb_prg.c tool_cb_prg.h tool_cb_rea.c tool_cb_rea.h tool_cb_see.c tool_cb_see.h tool_cb_soc.c tool_cb_soc.h tool_cb_wrt.c tool_cb_wrt.h tool_cfgable.c tool_cfgable.h tool_dirhie.c tool_dirhie.h tool_doswin.c tool_doswin.h tool_easysrc.c tool_easysrc.h tool_filetime.c tool_filetime.h tool_findfile.c tool_findfile.h tool_formparse.c tool_formparse.h tool_getparam.c tool_getparam.h tool_getpass.c tool_getpass.h tool_help.c tool_help.h tool_helpers.c tool_helpers.h tool_hugehelp.h tool_ipfs.c tool_ipfs.h tool_libinfo.c tool_libinfo.h tool_listhelp.c tool_main.c tool_main.h tool_msgs.c tool_msgs.h tool_operate.c tool_operate.h tool_operhlp.c tool_operhlp.h tool_paramhlp.c tool_paramhlp.h tool_parsecfg.c tool_parsecfg.h tool_progress.c tool_progress.h tool_sdecls.h tool_setopt.c tool_setopt.h tool_setup.h tool_ssls.c tool_ssls.h tool_stderr.c tool_stderr.h tool_urlglob.c tool_urlglob.h tool_util.c tool_util.h tool_version.h tool_vms.c tool_vms.h tool_writeout.c tool_writeout.h tool_writeout_json.c tool_writeout_json.h tool_xattr.c tool_xattr.h var.c var.h
tests
certs .gitignore CMakeLists.txt Makefile.am Makefile.inc genserv.pl srp-verifier-conf srp-verifier-db test-ca.cnf test-ca.prm test-client-cert.prm test-client-eku-only.prm test-localhost-san-first.prm test-localhost-san-last.prm test-localhost.nn.prm test-localhost.prm test-localhost0h.prm
cmake CMakeLists.txt test.c test.cpp test.sh
data .gitignore DISABLED Makefile.am data-xml1 data1400.c data1401.c data1402.c data1403.c data1404.c data1405.c data1406.c data1407.c data1420.c data1461.txt data1463.txt data1465.c data1481.c data1705-1.md data1705-2.md data1705-3.md data1705-4.md data1705-stdout.1 data1706-1.md data1706-2.md data1706-3.md data1706-4.md data1706-stdout.txt data320.html test1 test10 test100 test1000 test1001 test1002 test1003 test1004 test1005 test1006 test1007 test1008 test1009 test101 test1010 test1011 test1012 test1013 test1014 test1015 test1016 test1017 test1018 test1019 test102 test1020 test1021 test1022 test1023 test1024 test1025 test1026 test1027 test1028 test1029 test103 test1030 test1031 test1032 test1033 test1034 test1035 test1036 test1037 test1038 test1039 test104 test1040 test1041 test1042 test1043 test1044 test1045 test1046 test1047 test1048 test1049 test105 test1050 test1051 test1052 test1053 test1054 test1055 test1056 test1057 test1058 test1059 test106 test1060 test1061 test1062 test1063 test1064 test1065 test1066 test1067 test1068 test1069 test107 test1070 test1071 test1072 test1073 test1074 test1075 test1076 test1077 test1078 test1079 test108 test1080 test1081 test1082 test1083 test1084 test1085 test1086 test1087 test1088 test1089 test109 test1090 test1091 test1092 test1093 test1094 test1095 test1096 test1097 test1098 test1099 test11 test110 test1100 test1101 test1102 test1103 test1104 test1105 test1106 test1107 test1108 test1109 test111 test1110 test1111 test1112 test1113 test1114 test1115 test1116 test1117 test1118 test1119 test112 test1120 test1121 test1122 test1123 test1124 test1125 test1126 test1127 test1128 test1129 test113 test1130 test1131 test1132 test1133 test1134 test1135 test1136 test1137 test1138 test1139 test114 test1140 test1141 test1142 test1143 test1144 test1145 test1146 test1147 test1148 test1149 test115 test1150 test1151 test1152 test1153 test1154 test1155 test1156 test1157 test1158 test1159 test116 test1160 test1161 test1162 test1163 test1164 test1165 test1166 test1167 test1168 test1169 test117 test1170 test1171 test1172 test1173 test1174 test1175 test1176 test1177 test1178 test1179 test118 test1180 test1181 test1182 test1183 test1184 test1185 test1186 test1187 test1188 test1189 test119 test1190 test1191 test1192 test1193 test1194 test1195 test1196 test1197 test1198 test1199 test12 test120 test1200 test1201 test1202 test1203 test1204 test1205 test1206 test1207 test1208 test1209 test121 test1210 test1211 test1212 test1213 test1214 test1215 test1216 test1217 test1218 test1219 test122 test1220 test1221 test1222 test1223 test1224 test1225 test1226 test1227 test1228 test1229 test123 test1230 test1231 test1232 test1233 test1234 test1235 test1236 test1237 test1238 test1239 test124 test1240 test1241 test1242 test1243 test1244 test1245 test1246 test1247 test1248 test1249 test125 test1250 test1251 test1252 test1253 test1254 test1255 test1256 test1257 test1258 test1259 test126 test1260 test1261 test1262 test1263 test1264 test1265 test1266 test1267 test1268 test1269 test127 test1270 test1271 test1272 test1273 test1274 test1275 test1276 test1277 test1278 test1279 test128 test1280 test1281 test1282 test1283 test1284 test1285 test1286 test1287 test1288 test1289 test129 test1290 test1291 test1292 test1293 test1294 test1295 test1296 test1297 test1298 test1299 test13 test130 test1300 test1301 test1302 test1303 test1304 test1305 test1306 test1307 test1308 test1309 test131 test1310 test1311 test1312 test1313 test1314 test1315 test1316 test1317 test1318 test1319 test132 test1320 test1321 test1322 test1323 test1324 test1325 test1326 test1327 test1328 test1329 test133 test1330 test1331 test1332 test1333 test1334 test1335 test1336 test1337 test1338 test1339 test134 test1340 test1341 test1342 test1343 test1344 test1345 test1346 test1347 test1348 test1349 test135 test1350 test1351 test1352 test1353 test1354 test1355 test1356 test1357 test1358 test1359 test136 test1360 test1361 test1362 test1363 test1364 test1365 test1366 test1367 test1368 test1369 test137 test1370 test1371 test1372 test1373 test1374 test1375 test1376 test1377 test1378 test1379 test138 test1380 test1381 test1382 test1383 test1384 test1385 test1386 test1387 test1388 test1389 test139 test1390 test1391 test1392 test1393 test1394 test1395 test1396 test1397 test1398 test1399 test14 test140 test1400 test1401 test1402 test1403 test1404 test1405 test1406 test1407 test1408 test1409 test141 test1410 test1411 test1412 test1413 test1414 test1415 test1416 test1417 test1418 test1419 test142 test1420 test1421 test1422 test1423 test1424 test1425 test1426 test1427 test1428 test1429 test143 test1430 test1431 test1432 test1433 test1434 test1435 test1436 test1437 test1438 test1439 test144 test1440 test1441 test1442 test1443 test1444 test1445 test1446 test1447 test1448 test1449 test145 test1450 test1451 test1452 test1453 test1454 test1455 test1456 test1457 test1458 test1459 test146 test1460 test1461 test1462 test1463 test1464 test1465 test1466 test1467 test1468 test1469 test147 test1470 test1471 test1472 test1473 test1474 test1475 test1476 test1477 test1478 test1479 test148 test1480 test1481 test1482 test1483 test1484 test1485 test1486 test1487 test1488 test1489 test149 test1490 test1491 test1492 test1493 test1494 test1495 test1496 test1497 test1498 test1499 test15 test150 test1500 test1501 test1502 test1503 test1504 test1505 test1506 test1507 test1508 test1509 test151 test1510 test1511 test1512 test1513 test1514 test1515 test1516 test1517 test1518 test1519 test152 test1520 test1521 test1522 test1523 test1524 test1525 test1526 test1527 test1528 test1529 test153 test1530 test1531 test1532 test1533 test1534 test1535 test1536 test1537 test1538 test1539 test154 test1540 test1541 test1542 test1543 test1544 test1545 test1546 test1547 test1548 test1549 test155 test1550 test1551 test1552 test1553 test1554 test1555 test1556 test1557 test1558 test1559 test156 test1560 test1561 test1562 test1563 test1564 test1565 test1566 test1567 test1568 test1569 test157 test1570 test1571 test1572 test1573 test1574 test1575 test1576 test1577 test1578 test1579 test158 test1580 test1581 test1582 test1583 test1584 test1585 test1586 test1587 test1588 test1589 test159 test1590 test1591 test1592 test1593 test1594 test1595 test1596 test1597 test1598 test1599 test16 test160 test1600 test1601 test1602 test1603 test1604 test1605 test1606 test1607 test1608 test1609 test161 test1610 test1611 test1612 test1613 test1614 test1615 test1616 test1617 test1618 test1619 test162 test1620 test1621 test1622 test1623 test1624 test1625 test1626 test1627 test1628 test1629 test163 test1630 test1631 test1632 test1633 test1634 test1635 test1636 test1637 test1638 test1639 test164 test1640 test1641 test1642 test1643 test1644 test1645 test165 test1650 test1651 test1652 test1653 test1654 test1655 test1656 test1657 test1658 test1659 test166 test1660 test1661 test1662 test1663 test1664 test1665 test1666 test1667 test1668 test1669 test167 test1670 test1671 test1672 test1673 test1674 test1675 test1676 test168 test1680 test1681 test1682 test1683 test1684 test1685 test169 test17 test170 test1700 test1701 test1702 test1703 test1704 test1705 test1706 test1707 test1708 test1709 test171 test1710 test1711 test1712 test1713 test1714 test1715 test172 test1720 test1721 test173 test174 test175 test176 test177 test178 test179 test18 test180 test1800 test1801 test1802 test181 test182 test183 test184 test1847 test1848 test1849 test185 test1850 test1851 test186 test187 test188 test189 test19 test190 test1900 test1901 test1902 test1903 test1904 test1905 test1906 test1907 test1908 test1909 test191 test1910 test1911 test1912 test1913 test1914 test1915 test1916 test1917 test1918 test1919 test192 test1920 test1921 test193 test1933 test1934 test1935 test1936 test1937 test1938 test1939 test194 test1940 test1941 test1942 test1943 test1944 test1945 test1946 test1947 test1948 test195 test1955 test1956 test1957 test1958 test1959 test196 test1960 test1964 test1965 test1966 test197 test1970 test1971 test1972 test1973 test1974 test1975 test1976 test1977 test1978 test1979 test198 test1980 test1981 test1982 test1983 test1984 test199 test2 test20 test200 test2000 test2001 test2002 test2003 test2004 test2005 test2006 test2007 test2008 test2009 test201 test2010 test2011 test2012 test2013 test2014 test202 test2023 test2024 test2025 test2026 test2027 test2028 test2029 test203 test2030 test2031 test2032 test2033 test2034 test2035 test2037 test2038 test2039 test204 test2040 test2041 test2042 test2043 test2044 test2045 test2046 test2047 test2048 test2049 test205 test2050 test2051 test2052 test2053 test2054 test2055 test2056 test2057 test2058 test2059 test206 test2060 test2061 test2062 test2063 test2064 test2065 test2066 test2067 test2068 test2069 test207 test2070 test2071 test2072 test2073 test2074 test2075 test2076 test2077 test2078 test2079 test208 test2080 test2081 test2082 test2083 test2084 test2085 test2086 test2087 test2088 test2089 test209 test2090 test2091 test2092 test21 test210 test2100 test2101 test2102 test2103 test2104 test211 test212 test213 test214 test215 test216 test217 test218 test219 test22 test220 test2200 test2201 test2202 test2203 test2204 test2205 test2206 test2207 test221 test222 test223 test224 test225 test226 test227 test228 test229 test23 test230 test2300 test2301 test2302 test2303 test2304 test2306 test2307 test2308 test2309 test231 test232 test233 test234 test235 test236 test237 test238 test239 test24 test240 test2400 test2401 test2402 test2403 test2404 test2405 test2406 test2407 test2408 test2409 test241 test2410 test2411 test242 test243 test244 test245 test246 test247 test248 test249 test25 test250 test2500 test2501 test2502 test2503 test2504 test2505 test2506 test251 test252 test253 test254 test255 test256 test257 test258 test259 test26 test260 test2600 test2601 test2602 test2603 test2604 test2605 test261 test262 test263 test264 test265 test266 test267 test268 test269 test27 test270 test2700 test2701 test2702 test2703 test2704 test2705 test2706 test2707 test2708 test2709 test271 test2710 test2711 test2712 test2713 test2714 test2715 test2716 test2717 test2718 test2719 test272 test2720 test2721 test2722 test2723 test273 test274 test275 test276 test277 test278 test279 test28 test280 test281 test282 test283 test284 test285 test286 test287 test288 test289 test29 test290 test291 test292 test293 test294 test295 test296 test297 test298 test299 test3 test30 test300 test3000 test3001 test3002 test3003 test3004 test3005 test3006 test3007 test3008 test3009 test301 test3010 test3011 test3012 test3013 test3014 test3015 test3016 test3017 test3018 test3019 test302 test3020 test3021 test3022 test3023 test3024 test3025 test3026 test3027 test3028 test3029 test303 test3030 test3031 test3032 test3033 test3034 test3035 test3036 test304 test305 test306 test307 test308 test309 test31 test310 test3100 test3101 test3102 test3103 test3104 test3105 test3106 test311 test312 test313 test314 test315 test316 test317 test318 test319 test32 test320 test3200 test3201 test3202 test3203 test3204 test3205 test3206 test3207 test3208 test3209 test321 test3210 test3211 test3212 test3213 test3214 test3215 test3216 test3217 test3218 test3219 test322 test3220 test323 test324 test325 test326 test327 test328 test329 test33 test330 test3300 test3301 test3302 test331 test332 test333 test334 test335 test336 test337 test338 test339 test34 test340 test341 test342 test343 test344 test345 test346 test347 test348 test349 test35 test350 test351 test352 test353 test354 test355 test356 test357 test358 test359 test36 test360 test361 test362 test363 test364 test365 test366 test367 test368 test369 test37 test370 test371 test372 test373 test374 test375 test376 test378 test379 test38 test380 test381 test383 test384 test385 test386 test387 test388 test389 test39 test390 test391 test392 test393 test394 test395 test396 test397 test398 test399 test4 test40 test400 test4000 test4001 test401 test402 test403 test404 test405 test406 test407 test408 test409 test41 test410 test411 test412 test413 test414 test415 test416 test417 test418 test419 test42 test420 test421 test422 test423 test424 test425 test426 test427 test428 test429 test43 test430 test431 test432 test433 test434 test435 test436 test437 test438 test439 test44 test440 test441 test442 test443 test444 test445 test446 test447 test448 test449 test45 test450 test451 test452 test453 test454 test455 test456 test457 test458 test459 test46 test460 test461 test462 test463 test467 test468 test469 test47 test470 test471 test472 test473 test474 test475 test476 test477 test478 test479 test48 test480 test481 test482 test483 test484 test485 test486 test487 test488 test489 test49 test490 test491 test492 test493 test494 test495 test496 test497 test498 test499 test5 test50 test500 test501 test502 test503 test504 test505 test506 test507 test508 test509 test51 test510 test511 test512 test513 test514 test515 test516 test517 test518 test519 test52 test520 test521 test522 test523 test524 test525 test526 test527 test528 test529 test53 test530 test531 test532 test533 test534 test535 test536 test537 test538 test539 test54 test540 test541 test542 test543 test544 test545 test546 test547 test548 test549 test55 test550 test551 test552 test553 test554 test555 test556 test557 test558 test559 test56 test560 test561 test562 test563 test564 test565 test566 test567 test568 test569 test57 test570 test571 test572 test573 test574 test575 test576 test577 test578 test579 test58 test580 test581 test582 test583 test584 test585 test586 test587 test588 test589 test59 test590 test591 test592 test593 test594 test595 test596 test597 test598 test599 test6 test60 test600 test601 test602 test603 test604 test605 test606 test607 test608 test609 test61 test610 test611 test612 test613 test614 test615 test616 test617 test618 test619 test62 test620 test621 test622 test623 test624 test625 test626 test627 test628 test629 test63 test630 test631 test632 test633 test634 test635 test636 test637 test638 test639 test64 test640 test641 test642 test643 test644 test645 test646 test647 test648 test649 test65 test650 test651 test652 test653 test654 test655 test656 test658 test659 test66 test660 test661 test662 test663 test664 test665 test666 test667 test668 test669 test67 test670 test671 test672 test673 test674 test675 test676 test677 test678 test679 test68 test680 test681 test682 test683 test684 test685 test686 test687 test688 test689 test69 test690 test691 test692 test693 test694 test695 test696 test697 test698 test699 test7 test70 test700 test701 test702 test703 test704 test705 test706 test707 test708 test709 test71 test710 test711 test712 test713 test714 test715 test716 test717 test718 test719 test72 test720 test721 test722 test723 test724 test725 test726 test727 test728 test729 test73 test730 test731 test732 test733 test734 test735 test736 test737 test738 test739 test74 test740 test741 test742 test743 test744 test745 test746 test747 test748 test749 test75 test750 test751 test752 test753 test754 test755 test756 test757 test758 test759 test76 test760 test761 test762 test763 test764 test765 test766 test767 test768 test769 test77 test770 test771 test772 test773 test774 test775 test776 test777 test778 test779 test78 test780 test781 test782 test783 test784 test785 test786 test787 test788 test789 test79 test790 test791 test792 test793 test794 test795 test796 test797 test798 test799 test8 test80 test800 test801 test802 test803 test804 test805 test806 test807 test808 test809 test81 test810 test811 test812 test813 test814 test815 test816 test817 test818 test819 test82 test820 test821 test822 test823 test824 test825 test826 test827 test828 test829 test83 test830 test831 test832 test833 test834 test835 test836 test837 test838 test839 test84 test840 test841 test842 test843 test844 test845 test846 test847 test848 test849 test85 test850 test851 test852 test853 test854 test855 test856 test857 test858 test859 test86 test860 test861 test862 test863 test864 test865 test866 test867 test868 test869 test87 test870 test871 test872 test873 test874 test875 test876 test877 test878 test879 test88 test880 test881 test882 test883 test884 test885 test886 test887 test888 test889 test89 test890 test891 test892 test893 test894 test895 test896 test897 test898 test899 test9 test90 test900 test901 test902 test903 test904 test905 test906 test907 test908 test909 test91 test910 test911 test912 test913 test914 test915 test916 test917 test918 test919 test92 test920 test921 test922 test923 test924 test925 test926 test927 test928 test929 test93 test930 test931 test932 test933 test934 test935 test936 test937 test938 test939 test94 test940 test941 test942 test943 test944 test945 test946 test947 test948 test949 test95 test950 test951 test952 test953 test954 test955 test956 test957 test958 test959 test96 test960 test961 test962 test963 test964 test965 test966 test967 test968 test969 test97 test970 test971 test972 test973 test974 test975 test976 test977 test978 test979 test98 test980 test981 test982 test983 test984 test985 test986 test987 test988 test989 test99 test990 test991 test992 test993 test994 test995 test996 test997 test998 test999
http
testenv
mod_curltest .gitignore mod_curltest.c
__init__.py caddy.py certs.py client.py curl.py dante.py dnsd.py env.py httpd.py nghttpx.py ports.py sshd.py vsftpd.py ws_echo_server.py
.gitignore CMakeLists.txt Makefile.am config.ini.in conftest.py requirements.txt scorecard.py test_01_basic.py test_02_download.py test_03_goaway.py test_04_stuttered.py test_05_errors.py test_06_eyeballs.py test_07_upload.py test_08_caddy.py test_09_push.py test_10_proxy.py test_11_unix.py test_12_reuse.py test_13_proxy_auth.py test_14_auth.py test_15_tracing.py test_16_info.py test_17_ssl_use.py test_18_methods.py test_19_shutdown.py test_20_websockets.py test_21_resolve.py test_22_httpsrr.py test_30_vsftpd.py test_31_vsftpds.py test_32_ftps_vsftpd.py test_40_socks.py test_50_scp.py test_51_sftp.py
libtest .gitignore CMakeLists.txt Makefile.am Makefile.inc cli_ftp_upload.c cli_h2_pausing.c cli_h2_serverpush.c cli_h2_upgrade_extreme.c cli_hx_download.c cli_hx_upload.c cli_tls_session_reuse.c cli_upload_pausing.c cli_ws_data.c cli_ws_pingpong.c first.c first.h lib1156.c lib1301.c lib1308.c lib1485.c lib1500.c lib1501.c lib1502.c lib1506.c lib1507.c lib1508.c lib1509.c lib1510.c lib1511.c lib1512.c lib1513.c lib1514.c lib1515.c lib1517.c lib1518.c lib1520.c lib1522.c lib1523.c lib1525.c lib1526.c lib1527.c lib1528.c lib1529.c lib1530.c lib1531.c lib1532.c lib1533.c lib1534.c lib1535.c lib1536.c lib1537.c lib1538.c lib1540.c lib1541.c lib1542.c lib1545.c lib1549.c lib1550.c lib1551.c lib1552.c lib1553.c lib1554.c lib1555.c lib1556.c lib1557.c lib1558.c lib1559.c lib1560.c lib1564.c lib1565.c lib1567.c lib1568.c lib1569.c lib1571.c lib1576.c lib1582.c lib1587.c lib1588.c lib1589.c lib1591.c lib1592.c lib1593.c lib1594.c lib1597.c lib1598.c lib1599.c lib1662.c lib1900.c lib1901.c lib1902.c lib1903.c lib1905.c lib1906.c lib1907.c lib1908.c lib1910.c lib1911.c lib1912.c lib1913.c lib1915.c lib1916.c lib1918.c lib1919.c lib1920.c lib1921.c lib1933.c lib1934.c lib1935.c lib1936.c lib1937.c lib1938.c lib1939.c lib1940.c lib1945.c lib1947.c lib1948.c lib1955.c lib1956.c lib1957.c lib1958.c lib1959.c lib1960.c lib1964.c lib1965.c lib1970.c lib1971.c lib1972.c lib1973.c lib1974.c lib1975.c lib1977.c lib1978.c lib2023.c lib2032.c lib2082.c lib2301.c lib2302.c lib2304.c lib2306.c lib2308.c lib2309.c lib2402.c lib2404.c lib2405.c lib2502.c lib2504.c lib2505.c lib2506.c lib2700.c lib3010.c lib3025.c lib3026.c lib3027.c lib3033.c lib3034.c lib3100.c lib3101.c lib3102.c lib3103.c lib3104.c lib3105.c lib3207.c lib3208.c lib500.c lib501.c lib502.c lib503.c lib504.c lib505.c lib506.c lib507.c lib508.c lib509.c lib510.c lib511.c lib512.c lib513.c lib514.c lib515.c lib516.c lib517.c lib518.c lib519.c lib520.c lib521.c lib523.c lib524.c lib525.c lib526.c lib530.c lib533.c lib536.c lib537.c lib539.c lib540.c lib541.c lib542.c lib543.c lib544.c lib547.c lib549.c lib552.c lib553.c lib554.c lib555.c lib556.c lib557.c lib558.c lib559.c lib560.c lib562.c lib564.c lib566.c lib567.c lib568.c lib569.c lib570.c lib571.c lib572.c lib573.c lib574.c lib575.c lib576.c lib578.c lib579.c lib582.c lib583.c lib586.c lib589.c lib590.c lib591.c lib597.c lib598.c lib599.c lib643.c lib650.c lib651.c lib652.c lib653.c lib654.c lib655.c lib658.c lib659.c lib661.c lib666.c lib667.c lib668.c lib670.c lib674.c lib676.c lib677.c lib678.c lib694.c lib695.c lib751.c lib753.c lib757.c lib758.c lib766.c memptr.c mk-lib1521.pl test1013.pl test1022.pl test307.pl test610.pl test613.pl testtrace.c testtrace.h testutil.c testutil.h unitcheck.h
server .checksrc .gitignore CMakeLists.txt Makefile.am Makefile.inc dnsd.c first.c first.h getpart.c mqttd.c resolve.c rtspd.c sockfilt.c socksd.c sws.c tftpd.c util.c
tunit .gitignore CMakeLists.txt Makefile.am Makefile.inc README.md tool1394.c tool1604.c tool1621.c tool1622.c tool1623.c tool1720.c
unit .gitignore CMakeLists.txt Makefile.am Makefile.inc README.md unit1300.c unit1302.c unit1303.c unit1304.c unit1305.c unit1307.c unit1309.c unit1323.c unit1330.c unit1395.c unit1396.c unit1397.c unit1398.c unit1399.c unit1600.c unit1601.c unit1602.c unit1603.c unit1605.c unit1606.c unit1607.c unit1608.c unit1609.c unit1610.c unit1611.c unit1612.c unit1614.c unit1615.c unit1616.c unit1620.c unit1625.c unit1626.c unit1627.c unit1636.c unit1650.c unit1651.c unit1652.c unit1653.c unit1654.c unit1655.c unit1656.c unit1657.c unit1658.c unit1660.c unit1661.c unit1663.c unit1664.c unit1666.c unit1667.c unit1668.c unit1669.c unit1674.c unit1675.c unit1676.c unit1979.c unit1980.c unit2600.c unit2601.c unit2602.c unit2603.c unit2604.c unit2605.c unit3200.c unit3205.c unit3211.c unit3212.c unit3213.c unit3214.c unit3216.c unit3219.c unit3300.c unit3301.c unit3302.c
.gitignore CMakeLists.txt Makefile.am allversions.pm appveyor.pm azure.pm config.in configurehelp.pm.in devtest.pl dictserver.py directories.pm ech_combos.py ech_tests.sh ftpserver.pl getpart.pm globalconfig.pm http-server.pl http2-server.pl http3-server.pl memanalyze.pl memanalyzer.pm negtelnetserver.py nghttpx.conf pathhelp.pm processhelp.pm requirements.txt rtspserver.pl runner.pm runtests.pl secureserver.pl serverhelp.pm servers.pm smbserver.py sshhelp.pm sshserver.pl test1119.pl test1135.pl test1139.pl test1140.pl test1165.pl test1167.pl test1173.pl test1175.pl test1177.pl test1222.pl test1275.pl test1276.pl test1477.pl test1486.pl test1488.pl test1544.pl test1707.pl test745.pl test971.pl testcurl.pl testutil.pm tftpserver.pl util.py valgrind.pm valgrind.supp
.clang-tidy.yml .dir-locals.el .editorconfig .git-blame-ignore-revs .gitattributes .gitignore .mailmap CHANGES.md CMakeLists.txt COPYING Dockerfile GIT-INFO.md Makefile.am README README.md RELEASE-NOTES REUSE.toml SECURITY.md acinclude.m4 appveyor.sh appveyor.yml configure.ac curl-config.in libcurl.pc.in renovate.json
examples .env config.ini crypto_test.lua env_test.lua fs_example.lua http_server.lua https_test.lua ini_example.lua json.lua log.lua path_fs_example.lua process_example.lua request_download.lua request_test.lua run_all.lua sqlite_example.lua sqlite_http_template.lua stash_test.lua template_test.lua timer.lua websocket.lua
iniparser
.github
ISSUE_TEMPLATE config.yml
workflows disable-pull-requests.yml trigger-gitlab-ci.yml
cmake JoinPaths.cmake config.cmake.in pc.in
example iniexample.c iniwrite.c parse.c twisted-errors.ini twisted-genhuge.py twisted-ofkey.ini twisted-ofval.ini twisted.ini
src dictionary.c dictionary.h iniparser.c iniparser.h
test
ressources
bad_ini ends_well.ini twisted-errors.ini twisted-ofkey.ini twisted-ofval.ini
good_ini empty.ini spaced.ini spaced2.ini twisted.ini
gruezi.ini old.ini quotes.ini utf8.ini
CMakeLists.txt test_dictionary.c test_iniparser.c unity-config.yml unity_config.h
.cmake-format.py .gitignore .gitlab-ci.yml .gitmessage .travis.yml AUTHORS CMakeLists.txt FAQ-en.md FAQ-zhcn.md INSTALL LICENSE README.md compile_commands.json
jinjac
example CMakeLists.txt example.c
jinjac_test_app CMakeLists.txt jinjac_test_app.c
libjinjac
include jinjac.h
src CMakeLists.txt ast.c ast.h block_statement.c block_statement.h buffer.c buffer.h buildin.c buildin.h common.h convert.c convert.h flex_decl.h jfunction.c jfunction.h jinja_expression.l jinja_expression.y jinjac_parse.c jinjac_parse.h jinjac_stream.c jinjac_stream.h jlist.c jlist.h jobject.c jobject.h parameter.c parameter.h str_obj.c str_obj.h trace.c trace.h
CMakeLists.txt
test .gitignore CMakeLists.txt autotest.rb test_01.expected test_01.jinja test_01b.expected test_01b.jinja test_01c.expected test_01c.jinja test_01d.expected test_01d.jinja test_02.expected test_02.jinja test_03.expected test_03.jinja test_04.expected test_04.jinja test_05.expected test_05.jinja test_06.expected test_06.jinja test_07.expected test_07.jinja test_08.expected test_08.jinja test_08b.expected test_08b.jinja test_09.expected test_09.jinja test_10.expected test_10.jinja test_11.expected test_11.jinja test_12.expected test_12.jinja test_13.expected test_13.jinja test_14.expected test_14.jinja test_15.expected test_15.jinja test_16.expected test_16.jinja test_17.expected test_17.jinja test_18.expected test_18.jinja test_18b.expected test_18b.jinja test_18c.expected test_18c.jinja test_19.expected test_19.jinja test_19b.expected test_19b.jinja test_19c.expected test_19c.jinja test_19d.expected test_19d.jinja test_19e.expected test_19e.jinja test_19f.expected test_19f.jinja test_20.expected test_20.jinja test_21.expected test_21.jinja test_22.expected test_22.jinja test_22a.expected test_22a.jinja test_22b.expected test_22b.jinja test_23.expected test_23.jinja test_24.expected test_24.jinja
.gitignore CMakeLists.txt LICENSE.txt README.md build_coverage.sh build_debug.sh build_release.sh cppcheck_analysis.sh
libev Changes LICENSE Makefile Makefile.am Makefile.in README Symbols.ev Symbols.event aclocal.m4 autogen.sh compile config.guess config.h config.h.in config.status config.sub configure configure.ac depcomp ev++.h ev.3 ev.c ev.h ev.pod ev_epoll.c ev_kqueue.c ev_poll.c ev_port.c ev_select.c ev_vars.h ev_win32.c ev_wrap.h event.c event.h install-sh libev.m4 libtool ltmain.sh missing mkinstalldirs stamp-h1
luajit
doc
img contact.png
bluequad-print.css bluequad.css contact.html ext_buffer.html ext_c_api.html ext_ffi.html ext_ffi_api.html ext_ffi_semantics.html ext_ffi_tutorial.html ext_jit.html ext_profiler.html extensions.html install.html luajit.html running.html
dynasm dasm_arm.h dasm_arm.lua dasm_arm64.h dasm_arm64.lua dasm_mips.h dasm_mips.lua dasm_mips64.lua dasm_ppc.h dasm_ppc.lua dasm_proto.h dasm_x64.lua dasm_x86.h dasm_x86.lua dynasm.lua
etc luajit.1 luajit.pc
src
host .gitignore README buildvm.c buildvm.h buildvm_asm.c buildvm_fold.c buildvm_lib.c buildvm_libbc.h buildvm_peobj.c genlibbc.lua genminilua.lua genversion.lua minilua.c
jit .gitignore bc.lua bcsave.lua dis_arm.lua dis_arm64.lua dis_arm64be.lua dis_mips.lua dis_mips64.lua dis_mips64el.lua dis_mips64r6.lua dis_mips64r6el.lua dis_mipsel.lua dis_ppc.lua dis_x64.lua dis_x86.lua dump.lua p.lua v.lua zone.lua
.gitignore Makefile Makefile.dep lauxlib.h lib_aux.c lib_base.c lib_bit.c lib_buffer.c lib_debug.c lib_ffi.c lib_init.c lib_io.c lib_jit.c lib_math.c lib_os.c lib_package.c lib_string.c lib_table.c lj_alloc.c lj_alloc.h lj_api.c lj_arch.h lj_asm.c lj_asm.h lj_asm_arm.h lj_asm_arm64.h lj_asm_mips.h lj_asm_ppc.h lj_asm_x86.h lj_assert.c lj_bc.c lj_bc.h lj_bcdump.h lj_bcread.c lj_bcwrite.c lj_buf.c lj_buf.h lj_carith.c lj_carith.h lj_ccall.c lj_ccall.h lj_ccallback.c lj_ccallback.h lj_cconv.c lj_cconv.h lj_cdata.c lj_cdata.h lj_char.c lj_char.h lj_clib.c lj_clib.h lj_cparse.c lj_cparse.h lj_crecord.c lj_crecord.h lj_ctype.c lj_ctype.h lj_debug.c lj_debug.h lj_def.h lj_dispatch.c lj_dispatch.h lj_emit_arm.h lj_emit_arm64.h lj_emit_mips.h lj_emit_ppc.h lj_emit_x86.h lj_err.c lj_err.h lj_errmsg.h lj_ff.h lj_ffrecord.c lj_ffrecord.h lj_frame.h lj_func.c lj_func.h lj_gc.c lj_gc.h lj_gdbjit.c lj_gdbjit.h lj_ir.c lj_ir.h lj_ircall.h lj_iropt.h lj_jit.h lj_lex.c lj_lex.h lj_lib.c lj_lib.h lj_load.c lj_mcode.c lj_mcode.h lj_meta.c lj_meta.h lj_obj.c lj_obj.h lj_opt_dce.c lj_opt_fold.c lj_opt_loop.c lj_opt_mem.c lj_opt_narrow.c lj_opt_sink.c lj_opt_split.c lj_parse.c lj_parse.h lj_prng.c lj_prng.h lj_profile.c lj_profile.h lj_record.c lj_record.h lj_serialize.c lj_serialize.h lj_snap.c lj_snap.h lj_state.c lj_state.h lj_str.c lj_str.h lj_strfmt.c lj_strfmt.h lj_strfmt_num.c lj_strscan.c lj_strscan.h lj_tab.c lj_tab.h lj_target.h lj_target_arm.h lj_target_arm64.h lj_target_mips.h lj_target_ppc.h lj_target_x86.h lj_trace.c lj_trace.h lj_traceerr.h lj_udata.c lj_udata.h lj_vm.h lj_vmevent.c lj_vmevent.h lj_vmmath.c ljamalg.c lua.h lua.hpp luaconf.h luajit.c luajit_rolling.h lualib.h msvcbuild.bat nxbuild.bat ps4build.bat ps5build.bat psvitabuild.bat vm_arm.dasc vm_arm64.dasc vm_mips.dasc vm_mips64.dasc vm_ppc.dasc vm_x64.dasc vm_x86.dasc xb1build.bat xedkbuild.bat
.gitattributes .gitignore .relver COPYRIGHT Makefile README
sqlite shell.c sqlite3.c sqlite3.h sqlite3ext.h
wolfssl
.github
ISSUE_TEMPLATE bug_report.yaml other.yaml
actions
install-apt-deps action.yml
scripts
zephyr-4.x external_libc.conf zephyr-test.sh
openssl-ech.sh tls-anvil-test.sh
workflows
disabled haproxy.yml hitch.yml hostap.yml
hostap-files
configs
07c9f183ea744ac04585fb6dd10220c75a5e2e74 hostapd.config tests wpa_supplicant.config
b607d2723e927a3446d89aed813f1aa6068186bb hostapd.config tests wpa_supplicant.config
hostap_2_10 extra.patch hostapd.config tests wpa_supplicant.config
Makefile README dbus-wpa_supplicant.conf
ada.yml arduino.yml async-examples.yml async.yml atecc608-sim.yml bind.yml cmake-autoconf.yml cmake.yml codespell.yml coverity-scan-fixes.yml cryptocb-only.yml curl.yml cyrus-sasl.yml disable-pk-algs.yml docker-Espressif.yml docker-OpenWrt.yml emnet-nonblock.yml fil-c.yml freertos-mem-track.yml gencertbuf.yml grpc.yml haproxy.yml hostap-vm.yml intelasm-c-fallback.yml ipmitool.yml jwt-cpp.yml krb5.yml libspdm.yml libssh2.yml libvncserver.yml linuxkm.yml macos-apple-native-cert-validation.yml mbedtls.sh mbedtls.yml membrowse-comment.yml membrowse-onboard.yml membrowse-report.yml memcached.sh memcached.yml mono.yml mosquitto.yml msmtp.yml msys2.yml multi-arch.yml multi-compiler.yml net-snmp.yml nginx.yml no-malloc.yml no-tls.yml nss.sh nss.yml ntp.yml ocsp.yml openldap.yml openssh.yml openssl-ech.yml opensslcoexist.yml openvpn.yml os-check.yml packaging.yml pam-ipmi.yml pq-all.yml pr-commit-check.yml psk.yml puf.yml python.yml rng-tools.yml rust-wrapper.yml se050-sim.yml smallStackSize.yml socat.yml softhsm.yml sssd.yml stm32-sim.yml stsafe-a120-sim.yml stunnel.yml symbol-prefixes.yml threadx.yml tls-anvil.yml trackmemory.yml watcomc.yml win-csharp-test.yml wolfCrypt-Wconversion.yml wolfboot-integration.yml wolfsm.yml xcode.yml zephyr-4.x.yml zephyr.yml
PULL_REQUEST_TEMPLATE.md SECURITY.md membrowse-targets.json
Docker
OpenWrt Dockerfile README.md runTests.sh
packaging
debian Dockerfile
fedora Dockerfile
wolfCLU Dockerfile README.md
yocto Dockerfile buildAndPush.sh
Dockerfile Dockerfile.cross-compiler README.md buildAndPush.sh include.am run.sh
IDE
ARDUINO
sketches
wolfssl_client README.md
wolfssl_server README.md
wolfssl_version README.md
README.md
Arduino_README_prepend.md README.md include.am keywords.txt library.properties.template wolfssl-arduino.cpp wolfssl-arduino.sh wolfssl.h
AURIX Cpu0_Main.c README.md include.am user_settings.h wolf_main.c
Android Android.bp README.md include.am user_settings.h
CRYPTOCELL README.md include.am main.c user_settings.h
CSBENCH include.am user_settings.h
ECLIPSE
DEOS
deos_wolfssl .options
README.md deos_malloc.c include.am tls_wolfssl.c tls_wolfssl.h user_settings.h
MICRIUM README.md client_wolfssl.c client_wolfssl.h include.am server_wolfssl.c server_wolfssl.h user_settings.h wolfsslRunTests.c
RTTHREAD README.md include.am user_settings.h wolfssl_test.c
SIFIVE README.md include.am
Espressif
ESP-IDF
examples
template
VisualGDB wolfssl_template_IDF_v5.1_ESP32.vgdbproj
components
wolfssl
include user_settings.h
CMakeLists.txt Kconfig README.md component.mk
main
include main.h
CMakeLists.txt Kconfig.projbuild component.mk main.c
CMakeLists.txt Makefile README.md partitions_singleapp_large.csv sdkconfig.defaults sdkconfig.defaults.esp8266
wolfssl_benchmark
VisualGDB wolfssl_benchmark_IDF_v4.4_ESP32.sln wolfssl_benchmark_IDF_v4.4_ESP32.vgdbproj wolfssl_benchmark_IDF_v5_ESP32.sln wolfssl_benchmark_IDF_v5_ESP32.vgdbproj wolfssl_benchmark_IDF_v5_ESP32C3.sln wolfssl_benchmark_IDF_v5_ESP32C3.vgdbproj wolfssl_benchmark_IDF_v5_ESP32S3.sln wolfssl_benchmark_IDF_v5_ESP32S3.vgdbproj
components
wolfssl
include user_settings.h
CMakeLists.txt Kconfig README.md component.mk
main
include main.h
CMakeLists.txt Kconfig.projbuild component.mk main.c
CMakeLists.txt Makefile README.md partitions_singleapp_large.csv sdkconfig.defaults sdkconfig.defaults.esp8266
wolfssl_client
VisualGDB README.md wolfssl_client_IDF_v5_ESP32.sln wolfssl_client_IDF_v5_ESP32.vgdbproj
components
wolfssl
include user_settings.h
CMakeLists.txt Kconfig README.md component.mk
main
include client-tls.h main.h time_helper.h wifi_connect.h
CMakeLists.txt Kconfig.projbuild client-tls.c component.mk main.c time_helper.c wifi_connect.c
CMakeLists.txt Makefile README.md README_server_sm.md partitions_singleapp_large.csv sdkconfig.defaults sdkconfig.defaults.esp32c2 sdkconfig.defaults.esp8266 wolfssl_client_ESP8266.vgdbproj
wolfssl_server
VisualGDB README.md wolfssl_server_IDF_v5_ESP32.sln wolfssl_server_IDF_v5_ESP32.vgdbproj
components
wolfssl
include user_settings.h
CMakeLists.txt Kconfig README.md component.mk
main
include main.h server-tls.h time_helper.h wifi_connect.h
CMakeLists.txt Kconfig.projbuild component.mk main.c server-tls.c time_helper.c wifi_connect.c
CMakeLists.txt Makefile README.md README_server_sm.md partitions_singleapp_large.csv sdkconfig.defaults sdkconfig.defaults.esp32c2 sdkconfig.defaults.esp8266 wolfssl_server_ESP8266.vgdbproj
wolfssl_test
VisualGDB wolfssl_test-IDF_v5_ESP32.sln wolfssl_test-IDF_v5_ESP32.vgdbproj wolfssl_test-IDF_v5_ESP32C3.sln wolfssl_test-IDF_v5_ESP32C3.vgdbproj wolfssl_test-IDF_v5_ESP32C6.sln wolfssl_test-IDF_v5_ESP32C6.vgdbproj wolfssl_test_IDF_v5_ESP32S3.sln wolfssl_test_IDF_v5_ESP32S3.vgdbproj
components
wolfssl
include user_settings.h
CMakeLists.txt Kconfig README.md component.mk
main
include main.h
CMakeLists.txt Kconfig.projbuild component.mk main.c
CMakeLists.txt Makefile README.md partitions_singleapp_large.csv sdkconfig.defaults sdkconfig.defaults.esp32 sdkconfig.defaults.esp32c3 sdkconfig.defaults.esp32c6 sdkconfig.defaults.esp32h2 sdkconfig.defaults.esp32s2 sdkconfig.defaults.esp32s3 sdkconfig.defaults.esp8266 testAll.sh testMonitor.sh wolfssl_test_ESP8266.sln wolfssl_test_ESP8266.vgdbproj
wolfssl_test_idf
VisualGDB VisualGDB_wolfssl_test_idf.sln VisualGDB_wolfssl_test_idf.vgdbproj
main CMakeLists.txt Kconfig.projbuild component.mk main.c main_wip.c.ex time_helper.c time_helper.h
CMakeLists.txt Kconfig.projbuild README.md component.mk sdkconfig.defaults
README.md
libs CMakeLists.txt README.md component.mk tigard.cfg
test CMakeLists.txt README.md component.mk test_wolfssl.c
README.md README_32se.md UPDATE.md compileAllExamples.sh dummy_config_h dummy_test_paths.h setup.sh setup_win.bat user_settings.h
README.md include.am
GCC-ARM
Header user_settings.h
Source armtarget.c benchmark_main.c test_main.c tls_client.c tls_server.c wolf_main.c
Makefile Makefile.bench Makefile.client Makefile.common Makefile.server Makefile.static Makefile.test README.md include.am linker.ld linker_fips.ld
Gaisler-BCC README.md include.am
HEXAGON
DSP Makefile wolfssl_dsp.idl
Makefile README.md build.sh ecc-verify-benchmark.c ecc-verify.c include.am user_settings.h
HEXIWEAR
wolfSSL_HW .cwGeneratedFileSetLog user_settings.h
IAR-EWARM
Projects
benchmark benchmark-main.c current_time.c wolfCrypt-benchmark.ewd wolfCrypt-benchmark.ewp
common minimum-startup.c wolfssl.icf
lib wolfSSL-Lib.ewd wolfSSL-Lib.ewp
test test-main.c wolfCrypt-test.ewd wolfCrypt-test.ewp
user_settings.h wolfssl.eww
embOS
SAMV71_XULT
embOS_SAMV71_XULT_Linker_Script samv71q21_wolfssl.icf
embOS_SAMV71_XULT_user_settings user_settings.h user_settings_simple_example.h user_settings_verbose_example.h
embOS_wolfcrypt_benchmark_SAMV71_XULT
Application runBenchmarks.c
README_wolfcrypt_benchmark wolfcrypt_benchmark.ewd wolfcrypt_benchmark.ewp
embOS_wolfcrypt_lib_SAMV71_XULT README_wolfcrypt_lib wolfcrypt_lib.ewd wolfcrypt_lib.ewp
embOS_wolfcrypt_test_SAMV71_XULT
Application runWolfcryptTests.c
README_wolfcrypt_test wolfcrypt_test.ewd wolfcrypt_test.ewp
README_SAMV71
custom_port
custom_port_Linker_Script samv71q21_wolfssl.icf
custom_port_user_settings user_settings.h
wolfcrypt_benchmark_custom_port
Application runBenchmarks.c
wolfcrypt_test_custom_port
Application runWolfcryptTests.c
README_custom_port
extract_trial_here README_extract_trial_here
README
.gitignore README
IAR-MSP430 Makefile README.md include.am main.c user_settings.h
INTIME-RTOS Makefile README.md include.am libwolfssl.c libwolfssl.vcxproj user_settings.h wolfExamples.c wolfExamples.h wolfExamples.sln wolfExamples.vcxproj wolfssl-lib.sln wolfssl-lib.vcxproj
Infineon README.md include.am user_settings.h
KDS
config user_settings.h
include.am
LINUX-SGX README.md build.sh clean.sh include.am sgx_t_static.mk
LPCXPRESSO
lib_wolfssl lpc_18xx_port.c user_settings.h
wolf_example
src lpc_18xx_startup.c wolfssl_example.c
readme.txt
README.md
M68K
benchmark Makefile main.cpp
testwolfcrypt Makefile main.cpp
Makefile README.md include.am user_settings.h
MCUEXPRESSO
RT1170 fsl_caam_c.patch fsl_caam_h.patch user_settings.h
benchmark
source run_benchmark.c
wolfssl liblinks.xml
README.md include.am user_settings.h wolfcrypt_test.c
MDK-ARM
LPC43xx time-LCP43xx.c
MDK-ARM
wolfSSL Retarget.c cert_data.c cert_data.h config-BARE-METAL.h config-FS.h config-RTX-TCP-FS.h config-WOLFLIB.h main.c shell.c time-CortexM3-4.c time-dummy.c wolfssl_MDK_ARM.c wolfssl_MDK_ARM.h
STM32F2xx_StdPeriph_Lib time-STM32F2xx.c
MDK5-ARM
Conf user_settings.h
Inc wolfssl_MDK_ARM.h
Projects
CryptBenchmark Abstract.txt CryptBenchmark.sct CryptBenchmark.uvoptx CryptBenchmark.uvprojx main.c
CryptTest Abstract.txt CryptTest.sct CryptTest.uvoptx CryptTest.uvprojx main.c
EchoClient Abstract.txt EchoClient.uvoptx EchoClient.uvprojx main.c wolfssl-link.sct
EchoServer Abstract.txt EchoServer.uvoptx EchoServer.uvprojx main.c wolfssl-link.sct
SimpleClient Abstract.txt SimpleClient.uvoptx SimpleClient.uvprojx main.c wolfssl-link.sct
SimpleServer Abstract.txt SimpleServer.uvoptx SimpleServer.uvprojx main.c wolfssl-link.sct
wolfSSL-Full Abstract.txt main.c shell.c time-CortexM3-4.c wolfsslFull.uvoptx wolfsslFull.uvprojx
wolfSSL-Lib Abstract.txt wolfSSL-Lib.uvoptx wolfSSL-Lib.uvprojx
Src ssl-dummy.c
README.md include.am
MPLABX16
wolfcrypt_test.X
nbproject
private configurations.xml private.xml
configurations.xml include.am project.xml
Makefile
wolfssl.X
nbproject configurations.xml include.am project.xml
Makefile
README.md include.am main.c user_settings.h
MQX Makefile README-jp.md README.md client-tls.c include.am server-tls.c user_config.h user_settings.h
MSVS-2019-AZSPHERE
client client.c client.h
server server.c server.h
shared util.h
wolfssl_new_azsphere
HardwareDefinitions
avnet_mt3620_sk
inc
hw template_appliance.h
template_appliance.json
mt3620_rdb
inc
hw template_appliance.h
template_appliance.json
seeed_mt3620_mdb
inc
hw template_appliance.h
template_appliance.json
.gitignore CMakeLists.txt CMakeSettings.json app_manifest.json applibs_versions.h launch.vs.json main.c
README.md include.am user_settings.h
MYSQL CMakeLists_wolfCrypt.txt CMakeLists_wolfSSL.txt do.sh
NDS README.md
NETOS Makefile.wolfcrypt.inc README.md include.am user_settings.h user_settings.h-cert2425 user_settings.h-cert3389 wolfssl_netos_custom.c
OPENSTM32 README.md
PlatformIO
examples
wolfssl_benchmark
include README main.h
lib README
src CMakeLists.txt main.c
test README
CMakeLists.txt README.md platformio.ini sdkconfig.defaults wolfssl_benchmark.code-workspace
wolfssl_test
include README main.h
lib README
src CMakeLists.txt main.c
test README
CMakeLists.txt README.md platformio.ini sdkconfig.defaults wolfssl_test.code-workspace
README.md wolfssl_platformio.code-workspace
README.md include.am
QNX
CAAM-DRIVER Makefile
example-client Makefile client-tls.c
example-cmac Makefile cmac-test.c
example-server Makefile server-tls.c
testwolfcrypt Makefile
wolfssl Makefile user_settings.h
README.md include.am
RISCV
SIFIVE-HIFIVE1 Makefile README.md include.am main.c user_settings.h
SIFIVE-UNLEASHED README.md include.am
include.am
ROWLEY-CROSSWORKS-ARM Kinetis_FlashPlacement.xml README.md arm_startup.c benchmark_main.c hw.h include.am kinetis_hw.c retarget.c test_main.c user_settings.h wolfssl.hzp wolfssl_ltc.hzp
Renesas
cs+
Projects
common strings.h unistd.h user_settings.h wolfssl_dummy.c
t4_demo README_en.txt README_jp.txt t4_demo.mtpj wolf_client.c wolf_demo.h wolf_main.c wolf_server.c
test test.mtpj test_main.c
wolfssl_lib wolfssl_lib.mtpj
README include.am
e2studio
DK-S7G2
benchmark-template
src app_entry.c
example_server-template
src app_entry.c
wolfcrypttest-template
src app_entry.c
wolfssl-template-project configuration.xml
README.md include.am user_settings.h
Projects
common strings.h unistd.h user_settings.h wolfssl_dummy.c
test
src key_data.c key_data.h test_main.c wolf_client.c wolf_server.c wolfssl_demo.h
tools generate_rsa_keypair.sh genhexbuf.pl rsa_pss_sign.sh
wolfssl
src .gitkeep
wolfcrypt
src .gitkeep
README include.am
RA6M3
benchmark-wolfcrypt
common .gitkeep
script .gitkeep
src wolfssl_thread_entry.c
client-wolfssl
common
src .gitkeep
script .gitkeep
src wolfssl_thread_entry.c
wolfssl_thread_entry.h
common
ra6m3g README.md
src freertos_tcp_port.c
user_settings.h util.h
server-wolfssl
common
src .gitkeep
script .gitkeep
src wolfssl_thread_entry.c
wolfssl_thread_entry.h
test-wolfcrypt
common .gitkeep
script .gitkeep
src wolfssl_thread_entry.c
wolfssl
src .gitkeep
wolfcrypt .gitkeep
README.md README_APRA6M_en.md README_APRA6M_jp.md include.am
RA6M3G README.md
RA6M4
common user_settings.h wolfssl_demo.h
test
key_data key_data.h key_data_sce.c
src
SEGGER_RTT myprint.c
common .gitignore
test_main.c wolf_client.c wolfssl_sce_unit_test.c
test_RA6M4Debug.launch
tools
example_keys generate_SignedCA.sh rsa_private.pem rsa_public.pem
README.md
README.md include.am
RX65N
GR-ROSE
common strings.h unistd.h user_settings.h wolfssl_dummy.c
smc smc.scfg
test
src key_data.c key_data.h test_main.c wolf_client.c wolf_server.c wolfssl_demo.h
test.rcpc test_HardwareDebug.launch
tools
example_keys generate_SignedCA.sh rsa_private.pem rsa_public.pem
README.md
wolfssl wolfssl.rcpc
README_EN.md README_JP.md include.am
RSK
resource section.esi
wolfssl wolfssl.rcpc
wolfssl_demo key_data.c key_data.h user_settings.h wolfssl_demo.c wolfssl_demo.h
InstructionManualForExample_RSK+RX65N-2MB_EN.pdf InstructionManualForExample_RSK+RX65N-2MB_JP.pdf README_EN.md README_JP.md include.am
RX72N
EnvisionKit
Simple
common sectioninfo.esi wolfssl_dummy.c
test
src
client simple_tcp_client.c simple_tls_tsip_client.c
server simple_tcp_server.c simple_tls_server.c
test_main.c wolfssl_simple_demo.h
test.rcpc test.scfg test_HardwareDebug.launch
wolfssl wolfssl.rcpc
README_EN.md README_JP.md
resource section.esi
tools
example_keys generate_SignedCA.sh rsa_private.pem rsa_public.pem
README.md
wolfssl wolfssl.rcpc
wolfssl_demo key_data.c key_data.h user_settings.h wolfssl_demo.c wolfssl_demo.h wolfssl_tsip_unit_test.c
InstructionManualForExample_RX72N_EnvisonKit_EN.pdf InstructionManualForExample_RX72N_EnvisonKit_JP.pdf README_EN.md README_JP.md include.am
RZN2L
common user_settings.h wolfssl_demo.h
test
src
serial_io app_print.c
test wolf_client.c wolf_server.c wolfssl_rsip_unit_test.c
wolfCrypt .gitignore
wolfSSL .gitignore
local_system_init.c rzn2l_tst_thread_entry.c wolfssl_dummy.c
README.md include.am
SK-S7G2
common user_settings.h
wolfssl_lib configuration.xml
.gitignore README.md include.am
STARCORE README.txt include.am starcore_test.c user_settings.h
STM32Cube README.md STM32_Benchmarks.md default_conf.ftl include.am main.c wolfssl_example.c wolfssl_example.h
SimplicityStudio README.md include.am test_wolf.c user_settings.h
TRUESTUDIO
wolfssl user_settings.h
README include.am
VS-ARM README.md include.am user_settings.h wolfssl.sln wolfssl.vcxproj
VS-AZURE-SPHERE
client app_manifest.json client.c client.h client.vcxproj
server app_manifest.json server.c server.h server.vcxproj
shared util.h
wolfcrypt_test app_manifest.json wolfcrypt_test.vcxproj
README.md include.am user_settings.h wolfssl.sln wolfssl.vcxproj
VisualDSP include.am user_settings.h wolf_tasks.c
WICED-STUDIO README include.am user_settings.h
WIN README.txt include.am test.vcxproj user_settings.h user_settings_dtls.h wolfssl-fips.sln wolfssl-fips.vcxproj
WIN-SGX ReadMe.txt include.am wolfSSL_SGX.edl wolfSSL_SGX.sln wolfSSL_SGX.vcxproj
WIN-SRTP-KDF-140-3 README.txt include.am resource.h test.vcxproj user_settings.h wolfssl-fips.rc wolfssl-fips.sln wolfssl-fips.vcxproj
WIN10 README.txt include.am resource.h test.vcxproj user_settings.h wolfssl-fips.rc wolfssl-fips.sln wolfssl-fips.vcxproj
WINCE README.md include.am user_settings.h user_settings.h.140-2-deprecated
WORKBENCH README.md include.am
XCODE
Benchmark
wolfBench
Assets.xcassets
AppIcon.appiconset Contents.json
Base.lproj LaunchScreen.storyboard Main.storyboard
AppDelegate.h AppDelegate.m Info.plist ViewController.h ViewController.m main.m
wolfBench.xcodeproj project.pbxproj
include.am
wolfssl-FIPS.xcodeproj project.pbxproj
wolfssl.xcodeproj project.pbxproj
wolfssl_testsuite.xcodeproj project.pbxproj
README.md build-for-i386.sh include.am user_settings.h
XCODE-FIPSv2
macOS-C++
Intel user_settings.h
M1 user_settings.h
include.am user_settings.h
XCODE-FIPSv5 README include.am user_settings.h
XCODE-FIPSv6 README include.am user_settings.h
XilinxSDK
2018_2 lscript.ld
2019_2
wolfCrypt_example
src lscript.ld
wolfCrypt_example_system wolfCrypt_example_system.sprj
2022_1
wolfCrypt_FreeRTOS_example wolfCrypt_FreeRTOS_example.prj
wolfCrypt_FreeRTOS_example_system wolfCrypt_FreeRTOS_example_system.sprj
wolfCrypt_example wolfCrypt_example.prj
wolfCrypt_example_system wolfCrypt_example_system.sprj
.gitignore
README.md bench.sh combine.sh eclipse_formatter_profile.xml graph.sh include.am user_settings.h wolfssl_example.c
apple-universal
wolfssl-multiplatform
wolfssl-multiplatform
Assets.xcassets
AccentColor.colorset Contents.json
AppIcon.appiconset Contents.json
Contents.json
ContentView.swift simple_client_example.c simple_client_example.h wolfssl-multiplatform-Bridging-Header.h wolfssl_multiplatform.entitlements wolfssl_multiplatformApp.swift wolfssl_test_driver.c wolfssl_test_driver.h
wolfssl-multiplatform.xcodeproj project.pbxproj
.gitignore README.md build-wolfssl-framework.sh include.am
iotsafe Makefile README.md ca-cert.c devices.c devices.h include.am main.c memory-tls.c startup.c target.ld user_settings.h
iotsafe-raspberrypi Makefile README.md client-tls13.c include.am main.c
mynewt README.md apps.wolfcrypttest.pkg.yml crypto.wolfssl.pkg.yml crypto.wolfssl.syscfg.yml include.am setup.sh
zephyr README.md include.am
include.am
RTOS
nuttx
wolfssl .gitignore Kconfig Make.defs Makefile README.md setup-wolfssl.sh user_settings.h
include.am
bsdkm Makefile README.md bsdkm_wc_port.h include.am wolfkmod.c wolfkmod_aes.c x86_vecreg.c
certs
1024 ca-cert.der ca-cert.pem ca-key.der ca-key.pem client-cert.der client-cert.pem client-key.der client-key.pem client-keyPub.der dh1024.der dh1024.pem dsa-pub-1024.pem dsa1024.der dsa1024.pem include.am rsa1024.der server-cert.der server-cert.pem server-key.der server-key.pem
3072 client-cert.der client-cert.pem client-key.der client-key.pem client-keyPub.der include.am
4096 client-cert.der client-cert.pem client-key.der client-key.pem client-keyPub.der include.am
acert
rsa_pss acert.pem acert_ietf.pem acert_ietf_pubkey.pem acert_pubkey.pem
acert.pem acert_ietf.pem acert_ietf_pubkey.pem acert_pubkey.pem include.am
aia ca-issuers-cert.pem multi-aia-cert.pem overflow-aia-cert.pem
crl
extra-crls ca-int-cert-revoked.pem claim-root.pem crl_critical_entry.pem crlnum_57oct.pem crlnum_64oct.pem general-server-crl.pem large_crlnum.pem large_crlnum2.pem
hash_der 0fdb2da4.r0
hash_pem 0fdb2da4.r0
bad_time_fmt.pem ca-int-ecc.pem ca-int.pem ca-int2-ecc.pem ca-int2.pem caEcc384Crl.pem caEccCrl.der caEccCrl.pem cliCrl.pem client-int-ecc.pem client-int.pem crl.der crl.pem crl.revoked crl2.der crl2.pem crl_reason.pem crl_rsapss.pem eccCliCRL.pem eccSrvCRL.pem gencrls.sh include.am server-goodaltCrl.pem server-goodaltwildCrl.pem server-goodcnCrl.pem server-goodcnwildCrl.pem server-int-ecc.pem server-int.pem wolfssl.cnf
dilithium bench_dilithium_level2_key.der bench_dilithium_level3_key.der bench_dilithium_level5_key.der include.am
ecc bp256r1-key.der bp256r1-key.pem ca-secp256k1-cert.pem ca-secp256k1-key.pem client-bp256r1-cert.der client-bp256r1-cert.pem client-secp256k1-cert.der client-secp256k1-cert.pem genecc.sh include.am secp256k1-key.der secp256k1-key.pem secp256k1-param.pem secp256k1-privkey.der secp256k1-privkey.pem server-bp256r1-cert.der server-bp256r1-cert.pem server-secp256k1-cert.der server-secp256k1-cert.pem server2-secp256k1-cert.der server2-secp256k1-cert.pem wolfssl.cnf wolfssl_384.cnf
ed25519 ca-ed25519-key.der ca-ed25519-key.pem ca-ed25519-priv.der ca-ed25519-priv.pem ca-ed25519.der ca-ed25519.pem client-ed25519-key.der client-ed25519-key.pem client-ed25519-priv.der client-ed25519-priv.pem client-ed25519.der client-ed25519.pem eddsa-ed25519.der eddsa-ed25519.pem gen-ed25519-certs.sh gen-ed25519-keys.sh gen-ed25519.sh include.am root-ed25519-key.der root-ed25519-key.pem root-ed25519-priv.der root-ed25519-priv.pem root-ed25519.der root-ed25519.pem server-ed25519-cert.pem server-ed25519-key.der server-ed25519-key.pem server-ed25519-priv.der server-ed25519-priv.pem server-ed25519.der server-ed25519.pem
ed448 ca-ed448-key.der ca-ed448-key.pem ca-ed448-priv.der ca-ed448-priv.pem ca-ed448.der ca-ed448.pem client-ed448-key.der client-ed448-key.pem client-ed448-priv.der client-ed448-priv.pem client-ed448.der client-ed448.pem gen-ed448-certs.sh gen-ed448-keys.sh include.am root-ed448-key.der root-ed448-key.pem root-ed448-priv.der root-ed448-priv.pem root-ed448.der root-ed448.pem server-ed448-cert.pem server-ed448-key.der server-ed448-key.pem server-ed448-priv.der server-ed448-priv.pem server-ed448.der server-ed448.pem
external DigiCertGlobalRootCA.pem README.txt ca-digicert-ev.pem ca-globalsign-root.pem ca-google-root.pem ca_collection.pem include.am
falcon bench_falcon_level1_key.der bench_falcon_level5_key.der include.am
intermediate
ca_false_intermediate gentestcert.sh int_ca.key server.key test_ca.key test_ca.pem test_int_not_cacert.pem test_sign_bynoca_srv.pem wolfssl_base.conf wolfssl_srv.conf
ca-ecc-bad-aki.der ca-ecc-bad-aki.pem ca-int-cert.der ca-int-cert.pem ca-int-ecc-cert.der ca-int-ecc-cert.pem ca-int-ecc-key.der ca-int-ecc-key.pem ca-int-key.der ca-int-key.pem ca-int2-cert.der ca-int2-cert.pem ca-int2-ecc-cert.der ca-int2-ecc-cert.pem ca-int2-ecc-key.der ca-int2-ecc-key.pem ca-int2-key.der ca-int2-key.pem client-chain-alt-ecc.pem client-chain-alt.pem client-chain-ecc.der client-chain-ecc.pem client-chain.der client-chain.pem client-int-cert.der client-int-cert.pem client-int-ecc-cert.der client-int-ecc-cert.pem genintcerts.sh include.am server-chain-alt-ecc.pem server-chain-alt.pem server-chain-ecc.der server-chain-ecc.pem server-chain-short.pem server-chain.der server-chain.pem server-int-cert.der server-int-cert.pem server-int-ecc-cert.der server-int-ecc-cert.pem
lms bc_hss_L2_H5_W8_root.der bc_hss_L3_H5_W4_root.der bc_lms_chain_ca.der bc_lms_chain_leaf.der bc_lms_native_bc_root.der bc_lms_sha256_h10_w8_root.der bc_lms_sha256_h5_w4_root.der include.am
mldsa README.txt include.am mldsa44-cert.der mldsa44-cert.pem mldsa44-key.pem mldsa44_bare-priv.der mldsa44_bare-seed.der mldsa44_oqskeypair.der mldsa44_priv-only.der mldsa44_pub-spki.der mldsa44_seed-only.der mldsa44_seed-priv.der mldsa65-cert.der mldsa65-cert.pem mldsa65-key.pem mldsa65_bare-priv.der mldsa65_bare-seed.der mldsa65_oqskeypair.der mldsa65_priv-only.der mldsa65_pub-spki.der mldsa65_seed-only.der mldsa65_seed-priv.der mldsa87-cert.der mldsa87-cert.pem mldsa87-key.pem mldsa87_bare-priv.der mldsa87_bare-seed.der mldsa87_oqskeypair.der mldsa87_priv-only.der mldsa87_pub-spki.der mldsa87_seed-only.der mldsa87_seed-priv.der
ocsp imposter-root-ca-cert.der imposter-root-ca-cert.pem imposter-root-ca-key.der imposter-root-ca-key.pem include.am index-ca-and-intermediate-cas.txt index-ca-and-intermediate-cas.txt.attr index-intermediate1-ca-issued-certs.txt index-intermediate1-ca-issued-certs.txt.attr index-intermediate2-ca-issued-certs.txt index-intermediate2-ca-issued-certs.txt.attr index-intermediate3-ca-issued-certs.txt index-intermediate3-ca-issued-certs.txt.attr intermediate1-ca-cert.der intermediate1-ca-cert.pem intermediate1-ca-key.der intermediate1-ca-key.pem intermediate2-ca-cert.der intermediate2-ca-cert.pem intermediate2-ca-key.der intermediate2-ca-key.pem intermediate3-ca-cert.der intermediate3-ca-cert.pem intermediate3-ca-key.der intermediate3-ca-key.pem ocsp-responder-cert.der ocsp-responder-cert.pem ocsp-responder-key.der ocsp-responder-key.pem openssl.cnf renewcerts-for-test.sh renewcerts.sh root-ca-cert.der root-ca-cert.pem root-ca-crl.pem root-ca-key.der root-ca-key.pem server1-cert.der server1-cert.pem server1-chain-noroot.pem server1-key.der server1-key.pem server2-cert.der server2-cert.pem server2-key.der server2-key.pem server3-cert.der server3-cert.pem server3-key.der server3-key.pem server4-cert.der server4-cert.pem server4-key.der server4-key.pem server5-cert.der server5-cert.pem server5-key.der server5-key.pem test-leaf-response.der test-multi-response.der test-response-nointern.der test-response-rsapss.der test-response.der
p521 ca-p521-key.der ca-p521-key.pem ca-p521-priv.der ca-p521-priv.pem ca-p521.der ca-p521.pem client-p521-key.der client-p521-key.pem client-p521-priv.der client-p521-priv.pem client-p521.der client-p521.pem gen-p521-certs.sh gen-p521-keys.sh include.am root-p521-key.der root-p521-key.pem root-p521-priv.der root-p521-priv.pem root-p521.der root-p521.pem server-p521-cert.pem server-p521-key.der server-p521-key.pem server-p521-priv.der server-p521-priv.pem server-p521.der server-p521.pem
renewcerts wolfssl.cnf
rpk client-cert-rpk.der client-ecc-cert-rpk.der include.am server-cert-rpk.der server-ecc-cert-rpk.der
rsapss ca-3072-rsapss-key.der ca-3072-rsapss-key.pem ca-3072-rsapss-priv.der ca-3072-rsapss-priv.pem ca-3072-rsapss.der ca-3072-rsapss.pem ca-rsapss-key.der ca-rsapss-key.pem ca-rsapss-priv.der ca-rsapss-priv.pem ca-rsapss.der ca-rsapss.pem client-3072-rsapss-key.der client-3072-rsapss-key.pem client-3072-rsapss-priv.der client-3072-rsapss-priv.pem client-3072-rsapss.der client-3072-rsapss.pem client-rsapss-key.der client-rsapss-key.pem client-rsapss-priv.der client-rsapss-priv.pem client-rsapss.der client-rsapss.pem gen-rsapss-keys.sh include.am renew-rsapss-certs.sh root-3072-rsapss-key.der root-3072-rsapss-key.pem root-3072-rsapss-priv.der root-3072-rsapss-priv.pem root-3072-rsapss.der root-3072-rsapss.pem root-rsapss-key.der root-rsapss-key.pem root-rsapss-priv.der root-rsapss-priv.pem root-rsapss.der root-rsapss.pem server-3072-rsapss-cert.pem server-3072-rsapss-key.der server-3072-rsapss-key.pem server-3072-rsapss-priv.der server-3072-rsapss-priv.pem server-3072-rsapss.der server-3072-rsapss.pem server-mix-rsapss-cert.pem server-rsapss-cert.pem server-rsapss-key.der server-rsapss-key.pem server-rsapss-priv.der server-rsapss-priv.pem server-rsapss.der server-rsapss.pem
sia timestamping-sia-cert.pem
slhdsa bench_slhdsa_sha2_128f_key.der bench_slhdsa_sha2_128s_key.der bench_slhdsa_sha2_192f_key.der bench_slhdsa_sha2_192s_key.der bench_slhdsa_sha2_256f_key.der bench_slhdsa_sha2_256s_key.der bench_slhdsa_shake128f_key.der bench_slhdsa_shake128s_key.der bench_slhdsa_shake192f_key.der bench_slhdsa_shake192s_key.der bench_slhdsa_shake256f_key.der bench_slhdsa_shake256s_key.der client-mldsa44-priv.pem client-mldsa44-sha2.der client-mldsa44-sha2.pem client-mldsa44-shake.der client-mldsa44-shake.pem gen-slhdsa-mldsa-certs.sh include.am root-slhdsa-sha2-128s-priv.der root-slhdsa-sha2-128s-priv.pem root-slhdsa-sha2-128s.der root-slhdsa-sha2-128s.pem root-slhdsa-shake-128s-priv.der root-slhdsa-shake-128s-priv.pem root-slhdsa-shake-128s.der root-slhdsa-shake-128s.pem server-mldsa44-priv.pem server-mldsa44-sha2.der server-mldsa44-sha2.pem server-mldsa44-shake.der server-mldsa44-shake.pem
sm2 ca-sm2-key.der ca-sm2-key.pem ca-sm2-priv.der ca-sm2-priv.pem ca-sm2.der ca-sm2.pem client-sm2-key.der client-sm2-key.pem client-sm2-priv.der client-sm2-priv.pem client-sm2.der client-sm2.pem fix_sm2_spki.py gen-sm2-certs.sh gen-sm2-keys.sh include.am root-sm2-key.der root-sm2-key.pem root-sm2-priv.der root-sm2-priv.pem root-sm2.der root-sm2.pem self-sm2-cert.pem self-sm2-key.pem self-sm2-priv.pem server-sm2-cert.der server-sm2-cert.pem server-sm2-key.der server-sm2-key.pem server-sm2-priv.der server-sm2-priv.pem server-sm2.der server-sm2.pem
statickeys dh-ffdhe2048-params.pem dh-ffdhe2048-pub.der dh-ffdhe2048-pub.pem dh-ffdhe2048.der dh-ffdhe2048.pem ecc-secp256r1.der ecc-secp256r1.pem gen-static.sh include.am x25519-pub.der x25519-pub.pem x25519.der x25519.pem
test
expired expired-ca.der expired-ca.pem expired-cert.der expired-cert.pem
catalog.txt cert-bad-neg-int.der cert-bad-oid.der cert-bad-utf8.der cert-ext-ia.cfg cert-ext-ia.der cert-ext-ia.pem cert-ext-joi.cfg cert-ext-joi.der cert-ext-joi.pem cert-ext-mnc.der cert-ext-multiple.cfg cert-ext-multiple.der cert-ext-multiple.pem cert-ext-nc-combined.der cert-ext-nc-combined.pem cert-ext-nc.cfg cert-ext-nc.der cert-ext-nc.pem cert-ext-ncdns.der cert-ext-ncdns.pem cert-ext-ncip.der cert-ext-ncip.pem cert-ext-ncmixed.der cert-ext-ncmulti.der cert-ext-ncmulti.pem cert-ext-ncrid.der cert-ext-ncrid.pem cert-ext-nct.cfg cert-ext-nct.der cert-ext-nct.pem cert-ext-ndir-exc.cfg cert-ext-ndir-exc.der cert-ext-ndir-exc.pem cert-ext-ndir.cfg cert-ext-ndir.der cert-ext-ndir.pem cert-ext-ns.der cert-over-max-altnames.cfg cert-over-max-altnames.der cert-over-max-altnames.pem cert-over-max-nc.cfg cert-over-max-nc.der cert-over-max-nc.pem client-ecc-cert-ski.hex cn-ip-literal.der cn-ip-wildcard.der crit-cert.pem crit-key.pem dh1024.der dh1024.pem dh512.der dh512.pem digsigku.pem encrypteddata.msg gen-badsig.sh gen-ext-certs.sh gen-testcerts.sh include.am kari-keyid-cms.msg ktri-keyid-cms.msg ossl-trusted-cert.pem server-badaltname.der server-badaltname.pem server-badaltnull.der server-badaltnull.pem server-badcn.der server-badcn.pem server-badcnnull.der server-badcnnull.pem server-cert-ecc-badsig.der server-cert-ecc-badsig.pem server-cert-rsa-badsig.der server-cert-rsa-badsig.pem server-duplicate-policy.pem server-garbage.der server-garbage.pem server-goodalt.der server-goodalt.pem server-goodaltwild.der server-goodaltwild.pem server-goodcn.der server-goodcn.pem server-goodcnwild.der server-goodcnwild.pem server-localhost.der server-localhost.pem smime-test-canon.p7s smime-test-multipart-badsig.p7s smime-test-multipart.p7s smime-test.p7s
test-pathlen assemble-chains.sh chainA-ICA1-key.pem chainA-ICA1-pathlen0.pem chainA-assembled.pem chainA-entity-key.pem chainA-entity.pem chainB-ICA1-key.pem chainB-ICA1-pathlen0.pem chainB-ICA2-key.pem chainB-ICA2-pathlen1.pem chainB-assembled.pem chainB-entity-key.pem chainB-entity.pem chainC-ICA1-key.pem chainC-ICA1-pathlen1.pem chainC-assembled.pem chainC-entity-key.pem chainC-entity.pem chainD-ICA1-key.pem chainD-ICA1-pathlen127.pem chainD-assembled.pem chainD-entity-key.pem chainD-entity.pem chainE-ICA1-key.pem chainE-ICA1-pathlen128.pem chainE-assembled.pem chainE-entity-key.pem chainE-entity.pem chainF-ICA1-key.pem chainF-ICA1-pathlen1.pem chainF-ICA2-key.pem chainF-ICA2-pathlen0.pem chainF-assembled.pem chainF-entity-key.pem chainF-entity.pem chainG-ICA1-key.pem chainG-ICA1-pathlen0.pem chainG-ICA2-key.pem chainG-ICA2-pathlen1.pem chainG-ICA3-key.pem chainG-ICA3-pathlen99.pem chainG-ICA4-key.pem chainG-ICA4-pathlen5.pem chainG-ICA5-key.pem chainG-ICA5-pathlen20.pem chainG-ICA6-key.pem chainG-ICA6-pathlen10.pem chainG-ICA7-key.pem chainG-ICA7-pathlen100.pem chainG-assembled.pem chainG-entity-key.pem chainG-entity.pem chainH-ICA1-key.pem chainH-ICA1-pathlen0.pem chainH-ICA2-key.pem chainH-ICA2-pathlen2.pem chainH-ICA3-key.pem chainH-ICA3-pathlen2.pem chainH-ICA4-key.pem chainH-ICA4-pathlen2.pem chainH-assembled.pem chainH-entity-key.pem chainH-entity.pem chainI-ICA1-key.pem chainI-ICA1-no_pathlen.pem chainI-ICA2-key.pem chainI-ICA2-no_pathlen.pem chainI-ICA3-key.pem chainI-ICA3-pathlen2.pem chainI-assembled.pem chainI-entity-key.pem chainI-entity.pem chainJ-ICA1-key.pem chainJ-ICA1-no_pathlen.pem chainJ-ICA2-key.pem chainJ-ICA2-no_pathlen.pem chainJ-ICA3-key.pem chainJ-ICA3-no_pathlen.pem chainJ-ICA4-key.pem chainJ-ICA4-pathlen2.pem chainJ-assembled.pem chainJ-entity-key.pem chainJ-entity.pem include.am refreshkeys.sh
test-serial0 ee_normal.pem ee_serial0.pem generate_certs.sh include.am intermediate_serial0.pem root_serial0.pem root_serial0_key.pem selfsigned_nonca_serial0.pem
xmss bc_xmss_chain_ca.der bc_xmss_chain_leaf.der bc_xmss_sha2_10_256_root.der bc_xmss_sha2_16_256_root.der bc_xmssmt_sha2_20_2_256_root.der bc_xmssmt_sha2_20_4_256_root.der bc_xmssmt_sha2_40_8_256_root.der include.am
ca-cert-chain.der ca-cert.der ca-cert.pem ca-ecc-cert.der ca-ecc-cert.pem ca-ecc-key.der ca-ecc-key.pem ca-ecc384-cert.der ca-ecc384-cert.pem ca-ecc384-key.der ca-ecc384-key.pem ca-key-pkcs8-attribute.der ca-key.der ca-key.pem check_dates.sh client-absolute-urn.pem client-ca-cert.der client-ca-cert.pem client-ca.pem client-cert-ext.der client-cert-ext.pem client-cert.der client-cert.pem client-crl-dist.der client-crl-dist.pem client-ecc-ca-cert.der client-ecc-ca-cert.pem client-ecc-cert.der client-ecc-cert.pem client-ecc384-cert.der client-ecc384-cert.pem client-ecc384-key.der client-ecc384-key.pem client-key.der client-key.pem client-keyEnc.pem client-keyPub.der client-keyPub.pem client-relative-uri.pem client-uri-cert.pem csr.attr.der csr.dsa.der csr.dsa.pem csr.ext.der csr.signed.der dh-priv-2048.der dh-priv-2048.pem dh-pub-2048.der dh-pub-2048.pem dh-pubkey-2048.der dh2048.der dh2048.pem dh3072.der dh3072.pem dh4096.der dh4096.pem dsa-pubkey-2048.der dsa2048.der dsa2048.pem dsa3072.der dsaparams.der dsaparams.pem ecc-client-key.der ecc-client-key.pem ecc-client-keyPub.der ecc-client-keyPub.pem ecc-key-comp.pem ecc-keyPkcs8.der ecc-keyPkcs8.pem ecc-keyPkcs8Enc.der ecc-keyPkcs8Enc.pem ecc-keyPub.der ecc-keyPub.pem ecc-params.der ecc-params.pem ecc-privOnlyCert.pem ecc-privOnlyKey.pem ecc-privkey.der ecc-privkey.pem ecc-privkeyPkcs8.der ecc-privkeyPkcs8.pem ecc-rsa-server.p12 empty-issuer-cert.pem entity-no-ca-bool-cert.pem entity-no-ca-bool-key.pem fpki-cert.der fpki-certpol-cert.der gen_revoked.sh include.am renewcerts.sh rid-cert.der rsa-pub-2048.pem rsa2048.der rsa3072.der server-cert-chain.der server-cert.der server-cert.pem server-ecc-comp.der server-ecc-comp.pem server-ecc-rsa.der server-ecc-rsa.pem server-ecc-self.der server-ecc-self.pem server-ecc.der server-ecc.pem server-ecc384-cert.der server-ecc384-cert.pem server-ecc384-key.der server-ecc384-key.pem server-key.der server-key.pem server-keyEnc.pem server-keyPkcs8.der server-keyPkcs8.pem server-keyPkcs8Enc.der server-keyPkcs8Enc.pem server-keyPkcs8Enc12.pem server-keyPkcs8Enc2.pem server-keyPub.der server-keyPub.pem server-revoked-cert.pem server-revoked-key.pem taoCert.txt test-ber-exp02-05-2022.p7b test-degenerate.p7b test-multiple-recipients.p7b test-servercert-rc2.p12 test-servercert.p12 test-stream-dec.p7b test-stream-sign.p7b wolfssl-website-ca.pem x942dh2048.der x942dh2048.pem
cmake
consumer CMakeLists.txt README.md main.c
modules FindARIA.cmake FindOQS.cmake
Config.cmake.in README.md config.in functions.cmake include.am options.h.in wolfssl-config-version.cmake.in wolfssl-targets.cmake.in
debian
source format
changelog.in control.in copyright include.am libwolfssl-dev.install libwolfssl.install rules.in
doc
dox_comments
header_files aes.h arc4.h ascon.h asn.h asn_public.h blake2.h bn.h camellia.h chacha.h chacha20_poly1305.h cmac.h coding.h compress.h cryptocb.h curve25519.h curve448.h des3.h dh.h doxygen_groups.h doxygen_pages.h dsa.h ecc.h eccsi.h ed25519.h ed448.h error-crypt.h evp.h hash.h hmac.h iotsafe.h kdf.h logging.h md2.h md4.h md5.h memory.h ocsp.h pem.h pkcs11.h pkcs7.h poly1305.h psa.h puf.h pwdbased.h quic.h random.h ripemd.h rsa.h sakke.h sha.h sha256.h sha3.h sha512.h signature.h siphash.h srp.h ssl.h tfm.h types.h wc_encrypt.h wc_port.h wc_she.h wc_slhdsa.h wolfio.h
header_files-ja aes.h arc4.h ascon.h asn.h asn_public.h blake2.h bn.h camellia.h chacha.h chacha20_poly1305.h cmac.h coding.h compress.h cryptocb.h curve25519.h curve448.h des3.h dh.h doxygen_groups.h doxygen_pages.h dsa.h ecc.h eccsi.h ed25519.h ed448.h error-crypt.h evp.h hash.h hmac.h iotsafe.h kdf.h logging.h md2.h md4.h md5.h memory.h ocsp.h pem.h pkcs11.h pkcs7.h poly1305.h psa.h pwdbased.h quic.h random.h ripemd.h rsa.h sakke.h sha.h sha256.h sha3.h sha512.h signature.h siphash.h srp.h ssl.h tfm.h types.h wc_encrypt.h wc_port.h wolfio.h
formats
html
html_changes
search search.css search.js
customdoxygen.css doxygen.css menu.js menudata.js tabs.css
Doxyfile footer.html header.html mainpage.dox
pdf Doxyfile header.tex
images wolfssl_logo.png
QUIC.md README.txt README_DOXYGEN check_api.sh generate_documentation.sh include.am
examples
asn1 asn1.c dumpasn1.cfg gen_oid_names.rb include.am oid_names.h
async Makefile README.md async_client.c async_server.c async_tls.c async_tls.h include.am user_settings.h
benchmark include.am tls_bench.c tls_bench.h
client client.c client.h client.sln client.vcproj client.vcxproj include.am
configs README.md include.am user_settings_EBSnet.h user_settings_all.h user_settings_arduino.h user_settings_baremetal.h user_settings_ca.h user_settings_curve25519nonblock.h user_settings_dtls13.h user_settings_eccnonblock.h user_settings_espressif.h user_settings_fipsv2.h user_settings_fipsv5.h user_settings_min_ecc.h user_settings_openssl_compat.h user_settings_pkcs7.h user_settings_platformio.h user_settings_pq.h user_settings_rsa_only.h user_settings_stm32.h user_settings_template.h user_settings_tls12.h user_settings_tls13.h user_settings_wolfboot_keytools.h user_settings_wolfssh.h user_settings_wolftpm.h
crypto_policies
default wolfssl.txt
future wolfssl.txt
legacy wolfssl.txt
echoclient echoclient.c echoclient.h echoclient.sln echoclient.vcproj echoclient.vcxproj include.am quit
echoserver echoserver.c echoserver.h echoserver.sln echoserver.vcproj echoserver.vcxproj include.am
ocsp_responder include.am ocsp_responder.c ocsp_responder.h
pem include.am pem.c
sctp include.am sctp-client-dtls.c sctp-client.c sctp-server-dtls.c sctp-server.c
server include.am server.c server.h server.sln server.vcproj server.vcxproj
README.md include.am
linuxkm
patches
5.10.17 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-5v10v17.patch
5.10.236 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-5v10v236.patch
5.14.0-570.58.1.el9_6 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-5v14-570v58v1-el9_6.patch
5.15 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-5v15.patch
5.17 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-5v17.patch
5.17-ubuntu-jammy-tegra WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-5v17-ubuntu-jammy-tegra.patch
6.1.73 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-6v1v73.patch
6.12 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-6v12.patch
6.15 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-6v15.patch
7.0 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-7v0.patch
regen-patches.sh
Kbuild Makefile README.md get_thread_size.c include.am linuxkm-fips-hash-wrapper.sh linuxkm-fips-hash.c linuxkm_memory.c linuxkm_memory.h linuxkm_wc_port.h lkcapi_aes_glue.c lkcapi_dh_glue.c lkcapi_ecdh_glue.c lkcapi_ecdsa_glue.c lkcapi_glue.c lkcapi_rsa_glue.c lkcapi_sha_glue.c module_exports.c.template module_hooks.c pie_redirect_table.c wolfcrypt.lds x86_vector_register_glue.c
m4 ax_add_am_macro.m4 ax_am_jobserver.m4 ax_am_macros.m4 ax_append_compile_flags.m4 ax_append_flag.m4 ax_append_link_flags.m4 ax_append_to_file.m4 ax_atomic.m4 ax_bsdkm.m4 ax_check_compile_flag.m4 ax_check_link_flag.m4 ax_compiler_version.m4 ax_count_cpus.m4 ax_create_generic_config.m4 ax_debug.m4 ax_file_escapes.m4 ax_harden_compiler_flags.m4 ax_linuxkm.m4 ax_print_to_file.m4 ax_pthread.m4 ax_require_defined.m4 ax_tls.m4 ax_vcs_checkout.m4 hexversion.m4 lib_socket_nsl.m4 visibility.m4
mcapi
wolfcrypt_mcapi.X
nbproject configurations.xml include.am project.xml
Makefile
wolfcrypt_test.X
nbproject configurations.xml include.am project.xml
Makefile
wolfssl.X
nbproject configurations.xml include.am project.xml
Makefile
zlib.X
nbproject configurations.xml include.am project.xml
Makefile
PIC32MZ-serial.h README crypto.c crypto.h include.am mcapi_test.c user_settings.h
mplabx
wolfcrypt_benchmark.X
nbproject configurations.xml include.am project.xml
Makefile
wolfcrypt_test.X
nbproject configurations.xml include.am project.xml
Makefile
wolfssl.X
nbproject configurations.xml include.am project.xml
Makefile
PIC32MZ-serial.h README benchmark_main.c include.am test_main.c user_settings.h
mqx
util_lib
Sources include.am util.c util.h
wolfcrypt_benchmark
Debugger K70FN1M0.mem init_kinetis.tcl mass_erase_kinetis.tcl
Sources include.am main.c main.h
ReferencedRSESystems.xml wolfcrypt_benchmark_twrk70f120m_Int_Flash_DDRData_Debug_PnE_U-MultiLink.launch wolfcrypt_benchmark_twrk70f120m_Int_Flash_DDRData_Release_PnE_U-MultiLink.launch wolfcrypt_benchmark_twrk70f120m_Int_Flash_SramData_Debug_JTrace.jlink wolfcrypt_benchmark_twrk70f120m_Int_Flash_SramData_Debug_JTrace.launch wolfcrypt_benchmark_twrk70f120m_Int_Flash_SramData_Debug_PnE_U-MultiLink.launch wolfcrypt_benchmark_twrk70f120m_Int_Flash_SramData_Release_PnE_U-MultiLink.launch
wolfcrypt_test
Debugger K70FN1M0.mem init_kinetis.tcl mass_erase_kinetis.tcl
Sources include.am main.c main.h
ReferencedRSESystems.xml wolfcrypt_test_twrk70f120m_Int_Flash_DDRData_Debug_PnE_U-MultiLink.launch wolfcrypt_test_twrk70f120m_Int_Flash_DDRData_Release_PnE_U-MultiLink.launch wolfcrypt_test_twrk70f120m_Int_Flash_SramData_Debug_JTrace.jlink wolfcrypt_test_twrk70f120m_Int_Flash_SramData_Debug_JTrace.launch wolfcrypt_test_twrk70f120m_Int_Flash_SramData_Debug_PnE_U-MultiLink.launch wolfcrypt_test_twrk70f120m_Int_Flash_SramData_Release_PnE_U-MultiLink.launch
wolfssl include.am
wolfssl_client
Debugger K70FN1M0.mem init_kinetis.tcl mass_erase_kinetis.tcl
Sources include.am main.c main.h
ReferencedRSESystems.xml wolfssl_client_twrk70f120m_Int_Flash_DDRData_Debug_PnE_U-MultiLink.launch wolfssl_client_twrk70f120m_Int_Flash_DDRData_Release_PnE_U-MultiLink.launch wolfssl_client_twrk70f120m_Int_Flash_SramData_Debug_JTrace.jlink wolfssl_client_twrk70f120m_Int_Flash_SramData_Debug_JTrace.launch wolfssl_client_twrk70f120m_Int_Flash_SramData_Debug_PnE_U-MultiLink.launch wolfssl_client_twrk70f120m_Int_Flash_SramData_Release_PnE_U-MultiLink.launch
README
rpm include.am spec.in
scripts
bench bench_functions.sh
aria-cmake-build-test.sh asn1_oid_sum.pl benchmark.test benchmark_compare.sh cleanup_testfiles.sh crl-gen-openssl.test crl-revoked.test dertoc.pl dtls.test dtlscid.test external.test google.test include.am makedistsmall.sh memtest.sh ocsp-responder-openssl-interop.test ocsp-stapling-with-ca-as-responder.test ocsp-stapling-with-wolfssl-responder.test ocsp-stapling.test ocsp-stapling2.test ocsp-stapling_tls13multi.test ocsp.test openssl.test openssl_srtp.test pem.test ping.test pkcallbacks.test psk.test resume.test rsapss.test sniffer-gen.sh sniffer-ipv6.pcap sniffer-static-rsa.pcap sniffer-testsuite.test sniffer-tls12-keylog.out sniffer-tls12-keylog.pcap sniffer-tls12-keylog.sslkeylog sniffer-tls13-dh-resume.pcap sniffer-tls13-dh.pcap sniffer-tls13-ecc-resume.pcap sniffer-tls13-ecc.pcap sniffer-tls13-hrr.pcap sniffer-tls13-keylog.out sniffer-tls13-keylog.pcap sniffer-tls13-keylog.sslkeylog sniffer-tls13-x25519-resume.pcap sniffer-tls13-x25519.pcap stm32l4-v4_0_1_build.sh tls13.test trusted_peer.test unit.test.in user_settings_asm.sh
src bio.c conf.c crl.c dtls.c dtls13.c include.am internal.c keys.c ocsp.c pk.c pk_ec.c pk_rsa.c quic.c sniffer.c ssl.c ssl_api_cert.c ssl_api_crl_ocsp.c ssl_api_pk.c ssl_asn1.c ssl_bn.c ssl_certman.c ssl_crypto.c ssl_ech.c ssl_load.c ssl_misc.c ssl_p7p12.c ssl_sess.c ssl_sk.c tls.c tls13.c wolfio.c x509.c x509_str.c
sslSniffer
sslSnifferTest README_WIN.md include.am snifftest.c sslSniffTest.vcproj sslSniffTest.vcxproj
README.md sslSniffer.vcproj sslSniffer.vcxproj
support gen-debug-trace-error-codes.sh include.am wolfssl.pc.in
tests
api api.h api_decl.h create_ocsp_test_blobs.py include.am test_aes.c test_aes.h test_arc4.c test_arc4.h test_ascon.c test_ascon.h test_ascon_kats.h test_asn.c test_asn.h test_blake2.c test_blake2.h test_camellia.c test_camellia.h test_certman.c test_certman.h test_chacha.c test_chacha.h test_chacha20_poly1305.c test_chacha20_poly1305.h test_cmac.c test_cmac.h test_curve25519.c test_curve25519.h test_curve448.c test_curve448.h test_des3.c test_des3.h test_dh.c test_dh.h test_digest.h test_dsa.c test_dsa.h test_dtls.c test_dtls.h test_ecc.c test_ecc.h test_ed25519.c test_ed25519.h test_ed448.c test_ed448.h test_evp.c test_evp.h test_evp_cipher.c test_evp_cipher.h test_evp_digest.c test_evp_digest.h test_evp_pkey.c test_evp_pkey.h test_hash.c test_hash.h test_hmac.c test_hmac.h test_md2.c test_md2.h test_md4.c test_md4.h test_md5.c test_md5.h test_mldsa.c test_mldsa.h test_mlkem.c test_mlkem.h test_ocsp.c test_ocsp.h test_ocsp_test_blobs.h test_ossl_asn1.c test_ossl_asn1.h test_ossl_bio.c test_ossl_bio.h test_ossl_bn.c test_ossl_bn.h test_ossl_cipher.c test_ossl_cipher.h test_ossl_dgst.c test_ossl_dgst.h test_ossl_dh.c test_ossl_dh.h test_ossl_dsa.c test_ossl_dsa.h test_ossl_ec.c test_ossl_ec.h test_ossl_ecx.c test_ossl_ecx.h test_ossl_mac.c test_ossl_mac.h test_ossl_obj.c test_ossl_obj.h test_ossl_p7p12.c test_ossl_p7p12.h test_ossl_pem.c test_ossl_pem.h test_ossl_rand.c test_ossl_rand.h test_ossl_rsa.c test_ossl_rsa.h test_ossl_sk.c test_ossl_sk.h test_ossl_x509.c test_ossl_x509.h test_ossl_x509_acert.c test_ossl_x509_acert.h test_ossl_x509_crypto.c test_ossl_x509_crypto.h test_ossl_x509_ext.c test_ossl_x509_ext.h test_ossl_x509_info.c test_ossl_x509_info.h test_ossl_x509_io.c test_ossl_x509_io.h test_ossl_x509_lu.c test_ossl_x509_lu.h test_ossl_x509_name.c test_ossl_x509_name.h test_ossl_x509_pk.c test_ossl_x509_pk.h test_ossl_x509_str.c test_ossl_x509_str.h test_ossl_x509_vp.c test_ossl_x509_vp.h test_pkcs12.c test_pkcs12.h test_pkcs7.c test_pkcs7.h test_poly1305.c test_poly1305.h test_random.c test_random.h test_rc2.c test_rc2.h test_ripemd.c test_ripemd.h test_rsa.c test_rsa.h test_sha.c test_sha.h test_sha256.c test_sha256.h test_sha3.c test_sha3.h test_sha512.c test_sha512.h test_she.c test_she.h test_signature.c test_signature.h test_slhdsa.c test_slhdsa.h test_sm2.c test_sm2.h test_sm3.c test_sm3.h test_sm4.c test_sm4.h test_tls.c test_tls.h test_tls13.c test_tls13.h test_tls_ext.c test_tls_ext.h test_wc_encrypt.c test_wc_encrypt.h test_wolfmath.c test_wolfmath.h test_x509.c test_x509.h
emnet
IP IP.h
Makefile emnet_nonblock_test.c emnet_shim.c
freertos-mem-track-repro FreeRTOS.h repro.c run.sh semphr.h task.h user_settings.h
swdev .gitignore Makefile README.md swdev.c swdev.h swdev_loader.c swdev_loader.h user_settings.h
CONF_FILES_README.md NCONF_test.cnf README TXT_DB.txt api.c include.am quic.c srp.c suites.c test-altchains.conf test-chains.conf test-dhprime.conf test-dtls-downgrade.conf test-dtls-fails-cipher.conf test-dtls-fails.conf test-dtls-group.conf test-dtls-mtu.conf test-dtls-reneg-client.conf test-dtls-reneg-server.conf test-dtls-resume.conf test-dtls-sha2.conf test-dtls-srtp-fails.conf test-dtls-srtp.conf test-dtls.conf test-dtls13-cid.conf test-dtls13-downgrade-fails.conf test-dtls13-downgrade.conf test-dtls13-pq-hybrid-extra-frag.conf test-dtls13-pq-hybrid-extra.conf test-dtls13-pq-hybrid-frag.conf test-dtls13-pq-standalone-frag.conf test-dtls13-pq-standalone.conf test-dtls13-psk.conf test-dtls13.conf test-ecc-cust-curves.conf test-ed25519.conf test-ed448.conf test-enckeys.conf test-fails.conf test-maxfrag-dtls.conf test-maxfrag.conf test-p521.conf test-psk-no-id-sha2.conf test-psk-no-id.conf test-psk.conf test-rsapss.conf test-sctp-sha2.conf test-sctp.conf test-sha2.conf test-sig.conf test-sm2.conf test-tls-downgrade.conf test-tls13-down.conf test-tls13-ecc.conf test-tls13-pq-hybrid-extra.conf test-tls13-pq-hybrid.conf test-tls13-pq-standalone.conf test-tls13-psk-certs.conf test-tls13-psk.conf test-tls13-slhdsa-fail.conf test-tls13-slhdsa-sha2.conf test-tls13-slhdsa-shake.conf test-tls13.conf test-trustpeer.conf test.conf unit.c unit.h utils.c utils.h w64wrapper.c
testsuite include.am testsuite.c testsuite.sln testsuite.vcproj testsuite.vcxproj utils.c utils.h
tirtos
packages
ti
net
wolfssl
tests
EK_TM4C1294XL
wolfcrypt
benchmark TM4C1294NC.icf benchmark.cfg main.c package.bld.hide package.xdc
test TM4C1294NC.icf main.c package.bld.hide package.xdc test.cfg
package.bld package.xdc package.xs
.gitignore README include.am products.mak wolfssl.bld wolfssl.mak
wolfcrypt
benchmark README.md benchmark-VS2022.sln benchmark-VS2022.vcxproj benchmark-VS2022.vcxproj.user benchmark.c benchmark.h benchmark.sln benchmark.vcproj benchmark.vcxproj include.am
src
port
Espressif
esp_crt_bundle README.md cacrt_all.pem cacrt_deprecated.pem cacrt_local.pem esp_crt_bundle.c gen_crt_bundle.py pio_install_cryptography.py
README.md esp32_aes.c esp32_mp.c esp32_sha.c esp32_util.c esp_sdk_mem_lib.c esp_sdk_time_lib.c esp_sdk_wifi_lib.c
Renesas README.md renesas_common.c renesas_fspsm_aes.c renesas_fspsm_rsa.c renesas_fspsm_sha.c renesas_fspsm_util.c renesas_rx64_hw_sha.c renesas_rx64_hw_util.c renesas_tsip_aes.c renesas_tsip_rsa.c renesas_tsip_sha.c renesas_tsip_util.c
af_alg afalg_aes.c afalg_hash.c wc_afalg.c
aria aria-crypt.c aria-cryptocb.c
arm armv8-32-aes-asm.S armv8-32-aes-asm_c.c armv8-32-chacha-asm.S armv8-32-chacha-asm_c.c armv8-32-curve25519.S armv8-32-curve25519_c.c armv8-32-mlkem-asm.S armv8-32-mlkem-asm_c.c armv8-32-poly1305-asm.S armv8-32-poly1305-asm_c.c armv8-32-sha256-asm.S armv8-32-sha256-asm_c.c armv8-32-sha3-asm.S armv8-32-sha3-asm_c.c armv8-32-sha512-asm.S armv8-32-sha512-asm_c.c armv8-aes-asm.S armv8-aes-asm_c.c armv8-aes.c armv8-chacha-asm.S armv8-chacha-asm_c.c armv8-curve25519.S armv8-curve25519_c.c armv8-mlkem-asm.S armv8-mlkem-asm_c.c armv8-poly1305-asm.S armv8-poly1305-asm_c.c armv8-sha256-asm.S armv8-sha256-asm_c.c armv8-sha256.c armv8-sha3-asm.S armv8-sha3-asm_c.c armv8-sha512-asm.S armv8-sha512-asm_c.c armv8-sha512.c cryptoCell.c cryptoCellHash.c thumb2-aes-asm.S thumb2-aes-asm_c.c thumb2-chacha-asm.S thumb2-chacha-asm_c.c thumb2-curve25519.S thumb2-curve25519_c.c thumb2-mlkem-asm.S thumb2-mlkem-asm_c.c thumb2-poly1305-asm.S thumb2-poly1305-asm_c.c thumb2-sha256-asm.S thumb2-sha256-asm_c.c thumb2-sha3-asm.S thumb2-sha3-asm_c.c thumb2-sha512-asm.S thumb2-sha512-asm_c.c
atmel README.md atmel.c
autosar README.md cryif.c crypto.c csm.c include.am test.c
caam README.md caam_aes.c caam_doc.pdf caam_driver.c caam_error.c caam_integrity.c caam_qnx.c caam_sha.c wolfcaam_aes.c wolfcaam_cmac.c wolfcaam_ecdsa.c wolfcaam_fsl_nxp.c wolfcaam_hash.c wolfcaam_hmac.c wolfcaam_init.c wolfcaam_qnx.c wolfcaam_rsa.c wolfcaam_seco.c wolfcaam_x25519.c
cavium README.md README_Octeon.md cavium_nitrox.c cavium_octeon_sync.c
cuda README.md aes-cuda.cu
cypress README.md psoc6_crypto.c
devcrypto README.md devcrypto_aes.c devcrypto_ecdsa.c devcrypto_hash.c devcrypto_hmac.c devcrypto_rsa.c devcrypto_x25519.c wc_devcrypto.c
intel README.md quickassist.c quickassist_mem.c quickassist_sync.c
iotsafe iotsafe.c
kcapi README.md kcapi_aes.c kcapi_dh.c kcapi_ecc.c kcapi_hash.c kcapi_hmac.c kcapi_rsa.c
liboqs liboqs.c
maxim README.md max3266x.c maxq10xx.c
mynewt mynewt_port.c
nxp README.md README_SE050.md casper_port.c dcp_port.c hashcrypt_port.c ksdk_port.c se050_port.c
pic32 pic32mz-crypt.c
ppc32 ppc32-sha256-asm.S ppc32-sha256-asm_c.c ppc32-sha256-asm_cr.c
psa README.md psa.c psa_aes.c psa_hash.c psa_pkcbs.c
riscv riscv-64-aes.c riscv-64-chacha.c riscv-64-poly1305.c riscv-64-sha256.c riscv-64-sha3.c riscv-64-sha512.c
rpi_pico README.md pico.c
silabs README.md silabs_aes.c silabs_ecc.c silabs_hash.c silabs_random.c
st README.md STM32MP13.md STM32MP25.md stm32.c stsafe.c
ti ti-aes.c ti-ccm.c ti-des3.c ti-hash.c
tropicsquare README.md tropic01.c
xilinx xil-aesgcm.c xil-sha3.c xil-versal-glue.c xil-versal-trng.c
nrf51.c
ASN_TEMPLATE.md aes.c aes_asm.S aes_asm.asm aes_gcm_asm.S aes_gcm_asm.asm aes_gcm_x86_asm.S aes_xts_asm.S aes_xts_asm.asm arc4.c ascon.c asm.c asn.c asn_orig.c async.c blake2b.c blake2s.c camellia.c chacha.c chacha20_poly1305.c chacha_asm.S chacha_asm.asm cmac.c coding.c compress.c cpuid.c cryptocb.c curve25519.c curve448.c des3.c dh.c dilithium.c dsa.c ecc.c ecc_fp.c eccsi.c ed25519.c ed448.c error.c evp.c evp_pk.c falcon.c fe_448.c fe_low_mem.c fe_operations.c fe_x25519_128.h fe_x25519_asm.S fp_mont_small.i fp_mul_comba_12.i fp_mul_comba_17.i fp_mul_comba_20.i fp_mul_comba_24.i fp_mul_comba_28.i fp_mul_comba_3.i fp_mul_comba_32.i fp_mul_comba_4.i fp_mul_comba_48.i fp_mul_comba_6.i fp_mul_comba_64.i fp_mul_comba_7.i fp_mul_comba_8.i fp_mul_comba_9.i fp_mul_comba_small_set.i fp_sqr_comba_12.i fp_sqr_comba_17.i fp_sqr_comba_20.i fp_sqr_comba_24.i fp_sqr_comba_28.i fp_sqr_comba_3.i fp_sqr_comba_32.i fp_sqr_comba_4.i fp_sqr_comba_48.i fp_sqr_comba_6.i fp_sqr_comba_64.i fp_sqr_comba_7.i fp_sqr_comba_8.i fp_sqr_comba_9.i fp_sqr_comba_small_set.i ge_448.c ge_low_mem.c ge_operations.c hash.c hmac.c hpke.c include.am integer.c kdf.c logging.c md2.c md4.c md5.c memory.c misc.c pkcs12.c pkcs7.c poly1305.c poly1305_asm.S poly1305_asm.asm puf.c pwdbased.c random.c rc2.c ripemd.c rng_bank.c rsa.c sakke.c sha.c sha256.c sha256_asm.S sha3.c sha3_asm.S sha512.c sha512_asm.S signature.c siphash.c sm2.c sm3.c sm3_asm.S sm4.c sp_arm32.c sp_arm64.c sp_armthumb.c sp_c32.c sp_c64.c sp_cortexm.c sp_dsp32.c sp_int.c sp_sm2_arm32.c sp_sm2_arm64.c sp_sm2_armthumb.c sp_sm2_c32.c sp_sm2_c64.c sp_sm2_cortexm.c sp_sm2_x86_64.c sp_sm2_x86_64_asm.S sp_x86_64.c sp_x86_64_asm.S sp_x86_64_asm.asm srp.c tfm.c wc_dsp.c wc_encrypt.c wc_lms.c wc_lms_impl.c wc_mldsa_asm.S wc_mlkem.c wc_mlkem_asm.S wc_mlkem_poly.c wc_pkcs11.c wc_port.c wc_she.c wc_slhdsa.c wc_xmss.c wc_xmss_impl.c wolfentropy.c wolfevent.c wolfmath.c
test README.md include.am test-VS2022.sln test-VS2022.vcxproj test-VS2022.vcxproj.user test.c test.h test.sln test.vcproj test_paths.h.in
wolfssl
openssl aes.h asn1.h asn1t.h bio.h bn.h buffer.h camellia.h cmac.h cms.h compat_types.h conf.h crypto.h des.h dh.h dsa.h ec.h ec25519.h ec448.h ecdh.h ecdsa.h ed25519.h ed448.h engine.h err.h evp.h fips_rand.h hmac.h include.am kdf.h lhash.h md4.h md5.h modes.h obj_mac.h objects.h ocsp.h opensslconf.h opensslv.h ossl_typ.h pem.h pkcs12.h pkcs7.h rand.h rc4.h ripemd.h rsa.h safestack.h sha.h sha3.h srp.h ssl.h ssl23.h stack.h tls1.h txt_db.h ui.h x509.h x509_vfy.h x509v3.h
wolfcrypt
port
Espressif esp-sdk-lib.h esp32-crypt.h esp_crt_bundle.h
Renesas renesas-fspsm-crypt.h renesas-fspsm-types.h renesas-rx64-hw-crypt.h renesas-tsip-crypt.h renesas_cmn.h renesas_fspsm_internal.h renesas_sync.h renesas_tsip_internal.h renesas_tsip_types.h
af_alg afalg_hash.h wc_afalg.h
aria aria-crypt.h aria-cryptocb.h
arm cryptoCell.h
atmel atmel.h
autosar CryIf.h Crypto.h Csm.h StandardTypes.h
caam caam_driver.h caam_error.h caam_qnx.h wolfcaam.h wolfcaam_aes.h wolfcaam_cmac.h wolfcaam_ecdsa.h wolfcaam_fsl_nxp.h wolfcaam_hash.h wolfcaam_qnx.h wolfcaam_rsa.h wolfcaam_seco.h wolfcaam_sha.h wolfcaam_x25519.h
cavium cavium_nitrox.h cavium_octeon_sync.h
cypress psoc6_crypto.h
devcrypto wc_devcrypto.h
intel quickassist.h quickassist_mem.h quickassist_sync.h
iotsafe iotsafe.h
kcapi kcapi_dh.h kcapi_ecc.h kcapi_hash.h kcapi_hmac.h kcapi_rsa.h wc_kcapi.h
liboqs liboqs.h
maxim max3266x-cryptocb.h max3266x.h maxq10xx.h
nxp casper_port.h dcp_port.h hashcrypt_port.h ksdk_port.h se050_port.h
pic32 pic32mz-crypt.h
psa psa.h
riscv riscv-64-asm.h
rpi_pico pico.h
silabs silabs_aes.h silabs_ecc.h silabs_hash.h silabs_random.h
st stm32.h stsafe.h
ti ti-ccm.h ti-hash.h
tropicsquare tropic01.h
xilinx xil-sha3.h xil-versal-glue.h xil-versal-trng.h
nrf51.h
aes.h arc4.h ascon.h asn.h asn_public.h async.h blake2-impl.h blake2-int.h blake2.h camellia.h chacha.h chacha20_poly1305.h cmac.h coding.h compress.h cpuid.h cryptocb.h curve25519.h curve448.h des3.h dh.h dilithium.h dsa.h ecc.h eccsi.h ed25519.h ed448.h error-crypt.h falcon.h fe_448.h fe_operations.h fips_test.h ge_448.h ge_operations.h hash.h hmac.h hpke.h include.am integer.h kdf.h libwolfssl_sources.h libwolfssl_sources_asm.h logging.h md2.h md4.h md5.h mem_track.h memory.h misc.h mpi_class.h mpi_superclass.h oid_sum.h pkcs11.h pkcs12.h pkcs7.h poly1305.h puf.h pwdbased.h random.h rc2.h ripemd.h rng_bank.h rsa.h sakke.h selftest.h settings.h sha.h sha256.h sha3.h sha512.h signature.h siphash.h sm2.h sm3.h sm4.h sp.h sp_int.h srp.h tfm.h types.h visibility.h wc_encrypt.h wc_lms.h wc_mlkem.h wc_pkcs11.h wc_port.h wc_she.h wc_slhdsa.h wc_xmss.h wolfentropy.h wolfevent.h wolfmath.h
callbacks.h certs_test.h certs_test_sm.h crl.h error-ssl.h include.am internal.h ocsp.h options.h.in quic.h sniffer.h sniffer_error.h sniffer_error.rc ssl.h test.h version.h version.h.in wolfio.h
wrapper
Ada
examples
src aes_verify_main.adb rsa_verify_main.adb sha256_main.adb spark_sockets.adb spark_sockets.ads spark_terminal.adb spark_terminal.ads tls_client.adb tls_client.ads tls_client_main.adb tls_server.adb tls_server.ads tls_server_main.adb
.gitignore alire.toml examples.gpr
tests
src
support test_support.adb test_support.ads tests_root_suite.adb tests_root_suite.ads
aes_bindings_tests.adb aes_bindings_tests.ads rsa_verify_bindings_tests.adb rsa_verify_bindings_tests.ads sha256_bindings_tests.adb sha256_bindings_tests.ads tests.adb
.gitignore README.md alire.toml tests.gpr valgrind.supp
.gitignore README.md ada_binding.c alire.toml default.gpr include.am restricted.adc user_settings.h wolfssl-full_runtime.adb wolfssl-full_runtime.ads wolfssl.adb wolfssl.ads wolfssl.gpr
CSharp
wolfCrypt-Test
Properties AssemblyInfo.cs
App.config wolfCrypt-Test.cs wolfCrypt-Test.csproj
wolfSSL-DTLS-PSK-Server
Properties AssemblyInfo.cs
App.config wolfSSL-DTLS-PSK-Server.cs wolfSSL-DTLS-PSK-Server.csproj
wolfSSL-DTLS-Server
Properties AssemblyInfo.cs
App.config wolfSSL-DTLS-Server.cs wolfSSL-DTLS-Server.csproj
wolfSSL-Example-IOCallbacks
Properties AssemblyInfo.cs
App.config wolfSSL-Example-IOCallbacks.cs wolfSSL-Example-IOCallbacks.csproj
wolfSSL-TLS-Client
Properties AssemblyInfo.cs
App.config wolfSSL-TLS-Client.cs wolfSSL-TLS-Client.csproj
wolfSSL-TLS-PSK-Client
Properties AssemblyInfo.cs
App.config wolfSSL-TLS-PSK-Client.cs wolfSSL-TLS-PSK-Client.csproj
wolfSSL-TLS-PSK-Server
Properties AssemblyInfo.cs
App.config wolfSSL-TLS-PSK-Server.cs wolfSSL-TLS-PSK-Server.csproj
wolfSSL-TLS-Server
Properties AssemblyInfo.cs
App.config wolfSSL-TLS-Server.cs wolfSSL-TLS-Server.csproj
wolfSSL-TLS-ServerThreaded
Properties AssemblyInfo.cs
App.config wolfSSL-TLS-ServerThreaded.cs wolfSSL-TLS-ServerThreaded.csproj
wolfSSL_CSharp
Properties AssemblyInfo.cs Resources.Designer.cs Resources.resx
X509.cs wolfCrypt.cs wolfSSL.cs wolfSSL_CSharp.csproj
README.md include.am user_settings.h wolfSSL_CSharp.sln wolfssl.vcxproj
python README.md
rust
wolfssl-wolfcrypt
src aes.rs blake2.rs chacha20_poly1305.rs cmac.rs cmac_mac.rs curve25519.rs dh.rs dilithium.rs ecc.rs ecdsa.rs ed25519.rs ed448.rs fips.rs hkdf.rs hmac.rs hmac_mac.rs kdf.rs lib.rs lms.rs mlkem.rs mlkem_kem.rs pbkdf2_password_hash.rs prf.rs random.rs rsa.rs rsa_pkcs1v15.rs sha.rs sha_digest.rs sys.rs
tests
common mod.rs
test_aes.rs test_blake2.rs test_chacha20_poly1305.rs test_cmac.rs test_cmac_mac.rs test_curve25519.rs test_dh.rs test_dilithium.rs test_ecc.rs test_ecdsa.rs test_ed25519.rs test_ed448.rs test_hkdf.rs test_hmac.rs test_hmac_mac.rs test_kdf.rs test_lms.rs test_mlkem.rs test_mlkem_kem.rs test_pbkdf2_password_hash.rs test_prf.rs test_random.rs test_rsa.rs test_rsa_pkcs1v15.rs test_sha.rs test_sha_digest.rs test_wolfcrypt.rs
CHANGELOG.md Cargo.lock Cargo.toml Makefile README.md build.rs headers.h
Makefile README.md include.am
include.am
zephyr
samples
wolfssl_benchmark
boards native_sim.conf nrf5340dk_nrf5340_cpuapp.conf nrf5340dk_nrf5340_cpuapp_ns.conf
CMakeLists.txt README install_test.sh prj.conf sample.yaml zephyr_legacy.conf zephyr_v4.1.conf
wolfssl_test
boards native_sim.conf nrf5340dk_nrf5340_cpuapp.conf nrf5340dk_nrf5340_cpuapp_ns.conf
CMakeLists.txt README install_test.sh prj-no-malloc.conf prj.conf sample.yaml zephyr_legacy.conf zephyr_v4.1.conf
wolfssl_tls_sock
boards native_sim.conf
src tls_sock.c
CMakeLists.txt README install_sample.sh prj-no-malloc.conf prj.conf sample.yaml zephyr_legacy.conf zephyr_v4.1.conf
wolfssl_tls_thread
boards native_sim.conf nrf5340dk_nrf5340_cpuapp.conf nrf5340dk_nrf5340_cpuapp_ns.conf
src tls_threaded.c
CMakeLists.txt README install_sample.sh prj.conf sample.yaml zephyr_legacy.conf zephyr_v4.1.conf
wolfssl options.h
CMakeLists.txt Kconfig Kconfig.tls-generic README.md include.am module.yml user_settings-no-malloc.h user_settings.h zephyr_init.c
.codespellexcludelines .cyignore .editorconfig .gitignore .wolfssl_known_macro_extras AUTHORS CMakeLists.txt CMakePresets.json CMakeSettings.json COPYING ChangeLog.md INSTALL LICENSING LPCExpresso.cproject LPCExpresso.project Makefile.am README README-async.md README.md SCRIPTS-LIST SECURITY-POLICY.md SECURITY-REPORT-TEMPLATE.md Vagrantfile autogen.sh commit-tests.sh configure.ac fips-check.sh fips-hash.sh gencertbuf.pl input pull_to_vagrant.sh quit resource.h stamp-h.in valgrind-bash.supp valgrind-error.sh wnr-example.conf wolfssl-VS2022.vcxproj wolfssl.rc wolfssl.vcproj wolfssl.vcxproj wolfssl64.sln
.clangd .gitignore DOCS.md Makefile README.md assert.c core.c crypto.c env.c fs.c http.c ini.c json.c log.c luna.h main.c makext.mk path.c process.c request.c sqlite.c stash.c template.c util.c
sqlite/shell.c raw
    1/*
    2** This is the amalgamated source code to the "sqlite3" or "sqlite3.exe"
    3** command-line shell (CLI) for SQLite.  This file is automatically
    4** generated by the tool/mkshellc.tcl script from the following sources:
    5**
    6**   ext/expert/sqlite3expert.c
    7**   ext/expert/sqlite3expert.h
    8**   ext/intck/sqlite3intck.c
    9**   ext/intck/sqlite3intck.h
   10**   ext/misc/appendvfs.c
   11**   ext/misc/base64.c
   12**   ext/misc/base85.c
   13**   ext/misc/completion.c
   14**   ext/misc/decimal.c
   15**   ext/misc/fileio.c
   16**   ext/misc/ieee754.c
   17**   ext/misc/memtrace.c
   18**   ext/misc/pcachetrace.c
   19**   ext/misc/regexp.c
   20**   ext/misc/series.c
   21**   ext/misc/sha1.c
   22**   ext/misc/shathree.c
   23**   ext/misc/sqlar.c
   24**   ext/misc/sqlite3_stdio.c
   25**   ext/misc/sqlite3_stdio.h
   26**   ext/misc/stmtrand.c
   27**   ext/misc/uint.c
   28**   ext/misc/vfstrace.c
   29**   ext/misc/windirent.h
   30**   ext/misc/zipfile.c
   31**   ext/qrf/qrf.c
   32**   ext/qrf/qrf.h
   33**   ext/recover/dbdata.c
   34**   ext/recover/sqlite3recover.c
   35**   ext/recover/sqlite3recover.h
   36**   src/shell.c.in
   37**
   38** To modify this program, get a copy of the canonical SQLite source tree,
   39** edit the src/shell.c.in file and/or some of the other files that are
   40** listed above, then rerun the command "make shell.c".
   41*/
   42/************************* Begin src/shell.c.in ******************/
   43/*
   44** 2001 September 15
   45**
   46** The author disclaims copyright to this source code.  In place of
   47** a legal notice, here is a blessing:
   48**
   49**    May you do good and not evil.
   50**    May you find forgiveness for yourself and forgive others.
   51**    May you share freely, never taking more than you give.
   52**
   53*************************************************************************
   54** This file contains code to implement the "sqlite3" command line
   55** utility for accessing SQLite databases.
   56*/
   57#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
   58/* This needs to come before any includes for MSVC compiler */
   59#define _CRT_SECURE_NO_WARNINGS
   60#endif
   61typedef unsigned int u32;
   62typedef unsigned short int u16;
   63
   64/*
   65** Limit input nesting via .read or any other input redirect.
   66** It's not too expensive, so a generous allowance can be made.
   67*/
   68#define MAX_INPUT_NESTING 25
   69
   70/*
   71** Used to prevent warnings about unused parameters
   72*/
   73#define UNUSED_PARAMETER(x) (void)(x)
   74
   75/*
   76** Number of elements in an array
   77*/
   78#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
   79
   80/*
   81** Optionally #include a user-defined header, whereby compilation options
   82** may be set prior to where they take effect, but after platform setup.
   83** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
   84** file. Note that this macro has a like effect on sqlite3.c compilation.
   85*/
   86# define SHELL_STRINGIFY_(f) #f
   87# define SHELL_STRINGIFY(f) SHELL_STRINGIFY_(f)
   88#ifdef SQLITE_CUSTOM_INCLUDE
   89# include SHELL_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
   90#endif
   91
   92/*
   93** If SQLITE_SHELL_FIDDLE is defined then the shell is modified
   94** somewhat for use as a WASM module in a web browser. This flag
   95** should only be used when building the "fiddle" web application, as
   96** the browser-mode build has much different user input requirements
   97** and this build mode rewires the user input subsystem to account for
   98** that.
   99*/
  100#if defined(SQLITE_SHELL_FIDDLE)
  101# undef SQLITE_OMIT_LOAD_EXTENSION
  102# define SQLITE_OMIT_LOAD_EXTENSION 1
  103#endif
  104
  105/*
  106** Warning pragmas copied from msvc.h in the core.
  107*/
  108#if defined(_MSC_VER)
  109#pragma warning(disable : 4054)
  110#pragma warning(disable : 4055)
  111#pragma warning(disable : 4100)
  112#pragma warning(disable : 4127)
  113#pragma warning(disable : 4130)
  114#pragma warning(disable : 4152)
  115#pragma warning(disable : 4189)
  116#pragma warning(disable : 4206)
  117#pragma warning(disable : 4210)
  118#pragma warning(disable : 4232)
  119#pragma warning(disable : 4244)
  120#pragma warning(disable : 4305)
  121#pragma warning(disable : 4306)
  122#pragma warning(disable : 4702)
  123#pragma warning(disable : 4706)
  124#endif /* defined(_MSC_VER) */
  125
  126/*
  127** No support for loadable extensions in VxWorks.
  128*/
  129#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
  130# define SQLITE_OMIT_LOAD_EXTENSION 1
  131#endif
  132
  133/*
  134** Enable large-file support for fopen() and friends on unix.
  135*/
  136#ifndef SQLITE_DISABLE_LFS
  137# define _LARGE_FILE       1
  138# ifndef _FILE_OFFSET_BITS
  139#   define _FILE_OFFSET_BITS 64
  140# endif
  141# define _LARGEFILE_SOURCE 1
  142#endif
  143
  144#if defined(SQLITE_SHELL_FIDDLE) && !defined(_POSIX_SOURCE)
  145/*
  146** emcc requires _POSIX_SOURCE (or one of several similar defines)
  147** to expose strdup().
  148*/
  149# define _POSIX_SOURCE
  150#endif
  151
  152#include <stdlib.h>
  153#include <string.h>
  154#include <stdio.h>
  155#include <assert.h>
  156#include <math.h>
  157#include <stdint.h>
  158#include "sqlite3.h"
  159typedef sqlite3_int64 i64;
  160typedef sqlite3_uint64 u64;
  161typedef unsigned char u8;
  162#include <ctype.h>
  163#include <stdarg.h>
  164#ifndef _WIN32
  165# include <sys/time.h>
  166# include <sys/ioctl.h>
  167#endif
  168
  169#if !defined(_WIN32) && !defined(WIN32)
  170# include <signal.h>
  171# if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
  172#  include <pwd.h>
  173# endif
  174#endif
  175#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
  176# include <unistd.h>
  177# include <dirent.h>
  178# define GETPID getpid
  179# if defined(__MINGW32__)
  180#  define DIRENT dirent
  181#  ifndef S_ISLNK
  182#   define S_ISLNK(mode) (0)
  183#  endif
  184# endif
  185#else
  186# define GETPID (int)GetCurrentProcessId
  187#endif
  188#include <sys/types.h>
  189#include <sys/stat.h>
  190
  191#if HAVE_READLINE
  192# include <readline/readline.h>
  193# include <readline/history.h>
  194#endif
  195
  196#if HAVE_EDITLINE
  197# include <editline/readline.h>
  198#endif
  199
  200#if HAVE_EDITLINE || HAVE_READLINE
  201
  202# define shell_add_history(X) add_history(X)
  203# define shell_read_history(X) read_history(X)
  204# define shell_write_history(X) write_history(X)
  205# define shell_stifle_history(X) stifle_history(X)
  206# define shell_readline(X) readline(X)
  207
  208#elif HAVE_LINENOISE
  209
  210# include "linenoise.h"
  211# define shell_add_history(X) linenoiseHistoryAdd(X)
  212# define shell_read_history(X) linenoiseHistoryLoad(X)
  213# define shell_write_history(X) linenoiseHistorySave(X)
  214# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
  215# define shell_readline(X) linenoise(X)
  216
  217#else
  218
  219# define shell_read_history(X)
  220# define shell_write_history(X)
  221# define shell_stifle_history(X)
  222
  223# define SHELL_USE_LOCAL_GETLINE 1
  224#endif
  225
  226#ifndef deliberate_fall_through
  227/* Quiet some compilers about some of our intentional code. */
  228# if defined(GCC_VERSION) && GCC_VERSION>=7000000
  229#  define deliberate_fall_through __attribute__((fallthrough));
  230# else
  231#  define deliberate_fall_through
  232# endif
  233#endif
  234
  235#if defined(_WIN32) || defined(WIN32)
  236#  include <io.h>
  237#  include <fcntl.h>
  238#  define isatty(h) _isatty(h)
  239#  ifndef access
  240#   define access(f,m) _access((f),(m))
  241#  endif
  242#  ifndef unlink
  243#   define unlink _unlink
  244#  endif
  245#  ifndef strdup
  246#   define strdup _strdup
  247#  endif
  248#  undef pclose
  249#  define pclose _pclose
  250#else
  251 /* Make sure isatty() has a prototype. */
  252 extern int isatty(int);
  253
  254# if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
  255  /* popen and pclose are not C89 functions and so are
  256  ** sometimes omitted from the <stdio.h> header */
  257   extern FILE *popen(const char*,const char*);
  258   extern int pclose(FILE*);
  259# else
  260#  define SQLITE_OMIT_POPEN 1
  261# endif
  262#endif
  263
  264#if defined(_WIN32_WCE)
  265/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
  266 * thus we always assume that we have a console. That can be
  267 * overridden with the -batch command line option.
  268 */
  269#define isatty(x) 1
  270#endif
  271
  272/* ctype macros that work with signed characters */
  273#define IsSpace(X)  isspace((unsigned char)X)
  274#define IsDigit(X)  isdigit((unsigned char)X)
  275#define ToLower(X)  (char)tolower((unsigned char)X)
  276#define IsAlnum(X)  isalnum((unsigned char)X)
  277#define IsAlpha(X)  isalpha((unsigned char)X)
  278
  279#if defined(_WIN32) || defined(WIN32)
  280#undef WIN32_LEAN_AND_MEAN
  281#define WIN32_LEAN_AND_MEAN
  282#include <windows.h>
  283
  284/* string conversion routines only needed on Win32 */
  285extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
  286extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
  287#endif
  288
  289/************************* Begin ext/misc/sqlite3_stdio.h ******************/
  290/*
  291** 2024-09-24
  292**
  293** The author disclaims copyright to this source code.  In place of
  294** a legal notice, here is a blessing:
  295**
  296**    May you do good and not evil.
  297**    May you find forgiveness for yourself and forgive others.
  298**    May you share freely, never taking more than you give.
  299**
  300*************************************************************************
  301**
  302** This header file contains definitions of interfaces that provide 
  303** cross-platform I/O for UTF-8 content.
  304**
  305** On most platforms, the interfaces definitions in this file are
  306** just #defines.  For example sqlite3_fopen() is a macro that resolves
  307** to the standard fopen() in the C-library.
  308**
  309** But Windows does not have a standard C-library, at least not one that
  310** can handle UTF-8.  So for windows build, the interfaces resolve to new
  311** C-language routines contained in the separate sqlite3_stdio.c source file.
  312**
  313** So on all non-Windows platforms, simply #include this header file and
  314** use the interfaces defined herein.  Then to run your application on Windows,
  315** also link in the accompanying sqlite3_stdio.c source file when compiling
  316** to get compatible interfaces.
  317*/
  318#ifndef _SQLITE3_STDIO_H_
  319#define _SQLITE3_STDIO_H_ 1
  320#ifdef _WIN32
  321/**** Definitions For Windows ****/
  322#include <stdio.h>
  323#include <stdarg.h>
  324#include <windows.h>
  325
  326FILE *sqlite3_fopen(const char *zFilename, const char *zMode);
  327FILE *sqlite3_popen(const char *zCommand, const char *type);
  328char *sqlite3_fgets(char *s, int size, FILE *stream);
  329int sqlite3_fputs(const char *s, FILE *stream);
  330int sqlite3_fprintf(FILE *stream, const char *format, ...);
  331int sqlite3_vfprintf(FILE *stream, const char *format, va_list);
  332void sqlite3_fsetmode(FILE *stream, int mode);
  333
  334
  335#else
  336/**** Definitions For All Other Platforms ****/
  337#include <stdio.h>
  338#define sqlite3_fopen     fopen
  339#define sqlite3_popen     popen
  340#define sqlite3_fgets     fgets
  341#define sqlite3_fputs     fputs
  342#define sqlite3_fprintf   fprintf
  343#define sqlite3_vfprintf  vfprintf
  344#define sqlite3_fsetmode(F,X)   /*no-op*/
  345
  346#endif
  347#endif /* _SQLITE3_STDIO_H_ */
  348
  349/************************* End ext/misc/sqlite3_stdio.h ********************/
  350/************************* Begin ext/misc/sqlite3_stdio.c ******************/
  351/*
  352** 2024-09-24
  353**
  354** The author disclaims copyright to this source code.  In place of
  355** a legal notice, here is a blessing:
  356**
  357**    May you do good and not evil.
  358**    May you find forgiveness for yourself and forgive others.
  359**    May you share freely, never taking more than you give.
  360**
  361*************************************************************************
  362**
  363** Implementation of standard I/O interfaces for UTF-8 that are missing
  364** on Windows.
  365*/
  366#ifdef _WIN32  /* This file is a no-op on all platforms except Windows */
  367#ifndef _SQLITE3_STDIO_H_
  368/* #include "sqlite3_stdio.h" */
  369#endif
  370#undef WIN32_LEAN_AND_MEAN
  371#define WIN32_LEAN_AND_MEAN
  372#include <windows.h>
  373#include <stdlib.h>
  374#include <string.h>
  375#include <stdio.h>
  376#include <assert.h>
  377/* #include "sqlite3.h" */
  378#include <ctype.h>
  379#include <stdarg.h>
  380#include <io.h>
  381#include <fcntl.h>
  382
  383/*
  384** If the SQLITE_U8TEXT_ONLY option is defined, then use O_U8TEXT
  385** when appropriate on all output.  (Sometimes use O_BINARY when
  386** rendering ASCII text in cases where NL-to-CRLF expansion would
  387** not be correct.)
  388**
  389** If the SQLITE_U8TEXT_STDIO option is defined, then use O_U8TEXT
  390** when appropriate when writing to stdout or stderr.  Use O_BINARY
  391** or O_TEXT (depending on things like the .mode and the .crlf setting
  392** in the CLI, or other context clues in other applications) for all
  393** other output channels.
  394**
  395** The default behavior, if neither of the above is defined is to
  396** use O_U8TEXT when writing to the Windows console (or anything
  397** else for which _isatty() returns true) and to use O_BINARY or O_TEXT
  398** for all other output channels.
  399**
  400** The SQLITE_USE_W32_FOR_CONSOLE_IO macro is also available.  If
  401** defined, it forces the use of Win32 APIs for all console I/O, both
  402** input and output.  This is necessary for some non-Microsoft run-times
  403** that implement stdio differently from Microsoft/Visual-Studio.
  404*/
  405#if defined(SQLITE_U8TEXT_ONLY)
  406# define UseWtextForOutput(fd) 1
  407# define UseWtextForInput(fd)  1
  408# define IsConsole(fd)         _isatty(_fileno(fd))
  409#elif defined(SQLITE_U8TEXT_STDIO)
  410# define UseWtextForOutput(fd) ((fd)==stdout || (fd)==stderr)
  411# define UseWtextForInput(fd)  ((fd)==stdin)
  412# define IsConsole(fd)         _isatty(_fileno(fd))
  413#else
  414# define UseWtextForOutput(fd) _isatty(_fileno(fd))
  415# define UseWtextForInput(fd)  _isatty(_fileno(fd))
  416# define IsConsole(fd)         1
  417#endif
  418
  419/*
  420** Global variables determine if simulated O_BINARY mode is to be
  421** used for stdout or other, respectively.  Simulated O_BINARY mode
  422** means the mode is usually O_BINARY, but switches to O_U8TEXT for
  423** unicode characters U+0080 or greater (any character that has a
  424** multi-byte representation in UTF-8).  This is the only way we
  425** have found to render Unicode characters on a Windows console while
  426** at the same time avoiding undesirable \n to \r\n translation.
  427*/
  428static int simBinaryStdout = 0;
  429static int simBinaryOther = 0;
  430
  431
  432/*
  433** Determine if simulated binary mode should be used for output to fd
  434*/
  435static int UseBinaryWText(FILE *fd){
  436  if( fd==stdout || fd==stderr ){
  437    return simBinaryStdout;
  438  }else{
  439    return simBinaryOther;
  440  }
  441}
  442
  443
  444/*
  445** Work-alike for the fopen() routine from the standard C library.
  446*/
  447FILE *sqlite3_fopen(const char *zFilename, const char *zMode){
  448  FILE *fp = 0;
  449  wchar_t *b1, *b2;
  450  int sz1, sz2;
  451
  452  sz1 = (int)strlen(zFilename);
  453  sz2 = (int)strlen(zMode);
  454  b1 = sqlite3_malloc64( (sz1+1)*sizeof(b1[0]) );
  455  b2 = sqlite3_malloc64( (sz2+1)*sizeof(b1[0]) );
  456  if( b1 && b2 ){
  457    sz1 = MultiByteToWideChar(CP_UTF8, 0, zFilename, sz1, b1, sz1);
  458    b1[sz1] = 0;
  459    sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2);
  460    b2[sz2] = 0;
  461    fp = _wfopen(b1, b2);
  462  }
  463  sqlite3_free(b1);
  464  sqlite3_free(b2);
  465  simBinaryOther = 0;
  466  return fp;
  467}
  468
  469
  470/*
  471** Work-alike for the popen() routine from the standard C library.
  472*/
  473FILE *sqlite3_popen(const char *zCommand, const char *zMode){
  474  FILE *fp = 0;
  475  wchar_t *b1, *b2;
  476  int sz1, sz2;
  477
  478  sz1 = (int)strlen(zCommand);
  479  sz2 = (int)strlen(zMode);
  480  b1 = sqlite3_malloc64( (sz1+1)*sizeof(b1[0]) );
  481  b2 = sqlite3_malloc64( (sz2+1)*sizeof(b1[0]) );
  482  if( b1 && b2 ){
  483    sz1 = MultiByteToWideChar(CP_UTF8, 0, zCommand, sz1, b1, sz1);
  484    b1[sz1] = 0;
  485    sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2);
  486    b2[sz2] = 0;
  487    fp = _wpopen(b1, b2);
  488  }
  489  sqlite3_free(b1);
  490  sqlite3_free(b2);
  491  return fp;
  492}
  493
  494/*
  495** Work-alike for fgets() from the standard C library.
  496*/
  497char *sqlite3_fgets(char *buf, int sz, FILE *in){
  498  if( UseWtextForInput(in) ){
  499    /* When reading from the command-prompt in Windows, it is necessary
  500    ** to use _O_WTEXT input mode to read UTF-16 characters, then translate
  501    ** that into UTF-8.  Otherwise, non-ASCII characters all get translated
  502    ** into '?'.
  503    */
  504    wchar_t *b1 = sqlite3_malloc64( sz*sizeof(wchar_t) );
  505    if( b1==0 ) return 0;
  506#ifdef SQLITE_USE_W32_FOR_CONSOLE_IO
  507    DWORD nRead = 0;
  508    if( IsConsole(in)
  509     && ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), b1, sz-1, &nRead, 0)
  510    ){
  511      b1[nRead] = 0;
  512    }else
  513#endif
  514    {
  515      _setmode(_fileno(in), IsConsole(in) ? _O_WTEXT : _O_U8TEXT);
  516      if( fgetws(b1, sz/4, in)==0 ){
  517        sqlite3_free(b1);
  518        return 0;
  519      }
  520    }
  521    WideCharToMultiByte(CP_UTF8, 0, b1, -1, buf, sz, 0, 0);
  522    sqlite3_free(b1);
  523    return buf;
  524  }else{
  525    /* Reading from a file or other input source, just read bytes without
  526    ** any translation. */
  527    return fgets(buf, sz, in);
  528  }
  529}
  530
  531/*
  532** Send ASCII text as O_BINARY.  But for Unicode characters U+0080 and
  533** greater, switch to O_U8TEXT.
  534*/
  535static void piecemealOutput(wchar_t *b1, int sz, FILE *out){
  536  int i;
  537  wchar_t c;
  538  while( sz>0 ){
  539    for(i=0; i<sz && b1[i]>=0x80; i++){}
  540    if( i>0 ){
  541      c = b1[i];
  542      b1[i] = 0;
  543      fflush(out);
  544      _setmode(_fileno(out), _O_U8TEXT);
  545      fputws(b1, out);
  546      fflush(out);
  547      b1 += i;
  548      b1[0] = c;
  549      sz -= i;
  550    }else{
  551      fflush(out);
  552      _setmode(_fileno(out), _O_TEXT);
  553      _setmode(_fileno(out), _O_BINARY);
  554      fwrite(&b1[0], 1, 1, out);
  555      for(i=1; i<sz && b1[i]<0x80; i++){
  556        fwrite(&b1[i], 1, 1, out);
  557      }
  558      fflush(out);
  559      _setmode(_fileno(out), _O_U8TEXT);
  560      b1 += i;
  561      sz -= i;
  562    }
  563  }
  564}
  565
  566/*
  567** Work-alike for fputs() from the standard C library.
  568*/
  569int sqlite3_fputs(const char *z, FILE *out){
  570  if( !UseWtextForOutput(out) ){
  571    /* Writing to a file or other destination, just write bytes without
  572    ** any translation. */
  573    return fputs(z, out);
  574  }else{
  575    /* One must use UTF16 in order to get unicode support when writing
  576    ** to the console on Windows. 
  577    */
  578    int sz = (int)strlen(z);
  579    wchar_t *b1 = sqlite3_malloc64( (sz+1)*sizeof(wchar_t) );
  580    if( b1==0 ) return 0;
  581    sz = MultiByteToWideChar(CP_UTF8, 0, z, sz, b1, sz);
  582    b1[sz] = 0;
  583
  584#ifdef SQLITE_USE_W32_FOR_CONSOLE_IO
  585    DWORD nWr = 0;
  586    if( IsConsole(out)
  587      && WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),b1,sz,&nWr,0)
  588    ){
  589      /* If writing to the console, then the WriteConsoleW() is all we
  590      ** need to do. */
  591    }else
  592#endif
  593    {
  594      /* As long as SQLITE_USE_W32_FOR_CONSOLE_IO is not defined, or for
  595      ** non-console I/O even if that macro is defined, write using the
  596      ** standard library. */
  597      _setmode(_fileno(out), _O_U8TEXT);
  598      if( UseBinaryWText(out) ){
  599        piecemealOutput(b1, sz, out);
  600      }else{
  601        fputws(b1, out);
  602      }
  603    }
  604    sqlite3_free(b1);
  605    return 0;
  606  }
  607}
  608
  609
  610/*
  611** Work-alikes for fprintf() and vfprintf() from the standard C library.
  612*/
  613int sqlite3_fprintf(FILE *out, const char *zFormat, ...){
  614  int rc;
  615  if( UseWtextForOutput(out) ){
  616    /* When writing to the command-prompt in Windows, it is necessary
  617    ** to use _O_WTEXT input mode and write UTF-16 characters.
  618    */
  619    char *z;
  620    va_list ap;
  621
  622    va_start(ap, zFormat);
  623    z = sqlite3_vmprintf(zFormat, ap);
  624    va_end(ap);
  625    sqlite3_fputs(z, out);
  626    rc = (int)strlen(z);
  627    sqlite3_free(z);
  628  }else{
  629    /* Writing to a file or other destination, just write bytes without
  630    ** any translation. */
  631    va_list ap;
  632    va_start(ap, zFormat);
  633    rc = vfprintf(out, zFormat, ap);
  634    va_end(ap);
  635  }
  636  return rc;
  637}
  638int sqlite3_vfprintf(FILE *out, const char *zFormat, va_list ap){
  639  int rc;
  640  if( UseWtextForOutput(out) ){
  641    /* When writing to the command-prompt in Windows, it is necessary
  642    ** to use _O_WTEXT input mode and write UTF-16 characters.
  643    */
  644    char *z;
  645    z = sqlite3_vmprintf(zFormat, ap);
  646    sqlite3_fputs(z, out);
  647    rc = (int)strlen(z);
  648    sqlite3_free(z);
  649  }else{
  650    /* Writing to a file or other destination, just write bytes without
  651    ** any translation. */
  652    rc = vfprintf(out, zFormat, ap);
  653  }
  654  return rc;
  655}
  656
  657/*
  658** Set the mode for an output stream.  mode argument is typically _O_BINARY or
  659** _O_TEXT.
  660*/
  661void sqlite3_fsetmode(FILE *fp, int mode){
  662  if( !UseWtextForOutput(fp) ){
  663    fflush(fp);
  664    _setmode(_fileno(fp), mode);
  665  }else if( fp==stdout || fp==stderr ){
  666    simBinaryStdout = (mode==_O_BINARY);
  667  }else{
  668    simBinaryOther = (mode==_O_BINARY);
  669  }
  670}
  671
  672#endif /* defined(_WIN32) */
  673
  674/************************* End ext/misc/sqlite3_stdio.c ********************/
  675/************************* Begin ext/qrf/qrf.h ******************/
  676/*
  677** 2025-10-20
  678**
  679** The author disclaims copyright to this source code.  In place of
  680** a legal notice, here is a blessing:
  681**
  682**    May you do good and not evil.
  683**    May you find forgiveness for yourself and forgive others.
  684**    May you share freely, never taking more than you give.
  685**
  686*************************************************************************
  687** Header file for the Query Result-Format or "qrf" utility library for
  688** SQLite.  See the README.md documentation for additional information.
  689*/
  690#ifndef SQLITE_QRF_H
  691#define SQLITE_QRF_H
  692#ifdef __cplusplus
  693extern "C" {
  694#endif
  695#include <stdlib.h>
  696/* #include "sqlite3.h" */
  697
  698/*
  699** Specification used by clients to define the output format they want
  700*/
  701typedef struct sqlite3_qrf_spec sqlite3_qrf_spec;
  702struct sqlite3_qrf_spec {
  703  unsigned char iVersion;     /* Version number of this structure */
  704  unsigned char eStyle;       /* Formatting style.  "box", "csv", etc... */
  705  unsigned char eEsc;         /* How to escape control characters in text */
  706  unsigned char eText;        /* Quoting style for text */
  707  unsigned char eTitle;       /* Quating style for the text of column names */
  708  unsigned char eBlob;        /* Quoting style for BLOBs */
  709  unsigned char bTitles;      /* True to show column names */
  710  unsigned char bWordWrap;    /* Try to wrap on word boundaries */
  711  unsigned char bTextJsonb;   /* Render JSONB blobs as JSON text */
  712  unsigned char eDfltAlign;   /* Default alignment, no covered by aAlignment */
  713  unsigned char eTitleAlign;  /* Alignment for column headers */
  714  unsigned char bSplitColumn; /* Wrap single-column output into many columns */
  715  unsigned char bBorder;      /* Show outer border in Box and Table styles */
  716  short int nWrap;            /* Wrap columns wider than this */
  717  short int nScreenWidth;     /* Maximum overall table width */
  718  short int nLineLimit;       /* Maximum number of lines for any row */
  719  short int nTitleLimit;      /* Maximum number of characters in a title */
  720  unsigned int nMultiInsert;  /* Add rows to one INSERT until size exceeds */
  721  int nCharLimit;             /* Maximum number of characters in a cell */
  722  int nWidth;                 /* Number of entries in aWidth[] */
  723  int nAlign;                 /* Number of entries in aAlignment[] */
  724  short int *aWidth;          /* Column widths */
  725  unsigned char *aAlign;      /* Column alignments */
  726  char *zColumnSep;           /* Alternative column separator */
  727  char *zRowSep;              /* Alternative row separator */
  728  char *zTableName;           /* Output table name */
  729  char *zNull;                /* Rendering of NULL */
  730  char *(*xRender)(void*,sqlite3_value*);           /* Render a value */
  731  int (*xWrite)(void*,const char*,sqlite3_int64);   /* Write output */
  732  void *pRenderArg;           /* First argument to the xRender callback */
  733  void *pWriteArg;            /* First argument to the xWrite callback */
  734  char **pzOutput;            /* Storage location for output string */
  735  /* Additional fields may be added in the future */
  736};
  737
  738/*
  739** Interfaces
  740*/
  741int sqlite3_format_query_result(
  742  sqlite3_stmt *pStmt,             /* SQL statement to run */
  743  const sqlite3_qrf_spec *pSpec,   /* Result format specification */
  744  char **pzErr                     /* OUT: Write error message here */
  745);
  746
  747/*
  748** Range of values for sqlite3_qrf_spec.aWidth[] entries and for
  749** sqlite3_qrf_spec.mxColWidth and .nScreenWidth
  750*/
  751#define QRF_MAX_WIDTH    10000
  752#define QRF_MIN_WIDTH    0
  753
  754/*
  755** Output styles:
  756*/
  757#define QRF_STYLE_Auto      0 /* Choose a style automatically */
  758#define QRF_STYLE_Box       1 /* Unicode box-drawing characters */
  759#define QRF_STYLE_Column    2 /* One record per line in neat columns */
  760#define QRF_STYLE_Count     3 /* Output only a count of the rows of output */
  761#define QRF_STYLE_Csv       4 /* Comma-separated-value */
  762#define QRF_STYLE_Eqp       5 /* Format EXPLAIN QUERY PLAN output */
  763#define QRF_STYLE_Explain   6 /* EXPLAIN output */
  764#define QRF_STYLE_Html      7 /* Generate an XHTML table */
  765#define QRF_STYLE_Insert    8 /* Generate SQL "insert" statements */
  766#define QRF_STYLE_Json      9 /* Output is a list of JSON objects */
  767#define QRF_STYLE_JObject  10 /* Independent JSON objects for each row */
  768#define QRF_STYLE_Line     11 /* One column per line. */
  769#define QRF_STYLE_List     12 /* One record per line with a separator */
  770#define QRF_STYLE_Markdown 13 /* Markdown formatting */
  771#define QRF_STYLE_Off      14 /* No query output shown */
  772#define QRF_STYLE_Quote    15 /* SQL-quoted, comma-separated */
  773#define QRF_STYLE_Stats    16 /* EQP-like output but with performance stats */
  774#define QRF_STYLE_StatsEst 17 /* EQP-like output with planner estimates */
  775#define QRF_STYLE_StatsVm  18 /* EXPLAIN-like output with performance stats */
  776#define QRF_STYLE_Table    19 /* MySQL-style table formatting */
  777
  778/*
  779** Quoting styles for text.
  780** Allowed values for sqlite3_qrf_spec.eText
  781*/
  782#define QRF_TEXT_Auto    0 /* Choose text encoding automatically */
  783#define QRF_TEXT_Plain   1 /* Literal text */
  784#define QRF_TEXT_Sql     2 /* Quote as an SQL literal */
  785#define QRF_TEXT_Csv     3 /* CSV-style quoting */
  786#define QRF_TEXT_Html    4 /* HTML-style quoting */
  787#define QRF_TEXT_Tcl     5 /* C/Tcl quoting */
  788#define QRF_TEXT_Json    6 /* JSON quoting */
  789#define QRF_TEXT_Relaxed 7 /* Relaxed SQL quoting */
  790
  791/*
  792** Quoting styles for BLOBs
  793** Allowed values for sqlite3_qrf_spec.eBlob
  794*/
  795#define QRF_BLOB_Auto    0 /* Determine BLOB quoting using eText */
  796#define QRF_BLOB_Text    1 /* Display content exactly as it is */
  797#define QRF_BLOB_Sql     2 /* Quote as an SQL literal */
  798#define QRF_BLOB_Hex     3 /* Hexadecimal representation */
  799#define QRF_BLOB_Tcl     4 /* "\000" notation */
  800#define QRF_BLOB_Json    5 /* A JSON string */
  801#define QRF_BLOB_Size    6 /* Display the blob size only */
  802
  803/*
  804** Control-character escape modes.
  805** Allowed values for sqlite3_qrf_spec.eEsc
  806*/
  807#define QRF_ESC_Auto    0 /* Choose the ctrl-char escape automatically */
  808#define QRF_ESC_Off     1 /* Do not escape control characters */
  809#define QRF_ESC_Ascii   2 /* Unix-style escapes.  Ex: U+0007 shows ^G */
  810#define QRF_ESC_Symbol  3 /* Unicode escapes. Ex: U+0007 shows U+2407 */
  811
  812/*
  813** Allowed values for "boolean" fields, such as "bColumnNames", "bWordWrap",
  814** and "bTextJsonb".  There is an extra "auto" variants so these are actually
  815** tri-state settings, not booleans.
  816*/
  817#define QRF_SW_Auto     0 /* Let QRF choose the best value */
  818#define QRF_SW_Off      1 /* This setting is forced off */
  819#define QRF_SW_On       2 /* This setting is forced on */
  820#define QRF_Auto        0 /* Alternate spelling for QRF_*_Auto */
  821#define QRF_No          1 /* Alternate spelling for QRF_SW_Off */
  822#define QRF_Yes         2 /* Alternate spelling for QRF_SW_On */
  823
  824/*
  825** Possible alignment values alignment settings
  826**
  827**                             Horizontal   Vertial
  828**                             ----------   --------  */
  829#define QRF_ALIGN_Auto    0 /*   auto        auto     */
  830#define QRF_ALIGN_Left    1 /*   left        auto     */
  831#define QRF_ALIGN_Center  2 /*   center      auto     */
  832#define QRF_ALIGN_Right   3 /*   right       auto     */
  833#define QRF_ALIGN_Top     4 /*   auto        top      */
  834#define QRF_ALIGN_NW      5 /*   left        top      */
  835#define QRF_ALIGN_N       6 /*   center      top      */
  836#define QRF_ALIGN_NE      7 /*   right       top      */
  837#define QRF_ALIGN_Middle  8 /*   auto        middle   */
  838#define QRF_ALIGN_W       9 /*   left        middle   */
  839#define QRF_ALIGN_C      10 /*   center      middle   */
  840#define QRF_ALIGN_E      11 /*   right       middle   */
  841#define QRF_ALIGN_Bottom 12 /*   auto        bottom   */
  842#define QRF_ALIGN_SW     13 /*   left        bottom   */
  843#define QRF_ALIGN_S      14 /*   center      bottom   */
  844#define QRF_ALIGN_SE     15 /*   right       bottom   */
  845#define QRF_ALIGN_HMASK   3 /* Horizontal alignment mask */
  846#define QRF_ALIGN_VMASK  12 /* Vertical alignment mask */
  847
  848/*
  849** Auxiliary routines contined within this module that might be useful
  850** in other contexts, and which are therefore exported.
  851*/
  852/*
  853** Return an estimate of the width, in columns, for the single Unicode
  854** character c.  For normal characters, the answer is always 1.  But the
  855** estimate might be 0 or 2 for zero-width and double-width characters.
  856**
  857** Different devices display unicode using different widths.  So
  858** it is impossible to know that true display width with 100% accuracy.
  859** Inaccuracies in the width estimates might cause columns to be misaligned.
  860** Unfortunately, there is nothing we can do about that.
  861*/
  862int sqlite3_qrf_wcwidth(int c);
  863
  864/*
  865** Return an estimate of the number of display columns used by the
  866** string in the argument.  The width of individual characters is
  867** determined as for sqlite3_qrf_wcwidth().  VT100 escape code sequences
  868** are assigned a width of zero.
  869*/
  870size_t sqlite3_qrf_wcswidth(const char*);
  871
  872
  873#ifdef __cplusplus
  874}
  875#endif
  876#endif /* !defined(SQLITE_QRF_H) */
  877
  878/************************* End ext/qrf/qrf.h ********************/
  879/************************* Begin ext/qrf/qrf.c ******************/
  880/*
  881** 2025-10-20
  882**
  883** The author disclaims copyright to this source code.  In place of
  884** a legal notice, here is a blessing:
  885**
  886**    May you do good and not evil.
  887**    May you find forgiveness for yourself and forgive others.
  888**    May you share freely, never taking more than you give.
  889**
  890*************************************************************************
  891** Implementation of the Query Result-Format or "qrf" utility library for
  892** SQLite.  See the README.md documentation for additional information.
  893*/
  894#ifndef SQLITE_QRF_H
  895#include "qrf.h"
  896#endif
  897#include <string.h>
  898#include <assert.h>
  899#include <stdint.h>
  900
  901#ifndef SQLITE_AMALGAMATION
  902/* typedef sqlite3_int64 i64; */
  903#endif
  904
  905/* A single line in the EQP output */
  906typedef struct qrfEQPGraphRow qrfEQPGraphRow;
  907struct qrfEQPGraphRow {
  908  int iEqpId;            /* ID for this row */
  909  int iParentId;         /* ID of the parent row */
  910  qrfEQPGraphRow *pNext; /* Next row in sequence */
  911  char zText[1];         /* Text to display for this row */
  912};
  913
  914/* All EQP output is collected into an instance of the following */
  915typedef struct qrfEQPGraph qrfEQPGraph;
  916struct qrfEQPGraph {
  917  qrfEQPGraphRow *pRow;  /* Linked list of all rows of the EQP output */
  918  qrfEQPGraphRow *pLast; /* Last element of the pRow list */
  919  int nWidth;            /* Width of the graph */
  920  char zPrefix[400];     /* Graph prefix */
  921};
  922
  923/*
  924** Private state information.  Subject to change from one release to the
  925** next.
  926*/
  927typedef struct Qrf Qrf;
  928struct Qrf {
  929  sqlite3_stmt *pStmt;        /* The statement whose output is to be rendered */
  930  sqlite3 *db;                /* The corresponding database connection */
  931  sqlite3_stmt *pJTrans;      /* JSONB to JSON translator statement */
  932  char **pzErr;               /* Write error message here, if not NULL */
  933  sqlite3_str *pOut;          /* Accumulated output */
  934  int iErr;                   /* Error code */
  935  int nCol;                   /* Number of output columns */
  936  int expMode;                /* Original sqlite3_stmt_isexplain() plus 1 */
  937  int mxWidth;                /* Screen width */
  938  int mxHeight;               /* nLineLimit */
  939  union {
  940    struct {                  /* Content for QRF_STYLE_Line */
  941      int mxColWth;             /* Maximum display width of any column */
  942      char **azCol;             /* Names of output columns (MODE_Line) */
  943    } sLine;
  944    qrfEQPGraph *pGraph;      /* EQP graph (Eqp, Stats, and StatsEst) */
  945    struct {                  /* Content for QRF_STYLE_Explain */
  946      int nIndent;              /* Slots allocated for aiIndent */
  947      int iIndent;              /* Current slot */
  948      int *aiIndent;            /* Indentation for each opcode */
  949    } sExpln;
  950    unsigned int nIns;        /* Bytes used for current INSERT stmt */
  951  } u;
  952  sqlite3_int64 nRow;         /* Number of rows handled so far */
  953  int *actualWidth;           /* Actual width of each column */
  954  sqlite3_qrf_spec spec;      /* Copy of the original spec */
  955};
  956
  957/*
  958** Data for substitute ctype.h functions.  Used for x-platform
  959** consistency and so that '_' is counted as an alphabetic
  960** character.
  961**
  962**    0x01 -  space
  963**    0x02 -  digit
  964**    0x04 -  alphabetic, including '_'
  965*/
  966static const char qrfCType[] = {
  967  0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0,
  968  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  969  1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  970  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0,
  971  0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  972  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 4,
  973  0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  974  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0,
  975  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  976  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  977  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  978  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  979  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  980  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  981  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  982  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  983};
  984#define qrfSpace(x) ((qrfCType[(unsigned char)x]&1)!=0)
  985#define qrfDigit(x) ((qrfCType[(unsigned char)x]&2)!=0)
  986#define qrfAlpha(x) ((qrfCType[(unsigned char)x]&4)!=0)
  987#define qrfAlnum(x) ((qrfCType[(unsigned char)x]&6)!=0)
  988
  989#ifndef deliberate_fall_through
  990/* Quiet some compilers about some of our intentional code. */
  991# if defined(GCC_VERSION) && GCC_VERSION>=7000000
  992#  define deliberate_fall_through __attribute__((fallthrough));
  993# else
  994#  define deliberate_fall_through
  995# endif
  996#endif
  997
  998/*
  999** Set an error code and error message.
 1000*/
 1001static void qrfError(
 1002  Qrf *p,                /* Query result state */
 1003  int iCode,             /* Error code */
 1004  const char *zFormat,   /* Message format (or NULL) */
 1005  ...
 1006){
 1007  p->iErr = iCode;
 1008  if( p->pzErr!=0 ){
 1009    sqlite3_free(*p->pzErr);
 1010    *p->pzErr = 0;
 1011    if( zFormat ){
 1012      va_list ap;
 1013      va_start(ap, zFormat);
 1014      *p->pzErr = sqlite3_vmprintf(zFormat, ap);
 1015      va_end(ap);
 1016    }
 1017  }
 1018}
 1019
 1020/*
 1021** Out-of-memory error.
 1022*/
 1023static void qrfOom(Qrf *p){
 1024  qrfError(p, SQLITE_NOMEM, "out of memory");
 1025}
 1026
 1027/*
 1028** Transfer any error in pStr over into p.
 1029*/
 1030static void qrfStrErr(Qrf *p, sqlite3_str *pStr){
 1031  int rc = pStr ? sqlite3_str_errcode(pStr) : 0;
 1032  if( rc ){
 1033    qrfError(p, rc, sqlite3_errstr(rc));
 1034  }
 1035}
 1036
 1037
 1038/*
 1039** Add a new entry to the EXPLAIN QUERY PLAN data
 1040*/
 1041static void qrfEqpAppend(Qrf *p, int iEqpId, int p2, const char *zText){
 1042  qrfEQPGraphRow *pNew;
 1043  sqlite3_int64 nText;
 1044  if( zText==0 ) return;
 1045  if( p->u.pGraph==0 ){
 1046    p->u.pGraph = sqlite3_malloc64( sizeof(qrfEQPGraph) );
 1047    if( p->u.pGraph==0 ){
 1048      qrfOom(p);
 1049      return;
 1050    }
 1051    memset(p->u.pGraph, 0, sizeof(qrfEQPGraph) );
 1052  }
 1053  nText = strlen(zText);
 1054  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
 1055  if( pNew==0 ){
 1056    qrfOom(p);
 1057    return;
 1058  }
 1059  pNew->iEqpId = iEqpId;
 1060  pNew->iParentId = p2;
 1061  memcpy(pNew->zText, zText, nText+1);
 1062  pNew->pNext = 0;
 1063  if( p->u.pGraph->pLast ){
 1064    p->u.pGraph->pLast->pNext = pNew;
 1065  }else{
 1066    p->u.pGraph->pRow = pNew;
 1067  }
 1068  p->u.pGraph->pLast = pNew;
 1069}
 1070
 1071/*
 1072** Free and reset the EXPLAIN QUERY PLAN data that has been collected
 1073** in p->u.pGraph.
 1074*/
 1075static void qrfEqpReset(Qrf *p){
 1076  qrfEQPGraphRow *pRow, *pNext;
 1077  if( p->u.pGraph ){
 1078    for(pRow = p->u.pGraph->pRow; pRow; pRow = pNext){
 1079      pNext = pRow->pNext;
 1080      sqlite3_free(pRow);
 1081    }
 1082    sqlite3_free(p->u.pGraph);
 1083    p->u.pGraph = 0;
 1084  }
 1085}
 1086
 1087/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
 1088** pOld, or return the first such line if pOld is NULL
 1089*/
 1090static qrfEQPGraphRow *qrfEqpNextRow(Qrf *p, int iEqpId, qrfEQPGraphRow *pOld){
 1091  qrfEQPGraphRow *pRow = pOld ? pOld->pNext : p->u.pGraph->pRow;
 1092  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
 1093  return pRow;
 1094}
 1095
 1096/* Render a single level of the graph that has iEqpId as its parent.  Called
 1097** recursively to render sublevels.
 1098*/
 1099static void qrfEqpRenderLevel(Qrf *p, int iEqpId){
 1100  qrfEQPGraphRow *pRow, *pNext;
 1101  i64 n = strlen(p->u.pGraph->zPrefix);
 1102  char *z;
 1103  for(pRow = qrfEqpNextRow(p, iEqpId, 0); pRow; pRow = pNext){
 1104    pNext = qrfEqpNextRow(p, iEqpId, pRow);
 1105    z = pRow->zText;
 1106    sqlite3_str_appendf(p->pOut, "%s%s%s\n", p->u.pGraph->zPrefix,
 1107                            pNext ? "|--" : "`--", z);
 1108    if( n<(i64)sizeof(p->u.pGraph->zPrefix)-7 ){
 1109      memcpy(&p->u.pGraph->zPrefix[n], pNext ? "|  " : "   ", 4);
 1110      qrfEqpRenderLevel(p, pRow->iEqpId);
 1111      p->u.pGraph->zPrefix[n] = 0;
 1112    }
 1113  }
 1114}
 1115
 1116/*
 1117** Render the 64-bit value N in a more human-readable format into
 1118** pOut.
 1119**
 1120**   +  Only show the first three significant digits.
 1121**   +  Append suffixes K, M, G, T, P, and E for 1e3, 1e6, ... 1e18
 1122*/
 1123static void qrfApproxInt64(sqlite3_str *pOut, i64 N){
 1124  static const char aSuffix[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
 1125  int i;
 1126  if( N<0 ){
 1127    N = N==INT64_MIN ? INT64_MAX : -N;
 1128    sqlite3_str_append(pOut, "-", 1);
 1129  }
 1130  if( N<10000 ){
 1131    sqlite3_str_appendf(pOut, "%4lld ", N);
 1132    return;
 1133  }
 1134  for(i=1; i<=18; i++){
 1135    N = (N+5)/10;
 1136    if( N<10000 ){
 1137      int n = (int)N;
 1138      switch( i%3 ){
 1139        case 0:
 1140          sqlite3_str_appendf(pOut, "%d.%02d", n/1000, (n%1000)/10);
 1141          break;
 1142        case 1:
 1143          sqlite3_str_appendf(pOut, "%2d.%d", n/100, (n%100)/10);
 1144          break;
 1145        case 2:
 1146          sqlite3_str_appendf(pOut, "%4d", n/10);
 1147          break;
 1148      }
 1149      sqlite3_str_append(pOut, &aSuffix[i/3], 1);
 1150      break;
 1151    }
 1152  }
 1153}
 1154
 1155/*
 1156** Display and reset the EXPLAIN QUERY PLAN data
 1157*/
 1158static void qrfEqpRender(Qrf *p, i64 nCycle){
 1159  qrfEQPGraphRow *pRow;
 1160  if( p->u.pGraph!=0 && (pRow = p->u.pGraph->pRow)!=0 ){
 1161    if( pRow->zText[0]=='-' ){
 1162      if( pRow->pNext==0 ){
 1163        qrfEqpReset(p);
 1164        return;
 1165      }
 1166      sqlite3_str_appendf(p->pOut, "%s\n", pRow->zText+3);
 1167      p->u.pGraph->pRow = pRow->pNext;
 1168      sqlite3_free(pRow);
 1169    }else if( nCycle>0 ){
 1170      int nSp = p->u.pGraph->nWidth - 2;
 1171      if( p->spec.eStyle==QRF_STYLE_StatsEst ){
 1172        sqlite3_str_appendchar(p->pOut, nSp, ' ');
 1173        sqlite3_str_appendall(p->pOut,
 1174                                "Cycles      Loops  (est)  Rows   (est)\n");
 1175        sqlite3_str_appendchar(p->pOut, nSp, ' ');
 1176        sqlite3_str_appendall(p->pOut,
 1177                                "----------  ------------  ------------\n");
 1178      }else{
 1179        sqlite3_str_appendchar(p->pOut, nSp, ' ');
 1180        sqlite3_str_appendall(p->pOut,
 1181                                "Cycles      Loops  Rows \n");
 1182        sqlite3_str_appendchar(p->pOut, nSp, ' ');
 1183        sqlite3_str_appendall(p->pOut,
 1184                                "----------  -----  -----\n");
 1185      }
 1186      sqlite3_str_appendall(p->pOut, "QUERY PLAN");
 1187      sqlite3_str_appendchar(p->pOut, nSp - 10, ' ');
 1188      qrfApproxInt64(p->pOut, nCycle);
 1189      sqlite3_str_appendall(p->pOut, " 100%\n");
 1190    }else{
 1191      sqlite3_str_appendall(p->pOut, "QUERY PLAN\n");
 1192    }
 1193    p->u.pGraph->zPrefix[0] = 0;
 1194    qrfEqpRenderLevel(p, 0);
 1195    qrfEqpReset(p);
 1196  }
 1197}
 1198
 1199#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 1200/*
 1201** Helper function for qrfExpStats().
 1202**
 1203*/
 1204static int qrfStatsHeight(sqlite3_stmt *p, int iEntry){
 1205  int iPid = 0;
 1206  int ret = 1;
 1207  sqlite3_stmt_scanstatus_v2(p, iEntry,
 1208      SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid
 1209  );
 1210  while( iPid!=0 ){
 1211    int ii;
 1212    for(ii=0; 1; ii++){
 1213      int iId;
 1214      int res;
 1215      res = sqlite3_stmt_scanstatus_v2(p, ii,
 1216          SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iId
 1217      );
 1218      if( res ) break;
 1219      if( iId==iPid ){
 1220        sqlite3_stmt_scanstatus_v2(p, ii,
 1221            SQLITE_SCANSTAT_PARENTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid
 1222        );
 1223      }
 1224    }
 1225    ret++;
 1226  }
 1227  return ret;
 1228}
 1229#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
 1230
 1231
 1232/*
 1233** Generate ".scanstatus est" style of EQP output.
 1234*/
 1235static void qrfEqpStats(Qrf *p){
 1236#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
 1237  qrfError(p, SQLITE_ERROR, "not available in this build");
 1238#else
 1239  static const int f = SQLITE_SCANSTAT_COMPLEX;
 1240  sqlite3_stmt *pS = p->pStmt;
 1241  int i = 0;
 1242  i64 nTotal = 0;
 1243  int nWidth = 0;
 1244  int prevPid = -1;             /* Previous iPid */
 1245  double rEstCum = 1.0;         /* Cumulative row estimate */
 1246  sqlite3_str *pLine = sqlite3_str_new(p->db);
 1247  sqlite3_str *pStats = sqlite3_str_new(p->db);
 1248  qrfEqpReset(p);
 1249
 1250  for(i=0; 1; i++){
 1251    const char *z = 0;
 1252    int n = 0;
 1253    if( sqlite3_stmt_scanstatus_v2(pS,i,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&z) ){
 1254      break;
 1255    }
 1256    n = (int)strlen(z) + qrfStatsHeight(pS,i)*3;
 1257    if( n>nWidth ) nWidth = n;
 1258  }
 1259  nWidth += 2;
 1260
 1261  sqlite3_stmt_scanstatus_v2(pS,-1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal);
 1262  for(i=0; 1; i++){
 1263    i64 nLoop = 0;
 1264    i64 nRow = 0;
 1265    i64 nCycle = 0;
 1266    int iId = 0;
 1267    int iPid = 0;
 1268    const char *zo = 0;
 1269    const char *zName = 0;
 1270    double rEst = 0.0;
 1271
 1272    if( sqlite3_stmt_scanstatus_v2(pS,i,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&zo) ){
 1273      break;
 1274    }
 1275    sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid);
 1276    if( iPid!=prevPid ){
 1277      prevPid = iPid;
 1278      rEstCum = 1.0;
 1279    }
 1280    sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_EST,f,(void*)&rEst);
 1281    rEstCum *= rEst;
 1282    sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NLOOP,f,(void*)&nLoop);
 1283    sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NVISIT,f,(void*)&nRow);
 1284    sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NCYCLE,f,(void*)&nCycle);
 1285    sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_SELECTID,f,(void*)&iId);
 1286    sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NAME,f,(void*)&zName);
 1287
 1288    if( nCycle>=0 || nLoop>=0 || nRow>=0 ){
 1289      int nSp = 0;
 1290      sqlite3_str_reset(pStats);
 1291      if( nCycle>=0 && nTotal>0 ){
 1292        qrfApproxInt64(pStats, nCycle);
 1293        sqlite3_str_appendf(pStats, " %3d%%",
 1294            ((nCycle*100)+nTotal/2) / nTotal
 1295        );
 1296        nSp = 2;
 1297      }
 1298      if( nLoop>=0 ){
 1299        if( nSp ) sqlite3_str_appendchar(pStats, nSp, ' ');
 1300        qrfApproxInt64(pStats, nLoop);
 1301        nSp = 2;
 1302        if( p->spec.eStyle==QRF_STYLE_StatsEst ){
 1303          sqlite3_str_appendf(pStats, "  ");
 1304          qrfApproxInt64(pStats, (i64)(rEstCum/rEst));
 1305        }
 1306      }
 1307      if( nRow>=0 ){
 1308        if( nSp ) sqlite3_str_appendchar(pStats, nSp, ' ');
 1309        qrfApproxInt64(pStats, nRow);
 1310        nSp = 2;
 1311        if( p->spec.eStyle==QRF_STYLE_StatsEst ){
 1312          sqlite3_str_appendf(pStats, "  ");
 1313          qrfApproxInt64(pStats, (i64)rEstCum);
 1314        }
 1315      }
 1316      sqlite3_str_appendf(pLine,
 1317          "% *s %s", -1*(nWidth-qrfStatsHeight(pS,i)*3), zo,
 1318          sqlite3_str_value(pStats)
 1319      );
 1320      sqlite3_str_reset(pStats);
 1321      qrfEqpAppend(p, iId, iPid, sqlite3_str_value(pLine));
 1322      sqlite3_str_reset(pLine);
 1323    }else{
 1324      qrfEqpAppend(p, iId, iPid, zo);
 1325    }
 1326  }
 1327  if( p->u.pGraph ) p->u.pGraph->nWidth = nWidth;
 1328  qrfStrErr(p, pLine);
 1329  sqlite3_free(sqlite3_str_finish(pLine));
 1330  qrfStrErr(p, pStats);
 1331  sqlite3_free(sqlite3_str_finish(pStats));
 1332#endif
 1333}
 1334
 1335
 1336/*
 1337** Reset the prepared statement.
 1338*/
 1339static void qrfResetStmt(Qrf *p){
 1340  int rc = sqlite3_reset(p->pStmt);
 1341  if( rc!=SQLITE_OK && p->iErr==SQLITE_OK ){
 1342    qrfError(p, rc, "%s", sqlite3_errmsg(p->db));
 1343  }
 1344}
 1345
 1346/*
 1347** If xWrite is defined, send all content of pOut to xWrite and
 1348** reset pOut.
 1349*/
 1350static void qrfWrite(Qrf *p){
 1351  int n;
 1352  if( p->spec.xWrite && (n = sqlite3_str_length(p->pOut))>0 ){
 1353    int rc = p->spec.xWrite(p->spec.pWriteArg,
 1354                 sqlite3_str_value(p->pOut),
 1355                 (sqlite3_int64)n);
 1356    sqlite3_str_reset(p->pOut);
 1357    if( rc ){
 1358      qrfError(p, rc, "Failed to write %d bytes of output", n);
 1359    }
 1360  }
 1361}
 1362
 1363/* Lookup table to estimate the number of columns consumed by a Unicode
 1364** character.
 1365*/
 1366static const struct {
 1367  unsigned char w;    /* Width of the character in columns */
 1368  int iFirst;         /* First character in a span having this width */
 1369} aQrfUWidth[] = {
 1370   /* {1, 0x00000}, */
 1371  {0, 0x00300},  {1, 0x00370},  {0, 0x00483},  {1, 0x00487},  {0, 0x00488},
 1372  {1, 0x0048a},  {0, 0x00591},  {1, 0x005be},  {0, 0x005bf},  {1, 0x005c0},
 1373  {0, 0x005c1},  {1, 0x005c3},  {0, 0x005c4},  {1, 0x005c6},  {0, 0x005c7},
 1374  {1, 0x005c8},  {0, 0x00600},  {1, 0x00604},  {0, 0x00610},  {1, 0x00616},
 1375  {0, 0x0064b},  {1, 0x0065f},  {0, 0x00670},  {1, 0x00671},  {0, 0x006d6},
 1376  {1, 0x006e5},  {0, 0x006e7},  {1, 0x006e9},  {0, 0x006ea},  {1, 0x006ee},
 1377  {0, 0x0070f},  {1, 0x00710},  {0, 0x00711},  {1, 0x00712},  {0, 0x00730},
 1378  {1, 0x0074b},  {0, 0x007a6},  {1, 0x007b1},  {0, 0x007eb},  {1, 0x007f4},
 1379  {0, 0x00901},  {1, 0x00903},  {0, 0x0093c},  {1, 0x0093d},  {0, 0x00941},
 1380  {1, 0x00949},  {0, 0x0094d},  {1, 0x0094e},  {0, 0x00951},  {1, 0x00955},
 1381  {0, 0x00962},  {1, 0x00964},  {0, 0x00981},  {1, 0x00982},  {0, 0x009bc},
 1382  {1, 0x009bd},  {0, 0x009c1},  {1, 0x009c5},  {0, 0x009cd},  {1, 0x009ce},
 1383  {0, 0x009e2},  {1, 0x009e4},  {0, 0x00a01},  {1, 0x00a03},  {0, 0x00a3c},
 1384  {1, 0x00a3d},  {0, 0x00a41},  {1, 0x00a43},  {0, 0x00a47},  {1, 0x00a49},
 1385  {0, 0x00a4b},  {1, 0x00a4e},  {0, 0x00a70},  {1, 0x00a72},  {0, 0x00a81},
 1386  {1, 0x00a83},  {0, 0x00abc},  {1, 0x00abd},  {0, 0x00ac1},  {1, 0x00ac6},
 1387  {0, 0x00ac7},  {1, 0x00ac9},  {0, 0x00acd},  {1, 0x00ace},  {0, 0x00ae2},
 1388  {1, 0x00ae4},  {0, 0x00b01},  {1, 0x00b02},  {0, 0x00b3c},  {1, 0x00b3d},
 1389  {0, 0x00b3f},  {1, 0x00b40},  {0, 0x00b41},  {1, 0x00b44},  {0, 0x00b4d},
 1390  {1, 0x00b4e},  {0, 0x00b56},  {1, 0x00b57},  {0, 0x00b82},  {1, 0x00b83},
 1391  {0, 0x00bc0},  {1, 0x00bc1},  {0, 0x00bcd},  {1, 0x00bce},  {0, 0x00c3e},
 1392  {1, 0x00c41},  {0, 0x00c46},  {1, 0x00c49},  {0, 0x00c4a},  {1, 0x00c4e},
 1393  {0, 0x00c55},  {1, 0x00c57},  {0, 0x00cbc},  {1, 0x00cbd},  {0, 0x00cbf},
 1394  {1, 0x00cc0},  {0, 0x00cc6},  {1, 0x00cc7},  {0, 0x00ccc},  {1, 0x00cce},
 1395  {0, 0x00ce2},  {1, 0x00ce4},  {0, 0x00d41},  {1, 0x00d44},  {0, 0x00d4d},
 1396  {1, 0x00d4e},  {0, 0x00dca},  {1, 0x00dcb},  {0, 0x00dd2},  {1, 0x00dd5},
 1397  {0, 0x00dd6},  {1, 0x00dd7},  {0, 0x00e31},  {1, 0x00e32},  {0, 0x00e34},
 1398  {1, 0x00e3b},  {0, 0x00e47},  {1, 0x00e4f},  {0, 0x00eb1},  {1, 0x00eb2},
 1399  {0, 0x00eb4},  {1, 0x00eba},  {0, 0x00ebb},  {1, 0x00ebd},  {0, 0x00ec8},
 1400  {1, 0x00ece},  {0, 0x00f18},  {1, 0x00f1a},  {0, 0x00f35},  {1, 0x00f36},
 1401  {0, 0x00f37},  {1, 0x00f38},  {0, 0x00f39},  {1, 0x00f3a},  {0, 0x00f71},
 1402  {1, 0x00f7f},  {0, 0x00f80},  {1, 0x00f85},  {0, 0x00f86},  {1, 0x00f88},
 1403  {0, 0x00f90},  {1, 0x00f98},  {0, 0x00f99},  {1, 0x00fbd},  {0, 0x00fc6},
 1404  {1, 0x00fc7},  {0, 0x0102d},  {1, 0x01031},  {0, 0x01032},  {1, 0x01033},
 1405  {0, 0x01036},  {1, 0x0103b},  {0, 0x01058},
 1406  {1, 0x0105a},  {2, 0x01100},  {0, 0x01160},  {1, 0x01200},  {0, 0x0135f},
 1407  {1, 0x01360},  {0, 0x01712},  {1, 0x01715},  {0, 0x01732},  {1, 0x01735},
 1408  {0, 0x01752},  {1, 0x01754},  {0, 0x01772},  {1, 0x01774},  {0, 0x017b4},
 1409  {1, 0x017b6},  {0, 0x017b7},  {1, 0x017be},  {0, 0x017c6},  {1, 0x017c7},
 1410  {0, 0x017c9},  {1, 0x017d4},  {0, 0x017dd},  {1, 0x017de},  {0, 0x0180b},
 1411  {1, 0x0180e},  {0, 0x018a9},  {1, 0x018aa},  {0, 0x01920},  {1, 0x01923},
 1412  {0, 0x01927},  {1, 0x01929},  {0, 0x01932},  {1, 0x01933},  {0, 0x01939},
 1413  {1, 0x0193c},  {0, 0x01a17},  {1, 0x01a19},  {0, 0x01b00},  {1, 0x01b04},
 1414  {0, 0x01b34},  {1, 0x01b35},  {0, 0x01b36},  {1, 0x01b3b},  {0, 0x01b3c},
 1415  {1, 0x01b3d},  {0, 0x01b42},  {1, 0x01b43},  {0, 0x01b6b},  {1, 0x01b74},
 1416  {0, 0x01dc0},  {1, 0x01dcb},  {0, 0x01dfe},  {1, 0x01e00},  {0, 0x0200b},
 1417  {1, 0x02010},  {0, 0x0202a},  {1, 0x0202f},  {0, 0x02060},  {1, 0x02064},
 1418  {0, 0x0206a},  {1, 0x02070},  {0, 0x020d0},  {1, 0x020f0},  {2, 0x02329},
 1419  {1, 0x0232b},  {2, 0x02e80},  {0, 0x0302a},  {2, 0x03030},  {1, 0x0303f},
 1420  {2, 0x03040},  {0, 0x03099},  {2, 0x0309b},  {1, 0x0a4d0},  {0, 0x0a806},
 1421  {1, 0x0a807},  {0, 0x0a80b},  {1, 0x0a80c},  {0, 0x0a825},  {1, 0x0a827},
 1422  {2, 0x0ac00},  {1, 0x0d7a4},  {2, 0x0f900},  {1, 0x0fb00},  {0, 0x0fb1e},
 1423  {1, 0x0fb1f},  {0, 0x0fe00},  {2, 0x0fe10},  {1, 0x0fe1a},  {0, 0x0fe20},
 1424  {1, 0x0fe24},  {2, 0x0fe30},  {1, 0x0fe70},  {0, 0x0feff},  {2, 0x0ff00},
 1425  {1, 0x0ff61},  {2, 0x0ffe0},  {1, 0x0ffe7},  {0, 0x0fff9},  {1, 0x0fffc},
 1426  {0, 0x10a01},  {1, 0x10a04},  {0, 0x10a05},  {1, 0x10a07},  {0, 0x10a0c},
 1427  {1, 0x10a10},  {0, 0x10a38},  {1, 0x10a3b},  {0, 0x10a3f},  {1, 0x10a40},
 1428  {0, 0x1d167},  {1, 0x1d16a},  {0, 0x1d173},  {1, 0x1d183},  {0, 0x1d185},
 1429  {1, 0x1d18c},  {0, 0x1d1aa},  {1, 0x1d1ae},  {0, 0x1d242},  {1, 0x1d245},
 1430  {2, 0x20000},  {1, 0x2fffe},  {2, 0x30000},  {1, 0x3fffe},  {0, 0xe0001},
 1431  {1, 0xe0002},  {0, 0xe0020},  {1, 0xe0080},  {0, 0xe0100},  {1, 0xe01f0}
 1432};
 1433
 1434/*
 1435** Return an estimate of the width, in columns, for the single Unicode
 1436** character c.  For normal characters, the answer is always 1.  But the
 1437** estimate might be 0 or 2 for zero-width and double-width characters.
 1438**
 1439** Different display devices display unicode using different widths.  So
 1440** it is impossible to know that true display width with 100% accuracy.
 1441** Inaccuracies in the width estimates might cause columns to be misaligned.
 1442** Unfortunately, there is nothing we can do about that.
 1443*/
 1444int sqlite3_qrf_wcwidth(int c){
 1445  int iFirst, iLast;
 1446
 1447  /* Fast path for common characters */
 1448  if( c<0x300 ) return 1;
 1449
 1450  /* The general case */
 1451  iFirst = 0;
 1452  iLast = sizeof(aQrfUWidth)/sizeof(aQrfUWidth[0]) - 1;
 1453  while( iFirst<iLast-1 ){
 1454    int iMid = (iFirst+iLast)/2;
 1455    int cMid = aQrfUWidth[iMid].iFirst;
 1456    if( cMid < c ){
 1457      iFirst = iMid;
 1458    }else if( cMid > c ){
 1459      iLast = iMid - 1;
 1460    }else{
 1461      return aQrfUWidth[iMid].w;
 1462    }
 1463  }
 1464  if( aQrfUWidth[iLast].iFirst > c ) return aQrfUWidth[iFirst].w;
 1465  return aQrfUWidth[iLast].w;
 1466}
 1467
 1468/*
 1469** Compute the value and length of a multi-byte UTF-8 character that
 1470** begins at z[0]. Return the length.  Write the Unicode value into *pU.
 1471**
 1472** This routine only works for *multi-byte* UTF-8 characters.  It does
 1473** not attempt to detect illegal characters.
 1474*/
 1475int sqlite3_qrf_decode_utf8(const unsigned char *z, int *pU){
 1476  if( (z[0] & 0xe0)==0xc0 && (z[1] & 0xc0)==0x80 ){
 1477    *pU = ((z[0] & 0x1f)<<6) | (z[1] & 0x3f);
 1478    return 2;
 1479  }
 1480  if( (z[0] & 0xf0)==0xe0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 ){
 1481    *pU = ((z[0] & 0x0f)<<12) | ((z[1] & 0x3f)<<6) | (z[2] & 0x3f);
 1482    return 3;
 1483  }
 1484  if( (z[0] & 0xf8)==0xf0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80
 1485   && (z[3] & 0xc0)==0x80
 1486  ){
 1487    *pU = ((z[0] & 0x0f)<<18) | ((z[1] & 0x3f)<<12) | ((z[2] & 0x3f))<<6
 1488                              | (z[3] & 0x3f);
 1489    return 4;
 1490  }
 1491  *pU = 0;
 1492  return 1;
 1493}
 1494
 1495/*
 1496** Check to see if z[] is a valid VT100 escape.  If it is, then
 1497** return the number of bytes in the escape sequence.  Return 0 if
 1498** z[] is not a VT100 escape.
 1499**
 1500** This routine assumes that z[0] is \033 (ESC).
 1501*/
 1502static int qrfIsVt100(const unsigned char *z){
 1503  int i;
 1504  if( z[1]!='[' ) return 0;
 1505  i = 2;
 1506  while( z[i]>=0x30 && z[i]<=0x3f ){ i++; }
 1507  while( z[i]>=0x20 && z[i]<=0x2f ){ i++; }
 1508  if( z[i]<0x40 || z[i]>0x7e ) return 0;
 1509  return i+1;
 1510}
 1511
 1512/*
 1513** Return the length of a string in display characters.
 1514**
 1515** Most characters of the input string count as 1, including
 1516** multi-byte UTF8 characters.  However, zero-width unicode
 1517** characters and VT100 escape sequences count as zero, and
 1518** double-width characters count as two.
 1519**
 1520** The definition of "zero-width" and "double-width" characters
 1521** is not precise.  It depends on the output device, to some extent,
 1522** and it varies according to the Unicode version.  This routine
 1523** makes the best guess that it can.
 1524*/
 1525size_t sqlite3_qrf_wcswidth(const char *zIn){
 1526  const unsigned char *z = (const unsigned char*)zIn;
 1527  size_t n = 0;
 1528  while( *z ){
 1529    if( z[0]<' ' ){
 1530      int k;
 1531      if( z[0]=='\033' && (k = qrfIsVt100(z))>0 ){
 1532        z += k;
 1533      }else{
 1534        z++;
 1535      }
 1536    }else if( (0x80&z[0])==0 ){
 1537      n++;
 1538      z++;
 1539    }else{
 1540      int u = 0;
 1541      int len = sqlite3_qrf_decode_utf8(z, &u);
 1542      z += len;
 1543      n += sqlite3_qrf_wcwidth(u);
 1544    }
 1545  }
 1546  return n;
 1547}
 1548
 1549/*
 1550** Return the display width of the longest line of text
 1551** in the (possibly) multi-line input string zIn[0..nByte].
 1552** zIn[] is not necessarily zero-terminated.  Take
 1553** into account tab characters, zero- and double-width
 1554** characters, CR and NL, and VT100 escape codes.
 1555**
 1556** Write the number of newlines into *pnNL.  So, *pnNL will
 1557** return 0 if everything fits on one line, or positive it
 1558** it will need to be split.
 1559*/
 1560static int qrfDisplayWidth(const char *zIn, sqlite3_int64 nByte, int *pnNL){
 1561  const unsigned char *z;
 1562  const unsigned char *zEnd;
 1563  int mx = 0;
 1564  int n = 0;
 1565  int nNL = 0;
 1566  if( zIn==0 ) zIn = "";
 1567  z = (const unsigned char*)zIn;
 1568  zEnd = &z[nByte];
 1569  while( z<zEnd ){
 1570    if( z[0]<' ' ){
 1571      int k;
 1572      if( z[0]=='\033' && (k = qrfIsVt100(z))>0 ){
 1573        z += k;
 1574      }else{
 1575        if( z[0]=='\t' ){
 1576          n = (n+8)&~7;
 1577        }else if( z[0]=='\n' || z[0]=='\r' ){
 1578          nNL++;
 1579          if( n>mx ) mx = n;
 1580          n = 0;
 1581        }
 1582        z++;
 1583      }
 1584    }else if( (0x80&z[0])==0 ){
 1585      n++;
 1586      z++;
 1587    }else{
 1588      int u = 0;
 1589      int len = sqlite3_qrf_decode_utf8(z, &u);
 1590      z += len;
 1591      n += sqlite3_qrf_wcwidth(u);
 1592    }
 1593  }
 1594  if( mx>n ) n = mx;
 1595  if( pnNL ) *pnNL = nNL;
 1596  return n;
 1597}
 1598
 1599/*
 1600** Escape the input string if it is needed and in accordance with
 1601** eEsc, which is either QRF_ESC_Ascii or QRF_ESC_Symbol.
 1602**
 1603** Escaping is needed if the string contains any control characters
 1604** other than \t, \n, and \r\n
 1605**
 1606** If no escaping is needed (the common case) then set *ppOut to NULL
 1607** and return 0.  If escaping is needed, write the escaped string into
 1608** memory obtained from sqlite3_malloc64() and make *ppOut point to that
 1609** memory and return 0.  If an error occurs, return non-zero.
 1610**
 1611** The caller is responsible for freeing *ppFree if it is non-NULL in order
 1612** to reclaim memory.
 1613*/
 1614static void qrfEscape(
 1615  int eEsc,            /* QRF_ESC_Ascii or QRF_ESC_Symbol */
 1616  sqlite3_str *pStr,      /* String to be escaped */
 1617  int iStart              /* Begin escapding on this byte of pStr */
 1618){
 1619  sqlite3_int64 i, j;     /* Loop counters */
 1620  sqlite3_int64 sz;       /* Size of the string prior to escaping */
 1621  sqlite3_int64 nCtrl = 0;/* Number of control characters to escape */
 1622  unsigned char *zIn;     /* Text to be escaped */
 1623  unsigned char c;        /* A single character of the text */
 1624  unsigned char *zOut;    /* Where to write the results */
 1625
 1626  /* Find the text to be escaped */
 1627  zIn = (unsigned char*)sqlite3_str_value(pStr);
 1628  if( zIn==0 ) return;
 1629  zIn += iStart;
 1630
 1631  /* Count the control characters */
 1632  for(i=0; (c = zIn[i])!=0; i++){
 1633    if( c<=0x1f
 1634     && c!='\t'
 1635     && c!='\n'
 1636     && (c!='\r' || zIn[i+1]!='\n')
 1637    ){
 1638      nCtrl++;
 1639    }
 1640  }
 1641  if( nCtrl==0 ) return;  /* Early out if no control characters */
 1642
 1643  /* Make space to hold the escapes.  Copy the original text to the end
 1644  ** of the available space. */
 1645  sz = sqlite3_str_length(pStr) - iStart;
 1646  if( eEsc==QRF_ESC_Symbol ) nCtrl *= 2;
 1647  sqlite3_str_appendchar(pStr, nCtrl, ' ');
 1648  zOut = (unsigned char*)sqlite3_str_value(pStr);
 1649  if( zOut==0 ) return;
 1650  zOut += iStart;
 1651  zIn = zOut + nCtrl;
 1652  memmove(zIn,zOut,sz);
 1653
 1654  /* Convert the control characters */
 1655  for(i=j=0; (c = zIn[i])!=0; i++){
 1656    if( c>0x1f
 1657     || c=='\t'
 1658     || c=='\n'
 1659     || (c=='\r' && zIn[i+1]=='\n')
 1660    ){
 1661      continue;
 1662    }
 1663    if( i>0 ){
 1664      memmove(&zOut[j], zIn, i);
 1665      j += i;
 1666    }
 1667    zIn += i+1;
 1668    i = -1;
 1669    if( eEsc==QRF_ESC_Symbol ){
 1670      zOut[j++] = 0xe2;
 1671      zOut[j++] = 0x90;
 1672      zOut[j++] = 0x80+c;
 1673    }else{
 1674      zOut[j++] = '^';
 1675      zOut[j++] = 0x40+c;
 1676    }
 1677  }
 1678}
 1679
 1680/*
 1681** Determine if the string z[] can be shown as plain text.  Return true
 1682** if z[] is unambiguously text.  Return false if z[] needs to be
 1683** quoted.
 1684**
 1685** All of the following must be true in order for z[] to be relaxable:
 1686**
 1687**    (1) z[] does not begin or end with ' or whitespace
 1688**    (2) z[] is not the same as the NULL rendering
 1689**    (3) z[] does not looks like a numeric literal
 1690*/
 1691static int qrfRelaxable(Qrf *p, const char *z){
 1692  size_t i, n;
 1693  if( z[0]=='\'' || qrfSpace(z[0]) ) return 0;
 1694  if( z[0]==0 ){
 1695    return (p->spec.zNull!=0 && p->spec.zNull[0]!=0);
 1696  }
 1697  n = strlen(z);
 1698  if( n==0 || z[n-1]=='\'' || qrfSpace(z[n-1]) ) return 0;
 1699  if( p->spec.zNull && strcmp(p->spec.zNull,z)==0 ) return 0;
 1700  i = (z[0]=='-' || z[0]=='+');
 1701  if( strcmp(z+i,"Inf")==0 ) return 0;
 1702  if( !qrfDigit(z[i]) ) return 1;
 1703  i++;
 1704  while( qrfDigit(z[i]) ){ i++; }
 1705  if( z[i]==0 ) return 0;
 1706  if( z[i]=='.' ){
 1707    i++;
 1708    while( qrfDigit(z[i]) ){ i++; }
 1709    if( z[i]==0 ) return 0;
 1710  }
 1711  if( z[i]=='e' || z[i]=='E' ){
 1712    i++;
 1713    if( z[i]=='+' || z[i]=='-' ){ i++; }
 1714    if( !qrfDigit(z[i]) ) return 1;
 1715    i++;
 1716    while( qrfDigit(z[i]) ){ i++; }
 1717  }
 1718  return z[i]!=0;
 1719}
 1720
 1721/*
 1722** If a field contains any character identified by a 1 in the following
 1723** array, then the string must be quoted for CSV.
 1724*/
 1725static const char qrfCsvQuote[] = {
 1726  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
 1727  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
 1728  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
 1729  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
 1730  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
 1731  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
 1732  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
 1733  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
 1734  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
 1735  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
 1736  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
 1737  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
 1738  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
 1739  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
 1740  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
 1741  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
 1742};
 1743
 1744/*
 1745** Encode text appropriately and append it to pOut.
 1746*/
 1747static void qrfEncodeText(Qrf *p, sqlite3_str *pOut, const char *zTxt){
 1748  int iStart = sqlite3_str_length(pOut);
 1749  switch( p->spec.eText ){
 1750    case QRF_TEXT_Relaxed:
 1751      if( qrfRelaxable(p, zTxt) ){
 1752        sqlite3_str_appendall(pOut, zTxt);
 1753        break;
 1754      }
 1755      deliberate_fall_through; /* FALLTHRU */
 1756    case QRF_TEXT_Sql: {
 1757      if( p->spec.eEsc==QRF_ESC_Off ){
 1758        sqlite3_str_appendf(pOut, "%Q", zTxt);
 1759      }else{
 1760        sqlite3_str_appendf(pOut, "%#Q", zTxt);
 1761      }
 1762      break;
 1763    }
 1764    case QRF_TEXT_Csv: {
 1765      unsigned int i;
 1766      for(i=0; zTxt[i]; i++){
 1767        if( qrfCsvQuote[((const unsigned char*)zTxt)[i]] ){
 1768          i = 0;
 1769          break;
 1770        }
 1771      }
 1772      if( i==0 || strstr(zTxt, p->spec.zColumnSep)!=0 ){
 1773        sqlite3_str_appendf(pOut, "\"%w\"", zTxt);
 1774      }else{
 1775        sqlite3_str_appendall(pOut, zTxt);
 1776      }
 1777      break;
 1778    }
 1779    case QRF_TEXT_Html: {
 1780      const unsigned char *z = (const unsigned char*)zTxt;
 1781      while( *z ){
 1782        unsigned int i = 0;
 1783        unsigned char c;
 1784        while( (c=z[i])>'>'
 1785            || (c && c!='<' && c!='>' && c!='&' && c!='\"' && c!='\'')
 1786        ){
 1787          i++;
 1788        }
 1789        if( i>0 ){
 1790          sqlite3_str_append(pOut, (const char*)z, i);
 1791        }
 1792        switch( z[i] ){
 1793          case '>':   sqlite3_str_append(pOut, "&lt;", 4);   break;
 1794          case '&':   sqlite3_str_append(pOut, "&amp;", 5);  break;
 1795          case '<':   sqlite3_str_append(pOut, "&lt;", 4);   break;
 1796          case '"':   sqlite3_str_append(pOut, "&quot;", 6); break;
 1797          case '\'':  sqlite3_str_append(pOut, "&#39;", 5);  break;
 1798          default:    i--;
 1799        }
 1800        z += i + 1;
 1801      }
 1802      break;
 1803    }
 1804    case QRF_TEXT_Tcl:
 1805    case QRF_TEXT_Json: {
 1806      const unsigned char *z = (const unsigned char*)zTxt;
 1807      sqlite3_str_append(pOut, "\"", 1);
 1808      while( *z ){
 1809        unsigned int i;
 1810        for(i=0; z[i]>=0x20 && z[i]!='\\' && z[i]!='"'; i++){}
 1811        if( i>0 ){
 1812          sqlite3_str_append(pOut, (const char*)z, i);
 1813        }
 1814        if( z[i]==0 ) break;
 1815        switch( z[i] ){
 1816          case '"':   sqlite3_str_append(pOut, "\\\"", 2);  break;
 1817          case '\\':  sqlite3_str_append(pOut, "\\\\", 2);  break;
 1818          case '\b':  sqlite3_str_append(pOut, "\\b", 2);   break;
 1819          case '\f':  sqlite3_str_append(pOut, "\\f", 2);   break;
 1820          case '\n':  sqlite3_str_append(pOut, "\\n", 2);   break;
 1821          case '\r':  sqlite3_str_append(pOut, "\\r", 2);   break;
 1822          case '\t':  sqlite3_str_append(pOut, "\\t", 2);   break;
 1823          default: {
 1824            if( p->spec.eText==QRF_TEXT_Json ){
 1825              sqlite3_str_appendf(pOut, "\\u%04x", z[i]);
 1826            }else{
 1827              sqlite3_str_appendf(pOut, "\\%03o", z[i]);
 1828            }
 1829            break;
 1830          }
 1831        }
 1832        z += i + 1;
 1833      }
 1834      sqlite3_str_append(pOut, "\"", 1);
 1835      break;
 1836    }
 1837    default: {
 1838      sqlite3_str_appendall(pOut, zTxt);
 1839      break;
 1840    }
 1841  }
 1842  if( p->spec.eEsc!=QRF_ESC_Off ){
 1843    qrfEscape(p->spec.eEsc, pOut, iStart);
 1844  }
 1845}
 1846
 1847/*
 1848** Do a quick sanity check to see aBlob[0..nBlob-1] is valid JSONB
 1849** return true if it is and false if it is not.
 1850**
 1851** False positives are possible, but not false negatives.
 1852*/
 1853static int qrfJsonbQuickCheck(unsigned char *aBlob, int nBlob){
 1854  unsigned char x;   /* Payload size half-byte */
 1855  int i;             /* Loop counter */   
 1856  int n;             /* Bytes in the payload size integer */
 1857  sqlite3_uint64 sz; /* value of the payload size integer */
 1858
 1859  if( nBlob==0 ) return 0;
 1860  x = aBlob[0]>>4;
 1861  if( x<=11 ) return nBlob==(1+x);
 1862  n = x<14 ? x-11 : 4*(x-13);
 1863  if( nBlob<1+n ) return 0;
 1864  sz = aBlob[1];
 1865  for(i=1; i<n; i++) sz = (sz<<8) + aBlob[i+1];
 1866  return sz+n+1==(sqlite3_uint64)nBlob;
 1867}
 1868
 1869/*
 1870** The current iCol-th column of p->pStmt is known to be a BLOB.  Check
 1871** to see if that BLOB is really a JSONB blob.  If it is, then translate
 1872** it into a text JSON representation and return a pointer to that text JSON.
 1873** If the BLOB is not JSONB, then return a NULL pointer.
 1874**
 1875** The memory used to hold the JSON text is managed internally by the
 1876** "p" object and is overwritten and/or deallocated upon the next call
 1877** to this routine (with the same p argument) or when the p object is
 1878** finailized.
 1879*/
 1880static const char *qrfJsonbToJson(Qrf *p, int iCol){
 1881  int nByte;
 1882  const void *pBlob;
 1883  int rc;
 1884  nByte = sqlite3_column_bytes(p->pStmt, iCol);
 1885  pBlob = sqlite3_column_blob(p->pStmt, iCol);
 1886  if( qrfJsonbQuickCheck((unsigned char*)pBlob, nByte)==0 ){
 1887    return 0;
 1888  }
 1889  if( p->pJTrans==0 ){
 1890    sqlite3 *db;
 1891    rc = sqlite3_open(":memory:",&db);
 1892    if( rc ){
 1893      sqlite3_close(db);
 1894      return 0;
 1895    }
 1896    rc = sqlite3_prepare_v2(db, "SELECT json(?1)", -1, &p->pJTrans, 0);
 1897    if( rc ){
 1898      sqlite3_finalize(p->pJTrans);
 1899      p->pJTrans = 0;
 1900      sqlite3_close(db);
 1901      return 0;
 1902    }
 1903  }else{
 1904    sqlite3_reset(p->pJTrans);
 1905  }
 1906  sqlite3_bind_blob(p->pJTrans, 1, (void*)pBlob, nByte, SQLITE_STATIC);
 1907  rc = sqlite3_step(p->pJTrans);
 1908  if( rc==SQLITE_ROW ){
 1909    return (const char*)sqlite3_column_text(p->pJTrans, 0);
 1910  }else{
 1911    return 0;
 1912  }
 1913}
 1914
 1915/*
 1916** Adjust the input string zIn[] such that it is no more than N display
 1917** characters wide.  If it is wider than that, then truncate and add
 1918** ellipsis.  Or if zIn[] contains a \r or \n, truncate at that point,
 1919** adding ellipsis.  Embedded tabs in zIn[] are converted into ordinary
 1920** spaces.
 1921**
 1922** Return this display width of the modified title string.
 1923*/
 1924static int qrfTitleLimit(char *zIn, int N){
 1925  unsigned char *z = (unsigned char*)zIn;
 1926  int n = 0;
 1927  unsigned char *zEllipsis = 0;
 1928  while( 1 /*exit-by-break*/ ){
 1929    if( z[0]<' ' ){
 1930      int k;
 1931      if( z[0]==0 ){
 1932        zEllipsis = 0;
 1933        break;
 1934      }else if( z[0]=='\033' && (k = qrfIsVt100(z))>0 ){
 1935        z += k;
 1936      }else if( z[0]=='\t' ){
 1937        z[0] = ' ';
 1938      }else if( z[0]=='\n' || z[0]=='\r' ){
 1939        z[0] = ' ';
 1940      }else{
 1941        z++;
 1942      }
 1943    }else if( (0x80&z[0])==0 ){
 1944      if( n>=(N-3) && zEllipsis==0 ) zEllipsis = z;
 1945      if( n==N ){ z[0] = 0; break; }
 1946      n++;
 1947      z++;
 1948    }else{
 1949      int u = 0;
 1950      int len = sqlite3_qrf_decode_utf8(z, &u);
 1951      if( n+len>(N-3) && zEllipsis==0 ) zEllipsis = z;
 1952      if( n+len>N ){ z[0] = 0; break; }
 1953      z += len;
 1954      n += sqlite3_qrf_wcwidth(u);
 1955    }
 1956  }
 1957  if( zEllipsis && N>=3 ) memcpy(zEllipsis,"...",4);
 1958  return n;
 1959}
 1960
 1961
 1962/*
 1963** Render value pVal into pOut
 1964*/
 1965static void qrfRenderValue(Qrf *p, sqlite3_str *pOut, int iCol){
 1966#if SQLITE_VERSION_NUMBER>=3052000
 1967  int iStartLen = sqlite3_str_length(pOut);
 1968#endif
 1969  if( p->spec.xRender ){
 1970    sqlite3_value *pVal;
 1971    char *z;
 1972    pVal = sqlite3_value_dup(sqlite3_column_value(p->pStmt,iCol));
 1973    z = p->spec.xRender(p->spec.pRenderArg, pVal);
 1974    sqlite3_value_free(pVal);
 1975    if( z ){
 1976      sqlite3_str_appendall(pOut, z);
 1977      sqlite3_free(z);
 1978      return;
 1979    }
 1980  }
 1981  switch( sqlite3_column_type(p->pStmt,iCol) ){
 1982    case SQLITE_INTEGER: {
 1983      sqlite3_str_appendf(pOut, "%lld", sqlite3_column_int64(p->pStmt,iCol));
 1984      break;
 1985    }
 1986    case SQLITE_FLOAT: {
 1987      const char *zTxt = (const char*)sqlite3_column_text(p->pStmt,iCol);
 1988      sqlite3_str_appendall(pOut, zTxt);
 1989      break;
 1990    }
 1991    case SQLITE_BLOB: {
 1992      if( p->spec.bTextJsonb==QRF_Yes ){
 1993        const char *zJson = qrfJsonbToJson(p, iCol);
 1994        if( zJson ){
 1995          if( p->spec.eText==QRF_TEXT_Sql ){
 1996            sqlite3_str_append(pOut,"jsonb(",6);
 1997            qrfEncodeText(p, pOut, zJson);
 1998            sqlite3_str_append(pOut,")",1);
 1999          }else{
 2000            qrfEncodeText(p, pOut, zJson);
 2001          }
 2002          break;
 2003        }
 2004      }
 2005      switch( p->spec.eBlob ){
 2006        case QRF_BLOB_Hex:
 2007        case QRF_BLOB_Sql: {
 2008          int iStart;
 2009          int nBlob = sqlite3_column_bytes(p->pStmt,iCol);
 2010          int i, j;
 2011          char *zVal;
 2012          const unsigned char *a = sqlite3_column_blob(p->pStmt,iCol);
 2013          if( p->spec.eBlob==QRF_BLOB_Sql ){
 2014            sqlite3_str_append(pOut, "x'", 2);
 2015          }
 2016          iStart = sqlite3_str_length(pOut);
 2017          sqlite3_str_appendchar(pOut, nBlob, ' ');
 2018          sqlite3_str_appendchar(pOut, nBlob, ' ');
 2019          if( p->spec.eBlob==QRF_BLOB_Sql ){
 2020            sqlite3_str_appendchar(pOut, 1, '\'');
 2021          }
 2022          if( sqlite3_str_errcode(pOut) ) return;
 2023          zVal = sqlite3_str_value(pOut);
 2024          for(i=0, j=iStart; i<nBlob; i++, j+=2){
 2025            unsigned char c = a[i];
 2026            zVal[j] = "0123456789abcdef"[(c>>4)&0xf];
 2027            zVal[j+1] = "0123456789abcdef"[(c)&0xf];
 2028          }
 2029          break;
 2030        }
 2031        case QRF_BLOB_Tcl:
 2032        case QRF_BLOB_Json: {
 2033          int iStart;
 2034          int nBlob = sqlite3_column_bytes(p->pStmt,iCol);
 2035          int i, j;
 2036          char *zVal;
 2037          const unsigned char *a = sqlite3_column_blob(p->pStmt,iCol);
 2038          int szC = p->spec.eBlob==QRF_BLOB_Json ? 6 : 4;
 2039          sqlite3_str_append(pOut, "\"", 1);
 2040          iStart = sqlite3_str_length(pOut);
 2041          for(i=szC; i>0; i--){
 2042            sqlite3_str_appendchar(pOut, nBlob, ' ');
 2043          }
 2044          sqlite3_str_appendchar(pOut, 1, '"');
 2045          if( sqlite3_str_errcode(pOut) ) return;
 2046          zVal = sqlite3_str_value(pOut);
 2047          for(i=0, j=iStart; i<nBlob; i++, j+=szC){
 2048            unsigned char c = a[i];
 2049            zVal[j] = '\\';
 2050            if( szC==4 ){
 2051              zVal[j+1] = '0' + ((c>>6)&3);
 2052              zVal[j+2] = '0' + ((c>>3)&7);
 2053              zVal[j+3] = '0' + (c&7);
 2054            }else{
 2055              zVal[j+1] = 'u';
 2056              zVal[j+2] = '0';
 2057              zVal[j+3] = '0';
 2058              zVal[j+4] = "0123456789abcdef"[(c>>4)&0xf];
 2059              zVal[j+5] = "0123456789abcdef"[(c)&0xf];
 2060            }
 2061          }
 2062          break;
 2063        }
 2064        case QRF_BLOB_Size: {
 2065          int nBlob = sqlite3_column_bytes(p->pStmt,iCol);
 2066          sqlite3_str_appendf(pOut, "(%d-byte blob)", nBlob);
 2067          break;
 2068        }
 2069        default: {
 2070          const char *zTxt = (const char*)sqlite3_column_text(p->pStmt,iCol);
 2071          qrfEncodeText(p, pOut, zTxt);
 2072        }
 2073      }
 2074      break;
 2075    }
 2076    case SQLITE_NULL: {
 2077      sqlite3_str_appendall(pOut, p->spec.zNull);
 2078      break;
 2079    }
 2080    case SQLITE_TEXT: {
 2081      const char *zTxt = (const char*)sqlite3_column_text(p->pStmt,iCol);
 2082      qrfEncodeText(p, pOut, zTxt);
 2083      break;
 2084    }
 2085  }
 2086#if SQLITE_VERSION_NUMBER>=3052000
 2087  if( p->spec.nCharLimit>0
 2088   && (sqlite3_str_length(pOut) - iStartLen) > p->spec.nCharLimit
 2089  ){
 2090    const unsigned char *z;
 2091    int ii = 0, w = 0, limit = p->spec.nCharLimit;
 2092    z = (const unsigned char*)sqlite3_str_value(pOut) + iStartLen;
 2093    if( limit<4 ) limit = 4;
 2094    while( 1 ){
 2095      if( z[ii]<' ' ){
 2096        int k;
 2097        if( z[ii]=='\033' && (k = qrfIsVt100(z+ii))>0 ){
 2098          ii += k;
 2099        }else if( z[ii]==0 ){
 2100          break;
 2101        }else{
 2102          ii++;
 2103        }
 2104      }else if( (0x80&z[ii])==0 ){
 2105        w++;
 2106        if( w>limit ) break;
 2107        ii++;
 2108      }else{
 2109        int u = 0;
 2110        int len = sqlite3_qrf_decode_utf8(&z[ii], &u);
 2111        w += sqlite3_qrf_wcwidth(u);
 2112        if( w>limit ) break;
 2113        ii += len;
 2114      }
 2115    }
 2116    if( w>limit ){
 2117      sqlite3_str_truncate(pOut, iStartLen+ii);
 2118      sqlite3_str_append(pOut, "...", 3);
 2119    }
 2120  }
 2121#endif
 2122}
 2123
 2124/* Trim spaces of the end if pOut
 2125*/
 2126static void qrfRTrim(sqlite3_str *pOut){
 2127#if SQLITE_VERSION_NUMBER>=3052000
 2128  int nByte = sqlite3_str_length(pOut);
 2129  const char *zOut = sqlite3_str_value(pOut);
 2130  while( nByte>0 && zOut[nByte-1]==' ' ){ nByte--; }
 2131  sqlite3_str_truncate(pOut, nByte);
 2132#endif
 2133}
 2134
 2135/*
 2136** Store string zUtf to pOut as w characters.  If w is negative,
 2137** then right-justify the text.  W is the width in display characters, not
 2138** in bytes.  Double-width unicode characters count as two characters.
 2139** VT100 escape sequences count as zero.  And so forth.
 2140*/
 2141static void qrfWidthPrint(Qrf *p, sqlite3_str *pOut, int w, const char *zUtf){
 2142  const unsigned char *a = (const unsigned char*)zUtf;
 2143  static const int mxW = 10000000;
 2144  unsigned char c;
 2145  int i = 0;
 2146  int n = 0;
 2147  int k;
 2148  int aw;
 2149  (void)p;
 2150  if( w<-mxW ){
 2151    w = -mxW;
 2152  }else if( w>mxW ){
 2153    w= mxW;
 2154  }
 2155  aw = w<0 ? -w : w;
 2156  if( a==0 ) a = (const unsigned char*)"";
 2157  while( (c = a[i])!=0 ){
 2158    if( (c&0xc0)==0xc0 ){
 2159      int u;
 2160      int len = sqlite3_qrf_decode_utf8(a+i, &u);
 2161      int x = sqlite3_qrf_wcwidth(u);
 2162      if( x+n>aw ){
 2163        break;
 2164      }
 2165      i += len;
 2166      n += x;
 2167    }else if( c==0x1b && (k = qrfIsVt100(&a[i]))>0 ){
 2168      i += k;       
 2169    }else if( n>=aw ){
 2170      break;
 2171    }else{
 2172      n++;
 2173      i++;
 2174    }
 2175  }
 2176  if( n>=aw ){
 2177    sqlite3_str_append(pOut, zUtf, i);
 2178  }else if( w<0 ){
 2179    if( aw>n ) sqlite3_str_appendchar(pOut, aw-n, ' ');
 2180    sqlite3_str_append(pOut, zUtf, i);
 2181  }else{
 2182    sqlite3_str_append(pOut, zUtf, i);
 2183    if( aw>n ) sqlite3_str_appendchar(pOut, aw-n, ' ');
 2184  }
 2185}
 2186
 2187/*
 2188** (*pz)[] is a line of text that is to be displayed the box or table or
 2189** similar tabular formats.  z[] contain newlines or might be too wide
 2190** to fit in the columns so will need to be split into multiple line.
 2191**
 2192** This routine determines:
 2193**
 2194**    *  How many bytes of z[] should be shown on the current line.
 2195**    *  How many character positions those bytes will cover.
 2196**    *  The byte offset to the start of the next line.
 2197*/
 2198static void qrfWrapLine(
 2199  const char *zIn,   /* Input text to be displayed */
 2200  int w,             /* Column width in characters (not bytes) */
 2201  int bWrap,         /* True if we should do word-wrapping */
 2202  int *pnThis,       /* OUT: How many bytes of z[] for the current line */
 2203  int *pnWide,       /* OUT: How wide is the text of this line */
 2204  int *piNext        /* OUT: Offset into z[] to start of the next line */
 2205){
 2206  int i;                 /* Input bytes consumed */
 2207  int k;                 /* Bytes in a VT100 code */
 2208  int n;                 /* Output column number */
 2209  const unsigned char *z = (const unsigned char*)zIn;
 2210  unsigned char c = 0;
 2211
 2212  if( z[0]==0 ){
 2213    *pnThis = 0;
 2214    *pnWide = 0;
 2215    *piNext = 0;
 2216    return;
 2217  }
 2218  n = 0;
 2219  for(i=0; n<=w; i++){
 2220    c = z[i];
 2221    if( c>=0xc0 ){
 2222      int u;
 2223      int len = sqlite3_qrf_decode_utf8(&z[i], &u);
 2224      int wcw = sqlite3_qrf_wcwidth(u);
 2225      if( wcw+n>w ) break;
 2226      i += len-1;
 2227      n += wcw;
 2228      continue;
 2229    }
 2230    if( c>=' ' ){
 2231      if( n==w ) break;
 2232      n++;
 2233      continue;
 2234    }
 2235    if( c==0 || c=='\n' ) break;
 2236    if( c=='\r' && z[i+1]=='\n' ){ c = z[++i]; break; }
 2237    if( c=='\t' ){
 2238      int wcw = 8 - (n&7);
 2239      if( n+wcw>w ) break;
 2240      n += wcw;
 2241      continue;
 2242    }
 2243    if( c==0x1b && (k = qrfIsVt100(&z[i]))>0 ){
 2244      i += k-1;
 2245    }else if( n==w ){
 2246      break;
 2247    }else{
 2248      n++;
 2249    }
 2250  }
 2251  if( c==0 ){
 2252    *pnThis = i;
 2253    *pnWide = n;
 2254    *piNext = i;
 2255    return;
 2256  }
 2257  if( c=='\n' ){
 2258    *pnThis = i;
 2259    *pnWide = n;
 2260    *piNext = i+1;
 2261    return;
 2262  }
 2263
 2264  /* If we get this far, that means the current line will end at some
 2265  ** point that is neither a "\n" or a 0x00.  Figure out where that
 2266  ** split should occur
 2267  */
 2268  if( bWrap && z[i]!=0 && !qrfSpace(z[i]) && qrfAlnum(c)==qrfAlnum(z[i]) ){
 2269    /* Perhaps try to back up to a better place to break the line */
 2270    for(k=i-1; k>=i/2; k--){
 2271      if( qrfSpace(z[k]) ) break;
 2272    }
 2273    if( k<i/2 ){
 2274      for(k=i; k>=i/2; k--){
 2275        if( qrfAlnum(z[k-1])!=qrfAlnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;
 2276      }
 2277    }
 2278    if( k>=i/2 ){
 2279      i = k;
 2280      n = qrfDisplayWidth((const char*)z, k, 0);
 2281    }
 2282  }
 2283  *pnThis = i;
 2284  *pnWide = n;
 2285  while( zIn[i]==' ' || zIn[i]=='\t' || zIn[i]=='\r' ){ i++; }
 2286  *piNext = i;
 2287}
 2288
 2289/*
 2290** Append nVal bytes of text from zVal onto the end of pOut.
 2291** Convert tab characters in zVal to the appropriate number of
 2292** spaces.
 2293*/
 2294static void qrfAppendWithTabs(
 2295  sqlite3_str *pOut,       /* Append text here */
 2296  const char *zVal,        /* Text to append */
 2297  int nVal                 /* Use only the first nVal bytes of zVal[] */
 2298){
 2299  int i = 0;
 2300  unsigned int col = 0;
 2301  unsigned char *z = (unsigned char *)zVal;
 2302  while( i<nVal ){
 2303    unsigned char c = z[i];
 2304    if( c<' ' ){
 2305      int k;
 2306      sqlite3_str_append(pOut, (const char*)z, i);
 2307      nVal -= i;
 2308      z += i;
 2309      i = 0;
 2310      if( c=='\033' && (k = qrfIsVt100(z))>0 ){
 2311        sqlite3_str_append(pOut, (const char*)z, k);
 2312        z += k;
 2313        nVal -= k;
 2314      }else if( c=='\t' ){
 2315        k = 8 - (col&7);
 2316        sqlite3_str_appendchar(pOut, k, ' ');
 2317        col += k;
 2318        z++;
 2319        nVal--;
 2320      }else if( c=='\r' && nVal==1 ){
 2321        z++;
 2322        nVal--;
 2323      }else{
 2324        char zCtrlPik[4];
 2325        col++;
 2326        zCtrlPik[0] = 0xe2;
 2327        zCtrlPik[1] = 0x90;
 2328        zCtrlPik[2] = 0x80+c;
 2329        sqlite3_str_append(pOut, zCtrlPik, 3);
 2330        z++;
 2331        nVal--;
 2332      }
 2333    }else if( (0x80&c)==0 ){
 2334      i++;
 2335      col++;
 2336    }else{
 2337      int u = 0;
 2338      int len = sqlite3_qrf_decode_utf8(&z[i], &u);
 2339      i += len;
 2340      col += sqlite3_qrf_wcwidth(u);
 2341    }
 2342  }
 2343  sqlite3_str_append(pOut, (const char*)z, i);
 2344}    
 2345
 2346/*
 2347** GCC does not define the offsetof() macro so we'll have to do it
 2348** ourselves.
 2349*/
 2350#ifndef offsetof
 2351# define offsetof(ST,M) ((size_t)((char*)&((ST*)0)->M - (char*)0))
 2352#endif
 2353
 2354/*
 2355** Data for columnar layout, collected into a single object so
 2356** that it can be more easily passed into subroutines.
 2357*/
 2358typedef struct qrfColData qrfColData;
 2359struct qrfColData {
 2360  Qrf *p;                  /* The QRF instance */
 2361  int nCol;                /* Number of columns in the table */
 2362  unsigned char bMultiRow; /* One or more cells will span multiple lines */
 2363  unsigned char nMargin;   /* Width of column margins */
 2364  sqlite3_int64 nRow;      /* Number of rows */
 2365  sqlite3_int64 nAlloc;    /* Number of cells allocated */
 2366  sqlite3_int64 n;         /* Number of cells.  nCol*nRow */
 2367  char **az;               /* Content of all cells */
 2368  int *aiWth;              /* Width of each cell */
 2369  unsigned char *abNum;    /* True for each numeric cell */
 2370  struct qrfPerCol {       /* Per-column data */
 2371    char *z;                 /* Cache of text for current row */
 2372    int w;                   /* Computed width of this column */
 2373    int mxW;                 /* Maximum natural (unwrapped) width */
 2374    unsigned char e;         /* Alignment */
 2375    unsigned char fx;        /* Width is fixed */
 2376    unsigned char bNum;      /* True if is numeric */
 2377  } *a;                    /* One per column */
 2378};
 2379
 2380/*
 2381** Output horizontally justified text into pOut.  The text is the
 2382** first nVal bytes of zVal.  Include nWS bytes of whitespace, either
 2383** split between both sides, or on the left, or on the right, depending
 2384** on eAlign.
 2385*/
 2386static void qrfPrintAligned(
 2387  sqlite3_str *pOut,       /* Append text here */
 2388  struct qrfPerCol *pCol,  /* Information about the text to print */
 2389  int nVal,                /* Use only the first nVal bytes of zVal[] */
 2390  int nWS                 /* Whitespace for horizonal alignment */
 2391){
 2392  unsigned char eAlign = pCol->e & QRF_ALIGN_HMASK;
 2393  if( eAlign==QRF_Auto && pCol->bNum ) eAlign = QRF_ALIGN_Right;
 2394  if( eAlign==QRF_ALIGN_Center ){
 2395    /* Center the text */
 2396    sqlite3_str_appendchar(pOut, nWS/2, ' ');
 2397    qrfAppendWithTabs(pOut, pCol->z, nVal);
 2398    sqlite3_str_appendchar(pOut, nWS - nWS/2, ' ');
 2399  }else if( eAlign==QRF_ALIGN_Right ){
 2400    /* Right justify the text */
 2401    sqlite3_str_appendchar(pOut, nWS, ' ');
 2402    qrfAppendWithTabs(pOut, pCol->z, nVal);
 2403  }else{
 2404    /* Left justify the text */
 2405    qrfAppendWithTabs(pOut, pCol->z, nVal);
 2406    sqlite3_str_appendchar(pOut, nWS, ' ');
 2407  }
 2408}
 2409
 2410/*
 2411** Free all the memory allocates in the qrfColData object
 2412*/
 2413static void qrfColDataFree(qrfColData *p){
 2414  sqlite3_int64 i;
 2415  for(i=0; i<p->n; i++) sqlite3_free(p->az[i]);
 2416  sqlite3_free(p->az);
 2417  sqlite3_free(p->aiWth);
 2418  sqlite3_free(p->abNum);
 2419  sqlite3_free(p->a);
 2420  memset(p, 0, sizeof(*p));
 2421}
 2422
 2423/*
 2424** Allocate space for more cells in the qrfColData object.
 2425** Return non-zero if a memory allocation fails.
 2426*/
 2427static int qrfColDataEnlarge(qrfColData *p){
 2428  char **azData;
 2429  int *aiWth;
 2430  unsigned char *abNum;
 2431  p->nAlloc = 2*p->nAlloc + 10*p->nCol;
 2432  azData = sqlite3_realloc64(p->az, p->nAlloc*sizeof(char*));
 2433  if( azData==0 ){
 2434    qrfOom(p->p);
 2435    qrfColDataFree(p);
 2436    return 1;
 2437  }
 2438  p->az = azData;
 2439  aiWth = sqlite3_realloc64(p->aiWth, p->nAlloc*sizeof(int));
 2440  if( aiWth==0 ){
 2441    qrfOom(p->p);
 2442    qrfColDataFree(p);
 2443    return 1;
 2444  }
 2445  p->aiWth = aiWth;
 2446  abNum = sqlite3_realloc64(p->abNum, p->nAlloc);
 2447  if( abNum==0 ){
 2448    qrfOom(p->p);
 2449    qrfColDataFree(p);
 2450    return 1;
 2451  }
 2452  p->abNum = abNum;
 2453  return 0;
 2454}
 2455
 2456/*
 2457** Print a markdown or table-style row separator using ascii-art
 2458*/
 2459static void qrfRowSeparator(sqlite3_str *pOut, qrfColData *p, char cSep){
 2460  int i;
 2461  if( p->nCol>0 ){
 2462    int useBorder = p->p->spec.bBorder!=QRF_No;
 2463    if( useBorder ){
 2464      sqlite3_str_append(pOut, &cSep, 1);
 2465    }
 2466    sqlite3_str_appendchar(pOut, p->a[0].w+p->nMargin, '-');
 2467    for(i=1; i<p->nCol; i++){
 2468      sqlite3_str_append(pOut, &cSep, 1);
 2469      sqlite3_str_appendchar(pOut, p->a[i].w+p->nMargin, '-');
 2470    }
 2471    if( useBorder ){
 2472      sqlite3_str_append(pOut, &cSep, 1);
 2473    }
 2474  }
 2475  sqlite3_str_append(pOut, "\n", 1);
 2476}
 2477
 2478/*
 2479** UTF8 box-drawing characters.  Imagine box lines like this:
 2480**
 2481**           1
 2482**           |
 2483**       4 --+-- 2
 2484**           |
 2485**           3
 2486**
 2487** Each box characters has between 2 and 4 of the lines leading from
 2488** the center.  The characters are here identified by the numbers of
 2489** their corresponding lines.
 2490*/
 2491#define BOX_24   "\342\224\200"  /* U+2500 --- */
 2492#define BOX_13   "\342\224\202"  /* U+2502  |  */
 2493#define BOX_23   "\342\224\214"  /* U+250c  ,- */
 2494#define BOX_34   "\342\224\220"  /* U+2510 -,  */
 2495#define BOX_12   "\342\224\224"  /* U+2514  '- */
 2496#define BOX_14   "\342\224\230"  /* U+2518 -'  */
 2497#define BOX_123  "\342\224\234"  /* U+251c  |- */
 2498#define BOX_134  "\342\224\244"  /* U+2524 -|  */
 2499#define BOX_234  "\342\224\254"  /* U+252c -,- */
 2500#define BOX_124  "\342\224\264"  /* U+2534 -'- */
 2501#define BOX_1234 "\342\224\274"  /* U+253c -|- */
 2502
 2503/* Rounded corners: */
 2504#define BOX_R12  "\342\225\260"  /* U+2570  '- */
 2505#define BOX_R23  "\342\225\255"  /* U+256d  ,- */
 2506#define BOX_R34  "\342\225\256"  /* U+256e -,  */
 2507#define BOX_R14  "\342\225\257"  /* U+256f -'  */
 2508
 2509/* Doubled horizontal lines: */
 2510#define DBL_24   "\342\225\220"  /* U+2550 === */
 2511#define DBL_123  "\342\225\236"  /* U+255e  |= */
 2512#define DBL_134  "\342\225\241"  /* U+2561 =|  */
 2513#define DBL_1234 "\342\225\252"  /* U+256a =|= */
 2514
 2515/* Draw horizontal line N characters long using unicode box
 2516** characters
 2517*/
 2518static void qrfBoxLine(sqlite3_str *pOut, int N, int bDbl){
 2519  const char *azDash[2] = {
 2520      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24   BOX_24 BOX_24 BOX_24 BOX_24 BOX_24,
 2521      DBL_24 DBL_24 DBL_24 DBL_24 DBL_24   DBL_24 DBL_24 DBL_24 DBL_24 DBL_24
 2522  };/*  0       1      2     3      4        5      6      7      8      9   */
 2523  const int nDash = 30;
 2524  N *= 3;
 2525  while( N>nDash ){
 2526    sqlite3_str_append(pOut, azDash[bDbl], nDash);
 2527    N -= nDash;
 2528  }
 2529  sqlite3_str_append(pOut, azDash[bDbl], N);
 2530}
 2531
 2532/*
 2533** Draw a horizontal separator for a QRF_STYLE_Box table.
 2534*/
 2535static void qrfBoxSeparator(
 2536  sqlite3_str *pOut,
 2537  qrfColData *p,
 2538  const char *zSep1,
 2539  const char *zSep2,
 2540  const char *zSep3,
 2541  int bDbl
 2542){
 2543  int i;
 2544  if( p->nCol>0 ){
 2545    int useBorder = p->p->spec.bBorder!=QRF_No;
 2546    if( useBorder ){
 2547      sqlite3_str_appendall(pOut, zSep1);
 2548    }
 2549    qrfBoxLine(pOut, p->a[0].w+p->nMargin, bDbl);
 2550    for(i=1; i<p->nCol; i++){
 2551      sqlite3_str_appendall(pOut, zSep2);
 2552      qrfBoxLine(pOut, p->a[i].w+p->nMargin, bDbl);
 2553    }
 2554    if( useBorder ){
 2555      sqlite3_str_appendall(pOut, zSep3);
 2556    }
 2557  }
 2558  sqlite3_str_append(pOut, "\n", 1);
 2559}
 2560
 2561/*
 2562** Load into pData the default alignment for the body of a table.
 2563*/
 2564static void qrfLoadAlignment(qrfColData *pData, Qrf *p){
 2565  sqlite3_int64 i;
 2566  for(i=0; i<pData->nCol; i++){
 2567    pData->a[i].e = p->spec.eDfltAlign;
 2568    if( i<p->spec.nAlign ){
 2569      unsigned char ax = p->spec.aAlign[i];
 2570      if( (ax & QRF_ALIGN_HMASK)!=0 ){
 2571        pData->a[i].e = (ax & QRF_ALIGN_HMASK) |
 2572                            (pData->a[i].e & QRF_ALIGN_VMASK);
 2573      }
 2574    }else if( i<p->spec.nWidth ){
 2575      if( p->spec.aWidth[i]<0 ){
 2576         pData->a[i].e = QRF_ALIGN_Right |
 2577                               (pData->a[i].e & QRF_ALIGN_VMASK);
 2578      }
 2579    }
 2580  }
 2581}
 2582
 2583/*
 2584** If the single column in pData->a[] with pData->n entries can be
 2585** laid out as nCol columns with a 2-space gap between each such
 2586** that all columns fit within nSW, then return a pointer to an array
 2587** of integers which is the width of each column from left to right.
 2588**
 2589** If the layout is not possible, return a NULL pointer.
 2590**
 2591** Space to hold the returned array is from sqlite_malloc64().
 2592*/
 2593static int *qrfValidLayout(
 2594  qrfColData *pData,   /* Collected query results */
 2595  Qrf *p,              /* On which to report an OOM */
 2596  int nCol,            /* Attempt this many columns */
 2597  int nSW              /* Screen width */
 2598){
 2599  int i;        /* Loop counter */
 2600  int nr;       /* Number of rows */
 2601  int w = 0;    /* Width of the current column */
 2602  int t;        /* Total width of all columns */
 2603  int *aw;      /* Array of individual column widths */
 2604
 2605  aw = sqlite3_malloc64( sizeof(int)*nCol );
 2606  if( aw==0 ){
 2607    qrfOom(p);
 2608    return 0;
 2609  }
 2610  nr = (pData->n + nCol - 1)/nCol;
 2611  for(i=0; i<pData->n; i++){
 2612    if( (i%nr)==0 ){
 2613      if( i>0 ) aw[i/nr-1] = w;
 2614      w = pData->aiWth[i];
 2615    }else if( pData->aiWth[i]>w ){
 2616      w = pData->aiWth[i];
 2617    }
 2618  }
 2619  aw[nCol-1] = w;
 2620  for(t=i=0; i<nCol; i++) t += aw[i];
 2621  t += 2*(nCol-1);
 2622  if( t>nSW ){
 2623    sqlite3_free(aw);
 2624    return 0;
 2625  }
 2626  return aw;
 2627}
 2628
 2629/*
 2630** The output is single-column and the bSplitColumn flag is set.
 2631** Check to see if the single-column output can be split into multiple
 2632** columns that appear side-by-side.  Adjust pData appropriately.
 2633*/
 2634static void qrfSplitColumn(qrfColData *pData, Qrf *p){
 2635  int nCol = 1;
 2636  int *aw = 0;
 2637  char **az = 0;
 2638  int *aiWth = 0;
 2639  unsigned char *abNum = 0;
 2640  int nColNext = 2;
 2641  int w;
 2642  struct qrfPerCol *a = 0;
 2643  sqlite3_int64 nRow = 1;
 2644  sqlite3_int64 i;
 2645  while( 1/*exit-by-break*/ ){
 2646    int *awNew = qrfValidLayout(pData, p, nColNext, p->spec.nScreenWidth);
 2647    if( awNew==0 ) break;
 2648    sqlite3_free(aw);
 2649    aw = awNew;
 2650    nCol = nColNext;
 2651    nRow = (pData->n + nCol - 1)/nCol;
 2652    if( nRow==1 ) break;
 2653    nColNext++;
 2654    while( (pData->n + nColNext - 1)/nColNext == nRow ) nColNext++;
 2655  }
 2656  if( nCol==1 ){
 2657    sqlite3_free(aw);
 2658    return;  /* Cannot do better than 1 column */
 2659  }
 2660  az = sqlite3_malloc64( nRow*nCol*sizeof(char*) );
 2661  if( az==0 ){
 2662    qrfOom(p);
 2663    return;
 2664  }
 2665  aiWth = sqlite3_malloc64( nRow*nCol*sizeof(int) );
 2666  if( aiWth==0 ){
 2667    sqlite3_free(az);
 2668    qrfOom(p);
 2669    return;
 2670  }
 2671  a = sqlite3_malloc64( nCol*sizeof(struct qrfPerCol) );
 2672  if( a==0 ){
 2673    sqlite3_free(az);
 2674    sqlite3_free(aiWth);
 2675    qrfOom(p);
 2676    return;
 2677  }
 2678  abNum = sqlite3_malloc64( nRow*nCol );
 2679  if( abNum==0 ){
 2680    sqlite3_free(az);
 2681    sqlite3_free(aiWth);
 2682    sqlite3_free(a);
 2683    qrfOom(p);
 2684    return;
 2685  }
 2686  for(i=0; i<pData->n; i++){
 2687    sqlite3_int64 j = (i%nRow)*nCol + (i/nRow);
 2688    az[j] = pData->az[i];
 2689    abNum[j]= pData->abNum[i];
 2690    pData->az[i] = 0;
 2691    aiWth[j] = pData->aiWth[i];
 2692  }
 2693  while( i<nRow*nCol ){
 2694    sqlite3_int64 j = (i%nRow)*nCol + (i/nRow);
 2695    az[j] = sqlite3_mprintf("");
 2696    if( az[j]==0 ) qrfOom(p);
 2697    aiWth[j] = 0;
 2698    abNum[j] = 0;
 2699    i++;
 2700  }
 2701  for(i=0; i<nCol; i++){
 2702    a[i].fx = a[i].mxW = a[i].w = aw[i];
 2703    a[i].e = pData->a[0].e;
 2704  }
 2705  sqlite3_free(pData->az);
 2706  sqlite3_free(pData->aiWth);
 2707  sqlite3_free(pData->a);
 2708  sqlite3_free(pData->abNum);
 2709  sqlite3_free(aw);
 2710  pData->az = az;
 2711  pData->aiWth = aiWth;
 2712  pData->a = a;
 2713  pData->abNum = abNum;
 2714  pData->nCol = nCol;
 2715  pData->n = pData->nAlloc = nRow*nCol;
 2716  for(i=w=0; i<nCol; i++) w += a[i].w;
 2717  pData->nMargin = (p->spec.nScreenWidth - w)/(nCol - 1);
 2718  if( pData->nMargin>5 ) pData->nMargin = 5;
 2719}
 2720
 2721/*
 2722** Adjust the layout for the screen width restriction
 2723*/
 2724static void qrfRestrictScreenWidth(qrfColData *pData, Qrf *p){
 2725  int sepW;             /* Width of all box separators and margins */
 2726  int sumW;             /* Total width of data area over all columns */
 2727  int targetW;          /* Desired total data area */
 2728  int i;                /* Loop counters */
 2729  int nCol;             /* Number of columns */
 2730
 2731  pData->nMargin = 2;   /* Default to normal margins */
 2732  if( p->spec.nScreenWidth==0 ) return;
 2733  if( p->spec.eStyle==QRF_STYLE_Column ){
 2734    sepW = pData->nCol*2 - 2;
 2735  }else{
 2736    sepW = pData->nCol*3 + 1;
 2737    if( p->spec.bBorder==QRF_No ) sepW -= 2;
 2738  }
 2739  nCol = pData->nCol;
 2740  for(i=sumW=0; i<nCol; i++) sumW += pData->a[i].w;
 2741  if( p->spec.nScreenWidth >= sumW+sepW ) return;
 2742
 2743  /* First thing to do is reduce the separation between columns */
 2744  pData->nMargin = 0;
 2745  if( p->spec.eStyle==QRF_STYLE_Column ){
 2746    sepW = pData->nCol - 1;
 2747  }else{
 2748    sepW = pData->nCol + 1;
 2749    if( p->spec.bBorder==QRF_No ) sepW -= 2;
 2750  }
 2751  targetW = p->spec.nScreenWidth - sepW;
 2752
 2753#define MIN_SQUOZE    8
 2754#define MIN_EX_SQUOZE 16
 2755  /* Reduce the width of the widest eligible column.  A column is
 2756  ** eligible for narrowing if:
 2757  **
 2758  **    *  It is not a fixed-width column  (a[0].fx is false)
 2759  **    *  The current width is more than MIN_SQUOZE
 2760  **    *  Either:
 2761  **         +  The current width is more then MIN_EX_SQUOZE, or
 2762  **         +  The current width is more than half the max width (a[].mxW)
 2763  **
 2764  ** Keep making reductions until either no more reductions are
 2765  ** possible or until the size target is reached.
 2766  */
 2767  while( sumW > targetW ){
 2768    int gain, w;
 2769    int ix = -1;
 2770    int mx = 0;
 2771    for(i=0; i<nCol; i++){
 2772      if( pData->a[i].fx==0
 2773       && (w = pData->a[i].w)>mx
 2774       && w>MIN_SQUOZE
 2775       && (w>MIN_EX_SQUOZE || w*2>pData->a[i].mxW)
 2776      ){
 2777        ix = i;
 2778        mx = w;
 2779      }
 2780    }
 2781    if( ix<0 ) break;
 2782    if( mx>=MIN_SQUOZE*2 ){
 2783      gain = mx/2;
 2784    }else{
 2785      gain = mx - MIN_SQUOZE;
 2786    }
 2787    if( sumW - gain < targetW ){
 2788      gain = sumW - targetW;
 2789    }
 2790    sumW -= gain;
 2791    pData->a[ix].w -= gain;
 2792    pData->bMultiRow = 1;
 2793  }
 2794}
 2795
 2796/*
 2797** Columnar modes require that the entire query be evaluated first, with
 2798** results written into memory, so that we can compute appropriate column
 2799** widths.
 2800*/
 2801static void qrfColumnar(Qrf *p){
 2802  sqlite3_int64 i, j;                     /* Loop counters */
 2803  const char *colSep = 0;                 /* Column separator text */
 2804  const char *rowSep = 0;                 /* Row terminator text */
 2805  const char *rowStart = 0;               /* Row start text */
 2806  int szColSep, szRowSep, szRowStart;     /* Size in bytes of previous 3 */
 2807  int rc;                                 /* Result code */
 2808  int nColumn = p->nCol;                  /* Number of columns */
 2809  int bWW;                                /* True to do word-wrap */
 2810  sqlite3_str *pStr;                      /* Temporary rendering */
 2811  qrfColData data;                        /* Columnar layout data */
 2812  int bRTrim;                             /* Trim trailing space */
 2813
 2814  rc = sqlite3_step(p->pStmt);
 2815  if( rc!=SQLITE_ROW || nColumn==0 ){
 2816    return;   /* No output */
 2817  }
 2818
 2819  /* Initialize the data container */
 2820  memset(&data, 0, sizeof(data));
 2821  data.nCol = p->nCol;
 2822  data.p = p;
 2823  data.a = sqlite3_malloc64( nColumn*sizeof(struct qrfPerCol) );
 2824  if( data.a==0 ){
 2825    qrfOom(p);
 2826    return;
 2827  }
 2828  memset(data.a, 0, nColumn*sizeof(struct qrfPerCol) );
 2829  if( qrfColDataEnlarge(&data) ) return;
 2830  assert( data.az!=0 );
 2831
 2832  /* Load the column header names and all cell content into data */
 2833  if( p->spec.bTitles==QRF_Yes ){
 2834    unsigned char saved_eText = p->spec.eText;
 2835    p->spec.eText = p->spec.eTitle;
 2836    memset(data.abNum, 0, nColumn);
 2837    for(i=0; i<nColumn; i++){
 2838      const char *z = (const char*)sqlite3_column_name(p->pStmt,i);
 2839      int nNL = 0;
 2840      int n, w;
 2841      pStr = sqlite3_str_new(p->db);
 2842      qrfEncodeText(p, pStr, z ? z : "");
 2843      n = sqlite3_str_length(pStr);
 2844      qrfStrErr(p, pStr);
 2845      z = data.az[data.n] = sqlite3_str_finish(pStr);
 2846      if( p->spec.nTitleLimit ){
 2847        nNL = 0;
 2848        data.aiWth[data.n] = w = qrfTitleLimit(data.az[data.n],
 2849                                               p->spec.nTitleLimit );
 2850      }else{
 2851        data.aiWth[data.n] = w = qrfDisplayWidth(z, n, &nNL);
 2852      }
 2853      data.n++;
 2854      if( w>data.a[i].mxW ) data.a[i].mxW = w;
 2855      if( nNL ) data.bMultiRow = 1;
 2856    }
 2857    p->spec.eText = saved_eText;
 2858    p->nRow++;
 2859  }
 2860  do{
 2861    if( data.n+nColumn > data.nAlloc ){
 2862      if( qrfColDataEnlarge(&data) ) return;
 2863    }
 2864    for(i=0; i<nColumn; i++){
 2865      char *z;
 2866      int nNL = 0;
 2867      int n, w;
 2868      int eType = sqlite3_column_type(p->pStmt,i);
 2869      pStr = sqlite3_str_new(p->db);
 2870      qrfRenderValue(p, pStr, i);
 2871      n = sqlite3_str_length(pStr);
 2872      qrfStrErr(p, pStr);
 2873      z = data.az[data.n] = sqlite3_str_finish(pStr);
 2874      data.abNum[data.n] = eType==SQLITE_INTEGER || eType==SQLITE_FLOAT;
 2875      data.aiWth[data.n] = w = qrfDisplayWidth(z, n, &nNL);
 2876      data.n++;
 2877      if( w>data.a[i].mxW ) data.a[i].mxW = w;
 2878      if( nNL ) data.bMultiRow = 1;
 2879    }
 2880    p->nRow++;
 2881  }while( sqlite3_step(p->pStmt)==SQLITE_ROW && p->iErr==SQLITE_OK );
 2882  if( p->iErr ){
 2883    qrfColDataFree(&data);
 2884    return;
 2885  }
 2886
 2887  /* Compute the width and alignment of every column */
 2888  if( p->spec.bTitles==QRF_No ){
 2889    qrfLoadAlignment(&data, p);
 2890  }else{
 2891    unsigned char e;
 2892    if( p->spec.eTitleAlign==QRF_Auto ){
 2893      e = QRF_ALIGN_Center;
 2894    }else{
 2895      e = p->spec.eTitleAlign;
 2896    }
 2897    for(i=0; i<nColumn; i++) data.a[i].e = e;
 2898  }
 2899
 2900  for(i=0; i<nColumn; i++){
 2901    int w = 0;
 2902    if( i<p->spec.nWidth ){
 2903      w = p->spec.aWidth[i];
 2904      if( w==(-32768) ){
 2905        w = 0;
 2906        if( p->spec.nAlign>i && (p->spec.aAlign[i] & QRF_ALIGN_HMASK)==0 ){
 2907          data.a[i].e |= QRF_ALIGN_Right;
 2908        }
 2909      }else if( w<0 ){
 2910        w = -w;
 2911        if( p->spec.nAlign>i && (p->spec.aAlign[i] & QRF_ALIGN_HMASK)==0 ){
 2912          data.a[i].e |= QRF_ALIGN_Right;
 2913        }
 2914      }
 2915      if( w ) data.a[i].fx = 1;
 2916    }
 2917    if( w==0 ){
 2918      w = data.a[i].mxW;
 2919      if( p->spec.nWrap>0 && w>p->spec.nWrap ){
 2920        w = p->spec.nWrap;
 2921        data.bMultiRow = 1;
 2922      }
 2923    }else if( (data.bMultiRow==0 || w==1) && data.a[i].mxW>w ){
 2924      data.bMultiRow = 1;
 2925      if( w==1 ){
 2926        /* If aiWth[j] is 2 or more, then there might be a double-wide
 2927        ** character somewhere.  So make the column width at least 2. */
 2928        w = 2;
 2929      }
 2930    }
 2931    data.a[i].w = w;
 2932  }
 2933
 2934  if( nColumn==1
 2935   && data.n>1
 2936   && p->spec.bSplitColumn==QRF_Yes
 2937   && p->spec.eStyle==QRF_STYLE_Column
 2938   && p->spec.bTitles==QRF_No
 2939   && p->spec.nScreenWidth>data.a[0].w+3
 2940  ){
 2941    /* Attempt to convert single-column tables into multi-column by
 2942    ** verticle wrapping, if the screen is wide enough and if the
 2943    ** bSplitColumn flag is set. */
 2944    qrfSplitColumn(&data, p);
 2945    nColumn = data.nCol;
 2946  }else{
 2947    /* Adjust the column widths due to screen width restrictions */
 2948    qrfRestrictScreenWidth(&data, p);
 2949  }
 2950
 2951  /* Draw the line across the top of the table.  Also initialize
 2952  ** the row boundary and column separator texts. */
 2953  switch( p->spec.eStyle ){
 2954    case QRF_STYLE_Box:
 2955      if( data.nMargin ){
 2956        rowStart = BOX_13 " ";
 2957        colSep = " " BOX_13 " ";
 2958        rowSep = " " BOX_13 "\n";
 2959      }else{
 2960        rowStart = BOX_13;
 2961        colSep = BOX_13;
 2962        rowSep = BOX_13 "\n";
 2963      }
 2964      if( p->spec.bBorder==QRF_No){
 2965        rowStart += 3;
 2966        rowSep = "\n";
 2967      }else{
 2968        qrfBoxSeparator(p->pOut, &data, BOX_R23, BOX_234, BOX_R34, 0);
 2969      }
 2970      break;
 2971    case QRF_STYLE_Table:
 2972      if( data.nMargin ){
 2973        rowStart = "| ";
 2974        colSep = " | ";
 2975        rowSep = " |\n";
 2976      }else{
 2977        rowStart = "|";
 2978        colSep = "|";
 2979        rowSep = "|\n";
 2980      }
 2981      if( p->spec.bBorder==QRF_No ){
 2982        rowStart += 1;
 2983        rowSep = "\n";
 2984      }else{
 2985        qrfRowSeparator(p->pOut, &data, '+');
 2986      }
 2987      break;
 2988    case QRF_STYLE_Column: {
 2989      static const char zSpace[] = "     ";
 2990      rowStart = "";
 2991      if( data.nMargin<2 ){
 2992        colSep = " ";
 2993      }else if( data.nMargin<=5 ){
 2994        colSep = &zSpace[5-data.nMargin];
 2995      }else{
 2996        colSep = zSpace;
 2997      }
 2998      rowSep = "\n";
 2999      break;
 3000    }
 3001    default:  /*case QRF_STYLE_Markdown:*/
 3002      if( data.nMargin ){
 3003        rowStart = "| ";
 3004        colSep = " | ";
 3005        rowSep = " |\n";
 3006      }else{
 3007        rowStart = "|";
 3008        colSep = "|";
 3009        rowSep = "|\n";
 3010      }
 3011      break;
 3012  }
 3013  szRowStart = (int)strlen(rowStart);
 3014  szRowSep = (int)strlen(rowSep);
 3015  szColSep = (int)strlen(colSep);
 3016
 3017  bWW = (p->spec.bWordWrap==QRF_Yes && data.bMultiRow);
 3018  if( p->spec.eStyle==QRF_STYLE_Column
 3019   || (p->spec.bBorder==QRF_No
 3020       && (p->spec.eStyle==QRF_STYLE_Box || p->spec.eStyle==QRF_STYLE_Table)
 3021      )
 3022  ){
 3023    bRTrim = 1;
 3024  }else{
 3025    bRTrim = 0;
 3026  }
 3027  for(i=0; i<data.n && sqlite3_str_errcode(p->pOut)==SQLITE_OK; i+=nColumn){
 3028    int bMore;
 3029    int nRow = 0;
 3030
 3031    /* Draw a single row of the table.  This might be the title line
 3032    ** (if there is a title line) or a row in the body of the table.
 3033    ** The column number will be j.  The row number is i/nColumn.
 3034    */
 3035    for(j=0; j<nColumn; j++){
 3036      data.a[j].z = data.az[i+j];
 3037      if( data.a[j].z==0 ) data.a[j].z = "";
 3038      data.a[j].bNum = data.abNum[i+j];
 3039    }
 3040    do{
 3041      sqlite3_str_append(p->pOut, rowStart, szRowStart);
 3042      bMore = 0;
 3043      for(j=0; j<nColumn; j++){
 3044        int nThis = 0;
 3045        int nWide = 0;
 3046        int iNext = 0;
 3047        int nWS;
 3048        qrfWrapLine(data.a[j].z, data.a[j].w, bWW, &nThis, &nWide, &iNext);
 3049        nWS = data.a[j].w - nWide;
 3050        qrfPrintAligned(p->pOut, &data.a[j], nThis, nWS);
 3051        data.a[j].z += iNext;
 3052        if( data.a[j].z[0]!=0 ){
 3053          bMore = 1;
 3054        }
 3055        if( j<nColumn-1 ){
 3056          sqlite3_str_append(p->pOut, colSep, szColSep);
 3057        }else{
 3058          if( bRTrim ) qrfRTrim(p->pOut);
 3059          sqlite3_str_append(p->pOut, rowSep, szRowSep);
 3060        }
 3061      }
 3062    }while( bMore && ++nRow < p->mxHeight );
 3063    if( bMore ){
 3064      /* This row was terminated by nLineLimit.  Show ellipsis. */
 3065      sqlite3_str_append(p->pOut, rowStart, szRowStart);
 3066      for(j=0; j<nColumn; j++){
 3067        if( data.a[j].z[0]==0 ){
 3068          sqlite3_str_appendchar(p->pOut, data.a[j].w, ' ');
 3069        }else{
 3070          int nE = 3;
 3071          if( nE>data.a[j].w ) nE = data.a[j].w;
 3072          data.a[j].z = "...";
 3073          qrfPrintAligned(p->pOut, &data.a[j], nE, data.a[j].w-nE);
 3074        }
 3075        if( j<nColumn-1 ){
 3076          sqlite3_str_append(p->pOut, colSep, szColSep);
 3077        }else{
 3078          if( bRTrim ) qrfRTrim(p->pOut);
 3079          sqlite3_str_append(p->pOut, rowSep, szRowSep);
 3080        }
 3081      }
 3082    }
 3083
 3084    /* Draw either (1) the separator between the title line and the body
 3085    ** of the table, or (2) separators between individual rows of the table
 3086    ** body.  isTitleDataSeparator will be true if we are doing (1).
 3087    */
 3088    if( (i==0 || data.bMultiRow) && i+nColumn<data.n ){
 3089      int isTitleDataSeparator = (i==0 && p->spec.bTitles==QRF_Yes);
 3090      if( isTitleDataSeparator ){
 3091        qrfLoadAlignment(&data, p);
 3092      }
 3093      switch( p->spec.eStyle ){
 3094        case QRF_STYLE_Table: {
 3095          if( isTitleDataSeparator || data.bMultiRow ){
 3096            qrfRowSeparator(p->pOut, &data, '+');
 3097          }
 3098          break;
 3099        }
 3100        case QRF_STYLE_Box: {
 3101          if( isTitleDataSeparator ){
 3102            qrfBoxSeparator(p->pOut, &data, DBL_123, DBL_1234, DBL_134, 1);
 3103          }else if( data.bMultiRow ){
 3104            qrfBoxSeparator(p->pOut, &data, BOX_123, BOX_1234, BOX_134, 0);
 3105          }
 3106          break;
 3107        }
 3108        case QRF_STYLE_Markdown: {
 3109          if( isTitleDataSeparator ){
 3110            qrfRowSeparator(p->pOut, &data, '|');
 3111          }
 3112          break;
 3113        }
 3114        case QRF_STYLE_Column: {
 3115          if( isTitleDataSeparator ){
 3116            for(j=0; j<nColumn; j++){
 3117              sqlite3_str_appendchar(p->pOut, data.a[j].w, '-');
 3118              if( j<nColumn-1 ){
 3119                sqlite3_str_append(p->pOut, colSep, szColSep);
 3120              }else{
 3121                qrfRTrim(p->pOut);
 3122                sqlite3_str_append(p->pOut, rowSep, szRowSep);
 3123              }
 3124            }
 3125          }else if( data.bMultiRow ){
 3126            qrfRTrim(p->pOut);
 3127            sqlite3_str_append(p->pOut, "\n", 1);
 3128          }
 3129          break;
 3130        }
 3131      }
 3132    }
 3133  }
 3134
 3135  /* Draw the line across the bottom of the table */
 3136  if( p->spec.bBorder!=QRF_No ){
 3137    switch( p->spec.eStyle ){
 3138      case QRF_STYLE_Box:
 3139        qrfBoxSeparator(p->pOut, &data, BOX_R12, BOX_124, BOX_R14, 0);
 3140        break;
 3141      case QRF_STYLE_Table:
 3142        qrfRowSeparator(p->pOut, &data, '+');
 3143        break;
 3144    }
 3145  }
 3146  qrfWrite(p);
 3147
 3148  qrfColDataFree(&data);
 3149  return;
 3150}
 3151
 3152/*
 3153** Parameter azArray points to a zero-terminated array of strings. zStr
 3154** points to a single nul-terminated string. Return non-zero if zStr
 3155** is equal, according to strcmp(), to any of the strings in the array.
 3156** Otherwise, return zero.
 3157*/
 3158static int qrfStringInArray(const char *zStr, const char **azArray){
 3159  int i;
 3160  if( zStr==0 ) return 0;
 3161  for(i=0; azArray[i]; i++){
 3162    if( 0==strcmp(zStr, azArray[i]) ) return 1;
 3163  }
 3164  return 0;
 3165}
 3166
 3167/*
 3168** Print out an EXPLAIN with indentation.  This is a two-pass algorithm.
 3169**
 3170** On the first pass, we compute aiIndent[iOp] which is the amount of
 3171** indentation to apply to the iOp-th opcode.  The output actually occurs
 3172** on the second pass.
 3173**
 3174** The indenting rules are:
 3175**
 3176**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
 3177**       all opcodes that occur between the p2 jump destination and the opcode
 3178**       itself by 2 spaces.
 3179**
 3180**     * Do the previous for "Return" instructions for when P2 is positive.
 3181**       See tag-20220407a in wherecode.c and vdbe.c.
 3182**
 3183**     * For each "Goto", if the jump destination is earlier in the program
 3184**       and ends on one of:
 3185**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
 3186**       or if the P1 parameter is one instead of zero,
 3187**       then indent all opcodes between the earlier instruction
 3188**       and "Goto" by 2 spaces.
 3189*/
 3190static void qrfExplain(Qrf *p){
 3191  int *abYield = 0;     /* abYield[iOp] is rue if opcode iOp is an OP_Yield */
 3192  int *aiIndent = 0;    /* Indent the iOp-th opcode by aiIndent[iOp] */
 3193  i64 nAlloc = 0;       /* Allocated size of aiIndent[], abYield */
 3194  int nIndent = 0;      /* Number of entries in aiIndent[] */
 3195  int iOp;              /* Opcode number */
 3196  int i;                /* Column loop counter */
 3197
 3198  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
 3199                           "Return", 0 };
 3200  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
 3201                            "Rewind", 0 };
 3202  const char *azGoto[] = { "Goto", 0 };
 3203
 3204  /* The caller guarantees that the leftmost 4 columns of the statement
 3205  ** passed to this function are equivalent to the leftmost 4 columns
 3206  ** of EXPLAIN statement output. In practice the statement may be
 3207  ** an EXPLAIN, or it may be a query on the bytecode() virtual table.  */
 3208  assert( sqlite3_column_count(p->pStmt)>=4 );
 3209  assert( 0==sqlite3_stricmp( sqlite3_column_name(p->pStmt, 0), "addr" ) );
 3210  assert( 0==sqlite3_stricmp( sqlite3_column_name(p->pStmt, 1), "opcode" ) );
 3211  assert( 0==sqlite3_stricmp( sqlite3_column_name(p->pStmt, 2), "p1" ) );
 3212  assert( 0==sqlite3_stricmp( sqlite3_column_name(p->pStmt, 3), "p2" ) );
 3213
 3214  for(iOp=0; SQLITE_ROW==sqlite3_step(p->pStmt) && !p->iErr; iOp++){
 3215    int iAddr = sqlite3_column_int(p->pStmt, 0);
 3216    const char *zOp = (const char*)sqlite3_column_text(p->pStmt, 1);
 3217    int p1 = sqlite3_column_int(p->pStmt, 2);
 3218    int p2 = sqlite3_column_int(p->pStmt, 3);
 3219
 3220    /* Assuming that p2 is an instruction address, set variable p2op to the
 3221    ** index of that instruction in the aiIndent[] array. p2 and p2op may be
 3222    ** different if the current instruction is part of a sub-program generated
 3223    ** by an SQL trigger or foreign key.  */
 3224    int p2op = (p2 + (iOp-iAddr));
 3225
 3226    /* Grow the aiIndent array as required */
 3227    if( iOp>=nAlloc ){
 3228      nAlloc += 100;
 3229      aiIndent = (int*)sqlite3_realloc64(aiIndent, nAlloc*sizeof(int));
 3230      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
 3231      if( aiIndent==0 || abYield==0 ){
 3232        qrfOom(p);
 3233        sqlite3_free(aiIndent);
 3234        sqlite3_free(abYield);
 3235        return;
 3236      }
 3237    }
 3238
 3239    abYield[iOp] = qrfStringInArray(zOp, azYield);
 3240    aiIndent[iOp] = 0;
 3241    nIndent = iOp+1;
 3242    if( qrfStringInArray(zOp, azNext) && p2op>0 ){
 3243      for(i=p2op; i<iOp; i++) aiIndent[i] += 2;
 3244    }
 3245    if( qrfStringInArray(zOp, azGoto) && p2op<iOp && (abYield[p2op] || p1) ){
 3246      for(i=p2op; i<iOp; i++) aiIndent[i] += 2;
 3247    }
 3248  }
 3249  sqlite3_free(abYield);
 3250
 3251  /* Second pass.  Actually generate output */
 3252  sqlite3_reset(p->pStmt);
 3253  if( p->iErr==SQLITE_OK ){
 3254    static const int aExplainWidth[] = {4,       13, 4, 4, 4, 13, 2, 13};
 3255    static const int aExplainMap[] =   {0,       1,  2, 3, 4, 5,  6, 7 };
 3256    static const int aScanExpWidth[] = {4,15, 6, 13, 4, 4, 4, 13, 2, 13};
 3257    static const int aScanExpMap[] =   {0, 9, 8, 1,  2, 3, 4, 5,  6, 7 };
 3258    const int *aWidth = aExplainWidth;
 3259    const int *aMap = aExplainMap;
 3260    int nWidth = sizeof(aExplainWidth)/sizeof(int);
 3261    int iIndent = 1;
 3262    int nArg = p->nCol;
 3263    if( p->spec.eStyle==QRF_STYLE_StatsVm ){
 3264      aWidth = aScanExpWidth;
 3265      aMap = aScanExpMap;
 3266      nWidth = sizeof(aScanExpWidth)/sizeof(int);
 3267      iIndent = 3;
 3268    }
 3269    if( nArg>nWidth ) nArg = nWidth;
 3270
 3271    for(iOp=0; sqlite3_step(p->pStmt)==SQLITE_ROW && !p->iErr; iOp++){
 3272      /* If this is the first row seen, print out the headers */
 3273      if( iOp==0 ){
 3274        for(i=0; i<nArg; i++){
 3275          const char *zCol = sqlite3_column_name(p->pStmt, aMap[i]);
 3276          qrfWidthPrint(p,p->pOut, aWidth[i], zCol);
 3277          if( i==nArg-1 ){
 3278            sqlite3_str_append(p->pOut, "\n", 1);
 3279          }else{
 3280            sqlite3_str_append(p->pOut, "  ", 2);
 3281          }
 3282        }
 3283        for(i=0; i<nArg; i++){
 3284          sqlite3_str_appendf(p->pOut, "%.*c", aWidth[i], '-');
 3285          if( i==nArg-1 ){
 3286            sqlite3_str_append(p->pOut, "\n", 1);
 3287          }else{
 3288            sqlite3_str_append(p->pOut, "  ", 2);
 3289          }
 3290        }
 3291      }
 3292  
 3293      for(i=0; i<nArg; i++){
 3294        const char *zSep = "  ";
 3295        int w = aWidth[i];
 3296        const char *zVal = (const char*)sqlite3_column_text(p->pStmt, aMap[i]);
 3297        int len;
 3298        if( i==nArg-1 ) w = 0;
 3299        if( zVal==0 ) zVal = "";
 3300        len = (int)sqlite3_qrf_wcswidth(zVal);
 3301        if( len>w ){
 3302          w = len;
 3303          zSep = " ";
 3304        }
 3305        if( i==iIndent && aiIndent && iOp<nIndent ){
 3306          sqlite3_str_appendchar(p->pOut, aiIndent[iOp], ' ');
 3307        }
 3308        qrfWidthPrint(p, p->pOut, w, zVal);
 3309        if( i==nArg-1 ){
 3310          sqlite3_str_append(p->pOut, "\n", 1);
 3311        }else{
 3312          sqlite3_str_appendall(p->pOut, zSep);
 3313        }
 3314      }
 3315      p->nRow++;
 3316    }
 3317    qrfWrite(p);
 3318  }
 3319  sqlite3_free(aiIndent);
 3320}
 3321
 3322/*
 3323** Do a "scanstatus vm" style EXPLAIN listing on p->pStmt.
 3324**
 3325** p->pStmt is probably not an EXPLAIN query.  Instead, construct a
 3326** new query that is a bytecode() rendering of p->pStmt with extra
 3327** columns for the "scanstatus vm" outputs, and run the results of
 3328** that new query through the normal EXPLAIN formatting.
 3329*/
 3330static void qrfScanStatusVm(Qrf *p){
 3331  sqlite3_stmt *pOrigStmt = p->pStmt;
 3332  sqlite3_stmt *pExplain;
 3333  int rc;
 3334  static const char *zSql =
 3335      "  SELECT addr, opcode, p1, p2, p3, p4, p5, comment, nexec,"
 3336      "   format('% 6s (%.2f%%)',"
 3337      "      CASE WHEN ncycle<100_000 THEN ncycle || ' '"
 3338      "         WHEN ncycle<100_000_000 THEN (ncycle/1_000) || 'K'"
 3339      "         WHEN ncycle<100_000_000_000 THEN (ncycle/1_000_000) || 'M'"
 3340      "         ELSE (ncycle/1000_000_000) || 'G' END,"
 3341      "       ncycle*100.0/(sum(ncycle) OVER ())"
 3342      "   )  AS cycles"
 3343      "   FROM bytecode(?1)";
 3344  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pExplain, 0);
 3345  if( rc ){
 3346    qrfError(p, rc, "%s", sqlite3_errmsg(p->db));
 3347    sqlite3_finalize(pExplain);
 3348    return;
 3349  }
 3350  sqlite3_bind_pointer(pExplain, 1, pOrigStmt, "stmt-pointer", 0);
 3351  p->pStmt = pExplain;
 3352  p->nCol = 10;
 3353  qrfExplain(p);
 3354  sqlite3_finalize(pExplain);
 3355  p->pStmt = pOrigStmt;
 3356}
 3357
 3358/*
 3359** Attempt to determine if identifier zName needs to be quoted, either
 3360** because it contains non-alphanumeric characters, or because it is an
 3361** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
 3362** that quoting is required.
 3363**
 3364** Return 1 if quoting is required.  Return 0 if no quoting is required.
 3365*/
 3366
 3367static int qrf_need_quote(const char *zName){
 3368  int i;
 3369  const unsigned char *z = (const unsigned char*)zName;
 3370  if( z==0 ) return 1;
 3371  if( !qrfAlpha(z[0]) ) return 1;
 3372  for(i=0; z[i]; i++){
 3373    if( !qrfAlnum(z[i]) ) return 1;
 3374  }
 3375  return sqlite3_keyword_check(zName, i)!=0;
 3376}
 3377
 3378/*
 3379** Helper function for QRF_STYLE_Json and QRF_STYLE_JObject.
 3380** The initial "{" for a JSON object that will contain row content
 3381** has been output.  Now output all the content.
 3382*/
 3383static void qrfOneJsonRow(Qrf *p){
 3384  int i, nItem; 
 3385  for(nItem=i=0; i<p->nCol; i++){
 3386    const char *zCName;
 3387    zCName = sqlite3_column_name(p->pStmt, i);
 3388    if( nItem>0 ) sqlite3_str_append(p->pOut, ",", 1);
 3389    nItem++;
 3390    qrfEncodeText(p, p->pOut, zCName);
 3391    sqlite3_str_append(p->pOut, ":", 1);
 3392    qrfRenderValue(p, p->pOut, i);
 3393  }
 3394  qrfWrite(p);
 3395}
 3396
 3397/*
 3398** Render a single row of output for non-columnar styles - any
 3399** style that lets us render row by row as the content is received
 3400** from the query.
 3401*/
 3402static void qrfOneSimpleRow(Qrf *p){
 3403  int i;
 3404  switch( p->spec.eStyle ){
 3405    case QRF_STYLE_Off:
 3406    case QRF_STYLE_Count: {
 3407      /* No-op */
 3408      break;
 3409    }
 3410    case QRF_STYLE_Json: {
 3411      if( p->nRow==0 ){
 3412        sqlite3_str_append(p->pOut, "[{", 2);
 3413      }else{
 3414        sqlite3_str_append(p->pOut, "},\n{", 4);
 3415      }
 3416      qrfOneJsonRow(p);
 3417      break;
 3418    }
 3419    case QRF_STYLE_JObject: {
 3420      if( p->nRow==0 ){
 3421        sqlite3_str_append(p->pOut, "{", 1);
 3422      }else{
 3423        sqlite3_str_append(p->pOut, "}\n{", 3);
 3424      }
 3425      qrfOneJsonRow(p);
 3426      break;
 3427    }
 3428    case QRF_STYLE_Html: {
 3429      if( p->nRow==0 && p->spec.bTitles==QRF_Yes ){
 3430        sqlite3_str_append(p->pOut, "<TR>", 4);
 3431        for(i=0; i<p->nCol; i++){
 3432          const char *zCName = sqlite3_column_name(p->pStmt, i);
 3433          sqlite3_str_append(p->pOut, "\n<TH>", 5);
 3434          qrfEncodeText(p, p->pOut, zCName);
 3435        }
 3436        sqlite3_str_append(p->pOut, "\n</TR>\n", 7);
 3437      }
 3438      sqlite3_str_append(p->pOut, "<TR>", 4);
 3439      for(i=0; i<p->nCol; i++){
 3440        sqlite3_str_append(p->pOut, "\n<TD>", 5);
 3441        qrfRenderValue(p, p->pOut, i);
 3442      }
 3443      sqlite3_str_append(p->pOut, "\n</TR>\n", 7);
 3444      qrfWrite(p);
 3445      break;
 3446    }
 3447    case QRF_STYLE_Insert: {
 3448      unsigned int mxIns = p->spec.nMultiInsert;
 3449      int szStart = sqlite3_str_length(p->pOut);
 3450      if( p->u.nIns==0 || p->u.nIns>=mxIns ){
 3451        if( p->u.nIns ){
 3452          sqlite3_str_append(p->pOut, ";\n", 2);
 3453          p->u.nIns = 0;
 3454        }
 3455        if( qrf_need_quote(p->spec.zTableName) ){
 3456          sqlite3_str_appendf(p->pOut,"INSERT INTO \"%w\"",p->spec.zTableName);
 3457        }else{
 3458          sqlite3_str_appendf(p->pOut,"INSERT INTO %s",p->spec.zTableName);
 3459        }
 3460        if( p->spec.bTitles==QRF_Yes ){
 3461          for(i=0; i<p->nCol; i++){
 3462            const char *zCName = sqlite3_column_name(p->pStmt, i);
 3463            if( qrf_need_quote(zCName) ){
 3464              sqlite3_str_appendf(p->pOut, "%c\"%w\"",
 3465                                  i==0 ? '(' : ',', zCName);
 3466            }else{
 3467              sqlite3_str_appendf(p->pOut, "%c%s",
 3468                                  i==0 ? '(' : ',', zCName);
 3469            }
 3470          }
 3471          sqlite3_str_append(p->pOut, ")", 1);
 3472        }
 3473        sqlite3_str_append(p->pOut," VALUES(", 8);
 3474      }else{
 3475        sqlite3_str_append(p->pOut,",\n  (", 5);
 3476      }
 3477      for(i=0; i<p->nCol; i++){
 3478        if( i>0 ) sqlite3_str_append(p->pOut, ",", 1);
 3479        qrfRenderValue(p, p->pOut, i);
 3480      }
 3481      p->u.nIns += sqlite3_str_length(p->pOut) + 2 - szStart;
 3482      if( p->u.nIns>=mxIns ){
 3483        sqlite3_str_append(p->pOut, ");\n", 3);
 3484        p->u.nIns = 0;
 3485      }else{
 3486        sqlite3_str_append(p->pOut, ")", 1);
 3487      }
 3488      qrfWrite(p);
 3489      break;
 3490    }
 3491    case QRF_STYLE_Line: {
 3492      sqlite3_str *pVal;
 3493      int mxW;
 3494      int bWW;
 3495      int nSep;
 3496      if( p->u.sLine.azCol==0 ){
 3497        p->u.sLine.azCol = sqlite3_malloc64( p->nCol*sizeof(char*) );
 3498        if( p->u.sLine.azCol==0 ){
 3499          qrfOom(p);
 3500          break;
 3501        }
 3502        p->u.sLine.mxColWth = 0;
 3503        for(i=0; i<p->nCol; i++){
 3504          int sz;
 3505          const char *zCName = sqlite3_column_name(p->pStmt, i);
 3506          if( zCName==0 ) zCName = "unknown";
 3507          p->u.sLine.azCol[i] = sqlite3_mprintf("%s", zCName);
 3508          if( p->spec.nTitleLimit>0 ){
 3509            (void)qrfTitleLimit(p->u.sLine.azCol[i], p->spec.nTitleLimit);
 3510          }
 3511          sz = (int)sqlite3_qrf_wcswidth(p->u.sLine.azCol[i]);
 3512          if( sz > p->u.sLine.mxColWth ) p->u.sLine.mxColWth = sz;
 3513        }
 3514      }
 3515      if( p->nRow ) sqlite3_str_append(p->pOut, "\n", 1);
 3516      pVal = sqlite3_str_new(p->db);
 3517      nSep = (int)strlen(p->spec.zColumnSep);
 3518      mxW = p->mxWidth - (nSep + p->u.sLine.mxColWth);
 3519      bWW = p->spec.bWordWrap==QRF_Yes;
 3520      for(i=0; i<p->nCol; i++){
 3521        const char *zVal;
 3522        int cnt = 0;
 3523        qrfWidthPrint(p, p->pOut, -p->u.sLine.mxColWth, p->u.sLine.azCol[i]);
 3524        sqlite3_str_append(p->pOut, p->spec.zColumnSep, nSep);
 3525        qrfRenderValue(p, pVal, i);
 3526        zVal = sqlite3_str_value(pVal);
 3527        if( zVal==0 ) zVal = "";
 3528        do{
 3529          int nThis, nWide, iNext;
 3530          qrfWrapLine(zVal, mxW, bWW, &nThis, &nWide, &iNext);
 3531          if( cnt ){
 3532            sqlite3_str_appendchar(p->pOut,p->u.sLine.mxColWth+nSep,' ');
 3533          }
 3534          cnt++;
 3535          if( cnt>p->mxHeight ){
 3536            zVal = "...";
 3537            nThis = iNext = 3;
 3538          }
 3539          sqlite3_str_append(p->pOut, zVal, nThis);
 3540          sqlite3_str_append(p->pOut, "\n", 1);
 3541          zVal += iNext;
 3542        }while( zVal[0] );
 3543        sqlite3_str_reset(pVal);
 3544      }
 3545      qrfStrErr(p, pVal);
 3546      sqlite3_free(sqlite3_str_finish(pVal));
 3547      qrfWrite(p);
 3548      break;
 3549    }
 3550    case QRF_STYLE_Eqp: {
 3551      const char *zEqpLine = (const char*)sqlite3_column_text(p->pStmt,3);
 3552      int iEqpId = sqlite3_column_int(p->pStmt, 0);
 3553      int iParentId = sqlite3_column_int(p->pStmt, 1);
 3554      if( zEqpLine==0 ) zEqpLine = "";
 3555      if( zEqpLine[0]=='-' ) qrfEqpRender(p, 0);
 3556      qrfEqpAppend(p, iEqpId, iParentId, zEqpLine);
 3557      break;
 3558    }
 3559    default: {  /* QRF_STYLE_List */
 3560      if( p->nRow==0 && p->spec.bTitles==QRF_Yes ){
 3561        int saved_eText = p->spec.eText;
 3562        p->spec.eText = p->spec.eTitle;
 3563        for(i=0; i<p->nCol; i++){
 3564          const char *zCName = sqlite3_column_name(p->pStmt, i);
 3565          if( i>0 ) sqlite3_str_appendall(p->pOut, p->spec.zColumnSep);
 3566          qrfEncodeText(p, p->pOut, zCName);
 3567        }
 3568        sqlite3_str_appendall(p->pOut, p->spec.zRowSep);
 3569        qrfWrite(p);
 3570        p->spec.eText = saved_eText;
 3571      }
 3572      for(i=0; i<p->nCol; i++){
 3573        if( i>0 ) sqlite3_str_appendall(p->pOut, p->spec.zColumnSep);
 3574        qrfRenderValue(p, p->pOut, i);
 3575      }
 3576      sqlite3_str_appendall(p->pOut, p->spec.zRowSep);
 3577      qrfWrite(p);
 3578      break;
 3579    }
 3580  }
 3581  p->nRow++;
 3582}
 3583
 3584/*
 3585** Initialize the internal Qrf object.
 3586*/
 3587static void qrfInitialize(
 3588  Qrf *p,                        /* State object to be initialized */
 3589  sqlite3_stmt *pStmt,           /* Query whose output to be formatted */
 3590  const sqlite3_qrf_spec *pSpec, /* Format specification */
 3591  char **pzErr                   /* Write errors here */
 3592){
 3593  size_t sz;                     /* Size of pSpec[], based on pSpec->iVersion */
 3594  memset(p, 0, sizeof(*p));
 3595  p->pzErr = pzErr;
 3596  if( pSpec->iVersion>1 ){
 3597    qrfError(p, SQLITE_ERROR,
 3598       "unusable sqlite3_qrf_spec.iVersion (%d)",
 3599       pSpec->iVersion);
 3600    return;
 3601  }
 3602  p->pStmt = pStmt;
 3603  p->db = sqlite3_db_handle(pStmt);
 3604  p->pOut = sqlite3_str_new(p->db);
 3605  if( p->pOut==0 ){
 3606    qrfOom(p);
 3607    return;
 3608  }
 3609  p->iErr = SQLITE_OK;
 3610  p->nCol = sqlite3_column_count(p->pStmt);
 3611  p->nRow = 0;
 3612  sz = sizeof(sqlite3_qrf_spec);
 3613  memcpy(&p->spec, pSpec, sz);
 3614  if( p->spec.zNull==0 ) p->spec.zNull = "";
 3615  p->mxWidth = p->spec.nScreenWidth;
 3616  if( p->mxWidth<=0 ) p->mxWidth = QRF_MAX_WIDTH;
 3617  p->mxHeight = p->spec.nLineLimit;
 3618  if( p->mxHeight<=0 ) p->mxHeight = 2147483647;
 3619  if( p->spec.eStyle>QRF_STYLE_Table ) p->spec.eStyle = QRF_Auto;
 3620  if( p->spec.eEsc>QRF_ESC_Symbol ) p->spec.eEsc = QRF_Auto;
 3621  if( p->spec.eText>QRF_TEXT_Relaxed ) p->spec.eText = QRF_Auto;
 3622  if( p->spec.eTitle>QRF_TEXT_Relaxed ) p->spec.eTitle = QRF_Auto;
 3623  if( p->spec.eBlob>QRF_BLOB_Size ) p->spec.eBlob = QRF_Auto;
 3624qrf_reinit:
 3625  switch( p->spec.eStyle ){
 3626    case QRF_Auto: {
 3627      switch( sqlite3_stmt_isexplain(pStmt) ){
 3628        case 0:  p->spec.eStyle = QRF_STYLE_Box;      break;
 3629        case 1:  p->spec.eStyle = QRF_STYLE_Explain;  break;
 3630        default: p->spec.eStyle = QRF_STYLE_Eqp;      break;
 3631      }
 3632      goto qrf_reinit;
 3633    }
 3634    case QRF_STYLE_List: {
 3635      if( p->spec.zColumnSep==0 ) p->spec.zColumnSep = "|";
 3636      if( p->spec.zRowSep==0 ) p->spec.zRowSep = "\n";
 3637      break;
 3638    }
 3639    case QRF_STYLE_JObject:
 3640    case QRF_STYLE_Json: {
 3641      p->spec.eText = QRF_TEXT_Json;
 3642      p->spec.zNull = "null";
 3643      break;
 3644    }
 3645    case QRF_STYLE_Html: {
 3646      p->spec.eText = QRF_TEXT_Html;
 3647      p->spec.zNull = "null";
 3648      break;
 3649    }
 3650    case QRF_STYLE_Insert: {
 3651      p->spec.eText = QRF_TEXT_Sql;
 3652      p->spec.zNull = "NULL";
 3653      if( p->spec.zTableName==0 || p->spec.zTableName[0]==0 ){
 3654        p->spec.zTableName = "tab";
 3655      }
 3656      p->u.nIns = 0;
 3657      break;
 3658    }
 3659    case QRF_STYLE_Line: {
 3660      if( p->spec.zColumnSep==0 ){
 3661        p->spec.zColumnSep = ": ";
 3662      }
 3663      break;
 3664    }
 3665    case QRF_STYLE_Csv: {
 3666      p->spec.eStyle = QRF_STYLE_List;
 3667      p->spec.eText = QRF_TEXT_Csv;
 3668      p->spec.zColumnSep = ",";
 3669      p->spec.zRowSep = "\r\n";
 3670      p->spec.zNull = "";
 3671      break;
 3672    }
 3673    case QRF_STYLE_Quote: {
 3674      p->spec.eText = QRF_TEXT_Sql;
 3675      p->spec.zNull = "NULL";
 3676      p->spec.zColumnSep = ",";
 3677      p->spec.zRowSep = "\n";
 3678      break;
 3679    }
 3680    case QRF_STYLE_Eqp: {
 3681      int expMode = sqlite3_stmt_isexplain(p->pStmt);
 3682      if( expMode!=2 ){
 3683        sqlite3_stmt_explain(p->pStmt, 2);
 3684        p->expMode = expMode+1;
 3685      }
 3686      break;
 3687    }
 3688    case QRF_STYLE_Explain: {
 3689      int expMode = sqlite3_stmt_isexplain(p->pStmt);
 3690      if( expMode!=1 ){
 3691        sqlite3_stmt_explain(p->pStmt, 1);
 3692        p->expMode = expMode+1;
 3693      }
 3694      break;
 3695    }
 3696  }
 3697  if( p->spec.eEsc==QRF_Auto ){
 3698    p->spec.eEsc = QRF_ESC_Ascii;
 3699  }
 3700  if( p->spec.eText==QRF_Auto ){
 3701    p->spec.eText = QRF_TEXT_Plain;
 3702  }
 3703  if( p->spec.eTitle==QRF_Auto ){
 3704    switch( p->spec.eStyle ){
 3705      case QRF_STYLE_Box:
 3706      case QRF_STYLE_Column:
 3707      case QRF_STYLE_Table:
 3708        p->spec.eTitle = QRF_TEXT_Plain;
 3709        break;
 3710      default:
 3711        p->spec.eTitle = p->spec.eText;
 3712        break;
 3713    }
 3714  }
 3715  if( p->spec.eBlob==QRF_Auto ){
 3716    switch( p->spec.eText ){
 3717      case QRF_TEXT_Sql:  p->spec.eBlob = QRF_BLOB_Sql;  break;
 3718      case QRF_TEXT_Csv:  p->spec.eBlob = QRF_BLOB_Tcl;  break;
 3719      case QRF_TEXT_Tcl:  p->spec.eBlob = QRF_BLOB_Tcl;  break;
 3720      case QRF_TEXT_Json: p->spec.eBlob = QRF_BLOB_Json; break;
 3721      default:            p->spec.eBlob = QRF_BLOB_Text; break;
 3722    }
 3723  }
 3724  if( p->spec.bTitles==QRF_Auto ){
 3725    switch( p->spec.eStyle ){
 3726      case QRF_STYLE_Box:
 3727      case QRF_STYLE_Csv:
 3728      case QRF_STYLE_Column:
 3729      case QRF_STYLE_Table:
 3730      case QRF_STYLE_Markdown:
 3731        p->spec.bTitles = QRF_Yes;
 3732        break;
 3733      default:
 3734        p->spec.bTitles = QRF_No;
 3735        break;
 3736    }
 3737  }
 3738  if( p->spec.bWordWrap==QRF_Auto ){
 3739    p->spec.bWordWrap = QRF_Yes;
 3740  }
 3741  if( p->spec.bTextJsonb==QRF_Auto ){
 3742    p->spec.bTextJsonb = QRF_No;
 3743  }
 3744  if( p->spec.zColumnSep==0 ) p->spec.zColumnSep = ",";
 3745  if( p->spec.zRowSep==0 ) p->spec.zRowSep = "\n";
 3746}
 3747
 3748/*
 3749** Finish rendering the results
 3750*/
 3751static void qrfFinalize(Qrf *p){
 3752  switch( p->spec.eStyle ){
 3753    case QRF_STYLE_Count: {
 3754      sqlite3_str_appendf(p->pOut, "%lld\n", p->nRow);
 3755      break;
 3756    }
 3757    case QRF_STYLE_Json: {
 3758      if( p->nRow>0 ){
 3759        sqlite3_str_append(p->pOut, "}]\n", 3);
 3760      }
 3761      break;
 3762    }
 3763    case QRF_STYLE_JObject: {
 3764      if( p->nRow>0 ){
 3765        sqlite3_str_append(p->pOut, "}\n", 2);
 3766      }
 3767      break;
 3768    }
 3769    case QRF_STYLE_Insert: {
 3770      if( p->u.nIns ){
 3771        sqlite3_str_append(p->pOut, ";\n", 2);
 3772      }
 3773      break;
 3774    }
 3775    case QRF_STYLE_Line: {
 3776      if( p->u.sLine.azCol ){
 3777        int i;
 3778        for(i=0; i<p->nCol; i++) sqlite3_free(p->u.sLine.azCol[i]);
 3779        sqlite3_free(p->u.sLine.azCol);
 3780      }
 3781      break;
 3782    }
 3783    case QRF_STYLE_Stats:
 3784    case QRF_STYLE_StatsEst: {
 3785      i64 nCycle = 0;
 3786#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 3787      sqlite3_stmt_scanstatus_v2(p->pStmt, -1, SQLITE_SCANSTAT_NCYCLE,
 3788                                 SQLITE_SCANSTAT_COMPLEX, (void*)&nCycle);
 3789#endif
 3790      qrfEqpRender(p, nCycle);
 3791      break;
 3792    }
 3793    case QRF_STYLE_Eqp: {
 3794      qrfEqpRender(p, 0);
 3795      break;
 3796    }
 3797  }
 3798  qrfWrite(p);
 3799  qrfStrErr(p, p->pOut);
 3800  if( p->spec.pzOutput ){
 3801    if( p->spec.pzOutput[0] ){
 3802      sqlite3_int64 n, sz;
 3803      char *zCombined;
 3804      sz = strlen(p->spec.pzOutput[0]);
 3805      n = sqlite3_str_length(p->pOut);
 3806      zCombined = sqlite3_realloc64(p->spec.pzOutput[0], sz+n+1);
 3807      if( zCombined==0 ){
 3808        sqlite3_free(p->spec.pzOutput[0]);
 3809        p->spec.pzOutput[0] = 0;
 3810        qrfOom(p);
 3811      }else{
 3812        p->spec.pzOutput[0] = zCombined;
 3813        memcpy(zCombined+sz, sqlite3_str_value(p->pOut), n+1);
 3814      }
 3815      sqlite3_free(sqlite3_str_finish(p->pOut));
 3816    }else{
 3817      p->spec.pzOutput[0] = sqlite3_str_finish(p->pOut);
 3818    }
 3819  }else if( p->pOut ){
 3820    sqlite3_free(sqlite3_str_finish(p->pOut));
 3821  }
 3822  if( p->expMode>0 ){
 3823    sqlite3_stmt_explain(p->pStmt, p->expMode-1);
 3824  }
 3825  if( p->actualWidth ){
 3826    sqlite3_free(p->actualWidth);
 3827  }
 3828  if( p->pJTrans ){
 3829    sqlite3 *db = sqlite3_db_handle(p->pJTrans);
 3830    sqlite3_finalize(p->pJTrans);
 3831    sqlite3_close(db);
 3832  }
 3833}
 3834
 3835/*
 3836** Run the prepared statement pStmt and format the results according
 3837** to the specification provided in pSpec.  Return an error code.
 3838** If pzErr is not NULL and if an error occurs, write an error message
 3839** into *pzErr.
 3840*/
 3841int sqlite3_format_query_result(
 3842  sqlite3_stmt *pStmt,                 /* Statement to evaluate */
 3843  const sqlite3_qrf_spec *pSpec,       /* Format specification */
 3844  char **pzErr                         /* Write error message here */
 3845){
 3846  Qrf qrf;         /* The new Qrf being created */
 3847
 3848  if( pStmt==0 ) return SQLITE_OK;       /* No-op */
 3849  if( pSpec==0 ) return SQLITE_MISUSE;
 3850  qrfInitialize(&qrf, pStmt, pSpec, pzErr);
 3851  switch( qrf.spec.eStyle ){
 3852    case QRF_STYLE_Box:
 3853    case QRF_STYLE_Column:
 3854    case QRF_STYLE_Markdown: 
 3855    case QRF_STYLE_Table: {
 3856      /* Columnar modes require that the entire query be evaluated and the
 3857      ** results stored in memory, so that we can compute column widths */
 3858      qrfColumnar(&qrf);
 3859      break;
 3860    }
 3861    case QRF_STYLE_Explain: {
 3862      qrfExplain(&qrf);
 3863      break;
 3864    }
 3865    case QRF_STYLE_StatsVm: {
 3866      qrfScanStatusVm(&qrf);
 3867      break;
 3868    }
 3869    case QRF_STYLE_Stats:
 3870    case QRF_STYLE_StatsEst: {
 3871      qrfEqpStats(&qrf);
 3872      break;
 3873    }
 3874    default: {
 3875      /* Non-columnar modes where the output can occur after each row
 3876      ** of result is received */
 3877      while( qrf.iErr==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
 3878        qrfOneSimpleRow(&qrf);
 3879      }
 3880      break;
 3881    }
 3882  }
 3883  qrfResetStmt(&qrf);
 3884  qrfFinalize(&qrf);
 3885  return qrf.iErr;
 3886}
 3887
 3888/************************* End ext/qrf/qrf.c ********************/
 3889
 3890/* Use console I/O package as a direct INCLUDE. */
 3891#define SQLITE_INTERNAL_LINKAGE static
 3892
 3893#ifdef SQLITE_SHELL_FIDDLE
 3894/* Deselect most features from the console I/O package for Fiddle. */
 3895# define SQLITE_CIO_NO_REDIRECT
 3896# define SQLITE_CIO_NO_CLASSIFY
 3897# define SQLITE_CIO_NO_TRANSLATE
 3898# define SQLITE_CIO_NO_SETMODE
 3899# define SQLITE_CIO_NO_FLUSH
 3900#endif
 3901
 3902/*
 3903** The source code for several run-time loadable extensions is inserted
 3904** below by the ../tool/mkshellc.tcl script.  Before processing that included
 3905** code, we need to override some macros to make the included program code
 3906** work here in the middle of this regular program.
 3907*/
 3908#define SQLITE_EXTENSION_INIT1
 3909#define SQLITE_EXTENSION_INIT2(X) (void)(X)
 3910
 3911/************************* Begin ext/misc/windirent.h ******************/
 3912/*
 3913** 2025-06-05
 3914**
 3915** The author disclaims copyright to this source code.  In place of
 3916** a legal notice, here is a blessing:
 3917**
 3918**    May you do good and not evil.
 3919**    May you find forgiveness for yourself and forgive others.
 3920**    May you share freely, never taking more than you give.
 3921**
 3922*************************************************************************
 3923**
 3924** An implementation of opendir(), readdir(), and closedir() for Windows,
 3925** based on the FindFirstFile(), FindNextFile(), and FindClose() APIs
 3926** of Win32.
 3927**
 3928** #include this file inside any C-code module that needs to use
 3929** opendir()/readdir()/closedir().  This file is a no-op on non-Windows
 3930** machines.  On Windows, static functions are defined that implement
 3931** those standard interfaces.
 3932*/
 3933#if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
 3934#define SQLITE_WINDIRENT_H
 3935
 3936#ifndef WIN32_LEAN_AND_MEAN
 3937#define WIN32_LEAN_AND_MEAN
 3938#endif
 3939#include <windows.h>
 3940#include <io.h>
 3941#include <stdio.h>
 3942#include <stdlib.h>
 3943#include <errno.h>
 3944#include <limits.h>
 3945#include <sys/types.h>
 3946#include <sys/stat.h>
 3947#include <string.h>
 3948#ifndef FILENAME_MAX
 3949# define FILENAME_MAX (260)
 3950#endif
 3951#ifndef S_ISREG
 3952#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
 3953#endif
 3954#ifndef S_ISDIR
 3955#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
 3956#endif
 3957#ifndef S_ISLNK
 3958#define S_ISLNK(m) (0)
 3959#endif
 3960typedef unsigned short mode_t;
 3961
 3962/* The dirent object for Windows is abbreviated.  The only field really
 3963** usable by applications is d_name[].
 3964*/
 3965struct dirent {
 3966 int d_ino;                  /* Inode number (synthesized) */
 3967 unsigned d_attributes;      /* File attributes */
 3968 char d_name[FILENAME_MAX];  /* Null-terminated filename */
 3969};
 3970
 3971/* The internals of DIR are opaque according to standards.  So it
 3972** does not matter what we put here. */
 3973typedef struct DIR DIR;
 3974struct DIR {
 3975  intptr_t d_handle;         /* Handle for findfirst()/findnext() */
 3976  struct dirent cur;         /* Current entry */
 3977};
 3978
 3979/* Ignore hidden and system files */
 3980#define WindowsFileToIgnore(a) \
 3981    ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
 3982
 3983/*
 3984** Close a previously opened directory
 3985*/
 3986static int closedir(DIR *pDir){
 3987  int rc = 0;
 3988  if( pDir==0 ){
 3989    return EINVAL;
 3990  }
 3991  if( pDir->d_handle!=0 && pDir->d_handle!=(-1) ){
 3992    rc = _findclose(pDir->d_handle);
 3993  }
 3994  sqlite3_free(pDir);
 3995  return rc;
 3996}
 3997
 3998/*
 3999** Open a new directory.  The directory name should be UTF-8 encoded.
 4000** appropriate translations happen automatically.
 4001*/
 4002static DIR *opendir(const char *zDirName){
 4003  DIR *pDir;
 4004  wchar_t *b1;
 4005  sqlite3_int64 sz;
 4006  struct _wfinddata_t data;
 4007
 4008  pDir = sqlite3_malloc64( sizeof(DIR) );
 4009  if( pDir==0 ) return 0;
 4010  memset(pDir, 0, sizeof(DIR));
 4011  memset(&data, 0, sizeof(data));
 4012  sz = strlen(zDirName);
 4013  b1 = sqlite3_malloc64( (sz+3)*sizeof(b1[0]) );
 4014  if( b1==0 ){
 4015    closedir(pDir);
 4016    return NULL;
 4017  }
 4018  sz = MultiByteToWideChar(CP_UTF8, 0, zDirName, sz, b1, sz);
 4019  b1[sz++] = '\\';
 4020  b1[sz++] = '*';
 4021  b1[sz] = 0;
 4022  if( sz+1>sizeof(data.name)/sizeof(data.name[0]) ){
 4023    closedir(pDir);
 4024    sqlite3_free(b1);
 4025    return NULL;
 4026  }
 4027  memcpy(data.name, b1, (sz+1)*sizeof(b1[0]));
 4028  sqlite3_free(b1);
 4029  pDir->d_handle = _wfindfirst(data.name, &data);
 4030  if( pDir->d_handle<0 ){
 4031    closedir(pDir);
 4032    return NULL;
 4033  }
 4034  while( WindowsFileToIgnore(data) ){
 4035    memset(&data, 0, sizeof(data));
 4036    if( _wfindnext(pDir->d_handle, &data)==-1 ){
 4037      closedir(pDir);
 4038      return NULL;
 4039    }
 4040  }
 4041  pDir->cur.d_ino = 0;
 4042  pDir->cur.d_attributes = data.attrib;
 4043  WideCharToMultiByte(CP_UTF8, 0, data.name, -1,
 4044                      pDir->cur.d_name, FILENAME_MAX, 0, 0);
 4045  return pDir;
 4046}
 4047
 4048/*
 4049** Read the next entry from a directory.
 4050**
 4051** The returned struct-dirent object is managed by DIR.  It is only
 4052** valid until the next readdir() or closedir() call.  Only the
 4053** d_name[] field is meaningful.  The d_name[] value has been
 4054** translated into UTF8.
 4055*/
 4056static struct dirent *readdir(DIR *pDir){
 4057  struct _wfinddata_t data;
 4058  if( pDir==0 ) return 0;
 4059  if( (pDir->cur.d_ino++)==0 ){
 4060    return &pDir->cur;
 4061  }
 4062  do{
 4063    memset(&data, 0, sizeof(data));
 4064    if( _wfindnext(pDir->d_handle, &data)==-1 ){
 4065      return NULL;
 4066    }
 4067  }while( WindowsFileToIgnore(data) );
 4068  pDir->cur.d_attributes = data.attrib;
 4069  WideCharToMultiByte(CP_UTF8, 0, data.name, -1,
 4070                      pDir->cur.d_name, FILENAME_MAX, 0, 0);
 4071  return &pDir->cur;
 4072}
 4073
 4074#endif /* defined(_WIN32) && defined(_MSC_VER) */
 4075
 4076/************************* End ext/misc/windirent.h ********************/
 4077/************************* Begin ext/misc/memtrace.c ******************/
 4078/*
 4079** 2019-01-21
 4080**
 4081** The author disclaims copyright to this source code.  In place of
 4082** a legal notice, here is a blessing:
 4083**
 4084**    May you do good and not evil.
 4085**    May you find forgiveness for yourself and forgive others.
 4086**    May you share freely, never taking more than you give.
 4087**
 4088*************************************************************************
 4089**
 4090** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
 4091** mechanism to add a tracing layer on top of SQLite.  If this extension
 4092** is registered prior to sqlite3_initialize(), it will cause all memory
 4093** allocation activities to be logged on standard output, or to some other
 4094** FILE specified by the initializer.
 4095**
 4096** This file needs to be compiled into the application that uses it.
 4097**
 4098** This extension is used to implement the --memtrace option of the
 4099** command-line shell.
 4100*/
 4101#include <assert.h>
 4102#include <string.h>
 4103#include <stdio.h>
 4104
 4105/* The original memory allocation routines */
 4106static sqlite3_mem_methods memtraceBase;
 4107static FILE *memtraceOut;
 4108
 4109/* Methods that trace memory allocations */
 4110static void *memtraceMalloc(int n){
 4111  if( memtraceOut ){
 4112    fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n", 
 4113            memtraceBase.xRoundup(n));
 4114  }
 4115  return memtraceBase.xMalloc(n);
 4116}
 4117static void memtraceFree(void *p){
 4118  if( p==0 ) return;
 4119  if( memtraceOut ){
 4120    fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
 4121  }
 4122  memtraceBase.xFree(p);
 4123}
 4124static void *memtraceRealloc(void *p, int n){
 4125  if( p==0 ) return memtraceMalloc(n);
 4126  if( n==0 ){
 4127    memtraceFree(p);
 4128    return 0;
 4129  }
 4130  if( memtraceOut ){
 4131    fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
 4132            memtraceBase.xSize(p), memtraceBase.xRoundup(n));
 4133  }
 4134  return memtraceBase.xRealloc(p, n);
 4135}
 4136static int memtraceSize(void *p){
 4137  return memtraceBase.xSize(p);
 4138}
 4139static int memtraceRoundup(int n){
 4140  return memtraceBase.xRoundup(n);
 4141}
 4142static int memtraceInit(void *p){
 4143  return memtraceBase.xInit(p);
 4144}
 4145static void memtraceShutdown(void *p){
 4146  memtraceBase.xShutdown(p);
 4147}
 4148
 4149/* The substitute memory allocator */
 4150static sqlite3_mem_methods ersaztMethods = {
 4151  memtraceMalloc,
 4152  memtraceFree,
 4153  memtraceRealloc,
 4154  memtraceSize,
 4155  memtraceRoundup,
 4156  memtraceInit,
 4157  memtraceShutdown,
 4158  0
 4159};
 4160
 4161/* Begin tracing memory allocations to out. */
 4162int sqlite3MemTraceActivate(FILE *out){
 4163  int rc = SQLITE_OK;
 4164  if( memtraceBase.xMalloc==0 ){
 4165    rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
 4166    if( rc==SQLITE_OK ){
 4167      rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
 4168    }
 4169  }
 4170  memtraceOut = out;
 4171  return rc;
 4172}
 4173
 4174/* Deactivate memory tracing */
 4175int sqlite3MemTraceDeactivate(void){
 4176  int rc = SQLITE_OK;
 4177  if( memtraceBase.xMalloc!=0 ){
 4178    rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
 4179    if( rc==SQLITE_OK ){
 4180      memset(&memtraceBase, 0, sizeof(memtraceBase));
 4181    }
 4182  }
 4183  memtraceOut = 0;
 4184  return rc;
 4185}
 4186
 4187/************************* End ext/misc/memtrace.c ********************/
 4188/************************* Begin ext/misc/pcachetrace.c ******************/
 4189/*
 4190** 2023-06-21
 4191**
 4192** The author disclaims copyright to this source code.  In place of
 4193** a legal notice, here is a blessing:
 4194**
 4195**    May you do good and not evil.
 4196**    May you find forgiveness for yourself and forgive others.
 4197**    May you share freely, never taking more than you give.
 4198**
 4199*************************************************************************
 4200**
 4201** This file implements an extension that uses the SQLITE_CONFIG_PCACHE2
 4202** mechanism to add a tracing layer on top of pluggable page cache of
 4203** SQLite.  If this extension is registered prior to sqlite3_initialize(),
 4204** it will cause all page cache activities to be logged on standard output,
 4205** or to some other FILE specified by the initializer.
 4206**
 4207** This file needs to be compiled into the application that uses it.
 4208**
 4209** This extension is used to implement the --pcachetrace option of the
 4210** command-line shell.
 4211*/
 4212#include <assert.h>
 4213#include <string.h>
 4214#include <stdio.h>
 4215
 4216/* The original page cache routines */
 4217static sqlite3_pcache_methods2 pcacheBase;
 4218static FILE *pcachetraceOut;
 4219
 4220/* Methods that trace pcache activity */
 4221static int pcachetraceInit(void *pArg){
 4222  int nRes;
 4223  if( pcachetraceOut ){
 4224    fprintf(pcachetraceOut, "PCACHETRACE: xInit(%p)\n", pArg);
 4225  }
 4226  nRes = pcacheBase.xInit(pArg);
 4227  if( pcachetraceOut ){
 4228    fprintf(pcachetraceOut, "PCACHETRACE: xInit(%p) -> %d\n", pArg, nRes);
 4229  }
 4230  return nRes;
 4231}
 4232static void pcachetraceShutdown(void *pArg){
 4233  if( pcachetraceOut ){
 4234    fprintf(pcachetraceOut, "PCACHETRACE: xShutdown(%p)\n", pArg);
 4235  }
 4236  pcacheBase.xShutdown(pArg);
 4237}
 4238static sqlite3_pcache *pcachetraceCreate(int szPage, int szExtra, int bPurge){
 4239  sqlite3_pcache *pRes;
 4240  if( pcachetraceOut ){
 4241    fprintf(pcachetraceOut, "PCACHETRACE: xCreate(%d,%d,%d)\n",
 4242            szPage, szExtra, bPurge);
 4243  }
 4244  pRes = pcacheBase.xCreate(szPage, szExtra, bPurge);
 4245  if( pcachetraceOut ){
 4246    fprintf(pcachetraceOut, "PCACHETRACE: xCreate(%d,%d,%d) -> %p\n",
 4247            szPage, szExtra, bPurge, pRes);
 4248  }
 4249  return pRes;
 4250}
 4251static void pcachetraceCachesize(sqlite3_pcache *p, int nCachesize){
 4252  if( pcachetraceOut ){
 4253    fprintf(pcachetraceOut, "PCACHETRACE: xCachesize(%p, %d)\n", p, nCachesize);
 4254  }
 4255  pcacheBase.xCachesize(p, nCachesize);
 4256}
 4257static int pcachetracePagecount(sqlite3_pcache *p){
 4258  int nRes;
 4259  if( pcachetraceOut ){
 4260    fprintf(pcachetraceOut, "PCACHETRACE: xPagecount(%p)\n", p);
 4261  }
 4262  nRes = pcacheBase.xPagecount(p);
 4263  if( pcachetraceOut ){
 4264    fprintf(pcachetraceOut, "PCACHETRACE: xPagecount(%p) -> %d\n", p, nRes);
 4265  }
 4266  return nRes;
 4267}
 4268static sqlite3_pcache_page *pcachetraceFetch(
 4269  sqlite3_pcache *p,
 4270  unsigned key,
 4271  int crFg
 4272){
 4273  sqlite3_pcache_page *pRes;
 4274  if( pcachetraceOut ){
 4275    fprintf(pcachetraceOut, "PCACHETRACE: xFetch(%p,%u,%d)\n", p, key, crFg);
 4276  }
 4277  pRes = pcacheBase.xFetch(p, key, crFg);
 4278  if( pcachetraceOut ){
 4279    fprintf(pcachetraceOut, "PCACHETRACE: xFetch(%p,%u,%d) -> %p\n",
 4280            p, key, crFg, pRes);
 4281  }
 4282  return pRes;
 4283}
 4284static void pcachetraceUnpin(
 4285  sqlite3_pcache *p,
 4286  sqlite3_pcache_page *pPg,
 4287  int bDiscard
 4288){
 4289  if( pcachetraceOut ){
 4290    fprintf(pcachetraceOut, "PCACHETRACE: xUnpin(%p, %p, %d)\n",
 4291            p, pPg, bDiscard);
 4292  }
 4293  pcacheBase.xUnpin(p, pPg, bDiscard);
 4294}
 4295static void pcachetraceRekey(
 4296  sqlite3_pcache *p,
 4297  sqlite3_pcache_page *pPg,
 4298  unsigned oldKey,
 4299  unsigned newKey
 4300){
 4301  if( pcachetraceOut ){
 4302    fprintf(pcachetraceOut, "PCACHETRACE: xRekey(%p, %p, %u, %u)\n",
 4303        p, pPg, oldKey, newKey);
 4304  }
 4305  pcacheBase.xRekey(p, pPg, oldKey, newKey);
 4306}
 4307static void pcachetraceTruncate(sqlite3_pcache *p, unsigned n){
 4308  if( pcachetraceOut ){
 4309    fprintf(pcachetraceOut, "PCACHETRACE: xTruncate(%p, %u)\n", p, n);
 4310  }
 4311  pcacheBase.xTruncate(p, n);
 4312}
 4313static void pcachetraceDestroy(sqlite3_pcache *p){
 4314  if( pcachetraceOut ){
 4315    fprintf(pcachetraceOut, "PCACHETRACE: xDestroy(%p)\n", p);
 4316  }
 4317  pcacheBase.xDestroy(p);
 4318}
 4319static void pcachetraceShrink(sqlite3_pcache *p){
 4320  if( pcachetraceOut ){
 4321    fprintf(pcachetraceOut, "PCACHETRACE: xShrink(%p)\n", p);
 4322  }
 4323  pcacheBase.xShrink(p);
 4324}
 4325
 4326/* The substitute pcache methods */
 4327static sqlite3_pcache_methods2 ersaztPcacheMethods = {
 4328  0,
 4329  0,
 4330  pcachetraceInit,
 4331  pcachetraceShutdown,
 4332  pcachetraceCreate,
 4333  pcachetraceCachesize,
 4334  pcachetracePagecount,
 4335  pcachetraceFetch,
 4336  pcachetraceUnpin,
 4337  pcachetraceRekey,
 4338  pcachetraceTruncate,
 4339  pcachetraceDestroy,
 4340  pcachetraceShrink
 4341};
 4342
 4343/* Begin tracing memory allocations to out. */
 4344int sqlite3PcacheTraceActivate(FILE *out){
 4345  int rc = SQLITE_OK;
 4346  if( pcacheBase.xFetch==0 ){
 4347    rc = sqlite3_config(SQLITE_CONFIG_GETPCACHE2, &pcacheBase);
 4348    if( rc==SQLITE_OK ){
 4349      rc = sqlite3_config(SQLITE_CONFIG_PCACHE2, &ersaztPcacheMethods);
 4350    }
 4351  }
 4352  pcachetraceOut = out;
 4353  return rc;
 4354}
 4355
 4356/* Deactivate memory tracing */
 4357int sqlite3PcacheTraceDeactivate(void){
 4358  int rc = SQLITE_OK;
 4359  if( pcacheBase.xFetch!=0 ){
 4360    rc = sqlite3_config(SQLITE_CONFIG_PCACHE2, &pcacheBase);
 4361    if( rc==SQLITE_OK ){
 4362      memset(&pcacheBase, 0, sizeof(pcacheBase));
 4363    }
 4364  }
 4365  pcachetraceOut = 0;
 4366  return rc;
 4367}
 4368
 4369/************************* End ext/misc/pcachetrace.c ********************/
 4370/************************* Begin ext/misc/shathree.c ******************/
 4371/*
 4372** 2017-03-08
 4373**
 4374** The author disclaims copyright to this source code.  In place of
 4375** a legal notice, here is a blessing:
 4376**
 4377**    May you do good and not evil.
 4378**    May you find forgiveness for yourself and forgive others.
 4379**    May you share freely, never taking more than you give.
 4380**
 4381******************************************************************************
 4382**
 4383** This SQLite extension implements functions that compute SHA3 hashes
 4384** in the way described by the (U.S.) NIST FIPS 202 SHA-3 Standard.
 4385** Three SQL functions are implemented:
 4386**
 4387**     sha3(X,SIZE)
 4388**     sha3_agg(Y,SIZE)
 4389**     sha3_query(Z,SIZE)
 4390**
 4391** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
 4392** X is NULL.  If inputs X is text, the UTF-8 rendering of that text is
 4393** used to compute the hash.  If X is a BLOB, then the binary data of the
 4394** blob is used to compute the hash.  If X is an integer or real number,
 4395** then that number if converted into UTF-8 text and the hash is computed
 4396** over the text.
 4397**
 4398** The sha3_agg(Y) function computes the SHA3 hash of all Y inputs.  Since
 4399** order is important for the hash, it is recommended that the Y expression
 4400** by followed by an ORDER BY clause to guarantee that the inputs occur
 4401** in the desired order.
 4402**
 4403** The sha3_query(Y) function evaluates all queries in the SQL statements of Y
 4404** and returns a hash of their results.
 4405**
 4406** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
 4407** is used.  If SIZE is included it must be one of the integers 224, 256,
 4408** 384, or 512, to determine SHA3 hash variant that is computed.
 4409**
 4410** Because the sha3_agg() and sha3_query() functions compute a hash over
 4411** multiple values, the values are encode to use include type information.
 4412**
 4413** In sha3_agg(), the sequence of bytes that gets hashed for each input
 4414** Y depends on the datatype of Y:
 4415**
 4416**    typeof(Y)='null'         A single "N" is hashed.  (One byte)
 4417**
 4418**    typeof(Y)='integer'      The data hash is the character "I" followed
 4419**                             by an 8-byte big-endian binary of the
 4420**                             64-bit signed integer.  (Nine bytes total.)
 4421**
 4422**    typeof(Y)='real'         The character "F" followed by an 8-byte
 4423**                             big-ending binary of the double.  (Nine
 4424**                             bytes total.)
 4425**
 4426**    typeof(Y)='text'         The hash is over prefix "Tnnn:" followed
 4427**                             by the UTF8 encoding of the text.  The "nnn"
 4428**                             in the prefix is the minimum-length decimal
 4429**                             representation of the octet_length of the text.
 4430**                             Notice the ":" at the end of the prefix, which
 4431**                             is needed to separate the prefix from the
 4432**                             content in cases where the content starts
 4433**                             with a digit.
 4434**
 4435**    typeof(Y)='blob'         The hash is taken over prefix "Bnnn:" followed
 4436**                             by the binary content of the blob.  The "nnn"
 4437**                             in the prefix is the minimum-length decimal
 4438**                             representation of the byte-length of the blob.
 4439**
 4440** According to the rules above, all of the following SELECT statements
 4441** should return TRUE:
 4442**
 4443**    SELECT sha3(1) = sha3('1');
 4444**
 4445**    SELECT sha3('hello') = sha3(x'68656c6c6f');
 4446**
 4447**    WITH a(x) AS (VALUES('xyzzy'))
 4448**      SELECT sha3_agg(x) = sha3('T5:xyzzy')            FROM a;
 4449**
 4450**    WITH a(x) AS (VALUES(x'010203'))
 4451**      SELECT sha3_agg(x) = sha3(x'42333a010203')       FROM a;
 4452**
 4453**    WITH a(x) AS (VALUES(0x123456))
 4454**      SELECT sha3_agg(x) = sha3(x'490000000000123456') FROM a;
 4455**
 4456**    WITH a(x) AS (VALUES(100.015625))
 4457**      SELECT sha3_agg(x) = sha3(x'464059010000000000') FROM a;
 4458**
 4459**    WITH a(x) AS (VALUES(NULL))
 4460**      SELECT sha3_agg(x) = sha3('N') FROM a;
 4461**
 4462**
 4463** In sha3_query(), individual column values are encoded as with
 4464** sha3_agg(), but with the addition that a single "R" character is
 4465** inserted at the start of each row.
 4466**
 4467** Note that sha3_agg() hashes rows for which Y is NULL.  Add a FILTER
 4468** clause if NULL rows should be excluded:
 4469**
 4470**    SELECT sha3_agg(x ORDER BY rowid) FILTER(WHERE x NOT NULL) FROM t1;
 4471*/
 4472/* #include "sqlite3ext.h" */
 4473SQLITE_EXTENSION_INIT1
 4474#include <assert.h>
 4475#include <string.h>
 4476#include <stdarg.h>
 4477
 4478#ifndef SQLITE_AMALGAMATION
 4479/* typedef sqlite3_uint64 u64; */
 4480#endif /* SQLITE_AMALGAMATION */
 4481
 4482/******************************************************************************
 4483** The Hash Engine
 4484*/
 4485/*
 4486** Macros to determine whether the machine is big or little endian,
 4487** and whether or not that determination is run-time or compile-time.
 4488**
 4489** For best performance, an attempt is made to guess at the byte-order
 4490** using C-preprocessor macros.  If that is unsuccessful, or if
 4491** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
 4492** at run-time.
 4493*/
 4494#ifndef SHA3_BYTEORDER
 4495# if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
 4496     defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
 4497     defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
 4498     defined(__arm__)
 4499#   define SHA3_BYTEORDER    1234
 4500# elif defined(sparc)    || defined(__ppc__)
 4501#   define SHA3_BYTEORDER    4321
 4502# else
 4503#   define SHA3_BYTEORDER 0
 4504# endif
 4505#endif
 4506
 4507
 4508/*
 4509** State structure for a SHA3 hash in progress
 4510*/
 4511typedef struct SHA3Context SHA3Context;
 4512struct SHA3Context {
 4513  union {
 4514    u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
 4515    unsigned char x[1600];    /* ... or 1600 bytes */
 4516  } u;
 4517  unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
 4518  unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
 4519  unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
 4520  unsigned iSize;        /* 224, 256, 358, or 512 */
 4521};
 4522
 4523/*
 4524** A single step of the Keccak mixing function for a 1600-bit state
 4525*/
 4526static void KeccakF1600Step(SHA3Context *p){
 4527  int i;
 4528  u64 b0, b1, b2, b3, b4;
 4529  u64 c0, c1, c2, c3, c4;
 4530  u64 d0, d1, d2, d3, d4;
 4531  static const u64 RC[] = {
 4532    0x0000000000000001ULL,  0x0000000000008082ULL,
 4533    0x800000000000808aULL,  0x8000000080008000ULL,
 4534    0x000000000000808bULL,  0x0000000080000001ULL,
 4535    0x8000000080008081ULL,  0x8000000000008009ULL,
 4536    0x000000000000008aULL,  0x0000000000000088ULL,
 4537    0x0000000080008009ULL,  0x000000008000000aULL,
 4538    0x000000008000808bULL,  0x800000000000008bULL,
 4539    0x8000000000008089ULL,  0x8000000000008003ULL,
 4540    0x8000000000008002ULL,  0x8000000000000080ULL,
 4541    0x000000000000800aULL,  0x800000008000000aULL,
 4542    0x8000000080008081ULL,  0x8000000000008080ULL,
 4543    0x0000000080000001ULL,  0x8000000080008008ULL
 4544  };
 4545# define a00 (p->u.s[0])
 4546# define a01 (p->u.s[1])
 4547# define a02 (p->u.s[2])
 4548# define a03 (p->u.s[3])
 4549# define a04 (p->u.s[4])
 4550# define a10 (p->u.s[5])
 4551# define a11 (p->u.s[6])
 4552# define a12 (p->u.s[7])
 4553# define a13 (p->u.s[8])
 4554# define a14 (p->u.s[9])
 4555# define a20 (p->u.s[10])
 4556# define a21 (p->u.s[11])
 4557# define a22 (p->u.s[12])
 4558# define a23 (p->u.s[13])
 4559# define a24 (p->u.s[14])
 4560# define a30 (p->u.s[15])
 4561# define a31 (p->u.s[16])
 4562# define a32 (p->u.s[17])
 4563# define a33 (p->u.s[18])
 4564# define a34 (p->u.s[19])
 4565# define a40 (p->u.s[20])
 4566# define a41 (p->u.s[21])
 4567# define a42 (p->u.s[22])
 4568# define a43 (p->u.s[23])
 4569# define a44 (p->u.s[24])
 4570# define ROL64(a,x) ((a<<x)|(a>>(64-x)))
 4571
 4572  for(i=0; i<24; i+=4){
 4573    c0 = a00^a10^a20^a30^a40;
 4574    c1 = a01^a11^a21^a31^a41;
 4575    c2 = a02^a12^a22^a32^a42;
 4576    c3 = a03^a13^a23^a33^a43;
 4577    c4 = a04^a14^a24^a34^a44;
 4578    d0 = c4^ROL64(c1, 1);
 4579    d1 = c0^ROL64(c2, 1);
 4580    d2 = c1^ROL64(c3, 1);
 4581    d3 = c2^ROL64(c4, 1);
 4582    d4 = c3^ROL64(c0, 1);
 4583
 4584    b0 = (a00^d0);
 4585    b1 = ROL64((a11^d1), 44);
 4586    b2 = ROL64((a22^d2), 43);
 4587    b3 = ROL64((a33^d3), 21);
 4588    b4 = ROL64((a44^d4), 14);
 4589    a00 =   b0 ^((~b1)&  b2 );
 4590    a00 ^= RC[i];
 4591    a11 =   b1 ^((~b2)&  b3 );
 4592    a22 =   b2 ^((~b3)&  b4 );
 4593    a33 =   b3 ^((~b4)&  b0 );
 4594    a44 =   b4 ^((~b0)&  b1 );
 4595
 4596    b2 = ROL64((a20^d0), 3);
 4597    b3 = ROL64((a31^d1), 45);
 4598    b4 = ROL64((a42^d2), 61);
 4599    b0 = ROL64((a03^d3), 28);
 4600    b1 = ROL64((a14^d4), 20);
 4601    a20 =   b0 ^((~b1)&  b2 );
 4602    a31 =   b1 ^((~b2)&  b3 );
 4603    a42 =   b2 ^((~b3)&  b4 );
 4604    a03 =   b3 ^((~b4)&  b0 );
 4605    a14 =   b4 ^((~b0)&  b1 );
 4606
 4607    b4 = ROL64((a40^d0), 18);
 4608    b0 = ROL64((a01^d1), 1);
 4609    b1 = ROL64((a12^d2), 6);
 4610    b2 = ROL64((a23^d3), 25);
 4611    b3 = ROL64((a34^d4), 8);
 4612    a40 =   b0 ^((~b1)&  b2 );
 4613    a01 =   b1 ^((~b2)&  b3 );
 4614    a12 =   b2 ^((~b3)&  b4 );
 4615    a23 =   b3 ^((~b4)&  b0 );
 4616    a34 =   b4 ^((~b0)&  b1 );
 4617
 4618    b1 = ROL64((a10^d0), 36);
 4619    b2 = ROL64((a21^d1), 10);
 4620    b3 = ROL64((a32^d2), 15);
 4621    b4 = ROL64((a43^d3), 56);
 4622    b0 = ROL64((a04^d4), 27);
 4623    a10 =   b0 ^((~b1)&  b2 );
 4624    a21 =   b1 ^((~b2)&  b3 );
 4625    a32 =   b2 ^((~b3)&  b4 );
 4626    a43 =   b3 ^((~b4)&  b0 );
 4627    a04 =   b4 ^((~b0)&  b1 );
 4628
 4629    b3 = ROL64((a30^d0), 41);
 4630    b4 = ROL64((a41^d1), 2);
 4631    b0 = ROL64((a02^d2), 62);
 4632    b1 = ROL64((a13^d3), 55);
 4633    b2 = ROL64((a24^d4), 39);
 4634    a30 =   b0 ^((~b1)&  b2 );
 4635    a41 =   b1 ^((~b2)&  b3 );
 4636    a02 =   b2 ^((~b3)&  b4 );
 4637    a13 =   b3 ^((~b4)&  b0 );
 4638    a24 =   b4 ^((~b0)&  b1 );
 4639
 4640    c0 = a00^a20^a40^a10^a30;
 4641    c1 = a11^a31^a01^a21^a41;
 4642    c2 = a22^a42^a12^a32^a02;
 4643    c3 = a33^a03^a23^a43^a13;
 4644    c4 = a44^a14^a34^a04^a24;
 4645    d0 = c4^ROL64(c1, 1);
 4646    d1 = c0^ROL64(c2, 1);
 4647    d2 = c1^ROL64(c3, 1);
 4648    d3 = c2^ROL64(c4, 1);
 4649    d4 = c3^ROL64(c0, 1);
 4650
 4651    b0 = (a00^d0);
 4652    b1 = ROL64((a31^d1), 44);
 4653    b2 = ROL64((a12^d2), 43);
 4654    b3 = ROL64((a43^d3), 21);
 4655    b4 = ROL64((a24^d4), 14);
 4656    a00 =   b0 ^((~b1)&  b2 );
 4657    a00 ^= RC[i+1];
 4658    a31 =   b1 ^((~b2)&  b3 );
 4659    a12 =   b2 ^((~b3)&  b4 );
 4660    a43 =   b3 ^((~b4)&  b0 );
 4661    a24 =   b4 ^((~b0)&  b1 );
 4662
 4663    b2 = ROL64((a40^d0), 3);
 4664    b3 = ROL64((a21^d1), 45);
 4665    b4 = ROL64((a02^d2), 61);
 4666    b0 = ROL64((a33^d3), 28);
 4667    b1 = ROL64((a14^d4), 20);
 4668    a40 =   b0 ^((~b1)&  b2 );
 4669    a21 =   b1 ^((~b2)&  b3 );
 4670    a02 =   b2 ^((~b3)&  b4 );
 4671    a33 =   b3 ^((~b4)&  b0 );
 4672    a14 =   b4 ^((~b0)&  b1 );
 4673
 4674    b4 = ROL64((a30^d0), 18);
 4675    b0 = ROL64((a11^d1), 1);
 4676    b1 = ROL64((a42^d2), 6);
 4677    b2 = ROL64((a23^d3), 25);
 4678    b3 = ROL64((a04^d4), 8);
 4679    a30 =   b0 ^((~b1)&  b2 );
 4680    a11 =   b1 ^((~b2)&  b3 );
 4681    a42 =   b2 ^((~b3)&  b4 );
 4682    a23 =   b3 ^((~b4)&  b0 );
 4683    a04 =   b4 ^((~b0)&  b1 );
 4684
 4685    b1 = ROL64((a20^d0), 36);
 4686    b2 = ROL64((a01^d1), 10);
 4687    b3 = ROL64((a32^d2), 15);
 4688    b4 = ROL64((a13^d3), 56);
 4689    b0 = ROL64((a44^d4), 27);
 4690    a20 =   b0 ^((~b1)&  b2 );
 4691    a01 =   b1 ^((~b2)&  b3 );
 4692    a32 =   b2 ^((~b3)&  b4 );
 4693    a13 =   b3 ^((~b4)&  b0 );
 4694    a44 =   b4 ^((~b0)&  b1 );
 4695
 4696    b3 = ROL64((a10^d0), 41);
 4697    b4 = ROL64((a41^d1), 2);
 4698    b0 = ROL64((a22^d2), 62);
 4699    b1 = ROL64((a03^d3), 55);
 4700    b2 = ROL64((a34^d4), 39);
 4701    a10 =   b0 ^((~b1)&  b2 );
 4702    a41 =   b1 ^((~b2)&  b3 );
 4703    a22 =   b2 ^((~b3)&  b4 );
 4704    a03 =   b3 ^((~b4)&  b0 );
 4705    a34 =   b4 ^((~b0)&  b1 );
 4706
 4707    c0 = a00^a40^a30^a20^a10;
 4708    c1 = a31^a21^a11^a01^a41;
 4709    c2 = a12^a02^a42^a32^a22;
 4710    c3 = a43^a33^a23^a13^a03;
 4711    c4 = a24^a14^a04^a44^a34;
 4712    d0 = c4^ROL64(c1, 1);
 4713    d1 = c0^ROL64(c2, 1);
 4714    d2 = c1^ROL64(c3, 1);
 4715    d3 = c2^ROL64(c4, 1);
 4716    d4 = c3^ROL64(c0, 1);
 4717
 4718    b0 = (a00^d0);
 4719    b1 = ROL64((a21^d1), 44);
 4720    b2 = ROL64((a42^d2), 43);
 4721    b3 = ROL64((a13^d3), 21);
 4722    b4 = ROL64((a34^d4), 14);
 4723    a00 =   b0 ^((~b1)&  b2 );
 4724    a00 ^= RC[i+2];
 4725    a21 =   b1 ^((~b2)&  b3 );
 4726    a42 =   b2 ^((~b3)&  b4 );
 4727    a13 =   b3 ^((~b4)&  b0 );
 4728    a34 =   b4 ^((~b0)&  b1 );
 4729
 4730    b2 = ROL64((a30^d0), 3);
 4731    b3 = ROL64((a01^d1), 45);
 4732    b4 = ROL64((a22^d2), 61);
 4733    b0 = ROL64((a43^d3), 28);
 4734    b1 = ROL64((a14^d4), 20);
 4735    a30 =   b0 ^((~b1)&  b2 );
 4736    a01 =   b1 ^((~b2)&  b3 );
 4737    a22 =   b2 ^((~b3)&  b4 );
 4738    a43 =   b3 ^((~b4)&  b0 );
 4739    a14 =   b4 ^((~b0)&  b1 );
 4740
 4741    b4 = ROL64((a10^d0), 18);
 4742    b0 = ROL64((a31^d1), 1);
 4743    b1 = ROL64((a02^d2), 6);
 4744    b2 = ROL64((a23^d3), 25);
 4745    b3 = ROL64((a44^d4), 8);
 4746    a10 =   b0 ^((~b1)&  b2 );
 4747    a31 =   b1 ^((~b2)&  b3 );
 4748    a02 =   b2 ^((~b3)&  b4 );
 4749    a23 =   b3 ^((~b4)&  b0 );
 4750    a44 =   b4 ^((~b0)&  b1 );
 4751
 4752    b1 = ROL64((a40^d0), 36);
 4753    b2 = ROL64((a11^d1), 10);
 4754    b3 = ROL64((a32^d2), 15);
 4755    b4 = ROL64((a03^d3), 56);
 4756    b0 = ROL64((a24^d4), 27);
 4757    a40 =   b0 ^((~b1)&  b2 );
 4758    a11 =   b1 ^((~b2)&  b3 );
 4759    a32 =   b2 ^((~b3)&  b4 );
 4760    a03 =   b3 ^((~b4)&  b0 );
 4761    a24 =   b4 ^((~b0)&  b1 );
 4762
 4763    b3 = ROL64((a20^d0), 41);
 4764    b4 = ROL64((a41^d1), 2);
 4765    b0 = ROL64((a12^d2), 62);
 4766    b1 = ROL64((a33^d3), 55);
 4767    b2 = ROL64((a04^d4), 39);
 4768    a20 =   b0 ^((~b1)&  b2 );
 4769    a41 =   b1 ^((~b2)&  b3 );
 4770    a12 =   b2 ^((~b3)&  b4 );
 4771    a33 =   b3 ^((~b4)&  b0 );
 4772    a04 =   b4 ^((~b0)&  b1 );
 4773
 4774    c0 = a00^a30^a10^a40^a20;
 4775    c1 = a21^a01^a31^a11^a41;
 4776    c2 = a42^a22^a02^a32^a12;
 4777    c3 = a13^a43^a23^a03^a33;
 4778    c4 = a34^a14^a44^a24^a04;
 4779    d0 = c4^ROL64(c1, 1);
 4780    d1 = c0^ROL64(c2, 1);
 4781    d2 = c1^ROL64(c3, 1);
 4782    d3 = c2^ROL64(c4, 1);
 4783    d4 = c3^ROL64(c0, 1);
 4784
 4785    b0 = (a00^d0);
 4786    b1 = ROL64((a01^d1), 44);
 4787    b2 = ROL64((a02^d2), 43);
 4788    b3 = ROL64((a03^d3), 21);
 4789    b4 = ROL64((a04^d4), 14);
 4790    a00 =   b0 ^((~b1)&  b2 );
 4791    a00 ^= RC[i+3];
 4792    a01 =   b1 ^((~b2)&  b3 );
 4793    a02 =   b2 ^((~b3)&  b4 );
 4794    a03 =   b3 ^((~b4)&  b0 );
 4795    a04 =   b4 ^((~b0)&  b1 );
 4796
 4797    b2 = ROL64((a10^d0), 3);
 4798    b3 = ROL64((a11^d1), 45);
 4799    b4 = ROL64((a12^d2), 61);
 4800    b0 = ROL64((a13^d3), 28);
 4801    b1 = ROL64((a14^d4), 20);
 4802    a10 =   b0 ^((~b1)&  b2 );
 4803    a11 =   b1 ^((~b2)&  b3 );
 4804    a12 =   b2 ^((~b3)&  b4 );
 4805    a13 =   b3 ^((~b4)&  b0 );
 4806    a14 =   b4 ^((~b0)&  b1 );
 4807
 4808    b4 = ROL64((a20^d0), 18);
 4809    b0 = ROL64((a21^d1), 1);
 4810    b1 = ROL64((a22^d2), 6);
 4811    b2 = ROL64((a23^d3), 25);
 4812    b3 = ROL64((a24^d4), 8);
 4813    a20 =   b0 ^((~b1)&  b2 );
 4814    a21 =   b1 ^((~b2)&  b3 );
 4815    a22 =   b2 ^((~b3)&  b4 );
 4816    a23 =   b3 ^((~b4)&  b0 );
 4817    a24 =   b4 ^((~b0)&  b1 );
 4818
 4819    b1 = ROL64((a30^d0), 36);
 4820    b2 = ROL64((a31^d1), 10);
 4821    b3 = ROL64((a32^d2), 15);
 4822    b4 = ROL64((a33^d3), 56);
 4823    b0 = ROL64((a34^d4), 27);
 4824    a30 =   b0 ^((~b1)&  b2 );
 4825    a31 =   b1 ^((~b2)&  b3 );
 4826    a32 =   b2 ^((~b3)&  b4 );
 4827    a33 =   b3 ^((~b4)&  b0 );
 4828    a34 =   b4 ^((~b0)&  b1 );
 4829
 4830    b3 = ROL64((a40^d0), 41);
 4831    b4 = ROL64((a41^d1), 2);
 4832    b0 = ROL64((a42^d2), 62);
 4833    b1 = ROL64((a43^d3), 55);
 4834    b2 = ROL64((a44^d4), 39);
 4835    a40 =   b0 ^((~b1)&  b2 );
 4836    a41 =   b1 ^((~b2)&  b3 );
 4837    a42 =   b2 ^((~b3)&  b4 );
 4838    a43 =   b3 ^((~b4)&  b0 );
 4839    a44 =   b4 ^((~b0)&  b1 );
 4840  }
 4841}
 4842
 4843/*
 4844** Initialize a new hash.  iSize determines the size of the hash
 4845** in bits and should be one of 224, 256, 384, or 512.  Or iSize
 4846** can be zero to use the default hash size of 256 bits.
 4847*/
 4848static void SHA3Init(SHA3Context *p, int iSize){
 4849  memset(p, 0, sizeof(*p));
 4850  p->iSize = iSize;
 4851  if( iSize>=128 && iSize<=512 ){
 4852    p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
 4853  }else{
 4854    p->nRate = (1600 - 2*256)/8;
 4855  }
 4856#if SHA3_BYTEORDER==1234
 4857  /* Known to be little-endian at compile-time. No-op */
 4858#elif SHA3_BYTEORDER==4321
 4859  p->ixMask = 7;  /* Big-endian */
 4860#else
 4861  {
 4862    static unsigned int one = 1;
 4863    if( 1==*(unsigned char*)&one ){
 4864      /* Little endian.  No byte swapping. */
 4865      p->ixMask = 0;
 4866    }else{
 4867      /* Big endian.  Byte swap. */
 4868      p->ixMask = 7;
 4869    }
 4870  }
 4871#endif
 4872}
 4873
 4874/*
 4875** Make consecutive calls to the SHA3Update function to add new content
 4876** to the hash
 4877*/
 4878static void SHA3Update(
 4879  SHA3Context *p,
 4880  const unsigned char *aData,
 4881  unsigned int nData
 4882){
 4883  unsigned int i = 0;
 4884  if( aData==0 ) return;
 4885#if SHA3_BYTEORDER==1234
 4886  if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
 4887    for(; i+7<nData; i+=8){
 4888      p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
 4889      p->nLoaded += 8;
 4890      if( p->nLoaded>=p->nRate ){
 4891        KeccakF1600Step(p);
 4892        p->nLoaded = 0;
 4893      }
 4894    }
 4895  }
 4896#endif
 4897  for(; i<nData; i++){
 4898#if SHA3_BYTEORDER==1234
 4899    p->u.x[p->nLoaded] ^= aData[i];
 4900#elif SHA3_BYTEORDER==4321
 4901    p->u.x[p->nLoaded^0x07] ^= aData[i];
 4902#else
 4903    p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
 4904#endif
 4905    p->nLoaded++;
 4906    if( p->nLoaded==p->nRate ){
 4907      KeccakF1600Step(p);
 4908      p->nLoaded = 0;
 4909    }
 4910  }
 4911}
 4912
 4913/*
 4914** After all content has been added, invoke SHA3Final() to compute
 4915** the final hash.  The function returns a pointer to the binary
 4916** hash value.
 4917*/
 4918static unsigned char *SHA3Final(SHA3Context *p){
 4919  unsigned int i;
 4920  if( p->nLoaded==p->nRate-1 ){
 4921    const unsigned char c1 = 0x86;
 4922    SHA3Update(p, &c1, 1);
 4923  }else{
 4924    const unsigned char c2 = 0x06;
 4925    const unsigned char c3 = 0x80;
 4926    SHA3Update(p, &c2, 1);
 4927    p->nLoaded = p->nRate - 1;
 4928    SHA3Update(p, &c3, 1);
 4929  }
 4930  for(i=0; i<p->nRate; i++){
 4931    p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
 4932  }
 4933  return &p->u.x[p->nRate];
 4934}
 4935/* End of the hashing logic
 4936*****************************************************************************/
 4937
 4938/*
 4939** Implementation of the sha3(X,SIZE) function.
 4940**
 4941** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
 4942** size is 256.  If X is a BLOB, it is hashed as is.  
 4943** For all other non-NULL types of input, X is converted into a UTF-8 string
 4944** and the string is hashed without the trailing 0x00 terminator.  The hash
 4945** of a NULL value is NULL.
 4946*/
 4947static void sha3Func(
 4948  sqlite3_context *context,
 4949  int argc,
 4950  sqlite3_value **argv
 4951){
 4952  SHA3Context cx;
 4953  int eType = sqlite3_value_type(argv[0]);
 4954  int nByte = sqlite3_value_bytes(argv[0]);
 4955  int iSize;
 4956  if( argc==1 ){
 4957    iSize = 256;
 4958  }else{
 4959    iSize = sqlite3_value_int(argv[1]);
 4960    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
 4961      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
 4962                                    "384 512", -1);
 4963      return;
 4964    }
 4965  }
 4966  if( eType==SQLITE_NULL ) return;
 4967  SHA3Init(&cx, iSize);
 4968  if( eType==SQLITE_BLOB ){
 4969    SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
 4970  }else{
 4971    SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
 4972  }
 4973  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
 4974}
 4975
 4976/* Compute a string using sqlite3_vsnprintf() with a maximum length
 4977** of 50 bytes and add it to the hash.
 4978*/
 4979static void sha3_step_vformat(
 4980  SHA3Context *p,                 /* Add content to this context */
 4981  const char *zFormat,
 4982  ...
 4983){
 4984  va_list ap;
 4985  int n;
 4986  char zBuf[50];
 4987  va_start(ap, zFormat);
 4988  sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
 4989  va_end(ap);
 4990  n = (int)strlen(zBuf);
 4991  SHA3Update(p, (unsigned char*)zBuf, n);
 4992}
 4993
 4994/*
 4995** Update a SHA3Context using a single sqlite3_value.
 4996*/
 4997static void sha3UpdateFromValue(SHA3Context *p, sqlite3_value *pVal){
 4998  switch( sqlite3_value_type(pVal) ){
 4999    case SQLITE_NULL: {
 5000      SHA3Update(p, (const unsigned char*)"N",1);
 5001      break;
 5002    }
 5003    case SQLITE_INTEGER: {
 5004      sqlite3_uint64 u;
 5005      int j;
 5006      unsigned char x[9];
 5007      sqlite3_int64 v = sqlite3_value_int64(pVal);
 5008      memcpy(&u, &v, 8);
 5009      for(j=8; j>=1; j--){
 5010        x[j] = u & 0xff;
 5011        u >>= 8;
 5012      }
 5013      x[0] = 'I';
 5014      SHA3Update(p, x, 9);
 5015      break;
 5016    }
 5017    case SQLITE_FLOAT: {
 5018      sqlite3_uint64 u;
 5019      int j;
 5020      unsigned char x[9];
 5021      double r = sqlite3_value_double(pVal);
 5022      memcpy(&u, &r, 8);
 5023      for(j=8; j>=1; j--){
 5024        x[j] = u & 0xff;
 5025        u >>= 8;
 5026      }
 5027      x[0] = 'F';
 5028      SHA3Update(p,x,9);
 5029      break;
 5030    }
 5031    case SQLITE_TEXT: {
 5032      int n2 = sqlite3_value_bytes(pVal);
 5033      const unsigned char *z2 = sqlite3_value_text(pVal);
 5034      sha3_step_vformat(p,"T%d:",n2);
 5035      SHA3Update(p, z2, n2);
 5036      break;
 5037    }
 5038    case SQLITE_BLOB: {
 5039      int n2 = sqlite3_value_bytes(pVal);
 5040      const unsigned char *z2 = sqlite3_value_blob(pVal);
 5041      sha3_step_vformat(p,"B%d:",n2);
 5042      SHA3Update(p, z2, n2);
 5043      break;
 5044    }
 5045  }
 5046}
 5047
 5048/*
 5049** Implementation of the sha3_query(SQL,SIZE) function.
 5050**
 5051** This function compiles and runs the SQL statement(s) given in the
 5052** argument. The results are hashed using a SIZE-bit SHA3.  The default
 5053** size is 256.
 5054**
 5055** The format of the byte stream that is hashed is summarized as follows:
 5056**
 5057**       S<n>:<sql>
 5058**       R
 5059**       N
 5060**       I<int>
 5061**       F<ieee-float>
 5062**       B<size>:<bytes>
 5063**       T<size>:<text>
 5064**
 5065** <sql> is the original SQL text for each statement run and <n> is
 5066** the size of that text.  The SQL text is UTF-8.  A single R character
 5067** occurs before the start of each row.  N means a NULL value.
 5068** I mean an 8-byte little-endian integer <int>.  F is a floating point
 5069** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
 5070** B means blobs of <size> bytes.  T means text rendered as <size>
 5071** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
 5072** text integers.
 5073**
 5074** For each SQL statement in the X input, there is one S segment.  Each
 5075** S segment is followed by zero or more R segments, one for each row in the
 5076** result set.  After each R, there are one or more N, I, F, B, or T segments,
 5077** one for each column in the result set.  Segments are concatentated directly
 5078** with no delimiters of any kind.
 5079*/
 5080static void sha3QueryFunc(
 5081  sqlite3_context *context,
 5082  int argc,
 5083  sqlite3_value **argv
 5084){
 5085  sqlite3 *db = sqlite3_context_db_handle(context);
 5086  const char *zSql = (const char*)sqlite3_value_text(argv[0]);
 5087  sqlite3_stmt *pStmt = 0;
 5088  int nCol;                   /* Number of columns in the result set */
 5089  int i;                      /* Loop counter */
 5090  int rc;
 5091  int n;
 5092  const char *z;
 5093  SHA3Context cx;
 5094  int iSize;
 5095
 5096  if( argc==1 ){
 5097    iSize = 256;
 5098  }else{
 5099    iSize = sqlite3_value_int(argv[1]);
 5100    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
 5101      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
 5102                                    "384 512", -1);
 5103      return;
 5104    }
 5105  }
 5106  if( zSql==0 ) return;
 5107  SHA3Init(&cx, iSize);
 5108  while( zSql[0] ){
 5109    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
 5110    if( rc ){
 5111      char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
 5112                                   zSql, sqlite3_errmsg(db));
 5113      sqlite3_finalize(pStmt);
 5114      sqlite3_result_error(context, zMsg, -1);
 5115      sqlite3_free(zMsg);
 5116      return;
 5117    }
 5118    if( !sqlite3_stmt_readonly(pStmt) ){
 5119      char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
 5120      sqlite3_finalize(pStmt);
 5121      sqlite3_result_error(context, zMsg, -1);
 5122      sqlite3_free(zMsg);
 5123      return;
 5124    }
 5125    nCol = sqlite3_column_count(pStmt);
 5126    z = sqlite3_sql(pStmt);
 5127    if( z ){
 5128      n = (int)strlen(z);
 5129      sha3_step_vformat(&cx,"S%d:",n);
 5130      SHA3Update(&cx,(unsigned char*)z,n);
 5131    }
 5132
 5133    /* Compute a hash over the result of the query */
 5134    while( SQLITE_ROW==sqlite3_step(pStmt) ){
 5135      SHA3Update(&cx,(const unsigned char*)"R",1);
 5136      for(i=0; i<nCol; i++){
 5137        sha3UpdateFromValue(&cx, sqlite3_column_value(pStmt,i));
 5138      }
 5139    }
 5140    sqlite3_finalize(pStmt);
 5141  }
 5142  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
 5143}
 5144
 5145/*
 5146** xStep function for sha3_agg().
 5147*/
 5148static void sha3AggStep(
 5149  sqlite3_context *context,
 5150  int argc,
 5151  sqlite3_value **argv
 5152){
 5153  SHA3Context *p;
 5154  p = (SHA3Context*)sqlite3_aggregate_context(context, sizeof(*p));
 5155  if( p==0 ) return;
 5156  if( p->nRate==0 ){
 5157    int sz = 256;
 5158    if( argc==2 ){
 5159      sz = sqlite3_value_int(argv[1]);
 5160      if( sz!=224 && sz!=384 && sz!=512 ){
 5161        sz = 256;
 5162      }
 5163    }
 5164    SHA3Init(p, sz);
 5165  }
 5166  sha3UpdateFromValue(p, argv[0]);
 5167}
 5168
 5169
 5170/*
 5171** xFinal function for sha3_agg().
 5172*/
 5173static void sha3AggFinal(sqlite3_context *context){
 5174  SHA3Context *p;
 5175  p = (SHA3Context*)sqlite3_aggregate_context(context, sizeof(*p));
 5176  if( p==0 ) return;
 5177  if( p->iSize ){
 5178    sqlite3_result_blob(context, SHA3Final(p), p->iSize/8, SQLITE_TRANSIENT);
 5179  }
 5180}
 5181
 5182
 5183
 5184#ifdef _WIN32
 5185
 5186#endif
 5187int sqlite3_shathree_init(
 5188  sqlite3 *db,
 5189  char **pzErrMsg,
 5190  const sqlite3_api_routines *pApi
 5191){
 5192  int rc = SQLITE_OK;
 5193  SQLITE_EXTENSION_INIT2(pApi);
 5194  (void)pzErrMsg;  /* Unused parameter */
 5195  rc = sqlite3_create_function(db, "sha3", 1,
 5196                      SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
 5197                      0, sha3Func, 0, 0);
 5198  if( rc==SQLITE_OK ){
 5199    rc = sqlite3_create_function(db, "sha3", 2,
 5200                      SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
 5201                      0, sha3Func, 0, 0);
 5202  }
 5203  if( rc==SQLITE_OK ){
 5204    rc = sqlite3_create_function(db, "sha3_agg", 1,
 5205                      SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
 5206                      0, 0, sha3AggStep, sha3AggFinal);
 5207  }
 5208  if( rc==SQLITE_OK ){
 5209    rc = sqlite3_create_function(db, "sha3_agg", 2,
 5210                      SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
 5211                      0, 0, sha3AggStep, sha3AggFinal);
 5212  }
 5213  if( rc==SQLITE_OK ){
 5214    rc = sqlite3_create_function(db, "sha3_query", 1,
 5215                      SQLITE_UTF8 | SQLITE_DIRECTONLY,
 5216                      0, sha3QueryFunc, 0, 0);
 5217  }
 5218  if( rc==SQLITE_OK ){
 5219    rc = sqlite3_create_function(db, "sha3_query", 2,
 5220                      SQLITE_UTF8 | SQLITE_DIRECTONLY,
 5221                      0, sha3QueryFunc, 0, 0);
 5222  }
 5223  return rc;
 5224}
 5225
 5226/************************* End ext/misc/shathree.c ********************/
 5227/************************* Begin ext/misc/sha1.c ******************/
 5228/*
 5229** 2017-01-27
 5230**
 5231** The author disclaims copyright to this source code.  In place of
 5232** a legal notice, here is a blessing:
 5233**
 5234**    May you do good and not evil.
 5235**    May you find forgiveness for yourself and forgive others.
 5236**    May you share freely, never taking more than you give.
 5237**
 5238******************************************************************************
 5239**
 5240** This SQLite extension implements functions that compute SHA1 hashes.
 5241** Two SQL functions are implemented:
 5242**
 5243**     sha1(X)
 5244**     sha1_query(Y)
 5245**
 5246** The sha1(X) function computes the SHA1 hash of the input X, or NULL if
 5247** X is NULL.
 5248**
 5249** The sha1_query(Y) function evalutes all queries in the SQL statements of Y
 5250** and returns a hash of their results.
 5251*/
 5252/* #include "sqlite3ext.h" */
 5253SQLITE_EXTENSION_INIT1
 5254#include <assert.h>
 5255#include <string.h>
 5256#include <stdarg.h>
 5257
 5258/******************************************************************************
 5259** The Hash Engine
 5260*/
 5261/* Context for the SHA1 hash */
 5262typedef struct SHA1Context SHA1Context;
 5263struct SHA1Context {
 5264  unsigned int state[5];
 5265  unsigned int count[2];
 5266  unsigned char buffer[64];
 5267};
 5268
 5269#define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r))
 5270#define rol(x,k) SHA_ROT(x,k,32-(k))
 5271#define ror(x,k) SHA_ROT(x,32-(k),k)
 5272
 5273#define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \
 5274    |(rol(block[i],8)&0x00FF00FF))
 5275#define blk0be(i) block[i]
 5276#define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
 5277    ^block[(i+2)&15]^block[i&15],1))
 5278
 5279/*
 5280 * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
 5281 *
 5282 * Rl0() for little-endian and Rb0() for big-endian.  Endianness is
 5283 * determined at run-time.
 5284 */
 5285#define Rl0(v,w,x,y,z,i) \
 5286    z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2);
 5287#define Rb0(v,w,x,y,z,i) \
 5288    z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2);
 5289#define R1(v,w,x,y,z,i) \
 5290    z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2);
 5291#define R2(v,w,x,y,z,i) \
 5292    z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2);
 5293#define R3(v,w,x,y,z,i) \
 5294    z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2);
 5295#define R4(v,w,x,y,z,i) \
 5296    z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2);
 5297
 5298/*
 5299 * Hash a single 512-bit block. This is the core of the algorithm.
 5300 */
 5301static void SHA1Transform(unsigned int state[5], const unsigned char buffer[64]){
 5302  unsigned int qq[5]; /* a, b, c, d, e; */
 5303  static int one = 1;
 5304  unsigned int block[16];
 5305  memcpy(block, buffer, 64);
 5306  memcpy(qq,state,5*sizeof(unsigned int));
 5307
 5308#define a qq[0]
 5309#define b qq[1]
 5310#define c qq[2]
 5311#define d qq[3]
 5312#define e qq[4]
 5313
 5314  /* Copy p->state[] to working vars */
 5315  /*
 5316  a = state[0];
 5317  b = state[1];
 5318  c = state[2];
 5319  d = state[3];
 5320  e = state[4];
 5321  */
 5322
 5323  /* 4 rounds of 20 operations each. Loop unrolled. */
 5324  if( 1 == *(unsigned char*)&one ){
 5325    Rl0(a,b,c,d,e, 0); Rl0(e,a,b,c,d, 1); Rl0(d,e,a,b,c, 2); Rl0(c,d,e,a,b, 3);
 5326    Rl0(b,c,d,e,a, 4); Rl0(a,b,c,d,e, 5); Rl0(e,a,b,c,d, 6); Rl0(d,e,a,b,c, 7);
 5327    Rl0(c,d,e,a,b, 8); Rl0(b,c,d,e,a, 9); Rl0(a,b,c,d,e,10); Rl0(e,a,b,c,d,11);
 5328    Rl0(d,e,a,b,c,12); Rl0(c,d,e,a,b,13); Rl0(b,c,d,e,a,14); Rl0(a,b,c,d,e,15);
 5329  }else{
 5330    Rb0(a,b,c,d,e, 0); Rb0(e,a,b,c,d, 1); Rb0(d,e,a,b,c, 2); Rb0(c,d,e,a,b, 3);
 5331    Rb0(b,c,d,e,a, 4); Rb0(a,b,c,d,e, 5); Rb0(e,a,b,c,d, 6); Rb0(d,e,a,b,c, 7);
 5332    Rb0(c,d,e,a,b, 8); Rb0(b,c,d,e,a, 9); Rb0(a,b,c,d,e,10); Rb0(e,a,b,c,d,11);
 5333    Rb0(d,e,a,b,c,12); Rb0(c,d,e,a,b,13); Rb0(b,c,d,e,a,14); Rb0(a,b,c,d,e,15);
 5334  }
 5335  R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
 5336  R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
 5337  R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
 5338  R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
 5339  R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
 5340  R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
 5341  R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
 5342  R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
 5343  R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
 5344  R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
 5345  R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
 5346  R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
 5347  R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
 5348  R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
 5349  R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
 5350  R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
 5351
 5352  /* Add the working vars back into context.state[] */
 5353  state[0] += a;
 5354  state[1] += b;
 5355  state[2] += c;
 5356  state[3] += d;
 5357  state[4] += e;
 5358
 5359#undef a
 5360#undef b
 5361#undef c
 5362#undef d
 5363#undef e
 5364}
 5365
 5366
 5367/* Initialize a SHA1 context */
 5368static void hash_init(SHA1Context *p){
 5369  /* SHA1 initialization constants */
 5370  p->state[0] = 0x67452301;
 5371  p->state[1] = 0xEFCDAB89;
 5372  p->state[2] = 0x98BADCFE;
 5373  p->state[3] = 0x10325476;
 5374  p->state[4] = 0xC3D2E1F0;
 5375  p->count[0] = p->count[1] = 0;
 5376}
 5377
 5378/* Add new content to the SHA1 hash */
 5379static void hash_step(
 5380  SHA1Context *p,                 /* Add content to this context */
 5381  const unsigned char *data,      /* Data to be added */
 5382  unsigned int len                /* Number of bytes in data */
 5383){
 5384  unsigned int i, j;
 5385
 5386  j = p->count[0];
 5387  if( (p->count[0] += len << 3) < j ){
 5388    p->count[1] += (len>>29)+1;
 5389  }
 5390  j = (j >> 3) & 63;
 5391  if( (j + len) > 63 ){
 5392    (void)memcpy(&p->buffer[j], data, (i = 64-j));
 5393    SHA1Transform(p->state, p->buffer);
 5394    for(; i + 63 < len; i += 64){
 5395      SHA1Transform(p->state, &data[i]);
 5396    }
 5397    j = 0;
 5398  }else{
 5399    i = 0;
 5400  }
 5401  (void)memcpy(&p->buffer[j], &data[i], len - i);
 5402}
 5403
 5404/* Compute a string using sqlite3_vsnprintf() and hash it */
 5405static void hash_step_vformat(
 5406  SHA1Context *p,                 /* Add content to this context */
 5407  const char *zFormat,
 5408  ...
 5409){
 5410  va_list ap;
 5411  int n;
 5412  char zBuf[50];
 5413  va_start(ap, zFormat);
 5414  sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
 5415  va_end(ap);
 5416  n = (int)strlen(zBuf);
 5417  hash_step(p, (unsigned char*)zBuf, n);
 5418}
 5419
 5420
 5421/* Add padding and compute the message digest.  Render the
 5422** message digest as lower-case hexadecimal and put it into
 5423** zOut[].  zOut[] must be at least 41 bytes long. */
 5424static void hash_finish(
 5425  SHA1Context *p,           /* The SHA1 context to finish and render */
 5426  char *zOut,               /* Store hex or binary hash here */
 5427  int bAsBinary             /* 1 for binary hash, 0 for hex hash */
 5428){
 5429  unsigned int i;
 5430  unsigned char finalcount[8];
 5431  unsigned char digest[20];
 5432  static const char zEncode[] = "0123456789abcdef";
 5433
 5434  for (i = 0; i < 8; i++){
 5435    finalcount[i] = (unsigned char)((p->count[(i >= 4 ? 0 : 1)]
 5436       >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
 5437  }
 5438  hash_step(p, (const unsigned char *)"\200", 1);
 5439  while ((p->count[0] & 504) != 448){
 5440    hash_step(p, (const unsigned char *)"\0", 1);
 5441  }
 5442  hash_step(p, finalcount, 8);  /* Should cause a SHA1Transform() */
 5443  for (i = 0; i < 20; i++){
 5444    digest[i] = (unsigned char)((p->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
 5445  }
 5446  if( bAsBinary ){
 5447    memcpy(zOut, digest, 20);
 5448  }else{
 5449    for(i=0; i<20; i++){
 5450      zOut[i*2] = zEncode[(digest[i]>>4)&0xf];
 5451      zOut[i*2+1] = zEncode[digest[i] & 0xf];
 5452    }
 5453    zOut[i*2]= 0;
 5454  }
 5455}
 5456/* End of the hashing logic
 5457*****************************************************************************/
 5458
 5459/*
 5460** Two SQL functions:  sha1(X) and sha1b(X).
 5461**
 5462** sha1(X) returns a lower-case hexadecimal rendering of the SHA1 hash
 5463** of the argument X.  If X is a BLOB, it is hashed as is.  For all other
 5464** types of input, X is converted into a UTF-8 string and the string
 5465** is hashed without the trailing 0x00 terminator.  The hash of a NULL
 5466** value is NULL.
 5467**
 5468** sha1b(X) is the same except that it returns a 20-byte BLOB containing
 5469** the binary hash instead of a hexadecimal string.
 5470*/
 5471static void sha1Func(
 5472  sqlite3_context *context,
 5473  int argc,
 5474  sqlite3_value **argv
 5475){
 5476  SHA1Context cx;
 5477  int eType = sqlite3_value_type(argv[0]);
 5478  int nByte = sqlite3_value_bytes(argv[0]);
 5479  const unsigned char *pData;
 5480  char zOut[44];
 5481
 5482  assert( argc==1 );
 5483  if( eType==SQLITE_NULL ) return;
 5484  hash_init(&cx);
 5485  if( eType==SQLITE_BLOB ){
 5486    pData = (const unsigned char*)sqlite3_value_blob(argv[0]);
 5487  }else{
 5488    pData = (const unsigned char*)sqlite3_value_text(argv[0]);
 5489  }
 5490  if( pData==0 ) return;
 5491  hash_step(&cx, pData, nByte);
 5492  if( sqlite3_user_data(context)!=0 ){
 5493    /* sha1b() - binary result */
 5494    hash_finish(&cx, zOut, 1);
 5495    sqlite3_result_blob(context, zOut, 20, SQLITE_TRANSIENT);
 5496  }else{
 5497    /* sha1() - hexadecimal text result */
 5498    hash_finish(&cx, zOut, 0);
 5499    sqlite3_result_text(context, zOut, 40, SQLITE_TRANSIENT);
 5500  }
 5501}
 5502
 5503/*
 5504** Implementation of the sha1_query(SQL) function.
 5505**
 5506** This function compiles and runs the SQL statement(s) given in the
 5507** argument. The results are hashed using SHA1 and that hash is returned.
 5508**
 5509** The original SQL text is included as part of the hash.
 5510**
 5511** The hash is not just a concatenation of the outputs.  Each query
 5512** is delimited and each row and value within the query is delimited,
 5513** with all values being marked with their datatypes.
 5514*/
 5515static void sha1QueryFunc(
 5516  sqlite3_context *context,
 5517  int argc,
 5518  sqlite3_value **argv
 5519){
 5520  sqlite3 *db = sqlite3_context_db_handle(context);
 5521  const char *zSql = (const char*)sqlite3_value_text(argv[0]);
 5522  sqlite3_stmt *pStmt = 0;
 5523  int nCol;                   /* Number of columns in the result set */
 5524  int i;                      /* Loop counter */
 5525  int rc;
 5526  int n;
 5527  const char *z;
 5528  SHA1Context cx;
 5529  char zOut[44];
 5530
 5531  assert( argc==1 );
 5532  if( zSql==0 ) return;
 5533  hash_init(&cx);
 5534  while( zSql[0] ){
 5535    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
 5536    if( rc ){
 5537      char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
 5538                                   zSql, sqlite3_errmsg(db));
 5539      sqlite3_finalize(pStmt);
 5540      sqlite3_result_error(context, zMsg, -1);
 5541      sqlite3_free(zMsg);
 5542      return;
 5543    }
 5544    if( !sqlite3_stmt_readonly(pStmt) ){
 5545      char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
 5546      sqlite3_finalize(pStmt);
 5547      sqlite3_result_error(context, zMsg, -1);
 5548      sqlite3_free(zMsg);
 5549      return;
 5550    }
 5551    nCol = sqlite3_column_count(pStmt);
 5552    z = sqlite3_sql(pStmt);
 5553    if( z==0 ) z = "";
 5554    n = (int)strlen(z);
 5555    hash_step_vformat(&cx,"S%d:",n);
 5556    hash_step(&cx,(unsigned char*)z,n);
 5557
 5558    /* Compute a hash over the result of the query */
 5559    while( SQLITE_ROW==sqlite3_step(pStmt) ){
 5560      hash_step(&cx,(const unsigned char*)"R",1);
 5561      for(i=0; i<nCol; i++){
 5562        switch( sqlite3_column_type(pStmt,i) ){
 5563          case SQLITE_NULL: {
 5564            hash_step(&cx, (const unsigned char*)"N",1);
 5565            break;
 5566          }
 5567          case SQLITE_INTEGER: {
 5568            sqlite3_uint64 u;
 5569            int j;
 5570            unsigned char x[9];
 5571            sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
 5572            memcpy(&u, &v, 8);
 5573            for(j=8; j>=1; j--){
 5574              x[j] = u & 0xff;
 5575              u >>= 8;
 5576            }
 5577            x[0] = 'I';
 5578            hash_step(&cx, x, 9);
 5579            break;
 5580          }
 5581          case SQLITE_FLOAT: {
 5582            sqlite3_uint64 u;
 5583            int j;
 5584            unsigned char x[9];
 5585            double r = sqlite3_column_double(pStmt,i);
 5586            memcpy(&u, &r, 8);
 5587            for(j=8; j>=1; j--){
 5588              x[j] = u & 0xff;
 5589              u >>= 8;
 5590            }
 5591            x[0] = 'F';
 5592            hash_step(&cx,x,9);
 5593            break;
 5594          }
 5595          case SQLITE_TEXT: {
 5596            int n2 = sqlite3_column_bytes(pStmt, i);
 5597            const unsigned char *z2 = sqlite3_column_text(pStmt, i);
 5598            hash_step_vformat(&cx,"T%d:",n2);
 5599            hash_step(&cx, z2, n2);
 5600            break;
 5601          }
 5602          case SQLITE_BLOB: {
 5603            int n2 = sqlite3_column_bytes(pStmt, i);
 5604            const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
 5605            hash_step_vformat(&cx,"B%d:",n2);
 5606            hash_step(&cx, z2, n2);
 5607            break;
 5608          }
 5609        }
 5610      }
 5611    }
 5612    sqlite3_finalize(pStmt);
 5613  }
 5614  hash_finish(&cx, zOut, 0);
 5615  sqlite3_result_text(context, zOut, 40, SQLITE_TRANSIENT);
 5616}
 5617
 5618
 5619#ifdef _WIN32
 5620
 5621#endif
 5622int sqlite3_sha_init(
 5623  sqlite3 *db,
 5624  char **pzErrMsg,
 5625  const sqlite3_api_routines *pApi
 5626){
 5627  int rc = SQLITE_OK;
 5628  static int one = 1;
 5629  SQLITE_EXTENSION_INIT2(pApi);
 5630  (void)pzErrMsg;  /* Unused parameter */
 5631  rc = sqlite3_create_function(db, "sha1", 1, 
 5632                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
 5633                                0, sha1Func, 0, 0);
 5634  if( rc==SQLITE_OK ){
 5635    rc = sqlite3_create_function(db, "sha1b", 1, 
 5636                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
 5637                          (void*)&one, sha1Func, 0, 0);
 5638  }
 5639  if( rc==SQLITE_OK ){
 5640    rc = sqlite3_create_function(db, "sha1_query", 1, 
 5641                                 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
 5642                                 sha1QueryFunc, 0, 0);
 5643  }
 5644  return rc;
 5645}
 5646
 5647/************************* End ext/misc/sha1.c ********************/
 5648/************************* Begin ext/misc/uint.c ******************/
 5649/*
 5650** 2020-04-14
 5651**
 5652** The author disclaims copyright to this source code.  In place of
 5653** a legal notice, here is a blessing:
 5654**
 5655**    May you do good and not evil.
 5656**    May you find forgiveness for yourself and forgive others.
 5657**    May you share freely, never taking more than you give.
 5658**
 5659******************************************************************************
 5660**
 5661** This SQLite extension implements the UINT collating sequence.
 5662**
 5663** UINT works like BINARY for text, except that embedded strings
 5664** of digits compare in numeric order.
 5665**
 5666**     *   Leading zeros are handled properly, in the sense that
 5667**         they do not mess of the magnitude comparison of embedded
 5668**         strings of digits.  "x00123y" is equal to "x123y".
 5669**
 5670**     *   Only unsigned integers are recognized.  Plus and minus
 5671**         signs are ignored.  Decimal points and exponential notation
 5672**         are ignored.
 5673**
 5674**     *   Embedded integers can be of arbitrary length.  Comparison
 5675**         is *not* limited integers that can be expressed as a
 5676**         64-bit machine integer.
 5677*/
 5678/* #include "sqlite3ext.h" */
 5679SQLITE_EXTENSION_INIT1
 5680#include <assert.h>
 5681#include <string.h>
 5682#include <ctype.h>
 5683
 5684/*
 5685** Compare text in lexicographic order, except strings of digits
 5686** compare in numeric order.
 5687*/
 5688static int uintCollFunc(
 5689  void *notUsed,
 5690  int nKey1, const void *pKey1,
 5691  int nKey2, const void *pKey2
 5692){
 5693  const unsigned char *zA = (const unsigned char*)pKey1;
 5694  const unsigned char *zB = (const unsigned char*)pKey2;
 5695  int i=0, j=0, x;
 5696  (void)notUsed;
 5697  while( i<nKey1 && j<nKey2 ){
 5698    x = zA[i] - zB[j];
 5699    if( isdigit(zA[i]) ){
 5700      int k;
 5701      if( !isdigit(zB[j]) ) return x;
 5702      while( i<nKey1 && zA[i]=='0' ){ i++; }
 5703      while( j<nKey2 && zB[j]=='0' ){ j++; }
 5704      k = 0;
 5705      while( i+k<nKey1 && isdigit(zA[i+k])
 5706             && j+k<nKey2 && isdigit(zB[j+k]) ){
 5707        k++;
 5708      }
 5709      if( i+k<nKey1 && isdigit(zA[i+k]) ){
 5710        return +1;
 5711      }else if( j+k<nKey2 && isdigit(zB[j+k]) ){
 5712        return -1;
 5713      }else{
 5714        x = memcmp(zA+i, zB+j, k);
 5715        if( x ) return x;
 5716        i += k;
 5717        j += k;
 5718      }
 5719    }else if( x ){
 5720      return x;
 5721    }else{
 5722      i++;
 5723      j++;
 5724    }
 5725  }
 5726  return (nKey1 - i) - (nKey2 - j);
 5727}
 5728
 5729#ifdef _WIN32
 5730
 5731#endif
 5732int sqlite3_uint_init(
 5733  sqlite3 *db, 
 5734  char **pzErrMsg, 
 5735  const sqlite3_api_routines *pApi
 5736){
 5737  SQLITE_EXTENSION_INIT2(pApi);
 5738  (void)pzErrMsg;  /* Unused parameter */
 5739  return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
 5740}
 5741
 5742/************************* End ext/misc/uint.c ********************/
 5743/************************* Begin ext/misc/decimal.c ******************/
 5744/*
 5745** 2020-06-22
 5746**
 5747** The author disclaims copyright to this source code.  In place of
 5748** a legal notice, here is a blessing:
 5749**
 5750**    May you do good and not evil.
 5751**    May you find forgiveness for yourself and forgive others.
 5752**    May you share freely, never taking more than you give.
 5753**
 5754******************************************************************************
 5755**
 5756** Routines to implement arbitrary-precision decimal math.
 5757**
 5758** The focus here is on simplicity and correctness, not performance.
 5759*/
 5760/* #include "sqlite3ext.h" */
 5761SQLITE_EXTENSION_INIT1
 5762#include <assert.h>
 5763#include <string.h>
 5764#include <ctype.h>
 5765#include <stdlib.h>
 5766
 5767/* Mark a function parameter as unused, to suppress nuisance compiler
 5768** warnings. */
 5769#ifndef UNUSED_PARAMETER
 5770# define UNUSED_PARAMETER(X)  (void)(X)
 5771#endif
 5772
 5773#ifndef IsSpace
 5774#define IsSpace(X)  isspace((unsigned char)X)
 5775#endif
 5776
 5777#ifndef SQLITE_DECIMAL_MAX_DIGIT
 5778# define SQLITE_DECIMAL_MAX_DIGIT 10000000
 5779#endif
 5780
 5781/* A decimal object */
 5782typedef struct Decimal Decimal;
 5783struct Decimal {
 5784  char sign;        /* 0 for positive, 1 for negative */
 5785  char oom;         /* True if an OOM is encountered */
 5786  char isNull;      /* True if holds a NULL rather than a number */
 5787  char isInit;      /* True upon initialization */
 5788  int nDigit;       /* Total number of digits */
 5789  int nFrac;        /* Number of digits to the right of the decimal point */
 5790  signed char *a;   /* Array of digits.  Most significant first. */
 5791};
 5792
 5793/*
 5794** Release memory held by a Decimal, but do not free the object itself.
 5795*/
 5796static void decimal_clear(Decimal *p){
 5797  sqlite3_free(p->a);
 5798}
 5799
 5800/*
 5801** Destroy a Decimal object
 5802*/
 5803static void decimal_free(Decimal *p){
 5804  if( p ){
 5805    decimal_clear(p);
 5806    sqlite3_free(p);
 5807  }
 5808}
 5809
 5810/*
 5811** Allocate a new Decimal object initialized to the text in zIn[].
 5812** Return NULL if any kind of error occurs.
 5813*/
 5814static Decimal *decimalNewFromText(const char *zIn, int n){
 5815  Decimal *p = 0;
 5816  int i;
 5817  int iExp = 0;
 5818
 5819  if( zIn==0 ) goto new_from_text_failed;
 5820  p = sqlite3_malloc64( sizeof(*p) );
 5821  if( p==0 ) goto new_from_text_failed;
 5822  p->sign = 0;
 5823  p->oom = 0;
 5824  p->isInit = 1;
 5825  p->isNull = 0;
 5826  p->nDigit = 0;
 5827  p->nFrac = 0;
 5828  p->a = sqlite3_malloc64( n+1 );
 5829  if( p->a==0 ) goto new_from_text_failed;
 5830  for(i=0; IsSpace(zIn[i]); i++){}
 5831  if( zIn[i]=='-' ){
 5832    p->sign = 1;
 5833    i++;
 5834  }else if( zIn[i]=='+' ){
 5835    i++;
 5836  }
 5837  while( i<n && zIn[i]=='0' ) i++;
 5838  while( i<n ){
 5839    char c = zIn[i];
 5840    if( c>='0' && c<='9' ){
 5841      p->a[p->nDigit++] = c - '0';
 5842    }else if( c=='.' ){
 5843      p->nFrac = p->nDigit + 1;
 5844    }else if( c=='e' || c=='E' ){
 5845      int j = i+1;
 5846      int neg = 0;
 5847      if( j>=n ) break;
 5848      if( zIn[j]=='-' ){
 5849        neg = 1;
 5850        j++;
 5851      }else if( zIn[j]=='+' ){
 5852        j++;
 5853      }
 5854      while( j<n && iExp<1000000 ){
 5855        if( zIn[j]>='0' && zIn[j]<='9' ){
 5856          iExp = iExp*10 + zIn[j] - '0';
 5857        }
 5858        j++;
 5859      }
 5860      if( neg ) iExp = -iExp;
 5861      break;
 5862    }
 5863    i++;
 5864  }
 5865  if( p->nFrac ){
 5866    p->nFrac = p->nDigit - (p->nFrac - 1);
 5867  }
 5868  if( iExp>0 ){
 5869    if( p->nFrac>0 ){
 5870      if( iExp<=p->nFrac ){
 5871        p->nFrac -= iExp;
 5872        iExp = 0;
 5873      }else{
 5874        iExp -= p->nFrac;
 5875        p->nFrac = 0;
 5876      }
 5877    }
 5878    if( iExp>0 ){   
 5879      signed char *a = sqlite3_realloc64(p->a, (sqlite3_int64)p->nDigit
 5880                                     + (sqlite3_int64)iExp + 1 );
 5881      if( a==0 ) goto new_from_text_failed;
 5882      p->a = a;
 5883      memset(p->a+p->nDigit, 0, iExp);
 5884      p->nDigit += iExp;
 5885    }
 5886  }else if( iExp<0 ){
 5887    int nExtra;
 5888    iExp = -iExp;
 5889    nExtra = p->nDigit - p->nFrac - 1;
 5890    if( nExtra ){
 5891      if( nExtra>=iExp ){
 5892        p->nFrac += iExp;
 5893        iExp  = 0;
 5894      }else{
 5895        iExp -= nExtra;
 5896        p->nFrac = p->nDigit - 1;
 5897      }
 5898    }
 5899    if( iExp>0 ){
 5900      signed char *a = sqlite3_realloc64(p->a, (sqlite3_int64)p->nDigit
 5901                                     + (sqlite3_int64)iExp + 1 );
 5902      if( a==0 ) goto new_from_text_failed;
 5903      p->a = a;
 5904      memmove(p->a+iExp, p->a, p->nDigit);
 5905      memset(p->a, 0, iExp);
 5906      p->nDigit += iExp;
 5907      p->nFrac += iExp;
 5908    }
 5909  }
 5910  if( p->sign ){
 5911    for(i=0; i<p->nDigit && p->a[i]==0; i++){}
 5912    if( i>=p->nDigit ) p->sign = 0;
 5913  }
 5914  if( p->nDigit>SQLITE_DECIMAL_MAX_DIGIT ) goto new_from_text_failed;
 5915  return p;
 5916
 5917new_from_text_failed:
 5918  if( p ){
 5919    if( p->a ) sqlite3_free(p->a);
 5920    sqlite3_free(p);
 5921  }
 5922  return 0;
 5923}
 5924
 5925/* Forward reference */
 5926static Decimal *decimalFromDouble(double);
 5927
 5928/*
 5929** Allocate a new Decimal object from an sqlite3_value.  Return a pointer
 5930** to the new object, or NULL if there is an error.  If the pCtx argument
 5931** is not NULL, then errors are reported on it as well.
 5932**
 5933** If the pIn argument is SQLITE_TEXT or SQLITE_INTEGER, it is converted
 5934** directly into a Decimal.  For SQLITE_FLOAT or for SQLITE_BLOB of length
 5935** 8 bytes, the resulting double value is expanded into its decimal equivalent.
 5936** If pIn is NULL or if it is a BLOB that is not exactly 8 bytes in length,
 5937** then NULL is returned.
 5938*/
 5939static Decimal *decimal_new(
 5940  sqlite3_context *pCtx,       /* Report error here, if not null */
 5941  sqlite3_value *pIn,          /* Construct the decimal object from this */
 5942  int bTextOnly                /* Always interpret pIn as text if true */
 5943){
 5944  Decimal *p = 0;
 5945  int eType = sqlite3_value_type(pIn);
 5946  if( bTextOnly && (eType==SQLITE_FLOAT || eType==SQLITE_BLOB) ){
 5947    eType = SQLITE_TEXT;
 5948  }
 5949  switch( eType ){
 5950    case SQLITE_TEXT:
 5951    case SQLITE_INTEGER: {
 5952      const char *zIn = (const char*)sqlite3_value_text(pIn);
 5953      int n = sqlite3_value_bytes(pIn);
 5954      p = decimalNewFromText(zIn, n);
 5955      if( p==0 ) goto new_failed;
 5956      break;
 5957    }
 5958
 5959    case SQLITE_FLOAT: {
 5960      p = decimalFromDouble(sqlite3_value_double(pIn));
 5961      break;
 5962    }
 5963
 5964    case SQLITE_BLOB: {
 5965      const unsigned char *x;
 5966      unsigned int i;
 5967      sqlite3_uint64 v = 0;
 5968      double r;
 5969
 5970      if( sqlite3_value_bytes(pIn)!=sizeof(r) ) break;
 5971      x = sqlite3_value_blob(pIn);
 5972      for(i=0; i<sizeof(r); i++){
 5973        v = (v<<8) | x[i];
 5974      }
 5975      memcpy(&r, &v, sizeof(r));
 5976      p = decimalFromDouble(r);
 5977      break;
 5978    }
 5979
 5980    case SQLITE_NULL: {
 5981      break;
 5982    }
 5983  }
 5984  return p;
 5985
 5986new_failed:
 5987  if( pCtx ) sqlite3_result_error_nomem(pCtx);
 5988  sqlite3_free(p);
 5989  return 0;
 5990}
 5991
 5992/*
 5993** Make the given Decimal the result.
 5994*/
 5995static void decimal_result(sqlite3_context *pCtx, Decimal *p){
 5996  char *z;
 5997  int i, j;
 5998  int n;
 5999  if( p==0 || p->oom ){
 6000    sqlite3_result_error_nomem(pCtx);
 6001    return;
 6002  }
 6003  if( p->isNull ){
 6004    sqlite3_result_null(pCtx);
 6005    return;
 6006  }
 6007  z = sqlite3_malloc64( (sqlite3_int64)p->nDigit+4 );
 6008  if( z==0 ){
 6009    sqlite3_result_error_nomem(pCtx);
 6010    return;
 6011  }
 6012  i = 0;
 6013  if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){
 6014    p->sign = 0;
 6015  }
 6016  if( p->sign ){
 6017    z[0] = '-';
 6018    i = 1;
 6019  }
 6020  n = p->nDigit - p->nFrac;
 6021  if( n<=0 ){
 6022    z[i++] = '0';
 6023  }
 6024  j = 0;
 6025  while( n>1 && p->a[j]==0 ){
 6026    j++;
 6027    n--;
 6028  }
 6029  while( n>0  ){
 6030    z[i++] = p->a[j] + '0';
 6031    j++;
 6032    n--;
 6033  }
 6034  if( p->nFrac ){
 6035    z[i++] = '.';
 6036    do{
 6037      z[i++] = p->a[j] + '0';
 6038      j++;
 6039    }while( j<p->nDigit );
 6040  }
 6041  z[i] = 0;
 6042  sqlite3_result_text(pCtx, z, i, sqlite3_free);
 6043}
 6044
 6045/*
 6046** Round a decimal value to N significant digits.  N must be positive.
 6047*/
 6048static void decimal_round(Decimal *p, int N){
 6049  int i;
 6050  int nZero;
 6051  if( N<1 ) return;
 6052  if( p==0 ) return;
 6053  if( p->nDigit<=N ) return;
 6054  for(nZero=0; nZero<p->nDigit && p->a[nZero]==0; nZero++){}
 6055  N += nZero;
 6056  if( p->nDigit<=N ) return;
 6057  if( p->a[N]>4 ){
 6058    p->a[N-1]++;
 6059    for(i=N-1; i>0 && p->a[i]>9; i--){
 6060      p->a[i] = 0;
 6061      p->a[i-1]++;
 6062    }
 6063    if( p->a[0]>9 ){
 6064      p->a[0] = 1;
 6065      p->nFrac--;
 6066    }
 6067  }
 6068  memset(&p->a[N], 0, p->nDigit - N);
 6069}
 6070
 6071/*
 6072** Make the given Decimal the result in an format similar to  '%+#e'.
 6073** In other words, show exponential notation with leading and trailing
 6074** zeros omitted.
 6075*/
 6076static void decimal_result_sci(sqlite3_context *pCtx, Decimal *p, int N){
 6077  char *z;       /* The output buffer */
 6078  int i;         /* Loop counter */
 6079  int nZero;     /* Number of leading zeros */
 6080  int nDigit;    /* Number of digits not counting trailing zeros */
 6081  int nFrac;     /* Digits to the right of the decimal point */
 6082  int exp;       /* Exponent value */
 6083  signed char zero;     /* Zero value */
 6084  signed char *a;       /* Array of digits */
 6085
 6086  if( p==0 || p->oom ){
 6087    sqlite3_result_error_nomem(pCtx);
 6088    return;
 6089  }
 6090  if( p->isNull ){
 6091    sqlite3_result_null(pCtx);
 6092    return;
 6093  }
 6094  if( N<1 ) N = 0;
 6095  for(nDigit=p->nDigit; nDigit>N && p->a[nDigit-1]==0; nDigit--){}
 6096  for(nZero=0; nZero<nDigit && p->a[nZero]==0; nZero++){}
 6097  nFrac = p->nFrac + (nDigit - p->nDigit);
 6098  nDigit -= nZero;
 6099  z = sqlite3_malloc64( (sqlite3_int64)nDigit+20 );
 6100  if( z==0 ){
 6101    sqlite3_result_error_nomem(pCtx);
 6102    return;
 6103  }
 6104  if( nDigit==0 ){
 6105    zero = 0;
 6106    a = &zero;
 6107    nDigit = 1;
 6108    nFrac = 0;
 6109  }else{
 6110    a = &p->a[nZero];
 6111  }
 6112  if( p->sign && nDigit>0 ){
 6113    z[0] = '-';
 6114  }else{
 6115    z[0] = '+';
 6116  }
 6117  z[1] = a[0]+'0';
 6118  z[2] = '.';
 6119  if( nDigit==1 ){
 6120    z[3] = '0';
 6121    i = 4;
 6122  }else{
 6123    for(i=1; i<nDigit; i++){
 6124      z[2+i] = a[i]+'0';
 6125    }
 6126    i = nDigit+2;
 6127  }
 6128  exp = nDigit - nFrac - 1;
 6129  sqlite3_snprintf(nDigit+20-i, &z[i], "e%+03d", exp);
 6130  sqlite3_result_text(pCtx, z, -1, sqlite3_free);
 6131}
 6132
 6133/*
 6134** Compare to Decimal objects.  Return negative, 0, or positive if the
 6135** first object is less than, equal to, or greater than the second.
 6136**
 6137** Preconditions for this routine:
 6138**
 6139**    pA!=0
 6140**    pA->isNull==0
 6141**    pB!=0
 6142**    pB->isNull==0
 6143*/
 6144static int decimal_cmp(Decimal *pA, Decimal *pB){
 6145  int nASig, nBSig, rc, n;
 6146  while( pA->nFrac>0 && pA->a[pA->nDigit-1]==0 ){
 6147    pA->nDigit--;
 6148    pA->nFrac--;
 6149  }
 6150  while( pB->nFrac>0 && pB->a[pB->nDigit-1]==0 ){
 6151    pB->nDigit--;
 6152    pB->nFrac--;
 6153  }
 6154  if( pA->sign!=pB->sign ){
 6155    return pA->sign ? -1 : +1;
 6156  }
 6157  if( pA->sign ){
 6158    Decimal *pTemp = pA;
 6159    pA = pB;
 6160    pB = pTemp;
 6161  }
 6162  nASig = pA->nDigit - pA->nFrac;
 6163  nBSig = pB->nDigit - pB->nFrac;
 6164  if( nASig!=nBSig ){
 6165    return nASig - nBSig;
 6166  }
 6167  n = pA->nDigit;
 6168  if( n>pB->nDigit ) n = pB->nDigit;
 6169  rc = memcmp(pA->a, pB->a, n);
 6170  if( rc==0 ){
 6171    rc = pA->nDigit - pB->nDigit;
 6172  }
 6173  return rc;
 6174}
 6175
 6176/*
 6177** SQL Function:   decimal_cmp(X, Y)
 6178**
 6179** Return negative, zero, or positive if X is less then, equal to, or
 6180** greater than Y.
 6181*/
 6182static void decimalCmpFunc(
 6183  sqlite3_context *context,
 6184  int argc,
 6185  sqlite3_value **argv
 6186){
 6187  Decimal *pA = 0, *pB = 0;
 6188  int rc;
 6189
 6190  UNUSED_PARAMETER(argc);
 6191  pA = decimal_new(context, argv[0], 1);
 6192  if( pA==0 || pA->isNull ) goto cmp_done;
 6193  pB = decimal_new(context, argv[1], 1);
 6194  if( pB==0 || pB->isNull ) goto cmp_done;
 6195  rc = decimal_cmp(pA, pB);
 6196  if( rc<0 ) rc = -1;
 6197  else if( rc>0 ) rc = +1;
 6198  sqlite3_result_int(context, rc);
 6199cmp_done:
 6200  decimal_free(pA);
 6201  decimal_free(pB);
 6202}
 6203
 6204/*
 6205** Expand the Decimal so that it has a least nDigit digits and nFrac
 6206** digits to the right of the decimal point.
 6207*/
 6208static void decimal_expand(Decimal *p, int nDigit, int nFrac){
 6209  int nAddSig;
 6210  int nAddFrac;
 6211  signed char *a;
 6212  if( p==0 ) return;
 6213  nAddFrac = nFrac - p->nFrac;
 6214  nAddSig = (nDigit - p->nDigit) - nAddFrac;
 6215  if( nAddFrac==0 && nAddSig==0 ) return;
 6216  if( nDigit+1>SQLITE_DECIMAL_MAX_DIGIT ){ p->oom = 1; return; }
 6217  a = sqlite3_realloc64(p->a, nDigit+1);
 6218  if( a==0 ){
 6219    p->oom = 1;
 6220    return;
 6221  }
 6222  p->a = a;
 6223  if( nAddSig ){
 6224    memmove(p->a+nAddSig, p->a, p->nDigit);
 6225    memset(p->a, 0, nAddSig);
 6226    p->nDigit += nAddSig;
 6227  }
 6228  if( nAddFrac ){
 6229    memset(p->a+p->nDigit, 0, nAddFrac);
 6230    p->nDigit += nAddFrac;
 6231    p->nFrac += nAddFrac;
 6232  }
 6233}
 6234
 6235/*
 6236** Add the value pB into pA.   A := A + B.
 6237**
 6238** Both pA and pB might become denormalized by this routine.
 6239*/
 6240static void decimal_add(Decimal *pA, Decimal *pB){
 6241  int nSig, nFrac, nDigit;
 6242  int i, rc;
 6243  if( pA==0 ){
 6244    return;
 6245  }
 6246  if( pA->oom || pB==0 || pB->oom ){
 6247    pA->oom = 1;
 6248    return;
 6249  }
 6250  if( pA->isNull || pB->isNull ){
 6251    pA->isNull = 1;
 6252    return;
 6253  }
 6254  nSig = pA->nDigit - pA->nFrac;
 6255  if( nSig && pA->a[0]==0 ) nSig--;
 6256  if( nSig<pB->nDigit-pB->nFrac ){
 6257    nSig = pB->nDigit - pB->nFrac;
 6258  }
 6259  nFrac = pA->nFrac;
 6260  if( nFrac<pB->nFrac ) nFrac = pB->nFrac;
 6261  nDigit = nSig + nFrac + 1;
 6262  decimal_expand(pA, nDigit, nFrac);
 6263  decimal_expand(pB, nDigit, nFrac);
 6264  if( pA->oom || pB->oom ){
 6265    pA->oom = 1;
 6266  }else{
 6267    if( pA->sign==pB->sign ){
 6268      int carry = 0;
 6269      for(i=nDigit-1; i>=0; i--){
 6270        int x = pA->a[i] + pB->a[i] + carry;
 6271        if( x>=10 ){
 6272          carry = 1;
 6273          pA->a[i] = x - 10;
 6274        }else{
 6275          carry = 0;
 6276          pA->a[i] = x;
 6277        }
 6278      }
 6279    }else{
 6280      signed char *aA, *aB;
 6281      int borrow = 0;
 6282      rc = memcmp(pA->a, pB->a, nDigit);
 6283      if( rc<0 ){
 6284        aA = pB->a;
 6285        aB = pA->a;
 6286        pA->sign = !pA->sign;
 6287      }else{
 6288        aA = pA->a;
 6289        aB = pB->a;
 6290      }
 6291      for(i=nDigit-1; i>=0; i--){
 6292        int x = aA[i] - aB[i] - borrow;
 6293        if( x<0 ){
 6294          pA->a[i] = x+10;
 6295          borrow = 1;
 6296        }else{
 6297          pA->a[i] = x;
 6298          borrow = 0;
 6299        }
 6300      }
 6301    }
 6302  }
 6303}
 6304
 6305/*
 6306** Multiply A by B.   A := A * B
 6307**
 6308** All significant digits after the decimal point are retained.
 6309** Trailing zeros after the decimal point are omitted as long as
 6310** the number of digits after the decimal point is no less than
 6311** either the number of digits in either input.
 6312*/
 6313static void decimalMul(Decimal *pA, Decimal *pB){
 6314  signed char *acc = 0;
 6315  int i, j, k;
 6316  int minFrac;
 6317  sqlite3_int64 sumDigit;
 6318
 6319  if( pA==0 || pA->oom || pA->isNull
 6320   || pB==0 || pB->oom || pB->isNull 
 6321  ){
 6322    goto mul_end;
 6323  }
 6324  sumDigit = pA->nDigit;
 6325  sumDigit += pB->nDigit;
 6326  sumDigit += 2;
 6327  if( sumDigit>SQLITE_DECIMAL_MAX_DIGIT ){ pA->oom = 1; return; }
 6328  acc = sqlite3_malloc64( sumDigit );
 6329  if( acc==0 ){
 6330    pA->oom = 1;
 6331    goto mul_end;
 6332  }
 6333  memset(acc, 0, pA->nDigit + pB->nDigit + 2);
 6334  minFrac = pA->nFrac;
 6335  if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
 6336  for(i=pA->nDigit-1; i>=0; i--){
 6337    signed char f = pA->a[i];
 6338    int carry = 0, x;
 6339    for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
 6340      x = acc[k] + f*pB->a[j] + carry;
 6341      acc[k] = x%10;
 6342      carry = x/10;
 6343    }
 6344    x = acc[k] + carry;
 6345    acc[k] = x%10;
 6346    acc[k-1] += x/10;
 6347  }
 6348  sqlite3_free(pA->a);
 6349  pA->a = acc;
 6350  acc = 0;
 6351  pA->nDigit += pB->nDigit + 2;
 6352  pA->nFrac += pB->nFrac;
 6353  pA->sign ^= pB->sign;
 6354  while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
 6355    pA->nFrac--;
 6356    pA->nDigit--;
 6357  }
 6358
 6359mul_end:
 6360  sqlite3_free(acc);
 6361}
 6362
 6363/*
 6364** Create a new Decimal object that contains an integer power of 2.
 6365*/
 6366static Decimal *decimalPow2(int N){
 6367  Decimal *pA = 0;      /* The result to be returned */
 6368  Decimal *pX = 0;      /* Multiplier */
 6369  if( N<-20000 || N>20000 ) goto pow2_fault;
 6370  pA = decimalNewFromText("1.0", 3);
 6371  if( pA==0 || pA->oom ) goto pow2_fault;
 6372  if( N==0 ) return pA;
 6373  if( N>0 ){
 6374    pX = decimalNewFromText("2.0", 3);
 6375  }else{
 6376    N = -N;
 6377    pX = decimalNewFromText("0.5", 3);
 6378  }
 6379  if( pX==0 || pX->oom ) goto pow2_fault;
 6380  while( 1 /* Exit by break */ ){
 6381    if( N & 1 ){
 6382      decimalMul(pA, pX);
 6383      if( pA->oom ) goto pow2_fault;
 6384    }
 6385    N >>= 1;
 6386    if( N==0 ) break;
 6387    decimalMul(pX, pX);
 6388  }
 6389  decimal_free(pX);
 6390  return pA;
 6391
 6392pow2_fault:
 6393  decimal_free(pA);
 6394  decimal_free(pX);
 6395  return 0;
 6396}
 6397
 6398/*
 6399** Use an IEEE754 binary64 ("double") to generate a new Decimal object.
 6400*/
 6401static Decimal *decimalFromDouble(double r){
 6402  sqlite3_int64 m, a;
 6403  int e;
 6404  int isNeg;
 6405  Decimal *pA;
 6406  Decimal *pX;
 6407  char zNum[100];
 6408  if( r<0.0 ){
 6409    isNeg = 1;
 6410    r = -r;
 6411  }else{
 6412    isNeg = 0;
 6413  }
 6414  memcpy(&a,&r,sizeof(a));
 6415  if( a==0 || a==(sqlite3_int64)0x8000000000000000LL){
 6416    e = 0;
 6417    m = 0;
 6418  }else{
 6419    e = a>>52;
 6420    m = a & ((((sqlite3_int64)1)<<52)-1);
 6421    if( e==0 ){
 6422      m <<= 1;
 6423    }else{
 6424      m |= ((sqlite3_int64)1)<<52;
 6425    }
 6426    while( e<1075 && m>0 && (m&1)==0 ){
 6427      m >>= 1;
 6428      e++;
 6429    }
 6430    if( isNeg ) m = -m;
 6431    e = e - 1075;
 6432    if( e>971 ){
 6433      return 0;  /* A NaN or an Infinity */
 6434    }
 6435  }
 6436
 6437  /* At this point m is the integer significand and e is the exponent */
 6438  sqlite3_snprintf(sizeof(zNum), zNum, "%lld", m);
 6439  pA = decimalNewFromText(zNum, (int)strlen(zNum));
 6440  pX = decimalPow2(e);
 6441  decimalMul(pA, pX);
 6442  decimal_free(pX);
 6443  return pA;
 6444}
 6445
 6446/*
 6447** SQL Function:   decimal(X)
 6448** OR:             decimal_exp(X)
 6449**
 6450** Convert input X into decimal and then back into text.
 6451**
 6452** If X is originally a float, then a full decimal expansion of that floating
 6453** point value is done.  Or if X is an 8-byte blob, it is interpreted
 6454** as a float and similarly expanded.
 6455**
 6456** The decimal_exp(X) function returns the result in exponential notation.
 6457** decimal(X) returns a complete decimal, without the e+NNN at the end.
 6458*/
 6459static void decimalFunc(
 6460  sqlite3_context *context,
 6461  int argc,
 6462  sqlite3_value **argv
 6463){
 6464  Decimal *p =  decimal_new(context, argv[0], 0);
 6465  int N;
 6466  if( argc==2 ){
 6467    N = sqlite3_value_int(argv[1]);
 6468    if( N>0 ) decimal_round(p, N);
 6469  }else{
 6470    N = 0;
 6471  }
 6472  if( p ){
 6473    if( sqlite3_user_data(context)!=0 ){
 6474      decimal_result_sci(context, p, N);
 6475    }else{
 6476      decimal_result(context, p);
 6477    }
 6478    decimal_free(p);
 6479  }
 6480}
 6481
 6482/*
 6483** Compare text in decimal order.
 6484*/
 6485static int decimalCollFunc(
 6486  void *notUsed,
 6487  int nKey1, const void *pKey1,
 6488  int nKey2, const void *pKey2
 6489){
 6490  const unsigned char *zA = (const unsigned char*)pKey1;
 6491  const unsigned char *zB = (const unsigned char*)pKey2;
 6492  Decimal *pA = decimalNewFromText((const char*)zA, nKey1);
 6493  Decimal *pB = decimalNewFromText((const char*)zB, nKey2);
 6494  int rc;
 6495  UNUSED_PARAMETER(notUsed);
 6496  if( pA==0 || pB==0 ){
 6497    rc = 0;
 6498  }else{
 6499    rc = decimal_cmp(pA, pB);
 6500  }
 6501  decimal_free(pA);
 6502  decimal_free(pB);
 6503  return rc;
 6504}
 6505
 6506
 6507/*
 6508** SQL Function:   decimal_add(X, Y)
 6509**                 decimal_sub(X, Y)
 6510**
 6511** Return the sum or difference of X and Y.
 6512*/
 6513static void decimalAddFunc(
 6514  sqlite3_context *context,
 6515  int argc,
 6516  sqlite3_value **argv
 6517){
 6518  Decimal *pA = decimal_new(context, argv[0], 1);
 6519  Decimal *pB = decimal_new(context, argv[1], 1);
 6520  UNUSED_PARAMETER(argc);
 6521  decimal_add(pA, pB);
 6522  decimal_result(context, pA);
 6523  decimal_free(pA);
 6524  decimal_free(pB);
 6525}
 6526static void decimalSubFunc(
 6527  sqlite3_context *context,
 6528  int argc,
 6529  sqlite3_value **argv
 6530){
 6531  Decimal *pA = decimal_new(context, argv[0], 1);
 6532  Decimal *pB = decimal_new(context, argv[1], 1);
 6533  UNUSED_PARAMETER(argc);
 6534  if( pB ){
 6535    pB->sign = !pB->sign;
 6536    decimal_add(pA, pB);
 6537    decimal_result(context, pA);
 6538  }
 6539  decimal_free(pA);
 6540  decimal_free(pB);
 6541}
 6542
 6543/* Aggregate function:   decimal_sum(X)
 6544**
 6545** Works like sum() except that it uses decimal arithmetic for unlimited
 6546** precision.
 6547*/
 6548static void decimalSumStep(
 6549  sqlite3_context *context,
 6550  int argc,
 6551  sqlite3_value **argv
 6552){
 6553  Decimal *p;
 6554  Decimal *pArg;
 6555  UNUSED_PARAMETER(argc);
 6556  p = sqlite3_aggregate_context(context, sizeof(*p));
 6557  if( p==0 ) return;
 6558  if( !p->isInit ){
 6559    p->isInit = 1;
 6560    p->a = sqlite3_malloc64(2);
 6561    if( p->a==0 ){
 6562      p->oom = 1;
 6563    }else{
 6564      p->a[0] = 0;
 6565    }
 6566    p->nDigit = 1;
 6567    p->nFrac = 0;
 6568  }
 6569  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
 6570  pArg = decimal_new(context, argv[0], 1);
 6571  decimal_add(p, pArg);
 6572  decimal_free(pArg);
 6573}
 6574static void decimalSumInverse(
 6575  sqlite3_context *context,
 6576  int argc,
 6577  sqlite3_value **argv
 6578){
 6579  Decimal *p;
 6580  Decimal *pArg;
 6581  UNUSED_PARAMETER(argc);
 6582  p = sqlite3_aggregate_context(context, sizeof(*p));
 6583  if( p==0 ) return;
 6584  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
 6585  pArg = decimal_new(context, argv[0], 1);
 6586  if( pArg ) pArg->sign = !pArg->sign;
 6587  decimal_add(p, pArg);
 6588  decimal_free(pArg);
 6589}
 6590static void decimalSumValue(sqlite3_context *context){
 6591  Decimal *p = sqlite3_aggregate_context(context, 0);
 6592  if( p==0 ) return;
 6593  decimal_result(context, p);
 6594}
 6595static void decimalSumFinalize(sqlite3_context *context){
 6596  Decimal *p = sqlite3_aggregate_context(context, 0);
 6597  if( p==0 ) return;
 6598  decimal_result(context, p);
 6599  decimal_clear(p);
 6600}
 6601
 6602/*
 6603** SQL Function:   decimal_mul(X, Y)
 6604**
 6605** Return the product of X and Y.
 6606*/
 6607static void decimalMulFunc(
 6608  sqlite3_context *context,
 6609  int argc,
 6610  sqlite3_value **argv
 6611){
 6612  Decimal *pA = decimal_new(context, argv[0], 1);
 6613  Decimal *pB = decimal_new(context, argv[1], 1);
 6614  UNUSED_PARAMETER(argc);
 6615  if( pA==0 || pA->oom || pA->isNull
 6616   || pB==0 || pB->oom || pB->isNull 
 6617  ){
 6618    goto mul_end;
 6619  }
 6620  decimalMul(pA, pB);
 6621  if( pA->oom ){
 6622    goto mul_end;
 6623  }
 6624  decimal_result(context, pA);
 6625
 6626mul_end:
 6627  decimal_free(pA);
 6628  decimal_free(pB);
 6629}
 6630
 6631/*
 6632** SQL Function:   decimal_pow2(N)
 6633**
 6634** Return the N-th power of 2.  N must be an integer.
 6635*/
 6636static void decimalPow2Func(
 6637  sqlite3_context *context,
 6638  int argc,
 6639  sqlite3_value **argv
 6640){
 6641  UNUSED_PARAMETER(argc);
 6642  if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){
 6643    Decimal *pA = decimalPow2(sqlite3_value_int(argv[0]));
 6644    decimal_result_sci(context, pA, 0);
 6645    decimal_free(pA);
 6646  }
 6647}
 6648
 6649#ifdef _WIN32
 6650
 6651#endif
 6652int sqlite3_decimal_init(
 6653  sqlite3 *db, 
 6654  char **pzErrMsg, 
 6655  const sqlite3_api_routines *pApi
 6656){
 6657  int rc = SQLITE_OK;
 6658  static const struct {
 6659    const char *zFuncName;
 6660    int nArg;
 6661    int iArg;
 6662    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
 6663  } aFunc[] = {
 6664    { "decimal",       1, 0,  decimalFunc        },
 6665    { "decimal",       2, 0,  decimalFunc        },
 6666    { "decimal_exp",   1, 1,  decimalFunc        },
 6667    { "decimal_exp",   2, 1,  decimalFunc        },
 6668    { "decimal_cmp",   2, 0,  decimalCmpFunc     },
 6669    { "decimal_add",   2, 0,  decimalAddFunc     },
 6670    { "decimal_sub",   2, 0,  decimalSubFunc     },
 6671    { "decimal_mul",   2, 0,  decimalMulFunc     },
 6672    { "decimal_pow2",  1, 0,  decimalPow2Func    },
 6673  };
 6674  unsigned int i;
 6675  (void)pzErrMsg;  /* Unused parameter */
 6676
 6677  SQLITE_EXTENSION_INIT2(pApi);
 6678
 6679  for(i=0; i<(int)(sizeof(aFunc)/sizeof(aFunc[0])) && rc==SQLITE_OK; i++){
 6680    rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
 6681                   SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
 6682                   aFunc[i].iArg ? db : 0, aFunc[i].xFunc, 0, 0);
 6683  }
 6684  if( rc==SQLITE_OK ){
 6685    rc = sqlite3_create_window_function(db, "decimal_sum", 1,
 6686                   SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
 6687                   decimalSumStep, decimalSumFinalize,
 6688                   decimalSumValue, decimalSumInverse, 0);
 6689  }
 6690  if( rc==SQLITE_OK ){
 6691    rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8,
 6692                                  0, decimalCollFunc);
 6693  }
 6694  return rc;
 6695}
 6696
 6697/************************* End ext/misc/decimal.c ********************/
 6698/************************* Begin ext/misc/base64.c ******************/
 6699/*
 6700** 2022-11-18
 6701**
 6702** The author disclaims copyright to this source code.  In place of
 6703** a legal notice, here is a blessing:
 6704**
 6705**    May you do good and not evil.
 6706**    May you find forgiveness for yourself and forgive others.
 6707**    May you share freely, never taking more than you give.
 6708**
 6709*************************************************************************
 6710**
 6711** This is a SQLite extension for converting in either direction
 6712** between a (binary) blob and base64 text. Base64 can transit a
 6713** sane USASCII channel unmolested. It also plays nicely in CSV or
 6714** written as TCL brace-enclosed literals or SQL string literals,
 6715** and can be used unmodified in XML-like documents.
 6716**
 6717** This is an independent implementation of conversions specified in
 6718** RFC 4648, done on the above date by the author (Larry Brasfield)
 6719** who thereby has the right to put this into the public domain.
 6720**
 6721** The conversions meet RFC 4648 requirements, provided that this
 6722** C source specifies that line-feeds are included in the encoded
 6723** data to limit visible line lengths to 72 characters and to
 6724** terminate any encoded blob having non-zero length.
 6725**
 6726** Length limitations are not imposed except that the runtime
 6727** SQLite string or blob length limits are respected. Otherwise,
 6728** any length binary sequence can be represented and recovered.
 6729** Generated base64 sequences, with their line-feeds included,
 6730** can be concatenated; the result converted back to binary will
 6731** be the concatenation of the represented binary sequences.
 6732**
 6733** This SQLite3 extension creates a function, base64(x), which
 6734** either: converts text x containing base64 to a returned blob;
 6735** or converts a blob x to returned text containing base64. An
 6736** error will be thrown for other input argument types.
 6737**
 6738** This code relies on UTF-8 encoding only with respect to the
 6739** meaning of the first 128 (7-bit) codes matching that of USASCII.
 6740** It will fail miserably if somehow made to try to convert EBCDIC.
 6741** Because it is table-driven, it could be enhanced to handle that,
 6742** but the world and SQLite have moved on from that anachronism.
 6743**
 6744** To build the extension:
 6745** Set shell variable SQDIR=<your favorite SQLite checkout directory>
 6746** *Nix: gcc -O2 -shared -I$SQDIR -fPIC -o base64.so base64.c
 6747** OSX: gcc -O2 -dynamiclib -fPIC -I$SQDIR -o base64.dylib base64.c
 6748** Win32: gcc -O2 -shared -I%SQDIR% -o base64.dll base64.c
 6749** Win32: cl /Os -I%SQDIR% base64.c -link -dll -out:base64.dll
 6750*/
 6751
 6752#include <assert.h>
 6753
 6754/* #include "sqlite3ext.h" */
 6755
 6756#ifndef deliberate_fall_through
 6757/* Quiet some compilers about some of our intentional code. */
 6758# if GCC_VERSION>=7000000
 6759#  define deliberate_fall_through __attribute__((fallthrough));
 6760# else
 6761#  define deliberate_fall_through
 6762# endif
 6763#endif
 6764
 6765SQLITE_EXTENSION_INIT1;
 6766
 6767#define PC 0x80 /* pad character */
 6768#define WS 0x81 /* whitespace */
 6769#define ND 0x82 /* Not above or digit-value */
 6770#define PAD_CHAR '='
 6771
 6772#ifndef U8_TYPEDEF
 6773/* typedef unsigned char u8; */
 6774#define U8_TYPEDEF
 6775#endif
 6776
 6777/* Decoding table, ASCII (7-bit) value to base 64 digit value or other */
 6778static const u8 b64DigitValues[128] = {
 6779  /*                             HT LF VT  FF CR       */
 6780    ND,ND,ND,ND, ND,ND,ND,ND, ND,WS,WS,WS, WS,WS,ND,ND,
 6781  /*                                                US */
 6782    ND,ND,ND,ND, ND,ND,ND,ND, ND,ND,ND,ND, ND,ND,ND,ND,
 6783  /*sp                                  +            / */
 6784    WS,ND,ND,ND, ND,ND,ND,ND, ND,ND,ND,62, ND,ND,ND,63,
 6785  /* 0  1            5            9            =       */
 6786    52,53,54,55, 56,57,58,59, 60,61,ND,ND, ND,PC,ND,ND,
 6787  /*    A                                            O */
 6788    ND, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
 6789  /* P                               Z                 */
 6790    15,16,17,18, 19,20,21,22, 23,24,25,ND, ND,ND,ND,ND,
 6791  /*    a                                            o */
 6792    ND,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
 6793  /* p                               z                 */
 6794    41,42,43,44, 45,46,47,48, 49,50,51,ND, ND,ND,ND,ND
 6795};
 6796
 6797static const char b64Numerals[64+1]
 6798= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 6799
 6800#define BX_DV_PROTO(c) \
 6801  ((((u8)(c))<0x80)? (u8)(b64DigitValues[(u8)(c)]) : 0x80)
 6802#define IS_BX_DIGIT(bdp) (((u8)(bdp))<0x80)
 6803#define IS_BX_WS(bdp) ((bdp)==WS)
 6804#define IS_BX_PAD(bdp) ((bdp)==PC)
 6805#define BX_NUMERAL(dv) (b64Numerals[(u8)(dv)])
 6806/* Width of base64 lines. Should be an integer multiple of 4. */
 6807#define B64_DARK_MAX 72
 6808
 6809/* Encode a byte buffer into base64 text with linefeeds appended to limit
 6810** encoded group lengths to B64_DARK_MAX or to terminate the last group.
 6811*/
 6812static char* toBase64( u8 *pIn, int nbIn, char *pOut ){
 6813  int nCol = 0;
 6814  while( nbIn >= 3 ){
 6815    /* Do the bit-shuffle, exploiting unsigned input to avoid masking. */
 6816    pOut[0] = BX_NUMERAL(pIn[0]>>2);
 6817    pOut[1] = BX_NUMERAL(((pIn[0]<<4)|(pIn[1]>>4))&0x3f);
 6818    pOut[2] = BX_NUMERAL(((pIn[1]&0xf)<<2)|(pIn[2]>>6));
 6819    pOut[3] = BX_NUMERAL(pIn[2]&0x3f);
 6820    pOut += 4;
 6821    nbIn -= 3;
 6822    pIn += 3;
 6823    if( (nCol += 4)>=B64_DARK_MAX || nbIn<=0 ){
 6824      *pOut++ = '\n';
 6825      nCol = 0;
 6826    }
 6827  }
 6828  if( nbIn > 0 ){
 6829    signed char nco = nbIn+1;
 6830    int nbe;
 6831    unsigned long qv = *pIn++;
 6832    for( nbe=1; nbe<3; ++nbe ){
 6833      qv <<= 8;
 6834      if( nbe<nbIn ) qv |= *pIn++;
 6835    }
 6836    for( nbe=3; nbe>=0; --nbe ){
 6837      char ce = (nbe<nco)? BX_NUMERAL((u8)(qv & 0x3f)) : PAD_CHAR;
 6838      qv >>= 6;
 6839      pOut[nbe] = ce;
 6840    }
 6841    pOut += 4;
 6842    *pOut++ = '\n';
 6843  }
 6844  *pOut = 0;
 6845  return pOut;
 6846}
 6847
 6848/* Skip over text which is not base64 numeral(s). */
 6849static char * skipNonB64( char *s, int nc ){
 6850  char c;
 6851  while( nc-- > 0 && (c = *s) && !IS_BX_DIGIT(BX_DV_PROTO(c)) ) ++s;
 6852  return s;
 6853}
 6854
 6855/* Decode base64 text into a byte buffer. */
 6856static u8* fromBase64( char *pIn, int ncIn, u8 *pOut ){
 6857  if( ncIn>0 && pIn[ncIn-1]=='\n' ) --ncIn;
 6858  while( ncIn>0 && *pIn!=PAD_CHAR ){
 6859    static signed char nboi[] = { 0, 0, 1, 2, 3 };
 6860    char *pUse = skipNonB64(pIn, ncIn);
 6861    unsigned long qv = 0L;
 6862    int nti, nbo, nac;
 6863    ncIn -= (pUse - pIn);
 6864    pIn = pUse;
 6865    nti = (ncIn>4)? 4 : ncIn;
 6866    ncIn -= nti;
 6867    nbo = nboi[nti];
 6868    if( nbo==0 ) break;
 6869    for( nac=0; nac<4; ++nac ){
 6870      char c = (nac<nti)? *pIn++ : b64Numerals[0];
 6871      u8 bdp = BX_DV_PROTO(c);
 6872      switch( bdp ){
 6873      case ND:
 6874        /*  Treat dark non-digits as pad, but they terminate decode too. */
 6875        ncIn = 0;
 6876        deliberate_fall_through; /* FALLTHRU */
 6877      case WS:
 6878        /* Treat whitespace as pad and terminate this group.*/
 6879        nti = nac;
 6880        deliberate_fall_through; /* FALLTHRU */
 6881      case PC:
 6882        bdp = 0;
 6883        --nbo;
 6884        deliberate_fall_through; /* FALLTHRU */
 6885      default: /* bdp is the digit value. */
 6886        qv = qv<<6 | bdp;
 6887        break;
 6888      }
 6889    }
 6890    switch( nbo ){
 6891    case 3:
 6892      pOut[2] = (qv) & 0xff;
 6893      deliberate_fall_through; /* FALLTHRU */
 6894    case 2:
 6895      pOut[1] = (qv>>8) & 0xff;
 6896      deliberate_fall_through; /* FALLTHRU */
 6897    case 1:
 6898      pOut[0] = (qv>>16) & 0xff;
 6899      break;
 6900    }
 6901    pOut += nbo;
 6902  }
 6903  return pOut;
 6904}
 6905
 6906/* This function does the work for the SQLite base64(x) UDF. */
 6907static void base64(sqlite3_context *context, int na, sqlite3_value *av[]){
 6908  sqlite3_int64 nb;
 6909  sqlite3_int64 nv = sqlite3_value_bytes(av[0]);
 6910  sqlite3_int64 nc;
 6911  int nvMax = sqlite3_limit(sqlite3_context_db_handle(context),
 6912                            SQLITE_LIMIT_LENGTH, -1);
 6913  char *cBuf;
 6914  u8 *bBuf;
 6915  assert(na==1);
 6916  switch( sqlite3_value_type(av[0]) ){
 6917  case SQLITE_BLOB:
 6918    nb = nv;
 6919    nc = 4*((nv+2)/3); /* quads needed */
 6920    nc += (nc+(B64_DARK_MAX-1))/B64_DARK_MAX + 1; /* LFs and a 0-terminator */
 6921    if( nvMax < nc ){
 6922      sqlite3_result_error(context, "blob expanded to base64 too big", -1);
 6923      return;
 6924    }
 6925    bBuf = (u8*)sqlite3_value_blob(av[0]);
 6926    if( !bBuf ){
 6927      if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){
 6928        goto memFail;
 6929      }
 6930      sqlite3_result_text(context,"",-1,SQLITE_STATIC);
 6931      break;
 6932    }
 6933    cBuf = sqlite3_malloc64(nc);
 6934    if( !cBuf ) goto memFail;
 6935    nc = (int)(toBase64(bBuf, nb, cBuf) - cBuf);
 6936    sqlite3_result_text(context, cBuf, nc, sqlite3_free);
 6937    break;
 6938  case SQLITE_TEXT:
 6939    nc = nv;
 6940    nb = 3*((nv+3)/4); /* may overestimate due to LF and padding */
 6941    if( nvMax < nb ){
 6942      sqlite3_result_error(context, "blob from base64 may be too big", -1);
 6943      return;
 6944    }else if( nb<1 ){
 6945      nb = 1;
 6946    }
 6947    cBuf = (char *)sqlite3_value_text(av[0]);
 6948    if( !cBuf ){
 6949      if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){
 6950        goto memFail;
 6951      }
 6952      sqlite3_result_zeroblob(context, 0);
 6953      break;
 6954    }
 6955    bBuf = sqlite3_malloc64(nb);
 6956    if( !bBuf ) goto memFail;
 6957    nb = (int)(fromBase64(cBuf, nc, bBuf) - bBuf);
 6958    sqlite3_result_blob(context, bBuf, nb, sqlite3_free);
 6959    break;
 6960  default:
 6961    sqlite3_result_error(context, "base64 accepts only blob or text", -1);
 6962    return;
 6963  }
 6964  return;
 6965 memFail:
 6966  sqlite3_result_error(context, "base64 OOM", -1);
 6967}
 6968
 6969/*
 6970** Establish linkage to running SQLite library.
 6971*/
 6972#ifndef SQLITE_SHELL_EXTFUNCS
 6973#ifdef _WIN32
 6974
 6975#endif
 6976int sqlite3_base64_init
 6977#else
 6978static int sqlite3_base64_init
 6979#endif
 6980(sqlite3 *db, char **pzErr, const sqlite3_api_routines *pApi){
 6981  SQLITE_EXTENSION_INIT2(pApi);
 6982  (void)pzErr;
 6983  return sqlite3_create_function
 6984    (db, "base64", 1,
 6985     SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS|SQLITE_DIRECTONLY|SQLITE_UTF8,
 6986     0, base64, 0, 0);
 6987}
 6988
 6989/*
 6990** Define some macros to allow this extension to be built into the shell
 6991** conveniently, in conjunction with use of SQLITE_SHELL_EXTFUNCS. This
 6992** allows shell.c, as distributed, to have this extension built in.
 6993*/
 6994#define BASE64_INIT(db) sqlite3_base64_init(db, 0, 0)
 6995#define BASE64_EXPOSE(db, pzErr) /* Not needed, ..._init() does this. */
 6996
 6997/************************* End ext/misc/base64.c ********************/
 6998/************************* Begin ext/misc/base85.c ******************/
 6999/*
 7000** 2022-11-16
 7001**
 7002** The author disclaims copyright to this source code.  In place of
 7003** a legal notice, here is a blessing:
 7004**
 7005**    May you do good and not evil.
 7006**    May you find forgiveness for yourself and forgive others.
 7007**    May you share freely, never taking more than you give.
 7008**
 7009*************************************************************************
 7010**
 7011** This is a utility for converting binary to base85 or vice-versa.
 7012** It can be built as a standalone program or an SQLite3 extension.
 7013**
 7014** Much like base64 representations, base85 can be sent through a
 7015** sane USASCII channel unmolested. It also plays nicely in CSV or
 7016** written as TCL brace-enclosed literals or SQL string literals.
 7017** It is not suited for unmodified use in XML-like documents.
 7018**
 7019** The encoding used resembles Ascii85, but was devised by the author
 7020** (Larry Brasfield) before Mozilla, Adobe, ZMODEM or other Ascii85
 7021** variant sources existed, in the 1984 timeframe on a VAX mainframe.
 7022** Further, this is an independent implementation of a base85 system.
 7023** Hence, the author has rightfully put this into the public domain.
 7024**
 7025** Base85 numerals are taken from the set of 7-bit USASCII codes,
 7026** excluding control characters and Space ! " ' ( ) { | } ~ Del
 7027** in code order representing digit values 0 to 84 (base 10.)
 7028**
 7029** Groups of 4 bytes, interpreted as big-endian 32-bit values,
 7030** are represented as 5-digit base85 numbers with MS to LS digit
 7031** order. Groups of 1-3 bytes are represented with 2-4 digits,
 7032** still big-endian but 8-24 bit values. (Using big-endian yields
 7033** the simplest transition to byte groups smaller than 4 bytes.
 7034** These byte groups can also be considered base-256 numbers.)
 7035** Groups of 0 bytes are represented with 0 digits and vice-versa.
 7036** No pad characters are used; Encoded base85 numeral sequence
 7037** (aka "group") length maps 1-to-1 to the decoded binary length.
 7038**
 7039** Any character not in the base85 numeral set delimits groups.
 7040** When base85 is streamed or stored in containers of indefinite
 7041** size, newline is used to separate it into sub-sequences of no
 7042** more than 80 digits so that fgets() can be used to read it.
 7043**
 7044** Length limitations are not imposed except that the runtime
 7045** SQLite string or blob length limits are respected. Otherwise,
 7046** any length binary sequence can be represented and recovered.
 7047** Base85 sequences can be concatenated by separating them with
 7048** a non-base85 character; the conversion to binary will then
 7049** be the concatenation of the represented binary sequences.
 7050
 7051** The standalone program either converts base85 on stdin to create
 7052** a binary file or converts a binary file to base85 on stdout.
 7053** Read or make it blurt its help for invocation details.
 7054**
 7055** The SQLite3 extension creates a function, base85(x), which will
 7056** either convert text base85 to a blob or a blob to text base85
 7057** and return the result (or throw an error for other types.)
 7058** Unless built with OMIT_BASE85_CHECKER defined, it also creates a
 7059** function, is_base85(t), which returns 1 iff the text t contains
 7060** nothing other than base85 numerals and whitespace, or 0 otherwise.
 7061**
 7062** To build the extension:
 7063** Set shell variable SQDIR=<your favorite SQLite checkout directory>
 7064** and variable OPTS to -DOMIT_BASE85_CHECKER if is_base85() unwanted.
 7065** *Nix: gcc -O2 -shared -I$SQDIR $OPTS -fPIC -o base85.so base85.c
 7066** OSX: gcc -O2 -dynamiclib -fPIC -I$SQDIR $OPTS -o base85.dylib base85.c
 7067** Win32: gcc -O2 -shared -I%SQDIR% %OPTS% -o base85.dll base85.c
 7068** Win32: cl /Os -I%SQDIR% %OPTS% base85.c -link -dll -out:base85.dll
 7069**
 7070** To build the standalone program, define PP symbol BASE85_STANDALONE. Eg.
 7071** *Nix or OSX: gcc -O2 -DBASE85_STANDALONE base85.c -o base85
 7072** Win32: gcc -O2 -DBASE85_STANDALONE -o base85.exe base85.c
 7073** Win32: cl /Os /MD -DBASE85_STANDALONE base85.c
 7074*/
 7075
 7076#include <stdio.h>
 7077#include <memory.h>
 7078#include <string.h>
 7079#include <assert.h>
 7080#ifndef OMIT_BASE85_CHECKER
 7081# include <ctype.h>
 7082#endif
 7083
 7084#ifndef BASE85_STANDALONE
 7085
 7086/* # include "sqlite3ext.h" */
 7087
 7088SQLITE_EXTENSION_INIT1;
 7089
 7090#else
 7091
 7092# ifdef _WIN32
 7093#  include <io.h>
 7094#  include <fcntl.h>
 7095# else
 7096#  define setmode(fd,m)
 7097# endif
 7098
 7099static char *zHelp =
 7100  "Usage: base85 <dirFlag> <binFile>\n"
 7101  " <dirFlag> is either -r to read or -w to write <binFile>,\n"
 7102  "   content to be converted to/from base85 on stdout/stdin.\n"
 7103  " <binFile> names a binary file to be rendered or created.\n"
 7104  "   Or, the name '-' refers to the stdin or stdout stream.\n"
 7105  ;
 7106
 7107static void sayHelp(){
 7108  printf("%s", zHelp);
 7109}
 7110#endif
 7111
 7112#ifndef U8_TYPEDEF
 7113/* typedef unsigned char u8; */
 7114#define U8_TYPEDEF
 7115#endif
 7116
 7117/* Classify c according to interval within USASCII set w.r.t. base85
 7118 * Values of 1 and 3 are base85 numerals. Values of 0, 2, or 4 are not.
 7119 */
 7120#define B85_CLASS( c ) (((c)>='#')+((c)>'&')+((c)>='*')+((c)>'z'))
 7121
 7122/* Provide digitValue to b85Numeral offset as a function of above class. */
 7123static u8 b85_cOffset[] = { 0, '#', 0, '*'-4, 0 };
 7124#define B85_DNOS( c ) b85_cOffset[B85_CLASS(c)]
 7125
 7126/* Say whether c is a base85 numeral. */
 7127#define IS_B85( c ) (B85_CLASS(c) & 1)
 7128
 7129#if 0 /* Not used, */
 7130static u8 base85DigitValue( char c ){
 7131  u8 dv = (u8)(c - '#');
 7132  if( dv>87 ) return 0xff;
 7133  return (dv > 3)? dv-3 : dv;
 7134}
 7135#endif
 7136
 7137/* Width of base64 lines. Should be an integer multiple of 5. */
 7138#define B85_DARK_MAX 80
 7139
 7140
 7141static char * skipNonB85( char *s, int nc ){
 7142  char c;
 7143  while( nc-- > 0 && (c = *s) && !IS_B85(c) ) ++s;
 7144  return s;
 7145}
 7146
 7147/* Convert small integer, known to be in 0..84 inclusive, to base85 numeral.
 7148 * Do not use the macro form with argument expression having a side-effect.*/
 7149#if 0
 7150static char base85Numeral( u8 b ){
 7151  return (b < 4)? (char)(b + '#') : (char)(b - 4 + '*');
 7152}
 7153#else
 7154# define base85Numeral( dn )\
 7155  ((char)(((dn) < 4)? (char)((dn) + '#') : (char)((dn) - 4 + '*')))
 7156#endif
 7157
 7158static char *putcs(char *pc, char *s){
 7159  char c;
 7160  while( (c = *s++)!=0 ) *pc++ = c;
 7161  return pc;
 7162}
 7163
 7164/* Encode a byte buffer into base85 text. If pSep!=0, it's a C string
 7165** to be appended to encoded groups to limit their length to B85_DARK_MAX
 7166** or to terminate the last group (to aid concatenation.)
 7167*/
 7168static char* toBase85( u8 *pIn, int nbIn, char *pOut, char *pSep ){
 7169  int nCol = 0;
 7170  while( nbIn >= 4 ){
 7171    int nco = 5;
 7172    unsigned long qbv = (((unsigned long)pIn[0])<<24) |
 7173                        (pIn[1]<<16) | (pIn[2]<<8) | pIn[3];
 7174    while( nco > 0 ){
 7175      unsigned nqv = (unsigned)(qbv/85UL);
 7176      unsigned char dv = qbv - 85UL*nqv;
 7177      qbv = nqv;
 7178      pOut[--nco] = base85Numeral(dv);
 7179    }
 7180    nbIn -= 4;
 7181    pIn += 4;
 7182    pOut += 5;
 7183    if( pSep && (nCol += 5)>=B85_DARK_MAX ){
 7184      pOut = putcs(pOut, pSep);
 7185      nCol = 0;
 7186    }
 7187  }
 7188  if( nbIn > 0 ){
 7189    int nco = nbIn + 1;
 7190    unsigned long qv = *pIn++;
 7191    int nbe = 1;
 7192    while( nbe++ < nbIn ){
 7193      qv = (qv<<8) | *pIn++;
 7194    }
 7195    nCol += nco;
 7196    while( nco > 0 ){
 7197      u8 dv = (u8)(qv % 85);
 7198      qv /= 85;
 7199      pOut[--nco] = base85Numeral(dv);
 7200    }
 7201    pOut += (nbIn+1);
 7202  }
 7203  if( pSep && nCol>0 ) pOut = putcs(pOut, pSep);
 7204  *pOut = 0;
 7205  return pOut;
 7206}
 7207
 7208/* Decode base85 text into a byte buffer. */
 7209static u8* fromBase85( char *pIn, int ncIn, u8 *pOut ){
 7210  if( ncIn>0 && pIn[ncIn-1]=='\n' ) --ncIn;
 7211  while( ncIn>0 ){
 7212    static signed char nboi[] = { 0, 0, 1, 2, 3, 4 };
 7213    char *pUse = skipNonB85(pIn, ncIn);
 7214    unsigned long qv = 0L;
 7215    int nti, nbo;
 7216    ncIn -= (pUse - pIn);
 7217    pIn = pUse;
 7218    nti = (ncIn>5)? 5 : ncIn;
 7219    nbo = nboi[nti];
 7220    if( nbo==0 ) break;
 7221    while( nti>0 ){
 7222      char c = *pIn++;
 7223      u8 cdo = B85_DNOS(c);
 7224      --ncIn;
 7225      if( cdo==0 ) break;
 7226      qv = 85 * qv + (c - cdo);
 7227      --nti;
 7228    }
 7229    nbo -= nti; /* Adjust for early (non-digit) end of group. */
 7230    switch( nbo ){
 7231    case 4:
 7232      *pOut++ = (qv >> 24)&0xff;
 7233      /* FALLTHRU */
 7234    case 3:
 7235      *pOut++ = (qv >> 16)&0xff;
 7236      /* FALLTHRU */
 7237    case 2:
 7238      *pOut++ = (qv >> 8)&0xff;
 7239      /* FALLTHRU */
 7240    case 1:
 7241      *pOut++ = qv&0xff;
 7242      /* FALLTHRU */
 7243    case 0:
 7244      break;
 7245    }
 7246  }
 7247  return pOut;
 7248}
 7249
 7250#ifndef OMIT_BASE85_CHECKER
 7251/* Say whether input char sequence is all (base85 and/or whitespace).*/
 7252static int allBase85( char *p, int len ){
 7253  char c;
 7254  while( len-- > 0 && (c = *p++) != 0 ){
 7255    if( !IS_B85(c) && !isspace(c) ) return 0;
 7256  }
 7257  return 1;
 7258}
 7259#endif
 7260
 7261#ifndef BASE85_STANDALONE
 7262
 7263#ifndef OMIT_BASE85_CHECKER
 7264/* This function does the work for the SQLite is_base85(t) UDF. */
 7265static void is_base85(sqlite3_context *context, int na, sqlite3_value *av[]){
 7266  assert(na==1);
 7267  switch( sqlite3_value_type(av[0]) ){
 7268  case SQLITE_TEXT:
 7269    {
 7270      int rv = allBase85( (char *)sqlite3_value_text(av[0]),
 7271                          sqlite3_value_bytes(av[0]) );
 7272      sqlite3_result_int(context, rv);
 7273    }
 7274    break;
 7275  case SQLITE_NULL:
 7276    sqlite3_result_null(context);
 7277    break;
 7278  default:
 7279    sqlite3_result_error(context, "is_base85 accepts only text or NULL", -1);
 7280    return;
 7281  }
 7282}
 7283#endif
 7284
 7285/* This function does the work for the SQLite base85(x) UDF. */
 7286static void base85(sqlite3_context *context, int na, sqlite3_value *av[]){
 7287  sqlite3_int64 nb, nc, nv = sqlite3_value_bytes(av[0]);
 7288  int nvMax = sqlite3_limit(sqlite3_context_db_handle(context),
 7289                            SQLITE_LIMIT_LENGTH, -1);
 7290  char *cBuf;
 7291  u8 *bBuf;
 7292  assert(na==1);
 7293  switch( sqlite3_value_type(av[0]) ){
 7294  case SQLITE_BLOB:
 7295    nb = nv;
 7296    /*    ulongs    tail   newlines  tailenc+nul*/
 7297    nc = 5*(nv/4) + nv%4 + nv/64+1 + 2;
 7298    if( nvMax < nc ){
 7299      sqlite3_result_error(context, "blob expanded to base85 too big", -1);
 7300      return;
 7301    }
 7302    bBuf = (u8*)sqlite3_value_blob(av[0]);
 7303    if( !bBuf ){
 7304      if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){
 7305        goto memFail;
 7306      }
 7307      sqlite3_result_text(context,"",-1,SQLITE_STATIC);
 7308      break;
 7309    }
 7310    cBuf = sqlite3_malloc64(nc);
 7311    if( !cBuf ) goto memFail;
 7312    nc = (int)(toBase85(bBuf, nb, cBuf, "\n") - cBuf);
 7313    sqlite3_result_text(context, cBuf, nc, sqlite3_free);
 7314    break;
 7315  case SQLITE_TEXT:
 7316    nc = nv;
 7317    nb = 4*(nv/5) + nv%5; /* may overestimate */
 7318    if( nvMax < nb ){
 7319      sqlite3_result_error(context, "blob from base85 may be too big", -1);
 7320      return;
 7321    }else if( nb<1 ){
 7322      nb = 1;
 7323    }
 7324    cBuf = (char *)sqlite3_value_text(av[0]);
 7325    if( !cBuf ){
 7326      if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){
 7327        goto memFail;
 7328      }
 7329      sqlite3_result_zeroblob(context, 0);
 7330      break;
 7331    }
 7332    bBuf = sqlite3_malloc64(nb);
 7333    if( !bBuf ) goto memFail;
 7334    nb = (int)(fromBase85(cBuf, nc, bBuf) - bBuf);
 7335    sqlite3_result_blob(context, bBuf, nb, sqlite3_free);
 7336    break;
 7337  default:
 7338    sqlite3_result_error(context, "base85 accepts only blob or text.", -1);
 7339    return;
 7340  }
 7341  return;
 7342 memFail:
 7343  sqlite3_result_error(context, "base85 OOM", -1);
 7344}
 7345
 7346/*
 7347** Establish linkage to running SQLite library.
 7348*/
 7349#ifndef SQLITE_SHELL_EXTFUNCS
 7350#ifdef _WIN32
 7351
 7352#endif
 7353int sqlite3_base85_init
 7354#else
 7355static int sqlite3_base85_init
 7356#endif
 7357(sqlite3 *db, char **pzErr, const sqlite3_api_routines *pApi){
 7358  SQLITE_EXTENSION_INIT2(pApi);
 7359  (void)pzErr;
 7360#ifndef OMIT_BASE85_CHECKER
 7361  {
 7362    int rc = sqlite3_create_function
 7363      (db, "is_base85", 1,
 7364       SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS|SQLITE_UTF8,
 7365       0, is_base85, 0, 0);
 7366    if( rc!=SQLITE_OK ) return rc;
 7367  }
 7368#endif
 7369  return sqlite3_create_function
 7370    (db, "base85", 1,
 7371     SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS|SQLITE_DIRECTONLY|SQLITE_UTF8,
 7372     0, base85, 0, 0);
 7373}
 7374
 7375/*
 7376** Define some macros to allow this extension to be built into the shell
 7377** conveniently, in conjunction with use of SQLITE_SHELL_EXTFUNCS. This
 7378** allows shell.c, as distributed, to have this extension built in.
 7379*/
 7380# define BASE85_INIT(db) sqlite3_base85_init(db, 0, 0)
 7381# define BASE85_EXPOSE(db, pzErr) /* Not needed, ..._init() does this. */
 7382
 7383#else /* standalone program */
 7384
 7385int main(int na, char *av[]){
 7386  int cin;
 7387  int rc = 0;
 7388  u8 bBuf[4*(B85_DARK_MAX/5)];
 7389  char cBuf[5*(sizeof(bBuf)/4)+2];
 7390  size_t nio;
 7391# ifndef OMIT_BASE85_CHECKER
 7392  int b85Clean = 1;
 7393# endif
 7394  char rw;
 7395  FILE *fb = 0, *foc = 0;
 7396  char fmode[3] = "xb";
 7397  if( na < 3 || av[1][0]!='-' || (rw = av[1][1])==0 || (rw!='r' && rw!='w') ){
 7398    sayHelp();
 7399    return 0;
 7400  }
 7401  fmode[0] = rw;
 7402  if( av[2][0]=='-' && av[2][1]==0 ){
 7403    switch( rw ){
 7404    case 'r':
 7405      fb = stdin;
 7406      setmode(fileno(stdin), O_BINARY);
 7407      break;
 7408    case 'w':
 7409      fb = stdout;
 7410      setmode(fileno(stdout), O_BINARY);
 7411      break;
 7412    }
 7413  }else{
 7414    fb = fopen(av[2], fmode);
 7415    foc = fb;
 7416  }
 7417  if( !fb ){
 7418    fprintf(stderr, "Cannot open %s for %c\n", av[2], rw);
 7419    rc = 1;
 7420  }else{
 7421    switch( rw ){
 7422    case 'r':
 7423      while( (nio = fread( bBuf, 1, sizeof(bBuf), fb))>0 ){
 7424        toBase85( bBuf, (int)nio, cBuf, 0 );
 7425        fprintf(stdout, "%s\n", cBuf);
 7426      }
 7427      break;
 7428    case 'w':
 7429      while( 0 != fgets(cBuf, sizeof(cBuf), stdin) ){
 7430        int nc = strlen(cBuf);
 7431        size_t nbo = fromBase85( cBuf, nc, bBuf ) - bBuf;
 7432        if( 1 != fwrite(bBuf, nbo, 1, fb) ) rc = 1;
 7433#ifndef OMIT_BASE85_CHECKER
 7434        b85Clean &= allBase85( cBuf, nc );
 7435#endif
 7436      }
 7437      break;
 7438    default:
 7439      sayHelp();
 7440      rc = 1;
 7441    }
 7442    if( foc ) fclose(foc);
 7443  }
 7444# ifndef OMIT_BASE85_CHECKER
 7445  if( !b85Clean ){
 7446    fprintf(stderr, "Base85 input had non-base85 dark or control content.\n");
 7447  }
 7448# endif
 7449  return rc;
 7450}
 7451
 7452#endif
 7453
 7454/************************* End ext/misc/base85.c ********************/
 7455/************************* Begin ext/misc/ieee754.c ******************/
 7456/*
 7457** 2013-04-17
 7458**
 7459** The author disclaims copyright to this source code.  In place of
 7460** a legal notice, here is a blessing:
 7461**
 7462**    May you do good and not evil.
 7463**    May you find forgiveness for yourself and forgive others.
 7464**    May you share freely, never taking more than you give.
 7465**
 7466******************************************************************************
 7467**
 7468** This SQLite extension implements functions for the exact display
 7469** and input of IEEE754 Binary64 floating-point numbers.
 7470**
 7471**   ieee754(X)
 7472**   ieee754(Y,Z)
 7473**
 7474** In the first form, the value X should be a floating-point number.
 7475** The function will return a string of the form 'ieee754(Y,Z)' where
 7476** Y and Z are integers such that X==Y*pow(2,Z).
 7477**
 7478** In the second form, Y and Z are integers which are the mantissa and
 7479** base-2 exponent of a new floating point number.  The function returns
 7480** a floating-point value equal to Y*pow(2,Z).
 7481**
 7482** Examples:
 7483**
 7484**     ieee754(2.0)             ->     'ieee754(2,0)'
 7485**     ieee754(45.25)           ->     'ieee754(181,-2)'
 7486**     ieee754(2, 0)            ->     2.0
 7487**     ieee754(181, -2)         ->     45.25
 7488**
 7489** Two additional functions break apart the one-argument ieee754()
 7490** result into separate integer values:
 7491**
 7492**     ieee754_mantissa(45.25)  ->     181
 7493**     ieee754_exponent(45.25)  ->     -2
 7494**
 7495** These functions convert binary64 numbers into blobs and back again.
 7496**
 7497**     ieee754_from_blob(x'3ff0000000000000')  ->  1.0
 7498**     ieee754_to_blob(1.0)                    ->  x'3ff0000000000000'
 7499**
 7500** In all single-argument functions, if the argument is an 8-byte blob
 7501** then that blob is interpreted as a big-endian binary64 value.
 7502**
 7503**
 7504** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES
 7505** -----------------------------------------------
 7506**
 7507** This extension in combination with the separate 'decimal' extension
 7508** can be used to compute the exact decimal representation of binary64
 7509** values.  To begin, first compute a table of exponent values:
 7510**
 7511**    CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
 7512**    WITH RECURSIVE c(x,v) AS (
 7513**      VALUES(0,'1')
 7514**      UNION ALL
 7515**      SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
 7516**    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
 7517**    WITH RECURSIVE c(x,v) AS (
 7518**      VALUES(-1,'0.5')
 7519**      UNION ALL
 7520**      SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
 7521**    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
 7522**
 7523** Then, to compute the exact decimal representation of a floating
 7524** point value (the value 47.49 is used in the example) do:
 7525**
 7526**    WITH c(n) AS (VALUES(47.49))
 7527**          ---------------^^^^^---- Replace with whatever you want
 7528**    SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
 7529**      FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
 7530**
 7531** Here is a query to show various boundry values for the binary64
 7532** number format:
 7533**
 7534**    WITH c(name,bin) AS (VALUES
 7535**       ('minimum positive value',        x'0000000000000001'),
 7536**       ('maximum subnormal value',       x'000fffffffffffff'),
 7537**       ('minimum positive normal value', x'0010000000000000'),
 7538**       ('maximum value',                 x'7fefffffffffffff'))
 7539**    SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v)
 7540**      FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin);
 7541**
 7542*/
 7543/* #include "sqlite3ext.h" */
 7544SQLITE_EXTENSION_INIT1
 7545#include <assert.h>
 7546#include <string.h>
 7547
 7548/* Mark a function parameter as unused, to suppress nuisance compiler
 7549** warnings. */
 7550#ifndef UNUSED_PARAMETER
 7551# define UNUSED_PARAMETER(X)  (void)(X)
 7552#endif
 7553
 7554/*
 7555** Implementation of the ieee754() function
 7556*/
 7557static void ieee754func(
 7558  sqlite3_context *context,
 7559  int argc,
 7560  sqlite3_value **argv
 7561){
 7562  if( argc==1 ){
 7563    sqlite3_int64 m, a;
 7564    double r;
 7565    int e;
 7566    int isNeg;
 7567    char zResult[100];
 7568    assert( sizeof(m)==sizeof(r) );
 7569    if( sqlite3_value_type(argv[0])==SQLITE_BLOB
 7570     && sqlite3_value_bytes(argv[0])==sizeof(r)
 7571    ){
 7572      const unsigned char *x = sqlite3_value_blob(argv[0]);
 7573      unsigned int i;
 7574      sqlite3_uint64 v = 0;
 7575      for(i=0; i<sizeof(r); i++){
 7576        v = (v<<8) | x[i];
 7577      }
 7578      memcpy(&r, &v, sizeof(r));
 7579    }else{
 7580      r = sqlite3_value_double(argv[0]);
 7581    }
 7582    if( r<0.0 ){
 7583      isNeg = 1;
 7584      r = -r;
 7585    }else{
 7586      isNeg = 0;
 7587    }
 7588    memcpy(&a,&r,sizeof(a));
 7589    if( a==0 ){
 7590      e = 0;
 7591      m = 0;
 7592    }else if( a==(sqlite3_int64)0x8000000000000000LL ){
 7593      e = -1996;
 7594      m = -1;
 7595    }else{
 7596      e = a>>52;
 7597      m = a & ((((sqlite3_int64)1)<<52)-1);
 7598      if( e==0 ){
 7599        m <<= 1;
 7600      }else{
 7601        m |= ((sqlite3_int64)1)<<52;
 7602      }
 7603      while( e<1075 && m>0 && (m&1)==0 ){
 7604        m >>= 1;
 7605        e++;
 7606      }
 7607      if( isNeg ) m = -m;
 7608    }
 7609    switch( *(int*)sqlite3_user_data(context) ){
 7610      case 0:
 7611        sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",
 7612                         m, e-1075);
 7613        sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);
 7614        break;
 7615      case 1:
 7616        sqlite3_result_int64(context, m);
 7617        break;
 7618      case 2:
 7619        sqlite3_result_int(context, e-1075);
 7620        break;
 7621    }
 7622  }else{
 7623    sqlite3_int64 m, e, a;
 7624    double r;
 7625    int isNeg = 0;
 7626    m = sqlite3_value_int64(argv[0]);
 7627    e = sqlite3_value_int64(argv[1]);
 7628
 7629    /* Limit the range of e.  Ticket 22dea1cfdb9151e4 2021-03-02 */
 7630    if( e>10000 ){
 7631      e = 10000;
 7632    }else if( e<-10000 ){
 7633      e = -10000;
 7634    }
 7635
 7636    if( m<0 ){
 7637      if( m<(-9223372036854775807LL) ) return;
 7638      isNeg = 1;
 7639      m = -m;
 7640    }else if( m==0 && e>-1000 && e<1000 ){
 7641      sqlite3_result_double(context, 0.0);
 7642      return;
 7643    }
 7644    while( (m>>32)&0xffe00000 ){
 7645      m >>= 1;
 7646      e++;
 7647    }
 7648    while( m!=0 && ((m>>32)&0xfff00000)==0 ){
 7649      m <<= 1;
 7650      e--;
 7651    }
 7652    e += 1075;
 7653    if( e<=0 ){
 7654      /* Subnormal */
 7655      if( 1-e >= 64 ){
 7656        m = 0;
 7657      }else{
 7658        m >>= 1-e;
 7659      }
 7660      e = 0;
 7661    }else if( e>0x7ff ){
 7662      e = 0x7ff;
 7663    }
 7664    a = m & ((((sqlite3_int64)1)<<52)-1);
 7665    a |= e<<52;
 7666    if( isNeg ) a |= ((sqlite3_uint64)1)<<63;
 7667    memcpy(&r, &a, sizeof(r));
 7668    sqlite3_result_double(context, r);
 7669  }
 7670}
 7671
 7672/*
 7673** Functions to convert between blobs and floats.
 7674*/
 7675static void ieee754func_from_blob(
 7676  sqlite3_context *context,
 7677  int argc,
 7678  sqlite3_value **argv
 7679){
 7680  UNUSED_PARAMETER(argc);
 7681  if( sqlite3_value_type(argv[0])==SQLITE_BLOB
 7682   && sqlite3_value_bytes(argv[0])==sizeof(double)
 7683  ){
 7684    double r;
 7685    const unsigned char *x = sqlite3_value_blob(argv[0]);
 7686    unsigned int i;
 7687    sqlite3_uint64 v = 0;
 7688    for(i=0; i<sizeof(r); i++){
 7689      v = (v<<8) | x[i];
 7690    }
 7691    memcpy(&r, &v, sizeof(r));
 7692    sqlite3_result_double(context, r);
 7693  }
 7694}
 7695static void ieee754func_to_blob(
 7696  sqlite3_context *context,
 7697  int argc,
 7698  sqlite3_value **argv
 7699){
 7700  UNUSED_PARAMETER(argc);
 7701  if( sqlite3_value_type(argv[0])==SQLITE_FLOAT
 7702   || sqlite3_value_type(argv[0])==SQLITE_INTEGER
 7703  ){
 7704    double r = sqlite3_value_double(argv[0]);
 7705    sqlite3_uint64 v;
 7706    unsigned char a[sizeof(r)];
 7707    unsigned int i;
 7708    memcpy(&v, &r, sizeof(r));
 7709    for(i=1; i<=sizeof(r); i++){
 7710      a[sizeof(r)-i] = v&0xff;
 7711      v >>= 8;
 7712    }
 7713    sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
 7714  }
 7715}
 7716
 7717/*
 7718** Functions to convert between 64-bit integers and floats.
 7719**
 7720** The bit patterns are copied.  The numeric values are different.
 7721*/
 7722static void ieee754func_from_int(
 7723  sqlite3_context *context,
 7724  int argc,
 7725  sqlite3_value **argv
 7726){
 7727  UNUSED_PARAMETER(argc);
 7728  if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){
 7729    double r;
 7730    sqlite3_int64 v = sqlite3_value_int64(argv[0]);
 7731    memcpy(&r, &v, sizeof(r));
 7732    sqlite3_result_double(context, r);
 7733  }
 7734}
 7735static void ieee754func_to_int(
 7736  sqlite3_context *context,
 7737  int argc,
 7738  sqlite3_value **argv
 7739){
 7740  UNUSED_PARAMETER(argc);
 7741  if( sqlite3_value_type(argv[0])==SQLITE_FLOAT ){
 7742    double r = sqlite3_value_double(argv[0]);
 7743    sqlite3_uint64 v;
 7744    memcpy(&v, &r, sizeof(v));
 7745    sqlite3_result_int64(context, v);
 7746  }
 7747}
 7748
 7749/*
 7750** SQL Function:   ieee754_inc(r,N)
 7751**
 7752** Move the floating point value r by N quantums and return the new
 7753** values.
 7754**
 7755** Behind the scenes: this routine merely casts r into a 64-bit unsigned
 7756** integer, adds N, then casts the value back into float.
 7757**
 7758** Example:  To find the smallest positive number:
 7759**
 7760**     SELECT ieee754_inc(0.0,+1);
 7761*/
 7762static void ieee754inc(
 7763  sqlite3_context *context,
 7764  int argc,
 7765  sqlite3_value **argv
 7766){
 7767  double r;
 7768  sqlite3_int64 N;
 7769  sqlite3_uint64 m1, m2;
 7770  double r2;
 7771  UNUSED_PARAMETER(argc);
 7772  r = sqlite3_value_double(argv[0]);
 7773  N = sqlite3_value_int64(argv[1]);
 7774  memcpy(&m1, &r, 8);
 7775  m2 = m1 + N;
 7776  memcpy(&r2, &m2, 8);
 7777  sqlite3_result_double(context, r2);
 7778}
 7779
 7780
 7781#ifdef _WIN32
 7782
 7783#endif
 7784int sqlite3_ieee_init(
 7785  sqlite3 *db, 
 7786  char **pzErrMsg, 
 7787  const sqlite3_api_routines *pApi
 7788){
 7789  static const struct {
 7790    char *zFName;
 7791    int nArg;
 7792    int iAux;
 7793    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
 7794  } aFunc[] = {
 7795    { "ieee754",           1,   0, ieee754func },
 7796    { "ieee754",           2,   0, ieee754func },
 7797    { "ieee754_mantissa",  1,   1, ieee754func },
 7798    { "ieee754_exponent",  1,   2, ieee754func },
 7799    { "ieee754_to_blob",   1,   0, ieee754func_to_blob },
 7800    { "ieee754_from_blob", 1,   0, ieee754func_from_blob },
 7801    { "ieee754_to_int",    1,   0, ieee754func_to_int },
 7802    { "ieee754_from_int",  1,   0, ieee754func_from_int },
 7803    { "ieee754_inc",       2,   0, ieee754inc  },
 7804  };
 7805  unsigned int i;
 7806  int rc = SQLITE_OK;
 7807  SQLITE_EXTENSION_INIT2(pApi);
 7808  (void)pzErrMsg;  /* Unused parameter */
 7809  for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
 7810    rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg,
 7811                               SQLITE_UTF8|SQLITE_INNOCUOUS,
 7812                               (void*)&aFunc[i].iAux,
 7813                               aFunc[i].xFunc, 0, 0);
 7814  }
 7815  return rc;
 7816}
 7817
 7818/************************* End ext/misc/ieee754.c ********************/
 7819/************************* Begin ext/misc/series.c ******************/
 7820/*
 7821** 2015-08-18, 2023-04-28
 7822**
 7823** The author disclaims copyright to this source code.  In place of
 7824** a legal notice, here is a blessing:
 7825**
 7826**    May you do good and not evil.
 7827**    May you find forgiveness for yourself and forgive others.
 7828**    May you share freely, never taking more than you give.
 7829**
 7830*************************************************************************
 7831**
 7832** This file demonstrates how to create a table-valued-function using
 7833** a virtual table.  This demo implements the generate_series() function
 7834** which gives the same results as the eponymous function in PostgreSQL,
 7835** within the limitation that its arguments are signed 64-bit integers.
 7836**
 7837** Considering its equivalents to generate_series(start,stop,step): A
 7838** value V[n] sequence is produced for integer n ascending from 0 where
 7839**  ( V[n] == start + n * step  &&  sgn(V[n] - stop) * sgn(step) >= 0 )
 7840** for each produced value (independent of production time ordering.)
 7841**
 7842** All parameters must be either integer or convertable to integer.
 7843** The start parameter is required.
 7844** The stop parameter defaults to (1<<32)-1 (aka 4294967295 or 0xffffffff)
 7845** The step parameter defaults to 1 and 0 is treated as 1.
 7846**
 7847** Examples:
 7848**
 7849**      SELECT * FROM generate_series(0,100,5);
 7850**
 7851** The query above returns integers from 0 through 100 counting by steps
 7852** of 5.  In other words, 0, 5, 10, 15, ..., 90, 95, 100.  There are a total
 7853** of 21 rows.
 7854**
 7855**      SELECT * FROM generate_series(0,100);
 7856**
 7857** Integers from 0 through 100 with a step size of 1.  101 rows.
 7858**
 7859**      SELECT * FROM generate_series(20) LIMIT 10;
 7860**
 7861** Integers 20 through 29.  10 rows.
 7862**
 7863**      SELECT * FROM generate_series(0,-100,-5);
 7864**
 7865** Integers 0 -5 -10 ... -100.  21 rows.
 7866**
 7867**      SELECT * FROM generate_series(0,-1);
 7868**
 7869** Empty sequence.
 7870**
 7871** HOW IT WORKS
 7872**
 7873** The generate_series "function" is really a virtual table with the
 7874** following schema:
 7875**
 7876**     CREATE TABLE generate_series(
 7877**       value,
 7878**       start HIDDEN,
 7879**       stop HIDDEN,
 7880**       step HIDDEN
 7881**     );
 7882**
 7883** The virtual table also has a rowid which is an alias for the value.
 7884**
 7885** Function arguments in queries against this virtual table are translated
 7886** into equality constraints against successive hidden columns.  In other
 7887** words, the following pairs of queries are equivalent to each other:
 7888**
 7889**    SELECT * FROM generate_series(0,100,5);
 7890**    SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5;
 7891**
 7892**    SELECT * FROM generate_series(0,100);
 7893**    SELECT * FROM generate_series WHERE start=0 AND stop=100;
 7894**
 7895**    SELECT * FROM generate_series(20) LIMIT 10;
 7896**    SELECT * FROM generate_series WHERE start=20 LIMIT 10;
 7897**
 7898** The generate_series virtual table implementation leaves the xCreate method
 7899** set to NULL.  This means that it is not possible to do a CREATE VIRTUAL
 7900** TABLE command with "generate_series" as the USING argument.  Instead, there
 7901** is a single generate_series virtual table that is always available without
 7902** having to be created first.
 7903**
 7904** The xBestIndex method looks for equality constraints against the hidden
 7905** start, stop, and step columns, and if present, it uses those constraints
 7906** to bound the sequence of generated values.  If the equality constraints
 7907** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step.
 7908** xBestIndex returns a small cost when both start and stop are available,
 7909** and a very large cost if either start or stop are unavailable.  This
 7910** encourages the query planner to order joins such that the bounds of the
 7911** series are well-defined.
 7912**
 7913** Update on 2024-08-22:
 7914** xBestIndex now also looks for equality and inequality constraints against
 7915** the value column and uses those constraints as additional bounds against
 7916** the sequence range.  Thus, a query like this:
 7917**
 7918**     SELECT value FROM generate_series($SA,$EA)
 7919**      WHERE value BETWEEN $SB AND $EB;
 7920**
 7921** Is logically the same as:
 7922**
 7923**     SELECT value FROM generate_series(max($SA,$SB),min($EA,$EB));
 7924**
 7925** Constraints on the value column can server as substitutes for constraints
 7926** on the hidden start and stop columns.  So, the following two queries
 7927** are equivalent:
 7928**
 7929**     SELECT value FROM generate_series($S,$E);
 7930**     SELECT value FROM generate_series WHERE value BETWEEN $S and $E;
 7931**
 7932*/
 7933/* #include "sqlite3ext.h" */
 7934SQLITE_EXTENSION_INIT1
 7935#include <assert.h>
 7936#include <string.h>
 7937#include <limits.h>
 7938#include <math.h>
 7939
 7940#ifndef SQLITE_OMIT_VIRTUALTABLE
 7941
 7942/* series_cursor is a subclass of sqlite3_vtab_cursor which will
 7943** serve as the underlying representation of a cursor that scans
 7944** over rows of the result.
 7945**
 7946** iOBase, iOTerm, and iOStep are the original values of the
 7947** start=, stop=, and step= constraints on the query.  These are
 7948** the values reported by the start, stop, and step columns of the
 7949** virtual table.
 7950**
 7951** iBase, iTerm, iStep, and bDescp are the actual values used to generate
 7952** the sequence.  These might be different from the iOxxxx values.
 7953** For example in
 7954**
 7955**   SELECT value FROM generate_series(1,11,2)
 7956**    WHERE value BETWEEN 4 AND 8;
 7957**
 7958** The iOBase is 1, but the iBase is 5.  iOTerm is 11 but iTerm is 7.
 7959** Another example:
 7960**
 7961**   SELECT value FROM generate_series(1,15,3) ORDER BY value DESC;
 7962**
 7963** The cursor initialization for the above query is:
 7964**
 7965**   iOBase = 1        iBase = 13
 7966**   iOTerm = 15       iTerm = 1
 7967**   iOStep = 3        iStep = 3      bDesc = 1
 7968**
 7969** The actual step size is unsigned so that can have a value of
 7970** +9223372036854775808 which is needed for querys like this:
 7971**
 7972**   SELECT value
 7973**     FROM generate_series(9223372036854775807,
 7974**                          -9223372036854775808,
 7975**                          -9223372036854775808)
 7976**    ORDER BY value ASC;
 7977**
 7978** The setup for the previous query will be:
 7979**
 7980**   iOBase = 9223372036854775807    iBase = -1
 7981**   iOTerm = -9223372036854775808   iTerm = 9223372036854775807
 7982**   iOStep = -9223372036854775808   iStep = 9223372036854775808  bDesc = 0
 7983*/
 7984/* typedef unsigned char u8; */
 7985typedef struct series_cursor series_cursor;
 7986struct series_cursor {
 7987  sqlite3_vtab_cursor base;  /* Base class - must be first */
 7988  sqlite3_int64 iOBase;      /* Original starting value ("start") */
 7989  sqlite3_int64 iOTerm;      /* Original terminal value ("stop") */
 7990  sqlite3_int64 iOStep;      /* Original step value */
 7991  sqlite3_int64 iBase;       /* Starting value to actually use */
 7992  sqlite3_int64 iTerm;       /* Terminal value to actually use */
 7993  sqlite3_uint64 iStep;      /* The step size */
 7994  sqlite3_int64 iValue;      /* Current value */
 7995  u8 bDesc;                  /* iStep is really negative */
 7996  u8 bDone;                  /* True if stepped past last element */
 7997};
 7998
 7999/*
 8000** Computed the difference between two 64-bit signed integers using a
 8001** convoluted computation designed to work around the silly restriction
 8002** against signed integer overflow in C.
 8003*/
 8004static sqlite3_uint64 span64(sqlite3_int64 a, sqlite3_int64 b){
 8005  assert( a>=b );
 8006  return (*(sqlite3_uint64*)&a) - (*(sqlite3_uint64*)&b);
 8007}  
 8008
 8009/*
 8010** Add or substract an unsigned 64-bit integer from a signed 64-bit integer
 8011** and return the new signed 64-bit integer.
 8012*/
 8013static sqlite3_int64 add64(sqlite3_int64 a, sqlite3_uint64 b){
 8014  sqlite3_uint64 x = *(sqlite3_uint64*)&a;
 8015  x += b;
 8016  return *(sqlite3_int64*)&x;
 8017}
 8018static sqlite3_int64 sub64(sqlite3_int64 a, sqlite3_uint64 b){
 8019  sqlite3_uint64 x = *(sqlite3_uint64*)&a;
 8020  x -= b;
 8021  return *(sqlite3_int64*)&x;
 8022}
 8023
 8024/*
 8025** The seriesConnect() method is invoked to create a new
 8026** series_vtab that describes the generate_series virtual table.
 8027**
 8028** Think of this routine as the constructor for series_vtab objects.
 8029**
 8030** All this routine needs to do is:
 8031**
 8032**    (1) Allocate the series_vtab object and initialize all fields.
 8033**
 8034**    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
 8035**        result set of queries against generate_series will look like.
 8036*/
 8037static int seriesConnect(
 8038  sqlite3 *db,
 8039  void *pUnused,
 8040  int argcUnused, const char *const*argvUnused,
 8041  sqlite3_vtab **ppVtab,
 8042  char **pzErrUnused
 8043){
 8044  sqlite3_vtab *pNew;
 8045  int rc;
 8046
 8047/* Column numbers */
 8048#define SERIES_COLUMN_ROWID (-1)
 8049#define SERIES_COLUMN_VALUE 0
 8050#define SERIES_COLUMN_START 1
 8051#define SERIES_COLUMN_STOP  2
 8052#define SERIES_COLUMN_STEP  3
 8053
 8054  (void)pUnused;
 8055  (void)argcUnused;
 8056  (void)argvUnused;
 8057  (void)pzErrUnused;
 8058  rc = sqlite3_declare_vtab(db,
 8059     "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");
 8060  if( rc==SQLITE_OK ){
 8061    pNew = *ppVtab = sqlite3_malloc64( sizeof(*pNew) );
 8062    if( pNew==0 ) return SQLITE_NOMEM;
 8063    memset(pNew, 0, sizeof(*pNew));
 8064    sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
 8065  }
 8066  return rc;
 8067}
 8068
 8069/*
 8070** This method is the destructor for series_cursor objects.
 8071*/
 8072static int seriesDisconnect(sqlite3_vtab *pVtab){
 8073  sqlite3_free(pVtab);
 8074  return SQLITE_OK;
 8075}
 8076
 8077/*
 8078** Constructor for a new series_cursor object.
 8079*/
 8080static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){
 8081  series_cursor *pCur;
 8082  (void)pUnused;
 8083  pCur = sqlite3_malloc64( sizeof(*pCur) );
 8084  if( pCur==0 ) return SQLITE_NOMEM;
 8085  memset(pCur, 0, sizeof(*pCur));
 8086  *ppCursor = &pCur->base;
 8087  return SQLITE_OK;
 8088}
 8089
 8090/*
 8091** Destructor for a series_cursor.
 8092*/
 8093static int seriesClose(sqlite3_vtab_cursor *cur){
 8094  sqlite3_free(cur);
 8095  return SQLITE_OK;
 8096}
 8097
 8098
 8099/*
 8100** Advance a series_cursor to its next row of output.
 8101*/
 8102static int seriesNext(sqlite3_vtab_cursor *cur){
 8103  series_cursor *pCur = (series_cursor*)cur;
 8104  if( pCur->iValue==pCur->iTerm ){
 8105    pCur->bDone = 1;
 8106  }else if( pCur->bDesc ){
 8107    pCur->iValue = sub64(pCur->iValue, pCur->iStep);
 8108    assert( pCur->iValue>=pCur->iTerm );
 8109  }else{
 8110    pCur->iValue = add64(pCur->iValue, pCur->iStep);
 8111    assert( pCur->iValue<=pCur->iTerm );
 8112  }
 8113  return SQLITE_OK;
 8114}
 8115
 8116/*
 8117** Return values of columns for the row at which the series_cursor
 8118** is currently pointing.
 8119*/
 8120static int seriesColumn(
 8121  sqlite3_vtab_cursor *cur,   /* The cursor */
 8122  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
 8123  int i                       /* Which column to return */
 8124){
 8125  series_cursor *pCur = (series_cursor*)cur;
 8126  sqlite3_int64 x = 0;
 8127  switch( i ){
 8128    case SERIES_COLUMN_START:  x = pCur->iOBase;     break;
 8129    case SERIES_COLUMN_STOP:   x = pCur->iOTerm;     break;
 8130    case SERIES_COLUMN_STEP:   x = pCur->iOStep;     break;
 8131    default:                   x = pCur->iValue;     break;
 8132  }
 8133  sqlite3_result_int64(ctx, x);
 8134  return SQLITE_OK;
 8135}
 8136
 8137#ifndef LARGEST_UINT64
 8138#define LARGEST_INT64  ((sqlite3_int64)0x7fffffffffffffffLL)
 8139#define LARGEST_UINT64 ((sqlite3_uint64)0xffffffffffffffffULL)
 8140#define SMALLEST_INT64 ((sqlite3_int64)0x8000000000000000LL)
 8141#endif
 8142
 8143/*
 8144** The rowid is the same as the value.
 8145*/
 8146static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
 8147  series_cursor *pCur = (series_cursor*)cur;
 8148  *pRowid = pCur->iValue;
 8149  return SQLITE_OK;
 8150}
 8151
 8152/*
 8153** Return TRUE if the cursor has been moved off of the last
 8154** row of output.
 8155*/
 8156static int seriesEof(sqlite3_vtab_cursor *cur){
 8157  series_cursor *pCur = (series_cursor*)cur;
 8158  return pCur->bDone;
 8159}
 8160
 8161/* True to cause run-time checking of the start=, stop=, and/or step=
 8162** parameters.  The only reason to do this is for testing the
 8163** constraint checking logic for virtual tables in the SQLite core.
 8164*/
 8165#ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
 8166# define SQLITE_SERIES_CONSTRAINT_VERIFY 0
 8167#endif
 8168
 8169/*
 8170** Return the number of steps between pCur->iBase and pCur->iTerm if
 8171** the step width is pCur->iStep.
 8172*/
 8173static sqlite3_uint64 seriesSteps(series_cursor *pCur){
 8174  if( pCur->bDesc ){
 8175    assert( pCur->iBase >= pCur->iTerm );
 8176    return span64(pCur->iBase, pCur->iTerm)/pCur->iStep;
 8177  }else{
 8178    assert( pCur->iBase <= pCur->iTerm );
 8179    return span64(pCur->iTerm, pCur->iBase)/pCur->iStep;
 8180  }
 8181}
 8182
 8183#if defined(SQLITE_ENABLE_MATH_FUNCTIONS) || defined(_WIN32)
 8184/*
 8185** Case 1 (the most common case):
 8186** The standard math library is available so use ceil() and floor() from there.
 8187*/
 8188static double seriesCeil(double r){ return ceil(r); }
 8189static double seriesFloor(double r){ return floor(r); }
 8190#elif defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
 8191/*
 8192** Case 2 (2nd most common): Use GCC/Clang builtins
 8193*/
 8194static double seriesCeil(double r){ return __builtin_ceil(r); }
 8195static double seriesFloor(double r){ return __builtin_floor(r); }
 8196#else
 8197/*
 8198** Case 3 (rarely happens): Use home-grown ceil() and floor() routines.
 8199*/
 8200static double seriesCeil(double r){
 8201  sqlite3_int64 x;
 8202  if( r!=r ) return r;
 8203  if( r<=(-4503599627370496.0) ) return r;
 8204  if( r>=(+4503599627370496.0) ) return r;
 8205  x = (sqlite3_int64)r;
 8206  if( r==(double)x ) return r;
 8207  if( r>(double)x ) x++;
 8208  return (double)x;
 8209}
 8210static double seriesFloor(double r){
 8211  sqlite3_int64 x;
 8212  if( r!=r ) return r;
 8213  if( r<=(-4503599627370496.0) ) return r;
 8214  if( r>=(+4503599627370496.0) ) return r;
 8215  x = (sqlite3_int64)r;
 8216  if( r==(double)x ) return r;
 8217  if( r<(double)x ) x--;
 8218  return (double)x;
 8219}
 8220#endif
 8221
 8222/*
 8223** This method is called to "rewind" the series_cursor object back
 8224** to the first row of output.  This method is always called at least
 8225** once prior to any call to seriesColumn() or seriesRowid() or
 8226** seriesEof().
 8227**
 8228** The query plan selected by seriesBestIndex is passed in the idxNum
 8229** parameter.  (idxStr is not used in this implementation.)  idxNum
 8230** is a bitmask showing which constraints are available:
 8231**
 8232**   0x0001:    start=VALUE
 8233**   0x0002:    stop=VALUE
 8234**   0x0004:    step=VALUE
 8235**   0x0008:    descending order
 8236**   0x0010:    ascending order
 8237**   0x0020:    LIMIT  VALUE
 8238**   0x0040:    OFFSET  VALUE
 8239**   0x0080:    value=VALUE
 8240**   0x0100:    value>=VALUE
 8241**   0x0200:    value>VALUE
 8242**   0x1000:    value<=VALUE
 8243**   0x2000:    value<VALUE
 8244**
 8245** This routine should initialize the cursor and position it so that it
 8246** is pointing at the first row, or pointing off the end of the table
 8247** (so that seriesEof() will return true) if the table is empty.
 8248*/
 8249static int seriesFilter(
 8250  sqlite3_vtab_cursor *pVtabCursor,
 8251  int idxNum, const char *idxStrUnused,
 8252  int argc, sqlite3_value **argv
 8253){
 8254  series_cursor *pCur = (series_cursor *)pVtabCursor;
 8255  int iArg = 0;                         /* Arguments used so far */
 8256  int i;                                /* Loop counter */
 8257  sqlite3_int64 iMin = SMALLEST_INT64;  /* Smallest allowed output value */
 8258  sqlite3_int64 iMax = LARGEST_INT64;   /* Largest allowed output value */
 8259  sqlite3_int64 iLimit = 0;             /* if >0, the value of the LIMIT */
 8260  sqlite3_int64 iOffset = 0;            /* if >0, the value of the OFFSET */
 8261
 8262  (void)idxStrUnused;
 8263
 8264  /* If any constraints have a NULL value, then return no rows.
 8265  ** See ticket https://sqlite.org/src/info/fac496b61722daf2
 8266  */
 8267  for(i=0; i<argc; i++){
 8268    if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
 8269      goto series_no_rows;
 8270    }
 8271  }
 8272
 8273  /* Capture the three HIDDEN parameters to the virtual table and insert
 8274  ** default values for any parameters that are omitted.
 8275  */
 8276  if( idxNum & 0x01 ){
 8277    pCur->iOBase = sqlite3_value_int64(argv[iArg++]);
 8278  }else{
 8279    pCur->iOBase = 0;
 8280  }
 8281  if( idxNum & 0x02 ){
 8282    pCur->iOTerm = sqlite3_value_int64(argv[iArg++]);
 8283  }else{
 8284    pCur->iOTerm = 0xffffffff;
 8285  }
 8286  if( idxNum & 0x04 ){
 8287    pCur->iOStep = sqlite3_value_int64(argv[iArg++]);
 8288    if( pCur->iOStep==0 ) pCur->iOStep = 1;
 8289  }else{
 8290    pCur->iOStep = 1;
 8291  }
 8292
 8293  /* If there are constraints on the value column but there are
 8294  ** no constraints on  the start, stop, and step columns, then
 8295  ** initialize the default range to be the entire range of 64-bit signed
 8296  ** integers.  This range will contracted by the value column constraints
 8297  ** further below.
 8298  */
 8299  if( (idxNum & 0x05)==0 && (idxNum & 0x0380)!=0 ){
 8300    pCur->iOBase = SMALLEST_INT64;
 8301  }
 8302  if( (idxNum & 0x06)==0 && (idxNum & 0x3080)!=0 ){
 8303    pCur->iOTerm = LARGEST_INT64;
 8304  }
 8305  pCur->iBase = pCur->iOBase;
 8306  pCur->iTerm = pCur->iOTerm;
 8307  if( pCur->iOStep>0 ){  
 8308    pCur->iStep = pCur->iOStep;
 8309  }else if( pCur->iOStep>SMALLEST_INT64 ){
 8310    pCur->iStep = -pCur->iOStep;
 8311  }else{
 8312    pCur->iStep = LARGEST_INT64;
 8313    pCur->iStep++;
 8314  }
 8315  pCur->bDesc = pCur->iOStep<0;
 8316  if( pCur->bDesc==0 && pCur->iBase>pCur->iTerm ){
 8317    goto series_no_rows;
 8318  }
 8319  if( pCur->bDesc!=0 && pCur->iBase<pCur->iTerm ){
 8320    goto series_no_rows;
 8321  }
 8322
 8323  /* Extract the LIMIT and OFFSET values, but do not apply them yet.
 8324  ** The range must first be constrained by the limits on value.
 8325  */
 8326  if( idxNum & 0x20 ){
 8327    iLimit = sqlite3_value_int64(argv[iArg++]);
 8328    if( idxNum & 0x40 ){
 8329      iOffset = sqlite3_value_int64(argv[iArg++]);
 8330    }
 8331  }
 8332
 8333  /* Narrow the range of iMin and iMax (the minimum and maximum outputs)
 8334  ** based on equality and inequality constraints on the "value" column.
 8335  */
 8336  if( idxNum & 0x3380 ){
 8337    if( idxNum & 0x0080 ){    /* value=X */
 8338      if( sqlite3_value_numeric_type(argv[iArg])==SQLITE_FLOAT ){
 8339        double r = sqlite3_value_double(argv[iArg++]);
 8340        if( r==seriesCeil(r)
 8341         && r>=(double)SMALLEST_INT64
 8342         && r<=(double)LARGEST_INT64
 8343        ){
 8344          iMin = iMax = (sqlite3_int64)r;
 8345        }else{
 8346          goto series_no_rows;
 8347        }
 8348      }else{
 8349        iMin = iMax = sqlite3_value_int64(argv[iArg++]);
 8350      }
 8351    }else{
 8352      if( idxNum & 0x0300 ){  /* value>X or value>=X */
 8353        if( sqlite3_value_numeric_type(argv[iArg])==SQLITE_FLOAT ){
 8354          double r = sqlite3_value_double(argv[iArg++]);
 8355          if( r<(double)SMALLEST_INT64 ){
 8356            iMin = SMALLEST_INT64;
 8357          }else if( (idxNum & 0x0200)!=0 && r==seriesCeil(r) ){
 8358            iMin = (sqlite3_int64)seriesCeil(r+1.0);
 8359          }else{
 8360            iMin = (sqlite3_int64)seriesCeil(r);
 8361          }
 8362        }else{
 8363          iMin = sqlite3_value_int64(argv[iArg++]);
 8364          if( (idxNum & 0x0200)!=0 ){
 8365            if( iMin==LARGEST_INT64 ){
 8366              goto series_no_rows;
 8367            }else{
 8368              iMin++;
 8369            }
 8370          }
 8371        }
 8372      }
 8373      if( idxNum & 0x3000 ){   /* value<X or value<=X */
 8374        if( sqlite3_value_numeric_type(argv[iArg])==SQLITE_FLOAT ){
 8375          double r = sqlite3_value_double(argv[iArg++]);
 8376          if( r>(double)LARGEST_INT64 ){
 8377            iMax = LARGEST_INT64;
 8378          }else if( (idxNum & 0x2000)!=0 && r==seriesFloor(r) ){
 8379            iMax = (sqlite3_int64)(r-1.0);
 8380          }else{
 8381            iMax = (sqlite3_int64)seriesFloor(r);
 8382          }
 8383        }else{
 8384          iMax = sqlite3_value_int64(argv[iArg++]);
 8385          if( idxNum & 0x2000 ){
 8386            if( iMax==SMALLEST_INT64 ){
 8387              goto series_no_rows;
 8388            }else{
 8389              iMax--;
 8390            }
 8391          }
 8392        }
 8393      }
 8394      if( iMin>iMax ){
 8395        goto series_no_rows;
 8396      }
 8397    }
 8398
 8399    /* Try to reduce the range of values to be generated based on
 8400    ** constraints on the "value" column.
 8401    */
 8402    if( pCur->bDesc==0 ){
 8403      if( pCur->iBase<iMin ){
 8404        sqlite3_uint64 span = span64(iMin,pCur->iBase);
 8405        pCur->iBase = add64(pCur->iBase, (span/pCur->iStep)*pCur->iStep);
 8406        if( pCur->iBase<iMin ){
 8407          if( pCur->iBase > sub64(LARGEST_INT64, pCur->iStep) ){
 8408            goto series_no_rows;
 8409          }
 8410          pCur->iBase = add64(pCur->iBase, pCur->iStep);
 8411        }
 8412      }
 8413      if( pCur->iTerm>iMax ){
 8414        pCur->iTerm = iMax;
 8415      }
 8416    }else{
 8417      if( pCur->iBase>iMax ){
 8418        sqlite3_uint64 span = span64(pCur->iBase,iMax);
 8419        pCur->iBase = sub64(pCur->iBase, (span/pCur->iStep)*pCur->iStep);
 8420        if( pCur->iBase>iMax ){
 8421          if( pCur->iBase < add64(SMALLEST_INT64, pCur->iStep) ){
 8422            goto series_no_rows;
 8423          }
 8424          pCur->iBase = sub64(pCur->iBase, pCur->iStep);
 8425        }
 8426      }
 8427      if( pCur->iTerm<iMin ){
 8428        pCur->iTerm = iMin;
 8429      }
 8430    }
 8431  }
 8432
 8433  /* Adjust iTerm so that it is exactly the last value of the series.
 8434  */
 8435  if( pCur->bDesc==0 ){
 8436    if( pCur->iBase>pCur->iTerm ){
 8437      goto series_no_rows;
 8438    }
 8439    pCur->iTerm = sub64(pCur->iTerm,
 8440                        span64(pCur->iTerm,pCur->iBase) % pCur->iStep);
 8441  }else{
 8442    if( pCur->iBase<pCur->iTerm ){
 8443      goto series_no_rows;
 8444    }
 8445    pCur->iTerm = add64(pCur->iTerm,
 8446                        span64(pCur->iBase,pCur->iTerm) % pCur->iStep);
 8447  }
 8448
 8449  /* Transform the series generator to output values in the requested
 8450  ** order.
 8451  */
 8452  if( ((idxNum & 0x0008)!=0 && pCur->bDesc==0)
 8453   || ((idxNum & 0x0010)!=0 && pCur->bDesc!=0)
 8454  ){
 8455    sqlite3_int64 tmp = pCur->iBase;
 8456    pCur->iBase = pCur->iTerm;
 8457    pCur->iTerm = tmp;
 8458    pCur->bDesc = !pCur->bDesc;
 8459  }
 8460
 8461  /* Apply LIMIT and OFFSET constraints, if any */
 8462  assert( pCur->iStep!=0 );
 8463  if( idxNum & 0x20 ){
 8464    if( iOffset>0 ){
 8465      if( seriesSteps(pCur) < (sqlite3_uint64)iOffset ){
 8466        goto series_no_rows;
 8467      }else if( pCur->bDesc ){
 8468        pCur->iBase = sub64(pCur->iBase, pCur->iStep*iOffset);
 8469      }else{
 8470        pCur->iBase = add64(pCur->iBase, pCur->iStep*iOffset);
 8471      }
 8472    }
 8473    if( iLimit>=0 && seriesSteps(pCur) > (sqlite3_uint64)iLimit ){
 8474      pCur->iTerm = add64(pCur->iBase, (iLimit - 1)*pCur->iStep);
 8475    }
 8476  }
 8477  pCur->iValue = pCur->iBase;
 8478  pCur->bDone = 0;
 8479  return SQLITE_OK;
 8480
 8481series_no_rows:
 8482  pCur->iBase = 0;
 8483  pCur->iTerm = 0;
 8484  pCur->iStep = 1;
 8485  pCur->bDesc = 0;
 8486  pCur->bDone = 1;
 8487  return SQLITE_OK;  
 8488}
 8489
 8490/*
 8491** SQLite will invoke this method one or more times while planning a query
 8492** that uses the generate_series virtual table.  This routine needs to create
 8493** a query plan for each invocation and compute an estimated cost for that
 8494** plan.
 8495**
 8496** In this implementation idxNum is used to represent the
 8497** query plan.  idxStr is unused.
 8498**
 8499** The query plan is represented by bits in idxNum:
 8500**
 8501**   0x0001  start = $num
 8502**   0x0002  stop = $num
 8503**   0x0004  step = $num
 8504**   0x0008  output is in descending order
 8505**   0x0010  output is in ascending order
 8506**   0x0020  LIMIT $num
 8507**   0x0040  OFFSET $num
 8508**   0x0080  value = $num
 8509**   0x0100  value >= $num
 8510**   0x0200  value > $num
 8511**   0x1000  value <= $num
 8512**   0x2000  value < $num
 8513**
 8514** Only one of 0x0100 or 0x0200 will be returned.  Similarly, only
 8515** one of 0x1000 or 0x2000 will be returned.  If the 0x0080 is set, then
 8516** none of the 0xff00 bits will be set.
 8517**
 8518** The order of parameters passed to xFilter is as follows:
 8519**
 8520**    * The argument to start= if bit 0x0001 is in the idxNum mask
 8521**    * The argument to stop= if bit 0x0002 is in the idxNum mask
 8522**    * The argument to step= if bit 0x0004 is in the idxNum mask
 8523**    * The argument to LIMIT if bit 0x0020 is in the idxNum mask
 8524**    * The argument to OFFSET if bit 0x0040 is in the idxNum mask
 8525**    * The argument to value=, or value>= or value> if any of
 8526**      bits 0x0380 are in the idxNum mask
 8527**    * The argument to value<= or value< if either of bits 0x3000
 8528**      are in the mask
 8529**
 8530*/
 8531static int seriesBestIndex(
 8532  sqlite3_vtab *pVTab,
 8533  sqlite3_index_info *pIdxInfo
 8534){
 8535  int i, j;              /* Loop over constraints */
 8536  int idxNum = 0;        /* The query plan bitmask */
 8537#ifndef ZERO_ARGUMENT_GENERATE_SERIES
 8538  int bStartSeen = 0;    /* EQ constraint seen on the START column */
 8539#endif
 8540  int unusableMask = 0;  /* Mask of unusable constraints */
 8541  int nArg = 0;          /* Number of arguments that seriesFilter() expects */
 8542  int aIdx[7];           /* Constraints on start, stop, step, LIMIT, OFFSET,
 8543                         ** and value.  aIdx[5] covers value=, value>=, and
 8544                         ** value>,  aIdx[6] covers value<= and value< */
 8545  const struct sqlite3_index_constraint *pConstraint;
 8546
 8547  /* This implementation assumes that the start, stop, and step columns
 8548  ** are the last three columns in the virtual table. */
 8549  assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
 8550  assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
 8551
 8552  aIdx[0] = aIdx[1] = aIdx[2] = aIdx[3] = aIdx[4] = aIdx[5] = aIdx[6] = -1;
 8553  pConstraint = pIdxInfo->aConstraint;
 8554  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
 8555    int iCol;    /* 0 for start, 1 for stop, 2 for step */
 8556    int iMask;   /* bitmask for those column */
 8557    int op = pConstraint->op;
 8558    if( op>=SQLITE_INDEX_CONSTRAINT_LIMIT
 8559     && op<=SQLITE_INDEX_CONSTRAINT_OFFSET
 8560    ){
 8561      if( pConstraint->usable==0 ){
 8562        /* do nothing */
 8563      }else if( op==SQLITE_INDEX_CONSTRAINT_LIMIT ){
 8564        aIdx[3] = i;
 8565        idxNum |= 0x20;
 8566      }else{
 8567        assert( op==SQLITE_INDEX_CONSTRAINT_OFFSET );
 8568        aIdx[4] = i;
 8569        idxNum |= 0x40;
 8570      }
 8571      continue;
 8572    }
 8573    if( pConstraint->iColumn<SERIES_COLUMN_START ){
 8574      if( (pConstraint->iColumn==SERIES_COLUMN_VALUE ||
 8575           pConstraint->iColumn==SERIES_COLUMN_ROWID)
 8576       && pConstraint->usable
 8577      ){
 8578        switch( op ){
 8579          case SQLITE_INDEX_CONSTRAINT_EQ:
 8580          case SQLITE_INDEX_CONSTRAINT_IS: {
 8581            idxNum |=  0x0080;
 8582            idxNum &= ~0x3300;
 8583            aIdx[5] = i;
 8584            aIdx[6] = -1;
 8585#ifndef ZERO_ARGUMENT_GENERATE_SERIES
 8586            bStartSeen = 1;
 8587#endif
 8588            break;
 8589          }
 8590          case SQLITE_INDEX_CONSTRAINT_GE: {
 8591            if( idxNum & 0x0080 ) break;
 8592            idxNum |=  0x0100;
 8593            idxNum &= ~0x0200;
 8594            aIdx[5] = i;
 8595#ifndef ZERO_ARGUMENT_GENERATE_SERIES
 8596            bStartSeen = 1;
 8597#endif
 8598            break;
 8599          }
 8600          case SQLITE_INDEX_CONSTRAINT_GT: {
 8601            if( idxNum & 0x0080 ) break;
 8602            idxNum |=  0x0200;
 8603            idxNum &= ~0x0100;
 8604            aIdx[5] = i;
 8605#ifndef ZERO_ARGUMENT_GENERATE_SERIES
 8606            bStartSeen = 1;
 8607#endif
 8608            break;
 8609          }
 8610          case SQLITE_INDEX_CONSTRAINT_LE: {
 8611            if( idxNum & 0x0080 ) break;
 8612            idxNum |=  0x1000;
 8613            idxNum &= ~0x2000;
 8614            aIdx[6] = i;
 8615            break;
 8616          }
 8617          case SQLITE_INDEX_CONSTRAINT_LT: {
 8618            if( idxNum & 0x0080 ) break;
 8619            idxNum |=  0x2000;
 8620            idxNum &= ~0x1000;
 8621            aIdx[6] = i;
 8622            break;
 8623          }
 8624        }
 8625      }
 8626      continue;
 8627    }
 8628    iCol = pConstraint->iColumn - SERIES_COLUMN_START;
 8629    assert( iCol>=0 && iCol<=2 );
 8630    iMask = 1 << iCol;
 8631#ifndef ZERO_ARGUMENT_GENERATE_SERIES
 8632    if( iCol==0 && op==SQLITE_INDEX_CONSTRAINT_EQ ){
 8633      bStartSeen = 1;
 8634    }
 8635#endif
 8636    if( pConstraint->usable==0 ){
 8637      unusableMask |=  iMask;
 8638      continue;
 8639    }else if( op==SQLITE_INDEX_CONSTRAINT_EQ ){
 8640      idxNum |= iMask;
 8641      aIdx[iCol] = i;
 8642    }
 8643  }
 8644  if( aIdx[3]==0 ){
 8645    /* Ignore OFFSET if LIMIT is omitted */
 8646    idxNum &= ~0x60;
 8647    aIdx[4] = 0;
 8648  }
 8649  for(i=0; i<7; i++){
 8650    if( (j = aIdx[i])>=0 ){
 8651      pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
 8652      pIdxInfo->aConstraintUsage[j].omit =
 8653         !SQLITE_SERIES_CONSTRAINT_VERIFY || i>=3;
 8654    }
 8655  }
 8656  /* The current generate_column() implementation requires at least one
 8657  ** argument (the START value).  Legacy versions assumed START=0 if the
 8658  ** first argument was omitted.  Compile with -DZERO_ARGUMENT_GENERATE_SERIES
 8659  ** to obtain the legacy behavior */
 8660#ifndef ZERO_ARGUMENT_GENERATE_SERIES
 8661  if( !bStartSeen ){
 8662    sqlite3_free(pVTab->zErrMsg);
 8663    pVTab->zErrMsg = sqlite3_mprintf(
 8664        "first argument to \"generate_series()\" missing or unusable");
 8665    return SQLITE_ERROR;
 8666  }
 8667#endif
 8668  if( (unusableMask & ~idxNum)!=0 ){
 8669    /* The start, stop, and step columns are inputs.  Therefore if there
 8670    ** are unusable constraints on any of start, stop, or step then
 8671    ** this plan is unusable */
 8672    return SQLITE_CONSTRAINT;
 8673  }
 8674  if( (idxNum & 0x03)==0x03 ){
 8675    /* Both start= and stop= boundaries are available.  This is the 
 8676    ** the preferred case */
 8677    pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
 8678    pIdxInfo->estimatedRows = 1000;
 8679    if( pIdxInfo->nOrderBy>=1 && pIdxInfo->aOrderBy[0].iColumn==0 ){
 8680      if( pIdxInfo->aOrderBy[0].desc ){
 8681        idxNum |= 0x08;
 8682      }else{
 8683        idxNum |= 0x10;
 8684      }
 8685      pIdxInfo->orderByConsumed = 1;
 8686    }
 8687  }else if( (idxNum & 0x21)==0x21 ){
 8688    /* We have start= and LIMIT */
 8689    pIdxInfo->estimatedRows = 2500;
 8690  }else{
 8691    /* If either boundary is missing, we have to generate a huge span
 8692    ** of numbers.  Make this case very expensive so that the query
 8693    ** planner will work hard to avoid it. */
 8694    pIdxInfo->estimatedRows = 2147483647;
 8695  }
 8696  pIdxInfo->idxNum = idxNum;
 8697#ifdef SQLITE_INDEX_SCAN_HEX
 8698  pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_HEX;
 8699#endif
 8700  return SQLITE_OK;
 8701}
 8702
 8703/*
 8704** This following structure defines all the methods for the 
 8705** generate_series virtual table.
 8706*/
 8707static sqlite3_module seriesModule = {
 8708  0,                         /* iVersion */
 8709  0,                         /* xCreate */
 8710  seriesConnect,             /* xConnect */
 8711  seriesBestIndex,           /* xBestIndex */
 8712  seriesDisconnect,          /* xDisconnect */
 8713  0,                         /* xDestroy */
 8714  seriesOpen,                /* xOpen - open a cursor */
 8715  seriesClose,               /* xClose - close a cursor */
 8716  seriesFilter,              /* xFilter - configure scan constraints */
 8717  seriesNext,                /* xNext - advance a cursor */
 8718  seriesEof,                 /* xEof - check for end of scan */
 8719  seriesColumn,              /* xColumn - read data */
 8720  seriesRowid,               /* xRowid - read data */
 8721  0,                         /* xUpdate */
 8722  0,                         /* xBegin */
 8723  0,                         /* xSync */
 8724  0,                         /* xCommit */
 8725  0,                         /* xRollback */
 8726  0,                         /* xFindMethod */
 8727  0,                         /* xRename */
 8728  0,                         /* xSavepoint */
 8729  0,                         /* xRelease */
 8730  0,                         /* xRollbackTo */
 8731  0,                         /* xShadowName */
 8732  0                          /* xIntegrity */
 8733};
 8734
 8735#endif /* SQLITE_OMIT_VIRTUALTABLE */
 8736
 8737#ifdef _WIN32
 8738
 8739#endif
 8740int sqlite3_series_init(
 8741  sqlite3 *db, 
 8742  char **pzErrMsg, 
 8743  const sqlite3_api_routines *pApi
 8744){
 8745  int rc = SQLITE_OK;
 8746  SQLITE_EXTENSION_INIT2(pApi);
 8747#ifndef SQLITE_OMIT_VIRTUALTABLE
 8748  if( sqlite3_libversion_number()<3008012 && pzErrMsg!=0 ){
 8749    *pzErrMsg = sqlite3_mprintf(
 8750        "generate_series() requires SQLite 3.8.12 or later");
 8751    return SQLITE_ERROR;
 8752  }
 8753  rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0);
 8754#endif
 8755  return rc;
 8756}
 8757
 8758/************************* End ext/misc/series.c ********************/
 8759/************************* Begin ext/misc/regexp.c ******************/
 8760/*
 8761** 2012-11-13
 8762**
 8763** The author disclaims copyright to this source code.  In place of
 8764** a legal notice, here is a blessing:
 8765**
 8766**    May you do good and not evil.
 8767**    May you find forgiveness for yourself and forgive others.
 8768**    May you share freely, never taking more than you give.
 8769**
 8770******************************************************************************
 8771**
 8772** The code in this file implements a compact but reasonably
 8773** efficient regular-expression matcher for posix extended regular
 8774** expressions against UTF8 text.
 8775**
 8776** This file is an SQLite extension.  It registers a single function
 8777** named "regexp(A,B)" where A is the regular expression and B is the
 8778** string to be matched.  By registering this function, SQLite will also
 8779** then implement the "B regexp A" operator.  Note that with the function
 8780** the regular expression comes first, but with the operator it comes
 8781** second.
 8782**
 8783**  The following regular expression syntax is supported:
 8784**
 8785**     X*      zero or more occurrences of X
 8786**     X+      one or more occurrences of X
 8787**     X?      zero or one occurrences of X
 8788**     X{p,q}  between p and q occurrences of X
 8789**     (X)     match X
 8790**     X|Y     X or Y
 8791**     ^X      X occurring at the beginning of the string
 8792**     X$      X occurring at the end of the string
 8793**     .       Match any single character
 8794**     \c      Character c where c is one of \{}()[]|*+?-.
 8795**     \c      C-language escapes for c in afnrtv.  ex: \t or \n
 8796**     \uXXXX  Where XXXX is exactly 4 hex digits, unicode value XXXX
 8797**     \xXX    Where XX is exactly 2 hex digits, unicode value XX
 8798**     [abc]   Any single character from the set abc
 8799**     [^abc]  Any single character not in the set abc
 8800**     [a-z]   Any single character in the range a-z
 8801**     [^a-z]  Any single character not in the range a-z
 8802**     \b      Word boundary
 8803**     \w      Word character.  [A-Za-z0-9_]
 8804**     \W      Non-word character
 8805**     \d      Digit
 8806**     \D      Non-digit
 8807**     \s      Whitespace character
 8808**     \S      Non-whitespace character
 8809**
 8810** A nondeterministic finite automaton (NFA) is used for matching, so the
 8811** performance is bounded by O(N*M) where N is the size of the regular
 8812** expression and M is the size of the input string.  The matcher never
 8813** exhibits exponential behavior.  Note that the X{p,q} operator expands
 8814** to p copies of X following by q-p copies of X? and that the size of the
 8815** regular expression in the O(N*M) performance bound is computed after
 8816** this expansion.
 8817**
 8818** To help prevent DoS attacks, the maximum size of the NFA is restricted.
 8819*/
 8820#include <string.h>
 8821#include <stdlib.h>
 8822/* #include "sqlite3ext.h" */
 8823SQLITE_EXTENSION_INIT1
 8824
 8825/*
 8826** The following #defines change the names of some functions implemented in
 8827** this file to prevent name collisions with C-library functions of the
 8828** same name.
 8829*/
 8830#define re_match   sqlite3re_match
 8831#define re_compile sqlite3re_compile
 8832#define re_free    sqlite3re_free
 8833
 8834/* The end-of-input character */
 8835#define RE_EOF            0    /* End of input */
 8836#define RE_START  0xfffffff    /* Start of input - larger than an UTF-8 */
 8837
 8838/* The NFA is implemented as sequence of opcodes taken from the following
 8839** set.  Each opcode has a single integer argument.
 8840*/
 8841#define RE_OP_MATCH       1    /* Match the one character in the argument */
 8842#define RE_OP_ANY         2    /* Match any one character.  (Implements ".") */
 8843#define RE_OP_ANYSTAR     3    /* Special optimized version of .* */
 8844#define RE_OP_FORK        4    /* Continue to both next and opcode at iArg */
 8845#define RE_OP_GOTO        5    /* Jump to opcode at iArg */
 8846#define RE_OP_ACCEPT      6    /* Halt and indicate a successful match */
 8847#define RE_OP_CC_INC      7    /* Beginning of a [...] character class */
 8848#define RE_OP_CC_EXC      8    /* Beginning of a [^...] character class */
 8849#define RE_OP_CC_VALUE    9    /* Single value in a character class */
 8850#define RE_OP_CC_RANGE   10    /* Range of values in a character class */
 8851#define RE_OP_WORD       11    /* Perl word character [A-Za-z0-9_] */
 8852#define RE_OP_NOTWORD    12    /* Not a perl word character */
 8853#define RE_OP_DIGIT      13    /* digit:  [0-9] */
 8854#define RE_OP_NOTDIGIT   14    /* Not a digit */
 8855#define RE_OP_SPACE      15    /* space:  [ \t\n\r\v\f] */
 8856#define RE_OP_NOTSPACE   16    /* Not a digit */
 8857#define RE_OP_BOUNDARY   17    /* Boundary between word and non-word */
 8858#define RE_OP_ATSTART    18    /* Currently at the start of the string */
 8859
 8860/* Each opcode is a "state" in the NFA */
 8861typedef unsigned short ReStateNumber;
 8862
 8863/* Because this is an NFA and not a DFA, multiple states can be active at
 8864** once.  An instance of the following object records all active states in
 8865** the NFA.  The implementation is optimized for the common case where the
 8866** number of actives states is small.
 8867*/
 8868typedef struct ReStateSet {
 8869  unsigned nState;            /* Number of current states */
 8870  ReStateNumber *aState;      /* Current states */
 8871} ReStateSet;
 8872
 8873/* An input string read one character at a time.
 8874*/
 8875typedef struct ReInput ReInput;
 8876struct ReInput {
 8877  const unsigned char *z;  /* All text */
 8878  int i;                   /* Next byte to read */
 8879  int mx;                  /* EOF when i>=mx */
 8880};
 8881
 8882/* A compiled NFA (or an NFA that is in the process of being compiled) is
 8883** an instance of the following object.
 8884*/
 8885typedef struct ReCompiled ReCompiled;
 8886struct ReCompiled {
 8887  ReInput sIn;                /* Regular expression text */
 8888  const char *zErr;           /* Error message to return */
 8889  char *aOp;                  /* Operators for the virtual machine */
 8890  int *aArg;                  /* Arguments to each operator */
 8891  unsigned (*xNextChar)(ReInput*);  /* Next character function */
 8892  unsigned char zInit[12];    /* Initial text to match */
 8893  int nInit;                  /* Number of bytes in zInit */
 8894  unsigned nState;            /* Number of entries in aOp[] and aArg[] */
 8895  unsigned nAlloc;            /* Slots allocated for aOp[] and aArg[] */
 8896  unsigned mxAlloc;           /* Complexity limit */
 8897};
 8898
 8899/* Add a state to the given state set if it is not already there */
 8900static void re_add_state(ReStateSet *pSet, int newState){
 8901  unsigned i;
 8902  for(i=0; i<pSet->nState; i++) if( pSet->aState[i]==newState ) return;
 8903  pSet->aState[pSet->nState++] = (ReStateNumber)newState;
 8904}
 8905
 8906/* Extract the next unicode character from *pzIn and return it.  Advance
 8907** *pzIn to the first byte past the end of the character returned.  To
 8908** be clear:  this routine converts utf8 to unicode.  This routine is 
 8909** optimized for the common case where the next character is a single byte.
 8910*/
 8911static unsigned re_next_char(ReInput *p){
 8912  unsigned c;
 8913  if( p->i>=p->mx ) return 0;
 8914  c = p->z[p->i++];
 8915  if( c>=0x80 ){
 8916    if( (c&0xe0)==0xc0 && p->i<p->mx && (p->z[p->i]&0xc0)==0x80 ){
 8917      c = (c&0x1f)<<6 | (p->z[p->i++]&0x3f);
 8918      if( c<0x80 ) c = 0xfffd;
 8919    }else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80
 8920           && (p->z[p->i+1]&0xc0)==0x80 ){
 8921      c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f);
 8922      p->i += 2;
 8923      if( c<=0x7ff || (c>=0xd800 && c<=0xdfff) ) c = 0xfffd;
 8924    }else if( (c&0xf8)==0xf0 && p->i+2<p->mx && (p->z[p->i]&0xc0)==0x80
 8925           && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){
 8926      c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6)
 8927                       | (p->z[p->i+2]&0x3f);
 8928      p->i += 3;
 8929      if( c<=0xffff || c>0x10ffff ) c = 0xfffd;
 8930    }else{
 8931      c = 0xfffd;
 8932    }
 8933  }
 8934  return c;
 8935}
 8936static unsigned re_next_char_nocase(ReInput *p){
 8937  unsigned c = re_next_char(p);
 8938  if( c>='A' && c<='Z' ) c += 'a' - 'A';
 8939  return c;
 8940}
 8941
 8942/* Return true if c is a perl "word" character:  [A-Za-z0-9_] */
 8943static int re_word_char(int c){
 8944  return (c>='0' && c<='9') || (c>='a' && c<='z')
 8945      || (c>='A' && c<='Z') || c=='_';
 8946}
 8947
 8948/* Return true if c is a "digit" character:  [0-9] */
 8949static int re_digit_char(int c){
 8950  return (c>='0' && c<='9');
 8951}
 8952
 8953/* Return true if c is a perl "space" character:  [ \t\r\n\v\f] */
 8954static int re_space_char(int c){
 8955  return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
 8956}
 8957
 8958/* Run a compiled regular expression on the zero-terminated input
 8959** string zIn[].  Return true on a match and false if there is no match.
 8960*/
 8961static int re_match(ReCompiled *pRe, const unsigned char *zIn, int nIn){
 8962  ReStateSet aStateSet[2], *pThis, *pNext;
 8963  ReStateNumber aSpace[100];
 8964  ReStateNumber *pToFree;
 8965  unsigned int i = 0;
 8966  unsigned int iSwap = 0;
 8967  int c = RE_START;
 8968  int cPrev = 0;
 8969  int rc = 0;
 8970  ReInput in;
 8971
 8972  in.z = zIn;
 8973  in.i = 0;
 8974  in.mx = nIn>=0 ? nIn : (int)strlen((char const*)zIn);
 8975
 8976  /* Look for the initial prefix match, if there is one. */
 8977  if( pRe->nInit ){
 8978    unsigned char x = pRe->zInit[0];
 8979    while( in.i+pRe->nInit<=in.mx 
 8980     && (zIn[in.i]!=x ||
 8981         strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0)
 8982    ){
 8983      in.i++;
 8984    }
 8985    if( in.i+pRe->nInit>in.mx ) return 0;
 8986    c = RE_START-1;
 8987  }
 8988
 8989  if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){
 8990    pToFree = 0;
 8991    aStateSet[0].aState = aSpace;
 8992  }else{
 8993    pToFree = sqlite3_malloc64( sizeof(ReStateNumber)*2*pRe->nState );
 8994    if( pToFree==0 ) return -1;
 8995    aStateSet[0].aState = pToFree;
 8996  }
 8997  aStateSet[1].aState = &aStateSet[0].aState[pRe->nState];
 8998  pNext = &aStateSet[1];
 8999  pNext->nState = 0;
 9000  re_add_state(pNext, 0);
 9001  while( c!=RE_EOF && pNext->nState>0 ){
 9002    cPrev = c;
 9003    c = pRe->xNextChar(&in);
 9004    pThis = pNext;
 9005    pNext = &aStateSet[iSwap];
 9006    iSwap = 1 - iSwap;
 9007    pNext->nState = 0;
 9008    for(i=0; i<pThis->nState; i++){
 9009      int x = pThis->aState[i];
 9010      switch( pRe->aOp[x] ){
 9011        case RE_OP_MATCH: {
 9012          if( pRe->aArg[x]==c ) re_add_state(pNext, x+1);
 9013          break;
 9014        }
 9015        case RE_OP_ATSTART: {
 9016          if( cPrev==RE_START ) re_add_state(pThis, x+1);
 9017          break;
 9018        }
 9019        case RE_OP_ANY: {
 9020          if( c!=0 ) re_add_state(pNext, x+1);
 9021          break;
 9022        }
 9023        case RE_OP_WORD: {
 9024          if( re_word_char(c) ) re_add_state(pNext, x+1);
 9025          break;
 9026        }
 9027        case RE_OP_NOTWORD: {
 9028          if( !re_word_char(c) && c!=0 ) re_add_state(pNext, x+1);
 9029          break;
 9030        }
 9031        case RE_OP_DIGIT: {
 9032          if( re_digit_char(c) ) re_add_state(pNext, x+1);
 9033          break;
 9034        }
 9035        case RE_OP_NOTDIGIT: {
 9036          if( !re_digit_char(c) && c!=0 ) re_add_state(pNext, x+1);
 9037          break;
 9038        }
 9039        case RE_OP_SPACE: {
 9040          if( re_space_char(c) ) re_add_state(pNext, x+1);
 9041          break;
 9042        }
 9043        case RE_OP_NOTSPACE: {
 9044          if( !re_space_char(c) && c!=0 ) re_add_state(pNext, x+1);
 9045          break;
 9046        }
 9047        case RE_OP_BOUNDARY: {
 9048          if( re_word_char(c)!=re_word_char(cPrev) ) re_add_state(pThis, x+1);
 9049          break;
 9050        }
 9051        case RE_OP_ANYSTAR: {
 9052          re_add_state(pNext, x);
 9053          re_add_state(pThis, x+1);
 9054          break;
 9055        }
 9056        case RE_OP_FORK: {
 9057          re_add_state(pThis, x+pRe->aArg[x]);
 9058          re_add_state(pThis, x+1);
 9059          break;
 9060        }
 9061        case RE_OP_GOTO: {
 9062          re_add_state(pThis, x+pRe->aArg[x]);
 9063          break;
 9064        }
 9065        case RE_OP_ACCEPT: {
 9066          rc = 1;
 9067          goto re_match_end;
 9068        }
 9069        case RE_OP_CC_EXC: {
 9070          if( c==0 ) break;
 9071          /* fall-through */ goto re_op_cc_inc;
 9072        }
 9073        case RE_OP_CC_INC: re_op_cc_inc: {
 9074          int j = 1;
 9075          int n = pRe->aArg[x];
 9076          int hit = 0;
 9077          for(j=1; j>0 && j<n; j++){
 9078            if( pRe->aOp[x+j]==RE_OP_CC_VALUE ){
 9079              if( pRe->aArg[x+j]==c ){
 9080                hit = 1;
 9081                j = -1;
 9082              }
 9083            }else{
 9084              if( pRe->aArg[x+j]<=c && pRe->aArg[x+j+1]>=c ){
 9085                hit = 1;
 9086                j = -1;
 9087              }else{
 9088                j++;
 9089              }
 9090            }
 9091          }
 9092          if( pRe->aOp[x]==RE_OP_CC_EXC ) hit = !hit;
 9093          if( hit ) re_add_state(pNext, x+n);
 9094          break;
 9095        }
 9096      }
 9097    }
 9098  }
 9099  for(i=0; i<pNext->nState; i++){
 9100    int x = pNext->aState[i];
 9101    while( pRe->aOp[x]==RE_OP_GOTO ) x += pRe->aArg[x];
 9102    if( pRe->aOp[x]==RE_OP_ACCEPT ){ rc = 1; break; }
 9103  }
 9104re_match_end:
 9105  sqlite3_free(pToFree);
 9106  return rc;
 9107}
 9108
 9109/* Resize the opcode and argument arrays for an RE under construction.
 9110*/
 9111static int re_resize(ReCompiled *p, unsigned int N){
 9112  char *aOp;
 9113  int *aArg;
 9114  if( N>p->mxAlloc ){ p->zErr = "REGEXP pattern too big"; return 1; }
 9115  aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0]));
 9116  if( aOp==0 ){ p->zErr = "out of memory"; return 1; }
 9117  p->aOp = aOp;
 9118  aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0]));
 9119  if( aArg==0 ){ p->zErr = "out of memory"; return 1; }
 9120  p->aArg = aArg;
 9121  p->nAlloc = N;
 9122  return 0;
 9123}
 9124
 9125/* Insert a new opcode and argument into an RE under construction.  The
 9126** insertion point is just prior to existing opcode iBefore.
 9127*/
 9128static int re_insert(ReCompiled *p, int iBefore, int op, int arg){
 9129  int i;
 9130  if( p->nAlloc<=p->nState && re_resize(p, p->nAlloc*2) ) return 0;
 9131  for(i=p->nState; i>iBefore; i--){
 9132    p->aOp[i] = p->aOp[i-1];
 9133    p->aArg[i] = p->aArg[i-1];
 9134  }
 9135  p->nState++;
 9136  p->aOp[iBefore] = (char)op;
 9137  p->aArg[iBefore] = arg;
 9138  return iBefore;
 9139}
 9140
 9141/* Append a new opcode and argument to the end of the RE under construction.
 9142*/
 9143static int re_append(ReCompiled *p, int op, int arg){
 9144  return re_insert(p, p->nState, op, arg);
 9145}
 9146
 9147/* Make a copy of N opcodes starting at iStart onto the end of the RE
 9148** under construction.
 9149*/
 9150static void re_copy(ReCompiled *p, int iStart, unsigned int N){
 9151  if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return;
 9152  memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0]));
 9153  memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0]));
 9154  p->nState += N;
 9155}
 9156
 9157/* Return true if c is a hexadecimal digit character:  [0-9a-fA-F]
 9158** If c is a hex digit, also set *pV = (*pV)*16 + valueof(c).  If
 9159** c is not a hex digit *pV is unchanged.
 9160*/
 9161static int re_hex(int c, int *pV){
 9162  if( c>='0' && c<='9' ){
 9163    c -= '0';
 9164  }else if( c>='a' && c<='f' ){
 9165    c -= 'a' - 10;
 9166  }else if( c>='A' && c<='F' ){
 9167    c -= 'A' - 10;
 9168  }else{
 9169    return 0;
 9170  }
 9171  *pV = (*pV)*16 + (c & 0xff);
 9172  return 1;
 9173}
 9174
 9175/* A backslash character has been seen, read the next character and
 9176** return its interpretation.
 9177*/
 9178static unsigned re_esc_char(ReCompiled *p){
 9179  static const char zEsc[] = "afnrtv\\()*.+?[$^{|}]-";
 9180  static const char zTrans[] = "\a\f\n\r\t\v";
 9181  int i, v = 0;
 9182  char c;
 9183  if( p->sIn.i>=p->sIn.mx ) return 0;
 9184  c = p->sIn.z[p->sIn.i];
 9185  if( c=='u' && p->sIn.i+4<p->sIn.mx ){
 9186    const unsigned char *zIn = p->sIn.z + p->sIn.i;
 9187    if( re_hex(zIn[1],&v)
 9188     && re_hex(zIn[2],&v)
 9189     && re_hex(zIn[3],&v)
 9190     && re_hex(zIn[4],&v)
 9191    ){
 9192      p->sIn.i += 5;
 9193      return v;
 9194    }
 9195  }
 9196  if( c=='x' && p->sIn.i+2<p->sIn.mx ){
 9197    const unsigned char *zIn = p->sIn.z + p->sIn.i;
 9198    if( re_hex(zIn[1],&v)
 9199     && re_hex(zIn[2],&v)
 9200    ){
 9201      p->sIn.i += 3;
 9202      return v;
 9203    }
 9204  }
 9205  for(i=0; zEsc[i] && zEsc[i]!=c; i++){}
 9206  if( zEsc[i] ){
 9207    if( i<6 ) c = zTrans[i];
 9208    p->sIn.i++;
 9209  }else{
 9210    p->zErr = "unknown \\ escape";
 9211  }
 9212  return c;
 9213}
 9214
 9215/* Forward declaration */
 9216static const char *re_subcompile_string(ReCompiled*);
 9217
 9218/* Peek at the next byte of input */
 9219static unsigned char rePeek(ReCompiled *p){
 9220  return p->sIn.i<p->sIn.mx ? p->sIn.z[p->sIn.i] : 0;
 9221}
 9222
 9223/* Compile RE text into a sequence of opcodes.  Continue up to the
 9224** first unmatched ")" character, then return.  If an error is found,
 9225** return a pointer to the error message string.
 9226*/
 9227static const char *re_subcompile_re(ReCompiled *p){
 9228  const char *zErr;
 9229  int iStart, iEnd, iGoto;
 9230  iStart = p->nState;
 9231  zErr = re_subcompile_string(p);
 9232  if( zErr ) return zErr;
 9233  while( rePeek(p)=='|' ){
 9234    iEnd = p->nState;
 9235    re_insert(p, iStart, RE_OP_FORK, iEnd + 2 - iStart);
 9236    iGoto = re_append(p, RE_OP_GOTO, 0);
 9237    p->sIn.i++;
 9238    zErr = re_subcompile_string(p);
 9239    if( zErr ) return zErr;
 9240    p->aArg[iGoto] = p->nState - iGoto;
 9241  }
 9242  return 0;
 9243}
 9244
 9245/* Compile an element of regular expression text (anything that can be
 9246** an operand to the "|" operator).  Return NULL on success or a pointer
 9247** to the error message if there is a problem.
 9248*/
 9249static const char *re_subcompile_string(ReCompiled *p){
 9250  int iPrev = -1;
 9251  int iStart;
 9252  unsigned c;
 9253  const char *zErr;
 9254  while( (c = p->xNextChar(&p->sIn))!=0 ){
 9255    iStart = p->nState;
 9256    switch( c ){
 9257      case '|':
 9258      case ')': {
 9259        p->sIn.i--;
 9260        return 0;
 9261      }
 9262      case '(': {
 9263        zErr = re_subcompile_re(p);
 9264        if( zErr ) return zErr;
 9265        if( rePeek(p)!=')' ) return "unmatched '('";
 9266        p->sIn.i++;
 9267        break;
 9268      }
 9269      case '.': {
 9270        if( rePeek(p)=='*' ){
 9271          re_append(p, RE_OP_ANYSTAR, 0);
 9272          p->sIn.i++;
 9273        }else{
 9274          re_append(p, RE_OP_ANY, 0);
 9275        }
 9276        break;
 9277      }
 9278      case '*': {
 9279        if( iPrev<0 ) return "'*' without operand";
 9280        re_insert(p, iPrev, RE_OP_GOTO, p->nState - iPrev + 1);
 9281        re_append(p, RE_OP_FORK, iPrev - p->nState + 1);
 9282        break;
 9283      }
 9284      case '+': {
 9285        if( iPrev<0 ) return "'+' without operand";
 9286        re_append(p, RE_OP_FORK, iPrev - p->nState);
 9287        break;
 9288      }
 9289      case '?': {
 9290        if( iPrev<0 ) return "'?' without operand";
 9291        re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1);
 9292        break;
 9293      }
 9294      case '$': {
 9295        re_append(p, RE_OP_MATCH, RE_EOF);
 9296        break;
 9297      }
 9298      case '^': {
 9299        re_append(p, RE_OP_ATSTART, 0);
 9300        break;
 9301      }
 9302      case '{': {
 9303        unsigned int m = 0, n = 0;
 9304        unsigned int sz, j;
 9305        if( iPrev<0 ) return "'{m,n}' without operand";
 9306        while( (c=rePeek(p))>='0' && c<='9' ){
 9307          m = m*10 + c - '0';
 9308          if( m*2>p->mxAlloc ) return "REGEXP pattern too big";
 9309          p->sIn.i++;
 9310        }
 9311        n = m;
 9312        if( c==',' ){
 9313          p->sIn.i++;
 9314          n = 0;
 9315          while( (c=rePeek(p))>='0' && c<='9' ){
 9316            n = n*10 + c-'0';
 9317            if( n*2>p->mxAlloc ) return "REGEXP pattern too big";
 9318            p->sIn.i++;
 9319          }
 9320        }
 9321        if( c!='}' ) return "unmatched '{'";
 9322        if( n<m ) return "n less than m in '{m,n}'";
 9323        p->sIn.i++;
 9324        sz = p->nState - iPrev;
 9325        if( m==0 ){
 9326          if( n==0 ) return "both m and n are zero in '{m,n}'";
 9327          re_insert(p, iPrev, RE_OP_FORK, sz+1);
 9328          iPrev++;
 9329          n--;
 9330        }else{
 9331          for(j=1; j<m; j++) re_copy(p, iPrev, sz);
 9332        }
 9333        for(j=m; j<n; j++){
 9334          re_append(p, RE_OP_FORK, sz+1);
 9335          re_copy(p, iPrev, sz);
 9336        }
 9337        if( n==0 && m>0 ){
 9338          re_append(p, RE_OP_FORK, -(int)sz);
 9339        }
 9340        break;
 9341      }
 9342      case '[': {
 9343        unsigned int iFirst = p->nState;
 9344        if( rePeek(p)=='^' ){
 9345          re_append(p, RE_OP_CC_EXC, 0);
 9346          p->sIn.i++;
 9347        }else{
 9348          re_append(p, RE_OP_CC_INC, 0);
 9349        }
 9350        while( (c = p->xNextChar(&p->sIn))!=0 ){
 9351          if( c=='[' && rePeek(p)==':' ){
 9352            return "POSIX character classes not supported";
 9353          }
 9354          if( c=='\\' ) c = re_esc_char(p);
 9355          if( rePeek(p)=='-' ){
 9356            re_append(p, RE_OP_CC_RANGE, c);
 9357            p->sIn.i++;
 9358            c = p->xNextChar(&p->sIn);
 9359            if( c=='\\' ) c = re_esc_char(p);
 9360            re_append(p, RE_OP_CC_RANGE, c);
 9361          }else{
 9362            re_append(p, RE_OP_CC_VALUE, c);
 9363          }
 9364          if( rePeek(p)==']' ){ p->sIn.i++; break; }
 9365        }
 9366        if( c==0 ) return "unclosed '['";
 9367        if( p->nState>iFirst ) p->aArg[iFirst] = p->nState - iFirst;
 9368        break;
 9369      }
 9370      case '\\': {
 9371        int specialOp = 0;
 9372        switch( rePeek(p) ){
 9373          case 'b': specialOp = RE_OP_BOUNDARY;   break;
 9374          case 'd': specialOp = RE_OP_DIGIT;      break;
 9375          case 'D': specialOp = RE_OP_NOTDIGIT;   break;
 9376          case 's': specialOp = RE_OP_SPACE;      break;
 9377          case 'S': specialOp = RE_OP_NOTSPACE;   break;
 9378          case 'w': specialOp = RE_OP_WORD;       break;
 9379          case 'W': specialOp = RE_OP_NOTWORD;    break;
 9380        }
 9381        if( specialOp ){
 9382          p->sIn.i++;
 9383          re_append(p, specialOp, 0);
 9384        }else{
 9385          c = re_esc_char(p);
 9386          re_append(p, RE_OP_MATCH, c);
 9387        }
 9388        break;
 9389      }
 9390      default: {
 9391        re_append(p, RE_OP_MATCH, c);
 9392        break;
 9393      }
 9394    }
 9395    iPrev = iStart;
 9396  }
 9397  return 0;
 9398}
 9399
 9400/* Free and reclaim all the memory used by a previously compiled
 9401** regular expression.  Applications should invoke this routine once
 9402** for every call to re_compile() to avoid memory leaks.
 9403*/
 9404static void re_free(ReCompiled *pRe){
 9405  if( pRe ){
 9406    sqlite3_free(pRe->aOp);
 9407    sqlite3_free(pRe->aArg);
 9408    sqlite3_free(pRe);
 9409  }
 9410}
 9411
 9412/*
 9413** Version of re_free() that accepts a pointer of type (void*). Required
 9414** to satisfy sanitizers when the re_free() function is called via a
 9415** function pointer.
 9416*/
 9417static void re_free_voidptr(void *p){
 9418  re_free((ReCompiled*)p);
 9419}
 9420
 9421/*
 9422** Compile a textual regular expression in zIn[] into a compiled regular
 9423** expression suitable for us by re_match() and return a pointer to the
 9424** compiled regular expression in *ppRe.  Return NULL on success or an
 9425** error message if something goes wrong.
 9426*/
 9427static const char *re_compile(
 9428  ReCompiled **ppRe,      /* OUT: write compiled NFA here */
 9429  const char *zIn,        /* Input regular expression */
 9430  int mxRe,               /* Complexity limit */
 9431  int noCase              /* True for caseless comparisons */
 9432){
 9433  ReCompiled *pRe;
 9434  const char *zErr;
 9435  int i, j;
 9436
 9437  *ppRe = 0;
 9438  pRe = sqlite3_malloc64( sizeof(*pRe) );
 9439  if( pRe==0 ){
 9440    return "out of memory";
 9441  }
 9442  memset(pRe, 0, sizeof(*pRe));
 9443  pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char;
 9444  pRe->mxAlloc = mxRe;
 9445  if( re_resize(pRe, 30) ){
 9446    zErr = pRe->zErr;
 9447    re_free(pRe);
 9448    return zErr;
 9449  }
 9450  if( zIn[0]=='^' ){
 9451    zIn++;
 9452  }else{
 9453    re_append(pRe, RE_OP_ANYSTAR, 0);
 9454  }
 9455  pRe->sIn.z = (unsigned char*)zIn;
 9456  pRe->sIn.i = 0;
 9457  pRe->sIn.mx = (int)strlen(zIn);
 9458  zErr = re_subcompile_re(pRe);
 9459  if( zErr ){
 9460    re_free(pRe);
 9461    return zErr;
 9462  }
 9463  if( pRe->sIn.i>=pRe->sIn.mx ){
 9464    re_append(pRe, RE_OP_ACCEPT, 0);
 9465    *ppRe = pRe;
 9466  }else{
 9467    re_free(pRe);
 9468    return "unrecognized character";
 9469  }
 9470
 9471  /* The following is a performance optimization.  If the regex begins with
 9472  ** ".*" (if the input regex lacks an initial "^") and afterwards there are
 9473  ** one or more matching characters, enter those matching characters into
 9474  ** zInit[].  The re_match() routine can then search ahead in the input 
 9475  ** string looking for the initial match without having to run the whole
 9476  ** regex engine over the string.  Do not worry about trying to match
 9477  ** unicode characters beyond plane 0 - those are very rare and this is
 9478  ** just an optimization. */
 9479  if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){
 9480    for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){
 9481      unsigned x = pRe->aArg[i];
 9482      if( x<=0x7f ){
 9483        pRe->zInit[j++] = (unsigned char)x;
 9484      }else if( x<=0x7ff ){
 9485        pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6));
 9486        pRe->zInit[j++] = 0x80 | (x&0x3f);
 9487      }else if( x<=0xffff ){
 9488        pRe->zInit[j++] = (unsigned char)(0xe0 | (x>>12));
 9489        pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f);
 9490        pRe->zInit[j++] = 0x80 | (x&0x3f);
 9491      }else{
 9492        break;
 9493      }
 9494    }
 9495    if( j>0 && pRe->zInit[j-1]==0 ) j--;
 9496    pRe->nInit = j;
 9497  }
 9498  return pRe->zErr;
 9499}
 9500
 9501/*
 9502** The value of LIMIT_MAX_PATTERN_LENGTH.
 9503*/
 9504static int re_maxlen(sqlite3_context *context){
 9505  sqlite3 *db = sqlite3_context_db_handle(context);
 9506  return sqlite3_limit(db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH,-1);
 9507}
 9508
 9509/*
 9510** Maximum NFA size given a maximum pattern length.
 9511*/
 9512static int re_maxnfa(int mxlen){
 9513  return 75+mxlen/2;
 9514}
 9515
 9516/*
 9517** Implementation of the regexp() SQL function.  This function implements
 9518** the build-in REGEXP operator.  The first argument to the function is the
 9519** pattern and the second argument is the string.  So, the SQL statements:
 9520**
 9521**       A REGEXP B
 9522**
 9523** is implemented as regexp(B,A).
 9524*/
 9525static void re_sql_func(
 9526  sqlite3_context *context,
 9527  int argc,
 9528  sqlite3_value **argv
 9529){
 9530  ReCompiled *pRe;          /* Compiled regular expression */
 9531  const char *zPattern;     /* The regular expression */
 9532  const unsigned char *zStr;/* String being searched */
 9533  const char *zErr;         /* Compile error message */
 9534  int setAux = 0;           /* True to invoke sqlite3_set_auxdata() */
 9535
 9536  (void)argc;  /* Unused */
 9537  pRe = sqlite3_get_auxdata(context, 0);
 9538  if( pRe==0 ){
 9539    int mxLen = re_maxlen(context);
 9540    int nPattern;
 9541    zPattern = (const char*)sqlite3_value_text(argv[0]);
 9542    if( zPattern==0 ) return;
 9543    nPattern = sqlite3_value_bytes(argv[0]);
 9544    if( nPattern>mxLen ){
 9545      zErr = "REGEXP pattern too big";
 9546    }else{
 9547      zErr = re_compile(&pRe, zPattern, re_maxnfa(mxLen),
 9548                        sqlite3_user_data(context)!=0);
 9549    }
 9550    if( zErr ){
 9551      re_free(pRe);
 9552      sqlite3_result_error(context, zErr, -1);
 9553      return;
 9554    }
 9555    if( pRe==0 ){
 9556      sqlite3_result_error_nomem(context);
 9557      return;
 9558    }
 9559    setAux = 1;
 9560  }
 9561  zStr = (const unsigned char*)sqlite3_value_text(argv[1]);
 9562  if( zStr!=0 ){
 9563    sqlite3_result_int(context, re_match(pRe, zStr, -1));
 9564  }
 9565  if( setAux ){
 9566    sqlite3_set_auxdata(context, 0, pRe, re_free_voidptr);
 9567  }
 9568}
 9569
 9570#if defined(SQLITE_DEBUG)
 9571/*
 9572** This function is used for testing and debugging only.  It is only available
 9573** if the SQLITE_DEBUG compile-time option is used.
 9574**
 9575** Compile a regular expression and then convert the compiled expression into
 9576** text and return that text.
 9577*/
 9578static void re_bytecode_func(
 9579  sqlite3_context *context,
 9580  int argc,
 9581  sqlite3_value **argv
 9582){
 9583  const char *zPattern;
 9584  const char *zErr;
 9585  ReCompiled *pRe;
 9586  sqlite3_str *pStr;
 9587  int i;
 9588  int n;
 9589  char *z;
 9590  static const char *ReOpName[] = {
 9591    "EOF",
 9592    "MATCH",
 9593    "ANY",
 9594    "ANYSTAR",
 9595    "FORK",
 9596    "GOTO",
 9597    "ACCEPT",
 9598    "CC_INC",
 9599    "CC_EXC",
 9600    "CC_VALUE",
 9601    "CC_RANGE",
 9602    "WORD",
 9603    "NOTWORD",
 9604    "DIGIT",
 9605    "NOTDIGIT",
 9606    "SPACE",
 9607    "NOTSPACE",
 9608    "BOUNDARY",
 9609    "ATSTART",
 9610  };
 9611
 9612  (void)argc;
 9613  zPattern = (const char*)sqlite3_value_text(argv[0]);
 9614  if( zPattern==0 ) return;
 9615  zErr = re_compile(&pRe, zPattern, re_maxnfa(re_maxlen(context)),
 9616                    sqlite3_user_data(context)!=0);
 9617  if( zErr ){
 9618    re_free(pRe);
 9619    sqlite3_result_error(context, zErr, -1);
 9620    return;
 9621  }
 9622  if( pRe==0 ){
 9623    sqlite3_result_error_nomem(context);
 9624    return;
 9625  }
 9626  pStr = sqlite3_str_new(0);
 9627  if( pStr==0 ) goto re_bytecode_func_err;
 9628  if( pRe->nInit>0 ){
 9629    sqlite3_str_appendf(pStr, "INIT     ");
 9630    for(i=0; i<pRe->nInit; i++){
 9631      sqlite3_str_appendf(pStr, "%02x", pRe->zInit[i]);
 9632    }
 9633    sqlite3_str_appendf(pStr, "\n");
 9634  }
 9635  for(i=0; (unsigned)i<pRe->nState; i++){
 9636    sqlite3_str_appendf(pStr, "%-8s %4d\n",
 9637         ReOpName[(unsigned char)pRe->aOp[i]], pRe->aArg[i]);
 9638  }
 9639  n = sqlite3_str_length(pStr);
 9640  z = sqlite3_str_finish(pStr);
 9641  if( n==0 ){
 9642    sqlite3_free(z);
 9643  }else{
 9644    sqlite3_result_text(context, z, n-1, sqlite3_free);
 9645  }
 9646
 9647re_bytecode_func_err:
 9648  re_free(pRe);
 9649}
 9650
 9651#endif /* SQLITE_DEBUG */
 9652
 9653
 9654/*
 9655** Invoke this routine to register the regexp() function with the
 9656** SQLite database connection.
 9657*/
 9658#ifdef _WIN32
 9659
 9660#endif
 9661int sqlite3_regexp_init(
 9662  sqlite3 *db, 
 9663  char **pzErrMsg, 
 9664  const sqlite3_api_routines *pApi
 9665){
 9666  int rc = SQLITE_OK;
 9667  SQLITE_EXTENSION_INIT2(pApi);
 9668  (void)pzErrMsg;  /* Unused */
 9669  rc = sqlite3_create_function(db, "regexp", 2, 
 9670                            SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
 9671                            0, re_sql_func, 0, 0);
 9672  if( rc==SQLITE_OK ){
 9673    /* The regexpi(PATTERN,STRING) function is a case-insensitive version
 9674    ** of regexp(PATTERN,STRING). */
 9675    rc = sqlite3_create_function(db, "regexpi", 2,
 9676                            SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
 9677                            (void*)db, re_sql_func, 0, 0);
 9678#if defined(SQLITE_DEBUG)
 9679    if( rc==SQLITE_OK ){
 9680      rc = sqlite3_create_function(db, "regexp_bytecode", 1,
 9681                            SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
 9682                            0, re_bytecode_func, 0, 0);
 9683    }
 9684#endif /* SQLITE_DEBUG */
 9685  }
 9686  return rc;
 9687}
 9688
 9689/************************* End ext/misc/regexp.c ********************/
 9690#ifndef SQLITE_SHELL_FIDDLE
 9691/************************* Begin ext/misc/fileio.c ******************/
 9692/*
 9693** 2014-06-13
 9694**
 9695** The author disclaims copyright to this source code.  In place of
 9696** a legal notice, here is a blessing:
 9697**
 9698**    May you do good and not evil.
 9699**    May you find forgiveness for yourself and forgive others.
 9700**    May you share freely, never taking more than you give.
 9701**
 9702******************************************************************************
 9703**
 9704** This SQLite extension implements SQL functions readfile() and
 9705** writefile(), and eponymous virtual type "fsdir".
 9706**
 9707** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
 9708**
 9709**   If neither of the optional arguments is present, then this UDF
 9710**   function writes blob DATA to file FILE. If successful, the number
 9711**   of bytes written is returned. If an error occurs, NULL is returned.
 9712**
 9713**   If the first option argument - MODE - is present, then it must
 9714**   be passed an integer value that corresponds to a POSIX mode
 9715**   value (file type + permissions, as returned in the stat.st_mode
 9716**   field by the stat() system call). Three types of files may
 9717**   be written/created:
 9718**
 9719**     regular files:  (mode & 0170000)==0100000
 9720**     symbolic links: (mode & 0170000)==0120000
 9721**     directories:    (mode & 0170000)==0040000
 9722**
 9723**   For a directory, the DATA is ignored. For a symbolic link, it is
 9724**   interpreted as text and used as the target of the link. For a
 9725**   regular file, it is interpreted as a blob and written into the
 9726**   named file. Regardless of the type of file, its permissions are
 9727**   set to (mode & 0777) before returning.
 9728**
 9729**   If the optional MTIME argument is present, then it is interpreted
 9730**   as an integer - the number of seconds since the unix epoch. The
 9731**   modification-time of the target file is set to this value before
 9732**   returning.
 9733**
 9734**   If five or more arguments are passed to this function and an
 9735**   error is encountered, an exception is raised.
 9736**
 9737** READFILE(FILE):
 9738**
 9739**   Read and return the contents of file FILE (type blob) from disk.
 9740**
 9741** FSDIR:
 9742**
 9743**   Used as follows:
 9744**
 9745**     SELECT * FROM fsdir($path [, $dir]);
 9746**
 9747**   Parameter $path is an absolute or relative pathname. If the file that it
 9748**   refers to does not exist, it is an error. If the path refers to a regular
 9749**   file or symbolic link, it returns a single row. Or, if the path refers
 9750**   to a directory, it returns one row for the directory, and one row for each
 9751**   file within the hierarchy rooted at $path.
 9752**
 9753**   Each row has the following columns:
 9754**
 9755**     name:  Path to file or directory (text value).
 9756**     mode:  Value of stat.st_mode for directory entry (an integer).
 9757**     mtime: Value of stat.st_mtime for directory entry (an integer).
 9758**     data:  For a regular file, a blob containing the file data. For a
 9759**            symlink, a text value containing the text of the link. For a
 9760**            directory, NULL.
 9761**     level: Directory hierarchy level.  Topmost is 1.
 9762**
 9763**   If a non-NULL value is specified for the optional $dir parameter and
 9764**   $path is a relative path, then $path is interpreted relative to $dir. 
 9765**   And the paths returned in the "name" column of the table are also 
 9766**   relative to directory $dir.
 9767*/
 9768/* #include "sqlite3ext.h" */
 9769SQLITE_EXTENSION_INIT1
 9770#include <stdio.h>
 9771#include <string.h>
 9772#include <assert.h>
 9773
 9774#include <sys/types.h>
 9775#include <sys/stat.h>
 9776#include <fcntl.h>
 9777#if !defined(_WIN32) && !defined(WIN32)
 9778#  include <unistd.h>
 9779#  include <dirent.h>
 9780#  include <utime.h>
 9781#  include <sys/time.h>
 9782#  define STRUCT_STAT struct stat
 9783#  include <limits.h>
 9784#  include <stdlib.h>
 9785#else
 9786/* #  include "windirent.h" */
 9787#  include <direct.h>
 9788#  define STRUCT_STAT struct _stat
 9789#  define chmod(path,mode) fileio_chmod(path,mode)
 9790#  define mkdir(path,mode) fileio_mkdir(path)
 9791   extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
 9792   extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
 9793#endif
 9794#include <time.h>
 9795#include <errno.h>
 9796
 9797/* When used as part of the CLI, the sqlite3_stdio.h module will have
 9798** been included before this one. In that case use the sqlite3_stdio.h
 9799** #defines.  If not, create our own for fopen().
 9800*/
 9801#ifndef _SQLITE3_STDIO_H_
 9802# define sqlite3_fopen fopen
 9803#endif
 9804
 9805/*
 9806** Structure of the fsdir() table-valued function
 9807*/
 9808                 /*    0    1    2     3    4     5           6          */
 9809#define FSDIR_SCHEMA "(name,mode,mtime,data,level,path HIDDEN,dir HIDDEN)"
 9810
 9811#define FSDIR_COLUMN_NAME     0     /* Name of the file */
 9812#define FSDIR_COLUMN_MODE     1     /* Access mode */
 9813#define FSDIR_COLUMN_MTIME    2     /* Last modification time */
 9814#define FSDIR_COLUMN_DATA     3     /* File content */
 9815#define FSDIR_COLUMN_LEVEL    4     /* Level.  Topmost is 1 */
 9816#define FSDIR_COLUMN_PATH     5     /* Path to top of search */
 9817#define FSDIR_COLUMN_DIR      6     /* Path is relative to this directory */
 9818
 9819/*
 9820** UTF8 chmod() function for Windows
 9821*/
 9822#if defined(_WIN32) || defined(WIN32)
 9823static int fileio_chmod(const char *zPath, int pmode){
 9824  int rc;
 9825  wchar_t *b1 = sqlite3_win32_utf8_to_unicode(zPath);
 9826  if( b1==0 ) return -1;
 9827  rc = _wchmod(b1, pmode);
 9828  sqlite3_free(b1);
 9829  return rc;
 9830}
 9831#endif
 9832
 9833/*
 9834** UTF8 mkdir() function for Windows
 9835*/
 9836#if defined(_WIN32) || defined(WIN32)
 9837static int fileio_mkdir(const char *zPath){
 9838  int rc;
 9839  wchar_t *b1 = sqlite3_win32_utf8_to_unicode(zPath);
 9840  if( b1==0 ) return -1;
 9841  rc = _wmkdir(b1);
 9842  sqlite3_free(b1);
 9843  return rc;
 9844}
 9845#endif
 9846
 9847
 9848/*
 9849** Set the result stored by context ctx to a blob containing the 
 9850** contents of file zName.  Or, leave the result unchanged (NULL)
 9851** if the file does not exist or is unreadable.
 9852**
 9853** If the file exceeds the SQLite blob size limit, through an
 9854** SQLITE_TOOBIG error.
 9855**
 9856** Throw an SQLITE_IOERR if there are difficulties pulling the file
 9857** off of disk.
 9858*/
 9859static void readFileContents(sqlite3_context *ctx, const char *zName){
 9860  FILE *in;
 9861  sqlite3_int64 nIn;
 9862  void *pBuf;
 9863  sqlite3 *db;
 9864  int mxBlob;
 9865
 9866  in = sqlite3_fopen(zName, "rb");
 9867  if( in==0 ){
 9868    /* File does not exist or is unreadable. Leave the result set to NULL. */
 9869    return;
 9870  }
 9871  fseek(in, 0, SEEK_END);
 9872  nIn = ftell(in);
 9873  rewind(in);
 9874  db = sqlite3_context_db_handle(ctx);
 9875  mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
 9876  if( nIn>mxBlob ){
 9877    sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
 9878    fclose(in);
 9879    return;
 9880  }
 9881  pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
 9882  if( pBuf==0 ){
 9883    sqlite3_result_error_nomem(ctx);
 9884    fclose(in);
 9885    return;
 9886  }
 9887  if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
 9888    sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
 9889  }else{
 9890    sqlite3_result_error_code(ctx, SQLITE_IOERR);
 9891    sqlite3_free(pBuf);
 9892  }
 9893  fclose(in);
 9894}
 9895
 9896/*
 9897** Implementation of the "readfile(X)" SQL function.  The entire content
 9898** of the file named X is read and returned as a BLOB.  NULL is returned
 9899** if the file does not exist or is unreadable.
 9900*/
 9901static void readfileFunc(
 9902  sqlite3_context *context,
 9903  int argc,
 9904  sqlite3_value **argv
 9905){
 9906  const char *zName;
 9907  (void)(argc);  /* Unused parameter */
 9908  zName = (const char*)sqlite3_value_text(argv[0]);
 9909  if( zName==0 ) return;
 9910  readFileContents(context, zName);
 9911}
 9912
 9913/*
 9914** Set the error message contained in context ctx to the results of
 9915** vprintf(zFmt, ...).
 9916*/
 9917static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
 9918  char *zMsg = 0;
 9919  va_list ap;
 9920  va_start(ap, zFmt);
 9921  zMsg = sqlite3_vmprintf(zFmt, ap);
 9922  sqlite3_result_error(ctx, zMsg, -1);
 9923  sqlite3_free(zMsg);
 9924  va_end(ap);
 9925}
 9926
 9927#if defined(_WIN32)
 9928/*
 9929** This function is designed to convert a Win32 FILETIME structure into the
 9930** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
 9931*/
 9932static sqlite3_uint64 fileTimeToUnixTime(
 9933  LPFILETIME pFileTime
 9934){
 9935  SYSTEMTIME epochSystemTime;
 9936  ULARGE_INTEGER epochIntervals;
 9937  FILETIME epochFileTime;
 9938  ULARGE_INTEGER fileIntervals;
 9939
 9940  memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
 9941  epochSystemTime.wYear = 1970;
 9942  epochSystemTime.wMonth = 1;
 9943  epochSystemTime.wDay = 1;
 9944  SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
 9945  epochIntervals.LowPart = epochFileTime.dwLowDateTime;
 9946  epochIntervals.HighPart = epochFileTime.dwHighDateTime;
 9947
 9948  fileIntervals.LowPart = pFileTime->dwLowDateTime;
 9949  fileIntervals.HighPart = pFileTime->dwHighDateTime;
 9950
 9951  return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
 9952}
 9953#endif /* _WIN32 */
 9954
 9955/*
 9956** This function is used in place of stat().  On Windows, special handling
 9957** is required in order for the included time to be returned as UTC.  On all
 9958** other systems, this function simply calls stat().
 9959*/
 9960static int fileStat(
 9961  const char *zPath,
 9962  STRUCT_STAT *pStatBuf
 9963){
 9964#if defined(_WIN32)
 9965  int rc;
 9966  wchar_t *b1 = sqlite3_win32_utf8_to_unicode(zPath);
 9967  if( b1==0 ) return 1;
 9968  rc = _wstat(b1, pStatBuf);
 9969  if( rc==0 ){
 9970    HANDLE hFindFile;
 9971    WIN32_FIND_DATAW fd;
 9972    memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
 9973    hFindFile = FindFirstFileW(b1, &fd);
 9974    if( hFindFile!=NULL ){
 9975      pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
 9976      pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
 9977      pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
 9978      FindClose(hFindFile);
 9979    }
 9980  }
 9981  sqlite3_free(b1);
 9982  return rc;
 9983#else
 9984  return stat(zPath, pStatBuf);
 9985#endif
 9986}
 9987
 9988/*
 9989** This function is used in place of lstat().  On Windows, special handling
 9990** is required in order for the included time to be returned as UTC.  On all
 9991** other systems, this function simply calls lstat().
 9992*/
 9993static int fileLinkStat(
 9994  const char *zPath,
 9995  STRUCT_STAT *pStatBuf
 9996){
 9997#if defined(_WIN32)
 9998  return fileStat(zPath, pStatBuf);
 9999#else
10000  return lstat(zPath, pStatBuf);
10001#endif
10002}
10003
10004/*
10005** Argument zFile is the name of a file that will be created and/or written
10006** by SQL function writefile(). This function ensures that the directory
10007** zFile will be written to exists, creating it if required. The permissions
10008** for any path components created by this function are set in accordance
10009** with the current umask.
10010**
10011** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
10012** SQLITE_OK is returned if the directory is successfully created, or
10013** SQLITE_ERROR otherwise.
10014*/
10015static int makeDirectory(
10016  const char *zFile
10017){
10018  char *zCopy = sqlite3_mprintf("%s", zFile);
10019  int rc = SQLITE_OK;
10020
10021  if( zCopy==0 ){
10022    rc = SQLITE_NOMEM;
10023  }else{
10024    int nCopy = (int)strlen(zCopy);
10025    int i = 1;
10026
10027    while( rc==SQLITE_OK ){
10028      STRUCT_STAT sStat;
10029      int rc2;
10030
10031      for(; zCopy[i]!='/' && i<nCopy; i++);
10032      if( i==nCopy ) break;
10033      zCopy[i] = '\0';
10034
10035      rc2 = fileStat(zCopy, &sStat);
10036      if( rc2!=0 ){
10037        if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
10038      }else{
10039        if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
10040      }
10041      zCopy[i] = '/';
10042      i++;
10043    }
10044
10045    sqlite3_free(zCopy);
10046  }
10047
10048  return rc;
10049}
10050
10051/*
10052** This function does the work for the writefile() UDF. Refer to 
10053** header comments at the top of this file for details.
10054*/
10055static int writeFile(
10056  sqlite3_context *pCtx,          /* Context to return bytes written in */
10057  const char *zFile,              /* File to write */
10058  sqlite3_value *pData,           /* Data to write */
10059  mode_t mode,                    /* MODE parameter passed to writefile() */
10060  sqlite3_int64 mtime             /* MTIME parameter (or -1 to not set time) */
10061){
10062  if( zFile==0 ) return 1;
10063#if !defined(_WIN32) && !defined(WIN32)
10064  if( S_ISLNK(mode) ){
10065    const char *zTo = (const char*)sqlite3_value_text(pData);
10066    if( zTo==0 ) return 1;
10067    unlink(zFile);
10068    if( symlink(zTo, zFile)<0 ) return 1;
10069  }else
10070#endif
10071  {
10072    if( S_ISDIR(mode) ){
10073      if( mkdir(zFile, mode) ){
10074        /* The mkdir() call to create the directory failed. This might not
10075        ** be an error though - if there is already a directory at the same
10076        ** path and either the permissions already match or can be changed
10077        ** to do so using chmod(), it is not an error.  */
10078        STRUCT_STAT sStat;
10079        if( errno!=EEXIST
10080         || 0!=fileStat(zFile, &sStat)
10081         || !S_ISDIR(sStat.st_mode)
10082         || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
10083        ){
10084          return 1;
10085        }
10086      }
10087    }else{
10088      sqlite3_int64 nWrite = 0;
10089      const char *z;
10090      int rc = 0;
10091      FILE *out = sqlite3_fopen(zFile, "wb");
10092      if( out==0 ) return 1;
10093      z = (const char*)sqlite3_value_blob(pData);
10094      if( z ){
10095        sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
10096        nWrite = sqlite3_value_bytes(pData);
10097        if( nWrite!=n ){
10098          rc = 1;
10099        }
10100      }
10101      fclose(out);
10102      if( rc==0 && mode && chmod(zFile, mode & 0777) ){
10103        rc = 1;
10104      }
10105      if( rc ) return 2;
10106      sqlite3_result_int64(pCtx, nWrite);
10107    }
10108  }
10109
10110  if( mtime>=0 ){
10111#if defined(_WIN32)
10112    /* Windows */
10113    FILETIME lastAccess;
10114    FILETIME lastWrite;
10115    SYSTEMTIME currentTime;
10116    LONGLONG intervals;
10117    HANDLE hFile;
10118    LPWSTR zUnicodeName;
10119    extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
10120
10121    GetSystemTime(&currentTime);
10122    SystemTimeToFileTime(&currentTime, &lastAccess);
10123    intervals = (mtime*10000000) + 116444736000000000;
10124    lastWrite.dwLowDateTime = (DWORD)intervals;
10125    lastWrite.dwHighDateTime = intervals >> 32;
10126    zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
10127    if( zUnicodeName==0 ){
10128      return 1;
10129    }
10130    hFile = CreateFileW(
10131      zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
10132      FILE_FLAG_BACKUP_SEMANTICS, NULL
10133    );
10134    sqlite3_free(zUnicodeName);
10135    if( hFile!=INVALID_HANDLE_VALUE ){
10136      BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
10137      CloseHandle(hFile);
10138      return !bResult;
10139    }else{
10140      return 1;
10141    }
10142#elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
10143    /* Recent unix */
10144    struct timespec times[2];
10145    times[0].tv_nsec = times[1].tv_nsec = 0;
10146    times[0].tv_sec = time(0);
10147    times[1].tv_sec = mtime;
10148    if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
10149      return 1;
10150    }
10151#else
10152    /* Legacy unix. 
10153    **
10154    ** Do not use utimes() on a symbolic link - it sees through the link and
10155    ** modifies the timestamps on the target. Or fails if the target does 
10156    ** not exist.  */
10157    if( 0==S_ISLNK(mode) ){
10158      struct timeval times[2];
10159      times[0].tv_usec = times[1].tv_usec = 0;
10160      times[0].tv_sec = time(0);
10161      times[1].tv_sec = mtime;
10162      if( utimes(zFile, times) ){
10163        return 1;
10164      }
10165    }
10166#endif
10167  }
10168
10169  return 0;
10170}
10171
10172/*
10173** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.  
10174** Refer to header comments at the top of this file for details.
10175*/
10176static void writefileFunc(
10177  sqlite3_context *context,
10178  int argc,
10179  sqlite3_value **argv
10180){
10181  const char *zFile;
10182  mode_t mode = 0;
10183  int res;
10184  sqlite3_int64 mtime = -1;
10185
10186  if( argc<2 || argc>4 ){
10187    sqlite3_result_error(context, 
10188        "wrong number of arguments to function writefile()", -1
10189    );
10190    return;
10191  }
10192
10193  zFile = (const char*)sqlite3_value_text(argv[0]);
10194  if( zFile==0 ) return;
10195  if( argc>=3 ){
10196    mode = (mode_t)sqlite3_value_int(argv[2]);
10197  }
10198  if( argc==4 ){
10199    mtime = sqlite3_value_int64(argv[3]);
10200  }
10201
10202  res = writeFile(context, zFile, argv[1], mode, mtime);
10203  if( res==1 && errno==ENOENT ){
10204    if( makeDirectory(zFile)==SQLITE_OK ){
10205      res = writeFile(context, zFile, argv[1], mode, mtime);
10206    }
10207  }
10208
10209  if( argc>2 && res!=0 ){
10210    if( S_ISLNK(mode) ){
10211      ctxErrorMsg(context, "failed to create symlink: %s", zFile);
10212    }else if( S_ISDIR(mode) ){
10213      ctxErrorMsg(context, "failed to create directory: %s", zFile);
10214    }else{
10215      ctxErrorMsg(context, "failed to write file: %s", zFile);
10216    }
10217  }
10218}
10219
10220/*
10221** SQL function:   lsmode(MODE)
10222**
10223** Given a numberic st_mode from stat(), convert it into a human-readable
10224** text string in the style of "ls -l".
10225*/
10226static void lsModeFunc(
10227  sqlite3_context *context,
10228  int argc,
10229  sqlite3_value **argv
10230){
10231  int i;
10232  int iMode = sqlite3_value_int(argv[0]);
10233  char z[16];
10234  (void)argc;
10235  if( S_ISLNK(iMode) ){
10236    z[0] = 'l';
10237  }else if( S_ISREG(iMode) ){
10238    z[0] = '-';
10239  }else if( S_ISDIR(iMode) ){
10240    z[0] = 'd';
10241  }else{
10242    z[0] = '?';
10243  }
10244  for(i=0; i<3; i++){
10245    int m = (iMode >> ((2-i)*3));
10246    char *a = &z[1 + i*3];
10247    a[0] = (m & 0x4) ? 'r' : '-';
10248    a[1] = (m & 0x2) ? 'w' : '-';
10249    a[2] = (m & 0x1) ? 'x' : '-';
10250  }
10251  z[10] = '\0';
10252  sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
10253}
10254
10255#ifndef SQLITE_OMIT_VIRTUALTABLE
10256
10257/* 
10258** Cursor type for recursively iterating through a directory structure.
10259*/
10260typedef struct fsdir_cursor fsdir_cursor;
10261typedef struct FsdirLevel FsdirLevel;
10262
10263struct FsdirLevel {
10264  DIR *pDir;                 /* From opendir() */
10265  char *zDir;                /* Name of directory (nul-terminated) */
10266};
10267
10268struct fsdir_cursor {
10269  sqlite3_vtab_cursor base;  /* Base class - must be first */
10270
10271  int nLvl;                  /* Number of entries in aLvl[] array */
10272  int mxLvl;                 /* Maximum level */
10273  int iLvl;                  /* Index of current entry */
10274  FsdirLevel *aLvl;          /* Hierarchy of directories being traversed */
10275
10276  const char *zBase;
10277  int nBase;
10278
10279  STRUCT_STAT sStat;         /* Current lstat() results */
10280  char *zPath;               /* Path to current entry */
10281  sqlite3_int64 iRowid;      /* Current rowid */
10282};
10283
10284typedef struct fsdir_tab fsdir_tab;
10285struct fsdir_tab {
10286  sqlite3_vtab base;         /* Base class - must be first */
10287};
10288
10289/*
10290** Construct a new fsdir virtual table object.
10291*/
10292static int fsdirConnect(
10293  sqlite3 *db,
10294  void *pAux,
10295  int argc, const char *const*argv,
10296  sqlite3_vtab **ppVtab,
10297  char **pzErr
10298){
10299  fsdir_tab *pNew = 0;
10300  int rc;
10301  (void)pAux;
10302  (void)argc;
10303  (void)argv;
10304  (void)pzErr;
10305  rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
10306  if( rc==SQLITE_OK ){
10307    pNew = (fsdir_tab*)sqlite3_malloc64( sizeof(*pNew) );
10308    if( pNew==0 ) return SQLITE_NOMEM;
10309    memset(pNew, 0, sizeof(*pNew));
10310    sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
10311  }
10312  *ppVtab = (sqlite3_vtab*)pNew;
10313  return rc;
10314}
10315
10316/*
10317** This method is the destructor for fsdir vtab objects.
10318*/
10319static int fsdirDisconnect(sqlite3_vtab *pVtab){
10320  sqlite3_free(pVtab);
10321  return SQLITE_OK;
10322}
10323
10324/*
10325** Constructor for a new fsdir_cursor object.
10326*/
10327static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
10328  fsdir_cursor *pCur;
10329  (void)p;
10330  pCur = sqlite3_malloc64( sizeof(*pCur) );
10331  if( pCur==0 ) return SQLITE_NOMEM;
10332  memset(pCur, 0, sizeof(*pCur));
10333  pCur->iLvl = -1;
10334  *ppCursor = &pCur->base;
10335  return SQLITE_OK;
10336}
10337
10338/*
10339** Reset a cursor back to the state it was in when first returned
10340** by fsdirOpen().
10341*/
10342static void fsdirResetCursor(fsdir_cursor *pCur){
10343  int i;
10344  for(i=0; i<=pCur->iLvl; i++){
10345    FsdirLevel *pLvl = &pCur->aLvl[i];
10346    if( pLvl->pDir ) closedir(pLvl->pDir);
10347    sqlite3_free(pLvl->zDir);
10348  }
10349  sqlite3_free(pCur->zPath);
10350  sqlite3_free(pCur->aLvl);
10351  pCur->aLvl = 0;
10352  pCur->zPath = 0;
10353  pCur->zBase = 0;
10354  pCur->nBase = 0;
10355  pCur->nLvl = 0;
10356  pCur->iLvl = -1;
10357  pCur->iRowid = 1;
10358}
10359
10360/*
10361** Destructor for an fsdir_cursor.
10362*/
10363static int fsdirClose(sqlite3_vtab_cursor *cur){
10364  fsdir_cursor *pCur = (fsdir_cursor*)cur;
10365
10366  fsdirResetCursor(pCur);
10367  sqlite3_free(pCur);
10368  return SQLITE_OK;
10369}
10370
10371/*
10372** Set the error message for the virtual table associated with cursor
10373** pCur to the results of vprintf(zFmt, ...).
10374*/
10375static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
10376  va_list ap;
10377  va_start(ap, zFmt);
10378  pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
10379  va_end(ap);
10380}
10381
10382
10383/*
10384** Advance an fsdir_cursor to its next row of output.
10385*/
10386static int fsdirNext(sqlite3_vtab_cursor *cur){
10387  fsdir_cursor *pCur = (fsdir_cursor*)cur;
10388  mode_t m = pCur->sStat.st_mode;
10389
10390  pCur->iRowid++;
10391  if( S_ISDIR(m) && pCur->iLvl+3<pCur->mxLvl ){
10392    /* Descend into this directory */
10393    int iNew = pCur->iLvl + 1;
10394    FsdirLevel *pLvl;
10395    if( iNew>=pCur->nLvl ){
10396      int nNew = iNew+1;
10397      sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
10398      FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
10399      if( aNew==0 ) return SQLITE_NOMEM;
10400      memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
10401      pCur->aLvl = aNew;
10402      pCur->nLvl = nNew;
10403    }
10404    pCur->iLvl = iNew;
10405    pLvl = &pCur->aLvl[iNew];
10406    
10407    pLvl->zDir = pCur->zPath;
10408    pCur->zPath = 0;
10409    pLvl->pDir = opendir(pLvl->zDir);
10410    if( pLvl->pDir==0 ){
10411      fsdirSetErrmsg(pCur, "cannot read directory: %s", pLvl->zDir);
10412      return SQLITE_ERROR;
10413    }
10414  }
10415
10416  while( pCur->iLvl>=0 ){
10417    FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
10418    struct dirent *pEntry = readdir(pLvl->pDir);
10419    if( pEntry ){
10420      if( pEntry->d_name[0]=='.' ){
10421       if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
10422       if( pEntry->d_name[1]=='\0' ) continue;
10423      }
10424      sqlite3_free(pCur->zPath);
10425      pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
10426      if( pCur->zPath==0 ) return SQLITE_NOMEM;
10427      if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
10428        fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
10429        return SQLITE_ERROR;
10430      }
10431      return SQLITE_OK;
10432    }
10433    closedir(pLvl->pDir);
10434    sqlite3_free(pLvl->zDir);
10435    pLvl->pDir = 0;
10436    pLvl->zDir = 0;
10437    pCur->iLvl--;
10438  }
10439
10440  /* EOF */
10441  sqlite3_free(pCur->zPath);
10442  pCur->zPath = 0;
10443  return SQLITE_OK;
10444}
10445
10446/*
10447** Return values of columns for the row at which the series_cursor
10448** is currently pointing.
10449*/
10450static int fsdirColumn(
10451  sqlite3_vtab_cursor *cur,   /* The cursor */
10452  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
10453  int i                       /* Which column to return */
10454){
10455  fsdir_cursor *pCur = (fsdir_cursor*)cur;
10456  switch( i ){
10457    case FSDIR_COLUMN_NAME: {
10458      sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
10459      break;
10460    }
10461
10462    case FSDIR_COLUMN_MODE:
10463      sqlite3_result_int64(ctx, pCur->sStat.st_mode);
10464      break;
10465
10466    case FSDIR_COLUMN_MTIME:
10467      sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
10468      break;
10469
10470    case FSDIR_COLUMN_DATA: {
10471      mode_t m = pCur->sStat.st_mode;
10472      if( S_ISDIR(m) ){
10473        sqlite3_result_null(ctx);
10474#if !defined(_WIN32) && !defined(WIN32)
10475      }else if( S_ISLNK(m) ){
10476        char aStatic[64];
10477        char *aBuf = aStatic;
10478        sqlite3_int64 nBuf = 64;
10479        int n;
10480
10481        while( 1 ){
10482          n = readlink(pCur->zPath, aBuf, nBuf);
10483          if( n<nBuf ) break;
10484          if( aBuf!=aStatic ) sqlite3_free(aBuf);
10485          nBuf = nBuf*2;
10486          aBuf = sqlite3_malloc64(nBuf);
10487          if( aBuf==0 ){
10488            sqlite3_result_error_nomem(ctx);
10489            return SQLITE_NOMEM;
10490          }
10491        }
10492
10493        sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
10494        if( aBuf!=aStatic ) sqlite3_free(aBuf);
10495#endif
10496      }else{
10497        readFileContents(ctx, pCur->zPath);
10498      }
10499      break;
10500    }
10501    case FSDIR_COLUMN_LEVEL:
10502      sqlite3_result_int(ctx, pCur->iLvl+2);
10503      break;
10504    case FSDIR_COLUMN_PATH:
10505    default: {
10506      /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
10507      ** always return their values as NULL */
10508      break;
10509    }
10510  }
10511  return SQLITE_OK;
10512}
10513
10514/*
10515** Return the rowid for the current row. In this implementation, the
10516** first row returned is assigned rowid value 1, and each subsequent
10517** row a value 1 more than that of the previous.
10518*/
10519static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
10520  fsdir_cursor *pCur = (fsdir_cursor*)cur;
10521  *pRowid = pCur->iRowid;
10522  return SQLITE_OK;
10523}
10524
10525/*
10526** Return TRUE if the cursor has been moved off of the last
10527** row of output.
10528*/
10529static int fsdirEof(sqlite3_vtab_cursor *cur){
10530  fsdir_cursor *pCur = (fsdir_cursor*)cur;
10531  return (pCur->zPath==0);
10532}
10533
10534/*
10535** xFilter callback.
10536**
10537** idxNum bit      Meaning
10538**     0x01         PATH=N
10539**     0x02         DIR=N
10540**     0x04         LEVEL<N
10541**     0x08         LEVEL<=N  
10542*/
10543static int fsdirFilter(
10544  sqlite3_vtab_cursor *cur, 
10545  int idxNum, const char *idxStr,
10546  int argc, sqlite3_value **argv
10547){
10548  const char *zDir = 0;
10549  fsdir_cursor *pCur = (fsdir_cursor*)cur;
10550  int i;
10551  (void)idxStr;
10552  fsdirResetCursor(pCur);
10553
10554  if( idxNum==0 ){
10555    fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
10556    return SQLITE_ERROR;
10557  }
10558
10559  assert( (idxNum & 0x01)!=0 && argc>0 );
10560  zDir = (const char*)sqlite3_value_text(argv[0]);
10561  if( zDir==0 ){
10562    fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
10563    return SQLITE_ERROR;
10564  }
10565  i = 1;
10566  if( (idxNum & 0x02)!=0 ){
10567    assert( argc>i );
10568    pCur->zBase = (const char*)sqlite3_value_text(argv[i++]);
10569  }
10570  if( (idxNum & 0x0c)!=0 ){
10571    assert( argc>i );
10572    pCur->mxLvl = sqlite3_value_int(argv[i++]);
10573    if( idxNum & 0x08 ) pCur->mxLvl++;
10574    if( pCur->mxLvl<=0 ) pCur->mxLvl = 1000000000;
10575  }else{
10576    pCur->mxLvl = 1000000000;
10577  }
10578  if( pCur->zBase ){
10579    pCur->nBase = (int)strlen(pCur->zBase)+1;
10580    pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
10581  }else{
10582    pCur->zPath = sqlite3_mprintf("%s", zDir);
10583  }
10584
10585  if( pCur->zPath==0 ){
10586    return SQLITE_NOMEM;
10587  }
10588  if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
10589    fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
10590    return SQLITE_ERROR;
10591  }
10592
10593  return SQLITE_OK;
10594}
10595
10596/*
10597** SQLite will invoke this method one or more times while planning a query
10598** that uses the generate_series virtual table.  This routine needs to create
10599** a query plan for each invocation and compute an estimated cost for that
10600** plan.
10601**
10602** In this implementation idxNum is used to represent the
10603** query plan.  idxStr is unused.
10604**
10605** The query plan is represented by bits in idxNum:
10606**
10607**  0x01  The path value is supplied by argv[0]
10608**  0x02  dir is in argv[1]
10609**  0x04  maxdepth is in argv[1] or [2]
10610*/
10611static int fsdirBestIndex(
10612  sqlite3_vtab *tab,
10613  sqlite3_index_info *pIdxInfo
10614){
10615  int i;                 /* Loop over constraints */
10616  int idxPath = -1;      /* Index in pIdxInfo->aConstraint of PATH= */
10617  int idxDir = -1;       /* Index in pIdxInfo->aConstraint of DIR= */
10618  int idxLevel = -1;     /* Index in pIdxInfo->aConstraint of LEVEL< or <= */
10619  int idxLevelEQ = 0;    /* 0x08 for LEVEL<= or LEVEL=.  0x04 for LEVEL< */
10620  int omitLevel = 0;     /* omit the LEVEL constraint */
10621  int seenPath = 0;      /* True if an unusable PATH= constraint is seen */
10622  int seenDir = 0;       /* True if an unusable DIR= constraint is seen */
10623  const struct sqlite3_index_constraint *pConstraint;
10624
10625  (void)tab;
10626  pConstraint = pIdxInfo->aConstraint;
10627  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
10628    if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
10629      switch( pConstraint->iColumn ){
10630        case FSDIR_COLUMN_PATH: {
10631          if( pConstraint->usable ){
10632            idxPath = i;
10633            seenPath = 0;
10634          }else if( idxPath<0 ){
10635            seenPath = 1;
10636          }
10637          break;
10638        }
10639        case FSDIR_COLUMN_DIR: {
10640          if( pConstraint->usable ){
10641            idxDir = i;
10642            seenDir = 0;
10643          }else if( idxDir<0 ){
10644            seenDir = 1;
10645          }
10646          break;
10647        }
10648        case FSDIR_COLUMN_LEVEL: {
10649          if( pConstraint->usable && idxLevel<0 ){
10650            idxLevel = i;
10651            idxLevelEQ = 0x08;
10652            omitLevel = 0;
10653          }
10654          break;
10655        }
10656      }
10657    }else
10658    if( pConstraint->iColumn==FSDIR_COLUMN_LEVEL
10659     && pConstraint->usable
10660     && idxLevel<0
10661    ){
10662      if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE ){
10663        idxLevel = i;
10664        idxLevelEQ = 0x08;
10665        omitLevel = 1;
10666      }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT ){
10667        idxLevel = i;
10668        idxLevelEQ = 0x04;
10669        omitLevel = 1;
10670      }
10671    } 
10672  }
10673  if( seenPath || seenDir ){
10674    /* If input parameters are unusable, disallow this plan */
10675    return SQLITE_CONSTRAINT;
10676  }
10677
10678  if( idxPath<0 ){
10679    pIdxInfo->idxNum = 0;
10680    /* The pIdxInfo->estimatedCost should have been initialized to a huge
10681    ** number.  Leave it unchanged. */
10682    pIdxInfo->estimatedRows = 0x7fffffff;
10683  }else{
10684    pIdxInfo->aConstraintUsage[idxPath].omit = 1;
10685    pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
10686    pIdxInfo->idxNum = 0x01;
10687    pIdxInfo->estimatedCost = 1.0e9;
10688    i = 2;
10689    if( idxDir>=0 ){
10690      pIdxInfo->aConstraintUsage[idxDir].omit = 1;
10691      pIdxInfo->aConstraintUsage[idxDir].argvIndex = i++;
10692      pIdxInfo->idxNum |= 0x02;
10693      pIdxInfo->estimatedCost /= 1.0e4;
10694    }
10695    if( idxLevel>=0 ){
10696      pIdxInfo->aConstraintUsage[idxLevel].omit = omitLevel;
10697      pIdxInfo->aConstraintUsage[idxLevel].argvIndex = i++;
10698      pIdxInfo->idxNum |= idxLevelEQ;
10699      pIdxInfo->estimatedCost /= 1.0e4;
10700    }
10701  }
10702
10703  return SQLITE_OK;
10704}
10705
10706/*
10707** Register the "fsdir" virtual table.
10708*/
10709static int fsdirRegister(sqlite3 *db){
10710  static sqlite3_module fsdirModule = {
10711    0,                         /* iVersion */
10712    0,                         /* xCreate */
10713    fsdirConnect,              /* xConnect */
10714    fsdirBestIndex,            /* xBestIndex */
10715    fsdirDisconnect,           /* xDisconnect */
10716    0,                         /* xDestroy */
10717    fsdirOpen,                 /* xOpen - open a cursor */
10718    fsdirClose,                /* xClose - close a cursor */
10719    fsdirFilter,               /* xFilter - configure scan constraints */
10720    fsdirNext,                 /* xNext - advance a cursor */
10721    fsdirEof,                  /* xEof - check for end of scan */
10722    fsdirColumn,               /* xColumn - read data */
10723    fsdirRowid,                /* xRowid - read data */
10724    0,                         /* xUpdate */
10725    0,                         /* xBegin */
10726    0,                         /* xSync */
10727    0,                         /* xCommit */
10728    0,                         /* xRollback */
10729    0,                         /* xFindMethod */
10730    0,                         /* xRename */
10731    0,                         /* xSavepoint */
10732    0,                         /* xRelease */
10733    0,                         /* xRollbackTo */
10734    0,                         /* xShadowName */
10735    0                          /* xIntegrity */
10736  };
10737
10738  int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
10739  return rc;
10740}
10741#else         /* SQLITE_OMIT_VIRTUALTABLE */
10742# define fsdirRegister(x) SQLITE_OK
10743#endif
10744
10745/*
10746** This version of realpath() works on any system.  The string
10747** returned is held in memory allocated using sqlite3_malloc64().
10748** The caller is responsible for calling sqlite3_free().
10749*/
10750static char *portable_realpath(const char *zPath){
10751#if !defined(_WIN32)       /* BEGIN unix */
10752
10753  char *zOut = 0;          /* Result */
10754  char *z;                 /* Temporary buffer */
10755#if defined(PATH_MAX)
10756  char zBuf[PATH_MAX+1];   /* Space for the temporary buffer */
10757#endif
10758
10759  if( zPath==0 ) return 0;
10760#if defined(PATH_MAX)
10761  z = realpath(zPath, zBuf);
10762  if( z ){
10763    zOut = sqlite3_mprintf("%s", zBuf);
10764  }
10765#endif /* defined(PATH_MAX) */
10766  if( zOut==0 ){
10767    /* Try POSIX.1-2008 malloc behavior */
10768    z = realpath(zPath, NULL);
10769    if( z ){
10770      zOut = sqlite3_mprintf("%s", z);
10771      free(z);
10772    }
10773  }
10774  return zOut;
10775
10776#else /* End UNIX, Begin WINDOWS */
10777
10778  wchar_t *zPath16;        /* UTF16 translation of zPath */
10779  char *zOut = 0;          /* Result */
10780  wchar_t *z = 0;          /* Temporary buffer */
10781
10782  if( zPath==0 ) return 0;
10783
10784  zPath16 = sqlite3_win32_utf8_to_unicode(zPath);
10785  if( zPath16==0 ) return 0;
10786  z = _wfullpath(NULL, zPath16, 0);
10787  sqlite3_free(zPath16);
10788  if( z ){
10789    zOut = sqlite3_win32_unicode_to_utf8(z);
10790    free(z);
10791  }
10792  return zOut;
10793
10794#endif /* End WINDOWS, Begin common code */
10795}
10796
10797/*
10798** SQL function:   realpath(X)
10799**
10800** Try to convert file or pathname X into its real, absolute pathname.
10801** Return NULL if unable.
10802**
10803** The file or directory X is not required to exist.  The answer is formed
10804** by calling system realpath() on the prefix of X that does exist and
10805** appending the tail of X that does not (yet) exist.
10806*/
10807static void realpathFunc(
10808  sqlite3_context *context,
10809  int argc,
10810  sqlite3_value **argv
10811){
10812  const char *zPath;    /* Original input path */
10813  char *zCopy;          /* An editable copy of zPath */
10814  char *zOut;           /* The result */
10815  char cSep = 0;        /* Separator turned into \000 */
10816  size_t len;           /* Prefix length before cSep */
10817#ifdef _WIN32
10818  const int isWin = 1;
10819#else
10820  const int isWin = 0;
10821#endif
10822
10823  (void)argc;
10824  zPath = (const char*)sqlite3_value_text(argv[0]);
10825  if( zPath==0 ) return;
10826  if( zPath[0]==0 ) zPath = ".";
10827  zCopy = sqlite3_mprintf("%s",zPath);
10828  len = strlen(zCopy);
10829  while( len>1 && (zCopy[len-1]=='/' || (isWin && zCopy[len-1]=='\\')) ){
10830    len--;
10831  }
10832  zCopy[len] = 0;
10833  while( 1 /*exit-by-break*/ ){
10834    zOut = portable_realpath(zCopy);
10835    zCopy[len] = cSep;
10836    if( zOut ){
10837      if( cSep ){
10838        zOut = sqlite3_mprintf("%z%s",zOut,&zCopy[len]);
10839      }
10840      break;
10841    }else{
10842      size_t i = len-1;
10843      while( i>0 ){
10844        if( zCopy[i]=='/' || (isWin && zCopy[i]=='\\') ) break;
10845        i--;
10846      }
10847      if( i<=0 ){
10848        if( zCopy[0]=='/' ){
10849          zOut = zCopy;
10850          zCopy = 0;
10851        }else if( (zOut = portable_realpath("."))!=0 ){
10852          zOut = sqlite3_mprintf("%z/%s", zOut, zCopy);
10853        }          
10854        break;
10855      }
10856      cSep = zCopy[i];
10857      zCopy[i] = 0;
10858      len = i;
10859    }
10860  }
10861  sqlite3_free(zCopy);
10862  if( zOut ){
10863    /* Simplify any "/./" or "/../" that might have snuck into the
10864    ** pathname due to appending of zCopy.  We only have to consider
10865    ** unix "/" separators, because the _wfilepath() system call on
10866    ** Windows will have already done this simplification for us. */
10867    size_t i, j, n;
10868    n = strlen(zOut);
10869    for(i=j=0; i<n; i++){
10870      if( zOut[i]=='/' ){
10871        if( zOut[i+1]=='/' ) continue;
10872        if( zOut[i+1]=='.' && i+2<n && zOut[i+2]=='/' ){
10873          i += 1;
10874          continue;
10875        }
10876        if( zOut[i+1]=='.' && i+3<n && zOut[i+2]=='.' && zOut[i+3]=='/' ){
10877          while( j>0 && zOut[j-1]!='/' ){ j--; }
10878          if( j>0 ){ j--; }
10879          i += 2;
10880          continue;
10881        }
10882      }
10883      zOut[j++] = zOut[i];
10884    }
10885    zOut[j] = 0;
10886
10887    /* Return the result */
10888    sqlite3_result_text(context, zOut, -1, sqlite3_free);
10889  }
10890}
10891
10892
10893#ifdef _WIN32
10894
10895#endif
10896int sqlite3_fileio_init(
10897  sqlite3 *db, 
10898  char **pzErrMsg, 
10899  const sqlite3_api_routines *pApi
10900){
10901  int rc = SQLITE_OK;
10902  SQLITE_EXTENSION_INIT2(pApi);
10903  (void)pzErrMsg;  /* Unused parameter */
10904  rc = sqlite3_create_function(db, "readfile", 1, 
10905                               SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
10906                               readfileFunc, 0, 0);
10907  if( rc==SQLITE_OK ){
10908    rc = sqlite3_create_function(db, "writefile", -1,
10909                                 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
10910                                 writefileFunc, 0, 0);
10911  }
10912  if( rc==SQLITE_OK ){
10913    rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
10914                                 lsModeFunc, 0, 0);
10915  }
10916  if( rc==SQLITE_OK ){
10917    rc = fsdirRegister(db);
10918  }
10919  if( rc==SQLITE_OK ){
10920    rc = sqlite3_create_function(db, "realpath", 1,
10921                                 SQLITE_UTF8, 0,
10922                                 realpathFunc, 0, 0);
10923  }
10924  return rc;
10925}
10926
10927/************************* End ext/misc/fileio.c ********************/
10928/************************* Begin ext/misc/completion.c ******************/
10929/*
10930** 2017-07-10
10931**
10932** The author disclaims copyright to this source code.  In place of
10933** a legal notice, here is a blessing:
10934**
10935**    May you do good and not evil.
10936**    May you find forgiveness for yourself and forgive others.
10937**    May you share freely, never taking more than you give.
10938**
10939*************************************************************************
10940**
10941** This file implements an eponymous virtual table that returns suggested
10942** completions for a partial SQL input.
10943**
10944** Suggested usage:
10945**
10946**     SELECT DISTINCT candidate COLLATE nocase
10947**       FROM completion($prefix,$wholeline)
10948**      ORDER BY 1;
10949**
10950** The two query parameters are optional.  $prefix is the text of the
10951** current word being typed and that is to be completed.  $wholeline is
10952** the complete input line, used for context.
10953**
10954** The raw completion() table might return the same candidate multiple
10955** times, for example if the same column name is used to two or more
10956** tables.  And the candidates are returned in an arbitrary order.  Hence,
10957** the DISTINCT and ORDER BY are recommended.
10958**
10959** This virtual table operates at the speed of human typing, and so there
10960** is no attempt to make it fast.  Even a slow implementation will be much
10961** faster than any human can type.
10962**
10963*/
10964/* #include "sqlite3ext.h" */
10965SQLITE_EXTENSION_INIT1
10966#include <assert.h>
10967#include <string.h>
10968#include <ctype.h>
10969
10970#ifndef SQLITE_OMIT_VIRTUALTABLE
10971
10972#ifndef IsAlnum
10973#define IsAlnum(X)  isalnum((unsigned char)X)
10974#endif
10975
10976
10977/* completion_vtab is a subclass of sqlite3_vtab which will
10978** serve as the underlying representation of a completion virtual table
10979*/
10980typedef struct completion_vtab completion_vtab;
10981struct completion_vtab {
10982  sqlite3_vtab base;  /* Base class - must be first */
10983  sqlite3 *db;        /* Database connection for this completion vtab */
10984};
10985
10986/* completion_cursor is a subclass of sqlite3_vtab_cursor which will
10987** serve as the underlying representation of a cursor that scans
10988** over rows of the result
10989*/
10990typedef struct completion_cursor completion_cursor;
10991struct completion_cursor {
10992  sqlite3_vtab_cursor base;  /* Base class - must be first */
10993  sqlite3 *db;               /* Database connection for this cursor */
10994  int nPrefix, nLine;        /* Number of bytes in zPrefix and zLine */
10995  char *zPrefix;             /* The prefix for the word we want to complete */
10996  char *zLine;               /* The whole that we want to complete */
10997  const char *zCurrentRow;   /* Current output row */
10998  int szRow;                 /* Length of the zCurrentRow string */
10999  sqlite3_stmt *pStmt;       /* Current statement */
11000  sqlite3_int64 iRowid;      /* The rowid */
11001  int ePhase;                /* Current phase */
11002  int j;                     /* inter-phase counter */
11003};
11004
11005/* Values for ePhase:
11006*/
11007#define COMPLETION_FIRST_PHASE   1
11008#define COMPLETION_KEYWORDS      1
11009#define COMPLETION_PRAGMAS       2
11010#define COMPLETION_FUNCTIONS     3
11011#define COMPLETION_COLLATIONS    4
11012#define COMPLETION_INDEXES       5
11013#define COMPLETION_TRIGGERS      6
11014#define COMPLETION_DATABASES     7
11015#define COMPLETION_TABLES        8    /* Also VIEWs and TRIGGERs */
11016#define COMPLETION_COLUMNS       9
11017#define COMPLETION_MODULES       10
11018#define COMPLETION_EOF           11
11019
11020/*
11021** The completionConnect() method is invoked to create a new
11022** completion_vtab that describes the completion virtual table.
11023**
11024** Think of this routine as the constructor for completion_vtab objects.
11025**
11026** All this routine needs to do is:
11027**
11028**    (1) Allocate the completion_vtab object and initialize all fields.
11029**
11030**    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
11031**        result set of queries against completion will look like.
11032*/
11033static int completionConnect(
11034  sqlite3 *db,
11035  void *pAux,
11036  int argc, const char *const*argv,
11037  sqlite3_vtab **ppVtab,
11038  char **pzErr
11039){
11040  completion_vtab *pNew;
11041  int rc;
11042
11043  (void)(pAux);    /* Unused parameter */
11044  (void)(argc);    /* Unused parameter */
11045  (void)(argv);    /* Unused parameter */
11046  (void)(pzErr);   /* Unused parameter */
11047
11048/* Column numbers */
11049#define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
11050#define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
11051#define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
11052#define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
11053
11054  sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
11055  rc = sqlite3_declare_vtab(db,
11056      "CREATE TABLE x("
11057      "  candidate TEXT,"
11058      "  prefix TEXT HIDDEN,"
11059      "  wholeline TEXT HIDDEN,"
11060      "  phase INT HIDDEN"        /* Used for debugging only */
11061      ")");
11062  if( rc==SQLITE_OK ){
11063    pNew = sqlite3_malloc64( sizeof(*pNew) );
11064    *ppVtab = (sqlite3_vtab*)pNew;
11065    if( pNew==0 ) return SQLITE_NOMEM;
11066    memset(pNew, 0, sizeof(*pNew));
11067    pNew->db = db;
11068  }
11069  return rc;
11070}
11071
11072/*
11073** This method is the destructor for completion_cursor objects.
11074*/
11075static int completionDisconnect(sqlite3_vtab *pVtab){
11076  sqlite3_free(pVtab);
11077  return SQLITE_OK;
11078}
11079
11080/*
11081** Constructor for a new completion_cursor object.
11082*/
11083static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
11084  completion_cursor *pCur;
11085  pCur = sqlite3_malloc64( sizeof(*pCur) );
11086  if( pCur==0 ) return SQLITE_NOMEM;
11087  memset(pCur, 0, sizeof(*pCur));
11088  pCur->db = ((completion_vtab*)p)->db;
11089  *ppCursor = &pCur->base;
11090  return SQLITE_OK;
11091}
11092
11093/*
11094** Reset the completion_cursor.
11095*/
11096static void completionCursorReset(completion_cursor *pCur){
11097  sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;
11098  sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;
11099  sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
11100  pCur->j = 0;
11101}
11102
11103/*
11104** Destructor for a completion_cursor.
11105*/
11106static int completionClose(sqlite3_vtab_cursor *cur){
11107  completionCursorReset((completion_cursor*)cur);
11108  sqlite3_free(cur);
11109  return SQLITE_OK;
11110}
11111
11112/*
11113** Advance a completion_cursor to its next row of output.
11114**
11115** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
11116** record the current state of the scan.  This routine sets ->zCurrentRow
11117** to the current row of output and then returns.  If no more rows remain,
11118** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
11119** table that has reached the end of its scan.
11120**
11121** The current implementation just lists potential identifiers and
11122** keywords and filters them by zPrefix.  Future enhancements should
11123** take zLine into account to try to restrict the set of identifiers and
11124** keywords based on what would be legal at the current point of input.
11125*/
11126static int completionNext(sqlite3_vtab_cursor *cur){
11127  completion_cursor *pCur = (completion_cursor*)cur;
11128  int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
11129  int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
11130  int rc;
11131  pCur->iRowid++;
11132  while( pCur->ePhase!=COMPLETION_EOF ){
11133    switch( pCur->ePhase ){
11134      case COMPLETION_KEYWORDS: {
11135        if( pCur->j >= sqlite3_keyword_count() ){
11136          pCur->zCurrentRow = 0;
11137          pCur->ePhase = COMPLETION_DATABASES;
11138        }else{
11139          sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
11140        }
11141        iCol = -1;
11142        break;
11143      }
11144      case COMPLETION_DATABASES: {
11145        if( pCur->pStmt==0 ){
11146          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
11147                             &pCur->pStmt, 0);
11148        }
11149        iCol = 1;
11150        eNextPhase = COMPLETION_TABLES;
11151        break;
11152      }
11153      case COMPLETION_TABLES: {
11154        if( pCur->pStmt==0 ){
11155          sqlite3_stmt *pS2;
11156          sqlite3_str* pStr = sqlite3_str_new(pCur->db);
11157          char *zSql = 0;
11158          const char *zSep = "";
11159          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
11160          while( sqlite3_step(pS2)==SQLITE_ROW ){
11161            const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
11162            sqlite3_str_appendf(pStr,
11163               "%s"
11164               "SELECT name FROM \"%w\".sqlite_schema",
11165               zSep, zDb
11166            );
11167            zSep = " UNION ";
11168          }
11169          rc = sqlite3_finalize(pS2);
11170          zSql = sqlite3_str_finish(pStr);
11171          if( zSql==0 ) return SQLITE_NOMEM;
11172          if( rc==SQLITE_OK ){
11173            sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
11174          }
11175          sqlite3_free(zSql);
11176          if( rc ) return rc;
11177        }
11178        iCol = 0;
11179        eNextPhase = COMPLETION_COLUMNS;
11180        break;
11181      }
11182      case COMPLETION_COLUMNS: {
11183        if( pCur->pStmt==0 ){
11184          sqlite3_stmt *pS2;
11185          sqlite3_str *pStr = sqlite3_str_new(pCur->db);
11186          char *zSql = 0;
11187          const char *zSep = "";
11188          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
11189          while( sqlite3_step(pS2)==SQLITE_ROW ){
11190            const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
11191            sqlite3_str_appendf(pStr,
11192               "%s"
11193               "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
11194                       " JOIN pragma_table_xinfo(sm.name,%Q) AS pti"
11195               " WHERE sm.type='table'",
11196               zSep, zDb, zDb
11197            );
11198            zSep = " UNION ";
11199          }
11200          rc = sqlite3_finalize(pS2);
11201          zSql = sqlite3_str_finish(pStr);
11202          if( zSql==0 ) return SQLITE_NOMEM;
11203          if( rc==SQLITE_OK ){
11204            sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
11205          }
11206          sqlite3_free(zSql);
11207          if( rc ) return rc;
11208        }
11209        iCol = 0;
11210        eNextPhase = COMPLETION_EOF;
11211        break;
11212      }
11213    }
11214    if( iCol<0 ){
11215      /* This case is when the phase presets zCurrentRow */
11216      if( pCur->zCurrentRow==0 ) continue;
11217    }else{
11218      if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
11219        /* Extract the next row of content */
11220        pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
11221        pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
11222      }else{
11223        /* When all rows are finished, advance to the next phase */
11224        rc = sqlite3_finalize(pCur->pStmt);
11225        pCur->pStmt = 0;
11226        pCur->ePhase = eNextPhase;
11227        if( rc ) return rc;
11228        continue;
11229      }
11230    }
11231    if( pCur->nPrefix==0 ) break;
11232    if( pCur->nPrefix<=pCur->szRow
11233     && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
11234    ){
11235      break;
11236    }
11237  }
11238
11239  return SQLITE_OK;
11240}
11241
11242/*
11243** Return values of columns for the row at which the completion_cursor
11244** is currently pointing.
11245*/
11246static int completionColumn(
11247  sqlite3_vtab_cursor *cur,   /* The cursor */
11248  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
11249  int i                       /* Which column to return */
11250){
11251  completion_cursor *pCur = (completion_cursor*)cur;
11252  switch( i ){
11253    case COMPLETION_COLUMN_CANDIDATE: {
11254      sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
11255      break;
11256    }
11257    case COMPLETION_COLUMN_PREFIX: {
11258      sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
11259      break;
11260    }
11261    case COMPLETION_COLUMN_WHOLELINE: {
11262      sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
11263      break;
11264    }
11265    case COMPLETION_COLUMN_PHASE: {
11266      sqlite3_result_int(ctx, pCur->ePhase);
11267      break;
11268    }
11269  }
11270  return SQLITE_OK;
11271}
11272
11273/*
11274** Return the rowid for the current row.  In this implementation, the
11275** rowid is the same as the output value.
11276*/
11277static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
11278  completion_cursor *pCur = (completion_cursor*)cur;
11279  *pRowid = pCur->iRowid;
11280  return SQLITE_OK;
11281}
11282
11283/*
11284** Return TRUE if the cursor has been moved off of the last
11285** row of output.
11286*/
11287static int completionEof(sqlite3_vtab_cursor *cur){
11288  completion_cursor *pCur = (completion_cursor*)cur;
11289  return pCur->ePhase >= COMPLETION_EOF;
11290}
11291
11292/*
11293** This method is called to "rewind" the completion_cursor object back
11294** to the first row of output.  This method is always called at least
11295** once prior to any call to completionColumn() or completionRowid() or 
11296** completionEof().
11297*/
11298static int completionFilter(
11299  sqlite3_vtab_cursor *pVtabCursor, 
11300  int idxNum, const char *idxStr,
11301  int argc, sqlite3_value **argv
11302){
11303  completion_cursor *pCur = (completion_cursor *)pVtabCursor;
11304  int iArg = 0;
11305  (void)(idxStr);   /* Unused parameter */
11306  (void)(argc);     /* Unused parameter */
11307  completionCursorReset(pCur);
11308  if( idxNum & 1 ){
11309    pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
11310    if( pCur->nPrefix>0 ){
11311      pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
11312      if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
11313      pCur->nPrefix = (int)strlen(pCur->zPrefix);
11314    }
11315    iArg = 1;
11316  }
11317  if( idxNum & 2 ){
11318    pCur->nLine = sqlite3_value_bytes(argv[iArg]);
11319    if( pCur->nLine>0 ){
11320      pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
11321      if( pCur->zLine==0 ) return SQLITE_NOMEM;
11322      pCur->nLine = (int)strlen(pCur->zLine);
11323    }
11324  }
11325  if( pCur->zLine!=0 && pCur->zPrefix==0 ){
11326    int i = pCur->nLine;
11327    while( i>0 && (IsAlnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
11328      i--;
11329    }
11330    pCur->nPrefix = pCur->nLine - i;
11331    if( pCur->nPrefix>0 ){
11332      pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
11333      if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
11334      pCur->nPrefix = (int)strlen(pCur->zPrefix);
11335    }
11336  }
11337  pCur->iRowid = 0;
11338  pCur->ePhase = COMPLETION_FIRST_PHASE;
11339  return completionNext(pVtabCursor);
11340}
11341
11342/*
11343** SQLite will invoke this method one or more times while planning a query
11344** that uses the completion virtual table.  This routine needs to create
11345** a query plan for each invocation and compute an estimated cost for that
11346** plan.
11347**
11348** There are two hidden parameters that act as arguments to the table-valued
11349** function:  "prefix" and "wholeline".  Bit 0 of idxNum is set if "prefix"
11350** is available and bit 1 is set if "wholeline" is available.
11351*/
11352static int completionBestIndex(
11353  sqlite3_vtab *tab,
11354  sqlite3_index_info *pIdxInfo
11355){
11356  int i;                 /* Loop over constraints */
11357  int idxNum = 0;        /* The query plan bitmask */
11358  int prefixIdx = -1;    /* Index of the start= constraint, or -1 if none */
11359  int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
11360  int nArg = 0;          /* Number of arguments that completeFilter() expects */
11361  const struct sqlite3_index_constraint *pConstraint;
11362
11363  (void)(tab);    /* Unused parameter */
11364  pConstraint = pIdxInfo->aConstraint;
11365  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
11366    if( pConstraint->usable==0 ) continue;
11367    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
11368    switch( pConstraint->iColumn ){
11369      case COMPLETION_COLUMN_PREFIX:
11370        prefixIdx = i;
11371        idxNum |= 1;
11372        break;
11373      case COMPLETION_COLUMN_WHOLELINE:
11374        wholelineIdx = i;
11375        idxNum |= 2;
11376        break;
11377    }
11378  }
11379  if( prefixIdx>=0 ){
11380    pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
11381    pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
11382  }
11383  if( wholelineIdx>=0 ){
11384    pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
11385    pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
11386  }
11387  pIdxInfo->idxNum = idxNum;
11388  pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
11389  pIdxInfo->estimatedRows = 500 - 100*nArg;
11390  return SQLITE_OK;
11391}
11392
11393/*
11394** This following structure defines all the methods for the 
11395** completion virtual table.
11396*/
11397static sqlite3_module completionModule = {
11398  0,                         /* iVersion */
11399  0,                         /* xCreate */
11400  completionConnect,         /* xConnect */
11401  completionBestIndex,       /* xBestIndex */
11402  completionDisconnect,      /* xDisconnect */
11403  0,                         /* xDestroy */
11404  completionOpen,            /* xOpen - open a cursor */
11405  completionClose,           /* xClose - close a cursor */
11406  completionFilter,          /* xFilter - configure scan constraints */
11407  completionNext,            /* xNext - advance a cursor */
11408  completionEof,             /* xEof - check for end of scan */
11409  completionColumn,          /* xColumn - read data */
11410  completionRowid,           /* xRowid - read data */
11411  0,                         /* xUpdate */
11412  0,                         /* xBegin */
11413  0,                         /* xSync */
11414  0,                         /* xCommit */
11415  0,                         /* xRollback */
11416  0,                         /* xFindMethod */
11417  0,                         /* xRename */
11418  0,                         /* xSavepoint */
11419  0,                         /* xRelease */
11420  0,                         /* xRollbackTo */
11421  0,                         /* xShadowName */
11422  0                          /* xIntegrity */
11423};
11424
11425#endif /* SQLITE_OMIT_VIRTUALTABLE */
11426
11427int sqlite3CompletionVtabInit(sqlite3 *db){
11428  int rc = SQLITE_OK;
11429#ifndef SQLITE_OMIT_VIRTUALTABLE
11430  rc = sqlite3_create_module(db, "completion", &completionModule, 0);
11431#endif
11432  return rc;
11433}
11434
11435#ifdef _WIN32
11436
11437#endif
11438int sqlite3_completion_init(
11439  sqlite3 *db, 
11440  char **pzErrMsg, 
11441  const sqlite3_api_routines *pApi
11442){
11443  int rc = SQLITE_OK;
11444  SQLITE_EXTENSION_INIT2(pApi);
11445  (void)(pzErrMsg);  /* Unused parameter */
11446#ifndef SQLITE_OMIT_VIRTUALTABLE
11447  rc = sqlite3CompletionVtabInit(db);
11448#endif
11449  return rc;
11450}
11451
11452/************************* End ext/misc/completion.c ********************/
11453/************************* Begin ext/misc/appendvfs.c ******************/
11454/*
11455** 2017-10-20
11456**
11457** The author disclaims copyright to this source code.  In place of
11458** a legal notice, here is a blessing:
11459**
11460**    May you do good and not evil.
11461**    May you find forgiveness for yourself and forgive others.
11462**    May you share freely, never taking more than you give.
11463**
11464******************************************************************************
11465**
11466** This file implements a VFS shim that allows an SQLite database to be
11467** appended onto the end of some other file, such as an executable.
11468**
11469** A special record must appear at the end of the file that identifies the
11470** file as an appended database and provides the offset to the first page
11471** of the exposed content. (Or, it is the length of the content prefix.)
11472** For best performance page 1 should be located at a disk page boundary,
11473** though that is not required.
11474**
11475** When opening a database using this VFS, the connection might treat
11476** the file as an ordinary SQLite database, or it might treat it as a
11477** database appended onto some other file.  The decision is made by
11478** applying the following rules in order:
11479**
11480**  (1)  An empty file is an ordinary database.
11481**
11482**  (2)  If the file ends with the appendvfs trailer string
11483**       "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
11484**
11485**  (3)  If the file begins with the standard SQLite prefix string
11486**       "SQLite format 3", that file is an ordinary database.
11487**
11488**  (4)  If none of the above apply and the SQLITE_OPEN_CREATE flag is
11489**       set, then a new database is appended to the already existing file.
11490**
11491**  (5)  Otherwise, SQLITE_CANTOPEN is returned.
11492**
11493** To avoid unnecessary complications with the PENDING_BYTE, the size of
11494** the file containing the database is limited to 1GiB. (1073741824 bytes)
11495** This VFS will not read or write past the 1GiB mark.  This restriction
11496** might be lifted in future versions.  For now, if you need a larger
11497** database, then keep it in a separate file.
11498**
11499** If the file being opened is a plain database (not an appended one), then
11500** this shim is a pass-through into the default underlying VFS. (rule 3)
11501**/
11502/* #include "sqlite3ext.h" */
11503SQLITE_EXTENSION_INIT1
11504#include <string.h>
11505#include <assert.h>
11506
11507/* The append mark at the end of the database is:
11508**
11509**     Start-Of-SQLite3-NNNNNNNN
11510**     123456789 123456789 12345
11511**
11512** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
11513** the offset to page 1, and also the length of the prefix content.
11514*/
11515#define APND_MARK_PREFIX     "Start-Of-SQLite3-"
11516#define APND_MARK_PREFIX_SZ  17
11517#define APND_MARK_FOS_SZ      8
11518#define APND_MARK_SIZE       (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)
11519
11520/*
11521** Maximum size of the combined prefix + database + append-mark.  This
11522** must be less than 0x40000000 to avoid locking issues on Windows.
11523*/
11524#define APND_MAX_SIZE  (0x40000000)
11525
11526/*
11527** Try to align the database to an even multiple of APND_ROUNDUP bytes.
11528*/
11529#ifndef APND_ROUNDUP
11530#define APND_ROUNDUP 4096
11531#endif
11532#define APND_ALIGN_MASK         ((sqlite3_int64)(APND_ROUNDUP-1))
11533#define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK)
11534
11535/*
11536** Forward declaration of objects used by this utility
11537*/
11538typedef struct sqlite3_vfs ApndVfs;
11539typedef struct ApndFile ApndFile;
11540
11541/* Access to a lower-level VFS that (might) implement dynamic loading,
11542** access to randomness, etc.
11543*/
11544#define ORIGVFS(p)  ((sqlite3_vfs*)((p)->pAppData))
11545#define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
11546
11547/* An open appendvfs file
11548**
11549** An instance of this structure describes the appended database file.
11550** A separate sqlite3_file object is always appended. The appended
11551** sqlite3_file object (which can be accessed using ORIGFILE()) describes
11552** the entire file, including the prefix, the database, and the
11553** append-mark.
11554**
11555** The structure of an AppendVFS database is like this:
11556**
11557**   +-------------+---------+----------+-------------+
11558**   | prefix-file | padding | database | append-mark |
11559**   +-------------+---------+----------+-------------+
11560**                           ^          ^
11561**                           |          |
11562**                         iPgOne      iMark
11563**
11564**
11565** "prefix file" -  file onto which the database has been appended.
11566** "padding"     -  zero or more bytes inserted so that "database"
11567**                  starts on an APND_ROUNDUP boundary
11568** "database"    -  The SQLite database file
11569** "append-mark" -  The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
11570**                  the offset from the start of prefix-file to the start
11571**                  of "database".
11572**
11573** The size of the database is iMark - iPgOne.
11574**
11575** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
11576** of iPgOne stored as a big-ending 64-bit integer.
11577**
11578** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE).
11579** Or, iMark is -1 to indicate that it has not yet been written.
11580*/
11581struct ApndFile {
11582  sqlite3_file base;        /* Subclass.  MUST BE FIRST! */
11583  sqlite3_int64 iPgOne;     /* Offset to the start of the database */
11584  sqlite3_int64 iMark;      /* Offset of the append mark.  -1 if unwritten */
11585  /* Always followed by another sqlite3_file that describes the whole file */
11586};
11587
11588/*
11589** Methods for ApndFile
11590*/
11591static int apndClose(sqlite3_file*);
11592static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
11593static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
11594static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
11595static int apndSync(sqlite3_file*, int flags);
11596static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
11597static int apndLock(sqlite3_file*, int);
11598static int apndUnlock(sqlite3_file*, int);
11599static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
11600static int apndFileControl(sqlite3_file*, int op, void *pArg);
11601static int apndSectorSize(sqlite3_file*);
11602static int apndDeviceCharacteristics(sqlite3_file*);
11603static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
11604static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
11605static void apndShmBarrier(sqlite3_file*);
11606static int apndShmUnmap(sqlite3_file*, int deleteFlag);
11607static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
11608static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
11609
11610/*
11611** Methods for ApndVfs
11612*/
11613static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
11614static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
11615static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
11616static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
11617static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
11618static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
11619static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
11620static void apndDlClose(sqlite3_vfs*, void*);
11621static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
11622static int apndSleep(sqlite3_vfs*, int microseconds);
11623static int apndCurrentTime(sqlite3_vfs*, double*);
11624static int apndGetLastError(sqlite3_vfs*, int, char *);
11625static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
11626static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
11627static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
11628static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
11629
11630static sqlite3_vfs apnd_vfs = {
11631  3,                            /* iVersion (set when registered) */
11632  0,                            /* szOsFile (set when registered) */
11633  1024,                         /* mxPathname */
11634  0,                            /* pNext */
11635  "apndvfs",                    /* zName */
11636  0,                            /* pAppData (set when registered) */ 
11637  apndOpen,                     /* xOpen */
11638  apndDelete,                   /* xDelete */
11639  apndAccess,                   /* xAccess */
11640  apndFullPathname,             /* xFullPathname */
11641  apndDlOpen,                   /* xDlOpen */
11642  apndDlError,                  /* xDlError */
11643  apndDlSym,                    /* xDlSym */
11644  apndDlClose,                  /* xDlClose */
11645  apndRandomness,               /* xRandomness */
11646  apndSleep,                    /* xSleep */
11647  apndCurrentTime,              /* xCurrentTime */
11648  apndGetLastError,             /* xGetLastError */
11649  apndCurrentTimeInt64,         /* xCurrentTimeInt64 */
11650  apndSetSystemCall,            /* xSetSystemCall */
11651  apndGetSystemCall,            /* xGetSystemCall */
11652  apndNextSystemCall            /* xNextSystemCall */
11653};
11654
11655static const sqlite3_io_methods apnd_io_methods = {
11656  3,                              /* iVersion */
11657  apndClose,                      /* xClose */
11658  apndRead,                       /* xRead */
11659  apndWrite,                      /* xWrite */
11660  apndTruncate,                   /* xTruncate */
11661  apndSync,                       /* xSync */
11662  apndFileSize,                   /* xFileSize */
11663  apndLock,                       /* xLock */
11664  apndUnlock,                     /* xUnlock */
11665  apndCheckReservedLock,          /* xCheckReservedLock */
11666  apndFileControl,                /* xFileControl */
11667  apndSectorSize,                 /* xSectorSize */
11668  apndDeviceCharacteristics,      /* xDeviceCharacteristics */
11669  apndShmMap,                     /* xShmMap */
11670  apndShmLock,                    /* xShmLock */
11671  apndShmBarrier,                 /* xShmBarrier */
11672  apndShmUnmap,                   /* xShmUnmap */
11673  apndFetch,                      /* xFetch */
11674  apndUnfetch                     /* xUnfetch */
11675};
11676
11677/*
11678** Close an apnd-file.
11679*/
11680static int apndClose(sqlite3_file *pFile){
11681  pFile = ORIGFILE(pFile);
11682  return pFile->pMethods->xClose(pFile);
11683}
11684
11685/*
11686** Read data from an apnd-file.
11687*/
11688static int apndRead(
11689  sqlite3_file *pFile, 
11690  void *zBuf, 
11691  int iAmt, 
11692  sqlite_int64 iOfst
11693){
11694  ApndFile *paf = (ApndFile *)pFile;
11695  pFile = ORIGFILE(pFile);
11696  return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
11697}
11698
11699/*
11700** Add the append-mark onto what should become the end of the file.
11701*  If and only if this succeeds, internal ApndFile.iMark is updated.
11702*  Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
11703*/
11704static int apndWriteMark(
11705  ApndFile *paf,
11706  sqlite3_file *pFile,
11707  sqlite_int64 iWriteEnd
11708){
11709  sqlite_int64 iPgOne = paf->iPgOne;
11710  unsigned char a[APND_MARK_SIZE];
11711  int i = APND_MARK_FOS_SZ;
11712  int rc;
11713  assert(pFile == ORIGFILE(paf));
11714  memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
11715  while( --i >= 0 ){
11716    a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff);
11717    iPgOne >>= 8;
11718  }
11719  iWriteEnd += paf->iPgOne;
11720  if( SQLITE_OK==(rc = pFile->pMethods->xWrite
11721                  (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){
11722    paf->iMark = iWriteEnd;
11723  }
11724  return rc;
11725}
11726
11727/*
11728** Write data to an apnd-file.
11729*/
11730static int apndWrite(
11731  sqlite3_file *pFile,
11732  const void *zBuf,
11733  int iAmt,
11734  sqlite_int64 iOfst
11735){
11736  ApndFile *paf = (ApndFile *)pFile;
11737  sqlite_int64 iWriteEnd = iOfst + iAmt;
11738  if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL;
11739  pFile = ORIGFILE(pFile);
11740  /* If append-mark is absent or will be overwritten, write it. */
11741  if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){
11742    int rc = apndWriteMark(paf, pFile, iWriteEnd);
11743    if( SQLITE_OK!=rc ) return rc;
11744  }
11745  return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
11746}
11747
11748/*
11749** Truncate an apnd-file.
11750*/
11751static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
11752  ApndFile *paf = (ApndFile *)pFile;
11753  pFile = ORIGFILE(pFile);
11754  /* The append mark goes out first so truncate failure does not lose it. */
11755  if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR;
11756  /* Truncate underlying file just past append mark */
11757  return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE);
11758}
11759
11760/*
11761** Sync an apnd-file.
11762*/
11763static int apndSync(sqlite3_file *pFile, int flags){
11764  pFile = ORIGFILE(pFile);
11765  return pFile->pMethods->xSync(pFile, flags);
11766}
11767
11768/*
11769** Return the current file-size of an apnd-file.
11770** If the append mark is not yet there, the file-size is 0.
11771*/
11772static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
11773  ApndFile *paf = (ApndFile *)pFile;
11774  *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0;
11775  return SQLITE_OK;
11776}
11777
11778/*
11779** Lock an apnd-file.
11780*/
11781static int apndLock(sqlite3_file *pFile, int eLock){
11782  pFile = ORIGFILE(pFile);
11783  return pFile->pMethods->xLock(pFile, eLock);
11784}
11785
11786/*
11787** Unlock an apnd-file.
11788*/
11789static int apndUnlock(sqlite3_file *pFile, int eLock){
11790  pFile = ORIGFILE(pFile);
11791  return pFile->pMethods->xUnlock(pFile, eLock);
11792}
11793
11794/*
11795** Check if another file-handle holds a RESERVED lock on an apnd-file.
11796*/
11797static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
11798  pFile = ORIGFILE(pFile);
11799  return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
11800}
11801
11802/*
11803** File control method. For custom operations on an apnd-file.
11804*/
11805static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
11806  ApndFile *paf = (ApndFile *)pFile;
11807  int rc;
11808  pFile = ORIGFILE(pFile);
11809  if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne;
11810  rc = pFile->pMethods->xFileControl(pFile, op, pArg);
11811  if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
11812    *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg);
11813  }
11814  return rc;
11815}
11816
11817/*
11818** Return the sector-size in bytes for an apnd-file.
11819*/
11820static int apndSectorSize(sqlite3_file *pFile){
11821  pFile = ORIGFILE(pFile);
11822  return pFile->pMethods->xSectorSize(pFile);
11823}
11824
11825/*
11826** Return the device characteristic flags supported by an apnd-file.
11827*/
11828static int apndDeviceCharacteristics(sqlite3_file *pFile){
11829  pFile = ORIGFILE(pFile);
11830  return pFile->pMethods->xDeviceCharacteristics(pFile);
11831}
11832
11833/* Create a shared memory file mapping */
11834static int apndShmMap(
11835  sqlite3_file *pFile,
11836  int iPg,
11837  int pgsz,
11838  int bExtend,
11839  void volatile **pp
11840){
11841  pFile = ORIGFILE(pFile);
11842  return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
11843}
11844
11845/* Perform locking on a shared-memory segment */
11846static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
11847  pFile = ORIGFILE(pFile);
11848  return pFile->pMethods->xShmLock(pFile,offset,n,flags);
11849}
11850
11851/* Memory barrier operation on shared memory */
11852static void apndShmBarrier(sqlite3_file *pFile){
11853  pFile = ORIGFILE(pFile);
11854  pFile->pMethods->xShmBarrier(pFile);
11855}
11856
11857/* Unmap a shared memory segment */
11858static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
11859  pFile = ORIGFILE(pFile);
11860  return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
11861}
11862
11863/* Fetch a page of a memory-mapped file */
11864static int apndFetch(
11865  sqlite3_file *pFile,
11866  sqlite3_int64 iOfst,
11867  int iAmt,
11868  void **pp
11869){
11870  ApndFile *p = (ApndFile *)pFile;
11871  if( p->iMark < 0 || iOfst+iAmt > p->iMark ){
11872    return SQLITE_IOERR; /* Cannot read what is not yet there. */
11873  }
11874  pFile = ORIGFILE(pFile);
11875  return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
11876}
11877
11878/* Release a memory-mapped page */
11879static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
11880  ApndFile *p = (ApndFile *)pFile;
11881  pFile = ORIGFILE(pFile);
11882  return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
11883}
11884
11885/*
11886** Try to read the append-mark off the end of a file.  Return the
11887** start of the appended database if the append-mark is present.
11888** If there is no valid append-mark, return -1;
11889**
11890** An append-mark is only valid if the NNNNNNNN start-of-database offset
11891** indicates that the appended database contains at least one page.  The
11892** start-of-database value must be a multiple of 512.
11893*/
11894static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
11895  int rc, i;
11896  sqlite3_int64 iMark;
11897  int msbs = 8 * (APND_MARK_FOS_SZ-1);
11898  unsigned char a[APND_MARK_SIZE];
11899
11900  if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1;
11901  rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
11902  if( rc ) return -1;
11903  if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
11904  iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs;
11905  for(i=1; i<8; i++){
11906    msbs -= 8;
11907    iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs;
11908  }
11909  if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1;
11910  if( iMark & 0x1ff ) return -1;
11911  return iMark;
11912}
11913
11914static const char apvfsSqliteHdr[] = "SQLite format 3";
11915/*
11916** Check to see if the file is an appendvfs SQLite database file.
11917** Return true iff it is such. Parameter sz is the file's size.
11918*/
11919static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){
11920  int rc;
11921  char zHdr[16];
11922  sqlite3_int64 iMark = apndReadMark(sz, pFile);
11923  if( iMark>=0 ){
11924    /* If file has the correct end-marker, the expected odd size, and the
11925    ** SQLite DB type marker where the end-marker puts it, then it
11926    ** is an appendvfs database.
11927    */
11928    rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark);
11929    if( SQLITE_OK==rc
11930     && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0
11931     && (sz & 0x1ff) == APND_MARK_SIZE
11932     && sz>=512+APND_MARK_SIZE
11933    ){
11934      return 1; /* It's an appendvfs database */
11935    }
11936  }
11937  return 0;
11938}
11939
11940/*
11941** Check to see if the file is an ordinary SQLite database file.
11942** Return true iff so. Parameter sz is the file's size.
11943*/
11944static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
11945  char zHdr[16];
11946  if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */
11947   || (sz & 0x1ff) != 0
11948   || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0)
11949   || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0
11950  ){
11951    return 0;
11952  }else{
11953    return 1;
11954  }
11955}
11956
11957/*
11958** Open an apnd file handle.
11959*/
11960static int apndOpen(
11961  sqlite3_vfs *pApndVfs,
11962  const char *zName,
11963  sqlite3_file *pFile,
11964  int flags,
11965  int *pOutFlags
11966){
11967  ApndFile *pApndFile = (ApndFile*)pFile;
11968  sqlite3_file *pBaseFile = ORIGFILE(pFile);
11969  sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs);
11970  int rc;
11971  sqlite3_int64 sz = 0;
11972  if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
11973    /* The appendvfs is not to be used for transient or temporary databases.
11974    ** Just use the base VFS open to initialize the given file object and
11975    ** open the underlying file. (Appendvfs is then unused for this file.)
11976    */
11977    return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags);
11978  }
11979  memset(pApndFile, 0, sizeof(ApndFile));
11980  pFile->pMethods = &apnd_io_methods;
11981  pApndFile->iMark = -1;    /* Append mark not yet written */
11982
11983  rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags);
11984  if( rc==SQLITE_OK ){
11985    rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz);
11986    if( rc ){
11987      pBaseFile->pMethods->xClose(pBaseFile);
11988    }
11989  }
11990  if( rc ){
11991    pFile->pMethods = 0;
11992    return rc;
11993  }
11994  if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){
11995    /* The file being opened appears to be just an ordinary DB. Copy
11996    ** the base dispatch-table so this instance mimics the base VFS. 
11997    */
11998    memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile);
11999    return SQLITE_OK;
12000  }
12001  pApndFile->iPgOne = apndReadMark(sz, pFile);
12002  if( pApndFile->iPgOne>=0 ){
12003    pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */
12004    return SQLITE_OK;
12005  }
12006  if( (flags & SQLITE_OPEN_CREATE)==0 ){
12007    pBaseFile->pMethods->xClose(pBaseFile);
12008    rc = SQLITE_CANTOPEN;
12009    pFile->pMethods = 0;
12010  }else{
12011    /* Round newly added appendvfs location to #define'd page boundary. 
12012    ** Note that nothing has yet been written to the underlying file.
12013    ** The append mark will be written along with first content write.
12014    ** Until then, paf->iMark value indicates it is not yet written.
12015    */
12016    pApndFile->iPgOne = APND_START_ROUNDUP(sz);
12017  }
12018  return rc;
12019}
12020
12021/*
12022** Delete an apnd file.
12023** For an appendvfs, this could mean delete the appendvfs portion,
12024** leaving the appendee as it was before it gained an appendvfs.
12025** For now, this code deletes the underlying file too.
12026*/
12027static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
12028  return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
12029}
12030
12031/*
12032** All other VFS methods are pass-thrus.
12033*/
12034static int apndAccess(
12035  sqlite3_vfs *pVfs, 
12036  const char *zPath, 
12037  int flags, 
12038  int *pResOut
12039){
12040  return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
12041}
12042static int apndFullPathname(
12043  sqlite3_vfs *pVfs, 
12044  const char *zPath, 
12045  int nOut, 
12046  char *zOut
12047){
12048  return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
12049}
12050static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
12051  return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
12052}
12053static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
12054  ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
12055}
12056static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
12057  return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
12058}
12059static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
12060  ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
12061}
12062static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
12063  return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
12064}
12065static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
12066  return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
12067}
12068static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
12069  return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
12070}
12071static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
12072  return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
12073}
12074static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
12075  return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
12076}
12077static int apndSetSystemCall(
12078  sqlite3_vfs *pVfs,
12079  const char *zName,
12080  sqlite3_syscall_ptr pCall
12081){
12082  return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
12083}
12084static sqlite3_syscall_ptr apndGetSystemCall(
12085  sqlite3_vfs *pVfs,
12086  const char *zName
12087){
12088  return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
12089}
12090static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
12091  return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
12092}
12093
12094  
12095#ifdef _WIN32
12096
12097#endif
12098/* 
12099** This routine is called when the extension is loaded.
12100** Register the new VFS.
12101*/
12102int sqlite3_appendvfs_init(
12103  sqlite3 *db, 
12104  char **pzErrMsg, 
12105  const sqlite3_api_routines *pApi
12106){
12107  int rc = SQLITE_OK;
12108  sqlite3_vfs *pOrig;
12109  SQLITE_EXTENSION_INIT2(pApi);
12110  (void)pzErrMsg;
12111  (void)db;
12112  pOrig = sqlite3_vfs_find(0);
12113  if( pOrig==0 ) return SQLITE_ERROR;
12114  apnd_vfs.iVersion = pOrig->iVersion;
12115  apnd_vfs.pAppData = pOrig;
12116  apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
12117  rc = sqlite3_vfs_register(&apnd_vfs, 0);
12118#ifdef APPENDVFS_TEST
12119  if( rc==SQLITE_OK ){
12120    rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
12121  }
12122#endif
12123  if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
12124  return rc;
12125}
12126
12127/************************* End ext/misc/appendvfs.c ********************/
12128#endif
12129#ifdef SQLITE_HAVE_ZLIB
12130/************************* Begin ext/misc/zipfile.c ******************/
12131/*
12132** 2017-12-26
12133**
12134** The author disclaims copyright to this source code.  In place of
12135** a legal notice, here is a blessing:
12136**
12137**    May you do good and not evil.
12138**    May you find forgiveness for yourself and forgive others.
12139**    May you share freely, never taking more than you give.
12140**
12141******************************************************************************
12142**
12143** This file implements a virtual table for reading and writing ZIP archive
12144** files.
12145**
12146** Usage example:
12147**
12148**     SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
12149**
12150** Current limitations:
12151**
12152**    *  No support for encryption
12153**    *  No support for ZIP archives spanning multiple files
12154**    *  No support for zip64 extensions
12155**    *  Only the "inflate/deflate" (zlib) compression method is supported
12156*/
12157/* #include "sqlite3ext.h" */
12158SQLITE_EXTENSION_INIT1
12159#include <stdio.h>
12160#include <string.h>
12161#include <assert.h>
12162#ifndef SQLITE_NO_STDINT
12163#  include <stdint.h>
12164#endif
12165
12166#include <zlib.h>
12167
12168/* When used as part of the CLI, the sqlite3_stdio.h module will have
12169** been included before this one. In that case use the sqlite3_stdio.h
12170** #defines.  If not, create our own for fopen().
12171*/
12172#ifndef _SQLITE3_STDIO_H_
12173# define sqlite3_fopen fopen
12174#endif
12175
12176#ifndef SQLITE_OMIT_VIRTUALTABLE
12177
12178#ifndef SQLITE_AMALGAMATION
12179
12180#ifndef UINT32_TYPE
12181# ifdef HAVE_UINT32_T
12182#  define UINT32_TYPE uint32_t
12183# else
12184#  define UINT32_TYPE unsigned int
12185# endif
12186#endif
12187#ifndef UINT16_TYPE
12188# ifdef HAVE_UINT16_T
12189#  define UINT16_TYPE uint16_t
12190# else
12191#  define UINT16_TYPE unsigned short int
12192# endif
12193#endif
12194/* typedef sqlite3_int64 i64; */
12195/* typedef unsigned char u8; */
12196/* typedef UINT32_TYPE u32;           // 4-byte unsigned integer // */
12197/* typedef UINT16_TYPE u16;           // 2-byte unsigned integer // */
12198#define MIN(a,b) ((a)<(b) ? (a) : (b))
12199
12200#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
12201# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
12202#endif
12203#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
12204# define ALWAYS(X)      (1)
12205# define NEVER(X)       (0)
12206#elif !defined(NDEBUG)
12207# define ALWAYS(X)      ((X)?1:(assert(0),0))
12208# define NEVER(X)       ((X)?(assert(0),1):0)
12209#else
12210# define ALWAYS(X)      (X)
12211# define NEVER(X)       (X)
12212#endif
12213
12214#endif   /* SQLITE_AMALGAMATION */
12215
12216/*
12217** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
12218**
12219** In some ways it would be better to obtain these values from system 
12220** header files. But, the dependency is undesirable and (a) these
12221** have been stable for decades, (b) the values are part of POSIX and
12222** are also made explicit in [man stat], and (c) are part of the 
12223** file format for zip archives.
12224*/
12225#ifndef S_IFDIR
12226# define S_IFDIR 0040000
12227#endif
12228#ifndef S_IFREG
12229# define S_IFREG 0100000
12230#endif
12231#ifndef S_IFLNK
12232# define S_IFLNK 0120000
12233#endif
12234
12235static const char ZIPFILE_SCHEMA[] = 
12236  "CREATE TABLE y("
12237    "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
12238    "mode,"              /* 1: POSIX mode for file */
12239    "mtime,"             /* 2: Last modification time (secs since 1970)*/
12240    "sz,"                /* 3: Size of object */
12241    "rawdata,"           /* 4: Raw data */
12242    "data,"              /* 5: Uncompressed data */
12243    "method,"            /* 6: Compression method (integer) */
12244    "z HIDDEN"           /* 7: Name of zip file */
12245  ") WITHOUT ROWID;";
12246
12247#define ZIPFILE_F_COLUMN_IDX 7    /* Index of column "file" in the above */
12248#define ZIPFILE_MX_NAME (250)     /* Windows limitation on filename size */
12249
12250/*
12251** The buffer should be large enough to contain 3 65536 byte strings - the
12252** filename, the extra field and the file comment.
12253*/
12254#define ZIPFILE_BUFFER_SIZE (200*1024)
12255
12256
12257/*
12258** Magic numbers used to read and write zip files.
12259**
12260** ZIPFILE_NEWENTRY_MADEBY:
12261**   Use this value for the "version-made-by" field in new zip file
12262**   entries. The upper byte indicates "unix", and the lower byte 
12263**   indicates that the zip file matches pkzip specification 3.0. 
12264**   This is what info-zip seems to do.
12265**
12266** ZIPFILE_NEWENTRY_REQUIRED:
12267**   Value for "version-required-to-extract" field of new entries.
12268**   Version 2.0 is required to support folders and deflate compression.
12269**
12270** ZIPFILE_NEWENTRY_FLAGS:
12271**   Value for "general-purpose-bit-flags" field of new entries. Bit
12272**   11 means "utf-8 filename and comment".
12273**
12274** ZIPFILE_SIGNATURE_CDS:
12275**   First 4 bytes of a valid CDS record.
12276**
12277** ZIPFILE_SIGNATURE_LFH:
12278**   First 4 bytes of a valid LFH record.
12279**
12280** ZIPFILE_SIGNATURE_EOCD
12281**   First 4 bytes of a valid EOCD record.
12282*/
12283#define ZIPFILE_EXTRA_TIMESTAMP   0x5455
12284#define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
12285#define ZIPFILE_NEWENTRY_REQUIRED 20
12286#define ZIPFILE_NEWENTRY_FLAGS    0x800
12287#define ZIPFILE_SIGNATURE_CDS     0x02014b50
12288#define ZIPFILE_SIGNATURE_LFH     0x04034b50
12289#define ZIPFILE_SIGNATURE_EOCD    0x06054b50
12290
12291/*
12292** The sizes of the fixed-size part of each of the three main data 
12293** structures in a zip archive.
12294*/
12295#define ZIPFILE_LFH_FIXED_SZ      30
12296#define ZIPFILE_EOCD_FIXED_SZ     22
12297#define ZIPFILE_CDS_FIXED_SZ      46
12298
12299/*
12300*** 4.3.16  End of central directory record:
12301***
12302***   end of central dir signature    4 bytes  (0x06054b50)
12303***   number of this disk             2 bytes
12304***   number of the disk with the
12305***   start of the central directory  2 bytes
12306***   total number of entries in the
12307***   central directory on this disk  2 bytes
12308***   total number of entries in
12309***   the central directory           2 bytes
12310***   size of the central directory   4 bytes
12311***   offset of start of central
12312***   directory with respect to
12313***   the starting disk number        4 bytes
12314***   .ZIP file comment length        2 bytes
12315***   .ZIP file comment       (variable size)
12316*/
12317typedef struct ZipfileEOCD ZipfileEOCD;
12318struct ZipfileEOCD {
12319  u16 iDisk;
12320  u16 iFirstDisk;
12321  u16 nEntry;
12322  u16 nEntryTotal;
12323  u32 nSize;
12324  u32 iOffset;
12325};
12326
12327/*
12328*** 4.3.12  Central directory structure:
12329***
12330*** ...
12331***
12332***   central file header signature   4 bytes  (0x02014b50)
12333***   version made by                 2 bytes
12334***   version needed to extract       2 bytes
12335***   general purpose bit flag        2 bytes
12336***   compression method              2 bytes
12337***   last mod file time              2 bytes
12338***   last mod file date              2 bytes
12339***   crc-32                          4 bytes
12340***   compressed size                 4 bytes
12341***   uncompressed size               4 bytes
12342***   file name length                2 bytes
12343***   extra field length              2 bytes
12344***   file comment length             2 bytes
12345***   disk number start               2 bytes
12346***   internal file attributes        2 bytes
12347***   external file attributes        4 bytes
12348***   relative offset of local header 4 bytes
12349*/
12350typedef struct ZipfileCDS ZipfileCDS;
12351struct ZipfileCDS {
12352  u16 iVersionMadeBy;
12353  u16 iVersionExtract;
12354  u16 flags;
12355  u16 iCompression;
12356  u16 mTime;
12357  u16 mDate;
12358  u32 crc32;
12359  u32 szCompressed;
12360  u32 szUncompressed;
12361  u16 nFile;
12362  u16 nExtra;
12363  u16 nComment;
12364  u16 iDiskStart;
12365  u16 iInternalAttr;
12366  u32 iExternalAttr;
12367  u32 iOffset;
12368  char *zFile;                    /* Filename (sqlite3_malloc()) */
12369};
12370
12371/*
12372*** 4.3.7  Local file header:
12373***
12374***   local file header signature     4 bytes  (0x04034b50)
12375***   version needed to extract       2 bytes
12376***   general purpose bit flag        2 bytes
12377***   compression method              2 bytes
12378***   last mod file time              2 bytes
12379***   last mod file date              2 bytes
12380***   crc-32                          4 bytes
12381***   compressed size                 4 bytes
12382***   uncompressed size               4 bytes
12383***   file name length                2 bytes
12384***   extra field length              2 bytes
12385***   
12386*/
12387typedef struct ZipfileLFH ZipfileLFH;
12388struct ZipfileLFH {
12389  u16 iVersionExtract;
12390  u16 flags;
12391  u16 iCompression;
12392  u16 mTime;
12393  u16 mDate;
12394  u32 crc32;
12395  u32 szCompressed;
12396  u32 szUncompressed;
12397  u16 nFile;
12398  u16 nExtra;
12399};
12400
12401typedef struct ZipfileEntry ZipfileEntry;
12402struct ZipfileEntry {
12403  ZipfileCDS cds;            /* Parsed CDS record */
12404  u32 mUnixTime;             /* Modification time, in UNIX format */
12405  u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
12406  i64 iDataOff;              /* Offset to data in file (if aData==0) */
12407  u8 *aData;                 /* cds.szCompressed bytes of compressed data */
12408  ZipfileEntry *pNext;       /* Next element in in-memory CDS */
12409};
12410
12411/* 
12412** Cursor type for zipfile tables.
12413*/
12414typedef struct ZipfileCsr ZipfileCsr;
12415struct ZipfileCsr {
12416  sqlite3_vtab_cursor base;  /* Base class - must be first */
12417  i64 iId;                   /* Cursor ID */
12418  u8 bEof;                   /* True when at EOF */
12419  u8 bNoop;                  /* If next xNext() call is no-op */
12420
12421  /* Used outside of write transactions */
12422  FILE *pFile;               /* Zip file */
12423  i64 iNextOff;              /* Offset of next record in central directory */
12424  ZipfileEOCD eocd;          /* Parse of central directory record */
12425
12426  ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
12427  ZipfileEntry *pCurrent;    /* Current entry */
12428  ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
12429};
12430
12431typedef struct ZipfileTab ZipfileTab;
12432struct ZipfileTab {
12433  sqlite3_vtab base;         /* Base class - must be first */
12434  char *zFile;               /* Zip file this table accesses (may be NULL) */
12435  sqlite3 *db;               /* Host database connection */
12436  u8 *aBuffer;               /* Temporary buffer used for various tasks */
12437
12438  ZipfileCsr *pCsrList;      /* List of cursors */
12439  i64 iNextCsrid;
12440
12441  /* The following are used by write transactions only */
12442  ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
12443  ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
12444  FILE *pWriteFd;            /* File handle open on zip archive */
12445  i64 szCurrent;             /* Current size of zip archive */
12446  i64 szOrig;                /* Size of archive at start of transaction */
12447};
12448
12449/*
12450** Set the error message contained in context ctx to the results of
12451** vprintf(zFmt, ...).
12452*/
12453static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
12454  char *zMsg = 0;
12455  va_list ap;
12456  va_start(ap, zFmt);
12457  zMsg = sqlite3_vmprintf(zFmt, ap);
12458  sqlite3_result_error(ctx, zMsg, -1);
12459  sqlite3_free(zMsg);
12460  va_end(ap);
12461}
12462
12463/*
12464** If string zIn is quoted, dequote it in place. Otherwise, if the string
12465** is not quoted, do nothing.
12466*/
12467static void zipfileDequote(char *zIn){
12468  char q = zIn[0];
12469  if( q=='"' || q=='\'' || q=='`' || q=='[' ){
12470    int iIn = 1;
12471    int iOut = 0;
12472    if( q=='[' ) q = ']';
12473    while( ALWAYS(zIn[iIn]) ){
12474      char c = zIn[iIn++];
12475      if( c==q && zIn[iIn++]!=q ) break;
12476      zIn[iOut++] = c;
12477    }
12478    zIn[iOut] = '\0';
12479  }
12480}
12481
12482/*
12483** Construct a new ZipfileTab virtual table object.
12484** 
12485**   argv[0]   -> module name  ("zipfile")
12486**   argv[1]   -> database name
12487**   argv[2]   -> table name
12488**   argv[...] -> "column name" and other module argument fields.
12489*/
12490static int zipfileConnect(
12491  sqlite3 *db,
12492  void *pAux,
12493  int argc, const char *const*argv,
12494  sqlite3_vtab **ppVtab,
12495  char **pzErr
12496){
12497  int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
12498  int nFile = 0;
12499  const char *zFile = 0;
12500  ZipfileTab *pNew = 0;
12501  int rc;
12502  (void)pAux;
12503
12504  /* If the table name is not "zipfile", require that the argument be
12505  ** specified. This stops zipfile tables from being created as:
12506  **
12507  **   CREATE VIRTUAL TABLE zzz USING zipfile();
12508  **
12509  ** It does not prevent:
12510  **
12511  **   CREATE VIRTUAL TABLE zipfile USING zipfile();
12512  */
12513  assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
12514  if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
12515    *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
12516    return SQLITE_ERROR;
12517  }
12518
12519  if( argc>3 ){
12520    zFile = argv[3];
12521    nFile = (int)strlen(zFile)+1;
12522  }
12523
12524  rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
12525  if( rc==SQLITE_OK ){
12526    pNew = (ZipfileTab*)sqlite3_malloc64((i64)nByte+nFile);
12527    if( pNew==0 ) return SQLITE_NOMEM;
12528    memset(pNew, 0, nByte+nFile);
12529    pNew->db = db;
12530    pNew->aBuffer = (u8*)&pNew[1];
12531    if( zFile ){
12532      pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
12533      memcpy(pNew->zFile, zFile, nFile);
12534      zipfileDequote(pNew->zFile);
12535    }
12536  }
12537  sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
12538  *ppVtab = (sqlite3_vtab*)pNew;
12539  return rc;
12540}
12541
12542/*
12543** Free the ZipfileEntry structure indicated by the only argument.
12544*/
12545static void zipfileEntryFree(ZipfileEntry *p){
12546  if( p ){
12547    sqlite3_free(p->cds.zFile);
12548    sqlite3_free(p);
12549  }
12550}
12551
12552/*
12553** Release resources that should be freed at the end of a write 
12554** transaction.
12555*/
12556static void zipfileCleanupTransaction(ZipfileTab *pTab){
12557  ZipfileEntry *pEntry;
12558  ZipfileEntry *pNext;
12559
12560  if( pTab->pWriteFd ){
12561    fclose(pTab->pWriteFd);
12562    pTab->pWriteFd = 0;
12563  }
12564  for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
12565    pNext = pEntry->pNext;
12566    zipfileEntryFree(pEntry);
12567  }
12568  pTab->pFirstEntry = 0;
12569  pTab->pLastEntry = 0;
12570  pTab->szCurrent = 0;
12571  pTab->szOrig = 0;
12572}
12573
12574/*
12575** This method is the destructor for zipfile vtab objects.
12576*/
12577static int zipfileDisconnect(sqlite3_vtab *pVtab){
12578  zipfileCleanupTransaction((ZipfileTab*)pVtab);
12579  sqlite3_free(pVtab);
12580  return SQLITE_OK;
12581}
12582
12583/*
12584** Constructor for a new ZipfileCsr object.
12585*/
12586static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
12587  ZipfileTab *pTab = (ZipfileTab*)p;
12588  ZipfileCsr *pCsr;
12589  pCsr = sqlite3_malloc64(sizeof(*pCsr));
12590  *ppCsr = (sqlite3_vtab_cursor*)pCsr;
12591  if( pCsr==0 ){
12592    return SQLITE_NOMEM;
12593  }
12594  memset(pCsr, 0, sizeof(*pCsr));
12595  pCsr->iId = ++pTab->iNextCsrid;
12596  pCsr->pCsrNext = pTab->pCsrList;
12597  pTab->pCsrList = pCsr;
12598  return SQLITE_OK;
12599}
12600
12601/*
12602** Reset a cursor back to the state it was in when first returned
12603** by zipfileOpen().
12604*/
12605static void zipfileResetCursor(ZipfileCsr *pCsr){
12606  ZipfileEntry *p;
12607  ZipfileEntry *pNext;
12608
12609  pCsr->bEof = 0;
12610  if( pCsr->pFile ){
12611    fclose(pCsr->pFile);
12612    pCsr->pFile = 0;
12613    zipfileEntryFree(pCsr->pCurrent);
12614    pCsr->pCurrent = 0;
12615  }
12616
12617  for(p=pCsr->pFreeEntry; p; p=pNext){
12618    pNext = p->pNext;
12619    zipfileEntryFree(p);
12620  }
12621}
12622
12623/*
12624** Destructor for an ZipfileCsr.
12625*/
12626static int zipfileClose(sqlite3_vtab_cursor *cur){
12627  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
12628  ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
12629  ZipfileCsr **pp;
12630  zipfileResetCursor(pCsr);
12631
12632  /* Remove this cursor from the ZipfileTab.pCsrList list. */
12633  for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
12634  *pp = pCsr->pCsrNext;
12635
12636  sqlite3_free(pCsr);
12637  return SQLITE_OK;
12638}
12639
12640/*
12641** Set the error message for the virtual table associated with cursor
12642** pCsr to the results of vprintf(zFmt, ...).
12643*/
12644static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
12645  va_list ap;
12646  va_start(ap, zFmt);
12647  sqlite3_free(pTab->base.zErrMsg);
12648  pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
12649  va_end(ap);
12650}
12651static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
12652  va_list ap;
12653  va_start(ap, zFmt);
12654  sqlite3_free(pCsr->base.pVtab->zErrMsg);
12655  pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
12656  va_end(ap);
12657}
12658
12659/*
12660** Read nRead bytes of data from offset iOff of file pFile into buffer
12661** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
12662** otherwise. 
12663**
12664** If an error does occur, output variable (*pzErrmsg) may be set to point
12665** to an English language error message. It is the responsibility of the
12666** caller to eventually free this buffer using
12667** sqlite3_free().
12668*/
12669static int zipfileReadData(
12670  FILE *pFile,                    /* Read from this file */
12671  u8 *aRead,                      /* Read into this buffer */
12672  i64 nRead,                      /* Number of bytes to read */
12673  i64 iOff,                       /* Offset to read from */
12674  char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
12675){
12676  size_t n;
12677  fseek(pFile, (long)iOff, SEEK_SET);
12678  n = fread(aRead, 1, (long)nRead, pFile);
12679  if( n!=(size_t)nRead ){
12680    sqlite3_free(*pzErrmsg);
12681    *pzErrmsg = sqlite3_mprintf("error in fread()");
12682    return SQLITE_ERROR;
12683  }
12684  return SQLITE_OK;
12685}
12686
12687static int zipfileAppendData(
12688  ZipfileTab *pTab,
12689  const u8 *aWrite,
12690  int nWrite
12691){
12692  if( nWrite>0 ){
12693    size_t n = nWrite;
12694    fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
12695    n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
12696    if( (int)n!=nWrite ){
12697      zipfileTableErr(pTab,"error in fwrite()");
12698      return SQLITE_ERROR;
12699    }
12700    pTab->szCurrent += nWrite;
12701  }
12702  return SQLITE_OK;
12703}
12704
12705/*
12706** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
12707*/
12708static u16 zipfileGetU16(const u8 *aBuf){
12709  return (aBuf[1] << 8) + aBuf[0];
12710}
12711
12712/*
12713** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
12714*/
12715static u32 zipfileGetU32(const u8 *aBuf){
12716  if( aBuf==0 ) return 0;
12717  return ((u32)(aBuf[3]) << 24)
12718       + ((u32)(aBuf[2]) << 16)
12719       + ((u32)(aBuf[1]) <<  8)
12720       + ((u32)(aBuf[0]) <<  0);
12721}
12722
12723/*
12724** Write a 16-bit little endiate integer into buffer aBuf.
12725*/
12726static void zipfilePutU16(u8 *aBuf, u16 val){
12727  aBuf[0] = val & 0xFF;
12728  aBuf[1] = (val>>8) & 0xFF;
12729}
12730
12731/*
12732** Write a 32-bit little endiate integer into buffer aBuf.
12733*/
12734static void zipfilePutU32(u8 *aBuf, u32 val){
12735  aBuf[0] = val & 0xFF;
12736  aBuf[1] = (val>>8) & 0xFF;
12737  aBuf[2] = (val>>16) & 0xFF;
12738  aBuf[3] = (val>>24) & 0xFF;
12739}
12740
12741#define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
12742#define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
12743
12744#define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
12745#define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
12746
12747/*
12748** Magic numbers used to read CDS records.
12749*/
12750#define ZIPFILE_CDS_NFILE_OFF        28
12751#define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
12752
12753/*
12754** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
12755** if the record is not well-formed, or SQLITE_OK otherwise.
12756*/
12757static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
12758  u8 *aRead = aBuf;
12759  u32 sig = zipfileRead32(aRead);
12760  int rc = SQLITE_OK;
12761  if( sig!=ZIPFILE_SIGNATURE_CDS ){
12762    rc = SQLITE_ERROR;
12763  }else{
12764    pCDS->iVersionMadeBy = zipfileRead16(aRead);
12765    pCDS->iVersionExtract = zipfileRead16(aRead);
12766    pCDS->flags = zipfileRead16(aRead);
12767    pCDS->iCompression = zipfileRead16(aRead);
12768    pCDS->mTime = zipfileRead16(aRead);
12769    pCDS->mDate = zipfileRead16(aRead);
12770    pCDS->crc32 = zipfileRead32(aRead);
12771    pCDS->szCompressed = zipfileRead32(aRead);
12772    pCDS->szUncompressed = zipfileRead32(aRead);
12773    assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
12774    pCDS->nFile = zipfileRead16(aRead);
12775    pCDS->nExtra = zipfileRead16(aRead);
12776    pCDS->nComment = zipfileRead16(aRead);
12777    pCDS->iDiskStart = zipfileRead16(aRead);
12778    pCDS->iInternalAttr = zipfileRead16(aRead);
12779    pCDS->iExternalAttr = zipfileRead32(aRead);
12780    pCDS->iOffset = zipfileRead32(aRead);
12781    assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
12782  }
12783
12784  return rc;
12785}
12786
12787/*
12788** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
12789** if the record is not well-formed, or SQLITE_OK otherwise.
12790*/
12791static int zipfileReadLFH(
12792  u8 *aBuffer,
12793  ZipfileLFH *pLFH
12794){
12795  u8 *aRead = aBuffer;
12796  int rc = SQLITE_OK;
12797
12798  u32 sig = zipfileRead32(aRead);
12799  if( sig!=ZIPFILE_SIGNATURE_LFH ){
12800    rc = SQLITE_ERROR;
12801  }else{
12802    pLFH->iVersionExtract = zipfileRead16(aRead);
12803    pLFH->flags = zipfileRead16(aRead);
12804    pLFH->iCompression = zipfileRead16(aRead);
12805    pLFH->mTime = zipfileRead16(aRead);
12806    pLFH->mDate = zipfileRead16(aRead);
12807    pLFH->crc32 = zipfileRead32(aRead);
12808    pLFH->szCompressed = zipfileRead32(aRead);
12809    pLFH->szUncompressed = zipfileRead32(aRead);
12810    pLFH->nFile = zipfileRead16(aRead);
12811    pLFH->nExtra = zipfileRead16(aRead);
12812    if( pLFH->nFile>ZIPFILE_MX_NAME ) rc = SQLITE_ERROR;
12813  }
12814  return rc;
12815}
12816
12817
12818/*
12819** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
12820** Scan through this buffer to find an "extra-timestamp" field. If one
12821** exists, extract the 32-bit modification-timestamp from it and store
12822** the value in output parameter *pmTime.
12823**
12824** Zero is returned if no extra-timestamp record could be found (and so
12825** *pmTime is left unchanged), or non-zero otherwise.
12826**
12827** The general format of an extra field is:
12828**
12829**   Header ID    2 bytes
12830**   Data Size    2 bytes
12831**   Data         N bytes
12832*/
12833static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
12834  int ret = 0;
12835  u8 *p = aExtra;
12836  u8 *pEnd = &aExtra[nExtra];
12837
12838  /* Stop when there are less than 9 bytes left to scan in the buffer. This
12839  ** is because the timestamp field requires exactly 9 bytes - 4 bytes of
12840  ** header fields and 5 bytes of data. If there are less than 9 bytes 
12841  ** remaining, either it is some other field or else the extra data
12842  ** is corrupt. Either way, do not process it.  */
12843  while( p+(2*sizeof(u16) + 1 + sizeof(u32))<=pEnd ){
12844    u16 id = zipfileRead16(p);
12845    u16 nByte = zipfileRead16(p);
12846
12847    switch( id ){
12848      case ZIPFILE_EXTRA_TIMESTAMP: {
12849        u8 b = p[0];
12850        if( b & 0x01 ){     /* 0x01 -> modtime is present */
12851          *pmTime = zipfileGetU32(&p[1]);
12852          ret = 1;
12853        }
12854        break;
12855      }
12856    }
12857
12858    p += nByte;
12859  }
12860  return ret;
12861}
12862
12863/*
12864** Convert the standard MS-DOS timestamp stored in the mTime and mDate
12865** fields of the CDS structure passed as the only argument to a 32-bit
12866** UNIX seconds-since-the-epoch timestamp. Return the result.
12867**
12868** "Standard" MS-DOS time format:
12869**
12870**   File modification time:
12871**     Bits 00-04: seconds divided by 2
12872**     Bits 05-10: minute
12873**     Bits 11-15: hour
12874**   File modification date:
12875**     Bits 00-04: day
12876**     Bits 05-08: month (1-12)
12877**     Bits 09-15: years from 1980 
12878**
12879** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
12880*/
12881static u32 zipfileMtime(ZipfileCDS *pCDS){
12882  int Y,M,D,X1,X2,A,B,sec,min,hr;
12883  i64 JDsec;
12884  Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
12885  M = ((pCDS->mDate >> 5) & 0x0F);
12886  D = (pCDS->mDate & 0x1F);
12887  sec = (pCDS->mTime & 0x1F)*2;
12888  min = (pCDS->mTime >> 5) & 0x3F;
12889  hr = (pCDS->mTime >> 11) & 0x1F;
12890  if( M<=2 ){
12891    Y--;
12892    M += 12;
12893  }
12894  X1 = 36525*(Y+4716)/100;
12895  X2 = 306001*(M+1)/10000;
12896  A = Y/100;
12897  B = 2 - A + (A/4);
12898  JDsec = (i64)((X1 + X2 + D + B - 1524.5)*86400) + hr*3600 + min*60 + sec;
12899  return (u32)(JDsec - (i64)24405875*(i64)8640);
12900}
12901
12902/*
12903** The opposite of zipfileMtime(). This function populates the mTime and
12904** mDate fields of the CDS structure passed as the first argument according
12905** to the UNIX timestamp value passed as the second.
12906*/
12907static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
12908  /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
12909  i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
12910
12911  int A, B, C, D, E;
12912  int yr, mon, day;
12913  int hr, min, sec;
12914
12915  A = (int)((JD - 1867216.25)/36524.25);
12916  A = (int)(JD + 1 + A - (A/4));
12917  B = A + 1524;
12918  C = (int)((B - 122.1)/365.25);
12919  D = (36525*(C&32767))/100;
12920  E = (int)((B-D)/30.6001);
12921
12922  day = B - D - (int)(30.6001*E);
12923  mon = (E<14 ? E-1 : E-13);
12924  yr = mon>2 ? C-4716 : C-4715;
12925
12926  hr = (mUnixTime % (24*60*60)) / (60*60);
12927  min = (mUnixTime % (60*60)) / 60;
12928  sec = (mUnixTime % 60);
12929
12930  if( yr>=1980 ){
12931    pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
12932    pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
12933  }else{
12934    pCds->mDate = pCds->mTime = 0;
12935  }
12936
12937  assert( mUnixTime<315507600 
12938       || mUnixTime==zipfileMtime(pCds) 
12939       || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds)) 
12940       /* || (mUnixTime % 2) */
12941  );
12942}
12943
12944/*
12945** Set (*pzErr) to point to a buffer from sqlite3_malloc() containing a 
12946** generic corruption message and return SQLITE_CORRUPT;
12947*/
12948static int zipfileCorrupt(char **pzErr){
12949  sqlite3_free(*pzErr);
12950  *pzErr = sqlite3_mprintf("zip archive is corrupt");
12951  return SQLITE_CORRUPT;
12952}
12953
12954/*
12955** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
12956** size) containing an entire zip archive image. Or, if aBlob is NULL,
12957** then pFile is a file-handle open on a zip file. In either case, this
12958** function creates a ZipfileEntry object based on the zip archive entry
12959** for which the CDS record is at offset iOff.
12960**
12961** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
12962** the new object. Otherwise, an SQLite error code is returned and the
12963** final value of (*ppEntry) undefined.
12964*/
12965static int zipfileGetEntry(
12966  ZipfileTab *pTab,               /* Store any error message here */
12967  const u8 *aBlob,                /* Pointer to in-memory file image */
12968  i64 nBlob,                      /* Size of aBlob[] in bytes */
12969  FILE *pFile,                    /* If aBlob==0, read from this file */
12970  i64 iOff,                       /* Offset of CDS record */
12971  ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
12972){
12973  u8 *aRead;
12974  char **pzErr = &pTab->base.zErrMsg;
12975  int rc = SQLITE_OK;
12976
12977  if( aBlob==0 ){
12978    aRead = pTab->aBuffer;
12979    rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
12980  }else{
12981    if( (iOff+ZIPFILE_CDS_FIXED_SZ)>nBlob ){
12982      /* Not enough data for the CDS structure. Corruption. */
12983      return zipfileCorrupt(pzErr);
12984    }
12985    aRead = (u8*)&aBlob[iOff];
12986  }
12987
12988  if( rc==SQLITE_OK ){
12989    sqlite3_int64 nAlloc;
12990    ZipfileEntry *pNew;
12991
12992    int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
12993    int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
12994    nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
12995
12996    nAlloc = sizeof(ZipfileEntry) + nExtra;
12997    if( aBlob ){
12998      nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
12999    }
13000
13001    pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
13002    if( pNew==0 ){
13003      rc = SQLITE_NOMEM;
13004    }else{
13005      memset(pNew, 0, sizeof(ZipfileEntry));
13006      rc = zipfileReadCDS(aRead, &pNew->cds);
13007      if( rc!=SQLITE_OK ){
13008        zipfileTableErr(pTab, "failed to read CDS at offset %lld", iOff);
13009      }else if( aBlob==0 ){
13010        rc = zipfileReadData(
13011            pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
13012        );
13013      }else{
13014        aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
13015        if( (iOff + ZIPFILE_CDS_FIXED_SZ + nFile + nExtra)>nBlob ){
13016          rc = zipfileCorrupt(pzErr);
13017        }
13018      }
13019    }
13020
13021    if( rc==SQLITE_OK ){
13022      u32 *pt = &pNew->mUnixTime;
13023      pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead); 
13024      pNew->aExtra = (u8*)&pNew[1];
13025      memcpy(pNew->aExtra, &aRead[nFile], nExtra);
13026      if( pNew->cds.zFile==0 ){
13027        rc = SQLITE_NOMEM;
13028      }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
13029        pNew->mUnixTime = zipfileMtime(&pNew->cds);
13030      }
13031    }
13032
13033    if( rc==SQLITE_OK ){
13034      static const int szFix = ZIPFILE_LFH_FIXED_SZ;
13035      ZipfileLFH lfh;
13036      if( pFile ){
13037        rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
13038      }else{
13039        aRead = (u8*)&aBlob[pNew->cds.iOffset];
13040        if( ((i64)pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ)>nBlob ){
13041          rc = zipfileCorrupt(pzErr);
13042        }
13043      }
13044
13045      memset(&lfh, 0, sizeof(lfh));
13046      if( rc==SQLITE_OK ) rc = zipfileReadLFH(aRead, &lfh);
13047      if( rc==SQLITE_OK ){
13048        pNew->iDataOff =  (i64)pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
13049        pNew->iDataOff += lfh.nFile + lfh.nExtra;
13050        if( aBlob && pNew->cds.szCompressed ){
13051          if( pNew->iDataOff + pNew->cds.szCompressed > nBlob ){
13052            rc = zipfileCorrupt(pzErr);
13053          }else{
13054            pNew->aData = &pNew->aExtra[nExtra];
13055            memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
13056          }
13057        }
13058      }else{
13059        zipfileTableErr(pTab, "failed to read LFH at offset %d", 
13060            (int)pNew->cds.iOffset
13061        );
13062      }
13063    }
13064
13065    if( rc!=SQLITE_OK ){
13066      zipfileEntryFree(pNew);
13067    }else{
13068      *ppEntry = pNew;
13069    }
13070  }
13071
13072  return rc;
13073}
13074
13075/*
13076** Advance an ZipfileCsr to its next row of output.
13077*/
13078static int zipfileNext(sqlite3_vtab_cursor *cur){
13079  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
13080  int rc = SQLITE_OK;
13081
13082  if( pCsr->pFile ){
13083    i64 iEof = (i64)pCsr->eocd.iOffset + (i64)pCsr->eocd.nSize;
13084    zipfileEntryFree(pCsr->pCurrent);
13085    pCsr->pCurrent = 0;
13086    if( pCsr->iNextOff>=iEof ){
13087      pCsr->bEof = 1;
13088    }else{
13089      ZipfileEntry *p = 0;
13090      ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
13091      rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
13092      if( rc==SQLITE_OK ){
13093        pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
13094        pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
13095      }
13096      pCsr->pCurrent = p;
13097    }
13098  }else{
13099    if( !pCsr->bNoop ){
13100      pCsr->pCurrent = pCsr->pCurrent->pNext;
13101    }
13102    if( pCsr->pCurrent==0 ){
13103      pCsr->bEof = 1;
13104    }
13105  }
13106
13107  pCsr->bNoop = 0;
13108  return rc;
13109}
13110
13111static void zipfileFree(void *p) { 
13112  sqlite3_free(p); 
13113}
13114
13115/*
13116** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
13117** size is nOut bytes. This function uncompresses the data and sets the
13118** return value in context pCtx to the result (a blob).
13119**
13120** If an error occurs, an error code is left in pCtx instead.
13121*/
13122static void zipfileInflate(
13123  sqlite3_context *pCtx,          /* Store result here */
13124  const u8 *aIn,                  /* Compressed data */
13125  int nIn,                        /* Size of buffer aIn[] in bytes */
13126  int nOut                        /* Expected output size */
13127){
13128  u8 *aRes = sqlite3_malloc64(nOut);
13129  if( aRes==0 ){
13130    sqlite3_result_error_nomem(pCtx);
13131  }else{
13132    int err;
13133    z_stream str;
13134    memset(&str, 0, sizeof(str));
13135
13136    str.next_in = (Byte*)aIn;
13137    str.avail_in = nIn;
13138    str.next_out = (Byte*)aRes;
13139    str.avail_out = nOut;
13140
13141    err = inflateInit2(&str, -15);
13142    if( err!=Z_OK ){
13143      zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
13144    }else{
13145      err = inflate(&str, Z_NO_FLUSH);
13146      if( err!=Z_STREAM_END ){
13147        zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
13148      }else{
13149        sqlite3_result_blob(pCtx, aRes, (int)str.total_out, zipfileFree);
13150        aRes = 0;
13151      }
13152    }
13153    sqlite3_free(aRes);
13154    inflateEnd(&str);
13155  }
13156}
13157
13158/*
13159** Buffer aIn (size nIn bytes) contains uncompressed data. This function
13160** compresses it and sets (*ppOut) to point to a buffer containing the
13161** compressed data. The caller is responsible for eventually calling
13162** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut) 
13163** is set to the size of buffer (*ppOut) in bytes.
13164**
13165** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
13166** code is returned and an error message left in virtual-table handle
13167** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
13168** case.
13169*/
13170static int zipfileDeflate(
13171  const u8 *aIn, int nIn,         /* Input */
13172  u8 **ppOut, int *pnOut,         /* Output */
13173  char **pzErr                    /* OUT: Error message */
13174){
13175  int rc = SQLITE_OK;
13176  sqlite3_int64 nAlloc;
13177  z_stream str;
13178  u8 *aOut;
13179
13180  memset(&str, 0, sizeof(str));
13181  str.next_in = (Bytef*)aIn;
13182  str.avail_in = nIn;
13183  deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
13184
13185  nAlloc = deflateBound(&str, nIn);
13186  aOut = (u8*)sqlite3_malloc64(nAlloc);
13187  if( aOut==0 ){
13188    rc = SQLITE_NOMEM;
13189  }else{
13190    int res;
13191    str.next_out = aOut;
13192    str.avail_out = nAlloc;
13193    res = deflate(&str, Z_FINISH);
13194    if( res==Z_STREAM_END ){
13195      *ppOut = aOut;
13196      *pnOut = (int)str.total_out;
13197    }else{
13198      sqlite3_free(aOut);
13199      *pzErr = sqlite3_mprintf("zipfile: deflate() error");
13200      rc = SQLITE_ERROR;
13201    }
13202    deflateEnd(&str);
13203  }
13204
13205  return rc;
13206}
13207
13208
13209/*
13210** Return values of columns for the row at which the series_cursor
13211** is currently pointing.
13212*/
13213static int zipfileColumn(
13214  sqlite3_vtab_cursor *cur,   /* The cursor */
13215  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
13216  int i                       /* Which column to return */
13217){
13218  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
13219  ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
13220  int rc = SQLITE_OK;
13221  switch( i ){
13222    case 0:   /* name */
13223      sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
13224      break;
13225    case 1:   /* mode */
13226      /* TODO: Whether or not the following is correct surely depends on
13227      ** the platform on which the archive was created.  */
13228      sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
13229      break;
13230    case 2: { /* mtime */
13231      sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
13232      break;
13233    }
13234    case 3: { /* sz */
13235      if( sqlite3_vtab_nochange(ctx)==0 ){
13236        sqlite3_result_int64(ctx, pCDS->szUncompressed);
13237      }
13238      break;
13239    }
13240    case 4:   /* rawdata */
13241      if( sqlite3_vtab_nochange(ctx) ) break;
13242    case 5: { /* data */
13243      if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
13244        int sz = pCDS->szCompressed;
13245        int szFinal = pCDS->szUncompressed;
13246        if( szFinal>0 ){
13247          u8 *aBuf;
13248          u8 *aFree = 0;
13249          if( pCsr->pCurrent->aData ){
13250            aBuf = pCsr->pCurrent->aData;
13251          }else{
13252            aBuf = aFree = sqlite3_malloc64(sz);
13253            if( aBuf==0 ){
13254              rc = SQLITE_NOMEM;
13255            }else{
13256              FILE *pFile = pCsr->pFile;
13257              if( pFile==0 ){
13258                pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
13259              }
13260              rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
13261                  &pCsr->base.pVtab->zErrMsg
13262              );
13263            }
13264          }
13265          if( rc==SQLITE_OK ){
13266            if( i==5 && pCDS->iCompression ){
13267              zipfileInflate(ctx, aBuf, sz, szFinal);
13268            }else{
13269              sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
13270            }
13271          }
13272          sqlite3_free(aFree);
13273        }else{
13274          /* Figure out if this is a directory or a zero-sized file. Consider
13275          ** it to be a directory either if the mode suggests so, or if
13276          ** the final character in the name is '/'.  */
13277          u32 mode = pCDS->iExternalAttr >> 16;
13278          if( !(mode & S_IFDIR)
13279           && pCDS->nFile>=1
13280           && pCDS->zFile[pCDS->nFile-1]!='/'
13281          ){
13282            sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
13283          }
13284        }
13285      }
13286      break;
13287    }
13288    case 6:   /* method */
13289      sqlite3_result_int(ctx, pCDS->iCompression);
13290      break;
13291    default:  /* z */
13292      assert( i==7 );
13293      sqlite3_result_int64(ctx, pCsr->iId);
13294      break;
13295  }
13296
13297  return rc;
13298}
13299
13300/*
13301** Return TRUE if the cursor is at EOF.
13302*/
13303static int zipfileEof(sqlite3_vtab_cursor *cur){
13304  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
13305  return pCsr->bEof;
13306}
13307
13308/*
13309** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
13310** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
13311** is guaranteed to be a file-handle open on a zip file.
13312**
13313** This function attempts to locate the EOCD record within the zip archive
13314** and populate *pEOCD with the results of decoding it. SQLITE_OK is
13315** returned if successful. Otherwise, an SQLite error code is returned and
13316** an English language error message may be left in virtual-table pTab.
13317*/
13318static int zipfileReadEOCD(
13319  ZipfileTab *pTab,               /* Return errors here */
13320  const u8 *aBlob,                /* Pointer to in-memory file image */
13321  i64 nBlob,                      /* Size of aBlob[] in bytes */
13322  FILE *pFile,                    /* Read from this file if aBlob==0 */
13323  ZipfileEOCD *pEOCD              /* Object to populate */
13324){
13325  u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
13326  i64 nRead;                      /* Bytes to read from file */
13327  int rc = SQLITE_OK;
13328
13329  memset(pEOCD, 0, sizeof(ZipfileEOCD));
13330  if( aBlob==0 ){
13331    i64 iOff;                     /* Offset to read from */
13332    i64 szFile;                   /* Total size of file in bytes */
13333    fseek(pFile, 0, SEEK_END);
13334    szFile = (i64)ftell(pFile);
13335    if( szFile==0 ){
13336      return SQLITE_OK;
13337    }
13338    nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
13339    iOff = szFile - nRead;
13340    rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
13341  }else{
13342    nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
13343    aRead = (u8*)&aBlob[nBlob-nRead];
13344  }
13345
13346  if( rc==SQLITE_OK ){
13347    i64 i;
13348
13349    /* Scan backwards looking for the signature bytes */
13350    for(i=nRead-20; i>=0; i--){
13351      if( aRead[i]==0x50 && aRead[i+1]==0x4b 
13352       && aRead[i+2]==0x05 && aRead[i+3]==0x06 
13353      ){
13354        break;
13355      }
13356    }
13357    if( i<0 ){
13358      zipfileTableErr(pTab, "cannot find end of central directory record");
13359      return SQLITE_ERROR;
13360    }
13361
13362    aRead += i+4;
13363    pEOCD->iDisk = zipfileRead16(aRead);
13364    pEOCD->iFirstDisk = zipfileRead16(aRead);
13365    pEOCD->nEntry = zipfileRead16(aRead);
13366    pEOCD->nEntryTotal = zipfileRead16(aRead);
13367    pEOCD->nSize = zipfileRead32(aRead);
13368    pEOCD->iOffset = zipfileRead32(aRead);
13369  }
13370
13371  return rc;
13372}
13373
13374/*
13375** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry 
13376** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
13377** to the end of the list. Otherwise, it is added to the list immediately
13378** before pBefore (which is guaranteed to be a part of said list).
13379*/
13380static void zipfileAddEntry(
13381  ZipfileTab *pTab, 
13382  ZipfileEntry *pBefore, 
13383  ZipfileEntry *pNew
13384){
13385  assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
13386  assert( pNew->pNext==0 );
13387  if( pBefore==0 ){
13388    if( pTab->pFirstEntry==0 ){
13389      pTab->pFirstEntry = pTab->pLastEntry = pNew;
13390    }else{
13391      assert( pTab->pLastEntry->pNext==0 );
13392      pTab->pLastEntry->pNext = pNew;
13393      pTab->pLastEntry = pNew;
13394    }
13395  }else{
13396    ZipfileEntry **pp;
13397    for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
13398    pNew->pNext = pBefore;
13399    *pp = pNew;
13400  }
13401}
13402
13403static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, i64 nBlob){
13404  ZipfileEOCD eocd;
13405  int rc;
13406  int i;
13407  i64 iOff;
13408
13409  rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
13410  iOff = eocd.iOffset;
13411  for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
13412    ZipfileEntry *pNew = 0;
13413    rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
13414
13415    if( rc==SQLITE_OK ){
13416      zipfileAddEntry(pTab, 0, pNew);
13417      iOff += ZIPFILE_CDS_FIXED_SZ;
13418      iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
13419    }
13420  }
13421  return rc;
13422}
13423
13424/*
13425** xFilter callback.
13426*/
13427static int zipfileFilter(
13428  sqlite3_vtab_cursor *cur, 
13429  int idxNum, const char *idxStr,
13430  int argc, sqlite3_value **argv
13431){
13432  ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
13433  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
13434  const char *zFile = 0;          /* Zip file to scan */
13435  int rc = SQLITE_OK;             /* Return Code */
13436  int bInMemory = 0;              /* True for an in-memory zipfile */
13437
13438  (void)idxStr;
13439  (void)argc;
13440
13441  zipfileResetCursor(pCsr);
13442
13443  if( pTab->zFile ){
13444    zFile = pTab->zFile;
13445  }else if( idxNum==0 ){
13446    zipfileCursorErr(pCsr, "zipfile() function requires an argument");
13447    return SQLITE_ERROR;
13448  }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
13449    static const u8 aEmptyBlob = 0;
13450    const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
13451    i64 nBlob = sqlite3_value_bytes(argv[0]);
13452    assert( pTab->pFirstEntry==0 );
13453    if( aBlob==0 ){
13454      aBlob = &aEmptyBlob;
13455      nBlob = 0;
13456    }
13457    rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
13458    pCsr->pFreeEntry = pTab->pFirstEntry;
13459    pTab->pFirstEntry = pTab->pLastEntry = 0;
13460    if( rc!=SQLITE_OK ) return rc;
13461    bInMemory = 1;
13462  }else{
13463    zFile = (const char*)sqlite3_value_text(argv[0]);
13464  }
13465
13466  if( 0==pTab->pWriteFd && 0==bInMemory ){
13467    pCsr->pFile = zFile ? sqlite3_fopen(zFile, "rb") : 0;
13468    if( pCsr->pFile==0 ){
13469      zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
13470      rc = SQLITE_ERROR;
13471    }else{
13472      rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
13473      if( rc==SQLITE_OK ){
13474        if( pCsr->eocd.nEntry==0 ){
13475          pCsr->bEof = 1;
13476        }else{
13477          pCsr->iNextOff = pCsr->eocd.iOffset;
13478          rc = zipfileNext(cur);
13479        }
13480      }
13481    }
13482  }else{
13483    pCsr->bNoop = 1;
13484    pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
13485    rc = zipfileNext(cur);
13486  }
13487
13488  return rc;
13489}
13490
13491/*
13492** xBestIndex callback.
13493*/
13494static int zipfileBestIndex(
13495  sqlite3_vtab *tab,
13496  sqlite3_index_info *pIdxInfo
13497){
13498  int i;
13499  int idx = -1;
13500  int unusable = 0;
13501  (void)tab;
13502
13503  for(i=0; i<pIdxInfo->nConstraint; i++){
13504    const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
13505    if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
13506    if( pCons->usable==0 ){
13507      unusable = 1;
13508    }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
13509      idx = i;
13510    }
13511  }
13512  pIdxInfo->estimatedCost = 1000.0;
13513  if( idx>=0 ){
13514    pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
13515    pIdxInfo->aConstraintUsage[idx].omit = 1;
13516    pIdxInfo->idxNum = 1;
13517  }else if( unusable ){
13518    return SQLITE_CONSTRAINT;
13519  }
13520  return SQLITE_OK;
13521}
13522
13523static ZipfileEntry *zipfileNewEntry(const char *zPath){
13524  ZipfileEntry *pNew;
13525  pNew = sqlite3_malloc64(sizeof(ZipfileEntry));
13526  if( pNew ){
13527    memset(pNew, 0, sizeof(ZipfileEntry));
13528    pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
13529    if( pNew->cds.zFile==0 ){
13530      sqlite3_free(pNew);
13531      pNew = 0;
13532    }
13533  }
13534  return pNew;
13535}
13536
13537static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
13538  ZipfileCDS *pCds = &pEntry->cds;
13539  u8 *a = aBuf;
13540
13541  pCds->nExtra = 9;
13542
13543  /* Write the LFH itself */
13544  zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
13545  zipfileWrite16(a, pCds->iVersionExtract);
13546  zipfileWrite16(a, pCds->flags);
13547  zipfileWrite16(a, pCds->iCompression);
13548  zipfileWrite16(a, pCds->mTime);
13549  zipfileWrite16(a, pCds->mDate);
13550  zipfileWrite32(a, pCds->crc32);
13551  zipfileWrite32(a, pCds->szCompressed);
13552  zipfileWrite32(a, pCds->szUncompressed);
13553  zipfileWrite16(a, (u16)pCds->nFile);
13554  zipfileWrite16(a, pCds->nExtra);
13555  assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
13556
13557  /* Add the file name */
13558  memcpy(a, pCds->zFile, (int)pCds->nFile);
13559  a += (int)pCds->nFile;
13560
13561  /* The "extra" data */
13562  zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
13563  zipfileWrite16(a, 5);
13564  *a++ = 0x01;
13565  zipfileWrite32(a, pEntry->mUnixTime);
13566
13567  return a-aBuf;
13568}
13569
13570static int zipfileAppendEntry(
13571  ZipfileTab *pTab,
13572  ZipfileEntry *pEntry,
13573  const u8 *pData,
13574  int nData
13575){
13576  u8 *aBuf = pTab->aBuffer;
13577  int nBuf;
13578  int rc;
13579
13580  nBuf = zipfileSerializeLFH(pEntry, aBuf);
13581  rc = zipfileAppendData(pTab, aBuf, nBuf);
13582  if( rc==SQLITE_OK ){
13583    pEntry->iDataOff = pTab->szCurrent;
13584    rc = zipfileAppendData(pTab, pData, nData);
13585  }
13586
13587  return rc;
13588}
13589
13590static int zipfileGetMode(
13591  sqlite3_value *pVal, 
13592  int bIsDir,                     /* If true, default to directory */
13593  u32 *pMode,                     /* OUT: Mode value */
13594  char **pzErr                    /* OUT: Error message */
13595){
13596  const char *z = (const char*)sqlite3_value_text(pVal);
13597  u32 mode = 0;
13598  if( z==0 ){
13599    mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
13600  }else if( z[0]>='0' && z[0]<='9' ){
13601    mode = (unsigned int)sqlite3_value_int(pVal);
13602  }else{
13603    const char zTemplate[11] = "-rwxrwxrwx";
13604    int i;
13605    if( strlen(z)!=10 ) goto parse_error;
13606    switch( z[0] ){
13607      case '-': mode |= S_IFREG; break;
13608      case 'd': mode |= S_IFDIR; break;
13609      case 'l': mode |= S_IFLNK; break;
13610      default: goto parse_error;
13611    }
13612    for(i=1; i<10; i++){
13613      if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
13614      else if( z[i]!='-' ) goto parse_error;
13615    }
13616  }
13617  if( ((mode & S_IFDIR)==0)==bIsDir ){
13618    /* The "mode" attribute is a directory, but data has been specified.
13619    ** Or vice-versa - no data but "mode" is a file or symlink.  */
13620    *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
13621    return SQLITE_CONSTRAINT;
13622  }
13623  *pMode = mode;
13624  return SQLITE_OK;
13625
13626 parse_error:
13627  *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
13628  return SQLITE_ERROR;
13629}
13630
13631/*
13632** Both (const char*) arguments point to nul-terminated strings. Argument
13633** nB is the value of strlen(zB). This function returns 0 if the strings are
13634** identical, ignoring any trailing '/' character in either path.  */
13635static int zipfileComparePath(const char *zA, const char *zB, int nB){
13636  int nA = (int)strlen(zA);
13637  if( nA>0 && zA[nA-1]=='/' ) nA--;
13638  if( nB>0 && zB[nB-1]=='/' ) nB--;
13639  if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
13640  return 1;
13641}
13642
13643static int zipfileBegin(sqlite3_vtab *pVtab){
13644  ZipfileTab *pTab = (ZipfileTab*)pVtab;
13645  int rc = SQLITE_OK;
13646
13647  assert( pTab->pWriteFd==0 );
13648  if( pTab->zFile==0 || pTab->zFile[0]==0 ){
13649    zipfileTableErr(pTab, "zipfile: missing filename");
13650    return SQLITE_ERROR;
13651  }
13652
13653  /* Open a write fd on the file. Also load the entire central directory
13654  ** structure into memory. During the transaction any new file data is 
13655  ** appended to the archive file, but the central directory is accumulated
13656  ** in main-memory until the transaction is committed.  */
13657  pTab->pWriteFd = sqlite3_fopen(pTab->zFile, "ab+");
13658  if( pTab->pWriteFd==0 ){
13659    zipfileTableErr(pTab,
13660      "zipfile: failed to open file %s for writing", pTab->zFile
13661    );
13662    rc = SQLITE_ERROR;
13663  }else{
13664    fseek(pTab->pWriteFd, 0, SEEK_END);
13665    pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
13666    rc = zipfileLoadDirectory(pTab, 0, 0);
13667  }
13668
13669  if( rc!=SQLITE_OK ){
13670    zipfileCleanupTransaction(pTab);
13671  }
13672
13673  return rc;
13674}
13675
13676/*
13677** Return the current time as a 32-bit timestamp in UNIX epoch format (like
13678** time(2)).
13679*/
13680static u32 zipfileTime(void){
13681  sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
13682  u32 ret;
13683  if( pVfs==0 ) return 0;
13684  if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
13685    i64 ms;
13686    pVfs->xCurrentTimeInt64(pVfs, &ms);
13687    ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
13688  }else{
13689    double day;
13690    pVfs->xCurrentTime(pVfs, &day);
13691    ret = (u32)((day - 2440587.5) * 86400);
13692  }
13693  return ret;
13694}
13695
13696/*
13697** Return a 32-bit timestamp in UNIX epoch format.
13698**
13699** If the value passed as the only argument is either NULL or an SQL NULL,
13700** return the current time. Otherwise, return the value stored in (*pVal)
13701** cast to a 32-bit unsigned integer.
13702*/
13703static u32 zipfileGetTime(sqlite3_value *pVal){
13704  if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
13705    return zipfileTime();
13706  }
13707  return (u32)sqlite3_value_int64(pVal);
13708}
13709
13710/*
13711** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
13712** linked list.  Remove it from the list and free the object.
13713*/
13714static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
13715  if( pOld ){
13716    if( pTab->pFirstEntry==pOld ){
13717      pTab->pFirstEntry = pOld->pNext;
13718      if( pTab->pLastEntry==pOld ) pTab->pLastEntry = 0;
13719    }else{
13720      ZipfileEntry *p;
13721      for(p=pTab->pFirstEntry; p; p=p->pNext){
13722        if( p->pNext==pOld ){
13723          p->pNext = pOld->pNext;
13724          if( pTab->pLastEntry==pOld ) pTab->pLastEntry = p;
13725          break;
13726        }
13727      }
13728    }
13729    zipfileEntryFree(pOld);
13730  }
13731}
13732
13733/*
13734** xUpdate method.
13735*/
13736static int zipfileUpdate(
13737  sqlite3_vtab *pVtab, 
13738  int nVal, 
13739  sqlite3_value **apVal, 
13740  sqlite_int64 *pRowid
13741){
13742  ZipfileTab *pTab = (ZipfileTab*)pVtab;
13743  int rc = SQLITE_OK;             /* Return Code */
13744  ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
13745
13746  u32 mode = 0;                   /* Mode for new entry */
13747  u32 mTime = 0;                  /* Modification time for new entry */
13748  i64 sz = 0;                     /* Uncompressed size */
13749  const char *zPath = 0;          /* Path for new entry */
13750  int nPath = 0;                  /* strlen(zPath) */
13751  const u8 *pData = 0;            /* Pointer to buffer containing content */
13752  int nData = 0;                  /* Size of pData buffer in bytes */
13753  int iMethod = 0;                /* Compression method for new entry */
13754  u8 *pFree = 0;                  /* Free this */
13755  char *zFree = 0;                /* Also free this */
13756  ZipfileEntry *pOld = 0;
13757  ZipfileEntry *pOld2 = 0;
13758  int bUpdate = 0;                /* True for an update that modifies "name" */
13759  int bIsDir = 0;
13760  u32 iCrc32 = 0;
13761
13762  (void)pRowid;
13763
13764  if( pTab->pWriteFd==0 ){
13765    rc = zipfileBegin(pVtab);
13766    if( rc!=SQLITE_OK ) return rc;
13767  }
13768
13769  /* If this is a DELETE or UPDATE, find the archive entry to delete. */
13770  if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
13771    const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
13772    int nDelete = (int)strlen(zDelete);
13773    if( nVal>1 ){
13774      const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
13775      if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
13776        bUpdate = 1;
13777      }
13778    }
13779    for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
13780      if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
13781        break;
13782      }
13783      assert( pOld->pNext );
13784    }
13785  }
13786
13787  if( nVal>1 ){
13788    /* Check that "sz" and "rawdata" are both NULL: */
13789    if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
13790      zipfileTableErr(pTab, "sz must be NULL");
13791      rc = SQLITE_CONSTRAINT;
13792    }
13793    if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
13794      zipfileTableErr(pTab, "rawdata must be NULL"); 
13795      rc = SQLITE_CONSTRAINT;
13796    }
13797
13798    if( rc==SQLITE_OK ){
13799      if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
13800        /* data=NULL. A directory */
13801        bIsDir = 1;
13802      }else{
13803        /* Value specified for "data", and possibly "method". This must be
13804        ** a regular file or a symlink. */
13805        const u8 *aIn = sqlite3_value_blob(apVal[7]);
13806        int nIn = sqlite3_value_bytes(apVal[7]);
13807        int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
13808
13809        iMethod = sqlite3_value_int(apVal[8]);
13810        sz = nIn;
13811        pData = aIn;
13812        nData = nIn;
13813        if( iMethod!=0 && iMethod!=8 ){
13814          zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
13815          rc = SQLITE_CONSTRAINT;
13816        }else{
13817          if( bAuto || iMethod ){
13818            int nCmp;
13819            rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
13820            if( rc==SQLITE_OK ){
13821              if( iMethod || nCmp<nIn ){
13822                iMethod = 8;
13823                pData = pFree;
13824                nData = nCmp;
13825              }
13826            }
13827          }
13828          iCrc32 = crc32(0, aIn, nIn);
13829        }
13830      }
13831    }
13832
13833    if( rc==SQLITE_OK ){
13834      rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
13835    }
13836
13837    if( rc==SQLITE_OK ){
13838      zPath = (const char*)sqlite3_value_text(apVal[2]);
13839      if( zPath==0 ) zPath = "";
13840      nPath = (int)strlen(zPath);
13841      if( nPath>ZIPFILE_MX_NAME ){
13842        zipfileTableErr(pTab, "filename too long; max: %d bytes",
13843                        ZIPFILE_MX_NAME);
13844        rc = SQLITE_CONSTRAINT;
13845      }
13846      mTime = zipfileGetTime(apVal[4]);
13847    }
13848
13849    if( rc==SQLITE_OK && bIsDir ){
13850      /* For a directory, check that the last character in the path is a
13851      ** '/'. This appears to be required for compatibility with info-zip
13852      ** (the unzip command on unix). It does not create directories
13853      ** otherwise.  */
13854      if( nPath<=0 || zPath[nPath-1]!='/' ){
13855        zFree = sqlite3_mprintf("%s/", zPath);
13856        zPath = (const char*)zFree;
13857        if( zFree==0 ){
13858          rc = SQLITE_NOMEM;
13859          nPath = 0;
13860        }else{
13861          nPath = (int)strlen(zPath);
13862        }
13863      }
13864    }
13865
13866    /* Check that we're not inserting a duplicate entry -OR- updating an
13867    ** entry with a path, thereby making it into a duplicate. */
13868    if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
13869      ZipfileEntry *p;
13870      for(p=pTab->pFirstEntry; p; p=p->pNext){
13871        if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
13872          switch( sqlite3_vtab_on_conflict(pTab->db) ){
13873            case SQLITE_IGNORE: {
13874              goto zipfile_update_done;
13875            }
13876            case SQLITE_REPLACE: {
13877              pOld2 = p;
13878              break;
13879            }
13880            default: {
13881              zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
13882              rc = SQLITE_CONSTRAINT;
13883              break;
13884            }
13885          }
13886          break;
13887        }
13888      }
13889    }
13890
13891    if( rc==SQLITE_OK ){
13892      /* Create the new CDS record. */
13893      pNew = zipfileNewEntry(zPath);
13894      if( pNew==0 ){
13895        rc = SQLITE_NOMEM;
13896      }else{
13897        pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
13898        pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
13899        pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
13900        pNew->cds.iCompression = (u16)iMethod;
13901        zipfileMtimeToDos(&pNew->cds, mTime);
13902        pNew->cds.crc32 = iCrc32;
13903        pNew->cds.szCompressed = nData;
13904        pNew->cds.szUncompressed = (u32)sz;
13905        pNew->cds.iExternalAttr = (mode<<16);
13906        pNew->cds.iOffset = (u32)pTab->szCurrent;
13907        pNew->cds.nFile = (u16)nPath;
13908        pNew->mUnixTime = (u32)mTime;
13909        rc = zipfileAppendEntry(pTab, pNew, pData, nData);
13910        zipfileAddEntry(pTab, pOld, pNew);
13911      }
13912    }
13913  }
13914
13915  if( rc==SQLITE_OK && (pOld || pOld2) ){
13916    ZipfileCsr *pCsr;
13917    for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
13918      if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
13919        pCsr->pCurrent = pCsr->pCurrent->pNext;
13920        pCsr->bNoop = 1;
13921      }
13922    }
13923
13924    zipfileRemoveEntryFromList(pTab, pOld);
13925    zipfileRemoveEntryFromList(pTab, pOld2);
13926  }
13927
13928zipfile_update_done:
13929  sqlite3_free(pFree);
13930  sqlite3_free(zFree);
13931  return rc;
13932}
13933
13934static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
13935  u8 *a = aBuf;
13936  zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
13937  zipfileWrite16(a, p->iDisk);
13938  zipfileWrite16(a, p->iFirstDisk);
13939  zipfileWrite16(a, p->nEntry);
13940  zipfileWrite16(a, p->nEntryTotal);
13941  zipfileWrite32(a, p->nSize);
13942  zipfileWrite32(a, p->iOffset);
13943  zipfileWrite16(a, 0);        /* Size of trailing comment in bytes*/
13944
13945  return a-aBuf;
13946}
13947
13948static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
13949  int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
13950  assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
13951  return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
13952}
13953
13954/*
13955** Serialize the CDS structure into buffer aBuf[]. Return the number
13956** of bytes written.
13957*/
13958static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
13959  u8 *a = aBuf;
13960  ZipfileCDS *pCDS = &pEntry->cds;
13961
13962  if( pEntry->aExtra==0 ){
13963    pCDS->nExtra = 9;
13964  }
13965
13966  zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
13967  zipfileWrite16(a, pCDS->iVersionMadeBy);
13968  zipfileWrite16(a, pCDS->iVersionExtract);
13969  zipfileWrite16(a, pCDS->flags);
13970  zipfileWrite16(a, pCDS->iCompression);
13971  zipfileWrite16(a, pCDS->mTime);
13972  zipfileWrite16(a, pCDS->mDate);
13973  zipfileWrite32(a, pCDS->crc32);
13974  zipfileWrite32(a, pCDS->szCompressed);
13975  zipfileWrite32(a, pCDS->szUncompressed);
13976  assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
13977  zipfileWrite16(a, pCDS->nFile);
13978  zipfileWrite16(a, pCDS->nExtra);
13979  zipfileWrite16(a, pCDS->nComment);
13980  zipfileWrite16(a, pCDS->iDiskStart);
13981  zipfileWrite16(a, pCDS->iInternalAttr);
13982  zipfileWrite32(a, pCDS->iExternalAttr);
13983  zipfileWrite32(a, pCDS->iOffset);
13984
13985  memcpy(a, pCDS->zFile, pCDS->nFile);
13986  a += pCDS->nFile;
13987
13988  if( pEntry->aExtra ){
13989    int n = (int)pCDS->nExtra + (int)pCDS->nComment;
13990    memcpy(a, pEntry->aExtra, n);
13991    a += n;
13992  }else{
13993    assert( pCDS->nExtra==9 );
13994    zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
13995    zipfileWrite16(a, 5);
13996    *a++ = 0x01;
13997    zipfileWrite32(a, pEntry->mUnixTime);
13998  }
13999
14000  return a-aBuf;
14001}
14002
14003static int zipfileCommit(sqlite3_vtab *pVtab){
14004  ZipfileTab *pTab = (ZipfileTab*)pVtab;
14005  int rc = SQLITE_OK;
14006  if( pTab->pWriteFd ){
14007    i64 iOffset = pTab->szCurrent;
14008    ZipfileEntry *p;
14009    ZipfileEOCD eocd;
14010    int nEntry = 0;
14011
14012    /* Write out all entries */
14013    for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
14014      int n = zipfileSerializeCDS(p, pTab->aBuffer);
14015      rc = zipfileAppendData(pTab, pTab->aBuffer, n);
14016      nEntry++;
14017    }
14018
14019    /* Write out the EOCD record */
14020    eocd.iDisk = 0;
14021    eocd.iFirstDisk = 0;
14022    eocd.nEntry = (u16)nEntry;
14023    eocd.nEntryTotal = (u16)nEntry;
14024    eocd.nSize = (u32)(pTab->szCurrent - iOffset);
14025    eocd.iOffset = (u32)iOffset;
14026    rc = zipfileAppendEOCD(pTab, &eocd);
14027
14028    zipfileCleanupTransaction(pTab);
14029  }
14030  return rc;
14031}
14032
14033static int zipfileRollback(sqlite3_vtab *pVtab){
14034  return zipfileCommit(pVtab);
14035}
14036
14037static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
14038  ZipfileCsr *pCsr;
14039  for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
14040    if( iId==pCsr->iId ) break;
14041  }
14042  return pCsr;
14043}
14044
14045static void zipfileFunctionCds(
14046  sqlite3_context *context,
14047  int argc,
14048  sqlite3_value **argv
14049){
14050  ZipfileCsr *pCsr;
14051  ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
14052  assert( argc>0 );
14053
14054  pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
14055  if( pCsr ){
14056    ZipfileCDS *p = &pCsr->pCurrent->cds;
14057    char *zRes = sqlite3_mprintf("{"
14058        "\"version-made-by\" : %u, "
14059        "\"version-to-extract\" : %u, "
14060        "\"flags\" : %u, "
14061        "\"compression\" : %u, "
14062        "\"time\" : %u, "
14063        "\"date\" : %u, "
14064        "\"crc32\" : %u, "
14065        "\"compressed-size\" : %u, "
14066        "\"uncompressed-size\" : %u, "
14067        "\"file-name-length\" : %u, "
14068        "\"extra-field-length\" : %u, "
14069        "\"file-comment-length\" : %u, "
14070        "\"disk-number-start\" : %u, "
14071        "\"internal-attr\" : %u, "
14072        "\"external-attr\" : %u, "
14073        "\"offset\" : %u }",
14074        (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
14075        (u32)p->flags, (u32)p->iCompression,
14076        (u32)p->mTime, (u32)p->mDate,
14077        (u32)p->crc32, (u32)p->szCompressed,
14078        (u32)p->szUncompressed, (u32)p->nFile,
14079        (u32)p->nExtra, (u32)p->nComment,
14080        (u32)p->iDiskStart, (u32)p->iInternalAttr,
14081        (u32)p->iExternalAttr, (u32)p->iOffset
14082    );
14083
14084    if( zRes==0 ){
14085      sqlite3_result_error_nomem(context);
14086    }else{
14087      sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
14088      sqlite3_free(zRes);
14089    }
14090  }
14091}
14092
14093/*
14094** xFindFunction method.
14095*/
14096static int zipfileFindFunction(
14097  sqlite3_vtab *pVtab,            /* Virtual table handle */
14098  int nArg,                       /* Number of SQL function arguments */
14099  const char *zName,              /* Name of SQL function */
14100  void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
14101  void **ppArg                    /* OUT: User data for *pxFunc */
14102){
14103  (void)nArg;
14104  if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
14105    *pxFunc = zipfileFunctionCds;
14106    *ppArg = (void*)pVtab;
14107    return 1;
14108  }
14109  return 0;
14110}
14111
14112typedef struct ZipfileBuffer ZipfileBuffer;
14113struct ZipfileBuffer {
14114  u8 *a;                          /* Pointer to buffer */
14115  int n;                          /* Size of buffer in bytes */
14116  int nAlloc;                     /* Byte allocated at a[] */
14117};
14118
14119typedef struct ZipfileCtx ZipfileCtx;
14120struct ZipfileCtx {
14121  int nEntry;
14122  ZipfileBuffer body;
14123  ZipfileBuffer cds;
14124};
14125
14126static int zipfileBufferGrow(ZipfileBuffer *pBuf, i64 nByte){
14127  if( pBuf->n+nByte>pBuf->nAlloc ){
14128    u8 *aNew;
14129    sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
14130    int nReq = pBuf->n + nByte;
14131
14132    while( nNew<nReq ) nNew = nNew*2;
14133    aNew = sqlite3_realloc64(pBuf->a, nNew);
14134    if( aNew==0 ) return SQLITE_NOMEM;
14135    pBuf->a = aNew;
14136    pBuf->nAlloc = (int)nNew;
14137  }
14138  return SQLITE_OK;
14139}
14140
14141/*
14142** xStep() callback for the zipfile() aggregate. This can be called in
14143** any of the following ways:
14144**
14145**   SELECT zipfile(name,data) ...
14146**   SELECT zipfile(name,mode,mtime,data) ...
14147**   SELECT zipfile(name,mode,mtime,data,method) ...
14148*/
14149static void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
14150  ZipfileCtx *p;                  /* Aggregate function context */
14151  ZipfileEntry e;                 /* New entry to add to zip archive */
14152
14153  sqlite3_value *pName = 0;
14154  sqlite3_value *pMode = 0;
14155  sqlite3_value *pMtime = 0;
14156  sqlite3_value *pData = 0;
14157  sqlite3_value *pMethod = 0;
14158
14159  int bIsDir = 0;
14160  u32 mode;
14161  int rc = SQLITE_OK;
14162  char *zErr = 0;
14163
14164  int iMethod = -1;               /* Compression method to use (0 or 8) */
14165
14166  const u8 *aData = 0;            /* Possibly compressed data for new entry */
14167  int nData = 0;                  /* Size of aData[] in bytes */
14168  int szUncompressed = 0;         /* Size of data before compression */
14169  u8 *aFree = 0;                  /* Free this before returning */
14170  u32 iCrc32 = 0;                 /* crc32 of uncompressed data */
14171
14172  char *zName = 0;                /* Path (name) of new entry */
14173  int nName = 0;                  /* Size of zName in bytes */
14174  char *zFree = 0;                /* Free this before returning */
14175  i64 nByte;
14176
14177  memset(&e, 0, sizeof(e));
14178  p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
14179  if( p==0 ) return;
14180
14181  /* Martial the arguments into stack variables */
14182  if( nVal!=2 && nVal!=4 && nVal!=5 ){
14183    zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
14184    rc = SQLITE_ERROR;
14185    goto zipfile_step_out;
14186  }
14187  pName = apVal[0];
14188  if( nVal==2 ){
14189    pData = apVal[1];
14190  }else{
14191    pMode = apVal[1];
14192    pMtime = apVal[2];
14193    pData = apVal[3];
14194    if( nVal==5 ){
14195      pMethod = apVal[4];
14196    }
14197  }
14198
14199  /* Check that the 'name' parameter looks ok. */
14200  zName = (char*)sqlite3_value_text(pName);
14201  nName = sqlite3_value_bytes(pName);
14202  if( zName==0 ){
14203    zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
14204    rc = SQLITE_ERROR;
14205    goto zipfile_step_out;
14206  }
14207  if( nName>ZIPFILE_MX_NAME ){
14208    zErr = sqlite3_mprintf(
14209               "filename argument to zipfile() too big; max: %d bytes",
14210               ZIPFILE_MX_NAME);
14211    rc = SQLITE_ERROR;
14212    goto zipfile_step_out;
14213  }
14214
14215  /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
14216  ** deflate compression) or NULL (choose automatically).  */
14217  if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
14218    iMethod = (int)sqlite3_value_int64(pMethod);
14219    if( iMethod!=0 && iMethod!=8 ){
14220      zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
14221      rc = SQLITE_ERROR;
14222      goto zipfile_step_out;
14223    }
14224  }
14225
14226  /* Now inspect the data. If this is NULL, then the new entry must be a
14227  ** directory.  Otherwise, figure out whether or not the data should
14228  ** be deflated or simply stored in the zip archive. */
14229  if( sqlite3_value_type(pData)==SQLITE_NULL ){
14230    bIsDir = 1;
14231    iMethod = 0;
14232  }else{
14233    aData = sqlite3_value_blob(pData);
14234    szUncompressed = nData = sqlite3_value_bytes(pData);
14235    iCrc32 = crc32(0, aData, nData);
14236    if( iMethod<0 || iMethod==8 ){
14237      int nOut = 0;
14238      rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
14239      if( rc!=SQLITE_OK ){
14240        goto zipfile_step_out;
14241      }
14242      if( iMethod==8 || nOut<nData ){
14243        aData = aFree;
14244        nData = nOut;
14245        iMethod = 8;
14246      }else{
14247        iMethod = 0;
14248      }
14249    }
14250  }
14251
14252  /* Decode the "mode" argument. */
14253  rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
14254  if( rc ) goto zipfile_step_out;
14255
14256  /* Decode the "mtime" argument. */
14257  e.mUnixTime = zipfileGetTime(pMtime);
14258
14259  /* If this is a directory entry, ensure that there is exactly one '/'
14260  ** at the end of the path. Or, if this is not a directory and the path
14261  ** ends in '/' it is an error. */
14262  if( bIsDir==0 ){
14263    if( nName>0 && zName[nName-1]=='/' ){
14264      zErr = sqlite3_mprintf("non-directory name must not end with /");
14265      rc = SQLITE_ERROR;
14266      goto zipfile_step_out;
14267    }
14268  }else{
14269    if( nName==0 || zName[nName-1]!='/' ){
14270      zName = zFree = sqlite3_mprintf("%s/", zName);
14271      if( zName==0 ){
14272        rc = SQLITE_NOMEM;
14273        goto zipfile_step_out;
14274      }
14275      nName = (int)strlen(zName);
14276    }else{
14277      while( nName>1 && zName[nName-2]=='/' ) nName--;
14278    }
14279  }
14280
14281  /* Assemble the ZipfileEntry object for the new zip archive entry */
14282  e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
14283  e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
14284  e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
14285  e.cds.iCompression = (u16)iMethod;
14286  zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
14287  e.cds.crc32 = iCrc32;
14288  e.cds.szCompressed = nData;
14289  e.cds.szUncompressed = szUncompressed;
14290  e.cds.iExternalAttr = (mode<<16);
14291  e.cds.iOffset = p->body.n;
14292  e.cds.nFile = (u16)nName;
14293  e.cds.zFile = zName;
14294
14295  /* Append the LFH to the body of the new archive */
14296  nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
14297  if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
14298  p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
14299
14300  /* Append the data to the body of the new archive */
14301  if( nData>0 ){
14302    if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
14303    memcpy(&p->body.a[p->body.n], aData, nData);
14304    p->body.n += nData;
14305  }
14306
14307  /* Append the CDS record to the directory of the new archive */
14308  nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
14309  if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
14310  p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
14311
14312  /* Increment the count of entries in the archive */
14313  p->nEntry++;
14314
14315 zipfile_step_out:
14316  sqlite3_free(aFree);
14317  sqlite3_free(zFree);
14318  if( rc ){
14319    if( zErr ){
14320      sqlite3_result_error(pCtx, zErr, -1);
14321    }else{
14322      sqlite3_result_error_code(pCtx, rc);
14323    }
14324  }
14325  sqlite3_free(zErr);
14326}
14327
14328/*
14329** xFinalize() callback for zipfile aggregate function.
14330*/
14331static void zipfileFinal(sqlite3_context *pCtx){
14332  ZipfileCtx *p;
14333  ZipfileEOCD eocd;
14334  sqlite3_int64 nZip;
14335  u8 *aZip;
14336
14337  p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
14338  if( p==0 ) return;
14339  if( p->nEntry>0 ){
14340    memset(&eocd, 0, sizeof(eocd));
14341    eocd.nEntry = (u16)p->nEntry;
14342    eocd.nEntryTotal = (u16)p->nEntry;
14343    eocd.nSize = p->cds.n;
14344    eocd.iOffset = p->body.n;
14345
14346    nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
14347    aZip = (u8*)sqlite3_malloc64(nZip);
14348    if( aZip==0 ){
14349      sqlite3_result_error_nomem(pCtx);
14350    }else{
14351      memcpy(aZip, p->body.a, p->body.n);
14352      memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
14353      zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
14354      sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
14355    }
14356  }
14357
14358  sqlite3_free(p->body.a);
14359  sqlite3_free(p->cds.a);
14360}
14361
14362
14363/*
14364** Register the "zipfile" virtual table.
14365*/
14366static int zipfileRegister(sqlite3 *db){
14367  static sqlite3_module zipfileModule = {
14368    1,                         /* iVersion */
14369    zipfileConnect,            /* xCreate */
14370    zipfileConnect,            /* xConnect */
14371    zipfileBestIndex,          /* xBestIndex */
14372    zipfileDisconnect,         /* xDisconnect */
14373    zipfileDisconnect,         /* xDestroy */
14374    zipfileOpen,               /* xOpen - open a cursor */
14375    zipfileClose,              /* xClose - close a cursor */
14376    zipfileFilter,             /* xFilter - configure scan constraints */
14377    zipfileNext,               /* xNext - advance a cursor */
14378    zipfileEof,                /* xEof - check for end of scan */
14379    zipfileColumn,             /* xColumn - read data */
14380    0,                         /* xRowid - read data */
14381    zipfileUpdate,             /* xUpdate */
14382    zipfileBegin,              /* xBegin */
14383    0,                         /* xSync */
14384    zipfileCommit,             /* xCommit */
14385    zipfileRollback,           /* xRollback */
14386    zipfileFindFunction,       /* xFindMethod */
14387    0,                         /* xRename */
14388    0,                         /* xSavepoint */
14389    0,                         /* xRelease */
14390    0,                         /* xRollback */
14391    0,                         /* xShadowName */
14392    0                          /* xIntegrity */
14393  };
14394
14395  int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
14396  if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
14397  if( rc==SQLITE_OK ){
14398    rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0, 
14399        zipfileStep, zipfileFinal
14400    );
14401  }
14402  assert( sizeof(i64)==8 );
14403  assert( sizeof(u32)==4 );
14404  assert( sizeof(u16)==2 );
14405  assert( sizeof(u8)==1 );
14406  return rc;
14407}
14408#else         /* SQLITE_OMIT_VIRTUALTABLE */
14409# define zipfileRegister(x) SQLITE_OK
14410#endif
14411
14412#ifdef _WIN32
14413
14414#endif
14415int sqlite3_zipfile_init(
14416  sqlite3 *db, 
14417  char **pzErrMsg, 
14418  const sqlite3_api_routines *pApi
14419){
14420  SQLITE_EXTENSION_INIT2(pApi);
14421  (void)pzErrMsg;  /* Unused parameter */
14422  return zipfileRegister(db);
14423}
14424
14425/************************* End ext/misc/zipfile.c ********************/
14426/************************* Begin ext/misc/sqlar.c ******************/
14427/*
14428** 2017-12-17
14429**
14430** The author disclaims copyright to this source code.  In place of
14431** a legal notice, here is a blessing:
14432**
14433**    May you do good and not evil.
14434**    May you find forgiveness for yourself and forgive others.
14435**    May you share freely, never taking more than you give.
14436**
14437******************************************************************************
14438**
14439** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
14440** for working with sqlar archives and used by the shell tool's built-in
14441** sqlar support.
14442*/
14443/* #include "sqlite3ext.h" */
14444SQLITE_EXTENSION_INIT1
14445#include <zlib.h>
14446#include <assert.h>
14447
14448/*
14449** Implementation of the "sqlar_compress(X)" SQL function.
14450**
14451** If the type of X is SQLITE_BLOB, and compressing that blob using
14452** zlib utility function compress() yields a smaller blob, return the
14453** compressed blob. Otherwise, return a copy of X.
14454**
14455** SQLar uses the "zlib format" for compressed content.  The zlib format
14456** contains a two-byte identification header and a four-byte checksum at
14457** the end.  This is different from ZIP which uses the raw deflate format.
14458**
14459** Future enhancements to SQLar might add support for new compression formats.
14460** If so, those new formats will be identified by alternative headers in the
14461** compressed data.
14462*/
14463static void sqlarCompressFunc(
14464  sqlite3_context *context,
14465  int argc,
14466  sqlite3_value **argv
14467){
14468  assert( argc==1 );
14469  if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
14470    const Bytef *pData = sqlite3_value_blob(argv[0]);
14471    uLong nData = sqlite3_value_bytes(argv[0]);
14472    uLongf nOut = compressBound(nData);
14473    Bytef *pOut;
14474
14475    pOut = (Bytef*)sqlite3_malloc64(nOut);
14476    if( pOut==0 ){
14477      sqlite3_result_error_nomem(context);
14478      return;
14479    }else{
14480      if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
14481        sqlite3_result_error(context, "error in compress()", -1);
14482      }else if( nOut<nData ){
14483        sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
14484      }else{
14485        sqlite3_result_value(context, argv[0]);
14486      }
14487      sqlite3_free(pOut);
14488    }
14489  }else{
14490    sqlite3_result_value(context, argv[0]);
14491  }
14492}
14493
14494/*
14495** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
14496**
14497** Parameter SZ is interpreted as an integer. If it is less than or
14498** equal to zero, then this function returns a copy of X. Or, if
14499** SZ is equal to the size of X when interpreted as a blob, also
14500** return a copy of X. Otherwise, decompress blob X using zlib
14501** utility function uncompress() and return the results (another
14502** blob).
14503*/
14504static void sqlarUncompressFunc(
14505  sqlite3_context *context,
14506  int argc,
14507  sqlite3_value **argv
14508){
14509  uLong nData;
14510  sqlite3_int64 sz;
14511
14512  assert( argc==2 );
14513  sz = sqlite3_value_int64(argv[1]);
14514
14515  if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
14516    sqlite3_result_value(context, argv[0]);
14517  }else{
14518    uLongf szf = sz;
14519    const Bytef *pData= sqlite3_value_blob(argv[0]);
14520    Bytef *pOut = sqlite3_malloc64(sz);
14521    if( pOut==0 ){
14522      sqlite3_result_error_nomem(context);
14523    }else if( Z_OK!=uncompress(pOut, &szf, pData, nData) ){
14524      sqlite3_result_error(context, "error in uncompress()", -1);
14525    }else{
14526      sqlite3_result_blob(context, pOut, szf, SQLITE_TRANSIENT);
14527    }
14528    sqlite3_free(pOut);
14529  }
14530}
14531
14532#ifdef _WIN32
14533
14534#endif
14535int sqlite3_sqlar_init(
14536  sqlite3 *db, 
14537  char **pzErrMsg, 
14538  const sqlite3_api_routines *pApi
14539){
14540  int rc = SQLITE_OK;
14541  SQLITE_EXTENSION_INIT2(pApi);
14542  (void)pzErrMsg;  /* Unused parameter */
14543  rc = sqlite3_create_function(db, "sqlar_compress", 1, 
14544                               SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
14545                               sqlarCompressFunc, 0, 0);
14546  if( rc==SQLITE_OK ){
14547    rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
14548                                 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
14549                                 sqlarUncompressFunc, 0, 0);
14550  }
14551  return rc;
14552}
14553
14554/************************* End ext/misc/sqlar.c ********************/
14555#endif
14556#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
14557/************************* Begin ext/expert/sqlite3expert.h ******************/
14558/*
14559** 2017 April 07
14560**
14561** The author disclaims copyright to this source code.  In place of
14562** a legal notice, here is a blessing:
14563**
14564**    May you do good and not evil.
14565**    May you find forgiveness for yourself and forgive others.
14566**    May you share freely, never taking more than you give.
14567**
14568*************************************************************************
14569*/
14570#if !defined(SQLITEEXPERT_H)
14571#define SQLITEEXPERT_H 1
14572/* #include "sqlite3.h" */
14573
14574typedef struct sqlite3expert sqlite3expert;
14575
14576/*
14577** Create a new sqlite3expert object.
14578**
14579** If successful, a pointer to the new object is returned and (*pzErr) set
14580** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
14581** an English-language error message. In this case it is the responsibility
14582** of the caller to eventually free the error message buffer using
14583** sqlite3_free().
14584*/
14585sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
14586
14587/*
14588** Configure an sqlite3expert object.
14589**
14590** EXPERT_CONFIG_SAMPLE:
14591**   By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
14592**   each candidate index. This involves scanning and sorting the entire
14593**   contents of each user database table once for each candidate index
14594**   associated with the table. For large databases, this can be 
14595**   prohibitively slow. This option allows the sqlite3expert object to
14596**   be configured so that sqlite_stat1 data is instead generated based on a
14597**   subset of each table, or so that no sqlite_stat1 data is used at all.
14598**
14599**   A single integer argument is passed to this option. If the value is less
14600**   than or equal to zero, then no sqlite_stat1 data is generated or used by
14601**   the analysis - indexes are recommended based on the database schema only.
14602**   Or, if the value is 100 or greater, complete sqlite_stat1 data is
14603**   generated for each candidate index (this is the default). Finally, if the
14604**   value falls between 0 and 100, then it represents the percentage of user
14605**   table rows that should be considered when generating sqlite_stat1 data.
14606**
14607**   Examples:
14608**
14609**     // Do not generate any sqlite_stat1 data
14610**     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
14611**
14612**     // Generate sqlite_stat1 data based on 10% of the rows in each table.
14613**     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
14614*/
14615int sqlite3_expert_config(sqlite3expert *p, int op, ...);
14616
14617#define EXPERT_CONFIG_SAMPLE 1    /* int */
14618
14619/*
14620** Specify zero or more SQL statements to be included in the analysis.
14621**
14622** Buffer zSql must contain zero or more complete SQL statements. This
14623** function parses all statements contained in the buffer and adds them
14624** to the internal list of statements to analyze. If successful, SQLITE_OK
14625** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
14626** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
14627** may be set to point to an English language error message. In this case
14628** the caller is responsible for eventually freeing the error message buffer
14629** using sqlite3_free().
14630**
14631** If an error does occur while processing one of the statements in the
14632** buffer passed as the second argument, none of the statements in the
14633** buffer are added to the analysis.
14634**
14635** This function must be called before sqlite3_expert_analyze(). If a call
14636** to this function is made on an sqlite3expert object that has already
14637** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
14638** immediately and no statements are added to the analysis.
14639*/
14640int sqlite3_expert_sql(
14641  sqlite3expert *p,               /* From a successful sqlite3_expert_new() */
14642  const char *zSql,               /* SQL statement(s) to add */
14643  char **pzErr                    /* OUT: Error message (if any) */
14644);
14645
14646
14647/*
14648** This function is called after the sqlite3expert object has been configured
14649** with all SQL statements using sqlite3_expert_sql() to actually perform
14650** the analysis. Once this function has been called, it is not possible to
14651** add further SQL statements to the analysis.
14652**
14653** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
14654** an error occurs, an SQLite error code is returned and (*pzErr) set to 
14655** point to a buffer containing an English language error message. In this
14656** case it is the responsibility of the caller to eventually free the buffer
14657** using sqlite3_free().
14658**
14659** If an error does occur within this function, the sqlite3expert object
14660** is no longer useful for any purpose. At that point it is no longer
14661** possible to add further SQL statements to the object or to re-attempt
14662** the analysis. The sqlite3expert object must still be freed using a call
14663** sqlite3_expert_destroy().
14664*/
14665int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
14666
14667/*
14668** Return the total number of statements loaded using sqlite3_expert_sql().
14669** The total number of SQL statements may be different from the total number
14670** to calls to sqlite3_expert_sql().
14671*/
14672int sqlite3_expert_count(sqlite3expert*);
14673
14674/*
14675** Return a component of the report.
14676**
14677** This function is called after sqlite3_expert_analyze() to extract the
14678** results of the analysis. Each call to this function returns either a
14679** NULL pointer or a pointer to a buffer containing a nul-terminated string.
14680** The value passed as the third argument must be one of the EXPERT_REPORT_*
14681** #define constants defined below.
14682**
14683** For some EXPERT_REPORT_* parameters, the buffer returned contains 
14684** information relating to a specific SQL statement. In these cases that
14685** SQL statement is identified by the value passed as the second argument.
14686** SQL statements are numbered from 0 in the order in which they are parsed.
14687** If an out-of-range value (less than zero or equal to or greater than the
14688** value returned by sqlite3_expert_count()) is passed as the second argument
14689** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
14690**
14691** EXPERT_REPORT_SQL:
14692**   Return the text of SQL statement iStmt.
14693**
14694** EXPERT_REPORT_INDEXES:
14695**   Return a buffer containing the CREATE INDEX statements for all recommended
14696**   indexes for statement iStmt. If there are no new recommeded indexes, NULL 
14697**   is returned.
14698**
14699** EXPERT_REPORT_PLAN:
14700**   Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
14701**   iStmt after the proposed indexes have been added to the database schema.
14702**
14703** EXPERT_REPORT_CANDIDATES:
14704**   Return a pointer to a buffer containing the CREATE INDEX statements 
14705**   for all indexes that were tested (for all SQL statements). The iStmt
14706**   parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
14707*/
14708const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
14709
14710/*
14711** Values for the third argument passed to sqlite3_expert_report().
14712*/
14713#define EXPERT_REPORT_SQL        1
14714#define EXPERT_REPORT_INDEXES    2
14715#define EXPERT_REPORT_PLAN       3
14716#define EXPERT_REPORT_CANDIDATES 4
14717
14718/*
14719** Free an (sqlite3expert*) handle and all associated resources. There 
14720** should be one call to this function for each successful call to 
14721** sqlite3-expert_new().
14722*/
14723void sqlite3_expert_destroy(sqlite3expert*);
14724
14725#endif  /* !defined(SQLITEEXPERT_H) */
14726
14727/************************* End ext/expert/sqlite3expert.h ********************/
14728/************************* Begin ext/expert/sqlite3expert.c ******************/
14729/*
14730** 2017 April 09
14731**
14732** The author disclaims copyright to this source code.  In place of
14733** a legal notice, here is a blessing:
14734**
14735**    May you do good and not evil.
14736**    May you find forgiveness for yourself and forgive others.
14737**    May you share freely, never taking more than you give.
14738**
14739*************************************************************************
14740*/
14741/* #include "sqlite3expert.h" */
14742#include <assert.h>
14743#include <string.h>
14744#include <stdio.h>
14745
14746#if !defined(SQLITE_AMALGAMATION)
14747#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
14748# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
14749#endif
14750#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
14751# define ALWAYS(X)      (1)
14752# define NEVER(X)       (0)
14753#elif !defined(NDEBUG)
14754# define ALWAYS(X)      ((X)?1:(assert(0),0))
14755# define NEVER(X)       ((X)?(assert(0),1):0)
14756#else
14757# define ALWAYS(X)      (X)
14758# define NEVER(X)       (X)
14759#endif
14760#endif /* !defined(SQLITE_AMALGAMATION) */
14761
14762
14763#ifndef SQLITE_OMIT_VIRTUALTABLE
14764
14765/* typedef sqlite3_int64 i64; */
14766/* typedef sqlite3_uint64 u64; */
14767
14768typedef struct IdxColumn IdxColumn;
14769typedef struct IdxConstraint IdxConstraint;
14770typedef struct IdxScan IdxScan;
14771typedef struct IdxStatement IdxStatement;
14772typedef struct IdxTable IdxTable;
14773typedef struct IdxWrite IdxWrite;
14774
14775#define STRLEN  (int)strlen
14776
14777/*
14778** A temp table name that we assume no user database will actually use.
14779** If this assumption proves incorrect triggers on the table with the
14780** conflicting name will be ignored.
14781*/
14782#define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
14783
14784/*
14785** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
14786** any other type of single-ended range constraint on a column).
14787**
14788** pLink:
14789**   Used to temporarily link IdxConstraint objects into lists while
14790**   creating candidate indexes.
14791*/
14792struct IdxConstraint {
14793  char *zColl;                    /* Collation sequence */
14794  int bRange;                     /* True for range, false for eq */
14795  int iCol;                       /* Constrained table column */
14796  int bFlag;                      /* Used by idxFindCompatible() */
14797  int bDesc;                      /* True if ORDER BY <expr> DESC */
14798  IdxConstraint *pNext;           /* Next constraint in pEq or pRange list */
14799  IdxConstraint *pLink;           /* See above */
14800};
14801
14802/*
14803** A single scan of a single table.
14804*/
14805struct IdxScan {
14806  IdxTable *pTab;                 /* Associated table object */
14807  int iDb;                        /* Database containing table zTable */
14808  i64 covering;                   /* Mask of columns required for cov. index */
14809  IdxConstraint *pOrder;          /* ORDER BY columns */
14810  IdxConstraint *pEq;             /* List of == constraints */
14811  IdxConstraint *pRange;          /* List of < constraints */
14812  IdxScan *pNextScan;             /* Next IdxScan object for same analysis */
14813};
14814
14815/*
14816** Information regarding a single database table. Extracted from 
14817** "PRAGMA table_info" by function idxGetTableInfo().
14818*/
14819struct IdxColumn {
14820  char *zName;
14821  char *zColl;
14822  int iPk;
14823};
14824struct IdxTable {
14825  int nCol;
14826  char *zName;                    /* Table name */
14827  IdxColumn *aCol;
14828  IdxTable *pNext;                /* Next table in linked list of all tables */
14829};
14830
14831/*
14832** An object of the following type is created for each unique table/write-op
14833** seen. The objects are stored in a singly-linked list beginning at
14834** sqlite3expert.pWrite.
14835*/
14836struct IdxWrite {
14837  IdxTable *pTab;
14838  int eOp;                        /* SQLITE_UPDATE, DELETE or INSERT */
14839  IdxWrite *pNext;
14840};
14841
14842/*
14843** Each statement being analyzed is represented by an instance of this
14844** structure.
14845*/
14846struct IdxStatement {
14847  int iId;                        /* Statement number */
14848  char *zSql;                     /* SQL statement */
14849  char *zIdx;                     /* Indexes */
14850  char *zEQP;                     /* Plan */
14851  IdxStatement *pNext;
14852};
14853
14854
14855/*
14856** A hash table for storing strings. With space for a payload string
14857** with each entry. Methods are:
14858**
14859**   idxHashInit()
14860**   idxHashClear()
14861**   idxHashAdd()
14862**   idxHashSearch()
14863*/
14864#define IDX_HASH_SIZE 1023
14865typedef struct IdxHashEntry IdxHashEntry;
14866typedef struct IdxHash IdxHash;
14867struct IdxHashEntry {
14868  char *zKey;                     /* nul-terminated key */
14869  char *zVal;                     /* nul-terminated value string */
14870  char *zVal2;                    /* nul-terminated value string 2 */
14871  IdxHashEntry *pHashNext;        /* Next entry in same hash bucket */
14872  IdxHashEntry *pNext;            /* Next entry in hash */
14873};
14874struct IdxHash {
14875  IdxHashEntry *pFirst;
14876  IdxHashEntry *aHash[IDX_HASH_SIZE];
14877};
14878
14879/*
14880** sqlite3expert object.
14881*/
14882struct sqlite3expert {
14883  int iSample;                    /* Percentage of tables to sample for stat1 */
14884  sqlite3 *db;                    /* User database */
14885  sqlite3 *dbm;                   /* In-memory db for this analysis */
14886  sqlite3 *dbv;                   /* Vtab schema for this analysis */
14887  IdxTable *pTable;               /* List of all IdxTable objects */
14888  IdxScan *pScan;                 /* List of scan objects */
14889  IdxWrite *pWrite;               /* List of write objects */
14890  IdxStatement *pStatement;       /* List of IdxStatement objects */
14891  int bRun;                       /* True once analysis has run */
14892  char **pzErrmsg;
14893  int rc;                         /* Error code from whereinfo hook */
14894  IdxHash hIdx;                   /* Hash containing all candidate indexes */
14895  char *zCandidates;              /* For EXPERT_REPORT_CANDIDATES */
14896};
14897
14898
14899/*
14900** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc(). 
14901** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
14902*/
14903static void *idxMalloc(int *pRc, i64 nByte){
14904  void *pRet;
14905  assert( *pRc==SQLITE_OK );
14906  assert( nByte>0 );
14907  pRet = sqlite3_malloc64(nByte);
14908  if( pRet ){
14909    memset(pRet, 0, nByte);
14910  }else{
14911    *pRc = SQLITE_NOMEM;
14912  }
14913  return pRet;
14914}
14915
14916/*
14917** Initialize an IdxHash hash table.
14918*/
14919static void idxHashInit(IdxHash *pHash){
14920  memset(pHash, 0, sizeof(IdxHash));
14921}
14922
14923/*
14924** Reset an IdxHash hash table.
14925*/
14926static void idxHashClear(IdxHash *pHash){
14927  int i;
14928  for(i=0; i<IDX_HASH_SIZE; i++){
14929    IdxHashEntry *pEntry;
14930    IdxHashEntry *pNext;
14931    for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
14932      pNext = pEntry->pHashNext;
14933      sqlite3_free(pEntry->zVal2);
14934      sqlite3_free(pEntry);
14935    }
14936  }
14937  memset(pHash, 0, sizeof(IdxHash));
14938}
14939
14940/*
14941** Return the index of the hash bucket that the string specified by the
14942** arguments to this function belongs.
14943*/
14944static int idxHashString(const char *z, int n){
14945  unsigned int ret = 0;
14946  int i;
14947  for(i=0; i<n; i++){
14948    ret += (ret<<3) + (unsigned char)(z[i]);
14949  }
14950  return (int)(ret % IDX_HASH_SIZE);
14951}
14952
14953/*
14954** If zKey is already present in the hash table, return non-zero and do
14955** nothing. Otherwise, add an entry with key zKey and payload string zVal to
14956** the hash table passed as the second argument. 
14957*/
14958static int idxHashAdd(
14959  int *pRc, 
14960  IdxHash *pHash, 
14961  const char *zKey,
14962  const char *zVal
14963){
14964  int nKey = STRLEN(zKey);
14965  int iHash = idxHashString(zKey, nKey);
14966  int nVal = (zVal ? STRLEN(zVal) : 0);
14967  IdxHashEntry *pEntry;
14968  assert( iHash>=0 );
14969  for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
14970    if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
14971      return 1;
14972    }
14973  }
14974  pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + (i64)nKey+1 + (i64)nVal+1);
14975  if( pEntry ){
14976    pEntry->zKey = (char*)&pEntry[1];
14977    memcpy(pEntry->zKey, zKey, nKey);
14978    if( zVal ){
14979      pEntry->zVal = &pEntry->zKey[nKey+1];
14980      memcpy(pEntry->zVal, zVal, nVal);
14981    }
14982    pEntry->pHashNext = pHash->aHash[iHash];
14983    pHash->aHash[iHash] = pEntry;
14984
14985    pEntry->pNext = pHash->pFirst;
14986    pHash->pFirst = pEntry;
14987  }
14988  return 0;
14989}
14990
14991/*
14992** If zKey/nKey is present in the hash table, return a pointer to the 
14993** hash-entry object.
14994*/
14995static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
14996  int iHash;
14997  IdxHashEntry *pEntry;
14998  if( nKey<0 ) nKey = STRLEN(zKey);
14999  iHash = idxHashString(zKey, nKey);
15000  assert( iHash>=0 );
15001  for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
15002    if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
15003      return pEntry;
15004    }
15005  }
15006  return 0;
15007}
15008
15009/*
15010** If the hash table contains an entry with a key equal to the string
15011** passed as the final two arguments to this function, return a pointer
15012** to the payload string. Otherwise, if zKey/nKey is not present in the
15013** hash table, return NULL.
15014*/
15015static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
15016  IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
15017  if( pEntry ) return pEntry->zVal;
15018  return 0;
15019}
15020
15021/*
15022** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
15023** variable to point to a copy of nul-terminated string zColl.
15024*/
15025static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
15026  IdxConstraint *pNew;
15027  int nColl = STRLEN(zColl);
15028
15029  assert( *pRc==SQLITE_OK );
15030  pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
15031  if( pNew ){
15032    pNew->zColl = (char*)&pNew[1];
15033    memcpy(pNew->zColl, zColl, nColl+1);
15034  }
15035  return pNew;
15036}
15037
15038/*
15039** An error associated with database handle db has just occurred. Pass
15040** the error message to callback function xOut.
15041*/
15042static void idxDatabaseError(
15043  sqlite3 *db,                    /* Database handle */
15044  char **pzErrmsg                 /* Write error here */
15045){
15046  *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
15047}
15048
15049/*
15050** Prepare an SQL statement.
15051*/
15052static int idxPrepareStmt(
15053  sqlite3 *db,                    /* Database handle to compile against */
15054  sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
15055  char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
15056  const char *zSql                /* SQL statement to compile */
15057){
15058  int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
15059  if( rc!=SQLITE_OK ){
15060    *ppStmt = 0;
15061    idxDatabaseError(db, pzErrmsg);
15062  }
15063  return rc;
15064}
15065
15066/*
15067** Prepare an SQL statement using the results of a printf() formatting.
15068*/
15069static int idxPrintfPrepareStmt(
15070  sqlite3 *db,                    /* Database handle to compile against */
15071  sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
15072  char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
15073  const char *zFmt,               /* printf() format of SQL statement */
15074  ...                             /* Trailing printf() arguments */
15075){
15076  va_list ap;
15077  int rc;
15078  char *zSql;
15079  va_start(ap, zFmt);
15080  zSql = sqlite3_vmprintf(zFmt, ap);
15081  if( zSql==0 ){
15082    rc = SQLITE_NOMEM;
15083  }else{
15084    rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
15085    sqlite3_free(zSql);
15086  }
15087  va_end(ap);
15088  return rc;
15089}
15090
15091
15092/*************************************************************************
15093** Beginning of virtual table implementation.
15094*/
15095typedef struct ExpertVtab ExpertVtab;
15096struct ExpertVtab {
15097  sqlite3_vtab base;
15098  IdxTable *pTab;
15099  sqlite3expert *pExpert;
15100};
15101
15102typedef struct ExpertCsr ExpertCsr;
15103struct ExpertCsr {
15104  sqlite3_vtab_cursor base;
15105  sqlite3_stmt *pData;
15106};
15107
15108static char *expertDequote(const char *zIn){
15109  i64 n = STRLEN(zIn);
15110  char *zRet = sqlite3_malloc64(n);
15111
15112  assert( zIn[0]=='\'' );
15113  assert( zIn[n-1]=='\'' );
15114
15115  if( zRet ){
15116    i64 iOut = 0;
15117    i64 iIn = 0;
15118    for(iIn=1; iIn<(n-1); iIn++){
15119      if( zIn[iIn]=='\'' ){
15120        assert( zIn[iIn+1]=='\'' );
15121        iIn++;
15122      }
15123      zRet[iOut++] = zIn[iIn];
15124    }
15125    zRet[iOut] = '\0';
15126  }
15127
15128  return zRet;
15129}
15130
15131/* 
15132** This function is the implementation of both the xConnect and xCreate
15133** methods of the r-tree virtual table.
15134**
15135**   argv[0]   -> module name
15136**   argv[1]   -> database name
15137**   argv[2]   -> table name
15138**   argv[...] -> column names...
15139*/
15140static int expertConnect(
15141  sqlite3 *db,
15142  void *pAux,
15143  int argc, const char *const*argv,
15144  sqlite3_vtab **ppVtab,
15145  char **pzErr
15146){
15147  sqlite3expert *pExpert = (sqlite3expert*)pAux;
15148  ExpertVtab *p = 0;
15149  int rc;
15150
15151  if( argc!=4 ){
15152    *pzErr = sqlite3_mprintf("internal error!");
15153    rc = SQLITE_ERROR;
15154  }else{
15155    char *zCreateTable = expertDequote(argv[3]);
15156    if( zCreateTable ){
15157      rc = sqlite3_declare_vtab(db, zCreateTable);
15158      if( rc==SQLITE_OK ){
15159        p = idxMalloc(&rc, sizeof(ExpertVtab));
15160      }
15161      if( rc==SQLITE_OK ){
15162        p->pExpert = pExpert;
15163        p->pTab = pExpert->pTable;
15164        assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
15165      }
15166      sqlite3_free(zCreateTable);
15167    }else{
15168      rc = SQLITE_NOMEM;
15169    }
15170  }
15171
15172  *ppVtab = (sqlite3_vtab*)p;
15173  return rc;
15174}
15175
15176static int expertDisconnect(sqlite3_vtab *pVtab){
15177  ExpertVtab *p = (ExpertVtab*)pVtab;
15178  sqlite3_free(p);
15179  return SQLITE_OK;
15180}
15181
15182static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
15183  ExpertVtab *p = (ExpertVtab*)pVtab;
15184  int rc = SQLITE_OK;
15185  int n = 0;
15186  IdxScan *pScan;
15187  const int opmask = 
15188    SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
15189    SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
15190    SQLITE_INDEX_CONSTRAINT_LE;
15191
15192  pScan = idxMalloc(&rc, sizeof(IdxScan));
15193  if( pScan ){
15194    int i;
15195
15196    /* Link the new scan object into the list */
15197    pScan->pTab = p->pTab;
15198    pScan->pNextScan = p->pExpert->pScan;
15199    p->pExpert->pScan = pScan;
15200
15201    /* Add the constraints to the IdxScan object */
15202    for(i=0; i<pIdxInfo->nConstraint; i++){
15203      struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
15204      if( pCons->usable 
15205       && pCons->iColumn>=0 
15206       && p->pTab->aCol[pCons->iColumn].iPk==0
15207       && (pCons->op & opmask) 
15208      ){
15209        IdxConstraint *pNew;
15210        const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
15211        pNew = idxNewConstraint(&rc, zColl);
15212        if( pNew ){
15213          pNew->iCol = pCons->iColumn;
15214          if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
15215            pNew->pNext = pScan->pEq;
15216            pScan->pEq = pNew;
15217          }else{
15218            pNew->bRange = 1;
15219            pNew->pNext = pScan->pRange;
15220            pScan->pRange = pNew;
15221          }
15222        }
15223        n++;
15224        pIdxInfo->aConstraintUsage[i].argvIndex = n;
15225      }
15226    }
15227
15228    /* Add the ORDER BY to the IdxScan object */
15229    for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
15230      int iCol = pIdxInfo->aOrderBy[i].iColumn;
15231      if( iCol>=0 ){
15232        IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
15233        if( pNew ){
15234          pNew->iCol = iCol;
15235          pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
15236          pNew->pNext = pScan->pOrder;
15237          pNew->pLink = pScan->pOrder;
15238          pScan->pOrder = pNew;
15239          n++;
15240        }
15241      }
15242    }
15243  }
15244
15245  pIdxInfo->estimatedCost = 1000000.0 / (n+1);
15246  return rc;
15247}
15248
15249static int expertUpdate(
15250  sqlite3_vtab *pVtab, 
15251  int nData, 
15252  sqlite3_value **azData, 
15253  sqlite_int64 *pRowid
15254){
15255  (void)pVtab;
15256  (void)nData;
15257  (void)azData;
15258  (void)pRowid;
15259  return SQLITE_OK;
15260}
15261
15262/* 
15263** Virtual table module xOpen method.
15264*/
15265static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
15266  int rc = SQLITE_OK;
15267  ExpertCsr *pCsr;
15268  (void)pVTab;
15269  pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
15270  *ppCursor = (sqlite3_vtab_cursor*)pCsr;
15271  return rc;
15272}
15273
15274/* 
15275** Virtual table module xClose method.
15276*/
15277static int expertClose(sqlite3_vtab_cursor *cur){
15278  ExpertCsr *pCsr = (ExpertCsr*)cur;
15279  sqlite3_finalize(pCsr->pData);
15280  sqlite3_free(pCsr);
15281  return SQLITE_OK;
15282}
15283
15284/*
15285** Virtual table module xEof method.
15286**
15287** Return non-zero if the cursor does not currently point to a valid 
15288** record (i.e if the scan has finished), or zero otherwise.
15289*/
15290static int expertEof(sqlite3_vtab_cursor *cur){
15291  ExpertCsr *pCsr = (ExpertCsr*)cur;
15292  return pCsr->pData==0;
15293}
15294
15295/* 
15296** Virtual table module xNext method.
15297*/
15298static int expertNext(sqlite3_vtab_cursor *cur){
15299  ExpertCsr *pCsr = (ExpertCsr*)cur;
15300  int rc = SQLITE_OK;
15301
15302  assert( pCsr->pData );
15303  rc = sqlite3_step(pCsr->pData);
15304  if( rc!=SQLITE_ROW ){
15305    rc = sqlite3_finalize(pCsr->pData);
15306    pCsr->pData = 0;
15307  }else{
15308    rc = SQLITE_OK;
15309  }
15310
15311  return rc;
15312}
15313
15314/* 
15315** Virtual table module xRowid method.
15316*/
15317static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
15318  (void)cur;
15319  *pRowid = 0;
15320  return SQLITE_OK;
15321}
15322
15323/* 
15324** Virtual table module xColumn method.
15325*/
15326static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
15327  ExpertCsr *pCsr = (ExpertCsr*)cur;
15328  sqlite3_value *pVal;
15329  pVal = sqlite3_column_value(pCsr->pData, i);
15330  if( pVal ){
15331    sqlite3_result_value(ctx, pVal);
15332  }
15333  return SQLITE_OK;
15334}
15335
15336/* 
15337** Virtual table module xFilter method.
15338*/
15339static int expertFilter(
15340  sqlite3_vtab_cursor *cur, 
15341  int idxNum, const char *idxStr,
15342  int argc, sqlite3_value **argv
15343){
15344  ExpertCsr *pCsr = (ExpertCsr*)cur;
15345  ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
15346  sqlite3expert *pExpert = pVtab->pExpert;
15347  int rc;
15348
15349  (void)idxNum;
15350  (void)idxStr;
15351  (void)argc;
15352  (void)argv;
15353  rc = sqlite3_finalize(pCsr->pData);
15354  pCsr->pData = 0;
15355  if( rc==SQLITE_OK ){
15356    rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
15357        "SELECT * FROM main.%Q WHERE sqlite_expert_sample()", pVtab->pTab->zName
15358    );
15359  }
15360
15361  if( rc==SQLITE_OK ){
15362    rc = expertNext(cur);
15363  }
15364  return rc;
15365}
15366
15367static int idxRegisterVtab(sqlite3expert *p){
15368  static sqlite3_module expertModule = {
15369    2,                            /* iVersion */
15370    expertConnect,                /* xCreate - create a table */
15371    expertConnect,                /* xConnect - connect to an existing table */
15372    expertBestIndex,              /* xBestIndex - Determine search strategy */
15373    expertDisconnect,             /* xDisconnect - Disconnect from a table */
15374    expertDisconnect,             /* xDestroy - Drop a table */
15375    expertOpen,                   /* xOpen - open a cursor */
15376    expertClose,                  /* xClose - close a cursor */
15377    expertFilter,                 /* xFilter - configure scan constraints */
15378    expertNext,                   /* xNext - advance a cursor */
15379    expertEof,                    /* xEof */
15380    expertColumn,                 /* xColumn - read data */
15381    expertRowid,                  /* xRowid - read data */
15382    expertUpdate,                 /* xUpdate - write data */
15383    0,                            /* xBegin - begin transaction */
15384    0,                            /* xSync - sync transaction */
15385    0,                            /* xCommit - commit transaction */
15386    0,                            /* xRollback - rollback transaction */
15387    0,                            /* xFindFunction - function overloading */
15388    0,                            /* xRename - rename the table */
15389    0,                            /* xSavepoint */
15390    0,                            /* xRelease */
15391    0,                            /* xRollbackTo */
15392    0,                            /* xShadowName */
15393    0,                            /* xIntegrity */
15394  };
15395
15396  return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
15397}
15398/*
15399** End of virtual table implementation.
15400*************************************************************************/
15401/*
15402** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
15403** is called, set it to the return value of sqlite3_finalize() before
15404** returning. Otherwise, discard the sqlite3_finalize() return value.
15405*/
15406static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
15407  int rc = sqlite3_finalize(pStmt);
15408  if( *pRc==SQLITE_OK ) *pRc = rc;
15409}
15410
15411/*
15412** Attempt to allocate an IdxTable structure corresponding to table zTab
15413** in the main database of connection db. If successful, set (*ppOut) to
15414** point to the new object and return SQLITE_OK. Otherwise, return an
15415** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
15416** set to point to an error string.
15417**
15418** It is the responsibility of the caller to eventually free either the
15419** IdxTable object or error message using sqlite3_free().
15420*/
15421static int idxGetTableInfo(
15422  sqlite3 *db,                    /* Database connection to read details from */
15423  const char *zTab,               /* Table name */
15424  IdxTable **ppOut,               /* OUT: New object (if successful) */
15425  char **pzErrmsg                 /* OUT: Error message (if not) */
15426){
15427  sqlite3_stmt *p1 = 0;
15428  int nCol = 0;
15429  int nTab;
15430  i64 nByte;
15431  IdxTable *pNew = 0;
15432  int rc, rc2;
15433  char *pCsr = 0;
15434  int nPk = 0;
15435
15436  *ppOut = 0;
15437  if( zTab==0 ) return SQLITE_ERROR;
15438  nTab = STRLEN(zTab);
15439  nByte = sizeof(IdxTable) + nTab + 1;
15440  rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab);
15441  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
15442    const char *zCol = (const char*)sqlite3_column_text(p1, 1);
15443    const char *zColSeq = 0;
15444    if( zCol==0 ){
15445      rc = SQLITE_ERROR;
15446      break;
15447    }
15448    nByte += 1 + STRLEN(zCol);
15449    rc = sqlite3_table_column_metadata(
15450        db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0
15451    );
15452    if( zColSeq==0 ) zColSeq = "binary";
15453    nByte += 1 + STRLEN(zColSeq);
15454    nCol++;
15455    nPk += (sqlite3_column_int(p1, 5)>0);
15456  }
15457  rc2 = sqlite3_reset(p1);
15458  if( rc==SQLITE_OK ) rc = rc2;
15459
15460  nByte += sizeof(IdxColumn) * nCol;
15461  if( rc==SQLITE_OK ){
15462    pNew = idxMalloc(&rc, nByte);
15463  }
15464  if( rc==SQLITE_OK ){
15465    pNew->aCol = (IdxColumn*)&pNew[1];
15466    pNew->nCol = nCol;
15467    pCsr = (char*)&pNew->aCol[nCol];
15468  }
15469
15470  nCol = 0;
15471  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
15472    const char *zCol = (const char*)sqlite3_column_text(p1, 1);
15473    const char *zColSeq = 0;
15474    int nCopy;
15475    if( zCol==0 ) continue;
15476    nCopy = STRLEN(zCol) + 1;
15477    pNew->aCol[nCol].zName = pCsr;
15478    pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
15479    memcpy(pCsr, zCol, nCopy);
15480    pCsr += nCopy;
15481
15482    rc = sqlite3_table_column_metadata(
15483        db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0
15484    );
15485    if( rc==SQLITE_OK ){
15486      if( zColSeq==0 ) zColSeq = "binary";
15487      nCopy = STRLEN(zColSeq) + 1;
15488      pNew->aCol[nCol].zColl = pCsr;
15489      memcpy(pCsr, zColSeq, nCopy);
15490      pCsr += nCopy;
15491    }
15492
15493    nCol++;
15494  }
15495  idxFinalize(&rc, p1);
15496
15497  if( rc!=SQLITE_OK ){
15498    sqlite3_free(pNew);
15499    pNew = 0;
15500  }else if( ALWAYS(pNew!=0) ){
15501    pNew->zName = pCsr;
15502    if( ALWAYS(pNew->zName!=0) ) memcpy(pNew->zName, zTab, nTab+1);
15503  }
15504
15505  *ppOut = pNew;
15506  return rc;
15507}
15508
15509/*
15510** This function is a no-op if *pRc is set to anything other than 
15511** SQLITE_OK when it is called.
15512**
15513** If *pRc is initially set to SQLITE_OK, then the text specified by
15514** the printf() style arguments is appended to zIn and the result returned
15515** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
15516** zIn before returning.
15517*/
15518static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
15519  va_list ap;
15520  char *zAppend = 0;
15521  char *zRet = 0;
15522  i64 nIn = zIn ? STRLEN(zIn) : 0;
15523  i64 nAppend = 0;
15524  va_start(ap, zFmt);
15525  if( *pRc==SQLITE_OK ){
15526    zAppend = sqlite3_vmprintf(zFmt, ap);
15527    if( zAppend ){
15528      nAppend = STRLEN(zAppend);
15529      zRet = (char*)sqlite3_malloc64(nIn + nAppend + 1);
15530    }
15531    if( zAppend && zRet ){
15532      if( nIn ) memcpy(zRet, zIn, nIn);
15533      memcpy(&zRet[nIn], zAppend, nAppend+1);
15534    }else{
15535      sqlite3_free(zRet);
15536      zRet = 0;
15537      *pRc = SQLITE_NOMEM;
15538    }
15539    sqlite3_free(zAppend);
15540    sqlite3_free(zIn);
15541  }
15542  va_end(ap);
15543  return zRet;
15544}
15545
15546/*
15547** Return true if zId must be quoted in order to use it as an SQL
15548** identifier, or false otherwise.
15549*/
15550static int idxIdentifierRequiresQuotes(const char *zId){
15551  int i;
15552  int nId = STRLEN(zId);
15553  
15554  if( sqlite3_keyword_check(zId, nId) ) return 1;
15555
15556  for(i=0; zId[i]; i++){
15557    if( !(zId[i]=='_')
15558     && !(zId[i]>='0' && zId[i]<='9')
15559     && !(zId[i]>='a' && zId[i]<='z')
15560     && !(zId[i]>='A' && zId[i]<='Z')
15561    ){
15562      return 1;
15563    }
15564  }
15565  return 0;
15566}
15567
15568/*
15569** This function appends an index column definition suitable for constraint
15570** pCons to the string passed as zIn and returns the result.
15571*/
15572static char *idxAppendColDefn(
15573  int *pRc,                       /* IN/OUT: Error code */
15574  char *zIn,                      /* Column defn accumulated so far */
15575  IdxTable *pTab,                 /* Table index will be created on */
15576  IdxConstraint *pCons
15577){
15578  char *zRet = zIn;
15579  IdxColumn *p = &pTab->aCol[pCons->iCol];
15580  if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
15581
15582  if( idxIdentifierRequiresQuotes(p->zName) ){
15583    zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
15584  }else{
15585    zRet = idxAppendText(pRc, zRet, "%s", p->zName);
15586  }
15587
15588  if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
15589    if( idxIdentifierRequiresQuotes(pCons->zColl) ){
15590      zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
15591    }else{
15592      zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
15593    }
15594  }
15595
15596  if( pCons->bDesc ){
15597    zRet = idxAppendText(pRc, zRet, " DESC");
15598  }
15599  return zRet;
15600}
15601
15602/*
15603** Search database dbm for an index compatible with the one idxCreateFromCons()
15604** would create from arguments pScan, pEq and pTail. If no error occurs and 
15605** such an index is found, return non-zero. Or, if no such index is found,
15606** return zero.
15607**
15608** If an error occurs, set *pRc to an SQLite error code and return zero.
15609*/
15610static int idxFindCompatible(
15611  int *pRc,                       /* OUT: Error code */
15612  sqlite3* dbm,                   /* Database to search */
15613  IdxScan *pScan,                 /* Scan for table to search for index on */
15614  IdxConstraint *pEq,             /* List of == constraints */
15615  IdxConstraint *pTail            /* List of range constraints */
15616){
15617  const char *zTbl = pScan->pTab->zName;
15618  sqlite3_stmt *pIdxList = 0;
15619  IdxConstraint *pIter;
15620  int nEq = 0;                    /* Number of elements in pEq */
15621  int rc;
15622
15623  /* Count the elements in list pEq */
15624  for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
15625
15626  rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
15627  while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
15628    int bMatch = 1;
15629    IdxConstraint *pT = pTail;
15630    sqlite3_stmt *pInfo = 0;
15631    const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
15632    if( zIdx==0 ) continue;
15633
15634    /* Zero the IdxConstraint.bFlag values in the pEq list */
15635    for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
15636
15637    rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
15638    while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
15639      int iIdx = sqlite3_column_int(pInfo, 0);
15640      int iCol = sqlite3_column_int(pInfo, 1);
15641      const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
15642
15643      if( iIdx<nEq ){
15644        for(pIter=pEq; pIter; pIter=pIter->pLink){
15645          if( pIter->bFlag ) continue;
15646          if( pIter->iCol!=iCol ) continue;
15647          if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
15648          pIter->bFlag = 1;
15649          break;
15650        }
15651        if( pIter==0 ){
15652          bMatch = 0;
15653          break;
15654        }
15655      }else{
15656        if( pT ){
15657          if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
15658            bMatch = 0;
15659            break;
15660          }
15661          pT = pT->pLink;
15662        }
15663      }
15664    }
15665    idxFinalize(&rc, pInfo);
15666
15667    if( rc==SQLITE_OK && bMatch ){
15668      sqlite3_finalize(pIdxList);
15669      return 1;
15670    }
15671  }
15672  idxFinalize(&rc, pIdxList);
15673
15674  *pRc = rc;
15675  return 0;
15676}
15677
15678/* Callback for sqlite3_exec() with query with leading count(*) column.
15679 * The first argument is expected to be an int*, referent to be incremented
15680 * if that leading column is not exactly '0'.
15681 */
15682static int countNonzeros(void* pCount, int nc,
15683                         char* azResults[], char* azColumns[]){
15684  (void)azColumns;  /* Suppress unused parameter warning */
15685  if( nc>0 && (azResults[0][0]!='0' || azResults[0][1]!=0) ){
15686    *((int *)pCount) += 1;
15687  }
15688  return 0;
15689}
15690
15691static int idxCreateFromCons(
15692  sqlite3expert *p,
15693  IdxScan *pScan,
15694  IdxConstraint *pEq, 
15695  IdxConstraint *pTail
15696){
15697  sqlite3 *dbm = p->dbm;
15698  int rc = SQLITE_OK;
15699  if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
15700    IdxTable *pTab = pScan->pTab;
15701    char *zCols = 0;
15702    char *zIdx = 0;
15703    IdxConstraint *pCons;
15704    unsigned int h = 0;
15705    const char *zFmt;
15706
15707    for(pCons=pEq; pCons; pCons=pCons->pLink){
15708      zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
15709    }
15710    for(pCons=pTail; pCons; pCons=pCons->pLink){
15711      zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
15712    }
15713
15714    if( rc==SQLITE_OK ){
15715      /* Hash the list of columns to come up with a name for the index */
15716      const char *zTable = pScan->pTab->zName;
15717      int quoteTable = idxIdentifierRequiresQuotes(zTable);
15718      char *zName = 0;          /* Index name */
15719      int collisions = 0;
15720      do{
15721        int i;
15722        char *zFind;
15723        for(i=0; zCols[i]; i++){
15724          h += ((h<<3) + zCols[i]);
15725        }
15726        sqlite3_free(zName);
15727        zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
15728        if( zName==0 ) break;
15729        /* Is is unique among table, view and index names? */
15730        zFmt = "SELECT count(*) FROM sqlite_schema WHERE name=%Q"
15731          " AND type in ('index','table','view')";
15732        zFind = sqlite3_mprintf(zFmt, zName);
15733        i = 0;
15734        rc = sqlite3_exec(dbm, zFind, countNonzeros, &i, 0);
15735        assert(rc==SQLITE_OK);
15736        sqlite3_free(zFind);
15737        if( i==0 ){
15738          collisions = 0;
15739          break;
15740        }
15741        ++collisions;
15742      }while( collisions<50 && zName!=0 );
15743      if( collisions ){
15744        /* This return means "Gave up trying to find a unique index name." */
15745        rc = SQLITE_BUSY_TIMEOUT;
15746      }else if( zName==0 ){
15747        rc = SQLITE_NOMEM;
15748      }else{
15749        if( quoteTable ){
15750          zFmt = "CREATE INDEX \"%w\" ON \"%w\"(%s)";
15751        }else{
15752          zFmt = "CREATE INDEX %s ON %s(%s)";
15753        }
15754        zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
15755        if( !zIdx ){
15756          rc = SQLITE_NOMEM;
15757        }else{
15758          rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
15759          if( rc!=SQLITE_OK ){
15760            rc = SQLITE_BUSY_TIMEOUT;
15761          }else{
15762            idxHashAdd(&rc, &p->hIdx, zName, zIdx);
15763          }
15764        }
15765        sqlite3_free(zName);
15766        sqlite3_free(zIdx);
15767      }
15768    }
15769
15770    sqlite3_free(zCols);
15771  }
15772  return rc;
15773}
15774
15775/*
15776** Return true if list pList (linked by IdxConstraint.pLink) contains
15777** a constraint compatible with *p. Otherwise return false.
15778*/
15779static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
15780  IdxConstraint *pCmp;
15781  for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
15782    if( p->iCol==pCmp->iCol ) return 1;
15783  }
15784  return 0;
15785}
15786
15787static int idxCreateFromWhere(
15788  sqlite3expert *p, 
15789  IdxScan *pScan,                 /* Create indexes for this scan */
15790  IdxConstraint *pTail            /* range/ORDER BY constraints for inclusion */
15791){
15792  IdxConstraint *p1 = 0;
15793  IdxConstraint *pCon;
15794  int rc;
15795
15796  /* Gather up all the == constraints. */
15797  for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
15798    if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
15799      pCon->pLink = p1;
15800      p1 = pCon;
15801    }
15802  }
15803
15804  /* Create an index using the == constraints collected above. And the
15805  ** range constraint/ORDER BY terms passed in by the caller, if any. */
15806  rc = idxCreateFromCons(p, pScan, p1, pTail);
15807
15808  /* If no range/ORDER BY passed by the caller, create a version of the
15809  ** index for each range constraint.  */
15810  if( pTail==0 ){
15811    for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
15812      assert( pCon->pLink==0 );
15813      if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
15814        rc = idxCreateFromCons(p, pScan, p1, pCon);
15815      }
15816    }
15817  }
15818
15819  return rc;
15820}
15821
15822/*
15823** Create candidate indexes in database [dbm] based on the data in 
15824** linked-list pScan.
15825*/
15826static int idxCreateCandidates(sqlite3expert *p){
15827  int rc = SQLITE_OK;
15828  IdxScan *pIter;
15829
15830  for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
15831    rc = idxCreateFromWhere(p, pIter, 0);
15832    if( rc==SQLITE_OK && pIter->pOrder ){
15833      rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
15834    }
15835  }
15836
15837  return rc;
15838}
15839
15840/*
15841** Free all elements of the linked list starting at pConstraint.
15842*/
15843static void idxConstraintFree(IdxConstraint *pConstraint){
15844  IdxConstraint *pNext;
15845  IdxConstraint *p;
15846
15847  for(p=pConstraint; p; p=pNext){
15848    pNext = p->pNext;
15849    sqlite3_free(p);
15850  }
15851}
15852
15853/*
15854** Free all elements of the linked list starting from pScan up until pLast
15855** (pLast is not freed).
15856*/
15857static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
15858  IdxScan *p;
15859  IdxScan *pNext;
15860  for(p=pScan; p!=pLast; p=pNext){
15861    pNext = p->pNextScan;
15862    idxConstraintFree(p->pOrder);
15863    idxConstraintFree(p->pEq);
15864    idxConstraintFree(p->pRange);
15865    sqlite3_free(p);
15866  }
15867}
15868
15869/*
15870** Free all elements of the linked list starting from pStatement up 
15871** until pLast (pLast is not freed).
15872*/
15873static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
15874  IdxStatement *p;
15875  IdxStatement *pNext;
15876  for(p=pStatement; p!=pLast; p=pNext){
15877    pNext = p->pNext;
15878    sqlite3_free(p->zEQP);
15879    sqlite3_free(p->zIdx);
15880    sqlite3_free(p);
15881  }
15882}
15883
15884/*
15885** Free the linked list of IdxTable objects starting at pTab.
15886*/
15887static void idxTableFree(IdxTable *pTab){
15888  IdxTable *pIter;
15889  IdxTable *pNext;
15890  for(pIter=pTab; pIter; pIter=pNext){
15891    pNext = pIter->pNext;
15892    sqlite3_free(pIter);
15893  }
15894}
15895
15896/*
15897** Free the linked list of IdxWrite objects starting at pTab.
15898*/
15899static void idxWriteFree(IdxWrite *pTab){
15900  IdxWrite *pIter;
15901  IdxWrite *pNext;
15902  for(pIter=pTab; pIter; pIter=pNext){
15903    pNext = pIter->pNext;
15904    sqlite3_free(pIter);
15905  }
15906}
15907
15908
15909
15910/*
15911** This function is called after candidate indexes have been created. It
15912** runs all the queries to see which indexes they prefer, and populates
15913** IdxStatement.zIdx and IdxStatement.zEQP with the results.
15914*/
15915static int idxFindIndexes(
15916  sqlite3expert *p,
15917  char **pzErr                         /* OUT: Error message (sqlite3_malloc) */
15918){
15919  IdxStatement *pStmt;
15920  sqlite3 *dbm = p->dbm;
15921  int rc = SQLITE_OK;
15922
15923  IdxHash hIdx;
15924  idxHashInit(&hIdx);
15925
15926  for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
15927    IdxHashEntry *pEntry;
15928    sqlite3_stmt *pExplain = 0;
15929    idxHashClear(&hIdx);
15930    rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
15931        "EXPLAIN QUERY PLAN %s", pStmt->zSql
15932    );
15933    while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
15934      /* int iId = sqlite3_column_int(pExplain, 0); */
15935      /* int iParent = sqlite3_column_int(pExplain, 1); */
15936      /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
15937      const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
15938      int nDetail;
15939      int i;
15940
15941      if( !zDetail ) continue;
15942      nDetail = STRLEN(zDetail);
15943
15944      for(i=0; i<nDetail; i++){
15945        const char *zIdx = 0;
15946        if( i+13<nDetail && memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
15947          zIdx = &zDetail[i+13];
15948        }else if( i+22<nDetail 
15949            && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0 
15950        ){
15951          zIdx = &zDetail[i+22];
15952        }
15953        if( zIdx ){
15954          const char *zSql;
15955          int nIdx = 0;
15956          while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
15957            nIdx++;
15958          }
15959          zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
15960          if( zSql ){
15961            idxHashAdd(&rc, &hIdx, zSql, 0);
15962            if( rc ) goto find_indexes_out;
15963          }
15964          break;
15965        }
15966      }
15967
15968      if( zDetail[0]!='-' ){
15969        pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
15970      }
15971    }
15972
15973    for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
15974      pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
15975    }
15976
15977    idxFinalize(&rc, pExplain);
15978  }
15979
15980 find_indexes_out:
15981  idxHashClear(&hIdx);
15982  return rc;
15983}
15984
15985static int idxAuthCallback(
15986  void *pCtx,
15987  int eOp,
15988  const char *z3,
15989  const char *z4,
15990  const char *zDb,
15991  const char *zTrigger
15992){
15993  int rc = SQLITE_OK;
15994  (void)z4;
15995  (void)zTrigger;
15996  if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
15997    if( sqlite3_stricmp(zDb, "main")==0 ){
15998      sqlite3expert *p = (sqlite3expert*)pCtx;
15999      IdxTable *pTab;
16000      for(pTab=p->pTable; pTab; pTab=pTab->pNext){
16001        if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
16002      }
16003      if( pTab ){
16004        IdxWrite *pWrite;
16005        for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
16006          if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
16007        }
16008        if( pWrite==0 ){
16009          pWrite = idxMalloc(&rc, sizeof(IdxWrite));
16010          if( rc==SQLITE_OK ){
16011            pWrite->pTab = pTab;
16012            pWrite->eOp = eOp;
16013            pWrite->pNext = p->pWrite;
16014            p->pWrite = pWrite;
16015          }
16016        }
16017      }
16018    }
16019  }
16020  return rc;
16021}
16022
16023static int idxProcessOneTrigger(
16024  sqlite3expert *p, 
16025  IdxWrite *pWrite, 
16026  char **pzErr
16027){
16028  static const char *zInt = UNIQUE_TABLE_NAME;
16029  static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
16030  IdxTable *pTab = pWrite->pTab;
16031  const char *zTab = pTab->zName;
16032  const char *zSql = 
16033    "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
16034    "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
16035    "ORDER BY type;";
16036  sqlite3_stmt *pSelect = 0;
16037  int rc = SQLITE_OK;
16038  char *zWrite = 0;
16039
16040  /* Create the table and its triggers in the temp schema */
16041  rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
16042  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
16043    const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
16044    if( zCreate==0 ) continue;
16045    rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
16046  }
16047  idxFinalize(&rc, pSelect);
16048
16049  /* Rename the table in the temp schema to zInt */
16050  if( rc==SQLITE_OK ){
16051    char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
16052    if( z==0 ){
16053      rc = SQLITE_NOMEM;
16054    }else{
16055      rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
16056      sqlite3_free(z);
16057    }
16058  }
16059
16060  switch( pWrite->eOp ){
16061    case SQLITE_INSERT: {
16062      int i;
16063      zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
16064      for(i=0; i<pTab->nCol; i++){
16065        zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
16066      }
16067      zWrite = idxAppendText(&rc, zWrite, ")");
16068      break;
16069    }
16070    case SQLITE_UPDATE: {
16071      int i;
16072      zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
16073      for(i=0; i<pTab->nCol; i++){
16074        zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ", 
16075            pTab->aCol[i].zName
16076        );
16077      }
16078      break;
16079    }
16080    default: {
16081      assert( pWrite->eOp==SQLITE_DELETE );
16082      if( rc==SQLITE_OK ){
16083        zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
16084        if( zWrite==0 ) rc = SQLITE_NOMEM;
16085      }
16086    }
16087  }
16088
16089  if( rc==SQLITE_OK ){
16090    sqlite3_stmt *pX = 0;
16091    rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
16092    idxFinalize(&rc, pX);
16093    if( rc!=SQLITE_OK ){
16094      idxDatabaseError(p->dbv, pzErr);
16095    }
16096  }
16097  sqlite3_free(zWrite);
16098
16099  if( rc==SQLITE_OK ){
16100    rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
16101  }
16102
16103  return rc;
16104}
16105
16106static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
16107  int rc = SQLITE_OK;
16108  IdxWrite *pEnd = 0;
16109  IdxWrite *pFirst = p->pWrite;
16110
16111  while( rc==SQLITE_OK && pFirst!=pEnd ){
16112    IdxWrite *pIter;
16113    for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
16114      rc = idxProcessOneTrigger(p, pIter, pzErr);
16115    }
16116    pEnd = pFirst;
16117    pFirst = p->pWrite;
16118  }
16119
16120  return rc;
16121}
16122
16123/*
16124** This function tests if the schema of the main database of database handle
16125** db contains an object named zTab. Assuming no error occurs, output parameter
16126** (*pbContains) is set to true if zTab exists, or false if it does not.
16127**
16128** Or, if an error occurs, an SQLite error code is returned. The final value
16129** of (*pbContains) is undefined in this case.
16130*/
16131static int expertDbContainsObject(
16132  sqlite3 *db, 
16133  const char *zTab, 
16134  int *pbContains                 /* OUT: True if object exists */
16135){
16136  const char *zSql = "SELECT 1 FROM sqlite_schema WHERE name = ?";
16137  sqlite3_stmt *pSql = 0;
16138  int rc = SQLITE_OK;
16139  int ret = 0;
16140
16141  rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
16142  if( rc==SQLITE_OK ){
16143    sqlite3_bind_text(pSql, 1, zTab, -1, SQLITE_STATIC);
16144    if( SQLITE_ROW==sqlite3_step(pSql) ){
16145      ret = 1;
16146    }
16147    rc = sqlite3_finalize(pSql);
16148  }
16149
16150  *pbContains = ret;
16151  return rc;
16152}
16153
16154/*
16155** Execute SQL command zSql using database handle db. If no error occurs,
16156** set (*pzErr) to NULL and return SQLITE_OK. 
16157**
16158** If an error does occur, return an SQLite error code and set (*pzErr) to
16159** point to a buffer containing an English language error message. Except,
16160** if the error message begins with "no such module:", then ignore the
16161** error and return as if the SQL statement had succeeded.
16162**
16163** This is used to copy as much of the database schema as possible while 
16164** ignoring any errors related to missing virtual table modules.
16165*/
16166static int expertSchemaSql(sqlite3 *db, const char *zSql, char **pzErr){
16167  int rc = SQLITE_OK;
16168  char *zErr = 0;
16169
16170  rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
16171  if( rc!=SQLITE_OK && zErr ){
16172    int nErr = STRLEN(zErr);
16173    if( nErr>=15 && memcmp(zErr, "no such module:", 15)==0 ){
16174      sqlite3_free(zErr);
16175      rc = SQLITE_OK;
16176      zErr = 0;
16177    }
16178  }
16179
16180  *pzErr = zErr;
16181  return rc;
16182}
16183
16184static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
16185  int rc = idxRegisterVtab(p);
16186  sqlite3_stmt *pSchema = 0;
16187
16188  /* For each table in the main db schema:
16189  **
16190  **   1) Add an entry to the p->pTable list, and
16191  **   2) Create the equivalent virtual table in dbv.
16192  */
16193  rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
16194      "SELECT type, name, sql, 1, "
16195      "       substr(sql,1,14)=='create virtual' COLLATE nocase "
16196      "FROM sqlite_schema "
16197      "WHERE type IN ('table','view') AND "
16198      "      substr(name,1,7)!='sqlite_' COLLATE nocase "
16199      " UNION ALL "
16200      "SELECT type, name, sql, 2, 0 FROM sqlite_schema "
16201      "WHERE type = 'trigger'"
16202      "  AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
16203      "ORDER BY 4, 5 DESC, 1"
16204  );
16205  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
16206    const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
16207    const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
16208    const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
16209    int bVirtual = sqlite3_column_int(pSchema, 4);
16210    int bExists = 0;
16211
16212    if( zType==0 || zName==0 ) continue;
16213    rc = expertDbContainsObject(p->dbv, zName, &bExists);
16214    if( rc || bExists ) continue;
16215
16216    if( zType[0]=='v' || zType[1]=='r' || bVirtual ){
16217      /* A view. Or a trigger on a view. */
16218      if( zSql ) rc = expertSchemaSql(p->dbv, zSql, pzErrmsg);
16219    }else{
16220      IdxTable *pTab;
16221      rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
16222      if( rc==SQLITE_OK && ALWAYS(pTab!=0) ){
16223        int i;
16224        char *zInner = 0;
16225        char *zOuter = 0;
16226        pTab->pNext = p->pTable;
16227        p->pTable = pTab;
16228
16229        /* The statement the vtab will pass to sqlite3_declare_vtab() */
16230        zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
16231        for(i=0; i<pTab->nCol; i++){
16232          zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s", 
16233              (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
16234          );
16235        }
16236        zInner = idxAppendText(&rc, zInner, ")");
16237
16238        /* The CVT statement to create the vtab */
16239        zOuter = idxAppendText(&rc, 0, 
16240            "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
16241        );
16242        if( rc==SQLITE_OK ){
16243          rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
16244        }
16245        sqlite3_free(zInner);
16246        sqlite3_free(zOuter);
16247      }
16248    }
16249  }
16250  idxFinalize(&rc, pSchema);
16251  return rc;
16252}
16253
16254struct IdxSampleCtx {
16255  int iTarget;
16256  double target;                  /* Target nRet/nRow value */
16257  double nRow;                    /* Number of rows seen */
16258  double nRet;                    /* Number of rows returned */
16259};
16260
16261static void idxSampleFunc(
16262  sqlite3_context *pCtx,
16263  int argc,
16264  sqlite3_value **argv
16265){
16266  struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
16267  int bRet;
16268
16269  (void)argv;
16270  assert( argc==0 );
16271  if( p->nRow==0.0 ){
16272    bRet = 1;
16273  }else{
16274    bRet = (p->nRet / p->nRow) <= p->target;
16275    if( bRet==0 ){
16276      unsigned short rnd;
16277      sqlite3_randomness(2, (void*)&rnd);
16278      bRet = ((int)rnd % 100) <= p->iTarget;
16279    }
16280  }
16281
16282  sqlite3_result_int(pCtx, bRet);
16283  p->nRow += 1.0;
16284  p->nRet += (double)bRet;
16285}
16286
16287struct IdxRemCtx {
16288  int nSlot;
16289  struct IdxRemSlot {
16290    int eType;                    /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
16291    i64 iVal;                     /* SQLITE_INTEGER value */
16292    double rVal;                  /* SQLITE_FLOAT value */
16293    i64 nByte;                    /* Bytes of space allocated at z */
16294    i64 n;                        /* Size of buffer z */
16295    char *z;                      /* SQLITE_TEXT/BLOB value */
16296  } aSlot[1];
16297};
16298
16299/*
16300** Implementation of scalar function sqlite_expert_rem().
16301*/
16302static void idxRemFunc(
16303  sqlite3_context *pCtx,
16304  int argc,
16305  sqlite3_value **argv
16306){
16307  struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
16308  struct IdxRemSlot *pSlot;
16309  int iSlot;
16310  assert( argc==2 );
16311
16312  iSlot = sqlite3_value_int(argv[0]);
16313  assert( iSlot<p->nSlot );
16314  pSlot = &p->aSlot[iSlot];
16315
16316  switch( pSlot->eType ){
16317    case SQLITE_NULL:
16318      /* no-op */
16319      break;
16320
16321    case SQLITE_INTEGER:
16322      sqlite3_result_int64(pCtx, pSlot->iVal);
16323      break;
16324
16325    case SQLITE_FLOAT:
16326      sqlite3_result_double(pCtx, pSlot->rVal);
16327      break;
16328
16329    case SQLITE_BLOB:
16330      assert( pSlot->n <= 0x7fffffff );
16331      sqlite3_result_blob(pCtx, pSlot->z, (int)pSlot->n, SQLITE_TRANSIENT);
16332      break;
16333
16334    case SQLITE_TEXT:
16335      assert( pSlot->n <= 0x7fffffff );
16336      sqlite3_result_text(pCtx, pSlot->z, (int)pSlot->n, SQLITE_TRANSIENT);
16337      break;
16338  }
16339
16340  pSlot->eType = sqlite3_value_type(argv[1]);
16341  switch( pSlot->eType ){
16342    case SQLITE_NULL:
16343      /* no-op */
16344      break;
16345
16346    case SQLITE_INTEGER:
16347      pSlot->iVal = sqlite3_value_int64(argv[1]);
16348      break;
16349
16350    case SQLITE_FLOAT:
16351      pSlot->rVal = sqlite3_value_double(argv[1]);
16352      break;
16353
16354    case SQLITE_BLOB:
16355    case SQLITE_TEXT: {
16356      i64 nByte = sqlite3_value_bytes(argv[1]);
16357      const void *pData = 0;
16358      if( nByte>pSlot->nByte ){
16359        char *zNew = (char*)sqlite3_realloc64(pSlot->z, nByte*2);
16360        if( zNew==0 ){
16361          sqlite3_result_error_nomem(pCtx);
16362          return;
16363        }
16364        pSlot->nByte = nByte*2;
16365        pSlot->z = zNew;
16366      }
16367      pSlot->n = nByte;
16368      if( pSlot->eType==SQLITE_BLOB ){
16369        pData = sqlite3_value_blob(argv[1]);
16370        if( pData ) memcpy(pSlot->z, pData, nByte);
16371      }else{
16372        pData = sqlite3_value_text(argv[1]);
16373        memcpy(pSlot->z, pData, nByte);
16374      }
16375      break;
16376    }
16377  }
16378}
16379
16380static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
16381  int rc = SQLITE_OK;
16382  const char *zMax = 
16383    "SELECT max(i.seqno) FROM "
16384    "  sqlite_schema AS s, "
16385    "  pragma_index_list(s.name) AS l, "
16386    "  pragma_index_info(l.name) AS i "
16387    "WHERE s.type = 'table'";
16388  sqlite3_stmt *pMax = 0;
16389
16390  *pnMax = 0;
16391  rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
16392  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
16393    *pnMax = sqlite3_column_int(pMax, 0) + 1;
16394  }
16395  idxFinalize(&rc, pMax);
16396
16397  return rc;
16398}
16399
16400static int idxPopulateOneStat1(
16401  sqlite3expert *p,
16402  sqlite3_stmt *pIndexXInfo,
16403  sqlite3_stmt *pWriteStat,
16404  const char *zTab,
16405  const char *zIdx,
16406  char **pzErr
16407){
16408  char *zCols = 0;
16409  char *zOrder = 0;
16410  char *zQuery = 0;
16411  int nCol = 0;
16412  int i;
16413  sqlite3_stmt *pQuery = 0;
16414  i64 *aStat = 0;
16415  int rc = SQLITE_OK;
16416
16417  assert( p->iSample>0 );
16418
16419  /* Formulate the query text */
16420  sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
16421  while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
16422    const char *zComma = zCols==0 ? "" : ", ";
16423    const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
16424    const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
16425    if( zName==0 ){
16426      /* This index contains an expression. Ignore it. */
16427      sqlite3_free(zCols);
16428      sqlite3_free(zOrder);
16429      return sqlite3_reset(pIndexXInfo);
16430    }
16431    zCols = idxAppendText(&rc, zCols, 
16432        "%sx.%Q IS sqlite_expert_rem(%d, x.%Q) COLLATE %s", 
16433        zComma, zName, nCol, zName, zColl
16434    );
16435    zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
16436  }
16437  sqlite3_reset(pIndexXInfo);
16438  if( rc==SQLITE_OK ){
16439    if( p->iSample==100 ){
16440      zQuery = sqlite3_mprintf(
16441          "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
16442      );
16443    }else{
16444      zQuery = sqlite3_mprintf(
16445          "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
16446      );
16447    }
16448  }
16449  sqlite3_free(zCols);
16450  sqlite3_free(zOrder);
16451
16452  /* Formulate the query text */
16453  if( rc==SQLITE_OK ){
16454    sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
16455    rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
16456  }
16457  sqlite3_free(zQuery);
16458
16459  if( rc==SQLITE_OK ){
16460    aStat = (i64*)idxMalloc(&rc, sizeof(i64)*(nCol+1));
16461  }
16462  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
16463    IdxHashEntry *pEntry;
16464    char *zStat = 0;
16465    for(i=0; i<=nCol; i++) aStat[i] = 1;
16466    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
16467      aStat[0]++;
16468      for(i=0; i<nCol; i++){
16469        if( sqlite3_column_int(pQuery, i)==0 ) break;
16470      }
16471      for(/*no-op*/; i<nCol; i++){
16472        aStat[i+1]++;
16473      }
16474    }
16475
16476    if( rc==SQLITE_OK ){
16477      i64 s0 = aStat[0];
16478      zStat = sqlite3_mprintf("%lld", s0);
16479      if( zStat==0 ) rc = SQLITE_NOMEM;
16480      for(i=1; rc==SQLITE_OK && i<=nCol; i++){
16481        zStat = idxAppendText(&rc, zStat, " %lld", (s0+aStat[i]/2) / aStat[i]);
16482      }
16483    }
16484
16485    if( rc==SQLITE_OK ){
16486      sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
16487      sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
16488      sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
16489      sqlite3_step(pWriteStat);
16490      rc = sqlite3_reset(pWriteStat);
16491    }
16492
16493    pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
16494    if( pEntry ){
16495      assert( pEntry->zVal2==0 );
16496      pEntry->zVal2 = zStat;
16497    }else{
16498      sqlite3_free(zStat);
16499    }
16500  }
16501  sqlite3_free(aStat);
16502  idxFinalize(&rc, pQuery);
16503
16504  return rc;
16505}
16506
16507static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
16508  int rc;
16509  char *zSql;
16510
16511  rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
16512  if( rc!=SQLITE_OK ) return rc;
16513
16514  zSql = sqlite3_mprintf(
16515      "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
16516  );
16517  if( zSql==0 ) return SQLITE_NOMEM;
16518  rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
16519  sqlite3_free(zSql);
16520
16521  return rc;
16522}
16523
16524/*
16525** This function is called as part of sqlite3_expert_analyze(). Candidate
16526** indexes have already been created in database sqlite3expert.dbm, this
16527** function populates sqlite_stat1 table in the same database.
16528**
16529** The stat1 data is generated by querying the 
16530*/
16531static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
16532  int rc = SQLITE_OK;
16533  int nMax =0;
16534  struct IdxRemCtx *pCtx = 0;
16535  struct IdxSampleCtx samplectx; 
16536  int i;
16537  i64 iPrev = -100000;
16538  sqlite3_stmt *pAllIndex = 0;
16539  sqlite3_stmt *pIndexXInfo = 0;
16540  sqlite3_stmt *pWrite = 0;
16541
16542  const char *zAllIndex =
16543    "SELECT s.rowid, s.name, l.name FROM "
16544    "  sqlite_schema AS s, "
16545    "  pragma_index_list(s.name) AS l "
16546    "WHERE s.type = 'table'";
16547  const char *zIndexXInfo = 
16548    "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
16549  const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
16550
16551  /* If iSample==0, no sqlite_stat1 data is required. */
16552  if( p->iSample==0 ) return SQLITE_OK;
16553
16554  rc = idxLargestIndex(p->dbm, &nMax, pzErr);
16555  if( nMax<=0 || rc!=SQLITE_OK ) return rc;
16556
16557  rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
16558
16559  if( rc==SQLITE_OK ){
16560    i64 nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
16561    pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
16562  }
16563
16564  if( rc==SQLITE_OK ){
16565    sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
16566    rc = sqlite3_create_function(dbrem, "sqlite_expert_rem", 
16567        2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
16568    );
16569  }
16570  if( rc==SQLITE_OK ){
16571    rc = sqlite3_create_function(p->db, "sqlite_expert_sample", 
16572        0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
16573    );
16574  }
16575
16576  if( rc==SQLITE_OK ){
16577    pCtx->nSlot = (i64)nMax+1;
16578    rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
16579  }
16580  if( rc==SQLITE_OK ){
16581    rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
16582  }
16583  if( rc==SQLITE_OK ){
16584    rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
16585  }
16586
16587  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
16588    i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
16589    const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
16590    const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
16591    if( zTab==0 || zIdx==0 ) continue;
16592    if( p->iSample<100 && iPrev!=iRowid ){
16593      samplectx.target = (double)p->iSample / 100.0;
16594      samplectx.iTarget = p->iSample;
16595      samplectx.nRow = 0.0;
16596      samplectx.nRet = 0.0;
16597      rc = idxBuildSampleTable(p, zTab);
16598      if( rc!=SQLITE_OK ) break;
16599    }
16600    rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
16601    iPrev = iRowid;
16602  }
16603  if( rc==SQLITE_OK && p->iSample<100 ){
16604    rc = sqlite3_exec(p->dbv, 
16605        "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
16606    );
16607  }
16608
16609  idxFinalize(&rc, pAllIndex);
16610  idxFinalize(&rc, pIndexXInfo);
16611  idxFinalize(&rc, pWrite);
16612
16613  if( pCtx ){
16614    for(i=0; i<pCtx->nSlot; i++){
16615      sqlite3_free(pCtx->aSlot[i].z);
16616    }
16617    sqlite3_free(pCtx);
16618  }
16619
16620  if( rc==SQLITE_OK ){
16621    rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0);
16622  }
16623
16624  sqlite3_create_function(p->db, "sqlite_expert_rem", 2, SQLITE_UTF8, 0,0,0,0);
16625  sqlite3_create_function(p->db, "sqlite_expert_sample", 0,SQLITE_UTF8,0,0,0,0);
16626
16627  sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
16628  return rc;
16629}
16630
16631/*
16632** Define and possibly pretend to use a useless collation sequence.
16633** This pretense allows expert to accept SQL using custom collations.
16634*/
16635int dummyCompare(void *up1, int up2, const void *up3, int up4, const void *up5){
16636  (void)up1;
16637  (void)up2;
16638  (void)up3;
16639  (void)up4;
16640  (void)up5;
16641  assert(0); /* VDBE should never be run. */
16642  return 0;
16643}
16644/* And a callback to register above upon actual need */
16645void useDummyCS(void *up1, sqlite3 *db, int etr, const char *zName){
16646  (void)up1;
16647  sqlite3_create_collation_v2(db, zName, etr, 0, dummyCompare, 0);
16648}
16649
16650#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) \
16651  && !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
16652/*
16653** dummy functions for no-op implementation of UDFs during expert's work
16654*/
16655void dummyUDF(sqlite3_context *up1, int up2, sqlite3_value **up3){
16656  (void)up1;
16657  (void)up2;
16658  (void)up3;
16659  assert(0); /* VDBE should never be run. */
16660}
16661void dummyUDFvalue(sqlite3_context *up1){
16662  (void)up1;
16663  assert(0); /* VDBE should never be run. */
16664}
16665
16666/*
16667** Register UDFs from user database with another.
16668*/
16669int registerUDFs(sqlite3 *dbSrc, sqlite3 *dbDst){
16670  sqlite3_stmt *pStmt;
16671  int rc = sqlite3_prepare_v2(dbSrc,
16672            "SELECT name,type,enc,narg,flags "
16673            "FROM pragma_function_list() "
16674            "WHERE builtin==0", -1, &pStmt, 0);
16675  if( rc==SQLITE_OK ){
16676    while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
16677      int nargs = sqlite3_column_int(pStmt,3);
16678      int flags = sqlite3_column_int(pStmt,4);
16679      const char *name = (char*)sqlite3_column_text(pStmt,0);
16680      const char *type = (char*)sqlite3_column_text(pStmt,1);
16681      const char *enc = (char*)sqlite3_column_text(pStmt,2);
16682      if( name==0 || type==0 || enc==0 ){
16683        /* no-op.  Only happens on OOM */
16684      }else{
16685        int ienc = SQLITE_UTF8;
16686        int rcf = SQLITE_ERROR;
16687        if( strcmp(enc,"utf16le")==0 ) ienc = SQLITE_UTF16LE;
16688        else if( strcmp(enc,"utf16be")==0 ) ienc = SQLITE_UTF16BE;
16689        ienc |= (flags & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY));
16690        if( strcmp(type,"w")==0 ){
16691          rcf = sqlite3_create_window_function(dbDst,name,nargs,ienc,0,
16692                                               dummyUDF,dummyUDFvalue,0,0,0);
16693        }else if( strcmp(type,"a")==0 ){
16694          rcf = sqlite3_create_function(dbDst,name,nargs,ienc,0,
16695                                        0,dummyUDF,dummyUDFvalue);
16696        }else if( strcmp(type,"s")==0 ){
16697          rcf = sqlite3_create_function(dbDst,name,nargs,ienc,0,
16698                                        dummyUDF,0,0);
16699        }
16700        if( rcf!=SQLITE_OK ){
16701          rc = rcf;
16702          break;
16703        }
16704      }
16705    }
16706    sqlite3_finalize(pStmt);
16707    if( rc==SQLITE_DONE ) rc = SQLITE_OK;
16708  }
16709  return rc;
16710}
16711#endif
16712
16713/*
16714** Allocate a new sqlite3expert object.
16715*/
16716sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
16717  int rc = SQLITE_OK;
16718  sqlite3expert *pNew;
16719
16720  pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
16721
16722  /* Open two in-memory databases to work with. The "vtab database" (dbv)
16723  ** will contain a virtual table corresponding to each real table in
16724  ** the user database schema, and a copy of each view. It is used to
16725  ** collect information regarding the WHERE, ORDER BY and other clauses
16726  ** of the user's query.
16727  */
16728  if( rc==SQLITE_OK ){
16729    pNew->db = db;
16730    pNew->iSample = 100;
16731    rc = sqlite3_open(":memory:", &pNew->dbv);
16732  }
16733  if( rc==SQLITE_OK ){
16734    rc = sqlite3_open(":memory:", &pNew->dbm);
16735    if( rc==SQLITE_OK ){
16736      sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
16737    }
16738  }
16739
16740  /* Allow custom collations to be dealt with through prepare. */
16741  if( rc==SQLITE_OK ) rc = sqlite3_collation_needed(pNew->dbm,0,useDummyCS);
16742  if( rc==SQLITE_OK ) rc = sqlite3_collation_needed(pNew->dbv,0,useDummyCS);
16743
16744#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) \
16745  && !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
16746  /* Register UDFs from database [db] with [dbm] and [dbv]. */
16747  if( rc==SQLITE_OK ){
16748    rc = registerUDFs(pNew->db, pNew->dbm);
16749  }
16750  if( rc==SQLITE_OK ){
16751    rc = registerUDFs(pNew->db, pNew->dbv);
16752  }
16753#endif
16754
16755  /* Copy the entire schema of database [db] into [dbm]. */
16756  if( rc==SQLITE_OK ){
16757    sqlite3_stmt *pSql = 0;
16758    rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg, 
16759        "SELECT sql, name, substr(sql,1,14)=='create virtual' COLLATE nocase"
16760        " FROM sqlite_schema WHERE substr(name,1,7)!='sqlite_' COLLATE nocase"
16761        " ORDER BY 3 DESC, rowid"
16762    );
16763    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
16764      const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
16765      const char *zName = (const char*)sqlite3_column_text(pSql, 1);
16766      int bExists = 0;
16767      rc = expertDbContainsObject(pNew->dbm, zName, &bExists);
16768      if( rc==SQLITE_OK && zSql && bExists==0 ){
16769        rc = expertSchemaSql(pNew->dbm, zSql, pzErrmsg);
16770      }
16771    }
16772    idxFinalize(&rc, pSql);
16773  }
16774
16775  /* Create the vtab schema */
16776  if( rc==SQLITE_OK ){
16777    rc = idxCreateVtabSchema(pNew, pzErrmsg);
16778  }
16779
16780  /* Register the auth callback with dbv */
16781  if( rc==SQLITE_OK ){
16782    sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
16783  }
16784
16785  /* If an error has occurred, free the new object and return NULL. Otherwise,
16786  ** return the new sqlite3expert handle.  */
16787  if( rc!=SQLITE_OK ){
16788    sqlite3_expert_destroy(pNew);
16789    pNew = 0;
16790  }
16791  return pNew;
16792}
16793
16794/*
16795** Configure an sqlite3expert object.
16796*/
16797int sqlite3_expert_config(sqlite3expert *p, int op, ...){
16798  int rc = SQLITE_OK;
16799  va_list ap;
16800  va_start(ap, op);
16801  switch( op ){
16802    case EXPERT_CONFIG_SAMPLE: {
16803      int iVal = va_arg(ap, int);
16804      if( iVal<0 ) iVal = 0;
16805      if( iVal>100 ) iVal = 100;
16806      p->iSample = iVal;
16807      break;
16808    }
16809    default:
16810      rc = SQLITE_NOTFOUND;
16811      break;
16812  }
16813
16814  va_end(ap);
16815  return rc;
16816}
16817
16818/*
16819** Add an SQL statement to the analysis.
16820*/
16821int sqlite3_expert_sql(
16822  sqlite3expert *p,               /* From sqlite3_expert_new() */
16823  const char *zSql,               /* SQL statement to add */
16824  char **pzErr                    /* OUT: Error message (if any) */
16825){
16826  IdxScan *pScanOrig = p->pScan;
16827  IdxStatement *pStmtOrig = p->pStatement;
16828  int rc = SQLITE_OK;
16829  const char *zStmt = zSql;
16830
16831  if( p->bRun ) return SQLITE_MISUSE;
16832
16833  while( rc==SQLITE_OK && zStmt && zStmt[0] ){
16834    sqlite3_stmt *pStmt = 0;
16835    /* Ensure that the provided statement compiles against user's DB. */
16836    rc = idxPrepareStmt(p->db, &pStmt, pzErr, zStmt);
16837    if( rc!=SQLITE_OK ) break;
16838    sqlite3_finalize(pStmt);
16839    rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
16840    if( rc==SQLITE_OK ){
16841      if( pStmt ){
16842        IdxStatement *pNew;
16843        const char *z = sqlite3_sql(pStmt);
16844        i64 n = STRLEN(z);
16845        pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
16846        if( rc==SQLITE_OK ){
16847          pNew->zSql = (char*)&pNew[1];
16848          memcpy(pNew->zSql, z, n+1);
16849          pNew->pNext = p->pStatement;
16850          if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
16851          p->pStatement = pNew;
16852        }
16853        sqlite3_finalize(pStmt);
16854      }
16855    }else{
16856      idxDatabaseError(p->dbv, pzErr);
16857    }
16858  }
16859
16860  if( rc!=SQLITE_OK ){
16861    idxScanFree(p->pScan, pScanOrig);
16862    idxStatementFree(p->pStatement, pStmtOrig);
16863    p->pScan = pScanOrig;
16864    p->pStatement = pStmtOrig;
16865  }
16866
16867  return rc;
16868}
16869
16870int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
16871  int rc;
16872  IdxHashEntry *pEntry;
16873
16874  /* Do trigger processing to collect any extra IdxScan structures */
16875  rc = idxProcessTriggers(p, pzErr);
16876
16877  /* Create candidate indexes within the in-memory database file */
16878  if( rc==SQLITE_OK ){
16879    rc = idxCreateCandidates(p);
16880  }else if ( rc==SQLITE_BUSY_TIMEOUT ){
16881    if( pzErr )
16882      *pzErr = sqlite3_mprintf("Cannot find a unique index name to propose.");
16883    return rc;
16884  }
16885
16886  /* Generate the stat1 data */
16887  if( rc==SQLITE_OK ){
16888    rc = idxPopulateStat1(p, pzErr);
16889  }
16890
16891  /* Formulate the EXPERT_REPORT_CANDIDATES text */
16892  for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
16893    p->zCandidates = idxAppendText(&rc, p->zCandidates, 
16894        "%s;%s%s\n", pEntry->zVal, 
16895        pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
16896    );
16897  }
16898
16899  /* Figure out which of the candidate indexes are preferred by the query
16900  ** planner and report the results to the user.  */
16901  if( rc==SQLITE_OK ){
16902    rc = idxFindIndexes(p, pzErr);
16903  }
16904
16905  if( rc==SQLITE_OK ){
16906    p->bRun = 1;
16907  }
16908  return rc;
16909}
16910
16911/*
16912** Return the total number of statements that have been added to this
16913** sqlite3expert using sqlite3_expert_sql().
16914*/
16915int sqlite3_expert_count(sqlite3expert *p){
16916  int nRet = 0;
16917  if( p->pStatement ) nRet = p->pStatement->iId+1;
16918  return nRet;
16919}
16920
16921/*
16922** Return a component of the report.
16923*/
16924const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
16925  const char *zRet = 0;
16926  IdxStatement *pStmt;
16927
16928  if( p->bRun==0 ) return 0;
16929  for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
16930  switch( eReport ){
16931    case EXPERT_REPORT_SQL:
16932      if( pStmt ) zRet = pStmt->zSql;
16933      break;
16934    case EXPERT_REPORT_INDEXES:
16935      if( pStmt ) zRet = pStmt->zIdx;
16936      break;
16937    case EXPERT_REPORT_PLAN:
16938      if( pStmt ) zRet = pStmt->zEQP;
16939      break;
16940    case EXPERT_REPORT_CANDIDATES:
16941      zRet = p->zCandidates;
16942      break;
16943  }
16944  return zRet;
16945}
16946
16947/*
16948** Free an sqlite3expert object.
16949*/
16950void sqlite3_expert_destroy(sqlite3expert *p){
16951  if( p ){
16952    sqlite3_close(p->dbm);
16953    sqlite3_close(p->dbv);
16954    idxScanFree(p->pScan, 0);
16955    idxStatementFree(p->pStatement, 0);
16956    idxTableFree(p->pTable);
16957    idxWriteFree(p->pWrite);
16958    idxHashClear(&p->hIdx);
16959    sqlite3_free(p->zCandidates);
16960    sqlite3_free(p);
16961  }
16962}
16963
16964#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
16965
16966/************************* End ext/expert/sqlite3expert.c ********************/
16967#endif
16968/************************* Begin ext/intck/sqlite3intck.h ******************/
16969/*
16970** 2024-02-08
16971**
16972** The author disclaims copyright to this source code.  In place of
16973** a legal notice, here is a blessing:
16974**
16975**    May you do good and not evil.
16976**    May you find forgiveness for yourself and forgive others.
16977**    May you share freely, never taking more than you give.
16978**
16979*************************************************************************
16980*/
16981
16982/*
16983** Incremental Integrity-Check Extension
16984** -------------------------------------
16985**
16986** This module contains code to check whether or not an SQLite database
16987** is well-formed or corrupt. This is the same task as performed by SQLite's
16988** built-in "PRAGMA integrity_check" command. This module differs from
16989** "PRAGMA integrity_check" in that:
16990**
16991**   +  It is less thorough - this module does not detect certain types
16992**      of corruption that are detected by the PRAGMA command. However,
16993**      it does detect all kinds of corruption that are likely to cause
16994**      errors in SQLite applications.
16995**
16996**   +  It is slower. Sometimes up to three times slower.
16997**
16998**   +  It allows integrity-check operations to be split into multiple
16999**      transactions, so that the database does not need to be read-locked
17000**      for the duration of the integrity-check.
17001**
17002** One way to use the API to run integrity-check on the "main" database
17003** of handle db is:
17004**
17005**   int rc = SQLITE_OK;
17006**   sqlite3_intck *p = 0;
17007**
17008**   sqlite3_intck_open(db, "main", &p);
17009**   while( SQLITE_OK==sqlite3_intck_step(p) ){
17010**     const char *zMsg = sqlite3_intck_message(p);
17011**     if( zMsg ) printf("corruption: %s\n", zMsg);
17012**   }
17013**   rc = sqlite3_intck_error(p, &zErr);
17014**   if( rc!=SQLITE_OK ){
17015**     printf("error occured (rc=%d), (errmsg=%s)\n", rc, zErr);
17016**   }
17017**   sqlite3_intck_close(p);
17018**
17019** Usually, the sqlite3_intck object opens a read transaction within the
17020** first call to sqlite3_intck_step() and holds it open until the 
17021** integrity-check is complete. However, if sqlite3_intck_unlock() is
17022** called, the read transaction is ended and a new read transaction opened
17023** by the subsequent call to sqlite3_intck_step().
17024*/
17025
17026#ifndef _SQLITE_INTCK_H
17027#define _SQLITE_INTCK_H
17028
17029/* #include "sqlite3.h" */
17030
17031#ifdef __cplusplus
17032extern "C" {
17033#endif
17034
17035/*
17036** An ongoing incremental integrity-check operation is represented by an
17037** opaque pointer of the following type.
17038*/
17039typedef struct sqlite3_intck sqlite3_intck;
17040
17041/*
17042** Open a new incremental integrity-check object. If successful, populate
17043** output variable (*ppOut) with the new object handle and return SQLITE_OK.
17044** Or, if an error occurs, set (*ppOut) to NULL and return an SQLite error
17045** code (e.g. SQLITE_NOMEM).
17046**
17047** The integrity-check will be conducted on database zDb (which must be "main",
17048** "temp", or the name of an attached database) of database handle db. Once
17049** this function has been called successfully, the caller should not use 
17050** database handle db until the integrity-check object has been destroyed
17051** using sqlite3_intck_close().
17052*/
17053int sqlite3_intck_open(
17054  sqlite3 *db,                    /* Database handle */
17055  const char *zDb,                /* Database name ("main", "temp" etc.) */
17056  sqlite3_intck **ppOut           /* OUT: New sqlite3_intck handle */
17057);
17058
17059/*
17060** Close and release all resources associated with a handle opened by an
17061** earlier call to sqlite3_intck_open(). The results of using an
17062** integrity-check handle after it has been passed to this function are
17063** undefined.
17064*/
17065void sqlite3_intck_close(sqlite3_intck *pCk);
17066
17067/*
17068** Do the next step of the integrity-check operation specified by the handle
17069** passed as the only argument. This function returns SQLITE_DONE if the 
17070** integrity-check operation is finished, or an SQLite error code if
17071** an error occurs, or SQLITE_OK if no error occurs but the integrity-check
17072** is not finished. It is not considered an error if database corruption
17073** is encountered.
17074**
17075** Following a successful call to sqlite3_intck_step() (one that returns
17076** SQLITE_OK), sqlite3_intck_message() returns a non-NULL value if 
17077** corruption was detected in the db.
17078**
17079** If an error occurs and a value other than SQLITE_OK or SQLITE_DONE is
17080** returned, then the integrity-check handle is placed in an error state.
17081** In this state all subsequent calls to sqlite3_intck_step() or 
17082** sqlite3_intck_unlock() will immediately return the same error. The 
17083** sqlite3_intck_error() method may be used to obtain an English language 
17084** error message in this case.
17085*/
17086int sqlite3_intck_step(sqlite3_intck *pCk);
17087
17088/*
17089** If the previous call to sqlite3_intck_step() encountered corruption 
17090** within the database, then this function returns a pointer to a buffer
17091** containing a nul-terminated string describing the corruption in 
17092** English. If the previous call to sqlite3_intck_step() did not encounter
17093** corruption, or if there was no previous call, this function returns 
17094** NULL.
17095*/
17096const char *sqlite3_intck_message(sqlite3_intck *pCk);
17097
17098/*
17099** Close any read-transaction opened by an earlier call to 
17100** sqlite3_intck_step(). Any subsequent call to sqlite3_intck_step() will
17101** open a new transaction. Return SQLITE_OK if successful, or an SQLite error
17102** code otherwise.
17103**
17104** If an error occurs, then the integrity-check handle is placed in an error
17105** state. In this state all subsequent calls to sqlite3_intck_step() or 
17106** sqlite3_intck_unlock() will immediately return the same error. The 
17107** sqlite3_intck_error() method may be used to obtain an English language 
17108** error message in this case.
17109*/
17110int sqlite3_intck_unlock(sqlite3_intck *pCk);
17111
17112/*
17113** If an error has occurred in an earlier call to sqlite3_intck_step()
17114** or sqlite3_intck_unlock(), then this method returns the associated 
17115** SQLite error code. Additionally, if pzErr is not NULL, then (*pzErr)
17116** may be set to point to a nul-terminated string containing an English
17117** language error message. Or, if no error message is available, to
17118** NULL.
17119**
17120** If no error has occurred within sqlite3_intck_step() or
17121** sqlite_intck_unlock() calls on the handle passed as the first argument, 
17122** then SQLITE_OK is returned and (*pzErr) set to NULL.
17123*/
17124int sqlite3_intck_error(sqlite3_intck *pCk, const char **pzErr);
17125
17126/*
17127** This API is used for testing only. It returns the full-text of an SQL
17128** statement used to test object zObj, which may be a table or index.
17129** The returned buffer is valid until the next call to either this function
17130** or sqlite3_intck_close() on the same sqlite3_intck handle.
17131*/
17132const char *sqlite3_intck_test_sql(sqlite3_intck *pCk, const char *zObj);
17133
17134
17135#ifdef __cplusplus
17136}  /* end of the 'extern "C"' block */
17137#endif
17138
17139#endif /* ifndef _SQLITE_INTCK_H */
17140
17141/************************* End ext/intck/sqlite3intck.h ********************/
17142/************************* Begin ext/intck/sqlite3intck.c ******************/
17143/*
17144** 2024-02-08
17145**
17146** The author disclaims copyright to this source code.  In place of
17147** a legal notice, here is a blessing:
17148**
17149**    May you do good and not evil.
17150**    May you find forgiveness for yourself and forgive others.
17151**    May you share freely, never taking more than you give.
17152**
17153*************************************************************************
17154*/
17155
17156/* #include "sqlite3intck.h" */
17157#include <string.h>
17158#include <assert.h>
17159
17160#include <stdio.h>
17161#include <stdlib.h>
17162
17163/*
17164** nKeyVal:
17165**   The number of values that make up the 'key' for the current pCheck
17166**   statement.
17167**
17168** rc:
17169**   Error code returned by most recent sqlite3_intck_step() or 
17170**   sqlite3_intck_unlock() call. This is set to SQLITE_DONE when
17171**   the integrity-check operation is finished.
17172**
17173** zErr:
17174**   If the object has entered the error state, this is the error message.
17175**   Is freed using sqlite3_free() when the object is deleted.
17176**
17177** zTestSql:
17178**   The value returned by the most recent call to sqlite3_intck_testsql().
17179**   Each call to testsql() frees the previous zTestSql value (using
17180**   sqlite3_free()) and replaces it with the new value it will return.
17181*/
17182struct sqlite3_intck {
17183  sqlite3 *db;
17184  const char *zDb;                /* Copy of zDb parameter to _open() */
17185  char *zObj;                     /* Current object. Or NULL. */
17186
17187  sqlite3_stmt *pCheck;           /* Current check statement */
17188  char *zKey;
17189  int nKeyVal;
17190
17191  char *zMessage;
17192  int bCorruptSchema;
17193
17194  int rc;                         /* Error code */
17195  char *zErr;                     /* Error message */
17196  char *zTestSql;                 /* Returned by sqlite3_intck_test_sql() */
17197};
17198
17199
17200/*
17201** Some error has occurred while using database p->db. Save the error message
17202** and error code currently held by the database handle in p->rc and p->zErr.
17203*/
17204static void intckSaveErrmsg(sqlite3_intck *p){
17205  p->rc = sqlite3_errcode(p->db);
17206  sqlite3_free(p->zErr);
17207  p->zErr = sqlite3_mprintf("%s", sqlite3_errmsg(p->db));
17208}
17209
17210/*
17211** If the handle passed as the first argument is already in the error state,
17212** then this function is a no-op (returns NULL immediately). Otherwise, if an
17213** error occurs within this function, it leaves an error in said handle.
17214**
17215** Otherwise, this function attempts to prepare SQL statement zSql and
17216** return the resulting statement handle to the user.
17217*/
17218static sqlite3_stmt *intckPrepare(sqlite3_intck *p, const char *zSql){
17219  sqlite3_stmt *pRet = 0;
17220  if( p->rc==SQLITE_OK ){
17221    p->rc = sqlite3_prepare_v2(p->db, zSql, -1, &pRet, 0);
17222    if( p->rc!=SQLITE_OK ){
17223      intckSaveErrmsg(p);
17224      assert( pRet==0 );
17225    }
17226  }
17227  return pRet;
17228}
17229
17230/*
17231** If the handle passed as the first argument is already in the error state,
17232** then this function is a no-op (returns NULL immediately). Otherwise, if an
17233** error occurs within this function, it leaves an error in said handle.
17234**
17235** Otherwise, this function treats argument zFmt as a printf() style format
17236** string. It formats it according to the trailing arguments and then 
17237** attempts to prepare the results and return the resulting prepared
17238** statement.
17239*/
17240static sqlite3_stmt *intckPrepareFmt(sqlite3_intck *p, const char *zFmt, ...){
17241  sqlite3_stmt *pRet = 0;
17242  va_list ap;
17243  char *zSql = 0;
17244  va_start(ap, zFmt);
17245  zSql = sqlite3_vmprintf(zFmt, ap);
17246  if( p->rc==SQLITE_OK && zSql==0 ){
17247    p->rc = SQLITE_NOMEM;
17248  }
17249  pRet = intckPrepare(p, zSql);
17250  sqlite3_free(zSql);
17251  va_end(ap);
17252  return pRet;
17253}
17254
17255/*
17256** Finalize SQL statement pStmt. If an error occurs and the handle passed
17257** as the first argument does not already contain an error, store the
17258** error in the handle.
17259*/
17260static void intckFinalize(sqlite3_intck *p, sqlite3_stmt *pStmt){
17261  int rc = sqlite3_finalize(pStmt);
17262  if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
17263    intckSaveErrmsg(p);
17264  }
17265}
17266
17267/*
17268** If there is already an error in handle p, return it. Otherwise, call
17269** sqlite3_step() on the statement handle and return that value.
17270*/
17271static int intckStep(sqlite3_intck *p, sqlite3_stmt *pStmt){
17272  if( p->rc ) return p->rc;
17273  return sqlite3_step(pStmt);
17274}
17275
17276/*
17277** Execute SQL statement zSql. There is no way to obtain any results 
17278** returned by the statement. This function uses the sqlite3_intck error
17279** code convention.
17280*/
17281static void intckExec(sqlite3_intck *p, const char *zSql){
17282  sqlite3_stmt *pStmt = 0;
17283  pStmt = intckPrepare(p, zSql);
17284  intckStep(p, pStmt);
17285  intckFinalize(p, pStmt);
17286}
17287
17288/*
17289** A wrapper around sqlite3_mprintf() that uses the sqlite3_intck error
17290** code convention.
17291*/
17292static char *intckMprintf(sqlite3_intck *p, const char *zFmt, ...){
17293  va_list ap;
17294  char *zRet = 0;
17295  va_start(ap, zFmt);
17296  zRet = sqlite3_vmprintf(zFmt, ap);
17297  if( p->rc==SQLITE_OK ){
17298    if( zRet==0 ){
17299      p->rc = SQLITE_NOMEM;
17300    }
17301  }else{
17302    sqlite3_free(zRet);
17303    zRet = 0;
17304  }
17305  va_end(ap);
17306  return zRet;
17307}
17308
17309/*
17310** This is used by sqlite3_intck_unlock() to save the vector key value 
17311** required to restart the current pCheck query as a nul-terminated string 
17312** in p->zKey.
17313*/
17314static void intckSaveKey(sqlite3_intck *p){
17315  int ii;
17316  char *zSql = 0;
17317  sqlite3_stmt *pStmt = 0;
17318  sqlite3_stmt *pXinfo = 0;
17319  const char *zDir = 0;
17320
17321  assert( p->pCheck );
17322  assert( p->zKey==0 );
17323
17324  pXinfo = intckPrepareFmt(p, 
17325      "SELECT group_concat(desc, '') FROM %Q.sqlite_schema s, "
17326      "pragma_index_xinfo(%Q, %Q) "
17327      "WHERE s.type='index' AND s.name=%Q",
17328      p->zDb, p->zObj, p->zDb, p->zObj
17329  );
17330  if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXinfo) ){
17331    zDir = (const char*)sqlite3_column_text(pXinfo, 0);
17332  }
17333
17334  if( zDir==0 ){
17335    /* Object is a table, not an index. This is the easy case,as there are 
17336    ** no DESC columns or NULL values in a primary key.  */
17337    const char *zSep = "SELECT '(' || ";
17338    for(ii=0; ii<p->nKeyVal; ii++){
17339      zSql = intckMprintf(p, "%z%squote(?)", zSql, zSep);
17340      zSep = " || ', ' || ";
17341    }
17342    zSql = intckMprintf(p, "%z || ')'", zSql);
17343  }else{
17344
17345    /* Object is an index. */
17346    assert( p->nKeyVal>1 );
17347    for(ii=p->nKeyVal; ii>0; ii--){
17348      int bLastIsDesc = zDir[ii-1]=='1';
17349      int bLastIsNull = sqlite3_column_type(p->pCheck, ii)==SQLITE_NULL;
17350      const char *zLast = sqlite3_column_name(p->pCheck, ii);
17351      char *zLhs = 0;
17352      char *zRhs = 0;
17353      char *zWhere = 0;
17354
17355      if( bLastIsNull ){
17356        if( bLastIsDesc ) continue;
17357        zWhere = intckMprintf(p, "'%s IS NOT NULL'", zLast);
17358      }else{
17359        const char *zOp = bLastIsDesc ? "<" : ">";
17360        zWhere = intckMprintf(p, "'%s %s ' || quote(?%d)", zLast, zOp, ii);
17361      }
17362
17363      if( ii>1 ){
17364        const char *zLhsSep = "";
17365        const char *zRhsSep = "";
17366        int jj;
17367        for(jj=0; jj<ii-1; jj++){
17368          const char *zAlias = (const char*)sqlite3_column_name(p->pCheck,jj+1);
17369          zLhs = intckMprintf(p, "%z%s%s", zLhs, zLhsSep, zAlias);
17370          zRhs = intckMprintf(p, "%z%squote(?%d)", zRhs, zRhsSep, jj+1);
17371          zLhsSep = ",";
17372          zRhsSep = " || ',' || ";
17373        }
17374
17375        zWhere = intckMprintf(p, 
17376            "'(%z) IS (' || %z || ') AND ' || %z",
17377            zLhs, zRhs, zWhere);
17378      }
17379      zWhere = intckMprintf(p, "'WHERE ' || %z", zWhere);
17380
17381      zSql = intckMprintf(p, "%z%s(quote( %z ) )",
17382          zSql,
17383          (zSql==0 ? "VALUES" : ",\n      "),
17384          zWhere
17385      );
17386    }
17387    zSql = intckMprintf(p, 
17388        "WITH wc(q) AS (\n%z\n)"
17389        "SELECT 'VALUES' || group_concat('(' || q || ')', ',\n      ') FROM wc"
17390        , zSql
17391    );
17392  }
17393
17394  pStmt = intckPrepare(p, zSql);
17395  if( p->rc==SQLITE_OK ){
17396    for(ii=0; ii<p->nKeyVal; ii++){
17397      sqlite3_bind_value(pStmt, ii+1, sqlite3_column_value(p->pCheck, ii+1));
17398    }
17399    if( SQLITE_ROW==sqlite3_step(pStmt) ){
17400      p->zKey = intckMprintf(p,"%s",(const char*)sqlite3_column_text(pStmt, 0));
17401    }
17402    intckFinalize(p, pStmt);
17403  }
17404
17405  sqlite3_free(zSql);
17406  intckFinalize(p, pXinfo);
17407}
17408
17409/*
17410** Find the next database object (table or index) to check. If successful,
17411** set sqlite3_intck.zObj to point to a nul-terminated buffer containing
17412** the object's name before returning.
17413*/
17414static void intckFindObject(sqlite3_intck *p){
17415  sqlite3_stmt *pStmt = 0;
17416  char *zPrev = p->zObj;
17417  p->zObj = 0;
17418
17419  assert( p->rc==SQLITE_OK );
17420  assert( p->pCheck==0 );
17421
17422  pStmt = intckPrepareFmt(p, 
17423    "WITH tables(table_name) AS (" 
17424    "  SELECT name"
17425    "  FROM %Q.sqlite_schema WHERE (type='table' OR type='index') AND rootpage"
17426    "  UNION ALL "
17427    "  SELECT 'sqlite_schema'"
17428    ")"
17429    "SELECT table_name FROM tables "
17430    "WHERE ?1 IS NULL OR table_name%s?1 "
17431    "ORDER BY 1"
17432    , p->zDb, (p->zKey ? ">=" : ">")
17433  );
17434
17435  if( p->rc==SQLITE_OK ){
17436    sqlite3_bind_text(pStmt, 1, zPrev, -1, SQLITE_TRANSIENT);
17437    if( sqlite3_step(pStmt)==SQLITE_ROW ){
17438      p->zObj = intckMprintf(p,"%s",(const char*)sqlite3_column_text(pStmt, 0));
17439    }
17440  }
17441  intckFinalize(p, pStmt);
17442
17443  /* If this is a new object, ensure the previous key value is cleared. */
17444  if( sqlite3_stricmp(p->zObj, zPrev) ){
17445    sqlite3_free(p->zKey);
17446    p->zKey = 0;
17447  }
17448
17449  sqlite3_free(zPrev);
17450}
17451
17452/*
17453** Return the size in bytes of the first token in nul-terminated buffer z.
17454** For the purposes of this call, a token is either:
17455**
17456**   *  a quoted SQL string,
17457*    *  a contiguous series of ascii alphabet characters, or
17458*    *  any other single byte.
17459*/
17460static int intckGetToken(const char *z){
17461  char c = z[0];
17462  int iRet = 1;
17463  if( c=='\'' || c=='"' || c=='`' ){
17464    while( z[iRet] ){
17465      if( z[iRet]==c ){
17466        iRet++;
17467        if( z[iRet]!=c ) break;
17468      }
17469      iRet++;
17470    }
17471  }
17472  else if( c=='[' ){
17473    while( z[iRet++]!=']' && z[iRet] );
17474  }
17475  else if( (c>='A' && c<='Z') || (c>='a' && c<='z') ){
17476    while( (z[iRet]>='A' && z[iRet]<='Z') || (z[iRet]>='a' && z[iRet]<='z') ){
17477      iRet++;
17478    }
17479  }
17480
17481  return iRet;
17482}
17483
17484/*
17485** Return true if argument c is an ascii whitespace character.
17486*/
17487static int intckIsSpace(char c){
17488  return (c==' ' || c=='\t' || c=='\n' || c=='\r');
17489}
17490
17491/*
17492** Argument z points to the text of a CREATE INDEX statement. This function
17493** identifies the part of the text that contains either the index WHERE 
17494** clause (if iCol<0) or the iCol'th column of the index.
17495**
17496** If (iCol<0), the identified fragment does not include the "WHERE" keyword,
17497** only the expression that follows it. If (iCol>=0) then the identified
17498** fragment does not include any trailing sort-order keywords - "ASC" or 
17499** "DESC".
17500**
17501** If the CREATE INDEX statement does not contain the requested field or
17502** clause, NULL is returned and (*pnByte) is set to 0. Otherwise, a pointer to
17503** the identified fragment is returned and output parameter (*pnByte) set
17504** to its size in bytes.
17505*/
17506static const char *intckParseCreateIndex(const char *z, int iCol, int *pnByte){
17507  int iOff = 0;
17508  int iThisCol = 0;
17509  int iStart = 0;
17510  int nOpen = 0;
17511
17512  const char *zRet = 0;
17513  int nRet = 0;
17514
17515  int iEndOfCol = 0;
17516
17517  /* Skip forward until the first "(" token */
17518  while( z[iOff]!='(' ){
17519    iOff += intckGetToken(&z[iOff]);
17520    if( z[iOff]=='\0' ) return 0;
17521  }
17522  assert( z[iOff]=='(' );
17523
17524  nOpen = 1;
17525  iOff++;
17526  iStart = iOff;
17527  while( z[iOff] ){
17528    const char *zToken = &z[iOff];
17529    int nToken = 0;
17530
17531    /* Check if this is the end of the current column - either a "," or ")"
17532    ** when nOpen==1.  */
17533    if( nOpen==1 ){
17534      if( z[iOff]==',' || z[iOff]==')' ){
17535        if( iCol==iThisCol ){
17536          int iEnd = iEndOfCol ? iEndOfCol : iOff;
17537          nRet = (iEnd - iStart);
17538          zRet = &z[iStart];
17539          break;
17540        }
17541        iStart = iOff+1;
17542        while( intckIsSpace(z[iStart]) ) iStart++;
17543        iThisCol++;
17544      }
17545      if( z[iOff]==')' ) break;
17546    }
17547    if( z[iOff]=='(' ) nOpen++;
17548    if( z[iOff]==')' ) nOpen--;
17549    nToken = intckGetToken(zToken);
17550
17551    if( (nToken==3 && 0==sqlite3_strnicmp(zToken, "ASC", nToken))
17552     || (nToken==4 && 0==sqlite3_strnicmp(zToken, "DESC", nToken))
17553    ){
17554      iEndOfCol = iOff;
17555    }else if( 0==intckIsSpace(zToken[0]) ){
17556      iEndOfCol = 0;
17557    }
17558
17559    iOff += nToken;
17560  }
17561
17562  /* iStart is now the byte offset of 1 byte passed the final ')' in the
17563  ** CREATE INDEX statement. Try to find a WHERE clause to return.  */
17564  while( zRet==0 && z[iOff] ){
17565    int n = intckGetToken(&z[iOff]);
17566    if( n==5 && 0==sqlite3_strnicmp(&z[iOff], "where", 5) ){
17567      zRet = &z[iOff+5];
17568      nRet = (int)strlen(zRet);
17569    }
17570    iOff += n;
17571  }
17572
17573  /* Trim any whitespace from the start and end of the returned string. */
17574  if( zRet ){
17575    while( intckIsSpace(zRet[0]) ){
17576      nRet--;
17577      zRet++;
17578    }
17579    while( nRet>0 && intckIsSpace(zRet[nRet-1]) ) nRet--;
17580  }
17581
17582  *pnByte = nRet;
17583  return zRet;
17584}
17585
17586/*
17587** User-defined SQL function wrapper for intckParseCreateIndex():
17588**
17589**     SELECT parse_create_index(<sql>, <icol>);
17590*/
17591static void intckParseCreateIndexFunc(
17592  sqlite3_context *pCtx, 
17593  int nVal, 
17594  sqlite3_value **apVal
17595){
17596  const char *zSql = (const char*)sqlite3_value_text(apVal[0]);
17597  int idx = sqlite3_value_int(apVal[1]);
17598  const char *zRes = 0;
17599  int nRes = 0;
17600
17601  assert( nVal==2 );
17602  if( zSql ){
17603    zRes = intckParseCreateIndex(zSql, idx, &nRes);
17604  }
17605  sqlite3_result_text(pCtx, zRes, nRes, SQLITE_TRANSIENT);
17606}
17607
17608/*
17609** Return true if sqlite3_intck.db has automatic indexes enabled, false
17610** otherwise.
17611*/
17612static int intckGetAutoIndex(sqlite3_intck *p){
17613  int bRet = 0;
17614  sqlite3_stmt *pStmt = 0;
17615  pStmt = intckPrepare(p, "PRAGMA automatic_index");
17616  if( SQLITE_ROW==intckStep(p, pStmt) ){
17617    bRet = sqlite3_column_int(pStmt, 0);
17618  }
17619  intckFinalize(p, pStmt);
17620  return bRet;
17621}
17622
17623/*
17624** Return true if zObj is an index, or false otherwise.
17625*/
17626static int intckIsIndex(sqlite3_intck *p, const char *zObj){
17627  int bRet = 0;
17628  sqlite3_stmt *pStmt = 0;
17629  pStmt = intckPrepareFmt(p, 
17630      "SELECT 1 FROM %Q.sqlite_schema WHERE name=%Q AND type='index'",
17631      p->zDb, zObj
17632  );
17633  if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17634    bRet = 1;
17635  }
17636  intckFinalize(p, pStmt);
17637  return bRet;
17638}
17639
17640/*
17641** Return a pointer to a nul-terminated buffer containing the SQL statement
17642** used to check database object zObj (a table or index) for corruption.
17643** If parameter zPrev is not NULL, then it must be a string containing the
17644** vector key required to restart the check where it left off last time.
17645** If pnKeyVal is not NULL, then (*pnKeyVal) is set to the number of
17646** columns in the vector key value for the specified object.
17647**
17648** This function uses the sqlite3_intck error code convention.
17649*/
17650static char *intckCheckObjectSql(
17651  sqlite3_intck *p,               /* Integrity check object */
17652  const char *zObj,               /* Object (table or index) to scan */
17653  const char *zPrev,              /* Restart key vector, if any */
17654  int *pnKeyVal                   /* OUT: Number of key-values for this scan */
17655){
17656  char *zRet = 0;
17657  sqlite3_stmt *pStmt = 0;
17658  int bAutoIndex = 0;
17659  int bIsIndex = 0;
17660
17661  const char *zCommon = 
17662      /* Relation without_rowid also contains just one row. Column "b" is
17663      ** set to true if the table being examined is a WITHOUT ROWID table,
17664      ** or false otherwise.  */
17665      ", without_rowid(b) AS ("
17666      "  SELECT EXISTS ("
17667      "    SELECT 1 FROM tabname, pragma_index_list(tab, db) AS l"
17668      "      WHERE origin='pk' "
17669      "      AND NOT EXISTS (SELECT 1 FROM sqlite_schema WHERE name=l.name)"
17670      "  )"
17671      ")"
17672      ""
17673      /* Table idx_cols contains 1 row for each column in each index on the
17674      ** table being checked. Columns are:
17675      **
17676      **   idx_name: Name of the index.
17677      **   idx_ispk: True if this index is the PK of a WITHOUT ROWID table.
17678      **   col_name: Name of indexed column, or NULL for index on expression.
17679      **   col_expr: Indexed expression, including COLLATE clause.
17680      **   col_alias: Alias used for column in 'intck_wrapper' table.
17681      */
17682      ", idx_cols(idx_name, idx_ispk, col_name, col_expr, col_alias) AS ("
17683      "  SELECT l.name, (l.origin=='pk' AND w.b), i.name, COALESCE(("
17684      "    SELECT parse_create_index(sql, i.seqno) FROM "
17685      "    sqlite_schema WHERE name = l.name"
17686      "  ), format('\"%w\"', i.name) || ' COLLATE ' || quote(i.coll)),"
17687      "  'c' || row_number() OVER ()"
17688      "  FROM "
17689      "      tabname t,"
17690      "      without_rowid w,"
17691      "      pragma_index_list(t.tab, t.db) l,"
17692      "      pragma_index_xinfo(l.name) i"
17693      "      WHERE i.key"
17694      "  UNION ALL"
17695      "  SELECT '', 1, '_rowid_', '_rowid_', 'r1' FROM without_rowid WHERE b=0"
17696      ")"
17697      ""
17698      ""
17699      /*
17700      ** For a PK declared as "PRIMARY KEY(a, b) ... WITHOUT ROWID", where
17701      ** the intck_wrapper aliases of "a" and "b" are "c1" and "c2":
17702      **
17703      **   o_pk:   "o.c1, o.c2"
17704      **   i_pk:   "i.'a', i.'b'"
17705      **   ...
17706      **   n_pk:   2
17707      */ 
17708      ", tabpk(db, tab, idx, o_pk, i_pk, q_pk, eq_pk, ps_pk, pk_pk, n_pk) AS ("
17709      "    WITH pkfields(f, a) AS ("
17710      "      SELECT i.col_name, i.col_alias FROM idx_cols i WHERE i.idx_ispk"
17711      "    )"
17712      "    SELECT t.db, t.tab, t.idx, "
17713      "           group_concat(a, ', '), "
17714      "           group_concat('i.'||quote(f), ', '), "
17715      "           group_concat('quote(o.'||a||')', ' || '','' || '),  "
17716      "           format('(%s)==(%s)',"
17717      "               group_concat('o.'||a, ', '), "
17718      "               group_concat(format('\"%w\"', f), ', ')"
17719      "           ),"
17720      "           group_concat('%s', ','),"
17721      "           group_concat('quote('||a||')', ', '),  "
17722      "           count(*)"
17723      "    FROM tabname t, pkfields"
17724      ")"
17725      ""
17726      ", idx(name, match_expr, partial, partial_alias, idx_ps, idx_idx) AS ("
17727      "  SELECT idx_name,"
17728      "    format('(%s,%s) IS (%s,%s)', "
17729      "           group_concat(i.col_expr, ', '), i_pk,"
17730      "           group_concat('o.'||i.col_alias, ', '), o_pk"
17731      "    ), "
17732      "    parse_create_index("
17733      "        (SELECT sql FROM sqlite_schema WHERE name=idx_name), -1"
17734      "    ),"
17735      "    'cond' || row_number() OVER ()"
17736      "    , group_concat('%s', ',')"
17737      "    , group_concat('quote('||i.col_alias||')', ', ')"
17738      "  FROM tabpk t, "
17739      "       without_rowid w,"
17740      "       idx_cols i"
17741      "  WHERE i.idx_ispk==0 "
17742      "  GROUP BY idx_name"
17743      ")"
17744      ""
17745      ", wrapper_with(s) AS ("
17746      "  SELECT 'intck_wrapper AS (\n  SELECT\n    ' || ("
17747      "      WITH f(a, b) AS ("
17748      "        SELECT col_expr, col_alias FROM idx_cols"
17749      "          UNION ALL "
17750      "        SELECT partial, partial_alias FROM idx WHERE partial IS NOT NULL"
17751      "      )"
17752      "      SELECT group_concat(format('%s AS %s', a, b), ',\n    ') FROM f"
17753      "    )"
17754      "    || format('\n  FROM %Q.%Q ', t.db, t.tab)"
17755           /* If the object being checked is a table, append "NOT INDEXED".
17756           ** Otherwise, append "INDEXED BY <index>", and then, if the index 
17757           ** is a partial index " WHERE <condition>".  */
17758      "    || CASE WHEN t.idx IS NULL THEN "
17759      "        'NOT INDEXED'"
17760      "       ELSE"
17761      "        format('INDEXED BY %Q%s', t.idx, ' WHERE '||i.partial)"
17762      "       END"
17763      "    || '\n)'"
17764      "    FROM tabname t LEFT JOIN idx i ON (i.name=t.idx)"
17765      ")"
17766      ""
17767  ;
17768
17769  bAutoIndex = intckGetAutoIndex(p);
17770  if( bAutoIndex ) intckExec(p, "PRAGMA automatic_index = 0");
17771
17772  bIsIndex = intckIsIndex(p, zObj);
17773  if( bIsIndex ){
17774    pStmt = intckPrepareFmt(p,
17775      /* Table idxname contains a single row. The first column, "db", contains
17776      ** the name of the db containing the table (e.g. "main") and the second,
17777      ** "tab", the name of the table itself.  */
17778      "WITH tabname(db, tab, idx) AS ("
17779      "  SELECT %Q, (SELECT tbl_name FROM %Q.sqlite_schema WHERE name=%Q), %Q "
17780      ")"
17781      ""
17782      ", whereclause(w_c) AS (%s)"
17783      ""
17784      "%s" /* zCommon */
17785      ""
17786      ", case_statement(c) AS ("
17787      "  SELECT "
17788      "    'CASE WHEN (' || group_concat(col_alias, ', ') || ', 1) IS (\n' "
17789      "    || '      SELECT ' || group_concat(col_expr, ', ') || ', 1 FROM '"
17790      "    || format('%%Q.%%Q NOT INDEXED WHERE %%s\n', t.db, t.tab, p.eq_pk)"
17791      "    || '    )\n  THEN NULL\n    '"
17792      "    || 'ELSE format(''surplus entry ('"
17793      "    ||   group_concat('%%s', ',') || ',' || p.ps_pk"
17794      "    || ') in index ' || t.idx || ''', ' "
17795      "    ||   group_concat('quote('||i.col_alias||')', ', ') || ', ' || p.pk_pk"
17796      "    || ')'"
17797      "    || '\n  END AS error_message'"
17798      "  FROM tabname t, tabpk p, idx_cols i WHERE i.idx_name=t.idx"
17799      ")"
17800      ""
17801      ", thiskey(k, n) AS ("
17802      "    SELECT group_concat(i.col_alias, ', ') || ', ' || p.o_pk, "
17803      "           count(*) + p.n_pk "
17804      "    FROM tabpk p, idx_cols i WHERE i.idx_name=p.idx"
17805      ")"
17806      ""
17807      ", main_select(m, n) AS ("
17808      "  SELECT format("
17809      "      'WITH %%s\n' ||"
17810      "      ', idx_checker AS (\n' ||"
17811      "      '  SELECT %%s,\n' ||"
17812      "      '  %%s\n' || "
17813      "      '  FROM intck_wrapper AS o\n' ||"
17814      "      ')\n',"
17815      "      ww.s, c, t.k"
17816      "  ), t.n"
17817      "  FROM case_statement, wrapper_with ww, thiskey t"
17818      ")"
17819
17820      "SELECT m || "
17821      "    group_concat('SELECT * FROM idx_checker ' || w_c, ' UNION ALL '), n"
17822      " FROM "
17823      "main_select, whereclause "
17824      , p->zDb, p->zDb, zObj, zObj
17825      , zPrev ? zPrev : "VALUES('')", zCommon
17826      );
17827  }else{
17828    pStmt = intckPrepareFmt(p,
17829      /* Table tabname contains a single row. The first column, "db", contains
17830      ** the name of the db containing the table (e.g. "main") and the second,
17831      ** "tab", the name of the table itself.  */
17832      "WITH tabname(db, tab, idx, prev) AS (SELECT %Q, %Q, NULL, %Q)"
17833      ""
17834      "%s" /* zCommon */
17835
17836      /* expr(e) contains one row for each index on table zObj. Value e
17837      ** is set to an expression that evaluates to NULL if the required
17838      ** entry is present in the index, or an error message otherwise.  */
17839      ", expr(e, p) AS ("
17840      "  SELECT format('CASE WHEN EXISTS \n"
17841      "    (SELECT 1 FROM %%Q.%%Q AS i INDEXED BY %%Q WHERE %%s%%s)\n"
17842      "    THEN NULL\n"
17843      "    ELSE format(''entry (%%s,%%s) missing from index %%s'', %%s, %%s)\n"
17844      "  END\n'"
17845      "    , t.db, t.tab, i.name, i.match_expr, ' AND (' || partial || ')',"
17846      "      i.idx_ps, t.ps_pk, i.name, i.idx_idx, t.pk_pk),"
17847      "    CASE WHEN partial IS NULL THEN NULL ELSE i.partial_alias END"
17848      "  FROM tabpk t, idx i"
17849      ")"
17850
17851      ", numbered(ii, cond, e) AS ("
17852      "  SELECT 0, 'n.ii=0', 'NULL'"
17853      "    UNION ALL "
17854      "  SELECT row_number() OVER (),"
17855      "      '(n.ii='||row_number() OVER ()||COALESCE(' AND '||p||')', ')'), e"
17856      "  FROM expr"
17857      ")"
17858
17859      ", counter_with(w) AS ("
17860      "    SELECT 'WITH intck_counter(ii) AS (\n  ' || "
17861      "       group_concat('SELECT '||ii, ' UNION ALL\n  ') "
17862      "    || '\n)' FROM numbered"
17863      ")"
17864      ""
17865      ", case_statement(c) AS ("
17866      "    SELECT 'CASE ' || "
17867      "    group_concat(format('\n  WHEN %%s THEN (%%s)', cond, e), '') ||"
17868      "    '\nEND AS error_message'"
17869      "    FROM numbered"
17870      ")"
17871      ""
17872
17873      /* This table contains a single row consisting of a single value -
17874      ** the text of an SQL expression that may be used by the main SQL
17875      ** statement to output an SQL literal that can be used to resume
17876      ** the scan if it is suspended. e.g. for a rowid table, an expression
17877      ** like:
17878      **
17879      **     format('(%d,%d)', _rowid_, n.ii)
17880      */
17881      ", thiskey(k, n) AS ("
17882      "    SELECT o_pk || ', ii', n_pk+1 FROM tabpk"
17883      ")"
17884      ""
17885      ", whereclause(w_c) AS ("
17886      "    SELECT CASE WHEN prev!='' THEN "
17887      "    '\nWHERE (' || o_pk ||', n.ii) > ' || prev"
17888      "    ELSE ''"
17889      "    END"
17890      "    FROM tabpk, tabname"
17891      ")"
17892      ""
17893      ", main_select(m, n) AS ("
17894      "  SELECT format("
17895      "      '%%s, %%s\nSELECT %%s,\n%%s\nFROM intck_wrapper AS o"
17896               ", intck_counter AS n%%s\nORDER BY %%s', "
17897      "      w, ww.s, c, thiskey.k, whereclause.w_c, t.o_pk"
17898      "  ), thiskey.n"
17899      "  FROM case_statement, tabpk t, counter_with, "
17900      "       wrapper_with ww, thiskey, whereclause"
17901      ")"
17902
17903      "SELECT m, n FROM main_select",
17904      p->zDb, zObj, zPrev, zCommon
17905    );
17906  }
17907
17908  while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17909    zRet = intckMprintf(p, "%s", (const char*)sqlite3_column_text(pStmt, 0));
17910    if( pnKeyVal ){
17911      *pnKeyVal = sqlite3_column_int(pStmt, 1);
17912    }
17913  }
17914  intckFinalize(p, pStmt);
17915
17916  if( bAutoIndex ) intckExec(p, "PRAGMA automatic_index = 1");
17917  return zRet;
17918}
17919
17920/*
17921** Open a new integrity-check object.
17922*/
17923int sqlite3_intck_open(
17924  sqlite3 *db,                    /* Database handle to operate on */
17925  const char *zDbArg,             /* "main", "temp" etc. */
17926  sqlite3_intck **ppOut           /* OUT: New integrity-check handle */
17927){
17928  sqlite3_intck *pNew = 0;
17929  int rc = SQLITE_OK;
17930  const char *zDb = zDbArg ? zDbArg : "main";
17931  int nDb = (int)strlen(zDb);
17932
17933  pNew = (sqlite3_intck*)sqlite3_malloc(sizeof(*pNew) + nDb + 1);
17934  if( pNew==0 ){
17935    rc = SQLITE_NOMEM;
17936  }else{
17937    memset(pNew, 0, sizeof(*pNew));
17938    pNew->db = db;
17939    pNew->zDb = (const char*)&pNew[1];
17940    memcpy(&pNew[1], zDb, nDb+1);
17941    rc = sqlite3_create_function(db, "parse_create_index", 
17942        2, SQLITE_UTF8, 0, intckParseCreateIndexFunc, 0, 0
17943    );
17944    if( rc!=SQLITE_OK ){
17945      sqlite3_intck_close(pNew);
17946      pNew = 0;
17947    }
17948  }
17949
17950  *ppOut = pNew;
17951  return rc;
17952}
17953
17954/*
17955** Free the integrity-check object.
17956*/
17957void sqlite3_intck_close(sqlite3_intck *p){
17958  if( p ){
17959    sqlite3_finalize(p->pCheck);
17960    sqlite3_create_function(
17961        p->db, "parse_create_index", 1, SQLITE_UTF8, 0, 0, 0, 0
17962    );
17963    sqlite3_free(p->zObj);
17964    sqlite3_free(p->zKey);
17965    sqlite3_free(p->zTestSql);
17966    sqlite3_free(p->zErr);
17967    sqlite3_free(p->zMessage);
17968    sqlite3_free(p);
17969  }
17970}
17971
17972/*
17973** Step the integrity-check object.
17974*/
17975int sqlite3_intck_step(sqlite3_intck *p){
17976  if( p->rc==SQLITE_OK ){
17977
17978    if( p->zMessage ){
17979      sqlite3_free(p->zMessage);
17980      p->zMessage = 0;
17981    }
17982
17983    if( p->bCorruptSchema ){
17984      p->rc = SQLITE_DONE;
17985    }else
17986    if( p->pCheck==0 ){
17987      intckFindObject(p);
17988      if( p->rc==SQLITE_OK ){
17989        if( p->zObj ){
17990          char *zSql = 0;
17991          zSql = intckCheckObjectSql(p, p->zObj, p->zKey, &p->nKeyVal);
17992          p->pCheck = intckPrepare(p, zSql);
17993          sqlite3_free(zSql);
17994          sqlite3_free(p->zKey);
17995          p->zKey = 0;
17996        }else{
17997          p->rc = SQLITE_DONE;
17998        }
17999      }else if( p->rc==SQLITE_CORRUPT ){
18000        p->rc = SQLITE_OK;
18001        p->zMessage = intckMprintf(p, "%s",
18002            "corruption found while reading database schema"
18003        );
18004        p->bCorruptSchema = 1;
18005      }
18006    }
18007
18008    if( p->pCheck ){
18009      assert( p->rc==SQLITE_OK );
18010      if( sqlite3_step(p->pCheck)==SQLITE_ROW ){
18011        /* Normal case, do nothing. */
18012      }else{
18013        intckFinalize(p, p->pCheck);
18014        p->pCheck = 0;
18015        p->nKeyVal = 0;
18016        if( p->rc==SQLITE_CORRUPT ){
18017          p->rc = SQLITE_OK;
18018          p->zMessage = intckMprintf(p, 
18019              "corruption found while scanning database object %s", p->zObj
18020          );
18021        }
18022      }
18023    }
18024  }
18025
18026  return p->rc;
18027}
18028
18029/*
18030** Return a message describing the corruption encountered by the most recent
18031** call to sqlite3_intck_step(), or NULL if no corruption was encountered.
18032*/
18033const char *sqlite3_intck_message(sqlite3_intck *p){
18034  assert( p->pCheck==0 || p->zMessage==0 );
18035  if( p->zMessage ){
18036    return p->zMessage;
18037  }
18038  if( p->pCheck ){
18039    return (const char*)sqlite3_column_text(p->pCheck, 0);
18040  }
18041  return 0;
18042}
18043
18044/*
18045** Return the error code and message.
18046*/
18047int sqlite3_intck_error(sqlite3_intck *p, const char **pzErr){
18048  if( pzErr ) *pzErr = p->zErr;
18049  return (p->rc==SQLITE_DONE ? SQLITE_OK : p->rc);
18050}
18051
18052/*
18053** Close any read transaction the integrity-check object is holding open
18054** on the database.
18055*/
18056int sqlite3_intck_unlock(sqlite3_intck *p){
18057  if( p->rc==SQLITE_OK && p->pCheck ){
18058    assert( p->zKey==0 && p->nKeyVal>0 );
18059    intckSaveKey(p);
18060    intckFinalize(p, p->pCheck);
18061    p->pCheck = 0;
18062  }
18063  return p->rc;
18064}
18065
18066/*
18067** Return the SQL statement used to check object zObj. Or, if zObj is 
18068** NULL, the current SQL statement.
18069*/
18070const char *sqlite3_intck_test_sql(sqlite3_intck *p, const char *zObj){
18071  sqlite3_free(p->zTestSql);
18072  if( zObj ){
18073    p->zTestSql = intckCheckObjectSql(p, zObj, 0, 0);
18074  }else{
18075    if( p->zObj ){
18076      p->zTestSql = intckCheckObjectSql(p, p->zObj, p->zKey, 0);
18077    }else{
18078      sqlite3_free(p->zTestSql);
18079      p->zTestSql = 0;
18080    }
18081  }
18082  return p->zTestSql;
18083}
18084
18085/************************* End ext/intck/sqlite3intck.c ********************/
18086/************************* Begin ext/misc/stmtrand.c ******************/
18087/*
18088** 2024-05-24
18089**
18090** The author disclaims copyright to this source code.  In place of
18091** a legal notice, here is a blessing:
18092**
18093**    May you do good and not evil.
18094**    May you find forgiveness for yourself and forgive others.
18095**    May you share freely, never taking more than you give.
18096**
18097******************************************************************************
18098**
18099** An SQL function that return pseudo-random non-negative integers.
18100**
18101**      SELECT stmtrand(123);
18102**
18103** A special feature of this function is that the same sequence of random
18104** integers is returned for each invocation of the statement.  This makes
18105** the results repeatable, and hence useful for testing.  The argument is
18106** an integer which is the seed for the random number sequence.  The seed
18107** is used by the first invocation of this function only and is ignored
18108** for all subsequent calls within the same statement.
18109**
18110** Resetting a statement (sqlite3_reset()) also resets the random number
18111** sequence.
18112*/
18113/* #include "sqlite3ext.h" */
18114SQLITE_EXTENSION_INIT1
18115#include <assert.h>
18116#include <string.h>
18117
18118/* State of the pseudo-random number generator */
18119typedef struct Stmtrand {
18120  unsigned int x, y;
18121} Stmtrand;
18122
18123/* auxdata key */
18124#define STMTRAND_KEY  (-4418371)
18125
18126/*
18127** Function:     stmtrand(SEED)
18128**
18129** Return a pseudo-random number.
18130*/
18131static void stmtrandFunc(
18132  sqlite3_context *context,
18133  int argc,
18134  sqlite3_value **argv
18135){
18136  Stmtrand *p;
18137
18138  p = (Stmtrand*)sqlite3_get_auxdata(context, STMTRAND_KEY);
18139  if( p==0 ){
18140    unsigned int seed;
18141    p = sqlite3_malloc64( sizeof(*p) );
18142    if( p==0 ){
18143      sqlite3_result_error_nomem(context);
18144      return;
18145    }
18146    if( argc>=1 ){
18147      seed = (unsigned int)sqlite3_value_int(argv[0]);
18148    }else{
18149      seed = 0;
18150    }
18151    p->x = seed | 1;
18152    p->y = seed;
18153    sqlite3_set_auxdata(context, STMTRAND_KEY, p, sqlite3_free);
18154    p = (Stmtrand*)sqlite3_get_auxdata(context, STMTRAND_KEY);
18155    if( p==0 ){
18156      sqlite3_result_error_nomem(context);
18157      return;
18158    }
18159  }
18160  p->x = (p->x>>1) ^ ((1+~(p->x&1)) & 0xd0000001);
18161  p->y = p->y*1103515245 + 12345;
18162  sqlite3_result_int(context, (int)((p->x ^ p->y)&0x7fffffff));
18163}
18164
18165#ifdef _WIN32
18166
18167#endif
18168int sqlite3_stmtrand_init(
18169  sqlite3 *db, 
18170  char **pzErrMsg, 
18171  const sqlite3_api_routines *pApi
18172){
18173  int rc = SQLITE_OK;
18174  SQLITE_EXTENSION_INIT2(pApi);
18175  (void)pzErrMsg;  /* Unused parameter */
18176  rc = sqlite3_create_function(db, "stmtrand", 1, SQLITE_UTF8, 0,
18177                               stmtrandFunc, 0, 0);
18178  if( rc==SQLITE_OK ){
18179    rc = sqlite3_create_function(db, "stmtrand", 0, SQLITE_UTF8, 0,
18180                                 stmtrandFunc, 0, 0);
18181  }
18182  return rc;
18183}
18184
18185/************************* End ext/misc/stmtrand.c ********************/
18186/************************* Begin ext/misc/vfstrace.c ******************/
18187/*
18188** 2011 March 16
18189**
18190** The author disclaims copyright to this source code.  In place of
18191** a legal notice, here is a blessing:
18192**
18193**    May you do good and not evil.
18194**    May you find forgiveness for yourself and forgive others.
18195**    May you share freely, never taking more than you give.
18196**
18197******************************************************************************
18198**
18199** This file contains code implements a VFS shim that writes diagnostic
18200** output for each VFS call, similar to "strace".
18201**
18202** USAGE:
18203**
18204** This source file exports a single symbol which is the name of a
18205** function:
18206**
18207**   int vfstrace_register(
18208**     const char *zTraceName,         // Name of the newly constructed VFS
18209**     const char *zOldVfsName,        // Name of the underlying VFS
18210**     int (*xOut)(const char*,void*), // Output routine.  ex: fputs
18211**     void *pOutArg,                  // 2nd argument to xOut.  ex: stderr
18212**     int makeDefault                 // Make the new VFS the default
18213**   );
18214**
18215** Applications that want to trace their VFS usage must provide a callback
18216** function with this prototype:
18217**
18218**   int traceOutput(const char *zMessage, void *pAppData);
18219**
18220** This function will "output" the trace messages, where "output" can
18221** mean different things to different applications.  The traceOutput function
18222** for the command-line shell (see shell.c) is "fputs" from the standard
18223** library, which means that all trace output is written on the stream
18224** specified by the second argument.  In the case of the command-line shell
18225** the second argument is stderr.  Other applications might choose to output
18226** trace information to a file, over a socket, or write it into a buffer.
18227**
18228** The vfstrace_register() function creates a new "shim" VFS named by
18229** the zTraceName parameter.  A "shim" VFS is an SQLite backend that does
18230** not really perform the duties of a true backend, but simply filters or
18231** interprets VFS calls before passing them off to another VFS which does
18232** the actual work.  In this case the other VFS - the one that does the
18233** real work - is identified by the second parameter, zOldVfsName.  If
18234** the 2nd parameter is NULL then the default VFS is used.  The common
18235** case is for the 2nd parameter to be NULL.
18236**
18237** The third and fourth parameters are the pointer to the output function
18238** and the second argument to the output function.  For the SQLite
18239** command-line shell, when the -vfstrace option is used, these parameters
18240** are fputs and stderr, respectively.
18241**
18242** The fifth argument is true (non-zero) to cause the newly created VFS
18243** to become the default VFS.  The common case is for the fifth parameter
18244** to be true.
18245**
18246** The call to vfstrace_register() simply creates the shim VFS that does
18247** tracing.  The application must also arrange to use the new VFS for
18248** all database connections that are created and for which tracing is 
18249** desired.  This can be done by specifying the trace VFS using URI filename
18250** notation, or by specifying the trace VFS as the 4th parameter to
18251** sqlite3_open_v2() or by making the trace VFS be the default (by setting
18252** the 5th parameter of vfstrace_register() to 1).
18253**
18254**
18255** ENABLING VFSTRACE IN A COMMAND-LINE SHELL
18256**
18257** The SQLite command line shell implemented by the shell.c source file
18258** can be used with this module.  To compile in -vfstrace support, first
18259** gather this file (test_vfstrace.c), the shell source file (shell.c),
18260** and the SQLite amalgamation source files (sqlite3.c, sqlite3.h) into
18261** the working directory.  Then compile using a command like the following:
18262**
18263**    gcc -o sqlite3 -Os -I. -DSQLITE_ENABLE_VFSTRACE \
18264**        -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_RTREE \
18265**        -DHAVE_READLINE -DHAVE_USLEEP=1 \
18266**        shell.c test_vfstrace.c sqlite3.c -ldl -lreadline -lncurses
18267**
18268** The gcc command above works on Linux and provides (in addition to the
18269** -vfstrace option) support for FTS3 and FTS4, RTREE, and command-line
18270** editing using the readline library.  The command-line shell does not
18271** use threads so we added -DSQLITE_THREADSAFE=0 just to make the code
18272** run a little faster.   For compiling on a Mac, you'll probably need
18273** to omit the -DHAVE_READLINE, the -lreadline, and the -lncurses options.
18274** The compilation could be simplified to just this:
18275**
18276**    gcc -DSQLITE_ENABLE_VFSTRACE \
18277**         shell.c test_vfstrace.c sqlite3.c -ldl -lpthread
18278**
18279** In this second example, all unnecessary options have been removed
18280** Note that since the code is now threadsafe, we had to add the -lpthread
18281** option to pull in the pthreads library.
18282**
18283** To cross-compile for windows using MinGW, a command like this might
18284** work:
18285**
18286**    /opt/mingw/bin/i386-mingw32msvc-gcc -o sqlite3.exe -Os -I \
18287**         -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_VFSTRACE \
18288**         shell.c test_vfstrace.c sqlite3.c
18289**
18290** Similar compiler commands will work on different systems.  The key
18291** invariants are (1) you must have -DSQLITE_ENABLE_VFSTRACE so that
18292** the shell.c source file will know to include the -vfstrace command-line
18293** option and (2) you must compile and link the three source files
18294** shell,c, test_vfstrace.c, and sqlite3.c.
18295**
18296** RUNTIME CONTROL OF VFSTRACE OUTPUT
18297**
18298** The application can use the "vfstrace" pragma to control which VFS
18299** APIs are traced.  To disable all output:
18300**
18301**    PRAGMA vfstrace('-all');
18302**
18303** To enable all output (which is the default setting):
18304**
18305**    PRAGMA vfstrace('+all');
18306**
18307** Individual APIs can be enabled or disabled by name, with or without
18308** the initial "x" character.  For example, to set up for tracing lock
18309** primitives only:
18310**
18311**    PRAGMA vfstrace('-all, +Lock,Unlock,ShmLock');
18312**
18313** The argument to the vfstrace pragma ignores capitalization and any
18314** characters other than alphabetics, '+', and '-'.
18315*/
18316#include <stdlib.h>
18317#include <string.h>
18318/* #include "sqlite3.h" */
18319
18320/*
18321** An instance of this structure is attached to the each trace VFS to
18322** provide auxiliary information.
18323*/
18324typedef struct vfstrace_info vfstrace_info;
18325struct vfstrace_info {
18326  sqlite3_vfs *pRootVfs;              /* The underlying real VFS */
18327  int (*xOut)(const char*, void*);    /* Send output here */
18328  unsigned int mTrace;                /* Mask of interfaces to trace */
18329  u8 bOn;                             /* Tracing on/off */
18330  void *pOutArg;                      /* First argument to xOut */
18331  const char *zVfsName;               /* Name of this trace-VFS */
18332  sqlite3_vfs *pTraceVfs;             /* Pointer back to the trace VFS */
18333};
18334
18335/*
18336** The sqlite3_file object for the trace VFS
18337*/
18338typedef struct vfstrace_file vfstrace_file;
18339struct vfstrace_file {
18340  sqlite3_file base;        /* Base class.  Must be first */
18341  vfstrace_info *pInfo;     /* The trace-VFS to which this file belongs */
18342  const char *zFName;       /* Base name of the file */
18343  sqlite3_file *pReal;      /* The real underlying file */
18344};
18345
18346/*
18347** Bit values for vfstrace_info.mTrace.
18348*/
18349#define VTR_CLOSE           0x00000001
18350#define VTR_READ            0x00000002
18351#define VTR_WRITE           0x00000004
18352#define VTR_TRUNC           0x00000008
18353#define VTR_SYNC            0x00000010
18354#define VTR_FSIZE           0x00000020
18355#define VTR_LOCK            0x00000040
18356#define VTR_UNLOCK          0x00000080
18357#define VTR_CRL             0x00000100
18358#define VTR_FCTRL           0x00000200
18359#define VTR_SECSZ           0x00000400
18360#define VTR_DEVCHAR         0x00000800
18361#define VTR_SHMLOCK         0x00001000
18362#define VTR_SHMMAP          0x00002000
18363#define VTR_SHMBAR          0x00004000
18364#define VTR_SHMUNMAP        0x00008000
18365#define VTR_OPEN            0x00010000
18366#define VTR_DELETE          0x00020000
18367#define VTR_ACCESS          0x00040000
18368#define VTR_FULLPATH        0x00080000
18369#define VTR_DLOPEN          0x00100000
18370#define VTR_DLERR           0x00200000
18371#define VTR_DLSYM           0x00400000
18372#define VTR_DLCLOSE         0x00800000
18373#define VTR_RAND            0x01000000
18374#define VTR_SLEEP           0x02000000
18375#define VTR_CURTIME         0x04000000
18376#define VTR_LASTERR         0x08000000
18377#define VTR_FETCH           0x10000000   /* Also coverse xUnfetch */
18378
18379/*
18380** Method declarations for vfstrace_file.
18381*/
18382static int vfstraceClose(sqlite3_file*);
18383static int vfstraceRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
18384static int vfstraceWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
18385static int vfstraceTruncate(sqlite3_file*, sqlite3_int64 size);
18386static int vfstraceSync(sqlite3_file*, int flags);
18387static int vfstraceFileSize(sqlite3_file*, sqlite3_int64 *pSize);
18388static int vfstraceLock(sqlite3_file*, int);
18389static int vfstraceUnlock(sqlite3_file*, int);
18390static int vfstraceCheckReservedLock(sqlite3_file*, int *);
18391static int vfstraceFileControl(sqlite3_file*, int op, void *pArg);
18392static int vfstraceSectorSize(sqlite3_file*);
18393static int vfstraceDeviceCharacteristics(sqlite3_file*);
18394static int vfstraceShmLock(sqlite3_file*,int,int,int);
18395static int vfstraceShmMap(sqlite3_file*,int,int,int, void volatile **);
18396static void vfstraceShmBarrier(sqlite3_file*);
18397static int vfstraceShmUnmap(sqlite3_file*,int);
18398
18399/*
18400** Method declarations for vfstrace_vfs.
18401*/
18402static int vfstraceOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
18403static int vfstraceDelete(sqlite3_vfs*, const char *zName, int syncDir);
18404static int vfstraceAccess(sqlite3_vfs*, const char *zName, int flags, int *);
18405static int vfstraceFullPathname(sqlite3_vfs*, const char *zName, int, char *);
18406static void *vfstraceDlOpen(sqlite3_vfs*, const char *zFilename);
18407static void vfstraceDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
18408static void (*vfstraceDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void);
18409static void vfstraceDlClose(sqlite3_vfs*, void*);
18410static int vfstraceRandomness(sqlite3_vfs*, int nByte, char *zOut);
18411static int vfstraceSleep(sqlite3_vfs*, int microseconds);
18412static int vfstraceCurrentTime(sqlite3_vfs*, double*);
18413static int vfstraceGetLastError(sqlite3_vfs*, int, char*);
18414static int vfstraceCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
18415static int vfstraceSetSystemCall(sqlite3_vfs*,const char*, sqlite3_syscall_ptr);
18416static sqlite3_syscall_ptr vfstraceGetSystemCall(sqlite3_vfs*, const char *);
18417static const char *vfstraceNextSystemCall(sqlite3_vfs*, const char *zName);
18418
18419/*
18420** Return a pointer to the tail of the pathname.  Examples:
18421**
18422**     /home/drh/xyzzy.txt -> xyzzy.txt
18423**     xyzzy.txt           -> xyzzy.txt
18424*/
18425static const char *fileTail(const char *z){
18426  size_t i;
18427  if( z==0 ) return 0;
18428  i = strlen(z)-1;
18429  while( i>0 && z[i-1]!='/' ){ i--; }
18430  return &z[i];
18431}
18432
18433/*
18434** Send trace output defined by zFormat and subsequent arguments.
18435*/
18436static void vfstrace_printf(
18437  vfstrace_info *pInfo,
18438  const char *zFormat,
18439  ...
18440){
18441  va_list ap;
18442  char *zMsg;
18443  if( pInfo->bOn ){
18444    va_start(ap, zFormat);
18445    zMsg = sqlite3_vmprintf(zFormat, ap);
18446    va_end(ap);
18447    pInfo->xOut(zMsg, pInfo->pOutArg);
18448    sqlite3_free(zMsg);
18449  }
18450}
18451
18452/*
18453** Try to convert an error code into a symbolic name for that error code.
18454*/
18455static const char *vfstrace_errcode_name(int rc ){
18456  const char *zVal = 0;
18457  switch( rc ){
18458    case SQLITE_OK:                 zVal = "SQLITE_OK";                 break;
18459    case SQLITE_INTERNAL:           zVal = "SQLITE_INTERNAL";           break;
18460    case SQLITE_ERROR:              zVal = "SQLITE_ERROR";              break;
18461    case SQLITE_PERM:               zVal = "SQLITE_PERM";               break;
18462    case SQLITE_ABORT:              zVal = "SQLITE_ABORT";              break;
18463    case SQLITE_BUSY:               zVal = "SQLITE_BUSY";               break;
18464    case SQLITE_LOCKED:             zVal = "SQLITE_LOCKED";             break;
18465    case SQLITE_NOMEM:              zVal = "SQLITE_NOMEM";              break;
18466    case SQLITE_READONLY:           zVal = "SQLITE_READONLY";           break;
18467    case SQLITE_INTERRUPT:          zVal = "SQLITE_INTERRUPT";          break;
18468    case SQLITE_IOERR:              zVal = "SQLITE_IOERR";              break;
18469    case SQLITE_CORRUPT:            zVal = "SQLITE_CORRUPT";            break;
18470    case SQLITE_NOTFOUND:           zVal = "SQLITE_NOTFOUND";           break;
18471    case SQLITE_FULL:               zVal = "SQLITE_FULL";               break;
18472    case SQLITE_CANTOPEN:           zVal = "SQLITE_CANTOPEN";           break;
18473    case SQLITE_PROTOCOL:           zVal = "SQLITE_PROTOCOL";           break;
18474    case SQLITE_EMPTY:              zVal = "SQLITE_EMPTY";              break;
18475    case SQLITE_SCHEMA:             zVal = "SQLITE_SCHEMA";             break;
18476    case SQLITE_TOOBIG:             zVal = "SQLITE_TOOBIG";             break;
18477    case SQLITE_CONSTRAINT:         zVal = "SQLITE_CONSTRAINT";         break;
18478    case SQLITE_MISMATCH:           zVal = "SQLITE_MISMATCH";           break;
18479    case SQLITE_MISUSE:             zVal = "SQLITE_MISUSE";             break;
18480    case SQLITE_NOLFS:              zVal = "SQLITE_NOLFS";              break;
18481    case SQLITE_IOERR_READ:         zVal = "SQLITE_IOERR_READ";         break;
18482    case SQLITE_IOERR_SHORT_READ:   zVal = "SQLITE_IOERR_SHORT_READ";   break;
18483    case SQLITE_IOERR_WRITE:        zVal = "SQLITE_IOERR_WRITE";        break;
18484    case SQLITE_IOERR_FSYNC:        zVal = "SQLITE_IOERR_FSYNC";        break;
18485    case SQLITE_IOERR_DIR_FSYNC:    zVal = "SQLITE_IOERR_DIR_FSYNC";    break;
18486    case SQLITE_IOERR_TRUNCATE:     zVal = "SQLITE_IOERR_TRUNCATE";     break;
18487    case SQLITE_IOERR_FSTAT:        zVal = "SQLITE_IOERR_FSTAT";        break;
18488    case SQLITE_IOERR_UNLOCK:       zVal = "SQLITE_IOERR_UNLOCK";       break;
18489    case SQLITE_IOERR_RDLOCK:       zVal = "SQLITE_IOERR_RDLOCK";       break;
18490    case SQLITE_IOERR_DELETE:       zVal = "SQLITE_IOERR_DELETE";       break;
18491    case SQLITE_IOERR_BLOCKED:      zVal = "SQLITE_IOERR_BLOCKED";      break;
18492    case SQLITE_IOERR_NOMEM:        zVal = "SQLITE_IOERR_NOMEM";        break;
18493    case SQLITE_IOERR_ACCESS:       zVal = "SQLITE_IOERR_ACCESS";       break;
18494    case SQLITE_IOERR_CHECKRESERVEDLOCK:
18495                               zVal = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
18496    case SQLITE_IOERR_LOCK:         zVal = "SQLITE_IOERR_LOCK";         break;
18497    case SQLITE_IOERR_CLOSE:        zVal = "SQLITE_IOERR_CLOSE";        break;
18498    case SQLITE_IOERR_DIR_CLOSE:    zVal = "SQLITE_IOERR_DIR_CLOSE";    break;
18499    case SQLITE_IOERR_SHMOPEN:      zVal = "SQLITE_IOERR_SHMOPEN";      break;
18500    case SQLITE_IOERR_SHMSIZE:      zVal = "SQLITE_IOERR_SHMSIZE";      break;
18501    case SQLITE_IOERR_SHMLOCK:      zVal = "SQLITE_IOERR_SHMLOCK";      break;
18502    case SQLITE_IOERR_SHMMAP:       zVal = "SQLITE_IOERR_SHMMAP";       break;
18503    case SQLITE_IOERR_SEEK:         zVal = "SQLITE_IOERR_SEEK";         break;
18504    case SQLITE_IOERR_GETTEMPPATH:  zVal = "SQLITE_IOERR_GETTEMPPATH";  break;
18505    case SQLITE_IOERR_CONVPATH:     zVal = "SQLITE_IOERR_CONVPATH";     break;
18506    case SQLITE_READONLY_DBMOVED:   zVal = "SQLITE_READONLY_DBMOVED";   break;
18507    case SQLITE_LOCKED_SHAREDCACHE: zVal = "SQLITE_LOCKED_SHAREDCACHE"; break;
18508    case SQLITE_BUSY_RECOVERY:      zVal = "SQLITE_BUSY_RECOVERY";      break;
18509    case SQLITE_CANTOPEN_NOTEMPDIR: zVal = "SQLITE_CANTOPEN_NOTEMPDIR"; break;
18510  }
18511  return zVal;
18512}
18513
18514/*
18515** Convert value rc into a string and print it using zFormat.  zFormat
18516** should have exactly one %s
18517*/
18518static void vfstrace_print_errcode(
18519  vfstrace_info *pInfo,
18520  const char *zFormat,
18521  int rc
18522){
18523  const char *zVal;
18524  char zBuf[50];
18525  zVal = vfstrace_errcode_name(rc);
18526  if( zVal==0 ){
18527    zVal = vfstrace_errcode_name(rc&0xff);
18528    if( zVal ){
18529      sqlite3_snprintf(sizeof(zBuf), zBuf, "%s | 0x%x", zVal, rc&0xffff00);
18530    }else{
18531      sqlite3_snprintf(sizeof(zBuf), zBuf, "%d (0x%x)", rc, rc);
18532    }
18533    zVal = zBuf;
18534  }
18535  vfstrace_printf(pInfo, zFormat, zVal);
18536}
18537
18538/*
18539** Append to a buffer.
18540*/
18541static void strappend(char *z, int *pI, const char *zAppend){
18542  int i = *pI;
18543  while( zAppend[0] ){ z[i++] = *(zAppend++); }
18544  z[i] = 0;
18545  *pI = i;
18546}
18547
18548/*
18549** Turn tracing output on or off according to mMask.
18550*/
18551static void vfstraceOnOff(vfstrace_info *pInfo, unsigned int mMask){
18552  pInfo->bOn = (pInfo->mTrace & mMask)!=0;
18553}
18554
18555/*
18556** Close an vfstrace-file.
18557*/
18558static int vfstraceClose(sqlite3_file *pFile){
18559  vfstrace_file *p = (vfstrace_file *)pFile;
18560  vfstrace_info *pInfo = p->pInfo;
18561  int rc;
18562  vfstraceOnOff(pInfo, VTR_CLOSE);
18563  vfstrace_printf(pInfo, "%s.xClose(%s)", pInfo->zVfsName, p->zFName);
18564  rc = p->pReal->pMethods->xClose(p->pReal);
18565  vfstrace_print_errcode(pInfo, " -> %s\n", rc);
18566  if( rc==SQLITE_OK ){
18567    sqlite3_free((void*)p->base.pMethods);
18568    p->base.pMethods = 0;
18569  }
18570  return rc;
18571}
18572
18573/*
18574** Read data from an vfstrace-file.
18575*/
18576static int vfstraceRead(
18577  sqlite3_file *pFile, 
18578  void *zBuf, 
18579  int iAmt, 
18580  sqlite_int64 iOfst
18581){
18582  vfstrace_file *p = (vfstrace_file *)pFile;
18583  vfstrace_info *pInfo = p->pInfo;
18584  int rc;
18585  vfstraceOnOff(pInfo, VTR_READ);
18586  vfstrace_printf(pInfo, "%s.xRead(%s,n=%d,ofst=%lld)",
18587                  pInfo->zVfsName, p->zFName, iAmt, iOfst);
18588  rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
18589  vfstrace_print_errcode(pInfo, " -> %s\n", rc);
18590  return rc;
18591}
18592
18593/*
18594** Write data to an vfstrace-file.
18595*/
18596static int vfstraceWrite(
18597  sqlite3_file *pFile, 
18598  const void *zBuf, 
18599  int iAmt, 
18600  sqlite_int64 iOfst
18601){
18602  vfstrace_file *p = (vfstrace_file *)pFile;
18603  vfstrace_info *pInfo = p->pInfo;
18604  int rc;
18605  vfstraceOnOff(pInfo, VTR_WRITE);
18606  vfstrace_printf(pInfo, "%s.xWrite(%s,n=%d,ofst=%lld)",
18607                  pInfo->zVfsName, p->zFName, iAmt, iOfst);
18608  rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
18609  vfstrace_print_errcode(pInfo, " -> %s\n", rc);
18610  return rc;
18611}
18612
18613/*
18614** Truncate an vfstrace-file.
18615*/
18616static int vfstraceTruncate(sqlite3_file *pFile, sqlite_int64 size){
18617  vfstrace_file *p = (vfstrace_file *)pFile;
18618  vfstrace_info *pInfo = p->pInfo;
18619  int rc;
18620  vfstraceOnOff(pInfo, VTR_TRUNC);
18621  vfstrace_printf(pInfo, "%s.xTruncate(%s,%lld)", pInfo->zVfsName, p->zFName,
18622                  size);
18623  rc = p->pReal->pMethods->xTruncate(p->pReal, size);
18624  vfstrace_printf(pInfo, " -> %d\n", rc);
18625  return rc;
18626}
18627
18628/*
18629** Sync an vfstrace-file.
18630*/
18631static int vfstraceSync(sqlite3_file *pFile, int flags){
18632  vfstrace_file *p = (vfstrace_file *)pFile;
18633  vfstrace_info *pInfo = p->pInfo;
18634  int rc;
18635  int i;
18636  char zBuf[100];
18637  memcpy(zBuf, "|0", 3);
18638  i = 0;
18639  if( flags & SQLITE_SYNC_FULL )        strappend(zBuf, &i, "|FULL");
18640  else if( flags & SQLITE_SYNC_NORMAL ) strappend(zBuf, &i, "|NORMAL");
18641  if( flags & SQLITE_SYNC_DATAONLY )    strappend(zBuf, &i, "|DATAONLY");
18642  if( flags & ~(SQLITE_SYNC_FULL|SQLITE_SYNC_DATAONLY) ){
18643    sqlite3_snprintf(sizeof(zBuf)-i, &zBuf[i], "|0x%x", flags);
18644  }
18645  vfstraceOnOff(pInfo, VTR_SYNC);
18646  vfstrace_printf(pInfo, "%s.xSync(%s,%s)", pInfo->zVfsName, p->zFName,
18647                  &zBuf[1]);
18648  rc = p->pReal->pMethods->xSync(p->pReal, flags);
18649  vfstrace_printf(pInfo, " -> %d\n", rc);
18650  return rc;
18651}
18652
18653/*
18654** Return the current file-size of an vfstrace-file.
18655*/
18656static int vfstraceFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
18657  vfstrace_file *p = (vfstrace_file *)pFile;
18658  vfstrace_info *pInfo = p->pInfo;
18659  int rc;
18660  vfstraceOnOff(pInfo, VTR_FSIZE);
18661  vfstrace_printf(pInfo, "%s.xFileSize(%s)", pInfo->zVfsName, p->zFName);
18662  rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
18663  vfstrace_print_errcode(pInfo, " -> %s,", rc);
18664  vfstrace_printf(pInfo, " size=%lld\n", *pSize);
18665  return rc;
18666}
18667
18668/*
18669** Return the name of a lock.
18670*/
18671static const char *lockName(int eLock){
18672  const char *azLockNames[] = {
18673     "NONE", "SHARED", "RESERVED", "PENDING", "EXCLUSIVE"
18674  };
18675  if( eLock<0 || eLock>=(int)(sizeof(azLockNames)/sizeof(azLockNames[0])) ){
18676    return "???";
18677  }else{
18678    return azLockNames[eLock];
18679  }
18680}
18681
18682/*
18683** Lock an vfstrace-file.
18684*/
18685static int vfstraceLock(sqlite3_file *pFile, int eLock){
18686  vfstrace_file *p = (vfstrace_file *)pFile;
18687  vfstrace_info *pInfo = p->pInfo;
18688  int rc;
18689  vfstraceOnOff(pInfo, VTR_LOCK);
18690  vfstrace_printf(pInfo, "%s.xLock(%s,%s)", pInfo->zVfsName, p->zFName,
18691                  lockName(eLock));
18692  rc = p->pReal->pMethods->xLock(p->pReal, eLock);
18693  vfstrace_print_errcode(pInfo, " -> %s\n", rc);
18694  return rc;
18695}
18696
18697/*
18698** Unlock an vfstrace-file.
18699*/
18700static int vfstraceUnlock(sqlite3_file *pFile, int eLock){
18701  vfstrace_file *p = (vfstrace_file *)pFile;
18702  vfstrace_info *pInfo = p->pInfo;
18703  int rc;
18704  vfstraceOnOff(pInfo, VTR_UNLOCK);
18705  vfstrace_printf(pInfo, "%s.xUnlock(%s,%s)", pInfo->zVfsName, p->zFName,
18706                  lockName(eLock));
18707  rc = p->pReal->pMethods->xUnlock(p->pReal, eLock);
18708  vfstrace_print_errcode(pInfo, " -> %s\n", rc);
18709  return rc;
18710}
18711
18712/*
18713** Check if another file-handle holds a RESERVED lock on an vfstrace-file.
18714*/
18715static int vfstraceCheckReservedLock(sqlite3_file *pFile, int *pResOut){
18716  vfstrace_file *p = (vfstrace_file *)pFile;
18717  vfstrace_info *pInfo = p->pInfo;
18718  int rc;
18719  vfstraceOnOff(pInfo, VTR_CRL);
18720  vfstrace_printf(pInfo, "%s.xCheckReservedLock(%s,%d)", 
18721                  pInfo->zVfsName, p->zFName);
18722  rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
18723  vfstrace_print_errcode(pInfo, " -> %s", rc);
18724  vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
18725  return rc;
18726}
18727
18728/*
18729** File control method. For custom operations on an vfstrace-file.
18730*/
18731static int vfstraceFileControl(sqlite3_file *pFile, int op, void *pArg){
18732  vfstrace_file *p = (vfstrace_file *)pFile;
18733  vfstrace_info *pInfo = p->pInfo;
18734  int rc;
18735  char zBuf[100];
18736  char zBuf2[100];
18737  char *zOp;
18738  char *zRVal = 0;
18739  vfstraceOnOff(pInfo, VTR_FCTRL);
18740  switch( op ){
18741    case SQLITE_FCNTL_LOCKSTATE:           zOp = "LOCKSTATE";           break;
18742    case SQLITE_GET_LOCKPROXYFILE:         zOp = "GET_LOCKPROXYFILE";   break;
18743    case SQLITE_SET_LOCKPROXYFILE:         zOp = "SET_LOCKPROXYFILE";   break;
18744    case SQLITE_LAST_ERRNO:                zOp = "LAST_ERRNO";          break;
18745    case SQLITE_FCNTL_SIZE_HINT: {
18746      sqlite3_snprintf(sizeof(zBuf), zBuf, "SIZE_HINT,%lld",
18747                       *(sqlite3_int64*)pArg);
18748      zOp = zBuf;
18749      break;
18750    }
18751    case SQLITE_FCNTL_CHUNK_SIZE: {
18752      sqlite3_snprintf(sizeof(zBuf), zBuf, "CHUNK_SIZE,%d", *(int*)pArg);
18753      zOp = zBuf;
18754      break;
18755    }
18756    case SQLITE_FCNTL_FILE_POINTER:        zOp = "FILE_POINTER";        break;
18757    case SQLITE_FCNTL_WIN32_AV_RETRY:      zOp = "WIN32_AV_RETRY";      break;
18758    case SQLITE_FCNTL_PERSIST_WAL: {
18759       sqlite3_snprintf(sizeof(zBuf), zBuf, "PERSIST_WAL,%d", *(int*)pArg);
18760       zOp = zBuf;
18761       break;
18762    }
18763    case SQLITE_FCNTL_OVERWRITE:           zOp = "OVERWRITE";           break;
18764    case SQLITE_FCNTL_VFSNAME:             zOp = "VFSNAME";             break;
18765    case SQLITE_FCNTL_POWERSAFE_OVERWRITE: zOp = "POWERSAFE_OVERWRITE"; break;
18766    case SQLITE_FCNTL_PRAGMA: {
18767      const char *const* a = (const char*const*)pArg;
18768      if( a[1] && strcmp(a[1],"vfstrace")==0 && a[2] ){
18769        const u8 *zArg = (const u8*)a[2];
18770        if( zArg[0]>='0' && zArg[0]<='9' ){
18771          pInfo->mTrace = (sqlite3_uint64)strtoll(a[2], 0, 0);
18772        }else{
18773          static const struct {
18774            const char *z;
18775            unsigned int m;
18776          } aKw[] = {
18777            { "all",                   0xffffffff   },
18778            { "close",                 VTR_CLOSE    },
18779            { "read",                  VTR_READ     },
18780            { "write",                 VTR_WRITE    },
18781            { "truncate",              VTR_TRUNC    },
18782            { "sync",                  VTR_SYNC     },
18783            { "filesize",              VTR_FSIZE    },
18784            { "lock",                  VTR_LOCK     },
18785            { "unlock",                VTR_UNLOCK   },
18786            { "checkreservedlock",     VTR_CRL      },
18787            { "filecontrol",           VTR_FCTRL    },
18788            { "sectorsize",            VTR_SECSZ    },
18789            { "devicecharacteristics", VTR_DEVCHAR  },
18790            { "shmlock",               VTR_SHMLOCK  },
18791            { "shmmap",                VTR_SHMMAP   },
18792            { "shmummap",              VTR_SHMUNMAP },
18793            { "shmbarrier",            VTR_SHMBAR   },
18794            { "open",                  VTR_OPEN     },
18795            { "delete",                VTR_DELETE   },
18796            { "access",                VTR_ACCESS   },
18797            { "fullpathname",          VTR_FULLPATH },
18798            { "dlopen",                VTR_DLOPEN   },
18799            { "dlerror",               VTR_DLERR    },
18800            { "dlsym",                 VTR_DLSYM    },
18801            { "dlclose",               VTR_DLCLOSE  },
18802            { "randomness",            VTR_RAND     },
18803            { "sleep",                 VTR_SLEEP    },
18804            { "currenttime",           VTR_CURTIME  },
18805            { "currenttimeint64",      VTR_CURTIME  },
18806            { "getlasterror",          VTR_LASTERR  },
18807            { "fetch",                 VTR_FETCH    },
18808          };
18809          int onOff = 1;
18810          while( zArg[0] ){
18811            int jj, n;
18812            while( zArg[0]!=0 && zArg[0]!='-' && zArg[0]!='+'
18813                   && !isalpha(zArg[0]) ) zArg++;
18814            if( zArg[0]==0 ) break;
18815            if( zArg[0]=='-' ){
18816              onOff = 0;
18817              zArg++;
18818            }else if( zArg[0]=='+' ){
18819              onOff = 1;
18820              zArg++;
18821            }
18822            while( !isalpha(zArg[0]) ){
18823              if( zArg[0]==0 ) break;
18824              zArg++;
18825            }
18826            if( zArg[0]=='x' && isalpha(zArg[1]) ) zArg++;
18827            for(n=0; isalpha(zArg[n]); n++){}
18828            for(jj=0; jj<(int)(sizeof(aKw)/sizeof(aKw[0])); jj++){
18829              if( sqlite3_strnicmp(aKw[jj].z,(const char*)zArg,n)==0 ){
18830                if( onOff ){
18831                  pInfo->mTrace |= aKw[jj].m;
18832                }else{
18833                  pInfo->mTrace &= ~aKw[jj].m;
18834                }
18835                break;
18836              }
18837            }
18838            zArg += n;
18839          }
18840        }
18841      }
18842      sqlite3_snprintf(sizeof(zBuf), zBuf, "PRAGMA,[%s,%s]",a[1],a[2]);
18843      zOp = zBuf;
18844      break;
18845    }
18846    case SQLITE_FCNTL_BUSYHANDLER:         zOp = "BUSYHANDLER";         break;
18847    case SQLITE_FCNTL_TEMPFILENAME:        zOp = "TEMPFILENAME";        break;
18848    case SQLITE_FCNTL_MMAP_SIZE: {
18849      sqlite3_int64 iMMap = *(sqlite3_int64*)pArg;
18850      sqlite3_snprintf(sizeof(zBuf), zBuf, "MMAP_SIZE,%lld",iMMap);
18851      zOp = zBuf;
18852      break;
18853    }
18854    case SQLITE_FCNTL_TRACE:               zOp = "TRACE";               break;
18855    case SQLITE_FCNTL_HAS_MOVED:           zOp = "HAS_MOVED";           break;
18856    case SQLITE_FCNTL_SYNC:                zOp = "SYNC";                break;
18857    case SQLITE_FCNTL_COMMIT_PHASETWO:     zOp = "COMMIT_PHASETWO";     break;
18858    case SQLITE_FCNTL_WIN32_SET_HANDLE:    zOp = "WIN32_SET_HANDLE";    break;
18859    case SQLITE_FCNTL_WAL_BLOCK:           zOp = "WAL_BLOCK";           break;
18860    case SQLITE_FCNTL_ZIPVFS:              zOp = "ZIPVFS";              break; 
18861    case SQLITE_FCNTL_RBU:                 zOp = "RBU";                 break;
18862    case SQLITE_FCNTL_VFS_POINTER:         zOp = "VFS_POINTER";         break;
18863    case SQLITE_FCNTL_JOURNAL_POINTER:     zOp = "JOURNAL_POINTER";     break;
18864    case SQLITE_FCNTL_WIN32_GET_HANDLE:    zOp = "WIN32_GET_HANDLE";    break;
18865    case SQLITE_FCNTL_PDB:                 zOp = "PDB";                 break;
18866    case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE:  zOp = "BEGIN_ATOMIC_WRITE";  break;
18867    case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: zOp = "COMMIT_ATOMIC_WRITE"; break;
18868    case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: {
18869       zOp = "ROLLBACK_ATOMIC_WRITE";
18870       break;
18871    }
18872    case SQLITE_FCNTL_LOCK_TIMEOUT: {
18873       sqlite3_snprintf(sizeof(zBuf), zBuf, "LOCK_TIMEOUT,%d", *(int*)pArg);
18874       zOp = zBuf;
18875       break;
18876    }
18877    case SQLITE_FCNTL_DATA_VERSION:        zOp = "DATA_VERSION";        break;
18878    case SQLITE_FCNTL_SIZE_LIMIT:          zOp = "SIZE_LIMIT";          break;
18879    case SQLITE_FCNTL_CKPT_DONE:           zOp = "CKPT_DONE";           break;
18880    case SQLITE_FCNTL_RESERVE_BYTES:       zOp = "RESERVED_BYTES";      break;
18881    case SQLITE_FCNTL_CKPT_START:          zOp = "CKPT_START";          break;
18882    case SQLITE_FCNTL_EXTERNAL_READER:     zOp = "EXTERNAL_READER";     break;
18883    case SQLITE_FCNTL_CKSM_FILE:           zOp = "CKSM_FILE";           break;
18884    case SQLITE_FCNTL_RESET_CACHE:         zOp = "RESET_CACHE";         break;
18885    case 0xca093fa0:                       zOp = "DB_UNCHANGED";        break;
18886    default: {
18887      sqlite3_snprintf(sizeof zBuf, zBuf, "%d", op);
18888      zOp = zBuf;
18889      break;
18890    }
18891  }
18892  vfstrace_printf(pInfo, "%s.xFileControl(%s,%s)",
18893                  pInfo->zVfsName, p->zFName, zOp);
18894  rc = p->pReal->pMethods->xFileControl(p->pReal, op, pArg);
18895  if( rc==SQLITE_OK ){
18896    switch( op ){
18897      case SQLITE_FCNTL_VFSNAME: {
18898        *(char**)pArg = sqlite3_mprintf("vfstrace.%s/%z",
18899                                    pInfo->zVfsName, *(char**)pArg);
18900        zRVal = *(char**)pArg;
18901        break;
18902      }
18903      case SQLITE_FCNTL_MMAP_SIZE: {
18904        sqlite3_snprintf(sizeof(zBuf2), zBuf2, "%lld", *(sqlite3_int64*)pArg);
18905        zRVal = zBuf2;
18906        break;
18907      }
18908      case SQLITE_FCNTL_HAS_MOVED:
18909      case SQLITE_FCNTL_PERSIST_WAL: {
18910        sqlite3_snprintf(sizeof(zBuf2), zBuf2, "%d", *(int*)pArg);
18911        zRVal = zBuf2;
18912        break;
18913      }
18914      case SQLITE_FCNTL_PRAGMA:
18915      case SQLITE_FCNTL_TEMPFILENAME: {
18916        zRVal = *(char**)pArg;
18917        break;
18918      }
18919    }
18920  }
18921  if( zRVal ){
18922    vfstrace_print_errcode(pInfo, " -> %s", rc);
18923    vfstrace_printf(pInfo, ", %s\n", zRVal);
18924  }else{
18925    vfstrace_print_errcode(pInfo, " -> %s\n", rc);
18926  }
18927  return rc;
18928}
18929
18930/*
18931** Return the sector-size in bytes for an vfstrace-file.
18932*/
18933static int vfstraceSectorSize(sqlite3_file *pFile){
18934  vfstrace_file *p = (vfstrace_file *)pFile;
18935  vfstrace_info *pInfo = p->pInfo;
18936  int rc;
18937  vfstraceOnOff(pInfo, VTR_SECSZ);
18938  vfstrace_printf(pInfo, "%s.xSectorSize(%s)", pInfo->zVfsName, p->zFName);
18939  rc = p->pReal->pMethods->xSectorSize(p->pReal);
18940  vfstrace_printf(pInfo, " -> %d\n", rc);
18941  return rc;
18942}
18943
18944/*
18945** Return the device characteristic flags supported by an vfstrace-file.
18946*/
18947static int vfstraceDeviceCharacteristics(sqlite3_file *pFile){
18948  vfstrace_file *p = (vfstrace_file *)pFile;
18949  vfstrace_info *pInfo = p->pInfo;
18950  int rc;
18951  vfstraceOnOff(pInfo, VTR_DEVCHAR);
18952  vfstrace_printf(pInfo, "%s.xDeviceCharacteristics(%s)",
18953                  pInfo->zVfsName, p->zFName);
18954  rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
18955  vfstrace_printf(pInfo, " -> 0x%08x\n", rc);
18956  return rc;
18957}
18958
18959/*
18960** Shared-memory operations.
18961*/
18962static int vfstraceShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
18963  static const char *azLockName[] = {
18964     "WRITE",
18965     "CKPT",
18966     "RECOVER",
18967     "READ0",
18968     "READ1",
18969     "READ2",
18970     "READ3",
18971     "READ4",
18972  };
18973  vfstrace_file *p = (vfstrace_file *)pFile;
18974  vfstrace_info *pInfo = p->pInfo;
18975  int rc;
18976  char zLck[100];
18977  int i = 0;
18978  vfstraceOnOff(pInfo, VTR_SHMLOCK);
18979  memcpy(zLck, "|0", 3);
18980  if( flags & SQLITE_SHM_UNLOCK )    strappend(zLck, &i, "|UNLOCK");
18981  if( flags & SQLITE_SHM_LOCK )      strappend(zLck, &i, "|LOCK");
18982  if( flags & SQLITE_SHM_SHARED )    strappend(zLck, &i, "|SHARED");
18983  if( flags & SQLITE_SHM_EXCLUSIVE ) strappend(zLck, &i, "|EXCLUSIVE");
18984  if( flags & ~(0xf) ){
18985     sqlite3_snprintf(sizeof(zLck)-i, &zLck[i], "|0x%x", flags);
18986  }
18987  if( ofst>=0 && ofst<(int)(sizeof(azLockName)/sizeof(azLockName[0])) ){
18988    vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=%d(%s),n=%d,%s)",
18989                  pInfo->zVfsName, p->zFName, ofst, azLockName[ofst],
18990                  n, &zLck[1]);
18991  }else{
18992    vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=5d,n=%d,%s)",
18993                  pInfo->zVfsName, p->zFName, ofst,
18994                  n, &zLck[1]);
18995  }
18996  rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
18997  vfstrace_print_errcode(pInfo, " -> %s\n", rc);
18998  return rc;
18999}
19000static int vfstraceShmMap(
19001  sqlite3_file *pFile, 
19002  int iRegion, 
19003  int szRegion, 
19004  int isWrite, 
19005  void volatile **pp
19006){
19007  vfstrace_file *p = (vfstrace_file *)pFile;
19008  vfstrace_info *pInfo = p->pInfo;
19009  int rc;
19010  vfstraceOnOff(pInfo, VTR_SHMMAP);
19011  vfstrace_printf(pInfo, "%s.xShmMap(%s,iRegion=%d,szRegion=%d,isWrite=%d,*)",
19012                  pInfo->zVfsName, p->zFName, iRegion, szRegion, isWrite);
19013  rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
19014  vfstrace_print_errcode(pInfo, " -> %s\n", rc);
19015  return rc;
19016}
19017static void vfstraceShmBarrier(sqlite3_file *pFile){
19018  vfstrace_file *p = (vfstrace_file *)pFile;
19019  vfstrace_info *pInfo = p->pInfo;
19020  vfstraceOnOff(pInfo, VTR_SHMBAR);
19021  vfstrace_printf(pInfo, "%s.xShmBarrier(%s)\n", pInfo->zVfsName, p->zFName);
19022  p->pReal->pMethods->xShmBarrier(p->pReal);
19023}
19024static int vfstraceShmUnmap(sqlite3_file *pFile, int delFlag){
19025  vfstrace_file *p = (vfstrace_file *)pFile;
19026  vfstrace_info *pInfo = p->pInfo;
19027  int rc;
19028  vfstraceOnOff(pInfo, VTR_SHMUNMAP);
19029  vfstrace_printf(pInfo, "%s.xShmUnmap(%s,delFlag=%d)",
19030                  pInfo->zVfsName, p->zFName, delFlag);
19031  rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
19032  vfstrace_print_errcode(pInfo, " -> %s\n", rc);
19033  return rc;
19034}
19035static int vfstraceFetch(sqlite3_file *pFile, i64 iOff, int nAmt, void **pptr){
19036  vfstrace_file *p = (vfstrace_file *)pFile;
19037  vfstrace_info *pInfo = p->pInfo;
19038  int rc;
19039  vfstraceOnOff(pInfo, VTR_FETCH);
19040  vfstrace_printf(pInfo, "%s.xFetch(%s,iOff=%lld,nAmt=%d,p=%p)",
19041                  pInfo->zVfsName, p->zFName, iOff, nAmt, *pptr);
19042  rc = p->pReal->pMethods->xFetch(p->pReal, iOff, nAmt, pptr);
19043  vfstrace_print_errcode(pInfo, " -> %s\n", rc);
19044  return rc;
19045}
19046static int vfstraceUnfetch(sqlite3_file *pFile, i64 iOff, void *ptr){
19047  vfstrace_file *p = (vfstrace_file *)pFile;
19048  vfstrace_info *pInfo = p->pInfo;
19049  int rc;
19050  vfstraceOnOff(pInfo, VTR_FETCH);
19051  vfstrace_printf(pInfo, "%s.xUnfetch(%s,iOff=%lld,p=%p)",
19052                  pInfo->zVfsName, p->zFName, iOff, ptr);
19053  rc = p->pReal->pMethods->xUnfetch(p->pReal, iOff, ptr);
19054  vfstrace_print_errcode(pInfo, " -> %s\n", rc);
19055  return rc;
19056}
19057
19058
19059/*
19060** Open an vfstrace file handle.
19061*/
19062static int vfstraceOpen(
19063  sqlite3_vfs *pVfs,
19064  const char *zName,
19065  sqlite3_file *pFile,
19066  int flags,
19067  int *pOutFlags
19068){
19069  int rc;
19070  vfstrace_file *p = (vfstrace_file *)pFile;
19071  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19072  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19073  p->pInfo = pInfo;
19074  p->zFName = zName ? fileTail(zName) : "<temp>";
19075  p->pReal = (sqlite3_file *)&p[1];
19076  rc = pRoot->xOpen(pRoot, zName, p->pReal, flags, pOutFlags);
19077  vfstraceOnOff(pInfo, VTR_OPEN);
19078  vfstrace_printf(pInfo, "%s.xOpen(%s,flags=0x%x)",
19079                  pInfo->zVfsName, p->zFName, flags);
19080  if( p->pReal->pMethods ){
19081    sqlite3_io_methods *pNew = sqlite3_malloc64( sizeof(*pNew) );
19082    const sqlite3_io_methods *pSub = p->pReal->pMethods;
19083    memset(pNew, 0, sizeof(*pNew));
19084    pNew->iVersion = pSub->iVersion;
19085    pNew->xClose = vfstraceClose;
19086    pNew->xRead = vfstraceRead;
19087    pNew->xWrite = vfstraceWrite;
19088    pNew->xTruncate = vfstraceTruncate;
19089    pNew->xSync = vfstraceSync;
19090    pNew->xFileSize = vfstraceFileSize;
19091    pNew->xLock = vfstraceLock;
19092    pNew->xUnlock = vfstraceUnlock;
19093    pNew->xCheckReservedLock = vfstraceCheckReservedLock;
19094    pNew->xFileControl = vfstraceFileControl;
19095    pNew->xSectorSize = vfstraceSectorSize;
19096    pNew->xDeviceCharacteristics = vfstraceDeviceCharacteristics;
19097    if( pNew->iVersion>=2 ){
19098      pNew->xShmMap = pSub->xShmMap ? vfstraceShmMap : 0;
19099      pNew->xShmLock = pSub->xShmLock ? vfstraceShmLock : 0;
19100      pNew->xShmBarrier = pSub->xShmBarrier ? vfstraceShmBarrier : 0;
19101      pNew->xShmUnmap = pSub->xShmUnmap ? vfstraceShmUnmap : 0;
19102    }
19103    if( pNew->iVersion>=3 ){
19104      pNew->xFetch = pSub->xFetch ? vfstraceFetch : 0;
19105      pNew->xUnfetch = pSub->xUnfetch ? vfstraceUnfetch : 0;
19106    }
19107    pFile->pMethods = pNew;
19108  }
19109  vfstrace_print_errcode(pInfo, " -> %s", rc);
19110  if( pOutFlags ){
19111    vfstrace_printf(pInfo, ", outFlags=0x%x\n", *pOutFlags);
19112  }else{
19113    vfstrace_printf(pInfo, "\n");
19114  }
19115  return rc;
19116}
19117
19118/*
19119** Delete the file located at zPath. If the dirSync argument is true,
19120** ensure the file-system modifications are synced to disk before
19121** returning.
19122*/
19123static int vfstraceDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
19124  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19125  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19126  int rc;
19127  vfstraceOnOff(pInfo, VTR_DELETE);
19128  vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)",
19129                  pInfo->zVfsName, zPath, dirSync);
19130  rc = pRoot->xDelete(pRoot, zPath, dirSync);
19131  vfstrace_print_errcode(pInfo, " -> %s\n", rc);
19132  return rc;
19133}
19134
19135/*
19136** Test for access permissions. Return true if the requested permission
19137** is available, or false otherwise.
19138*/
19139static int vfstraceAccess(
19140  sqlite3_vfs *pVfs, 
19141  const char *zPath, 
19142  int flags, 
19143  int *pResOut
19144){
19145  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19146  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19147  int rc;
19148  vfstraceOnOff(pInfo, VTR_ACCESS);
19149  vfstrace_printf(pInfo, "%s.xAccess(\"%s\",%d)",
19150                  pInfo->zVfsName, zPath, flags);
19151  rc = pRoot->xAccess(pRoot, zPath, flags, pResOut);
19152  vfstrace_print_errcode(pInfo, " -> %s", rc);
19153  vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
19154  return rc;
19155}
19156
19157/*
19158** Populate buffer zOut with the full canonical pathname corresponding
19159** to the pathname in zPath. zOut is guaranteed to point to a buffer
19160** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
19161*/
19162static int vfstraceFullPathname(
19163  sqlite3_vfs *pVfs, 
19164  const char *zPath, 
19165  int nOut, 
19166  char *zOut
19167){
19168  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19169  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19170  int rc;
19171  vfstraceOnOff(pInfo, VTR_FULLPATH);
19172  vfstrace_printf(pInfo, "%s.xFullPathname(\"%s\")",
19173                  pInfo->zVfsName, zPath);
19174  rc = pRoot->xFullPathname(pRoot, zPath, nOut, zOut);
19175  vfstrace_print_errcode(pInfo, " -> %s", rc);
19176  vfstrace_printf(pInfo, ", out=\"%.*s\"\n", nOut, zOut);
19177  return rc;
19178}
19179
19180/*
19181** Open the dynamic library located at zPath and return a handle.
19182*/
19183static void *vfstraceDlOpen(sqlite3_vfs *pVfs, const char *zPath){
19184  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19185  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19186  vfstraceOnOff(pInfo, VTR_DLOPEN);
19187  vfstrace_printf(pInfo, "%s.xDlOpen(\"%s\")\n", pInfo->zVfsName, zPath);
19188  return pRoot->xDlOpen(pRoot, zPath);
19189}
19190
19191/*
19192** Populate the buffer zErrMsg (size nByte bytes) with a human readable
19193** utf-8 string describing the most recent error encountered associated 
19194** with dynamic libraries.
19195*/
19196static void vfstraceDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
19197  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19198  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19199  vfstraceOnOff(pInfo, VTR_DLERR);
19200  vfstrace_printf(pInfo, "%s.xDlError(%d)", pInfo->zVfsName, nByte);
19201  pRoot->xDlError(pRoot, nByte, zErrMsg);
19202  vfstrace_printf(pInfo, " -> \"%s\"", zErrMsg);
19203}
19204
19205/*
19206** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
19207*/
19208static void (*vfstraceDlSym(sqlite3_vfs *pVfs,void *p,const char *zSym))(void){
19209  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19210  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19211  vfstrace_printf(pInfo, "%s.xDlSym(\"%s\")\n", pInfo->zVfsName, zSym);
19212  return pRoot->xDlSym(pRoot, p, zSym);
19213}
19214
19215/*
19216** Close the dynamic library handle pHandle.
19217*/
19218static void vfstraceDlClose(sqlite3_vfs *pVfs, void *pHandle){
19219  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19220  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19221  vfstraceOnOff(pInfo, VTR_DLCLOSE);
19222  vfstrace_printf(pInfo, "%s.xDlClose()\n", pInfo->zVfsName);
19223  pRoot->xDlClose(pRoot, pHandle);
19224}
19225
19226/*
19227** Populate the buffer pointed to by zBufOut with nByte bytes of 
19228** random data.
19229*/
19230static int vfstraceRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
19231  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19232  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19233  vfstraceOnOff(pInfo, VTR_RAND);
19234  vfstrace_printf(pInfo, "%s.xRandomness(%d)\n", pInfo->zVfsName, nByte);
19235  return pRoot->xRandomness(pRoot, nByte, zBufOut);
19236}
19237
19238/*
19239** Sleep for nMicro microseconds. Return the number of microseconds 
19240** actually slept.
19241*/
19242static int vfstraceSleep(sqlite3_vfs *pVfs, int nMicro){
19243  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19244  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19245  vfstraceOnOff(pInfo, VTR_SLEEP);
19246  vfstrace_printf(pInfo, "%s.xSleep(%d)\n", pInfo->zVfsName, nMicro);
19247  return pRoot->xSleep(pRoot, nMicro);
19248}
19249
19250/*
19251** Return the current time as a Julian Day number in *pTimeOut.
19252*/
19253static int vfstraceCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
19254  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19255  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19256  int rc;
19257  vfstraceOnOff(pInfo, VTR_CURTIME);
19258  vfstrace_printf(pInfo, "%s.xCurrentTime()", pInfo->zVfsName);
19259  rc = pRoot->xCurrentTime(pRoot, pTimeOut);
19260  vfstrace_printf(pInfo, " -> %.17g\n", *pTimeOut);
19261  return rc;
19262}
19263static int vfstraceCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
19264  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19265  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19266  int rc;
19267  vfstraceOnOff(pInfo, VTR_CURTIME);
19268  vfstrace_printf(pInfo, "%s.xCurrentTimeInt64()", pInfo->zVfsName);
19269  rc = pRoot->xCurrentTimeInt64(pRoot, pTimeOut);
19270  vfstrace_printf(pInfo, " -> %lld\n", *pTimeOut);
19271  return rc;
19272}
19273
19274/*
19275** Return the most recent error code and message
19276*/
19277static int vfstraceGetLastError(sqlite3_vfs *pVfs, int nErr, char *zErr){
19278  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19279  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19280  int rc;
19281  vfstraceOnOff(pInfo, VTR_LASTERR);
19282  vfstrace_printf(pInfo, "%s.xGetLastError(%d,zBuf)", pInfo->zVfsName, nErr);
19283  if( nErr ) zErr[0] = 0;
19284  rc = pRoot->xGetLastError(pRoot, nErr, zErr);
19285  vfstrace_printf(pInfo, " -> zBuf[] = \"%s\", rc = %d\n", nErr?zErr:"", rc);
19286  return rc;
19287}
19288
19289/*
19290** Override system calls.
19291*/
19292static int vfstraceSetSystemCall(
19293  sqlite3_vfs *pVfs,
19294  const char *zName,
19295  sqlite3_syscall_ptr pFunc
19296){
19297  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19298  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19299  return pRoot->xSetSystemCall(pRoot, zName, pFunc);
19300}
19301static sqlite3_syscall_ptr vfstraceGetSystemCall(
19302  sqlite3_vfs *pVfs,
19303  const char *zName
19304){
19305  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19306  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19307  return pRoot->xGetSystemCall(pRoot, zName);
19308}
19309static const char *vfstraceNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
19310  vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
19311  sqlite3_vfs *pRoot = pInfo->pRootVfs;
19312  return pRoot->xNextSystemCall(pRoot, zName);
19313}
19314
19315
19316/*
19317** Clients invoke this routine to construct a new trace-vfs shim.
19318**
19319** Return SQLITE_OK on success.  
19320**
19321** SQLITE_NOMEM is returned in the case of a memory allocation error.
19322** SQLITE_NOTFOUND is returned if zOldVfsName does not exist.
19323*/
19324int vfstrace_register(
19325   const char *zTraceName,           /* Name of the newly constructed VFS */
19326   const char *zOldVfsName,          /* Name of the underlying VFS */
19327   int (*xOut)(const char*,void*),   /* Output routine.  ex: fputs */
19328   void *pOutArg,                    /* 2nd argument to xOut.  ex: stderr */
19329   int makeDefault                   /* True to make the new VFS the default */
19330){
19331  sqlite3_vfs *pNew;
19332  sqlite3_vfs *pRoot;
19333  vfstrace_info *pInfo;
19334  size_t nName;
19335  size_t nByte;
19336
19337  pRoot = sqlite3_vfs_find(zOldVfsName);
19338  if( pRoot==0 ) return SQLITE_NOTFOUND;
19339  nName = strlen(zTraceName);
19340  nByte = sizeof(*pNew) + sizeof(*pInfo) + nName + 1;
19341  pNew = sqlite3_malloc64( nByte );
19342  if( pNew==0 ) return SQLITE_NOMEM;
19343  memset(pNew, 0, nByte);
19344  pInfo = (vfstrace_info*)&pNew[1];
19345  pNew->iVersion = pRoot->iVersion;
19346  pNew->szOsFile = pRoot->szOsFile + sizeof(vfstrace_file);
19347  pNew->mxPathname = pRoot->mxPathname;
19348  pNew->zName = (char*)&pInfo[1];
19349  memcpy((char*)&pInfo[1], zTraceName, nName+1);
19350  pNew->pAppData = pInfo;
19351  pNew->xOpen = vfstraceOpen;
19352  pNew->xDelete = vfstraceDelete;
19353  pNew->xAccess = vfstraceAccess;
19354  pNew->xFullPathname = vfstraceFullPathname;
19355  pNew->xDlOpen = pRoot->xDlOpen==0 ? 0 : vfstraceDlOpen;
19356  pNew->xDlError = pRoot->xDlError==0 ? 0 : vfstraceDlError;
19357  pNew->xDlSym = pRoot->xDlSym==0 ? 0 : vfstraceDlSym;
19358  pNew->xDlClose = pRoot->xDlClose==0 ? 0 : vfstraceDlClose;
19359  pNew->xRandomness = vfstraceRandomness;
19360  pNew->xSleep = vfstraceSleep;
19361  pNew->xCurrentTime = vfstraceCurrentTime;
19362  pNew->xGetLastError = pRoot->xGetLastError==0 ? 0 : vfstraceGetLastError;
19363  if( pNew->iVersion>=2 ){
19364    pNew->xCurrentTimeInt64 = pRoot->xCurrentTimeInt64==0 ? 0 :
19365                                   vfstraceCurrentTimeInt64;
19366    if( pNew->iVersion>=3 ){
19367      pNew->xSetSystemCall = pRoot->xSetSystemCall==0 ? 0 : 
19368                                   vfstraceSetSystemCall;
19369      pNew->xGetSystemCall = pRoot->xGetSystemCall==0 ? 0 : 
19370                                   vfstraceGetSystemCall;
19371      pNew->xNextSystemCall = pRoot->xNextSystemCall==0 ? 0 : 
19372                                   vfstraceNextSystemCall;
19373    }
19374  }
19375  pInfo->pRootVfs = pRoot;
19376  pInfo->xOut = xOut;
19377  pInfo->pOutArg = pOutArg;
19378  pInfo->zVfsName = pNew->zName;
19379  pInfo->pTraceVfs = pNew;
19380  pInfo->mTrace = 0xffffffff;
19381  pInfo->bOn = 1;
19382  vfstrace_printf(pInfo, "%s.enabled_for(\"%s\")\n",
19383       pInfo->zVfsName, pRoot->zName);
19384  return sqlite3_vfs_register(pNew, makeDefault);
19385}
19386
19387/*
19388** Look for the named VFS.  If it is a TRACEVFS, then unregister it
19389** and delete it.
19390*/
19391void vfstrace_unregister(const char *zTraceName){
19392  sqlite3_vfs *pVfs = sqlite3_vfs_find(zTraceName);
19393  if( pVfs==0 ) return;
19394  if( pVfs->xOpen!=vfstraceOpen ) return;
19395  sqlite3_vfs_unregister(pVfs);
19396  sqlite3_free(pVfs);
19397}
19398
19399/************************* End ext/misc/vfstrace.c ********************/
19400
19401#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
19402#define SQLITE_SHELL_HAVE_RECOVER 1
19403#else
19404#define SQLITE_SHELL_HAVE_RECOVER 0
19405#endif
19406#if SQLITE_SHELL_HAVE_RECOVER
19407/************************* Begin ext/recover/sqlite3recover.h ******************/
19408/*
19409** 2022-08-27
19410**
19411** The author disclaims copyright to this source code.  In place of
19412** a legal notice, here is a blessing:
19413**
19414**    May you do good and not evil.
19415**    May you find forgiveness for yourself and forgive others.
19416**    May you share freely, never taking more than you give.
19417**
19418*************************************************************************
19419**
19420** This file contains the public interface to the "recover" extension -
19421** an SQLite extension designed to recover data from corrupted database
19422** files.
19423*/
19424
19425/*
19426** OVERVIEW:
19427**
19428** To use the API to recover data from a corrupted database, an
19429** application:
19430**
19431**   1) Creates an sqlite3_recover handle by calling either
19432**      sqlite3_recover_init() or sqlite3_recover_init_sql().
19433**
19434**   2) Configures the new handle using one or more calls to
19435**      sqlite3_recover_config().
19436**
19437**   3) Executes the recovery by repeatedly calling sqlite3_recover_step() on
19438**      the handle until it returns something other than SQLITE_OK. If it
19439**      returns SQLITE_DONE, then the recovery operation completed without 
19440**      error. If it returns some other non-SQLITE_OK value, then an error 
19441**      has occurred.
19442**
19443**   4) Retrieves any error code and English language error message using the
19444**      sqlite3_recover_errcode() and sqlite3_recover_errmsg() APIs,
19445**      respectively.
19446**
19447**   5) Destroys the sqlite3_recover handle and frees all resources
19448**      using sqlite3_recover_finish().
19449**
19450** The application may abandon the recovery operation at any point 
19451** before it is finished by passing the sqlite3_recover handle to
19452** sqlite3_recover_finish(). This is not an error, but the final state
19453** of the output database, or the results of running the partial script
19454** delivered to the SQL callback, are undefined.
19455*/
19456
19457#ifndef _SQLITE_RECOVER_H
19458#define _SQLITE_RECOVER_H
19459
19460/* #include "sqlite3.h" */
19461
19462#ifdef __cplusplus
19463extern "C" {
19464#endif
19465
19466/*
19467** An instance of the sqlite3_recover object represents a recovery
19468** operation in progress.
19469**
19470** Constructors:
19471**
19472**    sqlite3_recover_init()
19473**    sqlite3_recover_init_sql()
19474**
19475** Destructor:
19476**
19477**    sqlite3_recover_finish()
19478**
19479** Methods:
19480**
19481**    sqlite3_recover_config()
19482**    sqlite3_recover_errcode()
19483**    sqlite3_recover_errmsg()
19484**    sqlite3_recover_run()
19485**    sqlite3_recover_step()
19486*/
19487typedef struct sqlite3_recover sqlite3_recover;
19488
19489/* 
19490** These two APIs attempt to create and return a new sqlite3_recover object.
19491** In both cases the first two arguments identify the (possibly
19492** corrupt) database to recover data from. The first argument is an open
19493** database handle and the second the name of a database attached to that
19494** handle (i.e. "main", "temp" or the name of an attached database).
19495**
19496** If sqlite3_recover_init() is used to create the new sqlite3_recover
19497** handle, then data is recovered into a new database, identified by
19498** string parameter zUri. zUri may be an absolute or relative file path,
19499** or may be an SQLite URI. If the identified database file already exists,
19500** it is overwritten.
19501**
19502** If sqlite3_recover_init_sql() is invoked, then any recovered data will
19503** be returned to the user as a series of SQL statements. Executing these
19504** SQL statements results in the same database as would have been created
19505** had sqlite3_recover_init() been used. For each SQL statement in the
19506** output, the callback function passed as the third argument (xSql) is 
19507** invoked once. The first parameter is a passed a copy of the fourth argument
19508** to this function (pCtx) as its first parameter, and a pointer to a
19509** nul-terminated buffer containing the SQL statement formated as UTF-8 as 
19510** the second. If the xSql callback returns any value other than SQLITE_OK,
19511** then processing is immediately abandoned and the value returned used as
19512** the recover handle error code (see below).
19513**
19514** If an out-of-memory error occurs, NULL may be returned instead of
19515** a valid handle. In all other cases, it is the responsibility of the
19516** application to avoid resource leaks by ensuring that
19517** sqlite3_recover_finish() is called on all allocated handles.
19518*/
19519sqlite3_recover *sqlite3_recover_init(
19520  sqlite3* db, 
19521  const char *zDb, 
19522  const char *zUri
19523);
19524sqlite3_recover *sqlite3_recover_init_sql(
19525  sqlite3* db, 
19526  const char *zDb, 
19527  int (*xSql)(void*, const char*),
19528  void *pCtx
19529);
19530
19531/*
19532** Configure an sqlite3_recover object that has just been created using
19533** sqlite3_recover_init() or sqlite3_recover_init_sql(). This function
19534** may only be called before the first call to sqlite3_recover_step()
19535** or sqlite3_recover_run() on the object.
19536**
19537** The second argument passed to this function must be one of the
19538** SQLITE_RECOVER_* symbols defined below. Valid values for the third argument
19539** depend on the specific SQLITE_RECOVER_* symbol in use.
19540**
19541** SQLITE_OK is returned if the configuration operation was successful,
19542** or an SQLite error code otherwise.
19543*/
19544int sqlite3_recover_config(sqlite3_recover*, int op, void *pArg);
19545
19546/*
19547** SQLITE_RECOVER_LOST_AND_FOUND:
19548**   The pArg argument points to a string buffer containing the name
19549**   of a "lost-and-found" table in the output database, or NULL. If
19550**   the argument is non-NULL and the database contains seemingly
19551**   valid pages that cannot be associated with any table in the
19552**   recovered part of the schema, data is extracted from these
19553**   pages to add to the lost-and-found table.
19554**
19555** SQLITE_RECOVER_FREELIST_CORRUPT:
19556**   The pArg value must actually be a pointer to a value of type
19557**   int containing value 0 or 1 cast as a (void*). If this option is set
19558**   (argument is 1) and a lost-and-found table has been configured using
19559**   SQLITE_RECOVER_LOST_AND_FOUND, then is assumed that the freelist is 
19560**   corrupt and an attempt is made to recover records from pages that
19561**   appear to be linked into the freelist. Otherwise, pages on the freelist
19562**   are ignored. Setting this option can recover more data from the
19563**   database, but often ends up "recovering" deleted records. The default 
19564**   value is 0 (clear).
19565**
19566** SQLITE_RECOVER_ROWIDS:
19567**   The pArg value must actually be a pointer to a value of type
19568**   int containing value 0 or 1 cast as a (void*). If this option is set
19569**   (argument is 1), then an attempt is made to recover rowid values
19570**   that are not also INTEGER PRIMARY KEY values. If this option is
19571**   clear, then new rowids are assigned to all recovered rows. The
19572**   default value is 1 (set).
19573**
19574** SQLITE_RECOVER_SLOWINDEXES:
19575**   The pArg value must actually be a pointer to a value of type
19576**   int containing value 0 or 1 cast as a (void*). If this option is clear
19577**   (argument is 0), then when creating an output database, the recover 
19578**   module creates and populates non-UNIQUE indexes right at the end of the
19579**   recovery operation - after all recoverable data has been inserted
19580**   into the new database. This is faster overall, but means that the
19581**   final call to sqlite3_recover_step() for a recovery operation may
19582**   be need to create a large number of indexes, which may be very slow.
19583**
19584**   Or, if this option is set (argument is 1), then non-UNIQUE indexes
19585**   are created in the output database before it is populated with 
19586**   recovered data. This is slower overall, but avoids the slow call
19587**   to sqlite3_recover_step() at the end of the recovery operation.
19588**
19589**   The default option value is 0.
19590*/
19591#define SQLITE_RECOVER_LOST_AND_FOUND   1
19592#define SQLITE_RECOVER_FREELIST_CORRUPT 2
19593#define SQLITE_RECOVER_ROWIDS           3
19594#define SQLITE_RECOVER_SLOWINDEXES      4
19595
19596/*
19597** Perform a unit of work towards the recovery operation. This function 
19598** must normally be called multiple times to complete database recovery.
19599**
19600** If no error occurs but the recovery operation is not completed, this
19601** function returns SQLITE_OK. If recovery has been completed successfully
19602** then SQLITE_DONE is returned. If an error has occurred, then an SQLite
19603** error code (e.g. SQLITE_IOERR or SQLITE_NOMEM) is returned. It is not
19604** considered an error if some or all of the data cannot be recovered
19605** due to database corruption.
19606**
19607** Once sqlite3_recover_step() has returned a value other than SQLITE_OK,
19608** all further such calls on the same recover handle are no-ops that return
19609** the same non-SQLITE_OK value.
19610*/
19611int sqlite3_recover_step(sqlite3_recover*);
19612
19613/* 
19614** Run the recovery operation to completion. Return SQLITE_OK if successful,
19615** or an SQLite error code otherwise. Calling this function is the same
19616** as executing:
19617**
19618**     while( SQLITE_OK==sqlite3_recover_step(p) );
19619**     return sqlite3_recover_errcode(p);
19620*/
19621int sqlite3_recover_run(sqlite3_recover*);
19622
19623/*
19624** If an error has been encountered during a prior call to
19625** sqlite3_recover_step(), then this function attempts to return a 
19626** pointer to a buffer containing an English language explanation of 
19627** the error. If no error message is available, or if an out-of memory 
19628** error occurs while attempting to allocate a buffer in which to format
19629** the error message, NULL is returned.
19630**
19631** The returned buffer remains valid until the sqlite3_recover handle is
19632** destroyed using sqlite3_recover_finish().
19633*/
19634const char *sqlite3_recover_errmsg(sqlite3_recover*);
19635
19636/*
19637** If this function is called on an sqlite3_recover handle after
19638** an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK.
19639*/
19640int sqlite3_recover_errcode(sqlite3_recover*);
19641
19642/* 
19643** Clean up a recovery object created by a call to sqlite3_recover_init().
19644** The results of using a recovery object with any API after it has been
19645** passed to this function are undefined.
19646**
19647** This function returns the same value as sqlite3_recover_errcode().
19648*/
19649int sqlite3_recover_finish(sqlite3_recover*);
19650
19651
19652#ifdef __cplusplus
19653}  /* end of the 'extern "C"' block */
19654#endif
19655
19656#endif /* ifndef _SQLITE_RECOVER_H */
19657
19658/************************* End ext/recover/sqlite3recover.h ********************/
19659# ifndef SQLITE_HAVE_SQLITE3R
19660/************************* Begin ext/recover/dbdata.c ******************/
19661/*
19662** 2019-04-17
19663**
19664** The author disclaims copyright to this source code.  In place of
19665** a legal notice, here is a blessing:
19666**
19667**    May you do good and not evil.
19668**    May you find forgiveness for yourself and forgive others.
19669**    May you share freely, never taking more than you give.
19670**
19671******************************************************************************
19672**
19673** This file contains an implementation of two eponymous virtual tables,
19674** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
19675** "sqlite_dbpage" eponymous virtual table be available.
19676**
19677** SQLITE_DBDATA:
19678**   sqlite_dbdata is used to extract data directly from a database b-tree
19679**   page and its associated overflow pages, bypassing the b-tree layer.
19680**   The table schema is equivalent to:
19681**
19682**     CREATE TABLE sqlite_dbdata(
19683**       pgno INTEGER,
19684**       cell INTEGER,
19685**       field INTEGER,
19686**       value ANY,
19687**       schema TEXT HIDDEN
19688**     );
19689**
19690**   IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
19691**   FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
19692**   "schema".
19693**
19694**   Each page of the database is inspected. If it cannot be interpreted as
19695**   a b-tree page, or if it is a b-tree page containing 0 entries, the
19696**   sqlite_dbdata table contains no rows for that page.  Otherwise, the
19697**   table contains one row for each field in the record associated with
19698**   each cell on the page. For intkey b-trees, the key value is stored in
19699**   field -1.
19700**
19701**   For example, for the database:
19702**
19703**     CREATE TABLE t1(a, b);     -- root page is page 2
19704**     INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
19705**     INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
19706**
19707**   the sqlite_dbdata table contains, as well as from entries related to 
19708**   page 1, content equivalent to:
19709**
19710**     INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
19711**         (2, 0, -1, 5     ),
19712**         (2, 0,  0, 'v'   ),
19713**         (2, 0,  1, 'five'),
19714**         (2, 1, -1, 10    ),
19715**         (2, 1,  0, 'x'   ),
19716**         (2, 1,  1, 'ten' );
19717**
19718**   If database corruption is encountered, this module does not report an
19719**   error. Instead, it attempts to extract as much data as possible and
19720**   ignores the corruption.
19721**
19722** SQLITE_DBPTR:
19723**   The sqlite_dbptr table has the following schema:
19724**
19725**     CREATE TABLE sqlite_dbptr(
19726**       pgno INTEGER,
19727**       child INTEGER,
19728**       schema TEXT HIDDEN
19729**     );
19730**
19731**   It contains one entry for each b-tree pointer between a parent and
19732**   child page in the database.
19733*/
19734
19735#if !defined(SQLITEINT_H) 
19736/* #include "sqlite3.h" */
19737
19738/* typedef unsigned char u8; */
19739/* typedef unsigned int u32; */
19740
19741#endif
19742#include <string.h>
19743#include <assert.h>
19744
19745#ifndef SQLITE_OMIT_VIRTUALTABLE
19746
19747#define DBDATA_PADDING_BYTES 100 
19748
19749typedef struct DbdataTable DbdataTable;
19750typedef struct DbdataCursor DbdataCursor;
19751typedef struct DbdataBuffer DbdataBuffer;
19752
19753/*
19754** Buffer type.
19755*/
19756struct DbdataBuffer {
19757  u8 *aBuf;
19758  sqlite3_int64 nBuf;
19759};
19760
19761/* Cursor object */
19762struct DbdataCursor {
19763  sqlite3_vtab_cursor base;       /* Base class.  Must be first */
19764  sqlite3_stmt *pStmt;            /* For fetching database pages */
19765
19766  int iPgno;                      /* Current page number */
19767  u8 *aPage;                      /* Buffer containing page */
19768  int nPage;                      /* Size of aPage[] in bytes */
19769  int nCell;                      /* Number of cells on aPage[] */
19770  int iCell;                      /* Current cell number */
19771  int bOnePage;                   /* True to stop after one page */
19772  int szDb;
19773  sqlite3_int64 iRowid;
19774
19775  /* Only for the sqlite_dbdata table */
19776  DbdataBuffer rec;
19777  sqlite3_int64 nRec;             /* Size of pRec[] in bytes */
19778  sqlite3_int64 nHdr;             /* Size of header in bytes */
19779  int iField;                     /* Current field number */
19780  u8 *pHdrPtr;
19781  u8 *pPtr;
19782  u32 enc;                        /* Text encoding */
19783  
19784  sqlite3_int64 iIntkey;          /* Integer key value */
19785};
19786
19787/* Table object */
19788struct DbdataTable {
19789  sqlite3_vtab base;              /* Base class.  Must be first */
19790  sqlite3 *db;                    /* The database connection */
19791  sqlite3_stmt *pStmt;            /* For fetching database pages */
19792  int bPtr;                       /* True for sqlite3_dbptr table */
19793};
19794
19795/* Column and schema definitions for sqlite_dbdata */
19796#define DBDATA_COLUMN_PGNO        0
19797#define DBDATA_COLUMN_CELL        1
19798#define DBDATA_COLUMN_FIELD       2
19799#define DBDATA_COLUMN_VALUE       3
19800#define DBDATA_COLUMN_SCHEMA      4
19801#define DBDATA_SCHEMA             \
19802      "CREATE TABLE x("           \
19803      "  pgno INTEGER,"           \
19804      "  cell INTEGER,"           \
19805      "  field INTEGER,"          \
19806      "  value ANY,"              \
19807      "  schema TEXT HIDDEN"      \
19808      ")"
19809
19810/* Column and schema definitions for sqlite_dbptr */
19811#define DBPTR_COLUMN_PGNO         0
19812#define DBPTR_COLUMN_CHILD        1
19813#define DBPTR_COLUMN_SCHEMA       2
19814#define DBPTR_SCHEMA              \
19815      "CREATE TABLE x("           \
19816      "  pgno INTEGER,"           \
19817      "  child INTEGER,"          \
19818      "  schema TEXT HIDDEN"      \
19819      ")"
19820
19821/*
19822** Ensure the buffer passed as the first argument is at least nMin bytes
19823** in size. If an error occurs while attempting to resize the buffer,
19824** SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
19825*/
19826static int dbdataBufferSize(DbdataBuffer *pBuf, sqlite3_int64 nMin){
19827  if( nMin>pBuf->nBuf ){
19828    sqlite3_int64 nNew = nMin+16384;
19829    u8 *aNew = (u8*)sqlite3_realloc64(pBuf->aBuf, nNew);
19830
19831    if( aNew==0 ) return SQLITE_NOMEM;
19832    pBuf->aBuf = aNew;
19833    pBuf->nBuf = nNew;
19834  }
19835  return SQLITE_OK;
19836}
19837
19838/*
19839** Release the allocation managed by buffer pBuf.
19840*/
19841static void dbdataBufferFree(DbdataBuffer *pBuf){
19842  sqlite3_free(pBuf->aBuf);
19843  memset(pBuf, 0, sizeof(*pBuf));
19844}
19845
19846/*
19847** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual 
19848** table.
19849*/
19850static int dbdataConnect(
19851  sqlite3 *db,
19852  void *pAux,
19853  int argc, const char *const*argv,
19854  sqlite3_vtab **ppVtab,
19855  char **pzErr
19856){
19857  DbdataTable *pTab = 0;
19858  int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
19859
19860  (void)argc;
19861  (void)argv;
19862  (void)pzErr;
19863  sqlite3_vtab_config(db, SQLITE_VTAB_USES_ALL_SCHEMAS);
19864  if( rc==SQLITE_OK ){
19865    pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
19866    if( pTab==0 ){
19867      rc = SQLITE_NOMEM;
19868    }else{
19869      memset(pTab, 0, sizeof(DbdataTable));
19870      pTab->db = db;
19871      pTab->bPtr = (pAux!=0);
19872    }
19873  }
19874
19875  *ppVtab = (sqlite3_vtab*)pTab;
19876  return rc;
19877}
19878
19879/*
19880** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
19881*/
19882static int dbdataDisconnect(sqlite3_vtab *pVtab){
19883  DbdataTable *pTab = (DbdataTable*)pVtab;
19884  if( pTab ){
19885    sqlite3_finalize(pTab->pStmt);
19886    sqlite3_free(pVtab);
19887  }
19888  return SQLITE_OK;
19889}
19890
19891/*
19892** This function interprets two types of constraints:
19893**
19894**       schema=?
19895**       pgno=?
19896**
19897** If neither are present, idxNum is set to 0. If schema=? is present,
19898** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
19899** in idxNum is set.
19900**
19901** If both parameters are present, schema is in position 0 and pgno in
19902** position 1.
19903*/
19904static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
19905  DbdataTable *pTab = (DbdataTable*)tab;
19906  int i;
19907  int iSchema = -1;
19908  int iPgno = -1;
19909  int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
19910
19911  for(i=0; i<pIdx->nConstraint; i++){
19912    struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
19913    if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
19914      if( p->iColumn==colSchema ){
19915        if( p->usable==0 ) return SQLITE_CONSTRAINT;
19916        iSchema = i;
19917      }
19918      if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
19919        iPgno = i;
19920      }
19921    }
19922  }
19923
19924  if( iSchema>=0 ){
19925    pIdx->aConstraintUsage[iSchema].argvIndex = 1;
19926    pIdx->aConstraintUsage[iSchema].omit = 1;
19927  }
19928  if( iPgno>=0 ){
19929    pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
19930    pIdx->aConstraintUsage[iPgno].omit = 1;
19931    pIdx->estimatedCost = 100;
19932    pIdx->estimatedRows =  50;
19933
19934    if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
19935      int iCol = pIdx->aOrderBy[0].iColumn;
19936      if( pIdx->nOrderBy==1 ){
19937        pIdx->orderByConsumed = (iCol==0 || iCol==1);
19938      }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
19939        pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
19940      }
19941    }
19942
19943  }else{
19944    pIdx->estimatedCost = 100000000;
19945    pIdx->estimatedRows = 1000000000;
19946  }
19947  pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
19948  return SQLITE_OK;
19949}
19950
19951/*
19952** Open a new sqlite_dbdata or sqlite_dbptr cursor.
19953*/
19954static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
19955  DbdataCursor *pCsr;
19956
19957  pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
19958  if( pCsr==0 ){
19959    return SQLITE_NOMEM;
19960  }else{
19961    memset(pCsr, 0, sizeof(DbdataCursor));
19962    pCsr->base.pVtab = pVTab;
19963  }
19964
19965  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
19966  return SQLITE_OK;
19967}
19968
19969/*
19970** Restore a cursor object to the state it was in when first allocated 
19971** by dbdataOpen().
19972*/
19973static void dbdataResetCursor(DbdataCursor *pCsr){
19974  DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
19975  if( pTab->pStmt==0 ){
19976    pTab->pStmt = pCsr->pStmt;
19977  }else{
19978    sqlite3_finalize(pCsr->pStmt);
19979  }
19980  pCsr->pStmt = 0;
19981  pCsr->iPgno = 1;
19982  pCsr->iCell = 0;
19983  pCsr->iField = 0;
19984  pCsr->bOnePage = 0;
19985  sqlite3_free(pCsr->aPage);
19986  dbdataBufferFree(&pCsr->rec);
19987  pCsr->aPage = 0;
19988  pCsr->nRec = 0;
19989}
19990
19991/*
19992** Close an sqlite_dbdata or sqlite_dbptr cursor.
19993*/
19994static int dbdataClose(sqlite3_vtab_cursor *pCursor){
19995  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
19996  dbdataResetCursor(pCsr);
19997  sqlite3_free(pCsr);
19998  return SQLITE_OK;
19999}
20000
20001/* 
20002** Utility methods to decode 16 and 32-bit big-endian unsigned integers. 
20003*/
20004static u32 get_uint16(unsigned char *a){
20005  return (a[0]<<8)|a[1];
20006}
20007static u32 get_uint32(unsigned char *a){
20008  return ((u32)a[0]<<24)
20009       | ((u32)a[1]<<16)
20010       | ((u32)a[2]<<8)
20011       | ((u32)a[3]);
20012}
20013
20014/*
20015** Load page pgno from the database via the sqlite_dbpage virtual table.
20016** If successful, set (*ppPage) to point to a buffer containing the page
20017** data, (*pnPage) to the size of that buffer in bytes and return
20018** SQLITE_OK. In this case it is the responsibility of the caller to
20019** eventually free the buffer using sqlite3_free().
20020**
20021** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
20022** return an SQLite error code.
20023*/
20024static int dbdataLoadPage(
20025  DbdataCursor *pCsr,             /* Cursor object */
20026  u32 pgno,                       /* Page number of page to load */
20027  u8 **ppPage,                    /* OUT: pointer to page buffer */
20028  int *pnPage                     /* OUT: Size of (*ppPage) in bytes */
20029){
20030  int rc2;
20031  int rc = SQLITE_OK;
20032  sqlite3_stmt *pStmt = pCsr->pStmt;
20033
20034  *ppPage = 0;
20035  *pnPage = 0;
20036  if( pgno>0 ){
20037    sqlite3_bind_int64(pStmt, 2, pgno);
20038    if( SQLITE_ROW==sqlite3_step(pStmt) ){
20039      int nCopy = sqlite3_column_bytes(pStmt, 0);
20040      if( nCopy>0 ){
20041        u8 *pPage;
20042        pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
20043        if( pPage==0 ){
20044          rc = SQLITE_NOMEM;
20045        }else{
20046          const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
20047          memcpy(pPage, pCopy, nCopy);
20048          memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
20049        }
20050        *ppPage = pPage;
20051        *pnPage = nCopy;
20052      }
20053    }
20054    rc2 = sqlite3_reset(pStmt);
20055    if( rc==SQLITE_OK ) rc = rc2;
20056  }
20057
20058  return rc;
20059}
20060
20061/*
20062** Read a varint.  Put the value in *pVal and return the number of bytes.
20063*/
20064static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
20065  sqlite3_uint64 u = 0;
20066  int i;
20067  for(i=0; i<8; i++){
20068    u = (u<<7) + (z[i]&0x7f);
20069    if( (z[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; }
20070  }
20071  u = (u<<8) + (z[i]&0xff);
20072  *pVal = (sqlite3_int64)u;
20073  return 9;
20074}
20075
20076/*
20077** Like dbdataGetVarint(), but set the output to 0 if it is less than 0
20078** or greater than 0xFFFFFFFF. This can be used for all varints in an
20079** SQLite database except for key values in intkey tables.
20080*/
20081static int dbdataGetVarintU32(const u8 *z, sqlite3_int64 *pVal){
20082  sqlite3_int64 val;
20083  int nRet = dbdataGetVarint(z, &val);
20084  if( val<0 || val>0xFFFFFFFF ) val = 0;
20085  *pVal = val;
20086  return nRet;
20087}
20088
20089/*
20090** Return the number of bytes of space used by an SQLite value of type
20091** eType.
20092*/
20093static int dbdataValueBytes(int eType){
20094  switch( eType ){
20095    case 0: case 8: case 9:
20096    case 10: case 11:
20097      return 0;
20098    case 1:
20099      return 1;
20100    case 2:
20101      return 2;
20102    case 3:
20103      return 3;
20104    case 4:
20105      return 4;
20106    case 5:
20107      return 6;
20108    case 6:
20109    case 7:
20110      return 8;
20111    default:
20112      if( eType>0 ){
20113        return ((eType-12) / 2);
20114      }
20115      return 0;
20116  }
20117}
20118
20119/*
20120** Load a value of type eType from buffer pData and use it to set the
20121** result of context object pCtx.
20122*/
20123static void dbdataValue(
20124  sqlite3_context *pCtx, 
20125  u32 enc,
20126  int eType, 
20127  u8 *pData,
20128  sqlite3_int64 nData
20129){
20130  if( eType>=0 ){
20131    if( dbdataValueBytes(eType)<=nData ){
20132      switch( eType ){
20133        case 0: 
20134        case 10: 
20135        case 11: 
20136          sqlite3_result_null(pCtx);
20137          break;
20138        
20139        case 8: 
20140          sqlite3_result_int(pCtx, 0);
20141          break;
20142        case 9:
20143          sqlite3_result_int(pCtx, 1);
20144          break;
20145    
20146        case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
20147          sqlite3_uint64 v = (signed char)pData[0];
20148          pData++;
20149          switch( eType ){
20150            case 7:
20151            case 6:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
20152            case 5:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
20153            case 4:  v = (v<<8) + pData[0];  pData++;
20154            case 3:  v = (v<<8) + pData[0];  pData++;
20155            case 2:  v = (v<<8) + pData[0];  pData++;
20156          }
20157    
20158          if( eType==7 ){
20159            double r;
20160            memcpy(&r, &v, sizeof(r));
20161            sqlite3_result_double(pCtx, r);
20162          }else{
20163            sqlite3_result_int64(pCtx, (sqlite3_int64)v);
20164          }
20165          break;
20166        }
20167    
20168        default: {
20169          int n = ((eType-12) / 2);
20170          if( eType % 2 ){
20171            switch( enc ){
20172  #ifndef SQLITE_OMIT_UTF16
20173              case SQLITE_UTF16BE:
20174                sqlite3_result_text16be(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
20175                break;
20176              case SQLITE_UTF16LE:
20177                sqlite3_result_text16le(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
20178                break;
20179  #endif
20180              default:
20181                sqlite3_result_text(pCtx, (char*)pData, n, SQLITE_TRANSIENT);
20182                break;
20183            }
20184          }else{
20185            sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
20186          }
20187        }
20188      }
20189    }else{
20190      if( eType==7 ){
20191        sqlite3_result_double(pCtx, 0.0);
20192      }else if( eType<7 ){
20193        sqlite3_result_int(pCtx, 0);
20194      }else if( eType%2 ){
20195        sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
20196      }else{
20197        sqlite3_result_blob(pCtx, "", 0, SQLITE_STATIC);
20198      }
20199    }
20200  }
20201}
20202
20203/* This macro is a copy of the MX_CELL() macro in the SQLite core. Given
20204** a page-size, it returns the maximum number of cells that may be present
20205** on the page.  */
20206#define DBDATA_MX_CELL(pgsz) ((pgsz-8)/6)
20207
20208/* Maximum number of fields that may appear in a single record. This is
20209** the "hard-limit", according to comments in sqliteLimit.h. */
20210#define DBDATA_MX_FIELD 32676
20211
20212/*
20213** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
20214*/
20215static int dbdataNext(sqlite3_vtab_cursor *pCursor){
20216  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
20217  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
20218
20219  pCsr->iRowid++;
20220  while( 1 ){
20221    int rc;
20222    int iOff = (pCsr->iPgno==1 ? 100 : 0);
20223    int bNextPage = 0;
20224
20225    if( pCsr->aPage==0 ){
20226      while( 1 ){
20227        if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
20228        rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
20229        if( rc!=SQLITE_OK ) return rc;
20230        if( pCsr->aPage && pCsr->nPage>=256 ) break;
20231        sqlite3_free(pCsr->aPage);
20232        pCsr->aPage = 0;
20233        if( pCsr->bOnePage ) return SQLITE_OK;
20234        pCsr->iPgno++;
20235      }
20236
20237      assert( iOff+3+2<=pCsr->nPage );
20238      pCsr->iCell = pTab->bPtr ? -2 : 0;
20239      pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
20240      if( pCsr->nCell>DBDATA_MX_CELL(pCsr->nPage) ){
20241        pCsr->nCell = DBDATA_MX_CELL(pCsr->nPage);
20242      }
20243    }
20244
20245    if( pTab->bPtr ){
20246      if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
20247        pCsr->iCell = pCsr->nCell;
20248      }
20249      pCsr->iCell++;
20250      if( pCsr->iCell>=pCsr->nCell ){
20251        sqlite3_free(pCsr->aPage);
20252        pCsr->aPage = 0;
20253        if( pCsr->bOnePage ) return SQLITE_OK;
20254        pCsr->iPgno++;
20255      }else{
20256        return SQLITE_OK;
20257      }
20258    }else{
20259      /* If there is no record loaded, load it now. */
20260      assert( pCsr->rec.aBuf!=0 || pCsr->nRec==0 );
20261      if( pCsr->nRec==0 ){
20262        int bHasRowid = 0;
20263        int nPointer = 0;
20264        sqlite3_int64 nPayload = 0;
20265        sqlite3_int64 nHdr = 0;
20266        int iHdr;
20267        int U, X;
20268        int nLocal;
20269  
20270        switch( pCsr->aPage[iOff] ){
20271          case 0x02:
20272            nPointer = 4;
20273            break;
20274          case 0x0a:
20275            break;
20276          case 0x0d:
20277            bHasRowid = 1;
20278            break;
20279          default:
20280            /* This is not a b-tree page with records on it. Continue. */
20281            pCsr->iCell = pCsr->nCell;
20282            break;
20283        }
20284
20285        if( pCsr->iCell>=pCsr->nCell ){
20286          bNextPage = 1;
20287        }else{
20288          int iCellPtr = iOff + 8 + nPointer + pCsr->iCell*2;
20289  
20290          if( iCellPtr>pCsr->nPage ){
20291            bNextPage = 1;
20292          }else{
20293            iOff = get_uint16(&pCsr->aPage[iCellPtr]);
20294          }
20295    
20296          /* For an interior node cell, skip past the child-page number */
20297          iOff += nPointer;
20298    
20299          /* Load the "byte of payload including overflow" field */
20300          if( bNextPage || iOff>pCsr->nPage || iOff<=iCellPtr ){
20301            bNextPage = 1;
20302          }else{
20303            iOff += dbdataGetVarintU32(&pCsr->aPage[iOff], &nPayload);
20304            if( nPayload>0x7fffff00 ) nPayload &= 0x3fff;
20305            if( nPayload==0 ) nPayload = 1;
20306          }
20307    
20308          /* If this is a leaf intkey cell, load the rowid */
20309          if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
20310            iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
20311          }
20312    
20313          /* Figure out how much data to read from the local page */
20314          U = pCsr->nPage;
20315          if( bHasRowid ){
20316            X = U-35;
20317          }else{
20318            X = ((U-12)*64/255)-23;
20319          }
20320          if( nPayload<=X ){
20321            nLocal = nPayload;
20322          }else{
20323            int M, K;
20324            M = ((U-12)*32/255)-23;
20325            K = M+((nPayload-M)%(U-4));
20326            if( K<=X ){
20327              nLocal = K;
20328            }else{
20329              nLocal = M;
20330            }
20331          }
20332
20333          if( bNextPage || nLocal+iOff>pCsr->nPage ){
20334            bNextPage = 1;
20335          }else{
20336
20337            /* Allocate space for payload. And a bit more to catch small buffer
20338            ** overruns caused by attempting to read a varint or similar from 
20339            ** near the end of a corrupt record.  */
20340            rc = dbdataBufferSize(&pCsr->rec, nPayload+DBDATA_PADDING_BYTES);
20341            if( rc!=SQLITE_OK ) return rc;
20342            assert( pCsr->rec.aBuf!=0 );
20343            assert( nPayload!=0 );
20344
20345            /* Load the nLocal bytes of payload */
20346            memcpy(pCsr->rec.aBuf, &pCsr->aPage[iOff], nLocal);
20347            iOff += nLocal;
20348
20349            /* Load content from overflow pages */
20350            if( nPayload>nLocal ){
20351              sqlite3_int64 nRem = nPayload - nLocal;
20352              u32 pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
20353              while( nRem>0 ){
20354                u8 *aOvfl = 0;
20355                int nOvfl = 0;
20356                int nCopy;
20357                rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
20358                assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
20359                if( rc!=SQLITE_OK ) return rc;
20360                if( aOvfl==0 ) break;
20361
20362                nCopy = U-4;
20363                if( nCopy>nRem ) nCopy = nRem;
20364                memcpy(&pCsr->rec.aBuf[nPayload-nRem], &aOvfl[4], nCopy);
20365                nRem -= nCopy;
20366
20367                pgnoOvfl = get_uint32(aOvfl);
20368                sqlite3_free(aOvfl);
20369              }
20370              nPayload -= nRem;
20371            }
20372            memset(&pCsr->rec.aBuf[nPayload], 0, DBDATA_PADDING_BYTES);
20373            pCsr->nRec = nPayload;
20374    
20375            iHdr = dbdataGetVarintU32(pCsr->rec.aBuf, &nHdr);
20376            if( nHdr>nPayload ) nHdr = 0;
20377            pCsr->nHdr = nHdr;
20378            pCsr->pHdrPtr = &pCsr->rec.aBuf[iHdr];
20379            pCsr->pPtr = &pCsr->rec.aBuf[pCsr->nHdr];
20380            pCsr->iField = (bHasRowid ? -1 : 0);
20381          }
20382        }
20383      }else{
20384        pCsr->iField++;
20385        if( pCsr->iField>0 ){
20386          sqlite3_int64 iType;
20387          if( pCsr->pHdrPtr>=&pCsr->rec.aBuf[pCsr->nRec] 
20388           || pCsr->iField>=DBDATA_MX_FIELD
20389          ){
20390            bNextPage = 1;
20391          }else{
20392            int szField = 0;
20393            pCsr->pHdrPtr += dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
20394            szField = dbdataValueBytes(iType);
20395            if( (pCsr->nRec - (pCsr->pPtr - pCsr->rec.aBuf))<szField ){
20396              pCsr->pPtr = &pCsr->rec.aBuf[pCsr->nRec];
20397            }else{
20398              pCsr->pPtr += szField;
20399            }
20400          }
20401        }
20402      }
20403
20404      if( bNextPage ){
20405        sqlite3_free(pCsr->aPage);
20406        pCsr->aPage = 0;
20407        pCsr->nRec = 0;
20408        if( pCsr->bOnePage ) return SQLITE_OK;
20409        pCsr->iPgno++;
20410      }else{
20411        if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->rec.aBuf[pCsr->nHdr] ){
20412          return SQLITE_OK;
20413        }
20414
20415        /* Advance to the next cell. The next iteration of the loop will load
20416        ** the record and so on. */
20417        pCsr->nRec = 0;
20418        pCsr->iCell++;
20419      }
20420    }
20421  }
20422
20423  assert( !"can't get here" );
20424  return SQLITE_OK;
20425}
20426
20427/* 
20428** Return true if the cursor is at EOF.
20429*/
20430static int dbdataEof(sqlite3_vtab_cursor *pCursor){
20431  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
20432  return pCsr->aPage==0;
20433}
20434
20435/*
20436** Return true if nul-terminated string zSchema ends in "()". Or false
20437** otherwise.
20438*/
20439static int dbdataIsFunction(const char *zSchema){
20440  size_t n = strlen(zSchema);
20441  if( n>2 && zSchema[n-2]=='(' && zSchema[n-1]==')' ){
20442    return (int)n-2;
20443  }
20444  return 0;
20445}
20446
20447/* 
20448** Determine the size in pages of database zSchema (where zSchema is
20449** "main", "temp" or the name of an attached database) and set 
20450** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
20451** an SQLite error code.
20452*/
20453static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
20454  DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
20455  char *zSql = 0;
20456  int rc, rc2;
20457  int nFunc = 0;
20458  sqlite3_stmt *pStmt = 0;
20459
20460  if( (nFunc = dbdataIsFunction(zSchema))>0 ){
20461    zSql = sqlite3_mprintf("SELECT %.*s(0)", nFunc, zSchema);
20462  }else{
20463    zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
20464  }
20465  if( zSql==0 ) return SQLITE_NOMEM;
20466
20467  rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
20468  sqlite3_free(zSql);
20469  if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
20470    pCsr->szDb = sqlite3_column_int(pStmt, 0);
20471  }
20472  rc2 = sqlite3_finalize(pStmt);
20473  if( rc==SQLITE_OK ) rc = rc2;
20474  return rc;
20475}
20476
20477/*
20478** Attempt to figure out the encoding of the database by retrieving page 1
20479** and inspecting the header field. If successful, set the pCsr->enc variable
20480** and return SQLITE_OK. Otherwise, return an SQLite error code.
20481*/
20482static int dbdataGetEncoding(DbdataCursor *pCsr){
20483  int rc = SQLITE_OK;
20484  int nPg1 = 0;
20485  u8 *aPg1 = 0;
20486  rc = dbdataLoadPage(pCsr, 1, &aPg1, &nPg1);
20487  if( rc==SQLITE_OK && nPg1>=(56+4) ){
20488    pCsr->enc = get_uint32(&aPg1[56]);
20489  }
20490  sqlite3_free(aPg1);
20491  return rc;
20492}
20493
20494
20495/* 
20496** xFilter method for sqlite_dbdata and sqlite_dbptr.
20497*/
20498static int dbdataFilter(
20499  sqlite3_vtab_cursor *pCursor, 
20500  int idxNum, const char *idxStr,
20501  int argc, sqlite3_value **argv
20502){
20503  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
20504  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
20505  int rc = SQLITE_OK;
20506  const char *zSchema = "main";
20507  (void)idxStr;
20508  (void)argc;
20509
20510  dbdataResetCursor(pCsr);
20511  assert( pCsr->iPgno==1 );
20512  if( idxNum & 0x01 ){
20513    zSchema = (const char*)sqlite3_value_text(argv[0]);
20514    if( zSchema==0 ) zSchema = "";
20515  }
20516  if( idxNum & 0x02 ){
20517    pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
20518    pCsr->bOnePage = 1;
20519  }else{
20520    rc = dbdataDbsize(pCsr, zSchema);
20521  }
20522
20523  if( rc==SQLITE_OK ){
20524    int nFunc = 0;
20525    if( pTab->pStmt ){
20526      pCsr->pStmt = pTab->pStmt;
20527      pTab->pStmt = 0;
20528    }else if( (nFunc = dbdataIsFunction(zSchema))>0 ){
20529      char *zSql = sqlite3_mprintf("SELECT %.*s(?2)", nFunc, zSchema);
20530      if( zSql==0 ){
20531        rc = SQLITE_NOMEM;
20532      }else{
20533        rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
20534        sqlite3_free(zSql);
20535      }
20536    }else{
20537      rc = sqlite3_prepare_v2(pTab->db, 
20538          "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
20539          &pCsr->pStmt, 0
20540      );
20541    }
20542  }
20543  if( rc==SQLITE_OK ){
20544    rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
20545  }
20546
20547  /* Try to determine the encoding of the db by inspecting the header
20548  ** field on page 1. */
20549  if( rc==SQLITE_OK ){
20550    rc = dbdataGetEncoding(pCsr);
20551  }
20552
20553  if( rc!=SQLITE_OK ){
20554    pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
20555  }
20556
20557  if( rc==SQLITE_OK ){
20558    rc = dbdataNext(pCursor);
20559  }
20560  return rc;
20561}
20562
20563/*
20564** Return a column for the sqlite_dbdata or sqlite_dbptr table.
20565*/
20566static int dbdataColumn(
20567  sqlite3_vtab_cursor *pCursor, 
20568  sqlite3_context *ctx, 
20569  int i
20570){
20571  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
20572  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
20573  if( pTab->bPtr ){
20574    switch( i ){
20575      case DBPTR_COLUMN_PGNO:
20576        sqlite3_result_int64(ctx, pCsr->iPgno);
20577        break;
20578      case DBPTR_COLUMN_CHILD: {
20579        int iOff = pCsr->iPgno==1 ? 100 : 0;
20580        if( pCsr->iCell<0 ){
20581          iOff += 8;
20582        }else{
20583          iOff += 12 + pCsr->iCell*2;
20584          if( iOff>pCsr->nPage ) return SQLITE_OK;
20585          iOff = get_uint16(&pCsr->aPage[iOff]);
20586        }
20587        if( iOff<=pCsr->nPage ){
20588          sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
20589        }
20590        break;
20591      }
20592    }
20593  }else{
20594    switch( i ){
20595      case DBDATA_COLUMN_PGNO:
20596        sqlite3_result_int64(ctx, pCsr->iPgno);
20597        break;
20598      case DBDATA_COLUMN_CELL:
20599        sqlite3_result_int(ctx, pCsr->iCell);
20600        break;
20601      case DBDATA_COLUMN_FIELD:
20602        sqlite3_result_int(ctx, pCsr->iField);
20603        break;
20604      case DBDATA_COLUMN_VALUE: {
20605        if( pCsr->iField<0 ){
20606          sqlite3_result_int64(ctx, pCsr->iIntkey);
20607        }else if( &pCsr->rec.aBuf[pCsr->nRec] >= pCsr->pPtr ){
20608          sqlite3_int64 iType;
20609          dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
20610          dbdataValue(
20611              ctx, pCsr->enc, iType, pCsr->pPtr, 
20612              &pCsr->rec.aBuf[pCsr->nRec] - pCsr->pPtr
20613          );
20614        }
20615        break;
20616      }
20617    }
20618  }
20619  return SQLITE_OK;
20620}
20621
20622/* 
20623** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
20624*/
20625static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
20626  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
20627  *pRowid = pCsr->iRowid;
20628  return SQLITE_OK;
20629}
20630
20631
20632/*
20633** Invoke this routine to register the "sqlite_dbdata" virtual table module
20634*/
20635static int sqlite3DbdataRegister(sqlite3 *db){
20636  static sqlite3_module dbdata_module = {
20637    0,                            /* iVersion */
20638    0,                            /* xCreate */
20639    dbdataConnect,                /* xConnect */
20640    dbdataBestIndex,              /* xBestIndex */
20641    dbdataDisconnect,             /* xDisconnect */
20642    0,                            /* xDestroy */
20643    dbdataOpen,                   /* xOpen - open a cursor */
20644    dbdataClose,                  /* xClose - close a cursor */
20645    dbdataFilter,                 /* xFilter - configure scan constraints */
20646    dbdataNext,                   /* xNext - advance a cursor */
20647    dbdataEof,                    /* xEof - check for end of scan */
20648    dbdataColumn,                 /* xColumn - read data */
20649    dbdataRowid,                  /* xRowid - read data */
20650    0,                            /* xUpdate */
20651    0,                            /* xBegin */
20652    0,                            /* xSync */
20653    0,                            /* xCommit */
20654    0,                            /* xRollback */
20655    0,                            /* xFindMethod */
20656    0,                            /* xRename */
20657    0,                            /* xSavepoint */
20658    0,                            /* xRelease */
20659    0,                            /* xRollbackTo */
20660    0,                            /* xShadowName */
20661    0                             /* xIntegrity */
20662  };
20663
20664  int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
20665  if( rc==SQLITE_OK ){
20666    rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
20667  }
20668  return rc;
20669}
20670
20671#ifdef _WIN32
20672
20673#endif
20674int sqlite3_dbdata_init(
20675  sqlite3 *db, 
20676  char **pzErrMsg, 
20677  const sqlite3_api_routines *pApi
20678){
20679  (void)pzErrMsg;
20680  return sqlite3DbdataRegister(db);
20681}
20682
20683#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
20684
20685/************************* End ext/recover/dbdata.c ********************/
20686/************************* Begin ext/recover/sqlite3recover.c ******************/
20687/*
20688** 2022-08-27
20689**
20690** The author disclaims copyright to this source code.  In place of
20691** a legal notice, here is a blessing:
20692**
20693**    May you do good and not evil.
20694**    May you find forgiveness for yourself and forgive others.
20695**    May you share freely, never taking more than you give.
20696**
20697*************************************************************************
20698**
20699*/
20700
20701
20702/* #include "sqlite3recover.h" */
20703#include <assert.h>
20704#include <string.h>
20705
20706#ifndef SQLITE_OMIT_VIRTUALTABLE
20707
20708/*
20709** Declaration for public API function in file dbdata.c. This may be called
20710** with NULL as the final two arguments to register the sqlite_dbptr and
20711** sqlite_dbdata virtual tables with a database handle.
20712*/
20713#ifdef _WIN32
20714
20715#endif
20716int sqlite3_dbdata_init(sqlite3*, char**, const sqlite3_api_routines*);
20717
20718/* typedef unsigned int u32; */
20719/* typedef unsigned char u8; */
20720/* typedef sqlite3_int64 i64; */
20721
20722/*
20723** Work around C99 "flex-array" syntax for pre-C99 compilers, so as
20724** to avoid complaints from -fsanitize=strict-bounds.
20725*/
20726#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
20727# define FLEXARRAY
20728#else
20729# define FLEXARRAY 1
20730#endif
20731
20732typedef struct RecoverTable RecoverTable;
20733typedef struct RecoverColumn RecoverColumn;
20734
20735/*
20736** When recovering rows of data that can be associated with table
20737** definitions recovered from the sqlite_schema table, each table is
20738** represented by an instance of the following object.
20739**
20740** iRoot:
20741**   The root page in the original database. Not necessarily (and usually
20742**   not) the same in the recovered database.
20743**
20744** zTab:
20745**   Name of the table.
20746**
20747** nCol/aCol[]:
20748**   aCol[] is an array of nCol columns. In the order in which they appear 
20749**   in the table.
20750**
20751** bIntkey:
20752**   Set to true for intkey tables, false for WITHOUT ROWID.
20753**
20754** iRowidBind:
20755**   Each column in the aCol[] array has associated with it the index of
20756**   the bind parameter its values will be bound to in the INSERT statement
20757**   used to construct the output database. If the table does has a rowid
20758**   but not an INTEGER PRIMARY KEY column, then iRowidBind contains the
20759**   index of the bind paramater to which the rowid value should be bound.
20760**   Otherwise, it contains -1. If the table does contain an INTEGER PRIMARY 
20761**   KEY column, then the rowid value should be bound to the index associated
20762**   with the column.
20763**
20764** pNext:
20765**   All RecoverTable objects used by the recovery operation are allocated
20766**   and populated as part of creating the recovered database schema in
20767**   the output database, before any non-schema data are recovered. They
20768**   are then stored in a singly-linked list linked by this variable beginning
20769**   at sqlite3_recover.pTblList.
20770*/
20771struct RecoverTable {
20772  u32 iRoot;                      /* Root page in original database */
20773  char *zTab;                     /* Name of table */
20774  int nCol;                       /* Number of columns in table */
20775  RecoverColumn *aCol;            /* Array of columns */
20776  int bIntkey;                    /* True for intkey, false for without rowid */
20777  int iRowidBind;                 /* If >0, bind rowid to INSERT here */
20778  RecoverTable *pNext;
20779};
20780
20781/*
20782** Each database column is represented by an instance of the following object
20783** stored in the RecoverTable.aCol[] array of the associated table.
20784**
20785** iField:
20786**   The index of the associated field within database records. Or -1 if
20787**   there is no associated field (e.g. for virtual generated columns).
20788**
20789** iBind:
20790**   The bind index of the INSERT statement to bind this columns values
20791**   to. Or 0 if there is no such index (iff (iField<0)).
20792**
20793** bIPK:
20794**   True if this is the INTEGER PRIMARY KEY column.
20795**
20796** zCol:
20797**   Name of column.
20798**
20799** eHidden:
20800**   A RECOVER_EHIDDEN_* constant value (see below for interpretation of each).
20801*/
20802struct RecoverColumn {
20803  int iField;                     /* Field in record on disk */
20804  int iBind;                      /* Binding to use in INSERT */
20805  int bIPK;                       /* True for IPK column */
20806  char *zCol;
20807  int eHidden;
20808};
20809
20810#define RECOVER_EHIDDEN_NONE    0      /* Normal database column */
20811#define RECOVER_EHIDDEN_HIDDEN  1      /* Column is __HIDDEN__ */
20812#define RECOVER_EHIDDEN_VIRTUAL 2      /* Virtual generated column */
20813#define RECOVER_EHIDDEN_STORED  3      /* Stored generated column */
20814
20815/*
20816** Bitmap object used to track pages in the input database. Allocated
20817** and manipulated only by the following functions:
20818**
20819**     recoverBitmapAlloc()
20820**     recoverBitmapFree()
20821**     recoverBitmapSet()
20822**     recoverBitmapQuery()
20823**
20824** nPg:
20825**   Largest page number that may be stored in the bitmap. The range
20826**   of valid keys is 1 to nPg, inclusive.
20827**
20828** aElem[]:
20829**   Array large enough to contain a bit for each key. For key value
20830**   iKey, the associated bit is the bit (iKey%32) of aElem[iKey/32].
20831**   In other words, the following is true if bit iKey is set, or 
20832**   false if it is clear:
20833**
20834**       (aElem[iKey/32] & (1 << (iKey%32))) ? 1 : 0
20835*/
20836typedef struct RecoverBitmap RecoverBitmap;
20837struct RecoverBitmap {
20838  i64 nPg;                        /* Size of bitmap */
20839  u32 aElem[FLEXARRAY];           /* Array of 32-bit bitmasks */
20840};
20841
20842/* Size in bytes of a RecoverBitmap object sufficient to cover 32 pages */
20843#define SZ_RECOVERBITMAP_32  (16)
20844
20845/*
20846** State variables (part of the sqlite3_recover structure) used while
20847** recovering data for tables identified in the recovered schema (state
20848** RECOVER_STATE_WRITING).
20849*/
20850typedef struct RecoverStateW1 RecoverStateW1;
20851struct RecoverStateW1 {
20852  sqlite3_stmt *pTbls;
20853  sqlite3_stmt *pSel;
20854  sqlite3_stmt *pInsert;
20855  int nInsert;
20856
20857  RecoverTable *pTab;             /* Table currently being written */
20858  int nMax;                       /* Max column count in any schema table */
20859  sqlite3_value **apVal;          /* Array of nMax values */
20860  int nVal;                       /* Number of valid entries in apVal[] */
20861  int bHaveRowid;
20862  i64 iRowid;
20863  i64 iPrevPage;
20864  int iPrevCell;
20865};
20866
20867/*
20868** State variables (part of the sqlite3_recover structure) used while
20869** recovering data destined for the lost and found table (states
20870** RECOVER_STATE_LOSTANDFOUND[123]).
20871*/
20872typedef struct RecoverStateLAF RecoverStateLAF;
20873struct RecoverStateLAF {
20874  RecoverBitmap *pUsed;
20875  i64 nPg;                        /* Size of db in pages */
20876  sqlite3_stmt *pAllAndParent;
20877  sqlite3_stmt *pMapInsert;
20878  sqlite3_stmt *pMaxField;
20879  sqlite3_stmt *pUsedPages;
20880  sqlite3_stmt *pFindRoot;
20881  sqlite3_stmt *pInsert;          /* INSERT INTO lost_and_found ... */
20882  sqlite3_stmt *pAllPage;
20883  sqlite3_stmt *pPageData;
20884  sqlite3_value **apVal;
20885  int nMaxField;
20886};
20887
20888/*
20889** Main recover handle structure.
20890*/
20891struct sqlite3_recover {
20892  /* Copies of sqlite3_recover_init[_sql]() parameters */
20893  sqlite3 *dbIn;                  /* Input database */
20894  char *zDb;                      /* Name of input db ("main" etc.) */
20895  char *zUri;                     /* URI for output database */
20896  void *pSqlCtx;                  /* SQL callback context */
20897  int (*xSql)(void*,const char*); /* Pointer to SQL callback function */
20898
20899  /* Values configured by sqlite3_recover_config() */
20900  char *zStateDb;                 /* State database to use (or NULL) */
20901  char *zLostAndFound;            /* Name of lost-and-found table (or NULL) */
20902  int bFreelistCorrupt;           /* SQLITE_RECOVER_FREELIST_CORRUPT setting */
20903  int bRecoverRowid;              /* SQLITE_RECOVER_ROWIDS setting */
20904  int bSlowIndexes;               /* SQLITE_RECOVER_SLOWINDEXES setting */
20905
20906  int pgsz;
20907  int detected_pgsz;
20908  int nReserve;
20909  u8 *pPage1Disk;
20910  u8 *pPage1Cache;
20911
20912  /* Error code and error message */
20913  int errCode;                    /* For sqlite3_recover_errcode() */
20914  char *zErrMsg;                  /* For sqlite3_recover_errmsg() */
20915
20916  int eState;
20917  int bCloseTransaction;
20918
20919  /* Variables used with eState==RECOVER_STATE_WRITING */
20920  RecoverStateW1 w1;
20921
20922  /* Variables used with states RECOVER_STATE_LOSTANDFOUND[123] */
20923  RecoverStateLAF laf;
20924
20925  /* Fields used within sqlite3_recover_run() */
20926  sqlite3 *dbOut;                 /* Output database */
20927  sqlite3_stmt *pGetPage;         /* SELECT against input db sqlite_dbdata */
20928  RecoverTable *pTblList;         /* List of tables recovered from schema */
20929};
20930
20931/*
20932** The various states in which an sqlite3_recover object may exist:
20933**
20934**   RECOVER_STATE_INIT:
20935**    The object is initially created in this state. sqlite3_recover_step()
20936**    has yet to be called. This is the only state in which it is permitted
20937**    to call sqlite3_recover_config().
20938**
20939**   RECOVER_STATE_WRITING:
20940**
20941**   RECOVER_STATE_LOSTANDFOUND1:
20942**    State to populate the bitmap of pages used by other tables or the
20943**    database freelist.
20944**
20945**   RECOVER_STATE_LOSTANDFOUND2:
20946**    Populate the recovery.map table - used to figure out a "root" page
20947**    for each lost page from in the database from which records are
20948**    extracted.
20949**
20950**   RECOVER_STATE_LOSTANDFOUND3:
20951**    Populate the lost-and-found table itself.
20952*/
20953#define RECOVER_STATE_INIT           0
20954#define RECOVER_STATE_WRITING        1
20955#define RECOVER_STATE_LOSTANDFOUND1  2
20956#define RECOVER_STATE_LOSTANDFOUND2  3
20957#define RECOVER_STATE_LOSTANDFOUND3  4
20958#define RECOVER_STATE_SCHEMA2        5
20959#define RECOVER_STATE_DONE           6
20960
20961
20962/*
20963** Global variables used by this extension.
20964*/
20965typedef struct RecoverGlobal RecoverGlobal;
20966struct RecoverGlobal {
20967  const sqlite3_io_methods *pMethods;
20968  sqlite3_recover *p;
20969};
20970static RecoverGlobal recover_g;
20971
20972/*
20973** Use this static SQLite mutex to protect the globals during the
20974** first call to sqlite3_recover_step().
20975*/ 
20976#define RECOVER_MUTEX_ID SQLITE_MUTEX_STATIC_APP2
20977
20978
20979/* 
20980** Default value for SQLITE_RECOVER_ROWIDS (sqlite3_recover.bRecoverRowid).
20981*/
20982#define RECOVER_ROWID_DEFAULT 1
20983
20984/*
20985** Mutex handling:
20986**
20987**    recoverEnterMutex()       -   Enter the recovery mutex
20988**    recoverLeaveMutex()       -   Leave the recovery mutex
20989**    recoverAssertMutexHeld()  -   Assert that the recovery mutex is held
20990*/
20991#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
20992# define recoverEnterMutex()
20993# define recoverLeaveMutex()
20994#else
20995static void recoverEnterMutex(void){
20996  sqlite3_mutex_enter(sqlite3_mutex_alloc(RECOVER_MUTEX_ID));
20997}
20998static void recoverLeaveMutex(void){
20999  sqlite3_mutex_leave(sqlite3_mutex_alloc(RECOVER_MUTEX_ID));
21000}
21001#endif
21002#if SQLITE_THREADSAFE+0>=1 && defined(SQLITE_DEBUG)
21003static void recoverAssertMutexHeld(void){
21004  assert( sqlite3_mutex_held(sqlite3_mutex_alloc(RECOVER_MUTEX_ID)) );
21005}
21006#else
21007# define recoverAssertMutexHeld()
21008#endif
21009
21010
21011/*
21012** Like strlen(). But handles NULL pointer arguments.
21013*/
21014static int recoverStrlen(const char *zStr){
21015  if( zStr==0 ) return 0;
21016  return (int)(strlen(zStr)&0x7fffffff);
21017}
21018
21019/*
21020** This function is a no-op if the recover handle passed as the first 
21021** argument already contains an error (if p->errCode!=SQLITE_OK). 
21022**
21023** Otherwise, an attempt is made to allocate, zero and return a buffer nByte
21024** bytes in size. If successful, a pointer to the new buffer is returned. Or,
21025** if an OOM error occurs, NULL is returned and the handle error code
21026** (p->errCode) set to SQLITE_NOMEM.
21027*/
21028static void *recoverMalloc(sqlite3_recover *p, i64 nByte){
21029  void *pRet = 0;
21030  assert( nByte>0 );
21031  if( p->errCode==SQLITE_OK ){
21032    pRet = sqlite3_malloc64(nByte);
21033    if( pRet ){
21034      memset(pRet, 0, nByte);
21035    }else{
21036      p->errCode = SQLITE_NOMEM;
21037    }
21038  }
21039  return pRet;
21040}
21041
21042/*
21043** Set the error code and error message for the recover handle passed as
21044** the first argument. The error code is set to the value of parameter
21045** errCode.
21046**
21047** Parameter zFmt must be a printf() style formatting string. The handle 
21048** error message is set to the result of using any trailing arguments for 
21049** parameter substitutions in the formatting string.
21050**
21051** For example:
21052**
21053**   recoverError(p, SQLITE_ERROR, "no such table: %s", zTablename);
21054*/
21055static int recoverError(
21056  sqlite3_recover *p, 
21057  int errCode, 
21058  const char *zFmt, ...
21059){
21060  char *z = 0;
21061  va_list ap;
21062  va_start(ap, zFmt);
21063  if( zFmt ){
21064    z = sqlite3_vmprintf(zFmt, ap);
21065  }
21066  va_end(ap);
21067  sqlite3_free(p->zErrMsg);
21068  p->zErrMsg = z;
21069  p->errCode = errCode;
21070  return errCode;
21071}
21072
21073
21074/*
21075** This function is a no-op if p->errCode is initially other than SQLITE_OK.
21076** In this case it returns NULL.
21077**
21078** Otherwise, an attempt is made to allocate and return a bitmap object
21079** large enough to store a bit for all page numbers between 1 and nPg,
21080** inclusive. The bitmap is initially zeroed.
21081*/
21082static RecoverBitmap *recoverBitmapAlloc(sqlite3_recover *p, i64 nPg){
21083  int nElem = (nPg+1+31) / 32;
21084  int nByte = SZ_RECOVERBITMAP_32 + nElem*sizeof(u32);
21085  RecoverBitmap *pRet = (RecoverBitmap*)recoverMalloc(p, nByte);
21086
21087  if( pRet ){
21088    pRet->nPg = nPg;
21089  }
21090  return pRet;
21091}
21092
21093/*
21094** Free a bitmap object allocated by recoverBitmapAlloc().
21095*/
21096static void recoverBitmapFree(RecoverBitmap *pMap){
21097  sqlite3_free(pMap);
21098}
21099
21100/*
21101** Set the bit associated with page iPg in bitvec pMap.
21102*/
21103static void recoverBitmapSet(RecoverBitmap *pMap, i64 iPg){
21104  if( iPg<=pMap->nPg ){
21105    int iElem = (iPg / 32);
21106    int iBit = (iPg % 32);
21107    pMap->aElem[iElem] |= (((u32)1) << iBit);
21108  }
21109}
21110
21111/*
21112** Query bitmap object pMap for the state of the bit associated with page
21113** iPg. Return 1 if it is set, or 0 otherwise.
21114*/
21115static int recoverBitmapQuery(RecoverBitmap *pMap, i64 iPg){
21116  int ret = 1;
21117  if( iPg<=pMap->nPg && iPg>0 ){
21118    int iElem = (iPg / 32);
21119    int iBit = (iPg % 32);
21120    ret = (pMap->aElem[iElem] & (((u32)1) << iBit)) ? 1 : 0;
21121  }
21122  return ret;
21123}
21124
21125/*
21126** Set the recover handle error to the error code and message returned by
21127** calling sqlite3_errcode() and sqlite3_errmsg(), respectively, on database
21128** handle db.
21129*/
21130static int recoverDbError(sqlite3_recover *p, sqlite3 *db){
21131  return recoverError(p, sqlite3_errcode(db), "%s", sqlite3_errmsg(db));
21132}
21133
21134/*
21135** This function is a no-op if recover handle p already contains an error
21136** (if p->errCode!=SQLITE_OK). 
21137**
21138** Otherwise, it attempts to prepare the SQL statement in zSql against
21139** database handle db. If successful, the statement handle is returned.
21140** Or, if an error occurs, NULL is returned and an error left in the
21141** recover handle.
21142*/
21143static sqlite3_stmt *recoverPrepare(
21144  sqlite3_recover *p,
21145  sqlite3 *db, 
21146  const char *zSql
21147){
21148  sqlite3_stmt *pStmt = 0;
21149  if( p->errCode==SQLITE_OK ){
21150    if( sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) ){
21151      recoverDbError(p, db);
21152    }
21153  }
21154  return pStmt;
21155}
21156
21157/*
21158** This function is a no-op if recover handle p already contains an error
21159** (if p->errCode!=SQLITE_OK). 
21160**
21161** Otherwise, argument zFmt is used as a printf() style format string,
21162** along with any trailing arguments, to create an SQL statement. This
21163** SQL statement is prepared against database handle db and, if successful,
21164** the statment handle returned. Or, if an error occurs - either during
21165** the printf() formatting or when preparing the resulting SQL - an
21166** error code and message are left in the recover handle.
21167*/
21168static sqlite3_stmt *recoverPreparePrintf(
21169  sqlite3_recover *p,
21170  sqlite3 *db, 
21171  const char *zFmt, ...
21172){
21173  sqlite3_stmt *pStmt = 0;
21174  if( p->errCode==SQLITE_OK ){
21175    va_list ap;
21176    char *z;
21177    va_start(ap, zFmt);
21178    z = sqlite3_vmprintf(zFmt, ap);
21179    va_end(ap);
21180    if( z==0 ){
21181      p->errCode = SQLITE_NOMEM;
21182    }else{
21183      pStmt = recoverPrepare(p, db, z);
21184      sqlite3_free(z);
21185    }
21186  }
21187  return pStmt;
21188}
21189
21190/*
21191** Reset SQLite statement handle pStmt. If the call to sqlite3_reset() 
21192** indicates that an error occurred, and there is not already an error
21193** in the recover handle passed as the first argument, set the error
21194** code and error message appropriately.
21195**
21196** This function returns a copy of the statement handle pointer passed
21197** as the second argument.
21198*/
21199static sqlite3_stmt *recoverReset(sqlite3_recover *p, sqlite3_stmt *pStmt){
21200  int rc = sqlite3_reset(pStmt);
21201  if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT && p->errCode==SQLITE_OK ){
21202    recoverDbError(p, sqlite3_db_handle(pStmt));
21203  }
21204  return pStmt;
21205}
21206
21207/*
21208** Finalize SQLite statement handle pStmt. If the call to sqlite3_reset() 
21209** indicates that an error occurred, and there is not already an error
21210** in the recover handle passed as the first argument, set the error
21211** code and error message appropriately.
21212*/
21213static void recoverFinalize(sqlite3_recover *p, sqlite3_stmt *pStmt){
21214  sqlite3 *db = sqlite3_db_handle(pStmt);
21215  int rc = sqlite3_finalize(pStmt);
21216  if( rc!=SQLITE_OK && p->errCode==SQLITE_OK ){
21217    recoverDbError(p, db);
21218  }
21219}
21220
21221/*
21222** Run a single SQL statement in zSql.  If zSql contains two or more
21223** SQL statements separated by ';', only the first is run.
21224**
21225** Return the sqlite3_finalizer() or sqlite3_prepare() result code
21226** from running the zSql statement.
21227*/
21228static int recoverOneStmt(sqlite3 *db, const char *zSql){
21229  sqlite3_stmt *pStmt = 0;
21230  int rc;
21231  if( zSql==0 ) return SQLITE_OK;
21232  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
21233  if( rc ){
21234    sqlite3_finalize(pStmt);
21235    return rc;
21236  }
21237  while( SQLITE_ROW==sqlite3_step(pStmt) ){}
21238  return sqlite3_finalize(pStmt);
21239}
21240
21241/*
21242** This function is a no-op if recover handle p already contains an error
21243** (if p->errCode!=SQLITE_OK). A copy of p->errCode is returned in this 
21244** case.
21245**
21246** Otherwise, execute a single SQL statment in zSql.  Even if zSql contains
21247** two or more SQL statements separated by ';', only execute the first one.
21248** If successful, return SQLITE_OK.  Or, if an error occurs, leave an error
21249** code and message in the recover handle and return a copy of the error code.
21250*/
21251static int recoverExec(sqlite3_recover *p, sqlite3 *db, const char *zSql){
21252  if( p->errCode==SQLITE_OK ){
21253    int rc = recoverOneStmt(db, zSql);
21254    if( rc ){
21255      recoverDbError(p, db);
21256    }
21257  }
21258  return p->errCode;
21259}
21260
21261/*
21262** Bind the value pVal to parameter iBind of statement pStmt. Leave an
21263** error in the recover handle passed as the first argument if an error
21264** (e.g. an OOM) occurs.
21265*/
21266static void recoverBindValue(
21267  sqlite3_recover *p, 
21268  sqlite3_stmt *pStmt, 
21269  int iBind, 
21270  sqlite3_value *pVal
21271){
21272  if( p->errCode==SQLITE_OK ){
21273    int rc = sqlite3_bind_value(pStmt, iBind, pVal);
21274    if( rc ) recoverError(p, rc, 0);
21275  }
21276}
21277
21278/*
21279** This function is a no-op if recover handle p already contains an error
21280** (if p->errCode!=SQLITE_OK). NULL is returned in this case.
21281**
21282** Otherwise, an attempt is made to interpret zFmt as a printf() style
21283** formatting string and the result of using the trailing arguments for
21284** parameter substitution with it written into a buffer obtained from
21285** sqlite3_malloc(). If successful, a pointer to the buffer is returned.
21286** It is the responsibility of the caller to eventually free the buffer
21287** using sqlite3_free().
21288**
21289** Or, if an error occurs, an error code and message is left in the recover
21290** handle and NULL returned.
21291*/
21292static char *recoverMPrintf(sqlite3_recover *p, const char *zFmt, ...){
21293  va_list ap;
21294  char *z;
21295  va_start(ap, zFmt);
21296  z = sqlite3_vmprintf(zFmt, ap);
21297  va_end(ap);
21298  if( p->errCode==SQLITE_OK ){
21299    if( z==0 ) p->errCode = SQLITE_NOMEM;
21300  }else{
21301    sqlite3_free(z);
21302    z = 0;
21303  }
21304  return z;
21305}
21306
21307/*
21308** This function is a no-op if recover handle p already contains an error
21309** (if p->errCode!=SQLITE_OK). Zero is returned in this case.
21310**
21311** Otherwise, execute "PRAGMA page_count" against the input database. If
21312** successful, return the integer result. Or, if an error occurs, leave an
21313** error code and error message in the sqlite3_recover handle and return
21314** zero.
21315*/
21316static i64 recoverPageCount(sqlite3_recover *p){
21317  i64 nPg = 0;
21318  if( p->errCode==SQLITE_OK ){
21319    sqlite3_stmt *pStmt = 0;
21320    pStmt = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.page_count", p->zDb);
21321    if( pStmt ){
21322      sqlite3_step(pStmt);
21323      nPg = sqlite3_column_int64(pStmt, 0);
21324    }
21325    recoverFinalize(p, pStmt);
21326  }
21327  return nPg;
21328}
21329
21330/*
21331** Implementation of SQL scalar function "read_i32". The first argument to 
21332** this function must be a blob. The second a non-negative integer. This 
21333** function reads and returns a 32-bit big-endian integer from byte
21334** offset (4*<arg2>) of the blob.
21335**
21336**     SELECT read_i32(<blob>, <idx>)
21337*/
21338static void recoverReadI32(
21339  sqlite3_context *context, 
21340  int argc, 
21341  sqlite3_value **argv
21342){
21343  const unsigned char *pBlob;
21344  int nBlob;
21345  int iInt;
21346
21347  assert( argc==2 );
21348  nBlob = sqlite3_value_bytes(argv[0]);
21349  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
21350  iInt = sqlite3_value_int(argv[1]) & 0xFFFF;
21351
21352  if( (iInt+1)*4<=nBlob ){
21353    const unsigned char *a = &pBlob[iInt*4];
21354    i64 iVal = ((i64)a[0]<<24)
21355             + ((i64)a[1]<<16)
21356             + ((i64)a[2]<< 8)
21357             + ((i64)a[3]<< 0);
21358    sqlite3_result_int64(context, iVal);
21359  }
21360}
21361
21362/*
21363** Implementation of SQL scalar function "page_is_used". This function
21364** is used as part of the procedure for locating orphan rows for the
21365** lost-and-found table, and it depends on those routines having populated
21366** the sqlite3_recover.laf.pUsed variable.
21367**
21368** The only argument to this function is a page-number. It returns true 
21369** if the page has already been used somehow during data recovery, or false
21370** otherwise.
21371**
21372**     SELECT page_is_used(<pgno>);
21373*/
21374static void recoverPageIsUsed(
21375  sqlite3_context *pCtx,
21376  int nArg,
21377  sqlite3_value **apArg
21378){
21379  sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx);
21380  i64 pgno = sqlite3_value_int64(apArg[0]);
21381  assert( nArg==1 );
21382  sqlite3_result_int(pCtx, recoverBitmapQuery(p->laf.pUsed, pgno));
21383}
21384
21385/*
21386** The implementation of a user-defined SQL function invoked by the 
21387** sqlite_dbdata and sqlite_dbptr virtual table modules to access pages
21388** of the database being recovered.
21389**
21390** This function always takes a single integer argument. If the argument
21391** is zero, then the value returned is the number of pages in the db being
21392** recovered. If the argument is greater than zero, it is a page number. 
21393** The value returned in this case is an SQL blob containing the data for 
21394** the identified page of the db being recovered. e.g.
21395**
21396**     SELECT getpage(0);       -- return number of pages in db
21397**     SELECT getpage(4);       -- return page 4 of db as a blob of data 
21398*/
21399static void recoverGetPage(
21400  sqlite3_context *pCtx,
21401  int nArg,
21402  sqlite3_value **apArg
21403){
21404  sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx);
21405  i64 pgno = sqlite3_value_int64(apArg[0]);
21406  sqlite3_stmt *pStmt = 0;
21407
21408  assert( nArg==1 );
21409  if( pgno==0 ){
21410    i64 nPg = recoverPageCount(p);
21411    sqlite3_result_int64(pCtx, nPg);
21412    return;
21413  }else{
21414    if( p->pGetPage==0 ){
21415      pStmt = p->pGetPage = recoverPreparePrintf(
21416          p, p->dbIn, "SELECT data FROM sqlite_dbpage(%Q) WHERE pgno=?", p->zDb
21417      );
21418    }else if( p->errCode==SQLITE_OK ){
21419      pStmt = p->pGetPage;
21420    }
21421
21422    if( pStmt ){
21423      sqlite3_bind_int64(pStmt, 1, pgno);
21424      if( SQLITE_ROW==sqlite3_step(pStmt) ){
21425        const u8 *aPg;
21426        int nPg;
21427        assert( p->errCode==SQLITE_OK );
21428        aPg = sqlite3_column_blob(pStmt, 0);
21429        nPg = sqlite3_column_bytes(pStmt, 0);
21430        if( pgno==1 && nPg==p->pgsz && 0==memcmp(p->pPage1Cache, aPg, nPg) ){
21431          aPg = p->pPage1Disk;
21432        }
21433        sqlite3_result_blob(pCtx, aPg, nPg-p->nReserve, SQLITE_TRANSIENT);
21434      }
21435      recoverReset(p, pStmt);
21436    }
21437  }
21438
21439  if( p->errCode ){
21440    if( p->zErrMsg ) sqlite3_result_error(pCtx, p->zErrMsg, -1);
21441    sqlite3_result_error_code(pCtx, p->errCode);
21442  }
21443}
21444
21445/*
21446** Find a string that is not found anywhere in z[].  Return a pointer
21447** to that string.
21448**
21449** Try to use zA and zB first.  If both of those are already found in z[]
21450** then make up some string and store it in the buffer zBuf.
21451*/
21452static const char *recoverUnusedString(
21453  const char *z,                    /* Result must not appear anywhere in z */
21454  const char *zA, const char *zB,   /* Try these first */
21455  char *zBuf                        /* Space to store a generated string */
21456){
21457  unsigned i = 0;
21458  if( strstr(z, zA)==0 ) return zA;
21459  if( strstr(z, zB)==0 ) return zB;
21460  do{
21461    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
21462  }while( strstr(z,zBuf)!=0 );
21463  return zBuf;
21464}
21465
21466/*
21467** Implementation of scalar SQL function "escape_crlf".  The argument passed to
21468** this function is the output of built-in function quote(). If the first
21469** character of the input is "'", indicating that the value passed to quote()
21470** was a text value, then this function searches the input for "\n" and "\r"
21471** characters and adds a wrapper similar to the following:
21472**
21473**   replace(replace(<input>, '\n', char(10), '\r', char(13));
21474**
21475** Or, if the first character of the input is not "'", then a copy of the input
21476** is returned.
21477*/
21478static void recoverEscapeCrlf(
21479  sqlite3_context *context, 
21480  int argc, 
21481  sqlite3_value **argv
21482){
21483  const char *zText = (const char*)sqlite3_value_text(argv[0]);
21484  (void)argc;
21485  if( zText && zText[0]=='\'' ){
21486    int nText = sqlite3_value_bytes(argv[0]);
21487    int i;
21488    char zBuf1[20];
21489    char zBuf2[20];
21490    const char *zNL = 0;
21491    const char *zCR = 0;
21492    int nCR = 0;
21493    int nNL = 0;
21494
21495    for(i=0; zText[i]; i++){
21496      if( zNL==0 && zText[i]=='\n' ){
21497        zNL = recoverUnusedString(zText, "\\n", "\\012", zBuf1);
21498        nNL = (int)strlen(zNL);
21499      }
21500      if( zCR==0 && zText[i]=='\r' ){
21501        zCR = recoverUnusedString(zText, "\\r", "\\015", zBuf2);
21502        nCR = (int)strlen(zCR);
21503      }
21504    }
21505
21506    if( zNL || zCR ){
21507      int iOut = 0;
21508      i64 nMax = (nNL > nCR) ? nNL : nCR;
21509      i64 nAlloc = nMax * nText + (nMax+64)*2;
21510      char *zOut = (char*)sqlite3_malloc64(nAlloc);
21511      if( zOut==0 ){
21512        sqlite3_result_error_nomem(context);
21513        return;
21514      }
21515
21516      if( zNL && zCR ){
21517        memcpy(&zOut[iOut], "replace(replace(", 16);
21518        iOut += 16;
21519      }else{
21520        memcpy(&zOut[iOut], "replace(", 8);
21521        iOut += 8;
21522      }
21523      for(i=0; zText[i]; i++){
21524        if( zText[i]=='\n' ){
21525          memcpy(&zOut[iOut], zNL, nNL);
21526          iOut += nNL;
21527        }else if( zText[i]=='\r' ){
21528          memcpy(&zOut[iOut], zCR, nCR);
21529          iOut += nCR;
21530        }else{
21531          zOut[iOut] = zText[i];
21532          iOut++;
21533        }
21534      }
21535
21536      if( zNL ){
21537        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
21538        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
21539        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
21540      }
21541      if( zCR ){
21542        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
21543        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
21544        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
21545      }
21546
21547      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
21548      sqlite3_free(zOut);
21549      return;
21550    }
21551  }
21552
21553  sqlite3_result_value(context, argv[0]);
21554}
21555
21556/*
21557** This function is a no-op if recover handle p already contains an error
21558** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
21559** this case. 
21560**
21561** Otherwise, attempt to populate temporary table "recovery.schema" with the
21562** parts of the database schema that can be extracted from the input database.
21563**
21564** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
21565** and error message are left in the recover handle and a copy of the
21566** error code returned. It is not considered an error if part of all of
21567** the database schema cannot be recovered due to corruption.
21568*/
21569static int recoverCacheSchema(sqlite3_recover *p){
21570  return recoverExec(p, p->dbOut,
21571    "WITH RECURSIVE pages(p) AS ("
21572    "  SELECT 1"
21573    "    UNION"
21574    "  SELECT child FROM sqlite_dbptr('getpage()'), pages WHERE pgno=p"
21575    ")"
21576    "INSERT INTO recovery.schema SELECT"
21577    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
21578    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
21579    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
21580    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
21581    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
21582    "FROM sqlite_dbdata('getpage()') WHERE pgno IN ("
21583    "  SELECT p FROM pages"
21584    ") GROUP BY pgno, cell"
21585  );
21586}
21587
21588/*
21589** If this recover handle is not in SQL callback mode (i.e. was not created 
21590** using sqlite3_recover_init_sql()) of if an error has already occurred, 
21591** this function is a no-op. Otherwise, issue a callback with SQL statement
21592** zSql as the parameter. 
21593**
21594** If the callback returns non-zero, set the recover handle error code to
21595** the value returned (so that the caller will abandon processing).
21596*/
21597static void recoverSqlCallback(sqlite3_recover *p, const char *zSql){
21598  if( p->errCode==SQLITE_OK && p->xSql ){
21599    int res = p->xSql(p->pSqlCtx, zSql);
21600    if( res ){
21601      recoverError(p, SQLITE_ERROR, "callback returned an error - %d", res);
21602    }
21603  }
21604}
21605
21606/*
21607** Transfer the following settings from the input database to the output
21608** database:
21609**
21610**   + page-size,
21611**   + auto-vacuum settings,
21612**   + database encoding,
21613**   + user-version (PRAGMA user_version), and
21614**   + application-id (PRAGMA application_id), and
21615*/
21616static void recoverTransferSettings(sqlite3_recover *p){
21617  const char *aPragma[] = {
21618    "encoding",
21619    "page_size",
21620    "auto_vacuum",
21621    "user_version",
21622    "application_id"
21623  };
21624  int ii;
21625
21626  /* Truncate the output database to 0 pages in size. This is done by 
21627  ** opening a new, empty, temp db, then using the backup API to clobber 
21628  ** any existing output db with a copy of it. */
21629  if( p->errCode==SQLITE_OK ){
21630    sqlite3 *db2 = 0;
21631    int rc = sqlite3_open("", &db2);
21632    if( rc!=SQLITE_OK ){
21633      recoverDbError(p, db2);
21634      return;
21635    }
21636
21637    for(ii=0; ii<(int)(sizeof(aPragma)/sizeof(aPragma[0])); ii++){
21638      const char *zPrag = aPragma[ii];
21639      sqlite3_stmt *p1 = 0;
21640      p1 = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.%s", p->zDb, zPrag);
21641      if( p->errCode==SQLITE_OK && sqlite3_step(p1)==SQLITE_ROW ){
21642        const char *zArg = (const char*)sqlite3_column_text(p1, 0);
21643        char *z2 = recoverMPrintf(p, "PRAGMA %s = %Q", zPrag, zArg);
21644        recoverSqlCallback(p, z2);
21645        recoverExec(p, db2, z2);
21646        sqlite3_free(z2);
21647        if( zArg==0 ){
21648          recoverError(p, SQLITE_NOMEM, 0);
21649        }
21650      }
21651      recoverFinalize(p, p1);
21652    }
21653    recoverExec(p, db2, "CREATE TABLE t1(a)");
21654    recoverExec(p, db2, "DROP TABLE t1");
21655
21656    if( p->errCode==SQLITE_OK ){
21657      sqlite3 *db = p->dbOut;
21658      sqlite3_backup *pBackup = sqlite3_backup_init(db, "main", db2, "main");
21659      if( pBackup ){
21660        sqlite3_backup_step(pBackup, -1);
21661        p->errCode = sqlite3_backup_finish(pBackup);
21662      }else{
21663        recoverDbError(p, db);
21664      }
21665    }
21666
21667    sqlite3_close(db2);
21668  }
21669}
21670
21671/*
21672** This function is a no-op if recover handle p already contains an error
21673** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
21674** this case. 
21675**
21676** Otherwise, an attempt is made to open the output database, attach
21677** and create the schema of the temporary database used to store
21678** intermediate data, and to register all required user functions and
21679** virtual table modules with the output handle.
21680**
21681** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
21682** and error message are left in the recover handle and a copy of the
21683** error code returned.
21684*/
21685static int recoverOpenOutput(sqlite3_recover *p){
21686  struct Func {
21687    const char *zName;
21688    int nArg;
21689    void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
21690  } aFunc[] = {
21691    { "getpage", 1, recoverGetPage },
21692    { "page_is_used", 1, recoverPageIsUsed },
21693    { "read_i32", 2, recoverReadI32 },
21694    { "escape_crlf", 1, recoverEscapeCrlf },
21695  };
21696
21697  const int flags = SQLITE_OPEN_URI|SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
21698  sqlite3 *db = 0;                /* New database handle */
21699  int ii;                         /* For iterating through aFunc[] */
21700
21701  assert( p->dbOut==0 );
21702
21703  if( sqlite3_open_v2(p->zUri, &db, flags, 0) ){
21704    recoverDbError(p, db);
21705  }
21706
21707  /* Register the sqlite_dbdata and sqlite_dbptr virtual table modules.
21708  ** These two are registered with the output database handle - this
21709  ** module depends on the input handle supporting the sqlite_dbpage
21710  ** virtual table only.  */
21711  if( p->errCode==SQLITE_OK ){
21712    p->errCode = sqlite3_dbdata_init(db, 0, 0);
21713  }
21714
21715  /* Register the custom user-functions with the output handle. */
21716  for(ii=0;
21717      p->errCode==SQLITE_OK && ii<(int)(sizeof(aFunc)/sizeof(aFunc[0]));
21718      ii++){
21719    p->errCode = sqlite3_create_function(db, aFunc[ii].zName, 
21720        aFunc[ii].nArg, SQLITE_UTF8, (void*)p, aFunc[ii].xFunc, 0, 0
21721    );
21722  }
21723
21724  p->dbOut = db;
21725  return p->errCode;
21726}
21727
21728/*
21729** Attach the auxiliary database 'recovery' to the output database handle.
21730** This temporary database is used during the recovery process and then 
21731** discarded.
21732*/
21733static void recoverOpenRecovery(sqlite3_recover *p){
21734  char *zSql = recoverMPrintf(p, "ATTACH %Q AS recovery;", p->zStateDb);
21735  recoverExec(p, p->dbOut, zSql);
21736  sqlite3_free(zSql);
21737  recoverExec(p, p->dbOut, "PRAGMA writable_schema = 1");
21738  recoverExec(p, p->dbOut,
21739      "CREATE TABLE recovery.map(pgno INTEGER PRIMARY KEY, parent INT)");
21740  recoverExec(p, p->dbOut,
21741      "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql)");
21742}
21743
21744
21745/*
21746** This function is a no-op if recover handle p already contains an error
21747** (if p->errCode!=SQLITE_OK).
21748**
21749** Otherwise, argument zName must be the name of a table that has just been
21750** created in the output database. This function queries the output db
21751** for the schema of said table, and creates a RecoverTable object to
21752** store the schema in memory. The new RecoverTable object is linked into
21753** the list at sqlite3_recover.pTblList.
21754**
21755** Parameter iRoot must be the root page of table zName in the INPUT 
21756** database.
21757*/
21758static void recoverAddTable(
21759  sqlite3_recover *p, 
21760  const char *zName,              /* Name of table created in output db */
21761  i64 iRoot                       /* Root page of same table in INPUT db */
21762){
21763  sqlite3_stmt *pStmt = recoverPreparePrintf(p, p->dbOut, 
21764      "PRAGMA table_xinfo(%Q)", zName
21765  );
21766
21767  if( pStmt ){
21768    int iPk = -1;
21769    int iBind = 1;
21770    RecoverTable *pNew = 0;
21771    int nCol = 0;
21772    int nName = recoverStrlen(zName);
21773    int nByte = 0;
21774    while( sqlite3_step(pStmt)==SQLITE_ROW ){
21775      nCol++;
21776      nByte += (sqlite3_column_bytes(pStmt, 1)+1);
21777    }
21778    nByte += sizeof(RecoverTable) + nCol*sizeof(RecoverColumn) + nName+1;
21779    recoverReset(p, pStmt);
21780
21781    pNew = recoverMalloc(p, nByte);
21782    if( pNew ){
21783      int i = 0;
21784      int iField = 0;
21785      char *csr = 0;
21786      pNew->aCol = (RecoverColumn*)&pNew[1];
21787      pNew->zTab = csr = (char*)&pNew->aCol[nCol];
21788      pNew->nCol = nCol;
21789      pNew->iRoot = iRoot;
21790      memcpy(csr, zName, nName);
21791      csr += nName+1;
21792
21793      for(i=0; sqlite3_step(pStmt)==SQLITE_ROW; i++){
21794        int iPKF = sqlite3_column_int(pStmt, 5);
21795        int n = sqlite3_column_bytes(pStmt, 1);
21796        const char *z = (const char*)sqlite3_column_text(pStmt, 1);
21797        const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
21798        int eHidden = sqlite3_column_int(pStmt, 6);
21799
21800        if( iPk==-1 && iPKF==1 && !sqlite3_stricmp("integer", zType) ) iPk = i;
21801        if( iPKF>1 ) iPk = -2;
21802        pNew->aCol[i].zCol = csr;
21803        pNew->aCol[i].eHidden = eHidden;
21804        if( eHidden==RECOVER_EHIDDEN_VIRTUAL ){
21805          pNew->aCol[i].iField = -1;
21806        }else{
21807          pNew->aCol[i].iField = iField++;
21808        }
21809        if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
21810         && eHidden!=RECOVER_EHIDDEN_STORED
21811        ){
21812          pNew->aCol[i].iBind = iBind++;
21813        }
21814        memcpy(csr, z, n);
21815        csr += (n+1);
21816      }
21817
21818      pNew->pNext = p->pTblList;
21819      p->pTblList = pNew;
21820      pNew->bIntkey = 1;
21821    }
21822
21823    recoverFinalize(p, pStmt);
21824
21825    pStmt = recoverPreparePrintf(p, p->dbOut, "PRAGMA index_xinfo(%Q)", zName);
21826    while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
21827      int iField = sqlite3_column_int(pStmt, 0);
21828      int iCol = sqlite3_column_int(pStmt, 1);
21829
21830      assert( iCol<pNew->nCol );
21831      pNew->aCol[iCol].iField = iField;
21832
21833      pNew->bIntkey = 0;
21834      iPk = -2;
21835    }
21836    recoverFinalize(p, pStmt);
21837
21838    if( p->errCode==SQLITE_OK ){
21839      if( iPk>=0 ){
21840        pNew->aCol[iPk].bIPK = 1;
21841      }else if( pNew->bIntkey ){
21842        pNew->iRowidBind = iBind++;
21843      }
21844    }
21845  }
21846}
21847
21848/*
21849** This function is called after recoverCacheSchema() has cached those parts
21850** of the input database schema that could be recovered in temporary table
21851** "recovery.schema". This function creates in the output database copies
21852** of all parts of that schema that must be created before the tables can
21853** be populated. Specifically, this means:
21854**
21855**     * all tables that are not VIRTUAL, and
21856**     * UNIQUE indexes.
21857**
21858** If the recovery handle uses SQL callbacks, then callbacks containing
21859** the associated "CREATE TABLE" and "CREATE INDEX" statements are made.
21860**
21861** Additionally, records are added to the sqlite_schema table of the
21862** output database for any VIRTUAL tables. The CREATE VIRTUAL TABLE
21863** records are written directly to sqlite_schema, not actually executed.
21864** If the handle is in SQL callback mode, then callbacks are invoked 
21865** with equivalent SQL statements.
21866*/
21867static int recoverWriteSchema1(sqlite3_recover *p){
21868  sqlite3_stmt *pSelect = 0;
21869  sqlite3_stmt *pTblname = 0;
21870
21871  pSelect = recoverPrepare(p, p->dbOut,
21872      "WITH dbschema(rootpage, name, sql, tbl, isVirtual, isIndex) AS ("
21873      "  SELECT rootpage, name, sql, "
21874      "    type='table', "
21875      "    sql LIKE 'create virtual%',"
21876      "    (type='index' AND (sql LIKE '%unique%' OR ?1))"
21877      "  FROM recovery.schema"
21878      ")"
21879      "SELECT rootpage, tbl, isVirtual, name, sql"
21880      " FROM dbschema "
21881      "  WHERE (tbl OR isIndex) AND sql GLOB 'CREATE *'"
21882      "  ORDER BY tbl DESC, name=='sqlite_sequence' DESC"
21883  );
21884
21885  pTblname = recoverPrepare(p, p->dbOut,
21886      "SELECT name FROM sqlite_schema "
21887      "WHERE type='table' ORDER BY rowid DESC LIMIT 1"
21888  );
21889
21890  if( pSelect ){
21891    sqlite3_bind_int(pSelect, 1, p->bSlowIndexes);
21892    while( sqlite3_step(pSelect)==SQLITE_ROW ){
21893      i64 iRoot = sqlite3_column_int64(pSelect, 0);
21894      int bTable = sqlite3_column_int(pSelect, 1);
21895      int bVirtual = sqlite3_column_int(pSelect, 2);
21896      const char *zName = (const char*)sqlite3_column_text(pSelect, 3);
21897      const char *zSql = (const char*)sqlite3_column_text(pSelect, 4);
21898      char *zFree = 0;
21899      int rc = SQLITE_OK;
21900
21901      if( bVirtual ){
21902        zSql = (const char*)(zFree = recoverMPrintf(p,
21903            "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
21904            zName, zName, zSql
21905        ));
21906      }
21907      rc = recoverOneStmt(p->dbOut, zSql);
21908      if( rc==SQLITE_OK ){
21909        recoverSqlCallback(p, zSql);
21910        if( bTable && !bVirtual ){
21911          if( SQLITE_ROW==sqlite3_step(pTblname) ){
21912            const char *zTbl = (const char*)sqlite3_column_text(pTblname, 0);
21913            if( zTbl ) recoverAddTable(p, zTbl, iRoot);
21914          }
21915          recoverReset(p, pTblname);
21916        }
21917      }else if( rc!=SQLITE_ERROR ){
21918        recoverDbError(p, p->dbOut);
21919      }
21920      sqlite3_free(zFree);
21921    }
21922  }
21923  recoverFinalize(p, pSelect);
21924  recoverFinalize(p, pTblname);
21925
21926  return p->errCode;
21927}
21928
21929/*
21930** This function is called after the output database has been populated. It
21931** adds all recovered schema elements that were not created in the output
21932** database by recoverWriteSchema1() - everything except for tables and
21933** UNIQUE indexes. Specifically:
21934**
21935**     * views,
21936**     * triggers,
21937**     * non-UNIQUE indexes.
21938**
21939** If the recover handle is in SQL callback mode, then equivalent callbacks
21940** are issued to create the schema elements.
21941*/
21942static int recoverWriteSchema2(sqlite3_recover *p){
21943  sqlite3_stmt *pSelect = 0;
21944
21945  pSelect = recoverPrepare(p, p->dbOut,
21946      p->bSlowIndexes ?
21947      "SELECT rootpage, sql FROM recovery.schema "
21948      "  WHERE type!='table' AND type!='index'"
21949      "    AND sql GLOB 'CREATE *'"
21950      :
21951      "SELECT rootpage, sql FROM recovery.schema "
21952      "  WHERE type!='table' AND (type!='index' OR sql NOT LIKE '%unique%')"
21953      "    AND sql GLOB 'CREATE *'"
21954  );
21955
21956  if( pSelect ){
21957    while( sqlite3_step(pSelect)==SQLITE_ROW ){
21958      const char *zSql = (const char*)sqlite3_column_text(pSelect, 1);
21959      int rc = recoverOneStmt(p->dbOut, zSql);
21960      if( rc==SQLITE_OK ){
21961        recoverSqlCallback(p, zSql);
21962      }else if( rc!=SQLITE_ERROR ){
21963        recoverDbError(p, p->dbOut);
21964      }
21965    }
21966  }
21967  recoverFinalize(p, pSelect);
21968
21969  return p->errCode;
21970}
21971
21972/*
21973** This function is a no-op if recover handle p already contains an error
21974** (if p->errCode!=SQLITE_OK). In this case it returns NULL.
21975**
21976** Otherwise, if the recover handle is configured to create an output
21977** database (was created by sqlite3_recover_init()), then this function
21978** prepares and returns an SQL statement to INSERT a new record into table
21979** pTab, assuming the first nField fields of a record extracted from disk
21980** are valid.
21981**
21982** For example, if table pTab is:
21983**
21984**     CREATE TABLE name(a, b GENERATED ALWAYS AS (a+1) STORED, c, d, e);
21985**
21986** And nField is 4, then the SQL statement prepared and returned is:
21987**
21988**     INSERT INTO (a, c, d) VALUES (?1, ?2, ?3);
21989**
21990** In this case even though 4 values were extracted from the input db,
21991** only 3 are written to the output, as the generated STORED column 
21992** cannot be written.
21993**
21994** If the recover handle is in SQL callback mode, then the SQL statement
21995** prepared is such that evaluating it returns a single row containing
21996** a single text value - itself an SQL statement similar to the above,
21997** except with SQL literals in place of the variables. For example:
21998**
21999**     SELECT 'INSERT INTO (a, c, d) VALUES (' 
22000**          || quote(?1) || ', '
22001**          || quote(?2) || ', '
22002**          || quote(?3) || ')';
22003**
22004** In either case, it is the responsibility of the caller to eventually
22005** free the statement handle using sqlite3_finalize().
22006*/
22007static sqlite3_stmt *recoverInsertStmt(
22008  sqlite3_recover *p, 
22009  RecoverTable *pTab,
22010  int nField
22011){
22012  sqlite3_stmt *pRet = 0;
22013  const char *zSep = "";
22014  const char *zSqlSep = "";
22015  char *zSql = 0;
22016  char *zFinal = 0;
22017  char *zBind = 0;
22018  int ii;
22019  int bSql = p->xSql ? 1 : 0;
22020
22021  if( nField<=0 ) return 0;
22022
22023  assert( nField<=pTab->nCol );
22024
22025  zSql = recoverMPrintf(p, "INSERT OR IGNORE INTO %Q(", pTab->zTab);
22026
22027  if( pTab->iRowidBind ){
22028    assert( pTab->bIntkey );
22029    zSql = recoverMPrintf(p, "%z_rowid_", zSql);
22030    if( bSql ){
22031      zBind = recoverMPrintf(p, "%zquote(?%d)", zBind, pTab->iRowidBind);
22032    }else{
22033      zBind = recoverMPrintf(p, "%z?%d", zBind, pTab->iRowidBind);
22034    }
22035    zSqlSep = "||', '||";
22036    zSep = ", ";
22037  }
22038
22039  for(ii=0; ii<nField; ii++){
22040    int eHidden = pTab->aCol[ii].eHidden;
22041    if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
22042     && eHidden!=RECOVER_EHIDDEN_STORED
22043    ){
22044      assert( pTab->aCol[ii].iField>=0 && pTab->aCol[ii].iBind>=1 );
22045      zSql = recoverMPrintf(p, "%z%s%Q", zSql, zSep, pTab->aCol[ii].zCol);
22046
22047      if( bSql ){
22048        zBind = recoverMPrintf(p, 
22049            "%z%sescape_crlf(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind
22050        );
22051        zSqlSep = "||', '||";
22052      }else{
22053        zBind = recoverMPrintf(p, "%z%s?%d", zBind, zSep, pTab->aCol[ii].iBind);
22054      }
22055      zSep = ", ";
22056    }
22057  }
22058
22059  if( bSql ){
22060    zFinal = recoverMPrintf(p, "SELECT %Q || ') VALUES (' || %s || ')'", 
22061        zSql, zBind
22062    );
22063  }else{
22064    zFinal = recoverMPrintf(p, "%s) VALUES (%s)", zSql, zBind);
22065  }
22066
22067  pRet = recoverPrepare(p, p->dbOut, zFinal);
22068  sqlite3_free(zSql);
22069  sqlite3_free(zBind);
22070  sqlite3_free(zFinal);
22071  
22072  return pRet;
22073}
22074
22075
22076/*
22077** Search the list of RecoverTable objects at p->pTblList for one that
22078** has root page iRoot in the input database. If such an object is found,
22079** return a pointer to it. Otherwise, return NULL.
22080*/
22081static RecoverTable *recoverFindTable(sqlite3_recover *p, u32 iRoot){
22082  RecoverTable *pRet = 0;
22083  for(pRet=p->pTblList; pRet && pRet->iRoot!=iRoot; pRet=pRet->pNext);
22084  return pRet;
22085}
22086
22087/*
22088** This function attempts to create a lost and found table within the 
22089** output db. If successful, it returns a pointer to a buffer containing
22090** the name of the new table. It is the responsibility of the caller to
22091** eventually free this buffer using sqlite3_free().
22092**
22093** If an error occurs, NULL is returned and an error code and error 
22094** message left in the recover handle.
22095*/
22096static char *recoverLostAndFoundCreate(
22097  sqlite3_recover *p,             /* Recover object */
22098  int nField                      /* Number of column fields in new table */
22099){
22100  char *zTbl = 0;
22101  sqlite3_stmt *pProbe = 0;
22102  int ii = 0;
22103
22104  pProbe = recoverPrepare(p, p->dbOut,
22105    "SELECT 1 FROM sqlite_schema WHERE name=?"
22106  );
22107  for(ii=-1; zTbl==0 && p->errCode==SQLITE_OK && ii<1000; ii++){
22108    int bFail = 0;
22109    if( ii<0 ){
22110      zTbl = recoverMPrintf(p, "%s", p->zLostAndFound);
22111    }else{
22112      zTbl = recoverMPrintf(p, "%s_%d", p->zLostAndFound, ii);
22113    }
22114
22115    if( p->errCode==SQLITE_OK ){
22116      sqlite3_bind_text(pProbe, 1, zTbl, -1, SQLITE_STATIC);
22117      if( SQLITE_ROW==sqlite3_step(pProbe) ){
22118        bFail = 1;
22119      }
22120      recoverReset(p, pProbe);
22121    }
22122
22123    if( bFail ){
22124      sqlite3_clear_bindings(pProbe);
22125      sqlite3_free(zTbl);
22126      zTbl = 0;
22127    }
22128  }
22129  recoverFinalize(p, pProbe);
22130
22131  if( zTbl ){
22132    const char *zSep = 0;
22133    char *zField = 0;
22134    char *zSql = 0;
22135
22136    zSep = "rootpgno INTEGER, pgno INTEGER, nfield INTEGER, id INTEGER, ";
22137    for(ii=0; p->errCode==SQLITE_OK && ii<nField; ii++){
22138      zField = recoverMPrintf(p, "%z%sc%d", zField, zSep, ii);
22139      zSep = ", ";
22140    }
22141
22142    zSql = recoverMPrintf(p, "CREATE TABLE %s(%s)", zTbl, zField);
22143    sqlite3_free(zField);
22144
22145    recoverExec(p, p->dbOut, zSql);
22146    recoverSqlCallback(p, zSql);
22147    sqlite3_free(zSql);
22148  }else if( p->errCode==SQLITE_OK ){
22149    recoverError(
22150        p, SQLITE_ERROR, "failed to create %s output table", p->zLostAndFound
22151    );
22152  }
22153
22154  return zTbl;
22155}
22156
22157/*
22158** Synthesize and prepare an INSERT statement to write to the lost_and_found
22159** table in the output database. The name of the table is zTab, and it has
22160** nField c* fields.
22161*/
22162static sqlite3_stmt *recoverLostAndFoundInsert(
22163  sqlite3_recover *p,
22164  const char *zTab,
22165  int nField
22166){
22167  int nTotal = nField + 4;
22168  int ii;
22169  char *zBind = 0;
22170  sqlite3_stmt *pRet = 0;
22171
22172  if( p->xSql==0 ){
22173    for(ii=0; ii<nTotal; ii++){
22174      zBind = recoverMPrintf(p, "%z%s?", zBind, zBind?", ":"", ii);
22175    }
22176    pRet = recoverPreparePrintf(
22177        p, p->dbOut, "INSERT INTO %s VALUES(%s)", zTab, zBind
22178    );
22179  }else{
22180    const char *zSep = "";
22181    for(ii=0; ii<nTotal; ii++){
22182      zBind = recoverMPrintf(p, "%z%squote(?)", zBind, zSep);
22183      zSep = "|| ', ' ||";
22184    }
22185    pRet = recoverPreparePrintf(
22186        p, p->dbOut, "SELECT 'INSERT INTO %s VALUES(' || %s || ')'", zTab, zBind
22187    );
22188  }
22189
22190  sqlite3_free(zBind);
22191  return pRet;
22192}
22193
22194/*
22195** Input database page iPg contains data that will be written to the
22196** lost-and-found table of the output database. This function attempts
22197** to identify the root page of the tree that page iPg belonged to.
22198** If successful, it sets output variable (*piRoot) to the page number
22199** of the root page and returns SQLITE_OK. Otherwise, if an error occurs,
22200** an SQLite error code is returned and the final value of *piRoot 
22201** undefined.
22202*/
22203static int recoverLostAndFoundFindRoot(
22204  sqlite3_recover *p, 
22205  i64 iPg,
22206  i64 *piRoot
22207){
22208  RecoverStateLAF *pLaf = &p->laf;
22209
22210  if( pLaf->pFindRoot==0 ){
22211    pLaf->pFindRoot = recoverPrepare(p, p->dbOut,
22212        "WITH RECURSIVE p(pgno) AS ("
22213        "  SELECT ?"
22214        "    UNION"
22215        "  SELECT parent FROM recovery.map AS m, p WHERE m.pgno=p.pgno"
22216        ") "
22217        "SELECT p.pgno FROM p, recovery.map m WHERE m.pgno=p.pgno "
22218        "    AND m.parent IS NULL"
22219    );
22220  }
22221  if( p->errCode==SQLITE_OK ){
22222    sqlite3_bind_int64(pLaf->pFindRoot, 1, iPg);
22223    if( sqlite3_step(pLaf->pFindRoot)==SQLITE_ROW ){
22224      *piRoot = sqlite3_column_int64(pLaf->pFindRoot, 0);
22225    }else{
22226      *piRoot = iPg;
22227    }
22228    recoverReset(p, pLaf->pFindRoot);
22229  }
22230  return p->errCode;
22231}
22232
22233/*
22234** Recover data from page iPage of the input database and write it to
22235** the lost-and-found table in the output database.
22236*/
22237static void recoverLostAndFoundOnePage(sqlite3_recover *p, i64 iPage){
22238  RecoverStateLAF *pLaf = &p->laf;
22239  sqlite3_value **apVal = pLaf->apVal;
22240  sqlite3_stmt *pPageData = pLaf->pPageData;
22241  sqlite3_stmt *pInsert = pLaf->pInsert;
22242
22243  int nVal = -1;
22244  int iPrevCell = 0;
22245  i64 iRoot = 0;
22246  int bHaveRowid = 0;
22247  i64 iRowid = 0;
22248  int ii = 0;
22249
22250  if( recoverLostAndFoundFindRoot(p, iPage, &iRoot) ) return;
22251  sqlite3_bind_int64(pPageData, 1, iPage);
22252  while( p->errCode==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPageData) ){
22253    int iCell = sqlite3_column_int64(pPageData, 0);
22254    int iField = sqlite3_column_int64(pPageData, 1);
22255
22256    if( iPrevCell!=iCell && nVal>=0 ){
22257      /* Insert the new row */
22258      sqlite3_bind_int64(pInsert, 1, iRoot);      /* rootpgno */
22259      sqlite3_bind_int64(pInsert, 2, iPage);      /* pgno */
22260      sqlite3_bind_int(pInsert, 3, nVal);         /* nfield */
22261      if( bHaveRowid ){
22262        sqlite3_bind_int64(pInsert, 4, iRowid);   /* id */
22263      }
22264      for(ii=0; ii<nVal; ii++){
22265        recoverBindValue(p, pInsert, 5+ii, apVal[ii]);
22266      }
22267      if( sqlite3_step(pInsert)==SQLITE_ROW ){
22268        recoverSqlCallback(p, (const char*)sqlite3_column_text(pInsert, 0));
22269      }
22270      recoverReset(p, pInsert);
22271
22272      /* Discard the accumulated row data */
22273      for(ii=0; ii<nVal; ii++){
22274        sqlite3_value_free(apVal[ii]);
22275        apVal[ii] = 0;
22276      }
22277      sqlite3_clear_bindings(pInsert);
22278      bHaveRowid = 0;
22279      nVal = -1;
22280    }
22281
22282    if( iCell<0 ) break;
22283
22284    if( iField<0 ){
22285      assert( nVal==-1 );
22286      iRowid = sqlite3_column_int64(pPageData, 2);
22287      bHaveRowid = 1;
22288      nVal = 0;
22289    }else if( iField<pLaf->nMaxField ){
22290      sqlite3_value *pVal = sqlite3_column_value(pPageData, 2);
22291      apVal[iField] = sqlite3_value_dup(pVal);
22292      assert( iField==nVal || (nVal==-1 && iField==0) );
22293      nVal = iField+1;
22294      if( apVal[iField]==0 ){
22295        recoverError(p, SQLITE_NOMEM, 0);
22296      }
22297    }
22298
22299    iPrevCell = iCell;
22300  }
22301  recoverReset(p, pPageData);
22302
22303  for(ii=0; ii<nVal; ii++){
22304    sqlite3_value_free(apVal[ii]);
22305    apVal[ii] = 0;
22306  }
22307}
22308
22309/*
22310** Perform one step (sqlite3_recover_step()) of work for the connection 
22311** passed as the only argument, which is guaranteed to be in
22312** RECOVER_STATE_LOSTANDFOUND3 state - during which the lost-and-found 
22313** table of the output database is populated with recovered data that can 
22314** not be assigned to any recovered schema object.
22315*/ 
22316static int recoverLostAndFound3Step(sqlite3_recover *p){
22317  RecoverStateLAF *pLaf = &p->laf;
22318  if( p->errCode==SQLITE_OK ){
22319    if( pLaf->pInsert==0 ){
22320      return SQLITE_DONE;
22321    }else{
22322      if( p->errCode==SQLITE_OK ){
22323        int res = sqlite3_step(pLaf->pAllPage);
22324        if( res==SQLITE_ROW ){
22325          i64 iPage = sqlite3_column_int64(pLaf->pAllPage, 0);
22326          if( recoverBitmapQuery(pLaf->pUsed, iPage)==0 ){
22327            recoverLostAndFoundOnePage(p, iPage);
22328          }
22329        }else{
22330          recoverReset(p, pLaf->pAllPage);
22331          return SQLITE_DONE;
22332        }
22333      }
22334    }
22335  }
22336  return SQLITE_OK;
22337}
22338
22339/*
22340** Initialize resources required in RECOVER_STATE_LOSTANDFOUND3 
22341** state - during which the lost-and-found table of the output database 
22342** is populated with recovered data that can not be assigned to any 
22343** recovered schema object.
22344*/ 
22345static void recoverLostAndFound3Init(sqlite3_recover *p){
22346  RecoverStateLAF *pLaf = &p->laf;
22347
22348  if( pLaf->nMaxField>0 ){
22349    char *zTab = 0;               /* Name of lost_and_found table */
22350
22351    zTab = recoverLostAndFoundCreate(p, pLaf->nMaxField);
22352    pLaf->pInsert = recoverLostAndFoundInsert(p, zTab, pLaf->nMaxField);
22353    sqlite3_free(zTab);
22354
22355    pLaf->pAllPage = recoverPreparePrintf(p, p->dbOut,
22356        "WITH RECURSIVE seq(ii) AS ("
22357        "  SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
22358        ")"
22359        "SELECT ii FROM seq" , p->laf.nPg
22360    );
22361    pLaf->pPageData = recoverPrepare(p, p->dbOut,
22362        "SELECT cell, field, value "
22363        "FROM sqlite_dbdata('getpage()') d WHERE d.pgno=? "
22364        "UNION ALL "
22365        "SELECT -1, -1, -1"
22366    );
22367
22368    pLaf->apVal = (sqlite3_value**)recoverMalloc(p, 
22369        pLaf->nMaxField*sizeof(sqlite3_value*)
22370    );
22371  }
22372}
22373
22374/*
22375** Initialize resources required in RECOVER_STATE_WRITING state - during which
22376** tables recovered from the schema of the input database are populated with
22377** recovered data.
22378*/ 
22379static int recoverWriteDataInit(sqlite3_recover *p){
22380  RecoverStateW1 *p1 = &p->w1;
22381  RecoverTable *pTbl = 0;
22382  int nByte = 0;
22383
22384  /* Figure out the maximum number of columns for any table in the schema */
22385  assert( p1->nMax==0 );
22386  for(pTbl=p->pTblList; pTbl; pTbl=pTbl->pNext){
22387    if( pTbl->nCol>p1->nMax ) p1->nMax = pTbl->nCol;
22388  }
22389
22390  /* Allocate an array of (sqlite3_value*) in which to accumulate the values
22391  ** that will be written to the output database in a single row. */
22392  nByte = sizeof(sqlite3_value*) * (p1->nMax+1);
22393  p1->apVal = (sqlite3_value**)recoverMalloc(p, nByte);
22394  if( p1->apVal==0 ) return p->errCode;
22395
22396  /* Prepare the SELECT to loop through schema tables (pTbls) and the SELECT
22397  ** to loop through cells that appear to belong to a single table (pSel). */
22398  p1->pTbls = recoverPrepare(p, p->dbOut,
22399      "SELECT rootpage FROM recovery.schema "
22400      "  WHERE type='table' AND (sql NOT LIKE 'create virtual%')"
22401      "  ORDER BY (tbl_name='sqlite_sequence') ASC"
22402  );
22403  p1->pSel = recoverPrepare(p, p->dbOut, 
22404      "WITH RECURSIVE pages(page) AS ("
22405      "  SELECT ?1"
22406      "    UNION"
22407      "  SELECT child FROM sqlite_dbptr('getpage()'), pages "
22408      "    WHERE pgno=page"
22409      ") "
22410      "SELECT page, cell, field, value "
22411      "FROM sqlite_dbdata('getpage()') d, pages p WHERE p.page=d.pgno "
22412      "UNION ALL "
22413      "SELECT 0, 0, 0, 0"
22414  );
22415
22416  return p->errCode;
22417}
22418
22419/*
22420** Clean up resources allocated by recoverWriteDataInit() (stuff in 
22421** sqlite3_recover.w1).
22422*/
22423static void recoverWriteDataCleanup(sqlite3_recover *p){
22424  RecoverStateW1 *p1 = &p->w1;
22425  int ii;
22426  for(ii=0; ii<p1->nVal; ii++){
22427    sqlite3_value_free(p1->apVal[ii]);
22428  }
22429  sqlite3_free(p1->apVal);
22430  recoverFinalize(p, p1->pInsert);
22431  recoverFinalize(p, p1->pTbls);
22432  recoverFinalize(p, p1->pSel);
22433  memset(p1, 0, sizeof(*p1));
22434}
22435
22436/*
22437** Perform one step (sqlite3_recover_step()) of work for the connection 
22438** passed as the only argument, which is guaranteed to be in
22439** RECOVER_STATE_WRITING state - during which tables recovered from the
22440** schema of the input database are populated with recovered data.
22441*/ 
22442static int recoverWriteDataStep(sqlite3_recover *p){
22443  RecoverStateW1 *p1 = &p->w1;
22444  sqlite3_stmt *pSel = p1->pSel;
22445  sqlite3_value **apVal = p1->apVal;
22446
22447  if( p->errCode==SQLITE_OK && p1->pTab==0 ){
22448    if( sqlite3_step(p1->pTbls)==SQLITE_ROW ){
22449      i64 iRoot = sqlite3_column_int64(p1->pTbls, 0);
22450      p1->pTab = recoverFindTable(p, iRoot);
22451
22452      recoverFinalize(p, p1->pInsert);
22453      p1->pInsert = 0;
22454
22455      /* If this table is unknown, return early. The caller will invoke this
22456      ** function again and it will move on to the next table.  */
22457      if( p1->pTab==0 ) return p->errCode;
22458
22459      /* If this is the sqlite_sequence table, delete any rows added by
22460      ** earlier INSERT statements on tables with AUTOINCREMENT primary
22461      ** keys before recovering its contents. The p1->pTbls SELECT statement
22462      ** is rigged to deliver "sqlite_sequence" last of all, so we don't
22463      ** worry about it being modified after it is recovered. */
22464      if( sqlite3_stricmp("sqlite_sequence", p1->pTab->zTab)==0 ){
22465        recoverExec(p, p->dbOut, "DELETE FROM sqlite_sequence");
22466        recoverSqlCallback(p, "DELETE FROM sqlite_sequence");
22467      }
22468
22469      /* Bind the root page of this table within the original database to 
22470      ** SELECT statement p1->pSel. The SELECT statement will then iterate
22471      ** through cells that look like they belong to table pTab.  */
22472      sqlite3_bind_int64(pSel, 1, iRoot);
22473
22474      p1->nVal = 0;
22475      p1->bHaveRowid = 0;
22476      p1->iPrevPage = -1;
22477      p1->iPrevCell = -1;
22478    }else{
22479      return SQLITE_DONE;
22480    }
22481  }
22482  assert( p->errCode!=SQLITE_OK || p1->pTab );
22483
22484  if( p->errCode==SQLITE_OK && sqlite3_step(pSel)==SQLITE_ROW ){
22485    RecoverTable *pTab = p1->pTab;
22486
22487    i64 iPage = sqlite3_column_int64(pSel, 0);
22488    int iCell = sqlite3_column_int(pSel, 1);
22489    int iField = sqlite3_column_int(pSel, 2);
22490    sqlite3_value *pVal = sqlite3_column_value(pSel, 3);
22491    int bNewCell = (p1->iPrevPage!=iPage || p1->iPrevCell!=iCell);
22492
22493    assert( bNewCell==0 || (iField==-1 || iField==0) );
22494    assert( bNewCell || iField==p1->nVal || p1->nVal==pTab->nCol );
22495
22496    if( bNewCell ){
22497      int ii = 0;
22498      if( p1->nVal>=0 ){
22499        if( p1->pInsert==0 || p1->nVal!=p1->nInsert ){
22500          recoverFinalize(p, p1->pInsert);
22501          p1->pInsert = recoverInsertStmt(p, pTab, p1->nVal);
22502          p1->nInsert = p1->nVal;
22503        }
22504        if( p1->nVal>0 ){
22505          sqlite3_stmt *pInsert = p1->pInsert;
22506          for(ii=0; ii<pTab->nCol; ii++){
22507            RecoverColumn *pCol = &pTab->aCol[ii];
22508            int iBind = pCol->iBind;
22509            if( iBind>0 ){
22510              if( pCol->bIPK ){
22511                sqlite3_bind_int64(pInsert, iBind, p1->iRowid);
22512              }else if( pCol->iField<p1->nVal ){
22513                recoverBindValue(p, pInsert, iBind, apVal[pCol->iField]);
22514              }
22515            }
22516          }
22517          if( p->bRecoverRowid && pTab->iRowidBind>0 && p1->bHaveRowid ){
22518            sqlite3_bind_int64(pInsert, pTab->iRowidBind, p1->iRowid);
22519          }
22520          if( SQLITE_ROW==sqlite3_step(pInsert) ){
22521            const char *z = (const char*)sqlite3_column_text(pInsert, 0);
22522            recoverSqlCallback(p, z);
22523          }
22524          recoverReset(p, pInsert);
22525          assert( p->errCode || pInsert );
22526          if( pInsert ) sqlite3_clear_bindings(pInsert);
22527        }
22528      }
22529
22530      for(ii=0; ii<p1->nVal; ii++){
22531        sqlite3_value_free(apVal[ii]);
22532        apVal[ii] = 0;
22533      }
22534      p1->nVal = -1;
22535      p1->bHaveRowid = 0;
22536    }
22537
22538    if( iPage!=0 ){
22539      if( iField<0 ){
22540        p1->iRowid = sqlite3_column_int64(pSel, 3);
22541        assert( p1->nVal==-1 );
22542        p1->nVal = 0;
22543        p1->bHaveRowid = 1;
22544      }else if( iField<pTab->nCol ){
22545        assert( apVal[iField]==0 );
22546        apVal[iField] = sqlite3_value_dup( pVal );
22547        if( apVal[iField]==0 ){
22548          recoverError(p, SQLITE_NOMEM, 0);
22549        }
22550        p1->nVal = iField+1;
22551      }else if( pTab->nCol==0 ){
22552        p1->nVal = pTab->nCol;
22553      }
22554      p1->iPrevCell = iCell;
22555      p1->iPrevPage = iPage;
22556    }
22557  }else{
22558    recoverReset(p, pSel);
22559    p1->pTab = 0;
22560  }
22561
22562  return p->errCode;
22563}
22564
22565/*
22566** Initialize resources required by sqlite3_recover_step() in
22567** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
22568** already allocated to a recovered schema element is determined.
22569*/ 
22570static void recoverLostAndFound1Init(sqlite3_recover *p){
22571  RecoverStateLAF *pLaf = &p->laf;
22572  sqlite3_stmt *pStmt = 0;
22573
22574  assert( p->laf.pUsed==0 );
22575  pLaf->nPg = recoverPageCount(p);
22576  pLaf->pUsed = recoverBitmapAlloc(p, pLaf->nPg);
22577
22578  /* Prepare a statement to iterate through all pages that are part of any tree
22579  ** in the recoverable part of the input database schema to the bitmap. And,
22580  ** if !p->bFreelistCorrupt, add all pages that appear to be part of the
22581  ** freelist.  */
22582  pStmt = recoverPrepare(
22583      p, p->dbOut,
22584      "WITH trunk(pgno) AS ("
22585      "  SELECT read_i32(getpage(1), 8) AS x WHERE x>0"
22586      "    UNION"
22587      "  SELECT read_i32(getpage(trunk.pgno), 0) AS x FROM trunk WHERE x>0"
22588      "),"
22589      "trunkdata(pgno, data) AS ("
22590      "  SELECT pgno, getpage(pgno) FROM trunk"
22591      "),"
22592      "freelist(data, n, freepgno) AS ("
22593      "  SELECT data, min(16384, read_i32(data, 1)-1), pgno FROM trunkdata"
22594      "    UNION ALL"
22595      "  SELECT data, n-1, read_i32(data, 2+n) FROM freelist WHERE n>=0"
22596      "),"
22597      ""
22598      "roots(r) AS ("
22599      "  SELECT 1 UNION ALL"
22600      "  SELECT rootpage FROM recovery.schema WHERE rootpage>0"
22601      "),"
22602      "used(page) AS ("
22603      "  SELECT r FROM roots"
22604      "    UNION"
22605      "  SELECT child FROM sqlite_dbptr('getpage()'), used "
22606      "    WHERE pgno=page"
22607      ") "
22608      "SELECT page FROM used"
22609      " UNION ALL "
22610      "SELECT freepgno FROM freelist WHERE NOT ?"
22611  );
22612  if( pStmt ) sqlite3_bind_int(pStmt, 1, p->bFreelistCorrupt);
22613  pLaf->pUsedPages = pStmt;
22614}
22615
22616/*
22617** Perform one step (sqlite3_recover_step()) of work for the connection 
22618** passed as the only argument, which is guaranteed to be in
22619** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
22620** already allocated to a recovered schema element is determined.
22621*/ 
22622static int recoverLostAndFound1Step(sqlite3_recover *p){
22623  RecoverStateLAF *pLaf = &p->laf;
22624  int rc = p->errCode;
22625  if( rc==SQLITE_OK ){
22626    rc = sqlite3_step(pLaf->pUsedPages);
22627    if( rc==SQLITE_ROW ){
22628      i64 iPg = sqlite3_column_int64(pLaf->pUsedPages, 0);
22629      recoverBitmapSet(pLaf->pUsed, iPg);
22630      rc = SQLITE_OK;
22631    }else{
22632      recoverFinalize(p, pLaf->pUsedPages);
22633      pLaf->pUsedPages = 0;
22634    }
22635  }
22636  return rc;
22637}
22638
22639/*
22640** Initialize resources required by RECOVER_STATE_LOSTANDFOUND2 
22641** state - during which the pages identified in RECOVER_STATE_LOSTANDFOUND1
22642** are sorted into sets that likely belonged to the same database tree.
22643*/ 
22644static void recoverLostAndFound2Init(sqlite3_recover *p){
22645  RecoverStateLAF *pLaf = &p->laf;
22646
22647  assert( p->laf.pAllAndParent==0 );
22648  assert( p->laf.pMapInsert==0 );
22649  assert( p->laf.pMaxField==0 );
22650  assert( p->laf.nMaxField==0 );
22651
22652  pLaf->pMapInsert = recoverPrepare(p, p->dbOut,
22653      "INSERT OR IGNORE INTO recovery.map(pgno, parent) VALUES(?, ?)"
22654  );
22655  pLaf->pAllAndParent = recoverPreparePrintf(p, p->dbOut,
22656      "WITH RECURSIVE seq(ii) AS ("
22657      "  SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
22658      ")"
22659      "SELECT pgno, child FROM sqlite_dbptr('getpage()') "
22660      " UNION ALL "
22661      "SELECT NULL, ii FROM seq", p->laf.nPg
22662  );
22663  pLaf->pMaxField = recoverPreparePrintf(p, p->dbOut,
22664      "SELECT max(field)+1 FROM sqlite_dbdata('getpage') WHERE pgno = ?"
22665  );
22666}
22667
22668/*
22669** Perform one step (sqlite3_recover_step()) of work for the connection 
22670** passed as the only argument, which is guaranteed to be in
22671** RECOVER_STATE_LOSTANDFOUND2 state - during which the pages identified 
22672** in RECOVER_STATE_LOSTANDFOUND1 are sorted into sets that likely belonged 
22673** to the same database tree.
22674*/ 
22675static int recoverLostAndFound2Step(sqlite3_recover *p){
22676  RecoverStateLAF *pLaf = &p->laf;
22677  if( p->errCode==SQLITE_OK ){
22678    int res = sqlite3_step(pLaf->pAllAndParent);
22679    if( res==SQLITE_ROW ){
22680      i64 iChild = sqlite3_column_int(pLaf->pAllAndParent, 1);
22681      if( recoverBitmapQuery(pLaf->pUsed, iChild)==0 ){
22682        sqlite3_bind_int64(pLaf->pMapInsert, 1, iChild);
22683        sqlite3_bind_value(pLaf->pMapInsert, 2, 
22684            sqlite3_column_value(pLaf->pAllAndParent, 0)
22685        );
22686        sqlite3_step(pLaf->pMapInsert);
22687        recoverReset(p, pLaf->pMapInsert);
22688        sqlite3_bind_int64(pLaf->pMaxField, 1, iChild);
22689        if( SQLITE_ROW==sqlite3_step(pLaf->pMaxField) ){
22690          int nMax = sqlite3_column_int(pLaf->pMaxField, 0);
22691          if( nMax>pLaf->nMaxField ) pLaf->nMaxField = nMax;
22692        }
22693        recoverReset(p, pLaf->pMaxField);
22694      }
22695    }else{
22696      recoverFinalize(p, pLaf->pAllAndParent);
22697      pLaf->pAllAndParent =0;
22698      return SQLITE_DONE;
22699    }
22700  }
22701  return p->errCode;
22702}
22703
22704/*
22705** Free all resources allocated as part of sqlite3_recover_step() calls
22706** in one of the RECOVER_STATE_LOSTANDFOUND[123] states.
22707*/
22708static void recoverLostAndFoundCleanup(sqlite3_recover *p){
22709  recoverBitmapFree(p->laf.pUsed);
22710  p->laf.pUsed = 0;
22711  sqlite3_finalize(p->laf.pUsedPages);
22712  sqlite3_finalize(p->laf.pAllAndParent);
22713  sqlite3_finalize(p->laf.pMapInsert);
22714  sqlite3_finalize(p->laf.pMaxField);
22715  sqlite3_finalize(p->laf.pFindRoot);
22716  sqlite3_finalize(p->laf.pInsert);
22717  sqlite3_finalize(p->laf.pAllPage);
22718  sqlite3_finalize(p->laf.pPageData);
22719  p->laf.pUsedPages = 0;
22720  p->laf.pAllAndParent = 0;
22721  p->laf.pMapInsert = 0;
22722  p->laf.pMaxField = 0;
22723  p->laf.pFindRoot = 0;
22724  p->laf.pInsert = 0;
22725  p->laf.pAllPage = 0;
22726  p->laf.pPageData = 0;
22727  sqlite3_free(p->laf.apVal);
22728  p->laf.apVal = 0;
22729}
22730
22731/*
22732** Free all resources allocated as part of sqlite3_recover_step() calls.
22733*/
22734static void recoverFinalCleanup(sqlite3_recover *p){
22735  RecoverTable *pTab = 0;
22736  RecoverTable *pNext = 0;
22737
22738  recoverWriteDataCleanup(p);
22739  recoverLostAndFoundCleanup(p);
22740
22741  for(pTab=p->pTblList; pTab; pTab=pNext){
22742    pNext = pTab->pNext;
22743    sqlite3_free(pTab);
22744  }
22745  p->pTblList = 0;
22746  sqlite3_finalize(p->pGetPage);
22747  p->pGetPage = 0;
22748  sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0);
22749
22750  {
22751#ifndef NDEBUG
22752    int res = 
22753#endif
22754       sqlite3_close(p->dbOut);
22755    assert( res==SQLITE_OK );
22756  }
22757  p->dbOut = 0;
22758}
22759
22760/*
22761** Decode and return an unsigned 16-bit big-endian integer value from 
22762** buffer a[].
22763*/
22764static u32 recoverGetU16(const u8 *a){
22765  return (((u32)a[0])<<8) + ((u32)a[1]);
22766}
22767
22768/*
22769** Decode and return an unsigned 32-bit big-endian integer value from 
22770** buffer a[].
22771*/
22772static u32 recoverGetU32(const u8 *a){
22773  return (((u32)a[0])<<24) + (((u32)a[1])<<16) + (((u32)a[2])<<8) + ((u32)a[3]);
22774}
22775
22776/*
22777** Decode an SQLite varint from buffer a[]. Write the decoded value to (*pVal)
22778** and return the number of bytes consumed.
22779*/
22780static int recoverGetVarint(const u8 *a, i64 *pVal){
22781  sqlite3_uint64 u = 0;
22782  int i;
22783  for(i=0; i<8; i++){
22784    u = (u<<7) + (a[i]&0x7f);
22785    if( (a[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; }
22786  }
22787  u = (u<<8) + (a[i]&0xff);
22788  *pVal = (sqlite3_int64)u;
22789  return 9;
22790}
22791
22792/*
22793** The second argument points to a buffer n bytes in size. If this buffer
22794** or a prefix thereof appears to contain a well-formed SQLite b-tree page, 
22795** return the page-size in bytes. Otherwise, if the buffer does not 
22796** appear to contain a well-formed b-tree page, return 0.
22797*/
22798static int recoverIsValidPage(u8 *aTmp, const u8 *a, int n){
22799  u8 *aUsed = aTmp;
22800  int nFrag = 0;
22801  int nActual = 0;
22802  int iFree = 0;
22803  int nCell = 0;                  /* Number of cells on page */
22804  int iCellOff = 0;               /* Offset of cell array in page */
22805  int iContent = 0;
22806  int eType = 0;
22807  int ii = 0;
22808
22809  eType = (int)a[0];
22810  if( eType!=0x02 && eType!=0x05 && eType!=0x0A && eType!=0x0D ) return 0;
22811
22812  iFree = (int)recoverGetU16(&a[1]);
22813  nCell = (int)recoverGetU16(&a[3]);
22814  iContent = (int)recoverGetU16(&a[5]);
22815  if( iContent==0 ) iContent = 65536;
22816  nFrag = (int)a[7];
22817
22818  if( iContent>n ) return 0;
22819
22820  memset(aUsed, 0, n);
22821  memset(aUsed, 0xFF, iContent);
22822
22823  /* Follow the free-list. This is the same format for all b-tree pages. */
22824  if( iFree && iFree<=iContent ) return 0;
22825  while( iFree ){
22826    int iNext = 0;
22827    int nByte = 0;
22828    if( iFree>(n-4) ) return 0;
22829    iNext = recoverGetU16(&a[iFree]);
22830    nByte = recoverGetU16(&a[iFree+2]);
22831    if( iFree+nByte>n || nByte<4 ) return 0;
22832    if( iNext && iNext<iFree+nByte ) return 0;
22833    memset(&aUsed[iFree], 0xFF, nByte);
22834    iFree = iNext;
22835  }
22836
22837  /* Run through the cells */
22838  if( eType==0x02 || eType==0x05 ){
22839    iCellOff = 12;
22840  }else{
22841    iCellOff = 8;
22842  }
22843  if( (iCellOff + 2*nCell)>iContent ) return 0;
22844  for(ii=0; ii<nCell; ii++){
22845    int iByte;
22846    i64 nPayload = 0;
22847    int nByte = 0;
22848    int iOff = recoverGetU16(&a[iCellOff + 2*ii]);
22849    if( iOff<iContent || iOff>n ){
22850      return 0;
22851    }
22852    if( eType==0x05 || eType==0x02 ) nByte += 4;
22853    nByte += recoverGetVarint(&a[iOff+nByte], &nPayload);
22854    if( eType==0x0D ){
22855      i64 dummy = 0;
22856      nByte += recoverGetVarint(&a[iOff+nByte], &dummy);
22857    }
22858    if( eType!=0x05 ){
22859      int X = (eType==0x0D) ? n-35 : (((n-12)*64/255)-23);
22860      int M = ((n-12)*32/255)-23;
22861      int K = M+((nPayload-M)%(n-4));
22862
22863      if( nPayload<X ){
22864        nByte += nPayload;
22865      }else if( K<=X ){
22866        nByte += K+4;
22867      }else{
22868        nByte += M+4;
22869      }
22870    }
22871
22872    if( iOff+nByte>n ){
22873      return 0;
22874    }
22875    for(iByte=iOff; iByte<(iOff+nByte); iByte++){
22876      if( aUsed[iByte]!=0 ){
22877        return 0;
22878      }
22879      aUsed[iByte] = 0xFF;
22880    }
22881  }
22882
22883  nActual = 0;
22884  for(ii=0; ii<n; ii++){
22885    if( aUsed[ii]==0 ) nActual++;
22886  }
22887  return (nActual==nFrag);
22888}
22889
22890
22891static int recoverVfsClose(sqlite3_file*);
22892static int recoverVfsRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
22893static int recoverVfsWrite(sqlite3_file*, const void*, int, sqlite3_int64);
22894static int recoverVfsTruncate(sqlite3_file*, sqlite3_int64 size);
22895static int recoverVfsSync(sqlite3_file*, int flags);
22896static int recoverVfsFileSize(sqlite3_file*, sqlite3_int64 *pSize);
22897static int recoverVfsLock(sqlite3_file*, int);
22898static int recoverVfsUnlock(sqlite3_file*, int);
22899static int recoverVfsCheckReservedLock(sqlite3_file*, int *pResOut);
22900static int recoverVfsFileControl(sqlite3_file*, int op, void *pArg);
22901static int recoverVfsSectorSize(sqlite3_file*);
22902static int recoverVfsDeviceCharacteristics(sqlite3_file*);
22903static int recoverVfsShmMap(sqlite3_file*, int, int, int, void volatile**);
22904static int recoverVfsShmLock(sqlite3_file*, int offset, int n, int flags);
22905static void recoverVfsShmBarrier(sqlite3_file*);
22906static int recoverVfsShmUnmap(sqlite3_file*, int deleteFlag);
22907static int recoverVfsFetch(sqlite3_file*, sqlite3_int64, int, void**);
22908static int recoverVfsUnfetch(sqlite3_file *pFd, sqlite3_int64 iOff, void *p);
22909
22910static sqlite3_io_methods recover_methods = {
22911  2, /* iVersion */
22912  recoverVfsClose,
22913  recoverVfsRead,
22914  recoverVfsWrite,
22915  recoverVfsTruncate,
22916  recoverVfsSync,
22917  recoverVfsFileSize,
22918  recoverVfsLock,
22919  recoverVfsUnlock,
22920  recoverVfsCheckReservedLock,
22921  recoverVfsFileControl,
22922  recoverVfsSectorSize,
22923  recoverVfsDeviceCharacteristics,
22924  recoverVfsShmMap,
22925  recoverVfsShmLock,
22926  recoverVfsShmBarrier,
22927  recoverVfsShmUnmap,
22928  recoverVfsFetch,
22929  recoverVfsUnfetch
22930};
22931
22932static int recoverVfsClose(sqlite3_file *pFd){
22933  assert( pFd->pMethods!=&recover_methods );
22934  return pFd->pMethods->xClose(pFd);
22935}
22936
22937/*
22938** Write value v to buffer a[] as a 16-bit big-endian unsigned integer.
22939*/
22940static void recoverPutU16(u8 *a, u32 v){
22941  a[0] = (v>>8) & 0x00FF;
22942  a[1] = (v>>0) & 0x00FF;
22943}
22944
22945/*
22946** Write value v to buffer a[] as a 32-bit big-endian unsigned integer.
22947*/
22948static void recoverPutU32(u8 *a, u32 v){
22949  a[0] = (v>>24) & 0x00FF;
22950  a[1] = (v>>16) & 0x00FF;
22951  a[2] = (v>>8) & 0x00FF;
22952  a[3] = (v>>0) & 0x00FF;
22953}
22954
22955/*
22956** Detect the page-size of the database opened by file-handle pFd by 
22957** searching the first part of the file for a well-formed SQLite b-tree 
22958** page. If parameter nReserve is non-zero, then as well as searching for
22959** a b-tree page with zero reserved bytes, this function searches for one
22960** with nReserve reserved bytes at the end of it.
22961**
22962** If successful, set variable p->detected_pgsz to the detected page-size
22963** in bytes and return SQLITE_OK. Or, if no error occurs but no valid page
22964** can be found, return SQLITE_OK but leave p->detected_pgsz set to 0. Or,
22965** if an error occurs (e.g. an IO or OOM error), then an SQLite error code
22966** is returned. The final value of p->detected_pgsz is undefined in this
22967** case.
22968*/
22969static int recoverVfsDetectPagesize(
22970  sqlite3_recover *p,             /* Recover handle */
22971  sqlite3_file *pFd,              /* File-handle open on input database */
22972  u32 nReserve,                   /* Possible nReserve value */
22973  i64 nSz                         /* Size of database file in bytes */
22974){
22975  int rc = SQLITE_OK;
22976  const int nMin = 512;
22977  const int nMax = 65536;
22978  const int nMaxBlk = 4;
22979  u32 pgsz = 0;
22980  int iBlk = 0;
22981  u8 *aPg = 0;
22982  u8 *aTmp = 0;
22983  int nBlk = 0;
22984
22985  aPg = (u8*)sqlite3_malloc(2*nMax);
22986  if( aPg==0 ) return SQLITE_NOMEM;
22987  aTmp = &aPg[nMax];
22988
22989  nBlk = (nSz+nMax-1)/nMax;
22990  if( nBlk>nMaxBlk ) nBlk = nMaxBlk;
22991
22992  do {
22993    for(iBlk=0; rc==SQLITE_OK && iBlk<nBlk; iBlk++){
22994      int nByte = (nSz>=((iBlk+1)*nMax)) ? nMax : (nSz % nMax);
22995      memset(aPg, 0, nMax);
22996      rc = pFd->pMethods->xRead(pFd, aPg, nByte, iBlk*nMax);
22997      if( rc==SQLITE_OK ){
22998        int pgsz2;
22999        for(pgsz2=(pgsz ? pgsz*2 : nMin); pgsz2<=nMax; pgsz2=pgsz2*2){
23000          int iOff;
23001          for(iOff=0; iOff<nMax; iOff+=pgsz2){
23002            if( recoverIsValidPage(aTmp, &aPg[iOff], pgsz2-nReserve) ){
23003              pgsz = pgsz2;
23004              break;
23005            }
23006          }
23007        }
23008      }
23009    }
23010    if( pgsz>(u32)p->detected_pgsz ){
23011      p->detected_pgsz = pgsz;
23012      p->nReserve = nReserve;
23013    }
23014    if( nReserve==0 ) break;
23015    nReserve = 0;
23016  }while( 1 );
23017
23018  p->detected_pgsz = pgsz;
23019  sqlite3_free(aPg);
23020  return rc;
23021}
23022
23023/*
23024** The xRead() method of the wrapper VFS. This is used to intercept calls
23025** to read page 1 of the input database.
23026*/
23027static int recoverVfsRead(sqlite3_file *pFd, void *aBuf, int nByte, i64 iOff){
23028  int rc = SQLITE_OK;
23029  if( pFd->pMethods==&recover_methods ){
23030    pFd->pMethods = recover_g.pMethods;
23031    rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
23032    if( nByte==16 ){
23033      sqlite3_randomness(16, aBuf);
23034    }else
23035    if( rc==SQLITE_OK && iOff==0 && nByte>=108 ){
23036      /* Ensure that the database has a valid header file. The only fields
23037      ** that really matter to recovery are:
23038      **
23039      **   + Database page size (16-bits at offset 16)
23040      **   + Size of db in pages (32-bits at offset 28)
23041      **   + Database encoding (32-bits at offset 56)
23042      **
23043      ** Also preserved are:
23044      **
23045      **   + first freelist page (32-bits at offset 32)
23046      **   + size of freelist (32-bits at offset 36)
23047      **   + the wal-mode flags (16-bits at offset 18)
23048      **
23049      ** We also try to preserve the auto-vacuum, incr-value, user-version
23050      ** and application-id fields - all 32 bit quantities at offsets 
23051      ** 52, 60, 64 and 68. All other fields are set to known good values.
23052      **
23053      ** Byte offset 105 should also contain the page-size as a 16-bit 
23054      ** integer.
23055      */
23056      const int aPreserve[] = {32, 36, 52, 60, 64, 68};
23057      u8 aHdr[108] = {
23058        0x53, 0x51, 0x4c, 0x69, 0x74, 0x65, 0x20, 0x66, 
23059        0x6f, 0x72, 0x6d, 0x61, 0x74, 0x20, 0x33, 0x00,
23060        0xFF, 0xFF, 0x01, 0x01, 0x00, 0x40, 0x20, 0x20,
23061        0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
23062        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
23063        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
23064        0x00, 0x00, 0x10, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
23065        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
23066        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
23067        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23068        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23069        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23070        0x00, 0x2e, 0x5b, 0x30,
23071
23072        0x0D, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00
23073      };
23074      u8 *a = (u8*)aBuf;
23075
23076      u32 pgsz = recoverGetU16(&a[16]);
23077      u32 nReserve = a[20];
23078      u32 enc = recoverGetU32(&a[56]);
23079      u32 dbsz = 0;
23080      i64 dbFileSize = 0;
23081      int ii;
23082      sqlite3_recover *p = recover_g.p;
23083
23084      if( pgsz==0x01 ) pgsz = 65536;
23085      rc = pFd->pMethods->xFileSize(pFd, &dbFileSize);
23086
23087      if( rc==SQLITE_OK && p->detected_pgsz==0 ){
23088        rc = recoverVfsDetectPagesize(p, pFd, nReserve, dbFileSize);
23089      }
23090      if( p->detected_pgsz ){
23091        pgsz = p->detected_pgsz;
23092        nReserve = p->nReserve;
23093      }
23094
23095      if( pgsz ){
23096        dbsz = dbFileSize / pgsz;
23097      }
23098      if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16BE && enc!=SQLITE_UTF16LE ){
23099        enc = SQLITE_UTF8;
23100      }
23101
23102      sqlite3_free(p->pPage1Cache);
23103      p->pPage1Cache = 0;
23104      p->pPage1Disk = 0;
23105
23106      p->pgsz = nByte;
23107      p->pPage1Cache = (u8*)recoverMalloc(p, nByte*2);
23108      if( p->pPage1Cache ){
23109        p->pPage1Disk = &p->pPage1Cache[nByte];
23110        memcpy(p->pPage1Disk, aBuf, nByte);
23111        aHdr[18] = a[18];
23112        aHdr[19] = a[19];
23113        recoverPutU32(&aHdr[28], dbsz);
23114        recoverPutU32(&aHdr[56], enc);
23115        recoverPutU16(&aHdr[105], pgsz-nReserve);
23116        if( pgsz==65536 ) pgsz = 1;
23117        recoverPutU16(&aHdr[16], pgsz);
23118        aHdr[20] = nReserve;
23119        for(ii=0; ii<(int)(sizeof(aPreserve)/sizeof(aPreserve[0])); ii++){
23120          memcpy(&aHdr[aPreserve[ii]], &a[aPreserve[ii]], 4);
23121        }
23122        memcpy(aBuf, aHdr, sizeof(aHdr));
23123        memset(&((u8*)aBuf)[sizeof(aHdr)], 0, nByte-sizeof(aHdr));
23124
23125        memcpy(p->pPage1Cache, aBuf, nByte);
23126      }else{
23127        rc = p->errCode;
23128      }
23129
23130    }
23131    pFd->pMethods = &recover_methods;
23132  }else{
23133    rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
23134  }
23135  return rc;
23136}
23137
23138/*
23139** Used to make sqlite3_io_methods wrapper methods less verbose.
23140*/
23141#define RECOVER_VFS_WRAPPER(code)                         \
23142  int rc = SQLITE_OK;                                     \
23143  if( pFd->pMethods==&recover_methods ){                  \
23144    pFd->pMethods = recover_g.pMethods;                   \
23145    rc = code;                                            \
23146    pFd->pMethods = &recover_methods;                     \
23147  }else{                                                  \
23148    rc = code;                                            \
23149  }                                                       \
23150  return rc;                                              
23151
23152/*
23153** Methods of the wrapper VFS. All methods except for xRead() and xClose()
23154** simply uninstall the sqlite3_io_methods wrapper, invoke the equivalent
23155** method on the lower level VFS, then reinstall the wrapper before returning.
23156** Those that return an integer value use the RECOVER_VFS_WRAPPER macro.
23157*/
23158static int recoverVfsWrite(
23159  sqlite3_file *pFd, const void *aBuf, int nByte, i64 iOff
23160){
23161  RECOVER_VFS_WRAPPER (
23162      pFd->pMethods->xWrite(pFd, aBuf, nByte, iOff)
23163  );
23164}
23165static int recoverVfsTruncate(sqlite3_file *pFd, sqlite3_int64 size){
23166  RECOVER_VFS_WRAPPER (
23167      pFd->pMethods->xTruncate(pFd, size)
23168  );
23169}
23170static int recoverVfsSync(sqlite3_file *pFd, int flags){
23171  RECOVER_VFS_WRAPPER (
23172      pFd->pMethods->xSync(pFd, flags)
23173  );
23174}
23175static int recoverVfsFileSize(sqlite3_file *pFd, sqlite3_int64 *pSize){
23176  RECOVER_VFS_WRAPPER (
23177      pFd->pMethods->xFileSize(pFd, pSize)
23178  );
23179}
23180static int recoverVfsLock(sqlite3_file *pFd, int eLock){
23181  RECOVER_VFS_WRAPPER (
23182      pFd->pMethods->xLock(pFd, eLock)
23183  );
23184}
23185static int recoverVfsUnlock(sqlite3_file *pFd, int eLock){
23186  RECOVER_VFS_WRAPPER (
23187      pFd->pMethods->xUnlock(pFd, eLock)
23188  );
23189}
23190static int recoverVfsCheckReservedLock(sqlite3_file *pFd, int *pResOut){
23191  RECOVER_VFS_WRAPPER (
23192      pFd->pMethods->xCheckReservedLock(pFd, pResOut)
23193  );
23194}
23195static int recoverVfsFileControl(sqlite3_file *pFd, int op, void *pArg){
23196  RECOVER_VFS_WRAPPER (
23197    (pFd->pMethods ?  pFd->pMethods->xFileControl(pFd, op, pArg) : SQLITE_NOTFOUND)
23198  );
23199}
23200static int recoverVfsSectorSize(sqlite3_file *pFd){
23201  RECOVER_VFS_WRAPPER (
23202      pFd->pMethods->xSectorSize(pFd)
23203  );
23204}
23205static int recoverVfsDeviceCharacteristics(sqlite3_file *pFd){
23206  RECOVER_VFS_WRAPPER (
23207      pFd->pMethods->xDeviceCharacteristics(pFd)
23208  );
23209}
23210static int recoverVfsShmMap(
23211  sqlite3_file *pFd, int iPg, int pgsz, int bExtend, void volatile **pp
23212){
23213  RECOVER_VFS_WRAPPER (
23214      pFd->pMethods->xShmMap(pFd, iPg, pgsz, bExtend, pp)
23215  );
23216}
23217static int recoverVfsShmLock(sqlite3_file *pFd, int offset, int n, int flags){
23218  RECOVER_VFS_WRAPPER (
23219      pFd->pMethods->xShmLock(pFd, offset, n, flags)
23220  );
23221}
23222static void recoverVfsShmBarrier(sqlite3_file *pFd){
23223  if( pFd->pMethods==&recover_methods ){
23224    pFd->pMethods = recover_g.pMethods;
23225    pFd->pMethods->xShmBarrier(pFd);
23226    pFd->pMethods = &recover_methods;
23227  }else{
23228    pFd->pMethods->xShmBarrier(pFd);
23229  }
23230}
23231static int recoverVfsShmUnmap(sqlite3_file *pFd, int deleteFlag){
23232  RECOVER_VFS_WRAPPER (
23233      pFd->pMethods->xShmUnmap(pFd, deleteFlag)
23234  );
23235}
23236
23237static int recoverVfsFetch(
23238  sqlite3_file *pFd, 
23239  sqlite3_int64 iOff, 
23240  int iAmt, 
23241  void **pp
23242){
23243  (void)pFd;
23244  (void)iOff;
23245  (void)iAmt;
23246  *pp = 0;
23247  return SQLITE_OK;
23248}
23249static int recoverVfsUnfetch(sqlite3_file *pFd, sqlite3_int64 iOff, void *p){
23250  (void)pFd;
23251  (void)iOff;
23252  (void)p;
23253  return SQLITE_OK;
23254}
23255
23256/*
23257** Install the VFS wrapper around the file-descriptor open on the input
23258** database for recover handle p. Mutex RECOVER_MUTEX_ID must be held
23259** when this function is called.
23260*/
23261static void recoverInstallWrapper(sqlite3_recover *p){
23262  sqlite3_file *pFd = 0;
23263  assert( recover_g.pMethods==0 );
23264  recoverAssertMutexHeld();
23265  sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_FILE_POINTER, (void*)&pFd);
23266  assert( pFd==0 || pFd->pMethods!=&recover_methods );
23267  if( pFd && pFd->pMethods ){
23268    int iVersion = 1 + (pFd->pMethods->iVersion>1 && pFd->pMethods->xShmMap!=0);
23269    recover_g.pMethods = pFd->pMethods;
23270    recover_g.p = p;
23271    recover_methods.iVersion = iVersion;
23272    pFd->pMethods = &recover_methods;
23273  }
23274}
23275
23276/*
23277** Uninstall the VFS wrapper that was installed around the file-descriptor open
23278** on the input database for recover handle p. Mutex RECOVER_MUTEX_ID must be
23279** held when this function is called.
23280*/
23281static void recoverUninstallWrapper(sqlite3_recover *p){
23282  sqlite3_file *pFd = 0;
23283  recoverAssertMutexHeld();
23284  sqlite3_file_control(p->dbIn, p->zDb,SQLITE_FCNTL_FILE_POINTER,(void*)&pFd);
23285  if( pFd && pFd->pMethods ){
23286    pFd->pMethods = recover_g.pMethods;
23287    recover_g.pMethods = 0;
23288    recover_g.p = 0;
23289  }
23290}
23291
23292/*
23293** This function does the work of a single sqlite3_recover_step() call. It
23294** is guaranteed that the handle is not in an error state when this
23295** function is called.
23296*/
23297static void recoverStep(sqlite3_recover *p){
23298  assert( p && p->errCode==SQLITE_OK );
23299  switch( p->eState ){
23300    case RECOVER_STATE_INIT: {
23301      int bUseWrapper = 1;
23302      /* This is the very first call to sqlite3_recover_step() on this object.
23303      */
23304      recoverSqlCallback(p, "BEGIN");
23305      recoverSqlCallback(p, "PRAGMA writable_schema = on");
23306      recoverSqlCallback(p, "PRAGMA foreign_keys = off");
23307
23308      recoverEnterMutex();
23309
23310      /* Open the output database. And register required virtual tables and 
23311      ** user functions with the new handle. */
23312      recoverOpenOutput(p);
23313
23314      /* Attempt to open a transaction and read page 1 of the input database.
23315      ** Two attempts may be made - one with a wrapper installed to ensure
23316      ** that the database header is sane, and then if that attempt returns
23317      ** SQLITE_NOTADB, then again with no wrapper. The second attempt is
23318      ** required for encrypted databases.  */
23319      if( p->errCode==SQLITE_OK ){
23320        do{
23321          p->errCode = SQLITE_OK;
23322          if( bUseWrapper ) recoverInstallWrapper(p);
23323
23324          /* Open a transaction on the input database. */
23325          sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0);
23326          recoverExec(p, p->dbIn, "PRAGMA writable_schema = on");
23327          recoverExec(p, p->dbIn, "BEGIN");
23328          if( p->errCode==SQLITE_OK ) p->bCloseTransaction = 1;
23329          recoverExec(p, p->dbIn, "SELECT 1 FROM sqlite_schema");
23330          recoverTransferSettings(p);
23331          recoverOpenRecovery(p);
23332          recoverCacheSchema(p);
23333
23334          if( bUseWrapper ) recoverUninstallWrapper(p);
23335        }while( p->errCode==SQLITE_NOTADB 
23336             && (bUseWrapper--) 
23337             && SQLITE_OK==recoverOneStmt(p->dbIn, "ROLLBACK")
23338        );
23339      }
23340
23341      recoverLeaveMutex();
23342      recoverExec(p, p->dbOut, "BEGIN");
23343      recoverWriteSchema1(p);
23344      p->eState = RECOVER_STATE_WRITING;
23345      break;
23346    }
23347      
23348    case RECOVER_STATE_WRITING: {
23349      if( p->w1.pTbls==0 ){
23350        recoverWriteDataInit(p);
23351      }
23352      if( SQLITE_DONE==recoverWriteDataStep(p) ){
23353        recoverWriteDataCleanup(p);
23354        if( p->zLostAndFound ){
23355          p->eState = RECOVER_STATE_LOSTANDFOUND1;
23356        }else{
23357          p->eState = RECOVER_STATE_SCHEMA2;
23358        }
23359      }
23360      break;
23361    }
23362
23363    case RECOVER_STATE_LOSTANDFOUND1: {
23364      if( p->laf.pUsed==0 ){
23365        recoverLostAndFound1Init(p);
23366      }
23367      if( SQLITE_DONE==recoverLostAndFound1Step(p) ){
23368        p->eState = RECOVER_STATE_LOSTANDFOUND2;
23369      }
23370      break;
23371    }
23372    case RECOVER_STATE_LOSTANDFOUND2: {
23373      if( p->laf.pAllAndParent==0 ){
23374        recoverLostAndFound2Init(p);
23375      }
23376      if( SQLITE_DONE==recoverLostAndFound2Step(p) ){
23377        p->eState = RECOVER_STATE_LOSTANDFOUND3;
23378      }
23379      break;
23380    }
23381
23382    case RECOVER_STATE_LOSTANDFOUND3: {
23383      if( p->laf.pInsert==0 ){
23384        recoverLostAndFound3Init(p);
23385      }
23386      if( SQLITE_DONE==recoverLostAndFound3Step(p) ){
23387        p->eState = RECOVER_STATE_SCHEMA2;
23388      }
23389      break;
23390    }
23391
23392    case RECOVER_STATE_SCHEMA2: {
23393      int rc = SQLITE_OK;
23394
23395      recoverWriteSchema2(p);
23396      p->eState = RECOVER_STATE_DONE;
23397
23398      /* If no error has occurred, commit the write transaction on the output
23399      ** database. Regardless of whether or not an error has occurred, make
23400      ** an attempt to end the read transaction on the input database.  */
23401      recoverExec(p, p->dbOut, "COMMIT");
23402      rc = recoverOneStmt(p->dbIn, "END");
23403      if( p->errCode==SQLITE_OK ) p->errCode = rc;
23404
23405      recoverSqlCallback(p, "PRAGMA writable_schema = off");
23406      recoverSqlCallback(p, "COMMIT");
23407      p->eState = RECOVER_STATE_DONE;
23408      recoverFinalCleanup(p);
23409      break;
23410    };
23411
23412    case RECOVER_STATE_DONE: {
23413      /* no-op */
23414      break;
23415    };
23416  }
23417}
23418
23419
23420/*
23421** This is a worker function that does the heavy lifting for both init
23422** functions:
23423**
23424**     sqlite3_recover_init()
23425**     sqlite3_recover_init_sql()
23426**
23427** All this function does is allocate space for the recover handle and
23428** take copies of the input parameters. All the real work is done within
23429** sqlite3_recover_run().
23430*/
23431sqlite3_recover *recoverInit(
23432  sqlite3* db, 
23433  const char *zDb, 
23434  const char *zUri,               /* Output URI for _recover_init() */
23435  int (*xSql)(void*, const char*),/* SQL callback for _recover_init_sql() */
23436  void *pSqlCtx                   /* Context arg for _recover_init_sql() */
23437){
23438  sqlite3_recover *pRet = 0;
23439  int nDb = 0;
23440  int nUri = 0;
23441  int nByte = 0;
23442
23443  if( zDb==0 ){ zDb = "main"; }
23444
23445  nDb = recoverStrlen(zDb);
23446  nUri = recoverStrlen(zUri);
23447
23448  nByte = sizeof(sqlite3_recover) + nDb+1 + nUri+1;
23449  pRet = (sqlite3_recover*)sqlite3_malloc(nByte);
23450  if( pRet ){
23451    memset(pRet, 0, nByte);
23452    pRet->dbIn = db;
23453    pRet->zDb = (char*)&pRet[1];
23454    pRet->zUri = &pRet->zDb[nDb+1];
23455    memcpy(pRet->zDb, zDb, nDb);
23456    if( nUri>0 && zUri ) memcpy(pRet->zUri, zUri, nUri);
23457    pRet->xSql = xSql;
23458    pRet->pSqlCtx = pSqlCtx;
23459    pRet->bRecoverRowid = RECOVER_ROWID_DEFAULT;
23460  }
23461
23462  return pRet;
23463}
23464
23465/*
23466** Initialize a recovery handle that creates a new database containing
23467** the recovered data.
23468*/
23469sqlite3_recover *sqlite3_recover_init(
23470  sqlite3* db, 
23471  const char *zDb, 
23472  const char *zUri
23473){
23474  return recoverInit(db, zDb, zUri, 0, 0);
23475}
23476
23477/*
23478** Initialize a recovery handle that returns recovered data in the
23479** form of SQL statements via a callback.
23480*/
23481sqlite3_recover *sqlite3_recover_init_sql(
23482  sqlite3* db, 
23483  const char *zDb, 
23484  int (*xSql)(void*, const char*),
23485  void *pSqlCtx
23486){
23487  return recoverInit(db, zDb, 0, xSql, pSqlCtx);
23488}
23489
23490/*
23491** Return the handle error message, if any.
23492*/
23493const char *sqlite3_recover_errmsg(sqlite3_recover *p){
23494  return (p && p->errCode!=SQLITE_NOMEM) ? p->zErrMsg : "out of memory";
23495}
23496
23497/*
23498** Return the handle error code.
23499*/
23500int sqlite3_recover_errcode(sqlite3_recover *p){
23501  return p ? p->errCode : SQLITE_NOMEM;
23502}
23503
23504/*
23505** Configure the handle.
23506*/
23507int sqlite3_recover_config(sqlite3_recover *p, int op, void *pArg){
23508  int rc = SQLITE_OK;
23509  if( p==0 ){
23510    rc = SQLITE_NOMEM;
23511  }else if( p->eState!=RECOVER_STATE_INIT ){
23512    rc = SQLITE_MISUSE;
23513  }else{
23514    switch( op ){
23515      case 789:
23516        /* This undocumented magic configuration option is used to set the
23517        ** name of the auxiliary database that is ATTACH-ed to the database
23518        ** connection and used to hold state information during the
23519        ** recovery process.  This option is for debugging use only and
23520        ** is subject to change or removal at any time. */
23521        sqlite3_free(p->zStateDb);
23522        p->zStateDb = recoverMPrintf(p, "%s", (char*)pArg);
23523        break;
23524
23525      case SQLITE_RECOVER_LOST_AND_FOUND: {
23526        const char *zArg = (const char*)pArg;
23527        sqlite3_free(p->zLostAndFound);
23528        if( zArg ){
23529          p->zLostAndFound = recoverMPrintf(p, "%s", zArg);
23530        }else{
23531          p->zLostAndFound = 0;
23532        }
23533        break;
23534      }
23535
23536      case SQLITE_RECOVER_FREELIST_CORRUPT:
23537        p->bFreelistCorrupt = *(int*)pArg;
23538        break;
23539
23540      case SQLITE_RECOVER_ROWIDS:
23541        p->bRecoverRowid = *(int*)pArg;
23542        break;
23543
23544      case SQLITE_RECOVER_SLOWINDEXES:
23545        p->bSlowIndexes = *(int*)pArg;
23546        break;
23547
23548      default:
23549        rc = SQLITE_NOTFOUND;
23550        break;
23551    }
23552  }
23553
23554  return rc;
23555}
23556
23557/*
23558** Do a unit of work towards the recovery job. Return SQLITE_OK if
23559** no error has occurred but database recovery is not finished, SQLITE_DONE
23560** if database recovery has been successfully completed, or an SQLite
23561** error code if an error has occurred.
23562*/
23563int sqlite3_recover_step(sqlite3_recover *p){
23564  if( p==0 ) return SQLITE_NOMEM;
23565  if( p->errCode==SQLITE_OK ) recoverStep(p);
23566  if( p->eState==RECOVER_STATE_DONE && p->errCode==SQLITE_OK ){
23567    return SQLITE_DONE;
23568  }
23569  return p->errCode;
23570}
23571
23572/*
23573** Do the configured recovery operation. Return SQLITE_OK if successful, or
23574** else an SQLite error code.
23575*/
23576int sqlite3_recover_run(sqlite3_recover *p){
23577  while( SQLITE_OK==sqlite3_recover_step(p) );
23578  return sqlite3_recover_errcode(p);
23579}
23580
23581
23582/*
23583** Free all resources associated with the recover handle passed as the only
23584** argument. The results of using a handle with any sqlite3_recover_**
23585** API function after it has been passed to this function are undefined.
23586**
23587** A copy of the value returned by the first call made to sqlite3_recover_run()
23588** on this handle is returned, or SQLITE_OK if sqlite3_recover_run() has
23589** not been called on this handle.
23590*/
23591int sqlite3_recover_finish(sqlite3_recover *p){
23592  int rc;
23593  if( p==0 ){
23594    rc = SQLITE_NOMEM;
23595  }else{
23596    recoverFinalCleanup(p);
23597    if( p->bCloseTransaction && sqlite3_get_autocommit(p->dbIn)==0 ){
23598      rc = recoverOneStmt(p->dbIn, "END");
23599      if( p->errCode==SQLITE_OK ) p->errCode = rc;
23600    }
23601    rc = p->errCode;
23602    sqlite3_free(p->zErrMsg);
23603    sqlite3_free(p->zStateDb);
23604    sqlite3_free(p->zLostAndFound);
23605    sqlite3_free(p->pPage1Cache);
23606    sqlite3_free(p);
23607  }
23608  return rc;
23609}
23610
23611#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
23612
23613/************************* End ext/recover/sqlite3recover.c ********************/
23614# endif /* SQLITE_HAVE_SQLITE3R */
23615#endif
23616#ifdef SQLITE_SHELL_EXTSRC
23617# include SHELL_STRINGIFY(SQLITE_SHELL_EXTSRC)
23618#endif
23619
23620#if defined(SQLITE_ENABLE_SESSION)
23621/*
23622** State information for a single open session
23623*/
23624typedef struct OpenSession OpenSession;
23625struct OpenSession {
23626  char *zName;             /* Symbolic name for this session */
23627  int nFilter;             /* Number of xFilter rejection GLOB patterns */
23628  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
23629  sqlite3_session *p;      /* The open session */
23630};
23631#endif
23632
23633#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
23634typedef struct ExpertInfo ExpertInfo;
23635struct ExpertInfo {
23636  sqlite3expert *pExpert;
23637  int bVerbose;
23638};
23639#endif
23640
23641/* All the parameters that determine how to render query results.
23642*/
23643typedef struct Mode {
23644  u8 autoExplain;        /* Automatically turn on .explain mode */
23645  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to each SQL stmt */
23646  u8 autoEQPtrace;       /* autoEQP is in trace mode */
23647  u8 scanstatsOn;        /* True to display scan stats before each finalize */
23648  u8 bAutoScreenWidth;   /* Using the TTY to determine screen width */
23649  u8 mFlags;             /* MFLG_ECHO, MFLG_CRLF, etc. */
23650  u8 eMode;              /* One of the MODE_ values */
23651  sqlite3_qrf_spec spec; /* Spec to be passed into QRF */
23652} Mode;
23653
23654/* Flags for Mode.mFlags */
23655#define MFLG_ECHO  0x01  /* Echo inputs to output */
23656#define MFLG_CRLF  0x02  /* Use CR/LF output line endings */
23657#define MFLG_HDR   0x04  /* .header used to change headers on/off */
23658
23659
23660/*
23661** State information about the database connection is contained in an
23662** instance of the following structure.
23663*/
23664typedef struct ShellState ShellState;
23665struct ShellState {
23666  sqlite3 *db;           /* The database */
23667  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
23668  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
23669  u8 nEqpLevel;          /* Depth of the EQP output graph */
23670  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
23671  u8 bSafeMode;          /* True to prohibit unsafe operations */
23672  u8 bSafeModePersist;   /* The long-term value of bSafeMode */
23673  u8 eRestoreState;      /* See comments above doAutoDetectRestore() */
23674  unsigned statsOn;      /* True to display memory stats before each finalize */
23675  unsigned mEqpLines;    /* Mask of vertical lines in the EQP output graph */
23676  u8 nPopOutput;         /* Revert .output settings when reaching zero */
23677  u8 nPopMode;           /* Revert .mode settings when reaching zero */
23678  u8 enableTimer;        /* Enable the timer.  2: permanently 1: only once */
23679  int inputNesting;      /* Track nesting level of .read and other redirects */
23680  double prevTimer;      /* Last reported timer value */
23681  double tmProgress;     /* --timeout option for .progress */
23682  i64 lineno;            /* Line number of last line read from in */
23683  const char *zInFile;   /* Name of the input file */
23684  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
23685  FILE *in;              /* Read commands from this stream */
23686  FILE *out;             /* Write results here */
23687  FILE *traceOut;        /* Output for sqlite3_trace() */
23688  int nErr;              /* Number of errors seen */
23689  int writableSchema;    /* True if PRAGMA writable_schema=ON */
23690  int nCheck;            /* Number of ".check" commands run */
23691  unsigned nProgress;    /* Number of progress callbacks encountered */
23692  unsigned mxProgress;   /* Maximum progress callbacks before failing */
23693  unsigned flgProgress;  /* Flags for the progress callback */
23694  unsigned shellFlgs;    /* Various flags */
23695  unsigned nTestRun;     /* Number of test cases run */
23696  unsigned nTestErr;     /* Number of test cases that failed */
23697  sqlite3_int64 szMax;   /* --maxsize argument to .open */
23698  char *zDestTable;      /* Name of destination table when MODE_Insert */
23699  char *zTempFile;       /* Temporary file that might need deleting */
23700  char *zErrPrefix;      /* Alternative error message prefix */
23701  char zTestcase[30];    /* Name of current test case */
23702  char outfile[FILENAME_MAX]; /* Filename for *out */
23703  sqlite3_stmt *pStmt;   /* Current statement if any. */
23704  FILE *pLog;            /* Write log output here */
23705  Mode mode;             /* Current display mode */
23706  Mode modePrior;        /* Backup */
23707  struct SavedMode {     /* Ability to define custom mode configurations */
23708    char *zTag;            /* Name of this saved mode */
23709    Mode mode;              /* The saved mode */
23710  } *aSavedModes;        /* Array of saved .mode settings. system malloc() */
23711  int nSavedModes;       /* Number of saved .mode settings */
23712  struct AuxDb {         /* Storage space for auxiliary database connections */
23713    sqlite3 *db;               /* Connection pointer */
23714    const char *zDbFilename;   /* Filename used to open the connection */
23715    char *zFreeOnClose;        /* Free this memory allocation on close */
23716#if defined(SQLITE_ENABLE_SESSION)
23717    int nSession;              /* Number of active sessions */
23718    OpenSession aSession[4];   /* Array of sessions.  [0] is in focus. */
23719#endif
23720  } aAuxDb[5],           /* Array of all database connections */
23721    *pAuxDb;             /* Currently active database connection */
23722  char *zNonce;          /* Nonce for temporary safe-mode escapes */
23723#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
23724  ExpertInfo expert;     /* Valid if previous command was ".expert OPT..." */
23725#endif
23726  struct DotCmdLine {    /* Info about arguments to a dot-command */
23727    const char *zOrig;      /* Original text of the dot-command */
23728    char *zCopy;            /* Copy of zOrig, from malloc() */
23729    int nAlloc;             /* Size of allocates for arrays below */
23730    int nArg;               /* Number of argument slots actually used */
23731    char **azArg;           /* Pointer to each argument, dequoted */
23732    int *aiOfst;            /* Offset into zOrig[] for start of each arg */
23733    char *abQuot;           /* True if the argment was originally quoted */
23734  } dot;
23735#ifdef SQLITE_SHELL_FIDDLE
23736  struct {
23737    const char * zInput; /* Input string from wasm/JS proxy */
23738    const char * zPos;   /* Cursor pos into zInput */
23739    const char * zDefaultDbName; /* Default name for db file */
23740  } wasm;
23741#endif
23742};
23743
23744#ifdef SQLITE_SHELL_FIDDLE
23745static ShellState shellState;
23746#endif
23747
23748
23749/* Allowed values for ShellState.mode.autoEQP
23750*/
23751#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
23752#define AUTOEQP_on       1           /* Automatic EQP is on */
23753#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
23754#define AUTOEQP_full     3           /* Show full EXPLAIN */
23755
23756/* Allowed values for ShellState.openMode
23757*/
23758#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
23759#define SHELL_OPEN_NORMAL      1      /* Normal database file */
23760#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
23761#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
23762#define SHELL_OPEN_DESERIALIZE 4      /* Open using sqlite3_deserialize() */
23763#define SHELL_OPEN_HEXDB       5      /* Use "dbtotxt" output as data source */
23764
23765/* Allowed values for ShellState.eTraceType
23766*/
23767#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
23768#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
23769#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
23770
23771/* Bits in the ShellState.flgProgress variable */
23772#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
23773#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progress
23774                                   ** callback limit is reached, and for each
23775                                   ** top-level SQL statement */
23776#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
23777#define SHELL_PROGRESS_TMOUT 0x08  /* Stop after tmProgress seconds */
23778
23779/* Names of values for Mode.spec.eEsc and Mode.spec.eText
23780*/
23781static const char *qrfEscNames[] = { "auto", "off", "ascii", "symbol" };
23782static const char *qrfQuoteNames[] = 
23783      { "off","off","sql","hex","csv","tcl","json","relaxed"};
23784
23785/*
23786** These are the allowed shellFlgs values
23787*/
23788#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
23789#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
23790#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
23791#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
23792#define SHFLG_NoErrLineno    0x00000010 /* Omit line numbers from error msgs */
23793#define SHFLG_CountChanges   0x00000020 /* .changes setting */
23794#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
23795#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
23796#define SHFLG_TestingMode    0x00000400 /* allow unsafe testing features */
23797
23798/*
23799** Macros for testing and setting shellFlgs
23800*/
23801#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
23802#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
23803#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
23804
23805/*
23806** These are the allowed values for Mode.eMode.  There is a lot of overlap
23807** between these values and the Mode.spec.eStyle values, but they are not
23808** one-to-one, and thus need to be tracked separately.
23809*/
23810#define MODE_Ascii     0  /* Use ASCII unit and record separators (0x1F/0x1E) */
23811#define MODE_Box       1  /* Unicode box-drawing characters */
23812#define MODE_C         2  /* Comma-separated list of C-strings */
23813#define MODE_Column    3  /* One record per line in neat columns */
23814#define MODE_Count     4  /* Output only a count of the rows of output */
23815#define MODE_Csv       5  /* Quote strings, numbers are plain */
23816#define MODE_Html      6  /* Generate an XHTML table */
23817#define MODE_Insert    7  /* Generate SQL "insert" statements */
23818#define MODE_JAtom     8  /* Comma-separated list of JSON atoms */
23819#define MODE_JObject   9  /* One JSON object per row */
23820#define MODE_Json     10  /* Output JSON */
23821#define MODE_Line     11  /* One column per line.  Blank line between records */
23822#define MODE_List     12  /* One record per line with a separator */
23823#define MODE_Markdown 13  /* Markdown formatting */
23824#define MODE_Off      14  /* No query output shown */
23825#define MODE_Psql     15  /* Similar to psql */
23826#define MODE_QBox     16  /* BOX with SQL-quoted content */
23827#define MODE_Quote    17  /* Quote values as for SQL */
23828#define MODE_Split    18  /* Split-column mode */
23829#define MODE_Table    19  /* MySQL-style table formatting */
23830#define MODE_Tabs     20  /* Tab-separated values */
23831#define MODE_Tcl      21  /* Space-separated list of TCL strings */
23832#define MODE_Www      22  /* Full web-page output */
23833
23834#define MODE_BUILTIN  22  /* Maximum built-in mode */
23835#define MODE_BATCH    50  /* Default mode for batch processing */
23836#define MODE_TTY      51  /* Default mode for interactive processing */
23837#define MODE_USER     75  /* First user-defined mode */
23838#define MODE_N_USER   25  /* Maximum number of user-defined modes */
23839
23840/*
23841** Information about built-in display modes
23842*/
23843typedef struct ModeInfo ModeInfo;
23844struct ModeInfo {
23845  char zName[9];         /* Symbolic name of the mode */
23846  unsigned char eCSep;   /* Column separator */
23847  unsigned char eRSep;   /* Row separator */
23848  unsigned char eNull;   /* Null representation */
23849  unsigned char eText;   /* Default text encoding */
23850  unsigned char eHdr;    /* Default header encoding. */
23851  unsigned char eBlob;   /* Default blob encoding. */
23852  unsigned char bHdr;    /* Show headers by default.  0: n/a, 1: no 2: yes */
23853  unsigned char eStyle;  /* Underlying QRF style */
23854  unsigned char eCx;     /* 0: other, 1: line, 2: columnar */
23855  unsigned char mFlg;    /* Flags. 1=border-off 2=split-column */
23856};
23857
23858/* String constants used by built-in modes */
23859static const char *aModeStr[] = 
23860  /* 0    1       2       3       4     5        6        7        8    */
23861   { 0,   "\n",   "|",    " ",    ",",  "\r\n",  "\036",  "\037",  "\t",
23862     "",  "NULL", "null", "\"\"", ": ",                                   };
23863  /* 9    10      11      12      13                                    */
23864
23865static const ModeInfo aModeInfo[] = {
23866/*   zName      eCSep  eRSep eNull eText eHdr eBlob bHdr eStyle eCx mFlg */
23867  { "ascii",    7,     6,    9,    1,    1,    0,   1,   12,    0,  0 },
23868  { "box",      0,     0,    9,    1,    1,    0,   2,   1,     2,  0 },
23869  { "c",        4,     1,    10,   5,    5,    4,   1,   12,    0,  0 },
23870  { "column",   0,     0,    9,    1,    1,    0,   2,   2,     2,  0 },
23871  { "count",    0,     0,    0,    0,    0,    0,   0,   3,     0,  0 },
23872  { "csv",      4,     5,    9,    3,    3,    0,   1,   12,    0,  0 },
23873  { "html",     0,     0,    9,    4,    4,    0,   2,   7,     0,  0 },
23874  { "insert",   0,     0,    10,   2,    2,    0,   1,   8,     0,  0 },
23875  { "jatom",    4,     1,    11,   6,    6,    0,   1,   12,    0,  0 },
23876  { "jobject",  0,     1,    11,   6,    6,    0,   0,   10,    0,  0 },
23877  { "json",     0,     0,    11,   6,    6,    0,   0,   9,     0,  0 },
23878  { "line",     13,    1,    9,    1,    1,    0,   0,   11,    1,  0 },
23879  { "list",     2,     1,    9,    1,    1,    0,   1,   12,    0,  0 },
23880  { "markdown", 0,     0,    9,    1,    1,    0,   2,   13,    2,  0 },
23881  { "off",      0,     0,    0,    0,    0,    0,   0,   14,    0,  0 },
23882  { "psql",     0,     0,    9,    1,    1,    0,   2,   19,    2,  1 },
23883  { "qbox",     0,     0,    10,   2,    1,    0,   2,   1,     2,  0 },
23884  { "quote",    4,     1,    10,   2,    2,    0,   1,   12,    0,  0 },
23885  { "split",    0,     0,    9,    1,    1,    0,   1,   2,     2,  2 },
23886  { "table",    0,     0,    9,    1,    1,    0,   2,   19,    2,  0 },
23887  { "tabs",     8,     1,    9,    3,    3,    0,   1,   12,    0,  0 },
23888  { "tcl",      3,     1,    12,   5,    5,    4,   1,   12,    0,  0 },
23889  { "www",      0,     0,    9,    4,    4,    0,   2,   7,     0,  0 }
23890};     /*       |     /     /      |     /    /     |    |       \
23891       **       |    /     /       |    /    /      |    |        \_ 2: columnar
23892       ** Index into aModeStr[]    |   /    /       |    |           1: line
23893       **                          |  /    /        |    |           0: other
23894       **                          | /    /         |     \
23895       **           text encoding  |/     |    show |      \
23896       **      v-------------------'      |   hdrs? |       The QRF style
23897       **      0: n/a                blob |   v-----'
23898       **      1: plain        v_---------'   0: n/a
23899       **      2: sql          0: auto        1: no         
23900       **      3: csv          1: as-text     2: yes
23901       **      4: html         2: sql
23902       **      5: c            3: hex
23903       **      6: json         4: c
23904       **                      5: json
23905       **                      6: size
23906       ******************************************************************/
23907/*
23908** These are the column/row/line separators used by the various
23909** import/export modes.
23910*/
23911#define SEP_Column    "|"
23912#define SEP_Row       "\n"
23913#define SEP_Tab       "\t"
23914#define SEP_Space     " "
23915#define SEP_Comma     ","
23916#define SEP_CrLf      "\r\n"
23917#define SEP_Unit      "\x1F"
23918#define SEP_Record    "\x1E"
23919
23920/*
23921** Default values for the various QRF limits
23922*/
23923#ifndef DFLT_CHAR_LIMIT
23924# define DFLT_CHAR_LIMIT  300
23925#endif
23926#ifndef DFLT_LINE_LIMIT
23927# define DFLT_LINE_LIMIT  5
23928#endif
23929#ifndef DFLT_TITLE_LIMIT
23930# define DFLT_TITLE_LIMIT 20
23931#endif
23932#ifndef DFLT_MULTI_INSERT
23933# define DFLT_MULTI_INSERT 3000
23934#endif
23935
23936/*
23937** If the following flag is set, then command execution stops
23938** at an error if we are not interactive.
23939*/
23940static int bail_on_error = 0;
23941
23942/*
23943** Treat stdin as an interactive input if the following variable
23944** is true.  Otherwise, assume stdin is connected to a file or pipe.
23945*/
23946static int stdin_is_interactive = 1;
23947
23948/*
23949** Treat stdout like a TTY if true.
23950*/
23951static int stdout_is_console = 1;
23952
23953/*
23954** Use this value as the width of the output device.  Or, figure it
23955** out at runtime if the value is negative.  Or use a default width
23956** if this value is zero.
23957*/
23958static int stdout_tty_width = -1;
23959
23960/*
23961** The following is the open SQLite database.  We make a pointer
23962** to this database a static variable so that it can be accessed
23963** by the SIGINT handler to interrupt database processing.
23964*/
23965static sqlite3 *globalDb = 0;
23966
23967/*
23968** True if an interrupt (Control-C) has been received.
23969*/
23970static volatile int seenInterrupt = 0;
23971
23972/*
23973** This is the name of our program. It is set in main(), used
23974** in a number of other places, mostly for error messages.
23975*/
23976static char *Argv0;
23977
23978/*
23979** Prompt strings. Initialized in main. Settable with
23980**   .prompt main continue
23981*/
23982#define PROMPT_LEN_MAX 128
23983/* First line prompt.   default: "sqlite> " */
23984static char mainPrompt[PROMPT_LEN_MAX];
23985/* Continuation prompt. default: "   ...> " */
23986static char continuePrompt[PROMPT_LEN_MAX];
23987
23988/*
23989** Write I/O traces to the following stream.
23990*/
23991#ifdef SQLITE_ENABLE_IOTRACE
23992static FILE *iotrace = 0;
23993#endif
23994
23995/*
23996** Output routines that are able to redirect to memory rather than
23997** doing actually I/O.
23998**                                                Works like.
23999**                                                --------------
24000**   cli_printf(FILE*, const char*, ...);         fprintf()
24001**   cli_puts(const char*, FILE*);                fputs()
24002**   cli_vprintf(FILE*, const char*, va_list);    vfprintf()
24003**
24004** These are just thin wrappers with the following added semantics:
24005** If the file-scope variable cli_output_capture is not NULL, and
24006** if the FILE* argument is stdout or stderr, then rather than
24007** writing to stdout/stdout, append the text to the cli_output_capture
24008** variable.
24009**
24010** The cli_exit(int) routine works like exit() except that it
24011** first dumps any capture output to stdout.
24012*/
24013static sqlite3_str *cli_output_capture = 0; 
24014static int cli_printf(FILE *out, const char *zFormat, ...){
24015  va_list ap;
24016  int rc;
24017  va_start(ap,zFormat);
24018  if( cli_output_capture && (out==stdout || out==stderr) ){
24019    sqlite3_str_vappendf(cli_output_capture, zFormat, ap);
24020    rc = 1;
24021  }else{
24022    rc = sqlite3_vfprintf(out, zFormat, ap);
24023  }
24024  va_end(ap);
24025  return rc;
24026}
24027static int cli_puts(const char *zText, FILE *out){
24028  if( cli_output_capture && (out==stdout || out==stderr) ){
24029    sqlite3_str_appendall(cli_output_capture, zText);
24030    return 1;
24031  }
24032  return sqlite3_fputs(zText, out);
24033}
24034#if 0 /* Not currently used - available if we need it later */
24035static int cli_vprintf(FILE *out, const char *zFormat, va_list ap){
24036  if( cli_output_capture && (out==stdout || out==stderr) ){
24037    sqlite3_str_vappendf(cli_output_capture, zFormat, ap);
24038    return 1;
24039  }else{
24040    return sqlite3_vfprintf(out, zFormat, ap);
24041  }
24042}
24043#endif
24044static void cli_exit(int rc){
24045  if( cli_output_capture ){
24046    char *z = sqlite3_str_finish(cli_output_capture);
24047    sqlite3_fputs(z, stdout);
24048    fflush(stdout);
24049  }
24050  exit(rc);
24051}
24052
24053
24054#define eputz(z) cli_puts(z,stderr)
24055#define sputz(fp,z) cli_puts(z,fp)
24056
24057/* A version of strcmp() that works with NULL values */
24058static int cli_strcmp(const char *a, const char *b){
24059  if( a==0 ) a = "";
24060  if( b==0 ) b = "";
24061  return strcmp(a,b);
24062}
24063static int cli_strncmp(const char *a, const char *b, size_t n){
24064  if( a==0 ) a = "";
24065  if( b==0 ) b = "";
24066  return strncmp(a,b,n);
24067}
24068
24069/* Return the current wall-clock time in microseconds since the
24070** Unix epoch (1970-01-01T00:00:00Z)
24071*/
24072static sqlite3_int64 timeOfDay(void){
24073#if defined(_WIN64) && _WIN32_WINNT >= _WIN32_WINNT_WIN8
24074  sqlite3_uint64 t;
24075  FILETIME tm;
24076  GetSystemTimePreciseAsFileTime(&tm);
24077  t =  ((u64)tm.dwHighDateTime<<32) | (u64)tm.dwLowDateTime;
24078  t += 116444736000000000LL;
24079  t /= 10;
24080  return t;
24081#elif defined(_WIN32)
24082  static sqlite3_vfs *clockVfs = 0;
24083  sqlite3_int64 t;
24084  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
24085  if( clockVfs==0 ) return 0;  /* Never actually happens */
24086  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
24087    clockVfs->xCurrentTimeInt64(clockVfs, &t);
24088  }else{
24089    double r;
24090    clockVfs->xCurrentTime(clockVfs, &r);
24091    t = (sqlite3_int64)(r*86400000.0);
24092  }
24093  return t*1000;
24094#else
24095  struct timeval sNow;
24096  (void)gettimeofday(&sNow,0);
24097  return ((i64)sNow.tv_sec)*1000000 + sNow.tv_usec;
24098#endif
24099}
24100
24101
24102
24103/* This is variant of the standard-library strncpy() routine with the
24104** one change that the destination string is always zero-terminated, even
24105** if there is no zero-terminator in the first n-1 characters of the source
24106** string.
24107*/
24108static char *shell_strncpy(char *dest, const char *src, size_t n){
24109  size_t i;
24110  for(i=0; i<n-1 && src[i]!=0; i++) dest[i] = src[i];
24111  dest[i] = 0;
24112  return dest;
24113}
24114
24115/*
24116** strcpy() workalike to squelch an unwarranted link-time warning
24117** from OpenBSD.
24118*/
24119static void shell_strcpy(char *dest, const char *src){
24120  while( (*(dest++) = *(src++))!=0 ){}
24121}
24122
24123/*
24124** Optionally disable dynamic continuation prompt.
24125** Unless disabled, the continuation prompt shows open SQL lexemes if any,
24126** or open parentheses level if non-zero, or continuation prompt as set.
24127** This facility interacts with the scanner and process_input() where the
24128** below 5 macros are used.
24129*/
24130#ifdef SQLITE_OMIT_DYNAPROMPT
24131# define CONTINUATION_PROMPT continuePrompt
24132# define CONTINUE_PROMPT_RESET
24133# define CONTINUE_PROMPT_AWAITS(p,s)
24134# define CONTINUE_PROMPT_AWAITC(p,c)
24135# define CONTINUE_PAREN_INCR(p,n)
24136# define CONTINUE_PROMPT_PSTATE 0
24137typedef void *t_NoDynaPrompt;
24138# define SCAN_TRACKER_REFTYPE t_NoDynaPrompt
24139#else
24140# define CONTINUATION_PROMPT dynamicContinuePrompt()
24141# define CONTINUE_PROMPT_RESET \
24142  do {setLexemeOpen(&dynPrompt,0,0); trackParenLevel(&dynPrompt,0);} while(0)
24143# define CONTINUE_PROMPT_AWAITS(p,s) \
24144  if(p && stdin_is_interactive) setLexemeOpen(p, s, 0)
24145# define CONTINUE_PROMPT_AWAITC(p,c) \
24146  if(p && stdin_is_interactive) setLexemeOpen(p, 0, c)
24147# define CONTINUE_PAREN_INCR(p,n) \
24148  if(p && stdin_is_interactive) (trackParenLevel(p,n))
24149# define CONTINUE_PROMPT_PSTATE (&dynPrompt)
24150typedef struct DynaPrompt *t_DynaPromptRef;
24151# define SCAN_TRACKER_REFTYPE t_DynaPromptRef
24152
24153static struct DynaPrompt {
24154  char dynamicPrompt[PROMPT_LEN_MAX];
24155  char acAwait[2];
24156  int inParenLevel;
24157  char *zScannerAwaits;
24158} dynPrompt = { {0}, {0}, 0, 0 };
24159
24160/* Record parenthesis nesting level change, or force level to 0. */
24161static void trackParenLevel(struct DynaPrompt *p, int ni){
24162  p->inParenLevel += ni;
24163  if( ni==0 ) p->inParenLevel = 0;
24164  p->zScannerAwaits = 0;
24165}
24166
24167/* Record that a lexeme is opened, or closed with args==0. */
24168static void setLexemeOpen(struct DynaPrompt *p, char *s, char c){
24169  if( s!=0 || c==0 ){
24170    p->zScannerAwaits = s;
24171    p->acAwait[0] = 0;
24172  }else{
24173    p->acAwait[0] = c;
24174    p->zScannerAwaits = p->acAwait;
24175  }
24176}
24177
24178/* Upon demand, derive the continuation prompt to display. */
24179static char *dynamicContinuePrompt(void){
24180  if( continuePrompt[0]==0
24181      || (dynPrompt.zScannerAwaits==0 && dynPrompt.inParenLevel == 0) ){
24182    return continuePrompt;
24183  }else{
24184    if( dynPrompt.zScannerAwaits ){
24185      size_t ncp = strlen(continuePrompt);
24186      size_t ndp = strlen(dynPrompt.zScannerAwaits);
24187      if( ndp > ncp-3 ) return continuePrompt;
24188      shell_strcpy(dynPrompt.dynamicPrompt, dynPrompt.zScannerAwaits);
24189      while( ndp<3 ) dynPrompt.dynamicPrompt[ndp++] = ' ';
24190      shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3,
24191              PROMPT_LEN_MAX-4);
24192    }else{
24193      if( dynPrompt.inParenLevel>9 ){
24194        shell_strncpy(dynPrompt.dynamicPrompt, "(..", 4);
24195      }else if( dynPrompt.inParenLevel<0 ){
24196        shell_strncpy(dynPrompt.dynamicPrompt, ")x!", 4);
24197      }else{
24198        shell_strncpy(dynPrompt.dynamicPrompt, "(x.", 4);
24199        dynPrompt.dynamicPrompt[2] = (char)('0'+dynPrompt.inParenLevel);
24200      }
24201      shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3,
24202                    PROMPT_LEN_MAX-4);
24203    }
24204  }
24205  return dynPrompt.dynamicPrompt;
24206}
24207#endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
24208
24209/* Indicate out-of-memory and exit. */
24210static void shell_out_of_memory(void){
24211  eputz("Error: out of memory\n");
24212  cli_exit(1);
24213}
24214
24215/* Check a pointer to see if it is NULL.  If it is NULL, exit with an
24216** out-of-memory error.
24217*/
24218static void shell_check_oom(const void *p){
24219  if( p==0 ) shell_out_of_memory();
24220}
24221
24222/*
24223** This routine works like printf in that its first argument is a
24224** format string and subsequent arguments are values to be substituted
24225** in place of % fields.  The result of formatting this string
24226** is written to iotrace.
24227*/
24228#ifdef SQLITE_ENABLE_IOTRACE
24229static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
24230  va_list ap;
24231  char *z;
24232  if( iotrace==0 ) return;
24233  va_start(ap, zFormat);
24234  z = sqlite3_vmprintf(zFormat, ap);
24235  va_end(ap);
24236  cli_printf(iotrace, "%s", z);
24237  sqlite3_free(z);
24238}
24239#endif
24240
24241/*
24242** Compute a string length that is limited to what can be stored in
24243** lower 30 bits of a 32-bit signed integer.
24244*/
24245static int strlen30(const char *z){
24246  size_t n;
24247  if( z==0 ) return 0;
24248  n = strlen(z);
24249  return n>0x3fffffff ? 0x3fffffff : (int)n;
24250}
24251
24252/*
24253** Return open FILE * if zFile exists, can be opened for read
24254** and is an ordinary file or a character stream source.
24255** Otherwise return 0.
24256*/
24257static FILE * openChrSource(const char *zFile){
24258#if defined(_WIN32) || defined(WIN32)
24259  struct __stat64 x = {0};
24260# define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
24261  /* On Windows, open first, then check the stream nature. This order
24262  ** is necessary because _stat() and sibs, when checking a named pipe,
24263  ** effectively break the pipe as its supplier sees it. */
24264  FILE *rv = sqlite3_fopen(zFile, "rb");
24265  if( rv==0 ) return 0;
24266  if( _fstat64(_fileno(rv), &x) != 0
24267      || !STAT_CHR_SRC(x.st_mode)){
24268    fclose(rv);
24269    rv = 0;
24270  }
24271  return rv;
24272#else
24273  struct stat x = {0};
24274  int rc = stat(zFile, &x);
24275# define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
24276  if( rc!=0 ) return 0;
24277  if( STAT_CHR_SRC(x.st_mode) ){
24278    return sqlite3_fopen(zFile, "rb");
24279  }else{
24280    return 0;
24281  }
24282#endif
24283#undef STAT_CHR_SRC
24284}
24285
24286/*
24287** This routine reads a line of text from FILE in, stores
24288** the text in memory obtained from malloc() and returns a pointer
24289** to the text.  NULL is returned at end of file, or if malloc()
24290** fails, or if the length of the line is longer than about a gigabyte.
24291**
24292** If zLine is not NULL then it is a malloced buffer returned from
24293** a previous call to this routine that may be reused.
24294*/
24295static char *local_getline(char *zLine, FILE *in){
24296  int nLine = zLine==0 ? 0 : 100;
24297  int n = 0;
24298
24299  while( 1 ){
24300    if( n+100>nLine ){
24301      if( nLine>=1073741773 ){
24302        free(zLine);
24303        return 0;
24304      }
24305      nLine = nLine*2 + 100;
24306      zLine = realloc(zLine, nLine);
24307      shell_check_oom(zLine);
24308    }
24309    if( sqlite3_fgets(&zLine[n], nLine - n, in)==0 ){
24310      if( n==0 ){
24311        free(zLine);
24312        return 0;
24313      }
24314      zLine[n] = 0;
24315      break;
24316    }
24317    while( zLine[n] ) n++;
24318    if( n>0 && zLine[n-1]=='\n' ){
24319      n--;
24320      if( n>0 && zLine[n-1]=='\r' ) n--;
24321      zLine[n] = 0;
24322      break;
24323    }
24324  }
24325  return zLine;
24326}
24327
24328/*
24329** Retrieve a single line of input text.
24330**
24331** If in==0 then read from standard input and prompt before each line.
24332** If isContinuation is true, then a continuation prompt is appropriate.
24333** If isContinuation is zero, then the main prompt should be used.
24334**
24335** If zPrior is not NULL then it is a buffer from a prior call to this
24336** routine that can be reused.
24337**
24338** The result is stored in space obtained from malloc() and must either
24339** be freed by the caller or else passed back into this routine via the
24340** zPrior argument for reuse.
24341*/
24342#ifndef SQLITE_SHELL_FIDDLE
24343static char *one_input_line(ShellState *p, char *zPrior, int isContinuation){
24344  char *zPrompt;
24345  char *zResult;
24346  FILE *in = p->in;
24347  if( in!=0 ){
24348    zResult = local_getline(zPrior, in);
24349  }else{
24350    zPrompt = isContinuation ? CONTINUATION_PROMPT : mainPrompt;
24351#if SHELL_USE_LOCAL_GETLINE
24352    sputz(stdout, zPrompt);
24353    fflush(stdout);
24354    do{
24355      zResult = local_getline(zPrior, stdin);
24356      zPrior = 0;
24357      /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
24358      if( zResult==0 ) sqlite3_sleep(50);
24359    }while( zResult==0 && seenInterrupt>0 );
24360#else
24361    free(zPrior);
24362    zResult = shell_readline(zPrompt);
24363    while( zResult==0 ){
24364      /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
24365      sqlite3_sleep(50);
24366      if( seenInterrupt==0 ) break;
24367      zResult = shell_readline("");
24368    }
24369    if( zResult && *zResult ) shell_add_history(zResult);
24370#endif
24371  }
24372  return zResult;
24373}
24374#endif /* !SQLITE_SHELL_FIDDLE */
24375
24376/*
24377** Return the value of a hexadecimal digit.  Return -1 if the input
24378** is not a hex digit.
24379*/
24380static int hexDigitValue(char c){
24381  if( c>='0' && c<='9' ) return c - '0';
24382  if( c>='a' && c<='f' ) return c - 'a' + 10;
24383  if( c>='A' && c<='F' ) return c - 'A' + 10;
24384  return -1;
24385}
24386
24387/*
24388** Interpret zArg as an integer value, possibly with suffixes.
24389**
24390** If the value specified by zArg is outside the range of values that
24391** can be represented using a 64-bit twos-complement integer, then return
24392** the nearest representable value.
24393*/
24394static sqlite3_int64 integerValue(const char *zArg){
24395  sqlite3_uint64 v = 0;
24396  static const struct { char *zSuffix; unsigned int iMult; } aMult[] = {
24397    { "KiB", 1024 },
24398    { "MiB", 1024*1024 },
24399    { "GiB", 1024*1024*1024 },
24400    { "KB",  1000 },
24401    { "MB",  1000000 },
24402    { "GB",  1000000000 },
24403    { "K",   1000 },
24404    { "M",   1000000 },
24405    { "G",   1000000000 },
24406  };
24407  int i;
24408  int isNeg = 0;
24409  if( zArg[0]=='-' ){
24410    isNeg = 1;
24411    zArg++;
24412  }else if( zArg[0]=='+' ){
24413    zArg++;
24414  }
24415  if( zArg[0]=='0' && zArg[1]=='x' ){
24416    int x;
24417    zArg += 2;
24418    while( (x = hexDigitValue(zArg[0]))>=0 ){
24419      if( v > 0x0fffffffffffffffULL ) goto integer_overflow;
24420      v = (v<<4) + x;
24421      zArg++;
24422    }
24423  }else{
24424    while( IsDigit(zArg[0]) ){
24425      if( v>=922337203685477580LL ){
24426        if( v>922337203685477580LL || zArg[0]>='8' ) goto integer_overflow;
24427      }
24428      v = v*10 + (zArg[0] - '0');
24429      zArg++;
24430    }
24431  }
24432  for(i=0; i<ArraySize(aMult); i++){
24433    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
24434      if( 0x7fffffffffffffffULL/aMult[i].iMult < v ) goto integer_overflow;
24435      v *= aMult[i].iMult;
24436      break;
24437    }
24438  }
24439  if( isNeg && v>0x7fffffffffffffffULL ) goto integer_overflow;
24440  return isNeg? -(sqlite3_int64)v : (sqlite3_int64)v;
24441integer_overflow:
24442  return isNeg ? (i64)0x8000000000000000LL : 0x7fffffffffffffffLL;
24443}
24444
24445/*
24446** A variable length string to which one can append text.
24447*/
24448typedef struct ShellText ShellText;
24449struct ShellText {
24450  char *zTxt;       /* The text */
24451  i64 n;            /* Number of bytes of zTxt[] actually used */
24452  i64 nAlloc;       /* Number of bytes allocated for zTxt[] */
24453};
24454
24455/*
24456** Initialize and destroy a ShellText object
24457*/
24458static void initText(ShellText *p){
24459  memset(p, 0, sizeof(*p));
24460}
24461static void freeText(ShellText *p){
24462  sqlite3_free(p->zTxt);
24463  initText(p);
24464}
24465
24466/* zIn is either a pointer to a NULL-terminated string in memory obtained
24467** from malloc(), or a NULL pointer. The string pointed to by zAppend is
24468** added to zIn, and the result returned in memory obtained from malloc().
24469** zIn, if it was not NULL, is freed.
24470**
24471** If the third argument, quote, is not '\0', then it is used as a
24472** quote character for zAppend.
24473*/
24474static void appendText(ShellText *p, const char *zAppend, char quote){
24475  i64 len;
24476  i64 i;
24477  i64 nAppend = strlen30(zAppend);
24478
24479  len = nAppend+p->n+1;
24480  if( quote ){
24481    len += 2;
24482    for(i=0; i<nAppend; i++){
24483      if( zAppend[i]==quote ) len++;
24484    }
24485  }
24486
24487  if( p->zTxt==0 || p->n+len>=p->nAlloc ){
24488    p->nAlloc = p->nAlloc*2 + len + 20;
24489    p->zTxt = sqlite3_realloc64(p->zTxt, p->nAlloc);
24490    shell_check_oom(p->zTxt);
24491  }
24492
24493  if( quote ){
24494    char *zCsr = p->zTxt+p->n;
24495    *zCsr++ = quote;
24496    for(i=0; i<nAppend; i++){
24497      *zCsr++ = zAppend[i];
24498      if( zAppend[i]==quote ) *zCsr++ = quote;
24499    }
24500    *zCsr++ = quote;
24501    p->n = (i64)(zCsr - p->zTxt);
24502    *zCsr = '\0';
24503  }else{
24504    memcpy(p->zTxt+p->n, zAppend, nAppend);
24505    p->n += nAppend;
24506    p->zTxt[p->n] = '\0';
24507  }
24508}
24509
24510/*
24511** Attempt to determine if identifier zName needs to be quoted, either
24512** because it contains non-alphanumeric characters, or because it is an
24513** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
24514** that quoting is required.
24515**
24516** Return '"' if quoting is required.  Return 0 if no quoting is required.
24517*/
24518static char quoteChar(const char *zName){
24519  int i;
24520  if( zName==0 ) return '"';
24521  if( !IsAlpha(zName[0]) && zName[0]!='_' ) return '"';
24522  for(i=0; zName[i]; i++){
24523    if( !IsAlnum(zName[i]) && zName[i]!='_' ) return '"';
24524  }
24525  return sqlite3_keyword_check(zName, i) ? '"' : 0;
24526}
24527
24528/*
24529** Construct a fake object name and column list to describe the structure
24530** of the view, virtual table, or table valued function zSchema.zName.
24531**
24532** The returned string comes from sqlite3_mprintf() and should be freed
24533** by the caller using sqlite3_free().
24534*/
24535static char *shellFakeSchema(
24536  sqlite3 *db,            /* The database connection containing the vtab */
24537  const char *zSchema,    /* Schema of the database holding the vtab */
24538  const char *zName       /* The name of the virtual table */
24539){
24540  sqlite3_stmt *pStmt = 0;
24541  char *zSql;
24542  ShellText s;
24543  char cQuote;
24544  char *zDiv = "(";
24545  int nRow = 0;
24546
24547  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
24548                         zSchema ? zSchema : "main", zName);
24549  shell_check_oom(zSql);
24550  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
24551  sqlite3_free(zSql);
24552  initText(&s);
24553  if( zSchema ){
24554    cQuote = quoteChar(zSchema);
24555    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
24556    appendText(&s, zSchema, cQuote);
24557    appendText(&s, ".", 0);
24558  }
24559  cQuote = quoteChar(zName);
24560  appendText(&s, zName, cQuote);
24561  while( sqlite3_step(pStmt)==SQLITE_ROW ){
24562    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
24563    nRow++;
24564    appendText(&s, zDiv, 0);
24565    zDiv = ",";
24566    if( zCol==0 ) zCol = "";
24567    cQuote = quoteChar(zCol);
24568    appendText(&s, zCol, cQuote);
24569  }
24570  appendText(&s, ")", 0);
24571  sqlite3_finalize(pStmt);
24572  if( nRow==0 ){
24573    freeText(&s);
24574    s.zTxt = 0;
24575  }
24576  return s.zTxt;
24577}
24578
24579/*
24580** SQL function:  strtod(X)
24581**
24582** Use the C-library strtod() function to convert string X into a double.
24583** Used for comparing the accuracy of SQLite's internal text-to-float conversion
24584** routines against the C-library.
24585*/
24586static void shellStrtod(
24587  sqlite3_context *pCtx,
24588  int nVal,
24589  sqlite3_value **apVal
24590){
24591  char *z = (char*)sqlite3_value_text(apVal[0]);
24592  UNUSED_PARAMETER(nVal);
24593  if( z==0 ) return;
24594  sqlite3_result_double(pCtx, strtod(z,0));
24595}
24596
24597/*
24598** SQL function:  dtostr(X)
24599**
24600** Use the C-library printf() function to convert real value X into a string.
24601** Used for comparing the accuracy of SQLite's internal float-to-text conversion
24602** routines against the C-library.
24603*/
24604static void shellDtostr(
24605  sqlite3_context *pCtx,
24606  int nVal,
24607  sqlite3_value **apVal
24608){
24609  double r = sqlite3_value_double(apVal[0]);
24610  int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26;
24611  char z[400];
24612  if( n<1 ) n = 1;
24613  if( n>350 ) n = 350;
24614  sprintf(z, "%#+.*e", n, r);
24615  sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
24616}
24617
24618/*
24619** SQL function:  shell_add_schema(S,X)
24620**
24621** Add the schema name X to the CREATE statement in S and return the result.
24622** Examples:
24623**
24624**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
24625**
24626** Also works on
24627**
24628**    CREATE INDEX
24629**    CREATE UNIQUE INDEX
24630**    CREATE VIEW
24631**    CREATE TRIGGER
24632**    CREATE VIRTUAL TABLE
24633**
24634** This UDF is used by the .schema command to insert the schema name of
24635** attached databases into the middle of the sqlite_schema.sql field.
24636*/
24637static void shellAddSchemaName(
24638  sqlite3_context *pCtx,
24639  int nVal,
24640  sqlite3_value **apVal
24641){
24642  static const char *aPrefix[] = {
24643     "TABLE",
24644     "INDEX",
24645     "UNIQUE INDEX",
24646     "VIEW",
24647     "TRIGGER",
24648     "VIRTUAL TABLE"
24649  };
24650  int i = 0;
24651  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
24652  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
24653  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
24654  sqlite3 *db = sqlite3_context_db_handle(pCtx);
24655  UNUSED_PARAMETER(nVal);
24656  if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){
24657    for(i=0; i<ArraySize(aPrefix); i++){
24658      int n = strlen30(aPrefix[i]);
24659      if( cli_strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
24660        char *z = 0;
24661        char *zFake = 0;
24662        if( zSchema ){
24663          char cQuote = quoteChar(zSchema);
24664          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
24665            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
24666          }else{
24667            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
24668          }
24669        }
24670        if( zName
24671         && aPrefix[i][0]=='V'
24672         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
24673        ){
24674          if( z==0 ){
24675            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
24676          }else{
24677            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
24678          }
24679          sqlite3_free(zFake);
24680        }
24681        if( z ){
24682          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
24683          return;
24684        }
24685      }
24686    }
24687  }
24688  sqlite3_result_value(pCtx, apVal[0]);
24689}
24690
24691
24692/************************* BEGIN PERFORMANCE TIMER *****************************/
24693#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
24694#include <sys/time.h>
24695#include <sys/resource.h>
24696/* VxWorks does not support getrusage() as far as we can determine */
24697#if defined(_WRS_KERNEL) || defined(__RTP__)
24698struct rusage {
24699  struct timeval ru_utime; /* user CPU time used */
24700  struct timeval ru_stime; /* system CPU time used */
24701};
24702#define getrusage(A,B) memset(B,0,sizeof(*B))
24703#endif
24704
24705/* Saved resource information for the beginning of an operation */
24706static struct rusage sBegin;  /* CPU time at start */
24707static sqlite3_int64 iBegin;  /* Wall-clock time at start */
24708
24709/*
24710** Begin timing an operation
24711*/
24712static void beginTimer(ShellState *p){
24713  if( p->enableTimer || (p->flgProgress & SHELL_PROGRESS_TMOUT)!=0 ){
24714    getrusage(RUSAGE_SELF, &sBegin);
24715    iBegin = timeOfDay();
24716  }
24717}
24718
24719/* Return the difference of two time_structs in seconds */
24720static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
24721  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
24722         (double)(pEnd->tv_sec - pStart->tv_sec);
24723}
24724
24725#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
24726/* Return the time since the start of the timer in
24727** seconds. */
24728static double elapseTime(ShellState *NotUsed){
24729  (void)NotUsed;
24730  if( iBegin==0 ) return 0.0;
24731  return (timeOfDay() - iBegin)*0.000001;
24732}
24733#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
24734
24735/*
24736** Print the timing results.
24737*/
24738static void endTimer(ShellState *p){
24739  if( p->enableTimer ){
24740    sqlite3_int64 iEnd = timeOfDay();
24741    struct rusage sEnd;
24742    getrusage(RUSAGE_SELF, &sEnd);
24743    p->prevTimer = (iEnd - iBegin)*0.000001;
24744    cli_printf(p->out, "Run Time: real %.6f user %.6f sys %.6f\n",
24745          p->prevTimer,
24746          timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
24747          timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
24748    if( p->enableTimer==1 ) p->enableTimer = 0;
24749    iBegin = 0;
24750  }
24751}
24752
24753#define BEGIN_TIMER(X) beginTimer(X)
24754#define END_TIMER(X)   endTimer(X)
24755#define ELAPSE_TIME(X) elapseTime(X)
24756#define HAS_TIMER 1
24757
24758#elif (defined(_WIN32) || defined(WIN32))
24759
24760/* Saved resource information for the beginning of an operation */
24761static HANDLE hProcess;
24762static FILETIME ftKernelBegin;
24763static FILETIME ftUserBegin;
24764static sqlite3_int64 ftWallBegin;
24765typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
24766                                    LPFILETIME, LPFILETIME);
24767static GETPROCTIMES getProcessTimesAddr = NULL;
24768
24769/*
24770** Check to see if we have timer support.  Return 1 if necessary
24771** support found (or found previously).
24772*/
24773static int hasTimer(void){
24774  if( getProcessTimesAddr ){
24775    return 1;
24776  } else {
24777    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
24778    ** versions. See if the version we are running on has it, and if it
24779    ** does, save off a pointer to it and the current process handle.
24780    */
24781    hProcess = GetCurrentProcess();
24782    if( hProcess ){
24783      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
24784      if( NULL != hinstLib ){
24785        getProcessTimesAddr =
24786            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
24787        if( NULL != getProcessTimesAddr ){
24788          return 1;
24789        }
24790        FreeLibrary(hinstLib);
24791      }
24792    }
24793  }
24794  return 0;
24795}
24796
24797/*
24798** Begin timing an operation
24799*/
24800static void beginTimer(ShellState *p){
24801  if( (p->enableTimer || (p->flgProgress & SHELL_PROGRESS_TMOUT)!=0)
24802   && getProcessTimesAddr
24803  ){
24804    FILETIME ftCreation, ftExit;
24805    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
24806                        &ftKernelBegin,&ftUserBegin);
24807    ftWallBegin = timeOfDay();
24808  }
24809}
24810
24811/* Return the difference of two FILETIME structs in seconds */
24812static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
24813  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
24814  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
24815  return (double) ((i64End - i64Start) / 10000000.0);
24816}
24817
24818#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
24819/* Return the time since the start of the timer in
24820** seconds. */
24821static double elapseTime(ShellState *NotUsed){
24822  (void)NotUsed;
24823  if( ftWallBegin==0 ) return 0.0;
24824  return (timeOfDay() - ftWallBegin)*0.000001;
24825}
24826#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
24827
24828/*
24829** Print the timing results.
24830*/
24831static void endTimer(ShellState *p){
24832  if( p->enableTimer && getProcessTimesAddr){
24833    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
24834    sqlite3_int64 ftWallEnd = timeOfDay();
24835    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
24836    p->prevTimer = (ftWallEnd - ftWallBegin)*0.000001;
24837#ifdef _WIN64
24838    /* microsecond precision on 64-bit windows */
24839    cli_printf(p->out, "Run Time: real %.6f user %f sys %f\n",
24840          p->prevTimer,
24841          timeDiff(&ftUserBegin, &ftUserEnd),
24842          timeDiff(&ftKernelBegin, &ftKernelEnd));
24843#else
24844    /* millisecond precisino on 32-bit windows */
24845    cli_printf(p->out, "Run Time: real %.3f user %.3f sys %.3f\n",
24846          p->prevTimer,
24847          timeDiff(&ftUserBegin, &ftUserEnd),
24848          timeDiff(&ftKernelBegin, &ftKernelEnd));
24849#endif
24850    if( p->enableTimer==1 ) p->enableTimer = 0;
24851    ftWallBegin = 0;
24852  }
24853}
24854
24855#define BEGIN_TIMER(X) beginTimer(X)
24856#define ELAPSE_TIME(X) elapseTime(X)
24857#define END_TIMER(X)   endTimer(X)
24858#define HAS_TIMER      hasTimer()
24859
24860#else
24861#define BEGIN_TIMER(X) /* no-op */
24862#define ELAPSE_TIME(X) 0.0
24863#define END_TIMER(X)   /*no-op*/
24864#define HAS_TIMER 0
24865#endif
24866/************************* END PERFORMANCE TIMER ******************************/
24867
24868/*
24869** Clear a display mode, freeing any allocated memory that it
24870** contains.
24871*/
24872static void modeFree(Mode *p){
24873  u8 autoExplain = p->autoExplain;
24874  free(p->spec.aWidth);
24875  free(p->spec.aAlign);
24876  free(p->spec.zColumnSep);
24877  free(p->spec.zRowSep);
24878  free(p->spec.zTableName);
24879  free(p->spec.zNull);
24880  memset(p, 0, sizeof(*p));
24881  p->spec.iVersion = 1;
24882  p->autoExplain = autoExplain;
24883}
24884
24885/*
24886** Duplicate Mode pSrc into pDest.  pDest is assumed to be
24887** uninitialized prior to invoking this routine.
24888*/
24889static void modeDup(Mode *pDest, Mode *pSrc){
24890  memcpy(pDest, pSrc, sizeof(*pDest));
24891  if( pDest->spec.aWidth ){
24892    size_t sz = sizeof(pSrc->spec.aWidth[0]) * pSrc->spec.nWidth;
24893    pDest->spec.aWidth = malloc( sz );
24894    if( pDest->spec.aWidth ){
24895      memcpy(pDest->spec.aWidth, pSrc->spec.aWidth, sz);
24896    }else{
24897      pDest->spec.nWidth = 0;
24898    }
24899  }
24900  if( pDest->spec.aAlign ){
24901    size_t sz = sizeof(pSrc->spec.aAlign[0]) * pSrc->spec.nAlign;
24902    pDest->spec.aAlign = malloc( sz );
24903    if( pDest->spec.aAlign ){
24904      memcpy(pDest->spec.aAlign, pSrc->spec.aAlign, sz);
24905    }else{
24906      pDest->spec.nAlign = 0;
24907    }
24908  }
24909  if( pDest->spec.zColumnSep ){
24910    pDest->spec.zColumnSep = strdup(pSrc->spec.zColumnSep);
24911  }
24912  if( pDest->spec.zRowSep ){
24913    pDest->spec.zRowSep = strdup(pSrc->spec.zRowSep);
24914  }
24915  if( pDest->spec.zTableName ){
24916    pDest->spec.zTableName = strdup(pSrc->spec.zTableName);
24917  }
24918  if( pDest->spec.zNull ){
24919    pDest->spec.zNull = strdup(pSrc->spec.zNull);
24920  }
24921}
24922
24923/*
24924** Set a string value to a copy of the zNew string in memory
24925** obtained from system malloc().
24926*/
24927static void modeSetStr(char **az, const char *zNew){
24928  free(*az);
24929  if( zNew==0 ){
24930    *az = 0;
24931  }else{
24932    size_t n = strlen(zNew);
24933    *az = malloc( n+1 );
24934    if( *az ){
24935      memcpy(*az, zNew, n+1 );
24936    }
24937  }
24938}
24939
24940/*
24941** Change the mode to eMode
24942*/
24943static void modeChange(ShellState *p, unsigned char eMode){
24944  const ModeInfo *pI;
24945  if( eMode<ArraySize(aModeInfo) ){
24946    Mode *pM = &p->mode;
24947    pI = &aModeInfo[eMode];
24948    pM->eMode = eMode;
24949    if( pI->eCSep ) modeSetStr(&pM->spec.zColumnSep, aModeStr[pI->eCSep]);
24950    if( pI->eRSep ) modeSetStr(&pM->spec.zRowSep, aModeStr[pI->eRSep]);
24951    if( pI->eNull ) modeSetStr(&pM->spec.zNull, aModeStr[pI->eNull]);
24952    pM->spec.eText = pI->eText;
24953    pM->spec.eBlob = pI->eBlob;
24954    if( (pM->mFlags & MFLG_HDR)==0 ){
24955      pM->spec.bTitles = pI->bHdr;
24956    }
24957    pM->spec.eTitle = pI->eHdr;
24958    if( pI->mFlg & 0x01 ){
24959      pM->spec.bBorder = QRF_No;
24960    }else{
24961      pM->spec.bBorder = QRF_Auto;
24962    }
24963    if( pI->mFlg & 0x02 ){
24964      pM->spec.bSplitColumn = QRF_Yes;
24965      pM->bAutoScreenWidth = 1;
24966    }else{
24967      pM->spec.bSplitColumn = QRF_No;
24968    }
24969  }else if( eMode>=MODE_USER && eMode-MODE_USER<p->nSavedModes ){
24970    modeFree(&p->mode);
24971    modeDup(&p->mode, &p->aSavedModes[eMode-MODE_USER].mode);
24972  }else if( eMode==MODE_BATCH ){
24973    u8 mFlags = p->mode.mFlags;
24974    modeFree(&p->mode);
24975    modeChange(p, MODE_List);
24976    p->mode.mFlags = mFlags;
24977  }else if( eMode==MODE_TTY ){
24978    u8 mFlags = p->mode.mFlags;
24979    modeFree(&p->mode);
24980    modeChange(p, MODE_QBox);
24981    p->mode.bAutoScreenWidth = 1;
24982    p->mode.spec.eText = QRF_TEXT_Relaxed;
24983    p->mode.spec.nCharLimit = DFLT_CHAR_LIMIT;
24984    p->mode.spec.nLineLimit = DFLT_LINE_LIMIT;
24985    p->mode.spec.bTextJsonb = QRF_Yes;
24986    p->mode.spec.nTitleLimit = DFLT_TITLE_LIMIT;
24987    p->mode.spec.nMultiInsert = DFLT_MULTI_INSERT;
24988    p->mode.mFlags = mFlags;
24989  }
24990}
24991
24992/*
24993** Set the mode to the default.  It assumed that the mode has
24994** already been freed and zeroed prior to calling this routine.
24995*/
24996static void modeDefault(ShellState *p){
24997  p->mode.spec.iVersion = 1;
24998  p->mode.autoExplain = 1;
24999  if( stdin_is_interactive || stdout_is_console ){
25000    modeChange(p, MODE_TTY);
25001  }else{
25002    modeChange(p, MODE_BATCH);
25003  }
25004}
25005
25006/*
25007** Find the number of a display mode given its name.  Return -1 if
25008** the name does not match any mode.
25009**
25010** Saved modes are also searched if p!=NULL.  The number returned
25011** for a saved mode is the index into the p->aSavedModes[] array 
25012** plus MODE_USER.
25013**
25014** Two special mode names are also available: "batch" and "tty".
25015** evaluate to the default mode for batch operation and interactive
25016** operation on a TTY, respectively.
25017*/
25018static int modeFind(ShellState *p, const char *zName){
25019  int i;
25020  for(i=0; i<ArraySize(aModeInfo); i++){
25021    if( cli_strcmp(aModeInfo[i].zName,zName)==0 ) return i;
25022  }
25023  for(i=0; i<p->nSavedModes; i++){
25024    if( cli_strcmp(p->aSavedModes[i].zTag,zName)==0 ) return i+MODE_USER;
25025  }
25026  if( strcmp(zName,"batch")==0 ) return MODE_BATCH;
25027  if( strcmp(zName,"tty")==0 ) return MODE_TTY;
25028  return -1;
25029}
25030
25031/*
25032** Save or restore the current output mode
25033*/
25034static void modePush(ShellState *p){
25035  if( p->nPopMode==0 ){
25036    modeFree(&p->modePrior);
25037    modeDup(&p->modePrior,&p->mode);
25038  }
25039}
25040static void modePop(ShellState *p){
25041  if( p->modePrior.spec.iVersion>0 ){
25042    modeFree(&p->mode);
25043    p->mode = p->modePrior;
25044    memset(&p->modePrior, 0, sizeof(p->modePrior));
25045  }
25046}
25047
25048
25049/*
25050** A callback for the sqlite3_log() interface.
25051*/
25052static void shellLog(void *pArg, int iErrCode, const char *zMsg){
25053  ShellState *p = (ShellState*)pArg;
25054  if( p->pLog==0 ) return;
25055  cli_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
25056  fflush(p->pLog);
25057}
25058
25059/*
25060** SQL function:  shell_putsnl(X)
25061**
25062** Write the text X to the screen (or whatever output is being directed)
25063** adding a newline at the end, and then return X.
25064*/
25065static void shellPutsFunc(
25066  sqlite3_context *pCtx,
25067  int nVal,
25068  sqlite3_value **apVal
25069){
25070  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
25071  (void)nVal;
25072  cli_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
25073  sqlite3_result_value(pCtx, apVal[0]);
25074}
25075
25076/*
25077** Compute the name of the location of an input error in memory
25078** obtained from sqlite3_malloc().
25079*/
25080static char *shellErrorLocation(ShellState *p){
25081  char *zLoc;
25082  if( p->zErrPrefix ){
25083    zLoc = sqlite3_mprintf("%s", p->zErrPrefix);
25084  }else if( p->zInFile==0 || strcmp(p->zInFile,"<stdin>")==0){
25085    zLoc = sqlite3_mprintf("line %lld:", p->lineno);
25086  }else{
25087    zLoc = sqlite3_mprintf("%s:%lld:", p->zInFile, p->lineno);
25088  }
25089  return zLoc;
25090}
25091
25092/*
25093** If in safe mode, print an error message described by the arguments
25094** and exit immediately.
25095*/
25096static void failIfSafeMode(
25097  ShellState *p,
25098  const char *zErrMsg,
25099  ...
25100){
25101  if( p->bSafeMode ){
25102    va_list ap;
25103    char *zMsg;
25104    char *zLoc = shellErrorLocation(p);
25105    va_start(ap, zErrMsg);
25106    zMsg = sqlite3_vmprintf(zErrMsg, ap);
25107    va_end(ap);
25108    cli_printf(stderr, "%s %s\n", zLoc, zMsg);
25109    cli_exit(1);
25110  }
25111}
25112
25113/*
25114** Issue an error message from a dot-command.
25115*/
25116static void dotCmdError(
25117  ShellState *p,       /* Shell state */
25118  int iArg,            /* Index of argument on which error occurred */
25119  const char *zBrief,  /* Brief (<20 character) error description */
25120  const char *zDetail, /* Error details */
25121  ...
25122){
25123  FILE *out = stderr;
25124  char *zLoc = shellErrorLocation(p);
25125  if( zBrief!=0 && iArg>=0 && iArg<p->dot.nArg ){
25126    int i = p->dot.aiOfst[iArg];
25127    int nPrompt = strlen30(zBrief) + 5;
25128    cli_printf(out, "%s %s\n", zLoc, p->dot.zOrig);
25129    if( i > nPrompt ){
25130      cli_printf(out, "%s %*s%s ---^\n", zLoc, 1+i-nPrompt, "", zBrief);
25131    }else{
25132      cli_printf(out, "%s %*s^--- %s\n", zLoc, i, "", zBrief);
25133    }
25134  }
25135  if( zDetail ){
25136    char *zMsg;
25137    va_list ap;
25138    va_start(ap, zDetail);
25139    zMsg = sqlite3_vmprintf(zDetail,ap);
25140    va_end(ap);
25141    cli_printf(out,"%s %s\n", zLoc, zMsg);
25142    sqlite3_free(zMsg);
25143  }
25144  sqlite3_free(zLoc);
25145}
25146  
25147
25148/*
25149** SQL function:   edit(VALUE)
25150**                 edit(VALUE,EDITOR)
25151**
25152** These steps:
25153**
25154**     (1) Write VALUE into a temporary file.
25155**     (2) Run program EDITOR on that temporary file.
25156**     (3) Read the temporary file back and return its content as the result.
25157**     (4) Delete the temporary file
25158**
25159** If the EDITOR argument is omitted, use the value in the VISUAL
25160** environment variable.  If still there is no EDITOR, through an error.
25161**
25162** Also throw an error if the EDITOR program returns a non-zero exit code.
25163*/
25164#ifndef SQLITE_NOHAVE_SYSTEM
25165static void editFunc(
25166  sqlite3_context *context,
25167  int argc,
25168  sqlite3_value **argv
25169){
25170  const char *zEditor;
25171  char *zTempFile = 0;
25172  sqlite3 *db;
25173  char *zCmd = 0;
25174  int bBin;
25175  int rc;
25176  int hasCRLF = 0;
25177  FILE *f = 0;
25178  sqlite3_int64 sz;
25179  sqlite3_int64 x;
25180  unsigned char *p = 0;
25181
25182  if( argc==2 ){
25183    zEditor = (const char*)sqlite3_value_text(argv[1]);
25184  }else{
25185    zEditor = getenv("VISUAL");
25186  }
25187  if( zEditor==0 ){
25188    sqlite3_result_error(context, "no editor for edit()", -1);
25189    return;
25190  }
25191  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
25192    sqlite3_result_error(context, "NULL input to edit()", -1);
25193    return;
25194  }
25195  db = sqlite3_context_db_handle(context);
25196  zTempFile = 0;
25197  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
25198  if( zTempFile==0 ){
25199    sqlite3_uint64 r = 0;
25200    sqlite3_randomness(sizeof(r), &r);
25201    zTempFile = sqlite3_mprintf("temp%llx", r);
25202    if( zTempFile==0 ){
25203      sqlite3_result_error_nomem(context);
25204      return;
25205    }
25206  }
25207  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
25208  /* When writing the file to be edited, do \n to \r\n conversions on systems
25209  ** that want \r\n line endings */
25210  f = sqlite3_fopen(zTempFile, bBin ? "wb" : "w");
25211  if( f==0 ){
25212    sqlite3_result_error(context, "edit() cannot open temp file", -1);
25213    goto edit_func_end;
25214  }
25215  sz = sqlite3_value_bytes(argv[0]);
25216  if( bBin ){
25217    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
25218  }else{
25219    const char *z = (const char*)sqlite3_value_text(argv[0]);
25220    /* Remember whether or not the value originally contained \r\n */
25221    if( z && strstr(z,"\r\n")!=0 ) hasCRLF = 1;
25222    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
25223  }
25224  fclose(f);
25225  f = 0;
25226  if( x!=sz ){
25227    sqlite3_result_error(context, "edit() could not write the whole file", -1);
25228    goto edit_func_end;
25229  }
25230  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
25231  if( zCmd==0 ){
25232    sqlite3_result_error_nomem(context);
25233    goto edit_func_end;
25234  }
25235  rc = system(zCmd);
25236  sqlite3_free(zCmd);
25237  if( rc ){
25238    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
25239    goto edit_func_end;
25240  }
25241  f = sqlite3_fopen(zTempFile, "rb");
25242  if( f==0 ){
25243    sqlite3_result_error(context,
25244      "edit() cannot reopen temp file after edit", -1);
25245    goto edit_func_end;
25246  }
25247  fseek(f, 0, SEEK_END);
25248  sz = ftell(f);
25249  rewind(f);
25250  p = sqlite3_malloc64( sz+1 );
25251  if( p==0 ){
25252    sqlite3_result_error_nomem(context);
25253    goto edit_func_end;
25254  }
25255  x = fread(p, 1, (size_t)sz, f);
25256  fclose(f);
25257  f = 0;
25258  if( x!=sz ){
25259    sqlite3_result_error(context, "could not read back the whole file", -1);
25260    goto edit_func_end;
25261  }
25262  if( bBin ){
25263    sqlite3_result_blob64(context, p, sz, sqlite3_free);
25264  }else{
25265    sqlite3_int64 i, j;
25266    if( hasCRLF ){
25267      /* If the original contains \r\n then do no conversions back to \n */
25268    }else{
25269      /* If the file did not originally contain \r\n then convert any new
25270      ** \r\n back into \n */
25271      p[sz] = 0;
25272      for(i=j=0; i<sz; i++){
25273        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
25274        p[j++] = p[i];
25275      }
25276      sz = j;
25277      p[sz] = 0;
25278    }
25279    sqlite3_result_text64(context, (const char*)p, sz,
25280                          sqlite3_free, SQLITE_UTF8);
25281  }
25282  p = 0;
25283
25284edit_func_end:
25285  if( f ) fclose(f);
25286  unlink(zTempFile);
25287  sqlite3_free(zTempFile);
25288  sqlite3_free(p);
25289}
25290#endif /* SQLITE_NOHAVE_SYSTEM */
25291
25292/*
25293** Set output mode to text or binary for Windows.
25294*/
25295static void setCrlfMode(ShellState *p){
25296#ifdef _WIN32
25297  if( p->mode.mFlags & MFLG_CRLF ){
25298    sqlite3_fsetmode(p->out, _O_TEXT);
25299  }else{
25300    sqlite3_fsetmode(p->out, _O_BINARY);
25301  }
25302#else
25303  UNUSED_PARAMETER(p);
25304#endif    
25305}
25306
25307/*
25308** Find earliest of chars within s specified in zAny.
25309** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated.
25310*/
25311static const char *anyOfInStr(const char *s, const char *zAny, size_t ns){
25312  const char *pcFirst = 0;
25313  if( ns == ~(size_t)0 ) ns = strlen(s);
25314  while(*zAny){
25315    const char *pc = (const char*)memchr(s, *zAny&0xff, ns);
25316    if( pc ){
25317      pcFirst = pc;
25318      ns = pcFirst - s;
25319    }
25320    ++zAny;
25321  }
25322  return pcFirst;
25323}
25324
25325/* Skip over as much z[] input char sequence as is valid UTF-8,
25326** limited per nAccept char's or whole characters and containing
25327** no char cn such that ((1<<cn) & ccm)!=0. On return, the
25328** sequence z:return (inclusive:exclusive) is validated UTF-8.
25329** Limit: nAccept>=0 => char count, nAccept<0 => character
25330 */
25331const char *zSkipValidUtf8(const char *z, int nAccept, long ccm){
25332  int ng = (nAccept<0)? -nAccept : 0;
25333  const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
25334  assert(z!=0);
25335  while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
25336    unsigned char c = *(u8*)z;
25337    if( c<0x7f ){
25338      if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
25339      ++z; /* ASCII */
25340    }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
25341    else{
25342      const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
25343      do{
25344        if( pcLimit && zt >= pcLimit ) return z;
25345        else{
25346          char ct = *zt++;
25347          if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){
25348            /* Trailing bytes are too few, too many, or invalid. */
25349            return z;
25350          }
25351        }
25352      } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */
25353      z = zt;
25354    }
25355  }
25356  return z;
25357}
25358
25359
25360/*
25361** Output the given string as a quoted according to C or TCL quoting rules.
25362*/
25363static void output_c_string(FILE *out, const char *z){
25364  char c;
25365  static const char *zq = "\"";
25366  static long ctrlMask = ~0L;
25367  static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */
25368  char ace[3] = "\\?";
25369  char cbsSay;
25370  cli_puts(zq, out);
25371  if( z==0 ) z = "";
25372  while( *z!=0 ){
25373    const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0);
25374    const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask);
25375    const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast;
25376    if( pcEnd > z ){
25377      cli_printf(out, "%.*s", (int)(pcEnd-z), z);
25378    }
25379    if( (c = *pcEnd)==0 ) break;
25380    ++pcEnd;
25381    switch( c ){
25382    case '\\': case '"':
25383      cbsSay = (char)c;
25384      break;
25385    case '\t': cbsSay = 't'; break;
25386    case '\n': cbsSay = 'n'; break;
25387    case '\r': cbsSay = 'r'; break;
25388    case '\f': cbsSay = 'f'; break;
25389    default: cbsSay = 0; break;
25390    }
25391    if( cbsSay ){
25392      ace[1] = cbsSay;
25393      cli_puts(ace, out);
25394    }else if( !isprint(c&0xff) ){
25395      cli_printf(out, "\\%03o", c&0xff);
25396    }else{
25397      ace[1] = (char)c;
25398      cli_puts(ace+1, out);
25399    }
25400    z = pcEnd;
25401  }
25402  cli_puts(zq, out);
25403}
25404
25405/* Encode input string z[] as a C-language string literal and
25406** append it to the sqlite3_str.  If z is NULL render and empty string.
25407*/
25408static void append_c_string(sqlite3_str *out, const char *z){
25409  char c;
25410  static const char *zq = "\"";
25411  static long ctrlMask = ~0L;
25412  static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */
25413  char ace[3] = "\\?";
25414  char cbsSay;
25415  if( z==0 ) z = "";
25416  sqlite3_str_appendall(out,zq);
25417  while( *z!=0 ){
25418    const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0);
25419    const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask);
25420    const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast;
25421    if( pcEnd > z ){
25422      sqlite3_str_appendf(out, "%.*s", (int)(pcEnd-z), z);
25423    }
25424    if( (c = *pcEnd)==0 ) break;
25425    ++pcEnd;
25426    switch( c ){
25427    case '\\': case '"':
25428      cbsSay = (char)c;
25429      break;
25430    case '\t': cbsSay = 't'; break;
25431    case '\n': cbsSay = 'n'; break;
25432    case '\r': cbsSay = 'r'; break;
25433    case '\f': cbsSay = 'f'; break;
25434    default: cbsSay = 0; break;
25435    }
25436    if( cbsSay ){
25437      ace[1] = cbsSay;
25438      sqlite3_str_appendall(out,ace);
25439    }else if( !isprint(c&0xff) ){
25440      sqlite3_str_appendf(out, "\\%03o", c&0xff);
25441    }else{
25442      ace[1] = (char)c;
25443      sqlite3_str_appendall(out, ace+1);
25444    }
25445    z = pcEnd;
25446  }
25447  sqlite3_str_appendall(out, zq);
25448}
25449
25450/*
25451** This routine runs when the user presses Ctrl-C
25452*/
25453static void interrupt_handler(int NotUsed){
25454  UNUSED_PARAMETER(NotUsed);
25455  if( ++seenInterrupt>1 ) cli_exit(1);
25456  if( globalDb ) sqlite3_interrupt(globalDb);
25457}
25458
25459/* Try to determine the screen width.  Use the default if unable.
25460*/
25461int shellScreenWidth(void){
25462  if( stdout_tty_width>0 ){
25463    return stdout_tty_width;
25464  }else{
25465#if defined(TIOCGSIZE)
25466    struct ttysize ts;
25467    if( ioctl(STDIN_FILENO, TIOCGSIZE, &ts)>=0
25468     || ioctl(STDOUT_FILENO, TIOCGSIZE, &ts)>=0
25469     || ioctl(STDERR_FILENO, TIOCGSIZE, &ts)>=0
25470    ){
25471      return ts.ts_cols;
25472    }
25473#elif defined(TIOCGWINSZ)
25474    struct winsize ws;
25475    if( ioctl(STDIN_FILENO, TIOCGWINSZ, &ws)>=0
25476     || ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws)>=0
25477     || ioctl(STDERR_FILENO, TIOCGWINSZ, &ws)>=0
25478    ){
25479      return ws.ws_col;
25480    }
25481#elif defined(_WIN32)
25482    CONSOLE_SCREEN_BUFFER_INFO csbi;
25483    if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)
25484     || GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi)
25485     || GetConsoleScreenBufferInfo(GetStdHandle(STD_INPUT_HANDLE), &csbi)
25486    ){
25487      return csbi.srWindow.Right - csbi.srWindow.Left + 1;
25488    }
25489#endif
25490#define DEFAULT_SCREEN_WIDTH 80
25491    return DEFAULT_SCREEN_WIDTH;
25492  }
25493}
25494
25495#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
25496/*
25497** This routine runs for console events (e.g. Ctrl-C) on Win32
25498*/
25499static BOOL WINAPI ConsoleCtrlHandler(
25500  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
25501){
25502  if( dwCtrlType==CTRL_C_EVENT ){
25503    interrupt_handler(0);
25504    return TRUE;
25505  }
25506  return FALSE;
25507}
25508#endif
25509
25510#ifndef SQLITE_OMIT_AUTHORIZATION
25511/*
25512** This authorizer runs in safe mode.
25513*/
25514static int safeModeAuth(
25515  void *pClientData,
25516  int op,
25517  const char *zA1,
25518  const char *zA2,
25519  const char *zA3,
25520  const char *zA4
25521){
25522  ShellState *p = (ShellState*)pClientData;
25523  static const char *azProhibitedFunctions[] = {
25524    "edit",
25525    "fts3_tokenizer",
25526    "load_extension",
25527    "readfile",
25528    "realpath",
25529    "writefile",
25530    "zipfile",
25531    "zipfile_cds",
25532  };
25533  UNUSED_PARAMETER(zA1);
25534  UNUSED_PARAMETER(zA3);
25535  UNUSED_PARAMETER(zA4);
25536  switch( op ){
25537    case SQLITE_ATTACH: {
25538#ifndef SQLITE_SHELL_FIDDLE
25539      /* In WASM builds the filesystem is a virtual sandbox, so
25540      ** there's no harm in using ATTACH. */
25541      failIfSafeMode(p, "cannot run ATTACH in safe mode");
25542#endif
25543      break;
25544    }
25545    case SQLITE_FUNCTION: {
25546      int i;
25547      for(i=0; i<ArraySize(azProhibitedFunctions); i++){
25548        if( sqlite3_stricmp(zA2, azProhibitedFunctions[i])==0 ){
25549          failIfSafeMode(p, "cannot use the %s() function in safe mode",
25550                         azProhibitedFunctions[i]);
25551        }
25552      }
25553      break;
25554    }
25555  }
25556  return SQLITE_OK;
25557}
25558
25559/*
25560** When the ".auth ON" is set, the following authorizer callback is
25561** invoked.  It always returns SQLITE_OK.
25562*/
25563static int shellAuth(
25564  void *pClientData,
25565  int op,
25566  const char *zA1,
25567  const char *zA2,
25568  const char *zA3,
25569  const char *zA4
25570){
25571  ShellState *p = (ShellState*)pClientData;
25572  static const char *azAction[] = { 0,
25573     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
25574     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
25575     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
25576     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
25577     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
25578     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
25579     "PRAGMA",               "READ",                 "SELECT",
25580     "TRANSACTION",          "UPDATE",               "ATTACH",
25581     "DETACH",               "ALTER_TABLE",          "REINDEX",
25582     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
25583     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
25584  };
25585  int i;
25586  const char *az[4];
25587  az[0] = zA1;
25588  az[1] = zA2;
25589  az[2] = zA3;
25590  az[3] = zA4;
25591  cli_printf(p->out, "authorizer: %s", azAction[op]);
25592  for(i=0; i<4; i++){
25593    cli_puts(" ", p->out);
25594    if( az[i] ){
25595      output_c_string(p->out, az[i]);
25596    }else{
25597      cli_puts("NULL", p->out);
25598    }
25599  }
25600  cli_puts("\n", p->out);
25601  if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
25602  return SQLITE_OK;
25603}
25604#endif
25605
25606/*
25607** Print a schema statement.  This is helper routine to dump_callbac().
25608**
25609** This routine converts some CREATE TABLE statements for shadow tables
25610** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
25611**
25612** If the schema statement in z[] contains a start-of-comment and if
25613** sqlite3_complete() returns false, try to terminate the comment before
25614** printing the result.  https://sqlite.org/forum/forumpost/d7be961c5c
25615*/
25616static void printSchemaLine(FILE *out, const char *z, const char *zTail){
25617  char *zToFree = 0;
25618  if( z==0 ) return;
25619  if( zTail==0 ) return;
25620  if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
25621    const char *zOrig = z;
25622    static const char *azTerm[] = { "", "*/", "\n" };
25623    int i;
25624    for(i=0; i<ArraySize(azTerm); i++){
25625      char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]);
25626      shell_check_oom(zNew);
25627      if( sqlite3_complete(zNew) ){
25628        size_t n = strlen(zNew);
25629        zNew[n-1] = 0;
25630        zToFree = zNew;
25631        z = zNew;
25632        break;
25633      }
25634      sqlite3_free(zNew);
25635    }
25636  }
25637  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
25638    cli_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
25639  }else{
25640    cli_printf(out, "%s%s", z, zTail);
25641  }
25642  sqlite3_free(zToFree);
25643}
25644
25645/*
25646** Return true if string z[] has nothing but whitespace and comments to the
25647** end of the first line.
25648*/
25649static int wsToEol(const char *z){
25650  int i;
25651  for(i=0; z[i]; i++){
25652    if( z[i]=='\n' ) return 1;
25653    if( IsSpace(z[i]) ) continue;
25654    if( z[i]=='-' && z[i+1]=='-' ) return 1;
25655    return 0;
25656  }
25657  return 1;
25658}
25659
25660/*
25661** SQL Function:  shell_format_schema(SQL,FLAGS)
25662**
25663** This function is internally by the CLI to assist with the
25664** ".schema", ".fullschema", and ".dump" commands.  The first
25665** argument is the value from sqlite_schema.sql.  The value returned
25666** is a modification of the input that can actually be run as SQL
25667** to recreate the schema object.
25668**
25669** When FLAGS is zero, the only changes is to append ";".  If the
25670** 0x01 bit of FLAGS is set, then transformations are made to implement
25671** ".schema --indent".
25672*/
25673static void shellFormatSchema(
25674  sqlite3_context *pCtx,
25675  int nVal,
25676  sqlite3_value **apVal
25677){
25678  int flags;          /* Value of 2nd parameter */
25679  const char *zSql;   /* Value of 1st parameter */
25680  int nSql;           /* Bytes of text in zSql[] */
25681  sqlite3_str *pOut;  /* Output buffer */
25682  char *z;            /* Writable copy of zSql */
25683  int i, j;           /* Loop counters */
25684  int nParen = 0;
25685  char cEnd = 0;
25686  char c;
25687  int nLine = 0;
25688  int isIndex;
25689  int isWhere = 0;
25690
25691  assert( nVal==2 );
25692  pOut = sqlite3_str_new(sqlite3_context_db_handle(pCtx));
25693  nSql = sqlite3_value_bytes(apVal[0]);
25694  zSql = (const char*)sqlite3_value_text(apVal[0]);
25695  if( zSql==0 || zSql[0]==0 ) goto shellFormatSchema_finish;
25696  flags = sqlite3_value_int(apVal[1]);
25697  if( (flags & 0x01)==0 ){
25698    sqlite3_str_append(pOut, zSql, nSql);
25699    sqlite3_str_append(pOut, ";", 1);
25700    goto shellFormatSchema_finish;
25701  }
25702  if( sqlite3_strlike("CREATE VIEW%", zSql, 0)==0
25703   || sqlite3_strlike("CREATE TRIG%", zSql, 0)==0
25704  ){
25705    sqlite3_str_append(pOut, zSql, nSql);
25706    sqlite3_str_append(pOut, ";", 1);
25707    goto shellFormatSchema_finish;
25708  }
25709  isIndex = sqlite3_strlike("CREATE INDEX%", zSql, 0)==0
25710         || sqlite3_strlike("CREATE UNIQUE INDEX%", zSql, 0)==0;
25711  z = sqlite3_mprintf("%s", zSql);
25712  if( z==0 ){
25713    sqlite3_str_free(pOut);
25714    sqlite3_result_error_nomem(pCtx);
25715    return;
25716  }
25717  j = 0;
25718  for(i=0; IsSpace(z[i]); i++){}
25719  for(; (c = z[i])!=0; i++){
25720    if( IsSpace(c) ){
25721      if( z[j-1]=='\r' ) z[j-1] = '\n';
25722      if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
25723    }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
25724      j--;
25725    }
25726    z[j++] = c;
25727  }
25728  while( j>0 && IsSpace(z[j-1]) ){ j--; }
25729  z[j] = 0;
25730  if( strlen30(z)>=79 ){
25731    for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
25732      if( c==cEnd ){
25733        cEnd = 0;
25734      }else if( cEnd!=0){
25735        /* No-op */
25736      }else if( c=='"' || c=='\'' || c=='`' ){
25737        cEnd = c;
25738      }else if( c=='[' ){
25739        cEnd = ']';
25740      }else if( c=='-' && z[i+1]=='-' ){
25741        cEnd = '\n';
25742      }else if( c=='(' ){
25743        nParen++;
25744      }else if( c==')' ){
25745        nParen--;
25746        if( nLine>0 && nParen==0 && j>0 && !isWhere ){
25747          sqlite3_str_append(pOut, z, j);
25748          sqlite3_str_append(pOut, "\n", 1);
25749          j = 0;
25750        }
25751      }else if( (c=='w' || c=='W')
25752             && nParen==0 && isIndex
25753             && sqlite3_strnicmp("WHERE",&z[i],5)==0
25754             && !IsAlnum(z[i+5]) && z[i+5]!='_' ){
25755        isWhere = 1;
25756      }else if( isWhere && (c=='A' || c=='a')
25757             && nParen==0
25758             && sqlite3_strnicmp("AND",&z[i],3)==0
25759             && !IsAlnum(z[i+3]) && z[i+3]!='_' ){
25760        sqlite3_str_append(pOut, z, j);
25761        sqlite3_str_append(pOut, "\n    ", 5);
25762        j = 0;
25763      }
25764      z[j++] = c;
25765      if( nParen==1 && cEnd==0
25766       && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
25767       && !isWhere
25768      ){
25769        if( c=='\n' ) j--;
25770        sqlite3_str_append(pOut, z, j);
25771        sqlite3_str_append(pOut, "\n  ", 3);
25772        j = 0;
25773        nLine++;
25774        while( IsSpace(z[i+1]) ){ i++; }
25775      }
25776    }
25777    z[j] = 0;
25778  }
25779  sqlite3_str_appendall(pOut, z);
25780  sqlite3_str_append(pOut, ";", 1);
25781  sqlite3_free(z);
25782
25783shellFormatSchema_finish:
25784  sqlite3_result_text(pCtx, sqlite3_str_finish(pOut), -1, sqlite3_free);
25785}
25786
25787#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
25788/*
25789** Progress handler callback.
25790*/
25791static int progress_handler(void *pClientData) {
25792  ShellState *p = (ShellState*)pClientData;
25793  p->nProgress++;
25794  if( (p->flgProgress & SHELL_PROGRESS_TMOUT)!=0
25795   && ELAPSE_TIME(p)>=p->tmProgress
25796  ){
25797    cli_printf(p->out, "Progress timeout after %.6f seconds\n", 
25798               ELAPSE_TIME(p));
25799    return 1;
25800  }
25801  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
25802    cli_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
25803    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
25804    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
25805    return 1;
25806  }
25807  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
25808    cli_printf(p->out, "Progress %u\n", p->nProgress);
25809  }
25810  return 0;
25811}
25812#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
25813
25814/*
25815** This is the callback routine from sqlite3_exec() that appends all
25816** output onto the end of a ShellText object.
25817*/
25818static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
25819  ShellText *p = (ShellText*)pArg;
25820  int i;
25821  UNUSED_PARAMETER(az);
25822  if( azArg==0 ) return 0;
25823  if( p->n ) appendText(p, "|", 0);
25824  for(i=0; i<nArg; i++){
25825    if( i ) appendText(p, ",", 0);
25826    if( azArg[i] ) appendText(p, azArg[i], 0);
25827  }
25828  return 0;
25829}
25830
25831/*
25832** Generate an appropriate SELFTEST table in the main database.
25833*/
25834static void createSelftestTable(ShellState *p){
25835  char *zErrMsg = 0;
25836  sqlite3_exec(p->db,
25837    "SAVEPOINT selftest_init;\n"
25838    "CREATE TABLE IF NOT EXISTS selftest(\n"
25839    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
25840    "  op TEXT,\n"                   /* Operator:  memo run */
25841    "  cmd TEXT,\n"                  /* Command text */
25842    "  ans TEXT\n"                   /* Desired answer */
25843    ");"
25844    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
25845    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
25846    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
25847    "         'memo','Tests generated by --init');\n"
25848    "INSERT INTO [_shell$self]\n"
25849    "  SELECT 'run',\n"
25850    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
25851                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
25852    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
25853                          "FROM sqlite_schema ORDER BY 2',224));\n"
25854    "INSERT INTO [_shell$self]\n"
25855    "  SELECT 'run',"
25856    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
25857    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
25858    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
25859    "  FROM (\n"
25860    "    SELECT name FROM sqlite_schema\n"
25861    "     WHERE type='table'\n"
25862    "       AND name<>'selftest'\n"
25863    "       AND coalesce(rootpage,0)>0\n"
25864    "  )\n"
25865    " ORDER BY name;\n"
25866    "INSERT INTO [_shell$self]\n"
25867    "  VALUES('run','PRAGMA integrity_check','ok');\n"
25868    "INSERT INTO selftest(tno,op,cmd,ans)"
25869    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
25870    "DROP TABLE [_shell$self];"
25871    ,0,0,&zErrMsg);
25872  if( zErrMsg ){
25873    cli_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
25874    sqlite3_free(zErrMsg);
25875  }
25876  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
25877}
25878
25879
25880/*
25881** Set the destination table field of the ShellState structure to
25882** the name of the table given.  Escape any quote characters in the
25883** table name.
25884*/
25885static void set_table_name(ShellState *p, const char *zName){
25886  if( p->zDestTable ){
25887    sqlite3_free(p->zDestTable);
25888    p->zDestTable = 0;
25889  }
25890  if( zName==0 ) return;
25891  p->zDestTable = sqlite3_mprintf("%s", zName);
25892  shell_check_oom(p->zDestTable);
25893}
25894
25895/*
25896** Maybe construct two lines of text that point out the position of a
25897** syntax error.  Return a pointer to the text, in memory obtained from
25898** sqlite3_malloc().  Or, if the most recent error does not involve a
25899** specific token that we can point to, return an empty string.
25900**
25901** In all cases, the memory returned is obtained from sqlite3_malloc64()
25902** and should be released by the caller invoking sqlite3_free().
25903*/
25904static char *shell_error_context(const char *zSql, sqlite3 *db){
25905  int iOffset;
25906  size_t len;
25907  char *zCode;
25908  char *zMsg;
25909  int i;
25910  if( db==0
25911   || zSql==0
25912   || (iOffset = sqlite3_error_offset(db))<0
25913   || iOffset>=(int)strlen(zSql)
25914  ){
25915    return sqlite3_mprintf("");
25916  }
25917  while( iOffset>50 ){
25918    iOffset--;
25919    zSql++;
25920    while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
25921  }
25922  len = strlen(zSql);
25923  if( len>78 ){
25924    len = 78;
25925    while( len>0 && (zSql[len]&0xc0)==0x80 ) len--;
25926  }
25927  zCode = sqlite3_mprintf("%.*s", len, zSql);
25928  shell_check_oom(zCode);
25929  for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
25930  if( iOffset<25 ){
25931    zMsg = sqlite3_mprintf("\n  %z\n  %*s^--- error here", zCode,iOffset,"");
25932  }else{
25933    zMsg = sqlite3_mprintf("\n  %z\n  %*serror here ---^", zCode,iOffset-14,"");
25934  }
25935  return zMsg;
25936}
25937
25938
25939/*
25940** Execute a query statement that will generate SQL output.  Print
25941** the result columns, comma-separated, on a line and then add a
25942** semicolon terminator to the end of that line.
25943**
25944** If the number of columns is 1 and that column contains text "--"
25945** then write the semicolon on a separate line.  That way, if a
25946** "--" comment occurs at the end of the statement, the comment
25947** won't consume the semicolon terminator.
25948*/
25949static int run_table_dump_query(
25950  ShellState *p,           /* Query context */
25951  const char *zSelect      /* SELECT statement to extract content */
25952){
25953  sqlite3_stmt *pSelect;
25954  int rc;
25955  int nResult;
25956  int i;
25957  const char *z;
25958  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
25959  if( rc!=SQLITE_OK || !pSelect ){
25960    char *zContext = shell_error_context(zSelect, p->db);
25961    cli_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s",
25962          rc, sqlite3_errmsg(p->db), zContext);
25963    sqlite3_free(zContext);
25964    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
25965    return rc;
25966  }
25967  rc = sqlite3_step(pSelect);
25968  nResult = sqlite3_column_count(pSelect);
25969  while( rc==SQLITE_ROW ){
25970    z = (const char*)sqlite3_column_text(pSelect, 0);
25971    cli_printf(p->out, "%s", z);
25972    for(i=1; i<nResult; i++){
25973      cli_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
25974    }
25975    if( z==0 ) z = "";
25976    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
25977    if( z[0] ){
25978      cli_puts("\n;\n", p->out);
25979    }else{
25980      cli_puts(";\n", p->out);
25981    }
25982    rc = sqlite3_step(pSelect);
25983  }
25984  rc = sqlite3_finalize(pSelect);
25985  if( rc!=SQLITE_OK ){
25986    cli_printf(p->out, "/**** ERROR: (%d) %s *****/\n",
25987                    rc, sqlite3_errmsg(p->db));
25988    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
25989  }
25990  return rc;
25991}
25992
25993/*
25994** Allocate space and save off string indicating current error.
25995*/
25996static char *save_err_msg(
25997  sqlite3 *db,           /* Database to query */
25998  const char *zPhase,    /* When the error occurs */
25999  int rc,                /* Error code returned from API */
26000  const char *zSql       /* SQL string, or NULL */
26001){
26002  char *zErr;
26003  char *zContext;
26004  sqlite3_str *pStr = sqlite3_str_new(0);
26005  sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db));
26006  if( rc>1 ){
26007    sqlite3_str_appendf(pStr, " (%d)", rc);
26008  }
26009  zContext = shell_error_context(zSql, db);
26010  if( zContext ){
26011    sqlite3_str_appendall(pStr, zContext);
26012    sqlite3_free(zContext);
26013  }
26014  zErr = sqlite3_str_finish(pStr);
26015  shell_check_oom(zErr);
26016  return zErr;
26017}
26018
26019#ifdef __linux__
26020/*
26021** Attempt to display I/O stats on Linux using /proc/PID/io
26022*/
26023static void displayLinuxIoStats(FILE *out){
26024  FILE *in;
26025  char z[200];
26026  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
26027  in = sqlite3_fopen(z, "rb");
26028  if( in==0 ) return;
26029  while( sqlite3_fgets(z, sizeof(z), in)!=0 ){
26030    static const struct {
26031      const char *zPattern;
26032      const char *zDesc;
26033    } aTrans[] = {
26034      { "rchar: ",                  "Bytes received by read():" },
26035      { "wchar: ",                  "Bytes sent to write():"    },
26036      { "syscr: ",                  "Read() system calls:"      },
26037      { "syscw: ",                  "Write() system calls:"     },
26038      { "read_bytes: ",             "Bytes read from storage:"  },
26039      { "write_bytes: ",            "Bytes written to storage:" },
26040      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
26041    };
26042    int i;
26043    for(i=0; i<ArraySize(aTrans); i++){
26044      int n = strlen30(aTrans[i].zPattern);
26045      if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
26046        cli_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
26047        break;
26048      }
26049    }
26050  }
26051  fclose(in);
26052}
26053#endif
26054
26055/*
26056** Display a single line of status using 64-bit values.
26057*/
26058static void displayStatLine(
26059  FILE *out,                /* Write to this channel */
26060  char *zLabel,             /* Label for this one line */
26061  char *zFormat,            /* Format for the result */
26062  int iStatusCtrl,          /* Which status to display */
26063  int bReset                /* True to reset the stats */
26064){
26065  sqlite3_int64 iCur = -1;
26066  sqlite3_int64 iHiwtr = -1;
26067  int i, nPercent;
26068  char zLine[200];
26069  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
26070  for(i=0, nPercent=0; zFormat[i]; i++){
26071    if( zFormat[i]=='%' ) nPercent++;
26072  }
26073  if( nPercent>1 ){
26074    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
26075  }else{
26076    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
26077  }
26078  cli_printf(out, "%-36s %s\n", zLabel, zLine);
26079}
26080
26081/*
26082** Display memory stats.
26083*/
26084static int display_stats(
26085  sqlite3 *db,                /* Database to query */
26086  ShellState *pArg,           /* Pointer to ShellState */
26087  int bReset                  /* True to reset the stats */
26088){
26089  int iCur, iHiwtr;
26090  sqlite3_int64 iCur64, iHiwtr64;
26091  FILE *out;
26092  if( pArg==0 || pArg->out==0 ) return 0;
26093  out = pArg->out;
26094
26095  if( pArg->pStmt && pArg->statsOn==2 ){
26096    int nCol, i, x;
26097    sqlite3_stmt *pStmt = pArg->pStmt;
26098    char z[100];
26099    nCol = sqlite3_column_count(pStmt);
26100    cli_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
26101    for(i=0; i<nCol; i++){
26102      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
26103      cli_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
26104#ifndef SQLITE_OMIT_DECLTYPE
26105      sqlite3_snprintf(30, z+x, "declared type:");
26106      cli_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
26107#endif
26108#ifdef SQLITE_ENABLE_COLUMN_METADATA
26109      sqlite3_snprintf(30, z+x, "database name:");
26110      cli_printf(out, "%-36s %s\n", z,
26111                           sqlite3_column_database_name(pStmt,i));
26112      sqlite3_snprintf(30, z+x, "table name:");
26113      cli_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
26114      sqlite3_snprintf(30, z+x, "origin name:");
26115      cli_printf(out, "%-36s %s\n", z,sqlite3_column_origin_name(pStmt,i));
26116#endif
26117    }
26118  }
26119
26120  if( pArg->statsOn==3 ){
26121    if( pArg->pStmt ){
26122      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset);
26123      cli_printf(out, "VM-steps: %d\n", iCur);
26124    }
26125    return 0;
26126  }
26127
26128  displayStatLine(out, "Memory Used:",
26129     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
26130  displayStatLine(out, "Number of Outstanding Allocations:",
26131     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
26132  if( pArg->shellFlgs & SHFLG_Pagecache ){
26133    displayStatLine(out, "Number of Pcache Pages Used:",
26134       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
26135  }
26136  displayStatLine(out, "Number of Pcache Overflow Bytes:",
26137     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
26138  displayStatLine(out, "Largest Allocation:",
26139     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
26140  displayStatLine(out, "Largest Pcache Allocation:",
26141     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
26142#ifdef YYTRACKMAXSTACKDEPTH
26143  displayStatLine(out, "Deepest Parser Stack:",
26144     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
26145#endif
26146
26147  if( db ){
26148    if( pArg->shellFlgs & SHFLG_Lookaside ){
26149      iHiwtr = iCur = -1;
26150      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
26151                        &iCur, &iHiwtr, bReset);
26152      cli_printf(out, 
26153           "Lookaside Slots Used:                %d (max %d)\n", iCur, iHiwtr);
26154      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
26155                        &iCur, &iHiwtr, bReset);
26156      cli_printf(out,
26157           "Successful lookaside attempts:       %d\n", iHiwtr);
26158      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
26159                        &iCur, &iHiwtr, bReset);
26160      cli_printf(out,
26161           "Lookaside failures due to size:      %d\n", iHiwtr);
26162      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
26163                        &iCur, &iHiwtr, bReset);
26164      cli_printf(out,
26165           "Lookaside failures due to OOM:       %d\n", iHiwtr);
26166    }
26167    iHiwtr = iCur = -1;
26168    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
26169    cli_printf(out,
26170           "Pager Heap Usage:                    %d bytes\n", iCur);
26171    iHiwtr = iCur = -1;
26172    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
26173    cli_printf(out,
26174           "Page cache hits:                     %d\n", iCur);
26175    iHiwtr = iCur = -1;
26176    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
26177    cli_printf(out,
26178           "Page cache misses:                   %d\n", iCur);
26179    iHiwtr64 = iCur64 = -1;
26180    sqlite3_db_status64(db, SQLITE_DBSTATUS_TEMPBUF_SPILL, &iCur64, &iHiwtr64,
26181                        0);
26182    iHiwtr = iCur = -1;
26183    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
26184    cli_printf(out,
26185           "Page cache writes:                   %d\n", iCur);
26186    iHiwtr = iCur = -1;
26187    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
26188    cli_printf(out,
26189           "Page cache spills:                   %d\n", iCur);
26190    cli_printf(out,
26191           "Temporary data spilled to disk:      %lld\n", iCur64);
26192    sqlite3_db_status64(db, SQLITE_DBSTATUS_TEMPBUF_SPILL, &iCur64, &iHiwtr64,
26193                        1);
26194    iHiwtr = iCur = -1;
26195    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
26196    cli_printf(out,
26197           "Schema Heap Usage:                   %d bytes\n", iCur);
26198    iHiwtr = iCur = -1;
26199    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
26200    cli_printf(out,
26201           "Statement Heap/Lookaside Usage:      %d bytes\n", iCur);
26202  }
26203
26204  if( pArg->pStmt ){
26205    int iHit, iMiss;
26206    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
26207                               bReset);
26208    cli_printf(out,
26209           "Fullscan Steps:                      %d\n", iCur);
26210    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
26211    cli_printf(out,
26212           "Sort Operations:                     %d\n", iCur);
26213    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
26214    cli_printf(out,
26215           "Autoindex Inserts:                   %d\n", iCur);
26216    iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT,
26217                               bReset);
26218    iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS,
26219                                bReset);
26220    if( iHit || iMiss ){
26221      cli_printf(out,
26222           "Bloom filter bypass taken:           %d/%d\n", iHit, iHit+iMiss);
26223    }
26224    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
26225    cli_printf(out,
26226           "Virtual Machine Steps:               %d\n", iCur);
26227    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
26228    cli_printf(out,
26229           "Reprepare operations:                %d\n", iCur);
26230    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
26231    cli_printf(out,
26232           "Number of times run:                 %d\n", iCur);
26233    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
26234    cli_printf(out,
26235           "Memory used by prepared stmt:        %d\n", iCur);
26236  }
26237
26238#ifdef __linux__
26239  displayLinuxIoStats(pArg->out);
26240#endif
26241
26242  /* Do not remove this machine readable comment: extra-stats-output-here */
26243
26244  return 0;
26245}
26246
26247/*
26248** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
26249*/
26250static unsigned int savedSelectTrace;
26251static unsigned int savedWhereTrace;
26252static void disable_debug_trace_modes(void){
26253  unsigned int zero = 0;
26254  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
26255  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
26256  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
26257  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
26258}
26259static void restore_debug_trace_modes(void){
26260  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
26261  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
26262}
26263
26264/* Create the TEMP table used to store parameter bindings */
26265static void bind_table_init(ShellState *p){
26266  int wrSchema = 0;
26267  int defensiveMode = 0;
26268  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
26269  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
26270  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
26271  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
26272  sqlite3_exec(p->db,
26273    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
26274    "  key TEXT PRIMARY KEY,\n"
26275    "  value\n"
26276    ") WITHOUT ROWID;",
26277    0, 0, 0);
26278  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
26279  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
26280}
26281
26282/*
26283** Bind parameters on a prepared statement.
26284**
26285** Parameter bindings are taken from a TEMP table of the form:
26286**
26287**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
26288**    WITHOUT ROWID;
26289**
26290** No bindings occur if this table does not exist.  The name of the table
26291** begins with "sqlite_" so that it will not collide with ordinary application
26292** tables.  The table must be in the TEMP schema.
26293*/
26294static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
26295  int nVar;
26296  int i;
26297  int rc;
26298  sqlite3_stmt *pQ = 0;
26299
26300  nVar = sqlite3_bind_parameter_count(pStmt);
26301  if( nVar==0 ) return;  /* Nothing to do */
26302  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
26303                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
26304    rc = SQLITE_NOTFOUND;
26305    pQ = 0;
26306  }else{
26307    rc = sqlite3_prepare_v2(pArg->db,
26308            "SELECT value FROM temp.sqlite_parameters"
26309            " WHERE key=?1", -1, &pQ, 0);
26310  }
26311  for(i=1; i<=nVar; i++){
26312    char zNum[30];
26313    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
26314    if( zVar==0 ){
26315      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
26316      zVar = zNum;
26317    }
26318    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
26319    if( rc==SQLITE_OK && pQ && sqlite3_step(pQ)==SQLITE_ROW ){
26320      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
26321#ifdef NAN
26322    }else if( sqlite3_strlike("_NAN", zVar, 0)==0 ){
26323      sqlite3_bind_double(pStmt, i, NAN);
26324#endif
26325#ifdef INFINITY
26326    }else if( sqlite3_strlike("_INF", zVar, 0)==0 ){
26327      sqlite3_bind_double(pStmt, i, INFINITY);
26328#endif
26329    }else if( strncmp(zVar, "$int_", 5)==0 ){
26330      sqlite3_bind_int(pStmt, i, atoi(&zVar[5]));
26331    }else if( strncmp(zVar, "$text_", 6)==0 ){
26332      size_t szVar = strlen(zVar);
26333      char *zBuf = sqlite3_malloc64( szVar-5 );
26334      if( zBuf ){
26335        memcpy(zBuf, &zVar[6], szVar-5);
26336        sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8);
26337      }
26338    }else if( strcmp(zVar, "$TIMER")==0 ){
26339      sqlite3_bind_double(pStmt, i, pArg->prevTimer);
26340#ifdef SQLITE_ENABLE_CARRAY
26341    }else if( strncmp(zVar, "$carray_", 8)==0 ){
26342      static char *azColorNames[] = {
26343        "azure", "black", "blue",   "brown", "cyan",   "fuchsia", "gold",
26344        "gray",  "green", "indigo", "khaki", "lime",   "magenta", "maroon",
26345        "navy",  "olive", "orange", "pink",  "purple", "red",     "silver",
26346        "tan",   "teal",  "violet", "white", "yellow"
26347      };
26348      static int aPrimes[] = {
26349        1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
26350       53, 59, 61, 67, 71, 73, 79, 83, 89, 97
26351      };
26352      /* Special bindings:  carray($carray_clr), carray($carray_primes)
26353      ** with --unsafe-testing:  carray($carray_clr_p,26,'char*'),
26354      **                         carray($carray_primes_p,26,'int32')
26355      */
26356      if( strcmp(zVar+8,"clr")==0 ){
26357        sqlite3_carray_bind(pStmt,i,azColorNames,26,SQLITE_CARRAY_TEXT,0);
26358      }else if( strcmp(zVar+8,"primes")==0 ){
26359        sqlite3_carray_bind(pStmt,i,aPrimes,26,SQLITE_CARRAY_INT32,0);
26360      }else if( strcmp(zVar+8,"clr_p")==0
26361             && ShellHasFlag(pArg,SHFLG_TestingMode) ){
26362        sqlite3_bind_pointer(pStmt,i,azColorNames,"carray",0);
26363      }else if( strcmp(zVar+8,"primes_p")==0
26364             && ShellHasFlag(pArg,SHFLG_TestingMode) ){
26365        sqlite3_bind_pointer(pStmt,i,aPrimes,"carray",0);
26366      }else{
26367        sqlite3_bind_null(pStmt, i);
26368      }
26369#endif  
26370    }else{
26371      sqlite3_bind_null(pStmt, i);
26372    }
26373    sqlite3_reset(pQ);
26374  }
26375  sqlite3_finalize(pQ);
26376}
26377
26378#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
26379/*
26380** This function is called to process SQL if the previous shell command
26381** was ".expert". It passes the SQL in the second argument directly to
26382** the sqlite3expert object.
26383**
26384** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
26385** code. In this case, (*pzErr) may be set to point to a buffer containing
26386** an English language error message. It is the responsibility of the
26387** caller to eventually free this buffer using sqlite3_free().
26388*/
26389static int expertHandleSQL(
26390  ShellState *pState,
26391  const char *zSql,
26392  char **pzErr
26393){
26394  assert( pState->expert.pExpert );
26395  assert( pzErr==0 || *pzErr==0 );
26396  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
26397}
26398
26399/*
26400** This function is called either to silently clean up the object
26401** created by the ".expert" command (if bCancel==1), or to generate a
26402** report from it and then clean it up (if bCancel==0).
26403**
26404** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
26405** code. In this case, (*pzErr) may be set to point to a buffer containing
26406** an English language error message. It is the responsibility of the
26407** caller to eventually free this buffer using sqlite3_free().
26408*/
26409static int expertFinish(
26410  ShellState *pState,
26411  int bCancel,
26412  char **pzErr
26413){
26414  int rc = SQLITE_OK;
26415  sqlite3expert *p = pState->expert.pExpert;
26416  FILE *out = pState->out;
26417  assert( p );
26418  assert( bCancel || pzErr==0 || *pzErr==0 );
26419  if( bCancel==0 ){
26420    int bVerbose = pState->expert.bVerbose;
26421
26422    rc = sqlite3_expert_analyze(p, pzErr);
26423    if( rc==SQLITE_OK ){
26424      int nQuery = sqlite3_expert_count(p);
26425      int i;
26426
26427      if( bVerbose ){
26428        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
26429        cli_puts("-- Candidates -----------------------------\n", out);
26430        cli_printf(out, "%s\n", zCand);
26431      }
26432      for(i=0; i<nQuery; i++){
26433        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
26434        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
26435        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
26436        if( zIdx==0 ) zIdx = "(no new indexes)\n";
26437        if( bVerbose ){
26438          cli_printf(out,
26439              "-- Query %d --------------------------------\n"
26440              "%s\n\n"
26441              ,i+1, zSql);
26442        }
26443        cli_printf(out, "%s\n%s\n", zIdx, zEQP);
26444      }
26445    }
26446  }
26447  sqlite3_expert_destroy(p);
26448  pState->expert.pExpert = 0;
26449  return rc;
26450}
26451
26452/*
26453** Implementation of ".expert" dot command.
26454*/
26455static int expertDotCommand(
26456  ShellState *pState,             /* Current shell tool state */
26457  char **azArg,                   /* Array of arguments passed to dot command */
26458  int nArg                        /* Number of entries in azArg[] */
26459){
26460  int rc = SQLITE_OK;
26461  char *zErr = 0;
26462  int i;
26463  int iSample = 0;
26464
26465  assert( pState->expert.pExpert==0 );
26466  memset(&pState->expert, 0, sizeof(ExpertInfo));
26467
26468  for(i=1; rc==SQLITE_OK && i<nArg; i++){
26469    char *z = azArg[i];
26470    int n;
26471    if( z[0]=='-' && z[1]=='-' ) z++;
26472    n = strlen30(z);
26473    if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
26474      pState->expert.bVerbose = 1;
26475    }
26476    else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
26477      if( i==(nArg-1) ){
26478        cli_printf(stderr, "option requires an argument: %s\n", z);
26479        rc = SQLITE_ERROR;
26480      }else{
26481        iSample = (int)integerValue(azArg[++i]);
26482        if( iSample<0 || iSample>100 ){
26483          cli_printf(stderr,"value out of range: %s\n", azArg[i]);
26484          rc = SQLITE_ERROR;
26485        }
26486      }
26487    }
26488    else{
26489      cli_printf(stderr,"unknown option: %s\n", z);
26490      rc = SQLITE_ERROR;
26491    }
26492  }
26493
26494  if( rc==SQLITE_OK ){
26495    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
26496    if( pState->expert.pExpert==0 ){
26497      cli_printf(stderr,
26498          "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
26499      rc = SQLITE_ERROR;
26500    }else{
26501      sqlite3_expert_config(
26502          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
26503      );
26504    }
26505  }
26506  sqlite3_free(zErr);
26507
26508  return rc;
26509}
26510#endif /* !SQLITE_OMIT_VIRTUALTABLE && !SQLITE_OMIT_AUTHORIZATION */
26511
26512/*
26513** QRF write callback
26514*/
26515static int shellWriteQR(void *pX, const char *z, sqlite3_int64 n){
26516  ShellState *pArg = (ShellState*)pX;
26517  cli_printf(pArg->out, "%.*s", (int)n, z);
26518  return SQLITE_OK;
26519}
26520
26521/*
26522** Execute a statement or set of statements.  Print
26523** any result rows/columns depending on the current mode
26524** set via the supplied callback.
26525**
26526** This is very similar to SQLite's built-in sqlite3_exec()
26527** function except it takes a slightly different callback
26528** and callback data argument.
26529*/
26530static int shell_exec(
26531  ShellState *pArg,                         /* Pointer to ShellState */
26532  const char *zSql,                         /* SQL to be evaluated */
26533  char **pzErrMsg                           /* Error msg written here */
26534){
26535  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
26536  int rc = SQLITE_OK;             /* Return Code */
26537  int rc2;
26538  const char *zLeftover;          /* Tail of unprocessed SQL */
26539  sqlite3 *db = pArg->db;
26540  unsigned char eStyle;
26541  sqlite3_qrf_spec spec;
26542
26543  if( pzErrMsg ){
26544    *pzErrMsg = NULL;
26545  }
26546  memcpy(&spec, &pArg->mode.spec, sizeof(spec));
26547  spec.xWrite = shellWriteQR;
26548  spec.pWriteArg = (void*)pArg;
26549  if( pArg->mode.eMode==MODE_Insert && ShellHasFlag(pArg,SHFLG_PreserveRowid) ){
26550    spec.bTitles = QRF_SW_On;
26551  }
26552         /*                        ,- This is true, but it is omitted
26553         ** vvvvvvvvvvvvvvvvvvv ----- to avoid compiler warnings.         */
26554  assert( /*pArg->mode.eMode>=0 &&*/ pArg->mode.eMode<ArraySize(aModeInfo) );
26555  eStyle = aModeInfo[pArg->mode.eMode].eStyle;
26556  if( pArg->mode.bAutoScreenWidth ){
26557    spec.nScreenWidth = shellScreenWidth();
26558  }
26559  if( spec.eBlob==QRF_BLOB_Auto ){
26560    switch( spec.eText ){
26561      case QRF_TEXT_Relaxed: /* fall through */
26562      case QRF_TEXT_Sql:  spec.eBlob = QRF_BLOB_Sql;   break;
26563      case QRF_TEXT_Json: spec.eBlob = QRF_BLOB_Json;  break;
26564      default:            spec.eBlob = QRF_BLOB_Text;  break;
26565    }
26566  }
26567
26568#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
26569  if( pArg->expert.pExpert ){
26570    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
26571    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
26572  }
26573#endif
26574
26575  while( zSql && zSql[0] && (SQLITE_OK == rc) ){
26576    static const char *zStmtSql;
26577    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
26578    if( SQLITE_OK != rc ){
26579      if( pzErrMsg ){
26580        *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
26581      }
26582    }else{
26583      int isExplain;
26584      if( !pStmt ){
26585        /* this happens for a comment or white-space */
26586        zSql = zLeftover;
26587        while( IsSpace(zSql[0]) ) zSql++;
26588        continue;
26589      }
26590      zStmtSql = sqlite3_sql(pStmt);
26591      if( zStmtSql==0 ) zStmtSql = "";
26592      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
26593
26594      /* save off the prepared statement handle */
26595      if( pArg ){
26596        pArg->pStmt = pStmt;
26597      }
26598     
26599      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
26600      isExplain = sqlite3_stmt_isexplain(pStmt);
26601      if( pArg && pArg->mode.autoEQP && isExplain==0 && pArg->dot.nArg==0 ){
26602        int triggerEQP = 0;
26603        u8 savedEnableTimer = pArg->enableTimer;
26604        pArg->enableTimer = 0;
26605        disable_debug_trace_modes();
26606        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
26607        if( pArg->mode.autoEQP>=AUTOEQP_trigger ){
26608          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
26609        }
26610        sqlite3_reset(pStmt);
26611        spec.eStyle = QRF_STYLE_Auto;
26612        sqlite3_stmt_explain(pStmt, 2);
26613        sqlite3_format_query_result(pStmt, &spec, 0);
26614        if( pArg->mode.autoEQP>=AUTOEQP_full ){
26615          sqlite3_reset(pStmt);
26616          sqlite3_stmt_explain(pStmt, 1);
26617          sqlite3_format_query_result(pStmt, &spec, 0);
26618        }
26619
26620        if( pArg->mode.autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
26621          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
26622        }
26623        sqlite3_reset(pStmt);
26624        sqlite3_stmt_explain(pStmt, 0);
26625        restore_debug_trace_modes();
26626        pArg->enableTimer = savedEnableTimer;
26627      }
26628
26629      bind_prepared_stmt(pArg, pStmt);
26630      if( isExplain && pArg->mode.autoExplain ){
26631        spec.eStyle = isExplain==1 ? QRF_STYLE_Explain : QRF_STYLE_Eqp;
26632        sqlite3_format_query_result(pStmt, &spec, pzErrMsg);
26633      }else if( pArg->mode.eMode==MODE_Www ){
26634        cli_printf(pArg->out,
26635              "</PRE>\n"
26636              "<TABLE border='1' cellspacing='0' cellpadding='2'>\n");
26637        spec.eStyle = QRF_STYLE_Html;
26638        sqlite3_format_query_result(pStmt, &spec, pzErrMsg);
26639        cli_printf(pArg->out,
26640              "</TABLE>\n"
26641              "<PRE>");
26642      }else{
26643        spec.eStyle = eStyle;
26644        sqlite3_format_query_result(pStmt, &spec, pzErrMsg);
26645      }
26646
26647      /* print usage stats if stats on */
26648      if( pArg && pArg->statsOn ){
26649        display_stats(db, pArg, 0);
26650      }
26651
26652      /* print loop-counters if required */
26653      if( pArg && pArg->mode.scanstatsOn ){
26654        char *zErr = 0;
26655        switch( pArg->mode.scanstatsOn ){
26656          case 1:   spec.eStyle = QRF_STYLE_Stats;     break;
26657          case 2:   spec.eStyle = QRF_STYLE_StatsEst;  break;
26658          default:  spec.eStyle = QRF_STYLE_StatsVm;   break;
26659        }
26660        sqlite3_reset(pStmt);
26661        rc = sqlite3_format_query_result(pStmt, &spec, &zErr);
26662        if( rc ){
26663          cli_printf(stderr, "Stats query failed: %s\n", zErr);
26664          sqlite3_free(zErr);
26665        }          
26666      }
26667
26668      /* Finalize the statement just executed. If this fails, save a
26669      ** copy of the error message. Otherwise, set zSql to point to the
26670      ** next statement to execute. */
26671      rc2 = sqlite3_finalize(pStmt);
26672      if( rc!=SQLITE_NOMEM ) rc = rc2;
26673      if( rc==SQLITE_OK ){
26674        zSql = zLeftover;
26675        while( IsSpace(zSql[0]) ) zSql++;
26676      }else if( pzErrMsg ){
26677        *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
26678      }
26679
26680      /* clear saved stmt handle */
26681      if( pArg ){
26682        pArg->pStmt = NULL;
26683      }
26684    }
26685  } /* end while */
26686
26687  return rc;
26688}
26689
26690/*
26691** Release memory previously allocated by tableColumnList().
26692*/
26693static void freeColumnList(char **azCol){
26694  int i;
26695  for(i=1; azCol[i]; i++){
26696    sqlite3_free(azCol[i]);
26697  }
26698  /* azCol[0] is a static string */
26699  sqlite3_free(azCol);
26700}
26701
26702/*
26703** Return a list of pointers to strings which are the names of all
26704** columns in table zTab.   The memory to hold the names is dynamically
26705** allocated and must be released by the caller using a subsequent call
26706** to freeColumnList().
26707**
26708** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
26709** value that needs to be preserved, then azCol[0] is filled in with the
26710** name of the rowid column.
26711**
26712** The first regular column in the table is azCol[1].  The list is terminated
26713** by an entry with azCol[i]==0.
26714*/
26715static char **tableColumnList(ShellState *p, const char *zTab){
26716  char **azCol = 0;
26717  sqlite3_stmt *pStmt;
26718  char *zSql;
26719  int nCol = 0;
26720  i64 nAlloc = 0;
26721  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
26722  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
26723  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
26724  int rc;
26725
26726  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
26727  shell_check_oom(zSql);
26728  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
26729  sqlite3_free(zSql);
26730  if( rc ) return 0;
26731  while( sqlite3_step(pStmt)==SQLITE_ROW ){
26732    if( nCol>=nAlloc-2 ){
26733      nAlloc = nAlloc*2 + nCol + 10;
26734      azCol = sqlite3_realloc64(azCol, nAlloc*sizeof(azCol[0]));
26735      shell_check_oom(azCol);
26736    }
26737    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
26738    shell_check_oom(azCol[nCol]);
26739    if( sqlite3_column_int(pStmt, 5) ){
26740      nPK++;
26741      if( nPK==1
26742       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
26743                          "INTEGER")==0
26744      ){
26745        isIPK = 1;
26746      }else{
26747        isIPK = 0;
26748      }
26749    }
26750  }
26751  sqlite3_finalize(pStmt);
26752  if( azCol==0 ) return 0;
26753  azCol[0] = 0;
26754  azCol[nCol+1] = 0;
26755
26756  /* The decision of whether or not a rowid really needs to be preserved
26757  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
26758  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
26759  ** rowids on tables where the rowid is inaccessible because there are other
26760  ** columns in the table named "rowid", "_rowid_", and "oid".
26761  */
26762  if( preserveRowid && isIPK ){
26763    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
26764    ** might be an alias for the ROWID.  But it might also be a WITHOUT ROWID
26765    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
26766    ** ROWID aliases.  To distinguish these cases, check to see if
26767    ** there is a "pk" entry in "PRAGMA index_list".  There will be
26768    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
26769    */
26770    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
26771                           " WHERE origin='pk'", zTab);
26772    shell_check_oom(zSql);
26773    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
26774    sqlite3_free(zSql);
26775    if( rc ){
26776      freeColumnList(azCol);
26777      return 0;
26778    }
26779    rc = sqlite3_step(pStmt);
26780    sqlite3_finalize(pStmt);
26781    preserveRowid = rc==SQLITE_ROW;
26782  }
26783  if( preserveRowid ){
26784    /* Only preserve the rowid if we can find a name to use for the
26785    ** rowid */
26786    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
26787    int i, j;
26788    for(j=0; j<3; j++){
26789      for(i=1; i<=nCol; i++){
26790        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
26791      }
26792      if( i>nCol ){
26793        /* At this point, we know that azRowid[j] is not the name of any
26794        ** ordinary column in the table.  Verify that azRowid[j] is a valid
26795        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
26796        ** tables will fail this last check */
26797        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
26798        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
26799        break;
26800      }
26801    }
26802  }
26803  return azCol;
26804}
26805
26806/*
26807** Toggle the reverse_unordered_selects setting.
26808*/
26809static void toggleSelectOrder(sqlite3 *db){
26810  sqlite3_stmt *pStmt = 0;
26811  int iSetting = 0;
26812  char zStmt[100];
26813  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
26814  if( sqlite3_step(pStmt)==SQLITE_ROW ){
26815    iSetting = sqlite3_column_int(pStmt, 0);
26816  }
26817  sqlite3_finalize(pStmt);
26818  sqlite3_snprintf(sizeof(zStmt), zStmt,
26819       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
26820  sqlite3_exec(db, zStmt, 0, 0, 0);
26821}
26822
26823/* Forward reference */
26824static int db_int(sqlite3 *db, const char *zSql, ...);
26825
26826/*
26827** This is a different callback routine used for dumping the database.
26828** Each row received by this callback consists of a table name,
26829** the table type ("index" or "table") and SQL to create the table.
26830** This routine should print text sufficient to recreate the table.
26831*/
26832static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
26833  int rc;
26834  const char *zTable;
26835  const char *zType;
26836  const char *zSql;
26837  ShellState *p = (ShellState *)pArg;
26838  int dataOnly;
26839  int noSys;
26840
26841  UNUSED_PARAMETER(azNotUsed);
26842  if( nArg!=3 || azArg==0 ) return 0;
26843  zTable = azArg[0];
26844  zType = azArg[1];
26845  zSql = azArg[2];
26846  if( zTable==0 ) return 0;
26847  if( zType==0 ) return 0;
26848  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
26849  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
26850
26851  if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
26852    /* The sqlite_sequence table is repopulated last.  Delete content
26853    ** in the sqlite_sequence table added by prior repopulations prior to
26854    ** repopulating sqlite_sequence itself.  But only do this if the
26855    ** table is non-empty, because if it is empty the table might not
26856    ** have been recreated by prior repopulations. See forum posts:
26857    ** 2024-10-13T17:10:01z and 2025-10-29T19:38:43z
26858    */
26859    if( db_int(p->db, "SELECT count(*) FROM sqlite_sequence")>0 ){
26860      if( !p->writableSchema ){
26861        cli_puts("PRAGMA writable_schema=ON;\n", p->out);
26862        p->writableSchema = 1;
26863      }
26864      cli_puts("CREATE TABLE IF NOT EXISTS sqlite_sequence(name,seq);\n"
26865                    "DELETE FROM sqlite_sequence;\n", p->out);
26866    }
26867  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
26868    if( !dataOnly ) cli_puts("ANALYZE sqlite_schema;\n", p->out);
26869  }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
26870    return 0;
26871  }else if( dataOnly ){
26872    /* no-op */
26873  }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
26874    char *zIns;
26875    if( !p->writableSchema ){
26876      cli_puts("PRAGMA writable_schema=ON;\n", p->out);
26877      p->writableSchema = 1;
26878    }
26879    zIns = sqlite3_mprintf(
26880       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
26881       "VALUES('table','%q','%q',0,'%q');",
26882       zTable, zTable, zSql);
26883    shell_check_oom(zIns);
26884    cli_printf(p->out, "%s\n", zIns);
26885    sqlite3_free(zIns);
26886    return 0;
26887  }else{
26888    printSchemaLine(p->out, zSql, ";\n");
26889  }
26890
26891  if( cli_strcmp(zType, "table")==0 ){
26892    ShellText sSelect;
26893    ShellText sTable;
26894    char **azCol;
26895    int i;
26896    Mode savedMode;
26897
26898    azCol = tableColumnList(p, zTable);
26899    if( azCol==0 ){
26900      p->nErr++;
26901      return 0;
26902    }
26903
26904    /* Always quote the table name, even if it appears to be pure ascii,
26905    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
26906    initText(&sTable);
26907    appendText(&sTable, zTable, quoteChar(zTable));
26908    /* If preserving the rowid, add a column list after the table name.
26909    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
26910    ** instead of the usual "INSERT INTO tab VALUES(...)".
26911    */
26912    if( azCol[0] ){
26913      appendText(&sTable, "(", 0);
26914      appendText(&sTable, azCol[0], 0);
26915      for(i=1; azCol[i]; i++){
26916        appendText(&sTable, ",", 0);
26917        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
26918      }
26919      appendText(&sTable, ")", 0);
26920    }
26921
26922    /* Build an appropriate SELECT statement */
26923    initText(&sSelect);
26924    appendText(&sSelect, "SELECT ", 0);
26925    if( azCol[0] ){
26926      appendText(&sSelect, azCol[0], 0);
26927      appendText(&sSelect, ",", 0);
26928    }
26929    for(i=1; azCol[i]; i++){
26930      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
26931      if( azCol[i+1] ){
26932        appendText(&sSelect, ",", 0);
26933      }
26934    }
26935    freeColumnList(azCol);
26936    appendText(&sSelect, " FROM ", 0);
26937    appendText(&sSelect, zTable, quoteChar(zTable));
26938
26939
26940    savedMode = p->mode;
26941    p->mode.spec.zTableName = (char*)zTable;
26942    p->mode.eMode = MODE_Insert;
26943    p->mode.spec.eText = QRF_TEXT_Sql;
26944    p->mode.spec.eBlob = QRF_BLOB_Sql;
26945    p->mode.spec.bTitles = QRF_No;
26946    p->mode.spec.nCharLimit = 0;
26947    rc = shell_exec(p, sSelect.zTxt, 0);
26948    if( (rc&0xff)==SQLITE_CORRUPT ){
26949      cli_puts("/****** CORRUPTION ERROR *******/\n", p->out);
26950      toggleSelectOrder(p->db);
26951      shell_exec(p, sSelect.zTxt, 0);
26952      toggleSelectOrder(p->db);
26953    }
26954    p->mode = savedMode;
26955    freeText(&sTable);
26956    freeText(&sSelect);
26957    if( rc ) p->nErr++;
26958  }
26959  return 0;
26960}
26961
26962/*
26963** Run zQuery.  Use dump_callback() as the callback routine so that
26964** the contents of the query are output as SQL statements.
26965**
26966** If we get a SQLITE_CORRUPT error, rerun the query after appending
26967** "ORDER BY rowid DESC" to the end.
26968*/
26969static int run_schema_dump_query(
26970  ShellState *p,
26971  const char *zQuery
26972){
26973  int rc;
26974  char *zErr = 0;
26975  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
26976  if( rc==SQLITE_CORRUPT ){
26977    char *zQ2;
26978    int len = strlen30(zQuery);
26979    cli_puts("/****** CORRUPTION ERROR *******/\n", p->out);
26980    if( zErr ){
26981      cli_printf(p->out, "/****** %s ******/\n", zErr);
26982      sqlite3_free(zErr);
26983      zErr = 0;
26984    }
26985    zQ2 = malloc( len+100 );
26986    if( zQ2==0 ) return rc;
26987    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
26988    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
26989    if( rc ){
26990      cli_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
26991    }else{
26992      rc = SQLITE_CORRUPT;
26993    }
26994    free(zQ2);
26995  }
26996  sqlite3_free(zErr);
26997  return rc;
26998}
26999
27000/*
27001** Text of help messages.
27002**
27003** The help text for each individual command begins with a line that starts
27004** with ".".  Subsequent lines are supplemental information.
27005**
27006** There must be two or more spaces between the end of the command and the
27007** start of the description of what that command does.
27008*/
27009static const char *(azHelp[]) = {
27010#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
27011  && !defined(SQLITE_SHELL_FIDDLE)
27012  ".archive ...             Manage SQL archives",
27013  "   Each command must have exactly one of the following options:",
27014  "     -c, --create               Create a new archive",
27015  "     -u, --update               Add or update files with changed mtime",
27016  "     -i, --insert               Like -u but always add even if unchanged",
27017  "     -r, --remove               Remove files from archive",
27018  "     -t, --list                 List contents of archive",
27019  "     -x, --extract              Extract files from archive",
27020  "   Optional arguments:",
27021  "     -v, --verbose              Print each filename as it is processed",
27022  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
27023  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
27024  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
27025  "     -g, --glob                 Use glob matching for names in archive",
27026  "     -n, --dryrun               Show the SQL that would have occurred",
27027  "   Examples:",
27028  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
27029  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
27030  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
27031  "   See also:",
27032  "      http://sqlite.org/cli.html#sqlite_archive_support",
27033#endif
27034#ifndef SQLITE_OMIT_AUTHORIZATION
27035  ".auth ON|OFF             Show authorizer callbacks",
27036#endif
27037#ifndef SQLITE_SHELL_FIDDLE
27038  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
27039  "   Options:",
27040  "       --append            Use the appendvfs",
27041  "       --async             Write to FILE without journal and fsync()",
27042#endif
27043  ".bail on|off             Stop after hitting an error.  Default OFF",
27044#ifndef SQLITE_SHELL_FIDDLE
27045  ".cd DIRECTORY            Change the working directory to DIRECTORY",
27046#endif
27047  ".changes on|off          Show number of rows changed by SQL",
27048  ".check OPTIONS ...       Verify the results of a .testcase",
27049#ifndef SQLITE_SHELL_FIDDLE
27050  ".clone NEWDB             Clone data into NEWDB from the existing database",
27051#endif
27052  ".connection [close] [#]  Open or close an auxiliary database connection",
27053  ".crlf ?on|off?           Whether or not to use \\r\\n line endings",
27054  ".databases               List names and files of attached databases",
27055  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
27056#if SQLITE_SHELL_HAVE_RECOVER
27057  ".dbinfo ?DB?             Show status information about the database",
27058#endif
27059  ".dbtotxt                 Hex dump of the database file",
27060  ".dump ?OBJECTS?          Render database content as SQL",
27061  "   Options:",
27062  "     --data-only            Output only INSERT statements",
27063  "     --newlines             Allow unescaped newline characters in output",
27064  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
27065  "     --preserve-rowids      Include ROWID values in the output",
27066  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
27067  "   Additional LIKE patterns can be given in subsequent arguments",
27068  ".echo on|off             Turn command echo on or off",
27069  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
27070  "   Other Modes:",
27071#ifdef SQLITE_DEBUG
27072  "      test                  Show raw EXPLAIN QUERY PLAN output",
27073  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
27074#endif
27075  "      trigger               Like \"full\" but also show trigger bytecode",
27076#ifndef SQLITE_SHELL_FIDDLE
27077  ".excel                   Display the output of next command in spreadsheet",
27078  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
27079#endif
27080#ifndef SQLITE_SHELL_FIDDLE
27081  ".exit ?CODE?             Exit this program with return-code CODE",
27082#endif
27083#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
27084  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
27085#endif
27086  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
27087  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
27088  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
27089  "   --help                  Show CMD details",
27090  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
27091  ".headers on|off          Turn display of headers on or off",
27092  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
27093#ifndef SQLITE_SHELL_FIDDLE
27094  ".import FILE TABLE       Import data from FILE into TABLE",
27095#endif
27096#ifndef SQLITE_OMIT_TEST_CONTROL
27097  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
27098#endif
27099  ".indexes ?PATTERN?       Show names of indexes matching PATTERN",
27100  "   -a|--all                Also show system-generated indexes",
27101  "   --expr                  Show only expression indexes",
27102  "   --sys                   Show only system-generated indexes",
27103  ".intck ?STEPS_PER_UNLOCK?  Run an incremental integrity check on the db",
27104#ifdef SQLITE_ENABLE_IOTRACE
27105  ",iotrace FILE            Enable I/O diagnostic logging to FILE",
27106#endif
27107  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
27108  ".lint OPTIONS            Report potential schema issues.",
27109  "     Options:",
27110  "        fkey-indexes     Find missing foreign key indexes",
27111#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
27112  ".load FILE ?ENTRY?       Load an extension library",
27113#endif
27114#if !defined(SQLITE_SHELL_FIDDLE)
27115  ".log FILE|on|off         Turn logging on or off.  FILE can be stderr/stdout",
27116#else
27117  ".log on|off              Turn logging on or off.",
27118#endif
27119  ".mode ?MODE? ?OPTIONS?   Set output mode",
27120#ifndef SQLITE_SHELL_FIDDLE
27121  ".nonce STRING            Suspend safe mode for one command if nonce matches",
27122#endif
27123  ".nullvalue STRING        Use STRING in place of NULL values",
27124#ifndef SQLITE_SHELL_FIDDLE
27125  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
27126  /* Note that .open is (partially) available in WASM builds but is
27127  ** currently only intended to be used by the fiddle tool, not
27128  ** end users, so is "undocumented." */
27129  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
27130  "     Options:",
27131  "        --append        Use appendvfs to append database to the end of FILE",
27132#endif
27133#ifndef SQLITE_OMIT_DESERIALIZE
27134  "        --deserialize   Load into memory using sqlite3_deserialize()",
27135#endif
27136/*"        --exclusive     Set the SQLITE_OPEN_EXCLUSIVE flag", UNDOCUMENTED */
27137#ifndef SQLITE_OMIT_DESERIALIZE
27138  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
27139#endif
27140  "        --ifexist       Only open if FILE already exists",
27141#ifndef SQLITE_OMIT_DESERIALIZE
27142  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
27143#endif
27144  "        --new           Initialize FILE to an empty database",
27145  "        --normal        FILE is an ordinary SQLite database",
27146  "        --nofollow      Do not follow symbolic links",
27147  "        --readonly      Open FILE readonly",
27148  "        --zip           FILE is a ZIP archive",
27149#ifndef SQLITE_SHELL_FIDDLE
27150  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
27151#endif
27152  ".parameter CMD ...       Manage SQL parameter bindings",
27153  "   clear                   Erase all bindings",
27154  "   init                    Initialize the TEMP table that holds bindings",
27155  "   list                    List the current parameter bindings",
27156  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
27157  "                           PARAMETER should start with one of: $ : @ ?",
27158  "   unset PARAMETER         Remove PARAMETER from the binding table",
27159  ".print STRING...         Print literal STRING",
27160#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
27161  ".progress N              Invoke progress handler after every N opcodes",
27162  "   --limit N                 Interrupt after N progress callbacks",
27163  "   --once                    Do no more than one progress interrupt",
27164  "   --quiet|-q                No output except at interrupts",
27165  "   --reset                   Reset the count for each input and interrupt",
27166  "   --timeout S               Halt after running for S seconds",
27167#endif
27168  ".prompt MAIN CONTINUE    Replace the standard prompts",
27169#ifndef SQLITE_SHELL_FIDDLE
27170  ".quit                    Stop interpreting input stream, exit if primary.",
27171  ".read FILE               Read input from FILE or command output",
27172  "    If FILE begins with \"|\", it is a command that generates the input.",
27173#endif
27174#if SQLITE_SHELL_HAVE_RECOVER
27175  ".recover                 Recover as much data as possible from corrupt db.",
27176  "   --ignore-freelist        Ignore pages that appear to be on db freelist",
27177  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
27178  "   --no-rowids              Do not attempt to recover rowid values",
27179  "                            that are not also INTEGER PRIMARY KEYs",
27180#endif
27181#ifndef SQLITE_SHELL_FIDDLE
27182  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
27183  ".save ?OPTIONS? FILE     Write database to FILE (an alias for .backup ...)",
27184#endif
27185  ".scanstats on|off|est    Turn sqlite3_stmt_scanstatus() metrics on or off",
27186  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
27187  "   Options:",
27188  "      --indent             Try to pretty-print the schema",
27189  "      --nosys              Omit objects whose names start with \"sqlite_\"",
27190  ",selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
27191  "    Options:",
27192  "       --init               Create a new SELFTEST table",
27193  "       -v                   Verbose output",
27194  ",separator COL ?ROW?     Change the column and row separators",
27195#if defined(SQLITE_ENABLE_SESSION)
27196  ".session ?NAME? CMD ...  Create or control sessions",
27197  "   Subcommands:",
27198  "     attach TABLE             Attach TABLE",
27199  "     changeset FILE           Write a changeset into FILE",
27200  "     close                    Close one session",
27201  "     enable ?BOOLEAN?         Set or query the enable bit",
27202  "     filter GLOB...           Reject tables matching GLOBs",
27203  "     indirect ?BOOLEAN?       Mark or query the indirect status",
27204  "     isempty                  Query whether the session is empty",
27205  "     list                     List currently open session names",
27206  "     open DB NAME             Open a new session on DB",
27207  "     patchset FILE            Write a patchset into FILE",
27208  "   If ?NAME? is omitted, the first defined session is used.",
27209#endif
27210  ".sha3sum ...             Compute a SHA3 hash of database content",
27211  "    Options:",
27212  "      --schema              Also hash the sqlite_schema table",
27213  "      --sha3-224            Use the sha3-224 algorithm",
27214  "      --sha3-256            Use the sha3-256 algorithm (default)",
27215  "      --sha3-384            Use the sha3-384 algorithm",
27216  "      --sha3-512            Use the sha3-512 algorithm",
27217  "    Any other argument is a LIKE pattern for tables to hash",
27218#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
27219  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
27220#endif
27221  ",show                    Show the current values for various settings",
27222  ".stats ?ARG?             Show stats or turn stats on or off",
27223  "   off                      Turn off automatic stat display",
27224  "   on                       Turn on automatic stat display",
27225  "   stmt                     Show statement stats",
27226  "   vmstep                   Show the virtual machine step count only",
27227#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
27228  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
27229#endif
27230  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
27231  ".testcase NAME           Begin a test case.",
27232  ",testctrl CMD ...        Run various sqlite3_test_control() operations",
27233  "                           Run \".testctrl\" with no arguments for details",
27234  ".timeout MS              Try opening locked tables for MS milliseconds",
27235  ".timer on|off|once       Turn SQL timer on or off.",
27236#ifndef SQLITE_OMIT_TRACE
27237  ".trace ?OPTIONS?         Output each SQL statement as it is run",
27238  "    FILE                    Send output to FILE",
27239  "    stdout                  Send output to stdout",
27240  "    stderr                  Send output to stderr",
27241  "    off                     Disable tracing",
27242  "    --expanded              Expand query parameters",
27243#ifdef SQLITE_ENABLE_NORMALIZE
27244  "    --normalized            Normal the SQL statements",
27245#endif
27246  "    --plain                 Show SQL as it is input",
27247  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
27248  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
27249  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
27250  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
27251#endif /* SQLITE_OMIT_TRACE */
27252#ifdef SQLITE_DEBUG
27253  ".unmodule NAME ...       Unregister virtual table modules",
27254  "    --allexcept             Unregister everything except those named",
27255#endif
27256  ".version                 Show source, library and compiler versions",
27257  ".vfsinfo ?AUX?           Information about the top-level VFS",
27258  ".vfslist                 List all available VFSes",
27259  ".vfsname ?AUX?           Print the name of the VFS stack",
27260  ",width NUM1 NUM2 ...     Set minimum column widths for columnar output",
27261  "     Negative values right-justify",
27262#ifndef SQLITE_SHELL_FIDDLE
27263  ".www                     Display output of the next command in web browser",
27264  "    --plain                 Show results as text/plain, not as HTML",
27265#endif
27266};
27267
27268/**************************************************************
27269** "Usage" help text automatically generated from comments */
27270static const struct {
27271  const char *zCmd;   /* Name of the dot-command */
27272  const char *zUsage; /* Documentation */
27273} aUsage[] = {
27274  { ".import",
27275"USAGE: .import [OPTIONS] FILE TABLE\n"
27276"\n"
27277"Import CSV or similar text from FILE into TABLE.  If TABLE does\n"
27278"not exist, it is created using the first row of FILE as the column\n"
27279"names.  If FILE begins with \"|\" then it is a command that is run\n"
27280"and the output from the command is used as the input data.  If\n"
27281"FILE begins with \"<<\" followed by a label, then content is read from\n"
27282"the script until the first line that matches the label.\n"
27283"\n"
27284"The content of FILE is interpreted using RFC-4180 (\"CSV\") quoting\n"
27285"rules unless the current mode is \"ascii\" or \"tabs\" or unless one\n"
27286"the --ascii option is used.\n"
27287"\n"
27288"The column and row separators must be single ASCII characters.  If\n"
27289"multiple characters or a Unicode character are specified for the\n"
27290"separators, then only the first byte of the separator is used.  Except,\n"
27291"if the row separator is \\n and the mode is not --ascii, then \\r\\n is\n"
27292"understood as a row separator too.\n"
27293"\n"
27294"Options:\n"
27295"  --ascii         Do not use RFC-4180 quoting.  Use \\037 and \\036\n"
27296"                  as column and row separators on input, unless other\n"
27297"                  delimiters are specified using --colsep and/or --rowsep\n"
27298"  --colsep CHAR   Use CHAR as the column separator.\n"
27299"  --csv           Input is standard RFC-4180 CSV.\n"
27300"  --esc CHAR      Use CHAR as an escape character in unquoted CSV inputs.\n"
27301"  --qesc CHAR     Use CHAR as an escape character in quoted CSV inputs.\n"
27302"  --rowsep CHAR   Use CHAR as the row separator.\n"
27303"  --schema S      When creating TABLE, put it in schema S\n"
27304"  --skip N        Ignore the first N rows of input\n"
27305"  -v              Verbose mode\n"
27306  },
27307  { ".mode",
27308"USAGE: .mode [MODE] [OPTIONS]\n"
27309"\n"
27310"Change the output mode to MODE and/or apply OPTIONS to the output mode.\n"
27311"Arguments are processed from left to right.  If no arguments, show the\n"
27312"current output mode and relevant options.\n"
27313"\n"
27314"Options:\n"
27315"  --align STRING           Set the alignment of text in columnar modes\n"
27316"                           String consists of characters 'L', 'C', 'R'\n"
27317"                           meaning \"left\", \"centered\", and \"right\", with\n"
27318"                           one letter per column starting from the left.\n"
27319"                           Unspecified alignment defaults to 'L'.\n"
27320"  --blob-quote ARG         ARG can be \"auto\", \"text\", \"sql\", \"hex\", \"tcl\",\n"
27321"                           \"json\", or \"size\".  Default is \"auto\".\n"
27322"  --border on|off          Show outer border on \"box\" and \"table\" modes.\n"
27323"  --charlimit N            Set the maximum number of output characters to\n"
27324"                           show for any single SQL value to N. Longer values\n"
27325"                           truncated. Zero means \"no limit\".\n"
27326"  --colsep STRING          Use STRING as the column separator\n"
27327"  --escape ESC             Enable/disable escaping of control characters\n"
27328"                           found in the output. ESC can be \"off\", \"ascii\",\n"
27329"                           or \"symbol\".\n"
27330"  --linelimit N            Set the maximum number of output lines to show for\n"
27331"                           any single SQL value to N. Longer values are\n"
27332"                           truncated. Zero means \"no limit\". Only works\n"
27333"                           in \"line\" mode and in columnar modes.\n"
27334"  --limits L,C,T           Shorthand for \"--linelimit L --charlimit C\n"
27335"                           --titlelimit T\". The \",T\" can be omitted in which\n"
27336"                           case the --titlelimit is unchanged.  The argument\n"
27337"                           can also be \"off\" to mean \"0,0,0\" or \"on\" to\n"
27338"                           mean \"5,300,20\".\n"
27339"  --list                   List available modes\n"
27340"  --multiinsert N          In \"insert\" mode, put multiple rows on a single\n"
27341"                           INSERT statement until the size exceeds N bytes.\n"
27342"  --null STRING            Render SQL NULL values as the given string\n"
27343"  --once                   Setting changes to the right are reverted after\n"
27344"                           the next SQL command.\n"
27345"  --quote ARG              Enable/disable quoting of text. ARG can be\n"
27346"                           \"off\", \"on\", \"sql\", \"relaxed\", \"csv\", \"html\",\n"
27347"                           \"tcl\", or \"json\". \"off\" means show the text as-is.\n"
27348"                           \"on\" is an alias for \"sql\".\n"
27349"  --reset                  Changes all mode settings back to their default.\n"
27350"  --rowsep STRING          Use STRING as the row separator\n"
27351"  --sw|--screenwidth N     Declare the screen width of the output device\n"
27352"                           to be N characters.  An attempt may be made to\n"
27353"                           wrap output text to fit within this limit. Zero\n"
27354"                           means \"no limit\".  Or N can be \"auto\" to set the\n"
27355"                           width automatically.\n"
27356"  --tablename NAME         Set the name of the table for \"insert\" mode.\n"
27357"  --tag NAME               Save mode to the left as NAME.\n"
27358"  --textjsonb BOOLEAN      If enabled, JSONB text is displayed as text JSON.\n"
27359"  --title ARG              Whether or not to show column headers, and if so\n"
27360"                           how to encode them.  ARG can be \"off\", \"on\",\n"
27361"                           \"sql\", \"csv\", \"html\", \"tcl\", or \"json\".\n"
27362"  --titlelimit N           Limit the length of column titles to N characters.\n"
27363"  -v|--verbose             Verbose output\n"
27364"  --widths LIST            Set the columns widths for columnar modes. The\n"
27365"                           argument is a list of integers, one for each\n"
27366"                           column. A \"0\" width means use a dynamic width\n"
27367"                           based on the actual width of data. If there are\n"
27368"                           fewer entries in LIST than columns, \"0\" is used\n"
27369"                           for the unspecified widths.\n"
27370"  --wordwrap BOOLEAN       Enable/disable word wrapping\n"
27371"  --wrap N                 Wrap columns wider than N characters\n"
27372"  --ww                     Shorthand for \"--wordwrap on\"\n"
27373  },
27374  { ".output",
27375"USAGE: .output [OPTIONS] [FILE]\n"
27376"\n"
27377"Begin redirecting output to FILE.  Or if FILE is omitted, revert\n"
27378"to sending output to the console.  If FILE begins with \"|\" then\n"
27379"the remainder of file is taken as a pipe and output is directed\n"
27380"into that pipe.  If FILE is \"memory\" then output is captured in an\n"
27381"internal memory buffer.  If FILE is \"off\" then output is redirected\n"
27382"into /dev/null or the equivalent.\n"
27383"\n"
27384"Options:\n"
27385"  --bom             Prepend a byte-order mark to the output\n"
27386"  -e                Accumulate output in a temporary text file then\n"
27387"                    launch a text editor when the redirection ends.\n"
27388"  --error-prefix X  Use X as the left-margin prefix for error messages.\n"
27389"                    Set to an empty string to restore the default.\n"
27390"  --keep            Keep redirecting output to its current destination.\n"
27391"                    Use this option in combination with --show or\n"
27392"                    with --error-prefix when you do not want to stop\n"
27393"                    a current redirection.\n"
27394"  --plain           Use plain text rather than HTML tables with -w\n"
27395"  --show            Show output text captured by .testcase or by\n"
27396"                    redirecting to \"memory\".\n"
27397"  -w                Show the output in a web browser.  Output is\n"
27398"                    written into a temporary HTML file until the\n"
27399"                    redirect ends, then the web browser is launched.\n"
27400"                    Query results  are shown as HTML tables, unless\n"
27401"                    the --plain is used too.\n"
27402"  -x                Show the output in a spreadsheet.  Output is\n"
27403"                    written to a temp file as CSV then the spreadsheet\n"
27404"                    is launched when\n"
27405  },
27406  { ".once",
27407"USAGE: .once [OPTIONS] FILE ...\n"
27408"\n"
27409"Write the output for the next line of SQL or the next dot-command into\n"
27410"FILE.  If FILE begins with \"|\" then it is a program into which output\n"
27411"is written. The FILE argument should be omitted if one of the -e, -w,\n"
27412"or -x options is used.\n"
27413"\n"
27414"Options:\n"
27415"  -e                Capture output into a temporary file then bring up\n"
27416"                    a text editor on that temporary file.\n"
27417"  --plain           Use plain text rather than HTML tables with -w\n"
27418"  -w                Capture output into an HTML file then bring up that\n"
27419"                    file in a web browser\n"
27420"  -x                Show the output in a spreadsheet.  Output is\n"
27421"                    written to a temp file as CSV then the spreadsheet\n"
27422"                    is launched when\n"
27423  },
27424  { ".check",
27425"USAGE: .check [OPTIONS] PATTERN\n"
27426"\n"
27427"Verify results of commands since the most recent .testcase command.\n"
27428"Restore output to the console, unless --keep is used.\n"
27429"\n"
27430"If PATTERN starts with \"<<ENDMARK\" then the actual pattern is taken from\n"
27431"subsequent lines of text up to the first line that begins with ENDMARK.\n"
27432"All pattern lines and the ENDMARK are discarded.\n"
27433"\n"
27434"Options:\n"
27435"  --exact                Do an exact comparison including leading and\n"
27436"                         trailing whitespace.\n"
27437"  --glob                 Treat PATTERN as a GLOB\n"
27438"  --keep                 Do not reset the testcase.  More .check commands\n"
27439"                         will follow.\n"
27440"  --notglob              Output should not match PATTERN\n"
27441"  --show                 Write testcase output to the screen, for debugging.\n"
27442  },
27443  { ".testcase",
27444"USAGE: .testcase [OPTIONS] NAME\n"
27445"\n"
27446"Start a new test case identified by NAME.  All output\n"
27447"through the next \".check\" command is captured for comparison. See the\n"
27448"\".check\" commandn for additional informatioon.\n"
27449"\n"
27450"Options:\n"
27451"  --error-prefix TEXT       Change error message prefix text to TEXT\n"
27452  },
27453};
27454
27455/*
27456** Return a pointer to usage text for zCmd, or NULL if none exists.
27457*/
27458static const char *findUsage(const char *zCmd){
27459  int i;
27460  for(i=0; i<ArraySize(aUsage); i++){
27461    if( sqlite3_strglob(zCmd, aUsage[i].zCmd)==0 ) return aUsage[i].zUsage;
27462  }
27463  return 0;
27464}
27465
27466/*
27467** Output help text for commands that match zPattern.
27468**
27469**    *   If zPattern is NULL, then show all documented commands, but
27470**        only give a one-line summary of each.
27471**
27472**    *   If zPattern is "-a" or "-all" or "--all" then show all help text
27473**        for all commands except undocumented commands.
27474**
27475**    *   If zPattern is "0" then show all help for undocumented commands.
27476**        Undocumented commands begin with "," instead of "." in the azHelp[]
27477**        array.
27478**
27479**    *   If zPattern is a prefix for one or more documented commands, then
27480**        show help for those commands.  If only a single command matches the
27481**        prefix, show the full text of the help.  If multiple commands match,
27482**        Only show just the first line of each.
27483**
27484**    *   Otherwise, show the complete text of any documented command for which
27485**        zPattern is a LIKE match for any text within that command help
27486**        text.
27487**
27488** Return the number commands that match zPattern.
27489*/
27490static int showHelp(FILE *out, const char *zPattern){
27491  int i = 0;
27492  int j = 0;
27493  int n = 0;
27494  char *zPat;
27495  const char *zHit = 0;
27496  if( zPattern==0 ){
27497    /* Show just the first line for all help topics */
27498    zPattern = "[a-z]";
27499  }else if( cli_strcmp(zPattern,"-a")==0
27500         || cli_strcmp(zPattern,"-all")==0
27501         || cli_strcmp(zPattern,"--all")==0
27502  ){
27503    /* Show everything except undocumented commands */
27504    zPattern = ".";
27505  }else if( cli_strcmp(zPattern,"0")==0 ){
27506    /* Show complete help text of undocumented commands */
27507    int show = 0;
27508    for(i=0; i<ArraySize(azHelp); i++){
27509      if( azHelp[i][0]=='.' ){
27510        show = 0;
27511      }else if( azHelp[i][0]==',' ){
27512        show = 1;
27513        cli_printf(out, ".%s\n", &azHelp[i][1]);
27514        n++;
27515      }else if( show ){
27516        cli_printf(out, "%s\n", azHelp[i]);
27517      }
27518    }
27519    return n;
27520  }
27521
27522  /* Seek documented commands for which zPattern is an exact prefix */
27523  zPat = sqlite3_mprintf(".%s*", zPattern[0]=='.' ? &zPattern[1] : zPattern);
27524  shell_check_oom(zPat);
27525  for(i=0; i<ArraySize(azHelp); i++){
27526    if( sqlite3_strglob(zPat, azHelp[i])==0 ){
27527      if( zHit ) cli_printf(out, "%s\n", zHit);
27528      zHit = azHelp[i];
27529      j = i+1;
27530      n++;
27531    }
27532  }
27533  if( n ){
27534    if( n==1 ){
27535      const char *zUsage = findUsage(zPat);
27536      if( zUsage ){
27537        cli_puts(zUsage, out);
27538      }else{
27539        /* when zPattern is a prefix of exactly one command, then include
27540        ** the details of that command, which should begin at offset j */
27541        cli_printf(out, "%s\n", zHit);
27542        while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){
27543          cli_printf(out, "%s\n", azHelp[j]);
27544          j++;
27545        }
27546      }
27547    }else{
27548      cli_printf(out, "%s\n", zHit);
27549    }
27550  }
27551  sqlite3_free(zPat);
27552  if( n ) return n;
27553
27554  /* Look for documented commands that contain zPattern anywhere.
27555  ** Show complete text of all documented commands that match. */
27556  zPat = sqlite3_mprintf("%%%s%%", zPattern);
27557  shell_check_oom(zPat);
27558  for(i=0; i<ArraySize(azHelp); i++){
27559    if( azHelp[i][0]==',' ){
27560      while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i;
27561      continue;
27562    }
27563    if( azHelp[i][0]=='.' ) j = i;
27564    if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
27565      cli_printf(out, "%s\n", azHelp[j]);
27566      while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){
27567        j++;
27568        cli_printf(out, "%s\n", azHelp[j]);
27569      }
27570      i = j;
27571      n++;
27572    }
27573  }
27574  sqlite3_free(zPat);
27575  return n;
27576}
27577
27578/* Forward reference */
27579static int process_input(ShellState *p, const char*);
27580
27581/*
27582** Read the content of file zName into memory obtained from sqlite3_malloc64()
27583** and return a pointer to the buffer. The caller is responsible for freeing
27584** the memory.
27585**
27586** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
27587** read.
27588**
27589** For convenience, a nul-terminator byte is always appended to the data read
27590** from the file before the buffer is returned. This byte is not included in
27591** the final value of (*pnByte), if applicable.
27592**
27593** NULL is returned if any error is encountered. The final value of *pnByte
27594** is undefined in this case.
27595*/
27596static char *readFile(const char *zName, int *pnByte){
27597  FILE *in = sqlite3_fopen(zName, "rb");
27598  long nIn;
27599  size_t nRead;
27600  char *pBuf;
27601  int rc;
27602  if( in==0 ) return 0;
27603  rc = fseek(in, 0, SEEK_END);
27604  if( rc!=0 ){
27605    cli_printf(stderr,"Error: '%s' not seekable\n", zName);
27606    fclose(in);
27607    return 0;
27608  }
27609  nIn = ftell(in);
27610  rewind(in);
27611  pBuf = sqlite3_malloc64( nIn+1 );
27612  if( pBuf==0 ){
27613    cli_puts("Error: out of memory\n", stderr);
27614    fclose(in);
27615    return 0;
27616  }
27617  nRead = fread(pBuf, nIn, 1, in);
27618  fclose(in);
27619  if( nRead!=1 ){
27620    sqlite3_free(pBuf);
27621    cli_printf(stderr,"Error: cannot read '%s'\n", zName);
27622    return 0;
27623  }
27624  pBuf[nIn] = 0;
27625  if( pnByte ) *pnByte = nIn;
27626  return pBuf;
27627}
27628
27629#if defined(SQLITE_ENABLE_SESSION)
27630/*
27631** Close a single OpenSession object and release all of its associated
27632** resources.
27633*/
27634static void session_close(OpenSession *pSession){
27635  int i;
27636  sqlite3session_delete(pSession->p);
27637  sqlite3_free(pSession->zName);
27638  for(i=0; i<pSession->nFilter; i++){
27639    sqlite3_free(pSession->azFilter[i]);
27640  }
27641  sqlite3_free(pSession->azFilter);
27642  memset(pSession, 0, sizeof(OpenSession));
27643}
27644#endif
27645
27646/*
27647** Close all OpenSession objects and release all associated resources.
27648*/
27649#if defined(SQLITE_ENABLE_SESSION)
27650static void session_close_all(ShellState *p, int i){
27651  int j;
27652  struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
27653  for(j=0; j<pAuxDb->nSession; j++){
27654    session_close(&pAuxDb->aSession[j]);
27655  }
27656  pAuxDb->nSession = 0;
27657}
27658#else
27659# define session_close_all(X,Y)
27660#endif
27661
27662/*
27663** Implementation of the xFilter function for an open session.  Omit
27664** any tables named by ".session filter" but let all other table through.
27665*/
27666#if defined(SQLITE_ENABLE_SESSION)
27667static int session_filter(void *pCtx, const char *zTab){
27668  OpenSession *pSession = (OpenSession*)pCtx;
27669  int i;
27670  for(i=0; i<pSession->nFilter; i++){
27671    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
27672  }
27673  return 1;
27674}
27675#endif
27676
27677/*
27678** Return the size of the named file in bytes.  Or return a negative
27679** number if the file does not exist.
27680*/
27681static sqlite3_int64 fileSize(const char *zFile){
27682#if defined(_WIN32) || defined(WIN32)
27683  struct _stat64 x;
27684  if( _stat64(zFile, &x)!=0 ) return -1;
27685  return (sqlite3_int64)x.st_size;
27686#else
27687  struct stat x;
27688  if( stat(zFile, &x)!=0 ) return -1;
27689  return (sqlite3_int64)x.st_size;
27690#endif
27691}
27692
27693/*
27694** Return true if zFile is an SQLite database.
27695**
27696** Algorithm:
27697**    * If the file does not exist -> return false
27698**    * If the size of the file is not a multiple of 512 -> return false
27699**    * If sqlite3_open() fails -> return false
27700**    * if sqlite3_prepare() or sqlite3_step() fails -> return false
27701**    * Otherwise -> return true
27702*/
27703static int isDatabaseFile(const char *zFile, int openFlags){
27704  sqlite3 *db = 0;
27705  sqlite3_stmt *pStmt = 0;
27706  int rc;
27707  sqlite3_int64 sz = fileSize(zFile);
27708  if( sz<512 || (sz%512)!=0 ) return 0;
27709  if( sqlite3_open_v2(zFile, &db, openFlags, 0)==SQLITE_OK
27710   && sqlite3_prepare_v2(db,"SELECT count(*) FROM sqlite_schema",-1,&pStmt,0)
27711           ==SQLITE_OK
27712   && sqlite3_step(pStmt)==SQLITE_ROW
27713  ){
27714    rc = 1;
27715  }else{
27716    rc = 0;
27717  }
27718  sqlite3_finalize(pStmt);
27719  sqlite3_close(db);
27720  return rc;
27721}
27722
27723/*
27724** Try to deduce the type of file for zName based on its content.  Return
27725** one of the SHELL_OPEN_* constants.
27726**
27727** If the file does not exist or is empty but its name looks like a ZIP
27728** archive and the dfltZip flag is true, then assume it is a ZIP archive.
27729** Otherwise, assume an ordinary database regardless of the filename if
27730** the type cannot be determined from content.
27731*/
27732int deduceDatabaseType(const char *zName, int dfltZip, int openFlags){
27733  FILE *f;
27734  size_t n;
27735  int rc = SHELL_OPEN_UNSPEC;
27736  char zBuf[100];
27737  if( access(zName,0)!=0 ) goto database_type_by_name;
27738  if( isDatabaseFile(zName, openFlags) ){
27739    rc = SHELL_OPEN_NORMAL;
27740  }
27741  if( rc==SHELL_OPEN_NORMAL ) return SHELL_OPEN_NORMAL;
27742  f = sqlite3_fopen(zName, "rb");
27743  if( f==0 ) goto database_type_by_name;
27744  n = fread(zBuf, 16, 1, f);
27745  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
27746    fclose(f);
27747    return SHELL_OPEN_NORMAL;
27748  }
27749  fseek(f, -25, SEEK_END);
27750  n = fread(zBuf, 25, 1, f);
27751  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
27752    rc = SHELL_OPEN_APPENDVFS;
27753  }else{
27754    fseek(f, -22, SEEK_END);
27755    n = fread(zBuf, 22, 1, f);
27756    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
27757       && zBuf[3]==0x06 ){
27758      rc = SHELL_OPEN_ZIPFILE;
27759    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
27760      rc = SHELL_OPEN_ZIPFILE;
27761    }
27762  }
27763  fclose(f);
27764  return rc;
27765
27766database_type_by_name:
27767  if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
27768    rc = SHELL_OPEN_ZIPFILE;
27769  }else{
27770    rc = SHELL_OPEN_NORMAL;
27771  }
27772  return rc;
27773}
27774
27775/*
27776** If the text in z[] is the name of a readable file and that file appears
27777** to contain SQL text and/or dot-commands, then return true.  If z[] is
27778** not a file, or if the file is unreadable, or if the file is a database
27779** or anything else that is not SQL text and dot-commands, then return false.
27780**
27781** If the bLeaveUninit flag is set, then be sure to leave SQLite in an
27782** uninitialized state.  This means invoking sqlite3_shutdown() after any
27783** SQLite API is used.
27784**
27785** Some amount of guesswork is involved in this decision.
27786*/
27787static int isScriptFile(const char *z, int bLeaveUninit){
27788  sqlite3_int64 sz = fileSize(z);
27789  if( sz<=0 ) return 0;
27790  if( (sz%512)==0 ){
27791    int rc;
27792    sqlite3_initialize();
27793    rc = isDatabaseFile(z, SQLITE_OPEN_READONLY);
27794    if( bLeaveUninit ){
27795      sqlite3_shutdown();
27796    }
27797    if( rc ) return 0;  /* Is a database */
27798  }
27799  if( sqlite3_strlike("%.sql",z,0)==0 ) return 1;
27800  if( sqlite3_strlike("%.txt",z,0)==0 ) return 1;
27801  return 0;
27802}
27803
27804#ifndef SQLITE_OMIT_DESERIALIZE
27805/*
27806** Reconstruct an in-memory database using the output from the "dbtotxt"
27807** program.  Read content from the file in p->aAuxDb[].zDbFilename.
27808** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
27809*/
27810static unsigned char *readHexDb(ShellState *p, int *pnData){
27811  unsigned char *a = 0;
27812  i64 nLine;
27813  int n = 0;                      /* Size of db per first line of hex dump */
27814  i64 sz = 0;                     /* n rounded up to nearest page boundary */
27815  int pgsz = 0;
27816  i64 iOffset = 0;
27817  int rc;
27818  FILE *in;
27819  const char *zDbFilename = p->pAuxDb->zDbFilename;
27820  unsigned int x[16];
27821  char zLine[1000];
27822  if( zDbFilename ){
27823    in = sqlite3_fopen(zDbFilename, "r");
27824    if( in==0 ){
27825      cli_printf(stderr,"cannot open \"%s\" for reading\n", zDbFilename);
27826      return 0;
27827    }
27828    nLine = 0;
27829  }else{
27830    in = p->in;
27831    nLine = p->lineno;
27832    if( in==0 ) in = stdin;
27833  }
27834  *pnData = 0;
27835  nLine++;
27836  if( sqlite3_fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
27837  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
27838  if( rc!=2 ) goto readHexDb_error;
27839  if( n<0 ) goto readHexDb_error;
27840  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
27841    cli_puts("invalid pagesize\n", stderr);
27842    goto readHexDb_error;
27843  }
27844  sz = ((i64)n+pgsz-1)&~(pgsz-1); /* Round up to nearest multiple of pgsz */
27845  a = sqlite3_malloc64( sz ? sz : 1 );
27846  shell_check_oom(a);
27847  memset(a, 0, sz);
27848  for(nLine++; sqlite3_fgets(zLine, sizeof(zLine), in)!=0; nLine++){
27849    int j = 0;                    /* Page number from "| page" line */
27850    int k = 0;                    /* Offset from "| page" line */
27851    if( nLine>=2000000000 ){
27852      cli_printf(stderr, "input too big\n");
27853      goto readHexDb_error;
27854    }
27855    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
27856    if( rc==2 ){
27857      iOffset = k;
27858      continue;
27859    }
27860    if( cli_strncmp(zLine, "| end ", 6)==0 ){
27861      break;
27862    }
27863    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
27864                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
27865                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
27866    if( rc==17 ){
27867      i64 iOff = iOffset+j;
27868      if( iOff+16<=sz && iOff>=0 ){
27869        int ii;
27870        for(ii=0; ii<16; ii++) a[iOff+ii] = x[ii]&0xff;
27871      }
27872    }
27873  }
27874  *pnData = sz;
27875  if( in!=p->in ){
27876    fclose(in);
27877  }else{
27878    p->lineno = nLine;
27879  }
27880  return a;
27881
27882readHexDb_error:
27883  if( in!=p->in ){
27884    fclose(in);
27885  }else{
27886    while( sqlite3_fgets(zLine, sizeof(zLine), p->in)!=0 ){
27887      nLine++;
27888      if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
27889    }
27890    p->lineno = nLine;
27891  }
27892  sqlite3_free(a);
27893  cli_printf(stderr,"Error on line %lld of --hexdb input\n", nLine);
27894  return 0;
27895}
27896#endif /* SQLITE_OMIT_DESERIALIZE */
27897
27898/*
27899** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
27900*/
27901static void shellUSleepFunc(
27902  sqlite3_context *context,
27903  int argcUnused,
27904  sqlite3_value **argv
27905){
27906  int sleep = sqlite3_value_int(argv[0]);
27907  (void)argcUnused;
27908  sqlite3_sleep(sleep/1000);
27909  sqlite3_result_int(context, sleep);
27910}
27911
27912/*
27913** SQL function:  shell_module_schema(X)
27914**
27915** Return a fake schema for the table-valued function or eponymous virtual
27916** table X.
27917*/
27918static void shellModuleSchema(
27919  sqlite3_context *pCtx,
27920  int nVal,
27921  sqlite3_value **apVal
27922){
27923  const char *zName;
27924  char *zFake;
27925  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
27926  FILE *pSavedLog = p->pLog;
27927  UNUSED_PARAMETER(nVal);
27928  zName = (const char*)sqlite3_value_text(apVal[0]);
27929
27930  /* Temporarily disable the ".log" when calling shellFakeSchema() because
27931  ** shellFakeSchema() might generate failures for some ephemeral virtual
27932  ** tables due to missing arguments.  Example: fts4aux.
27933  ** https://sqlite.org/forum/forumpost/42fe6520b803be51 */
27934  p->pLog = 0;
27935  zFake = zName? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
27936  p->pLog = pSavedLog;
27937
27938  if( zFake ){
27939    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
27940                        -1, sqlite3_free);
27941    sqlite3_free(zFake);
27942  }
27943}
27944
27945/* Flags for open_db().
27946**
27947** The default behavior of open_db() is to exit(1) if the database fails to
27948** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
27949** but still returns without calling exit.
27950**
27951** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
27952** ZIP archive if the file does not exist or is empty and its name matches
27953** the *.zip pattern.
27954*/
27955#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
27956#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
27957
27958/*
27959** Make sure the database is open.  If it is not, then open it.  If
27960** the database fails to open, print an error message and exit.
27961*/
27962static void open_db(ShellState *p, int openFlags){
27963  if( p->db==0 ){
27964    const char *zDbFilename = p->pAuxDb->zDbFilename;
27965    if( p->openMode==SHELL_OPEN_UNSPEC ){
27966      if( zDbFilename==0 || zDbFilename[0]==0 ){
27967        p->openMode = SHELL_OPEN_NORMAL;
27968      }else{
27969        p->openMode = (u8)deduceDatabaseType(zDbFilename,
27970                             (openFlags & OPEN_DB_ZIPFILE)!=0, p->openFlags);
27971      }
27972    }
27973    if( (p->openFlags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE))==0 ){
27974      if( p->openFlags==0 ) p->openFlags = SQLITE_OPEN_CREATE;
27975      p->openFlags |= SQLITE_OPEN_READWRITE;
27976    }
27977    switch( p->openMode ){
27978      case SHELL_OPEN_APPENDVFS: {
27979        sqlite3_open_v2(zDbFilename, &p->db, p->openFlags, "apndvfs");
27980        break;
27981      }
27982      case SHELL_OPEN_HEXDB:
27983      case SHELL_OPEN_DESERIALIZE: {
27984        sqlite3_open(0, &p->db);
27985        break;
27986      }
27987      case SHELL_OPEN_ZIPFILE: {
27988        sqlite3_open(":memory:", &p->db);
27989        break;
27990      }
27991      case SHELL_OPEN_UNSPEC:
27992      case SHELL_OPEN_NORMAL: {
27993        sqlite3_open_v2(zDbFilename, &p->db, p->openFlags, 0);
27994        break;
27995      }
27996    }
27997    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
27998      cli_printf(stderr,"Error: unable to open database \"%s\": %s\n",
27999            zDbFilename, sqlite3_errmsg(p->db));
28000      if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
28001        cli_exit(1);
28002      }
28003      sqlite3_close(p->db);
28004      sqlite3_open(":memory:", &p->db);
28005      if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
28006        cli_puts("Also: unable to open substitute in-memory database.\n",
28007                      stderr);
28008        cli_exit(1);
28009      }else{
28010        cli_printf(stderr,
28011              "Notice: using substitute in-memory database instead of \"%s\"\n",
28012              zDbFilename);
28013      }
28014    }
28015    globalDb = p->db;
28016    sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
28017
28018    /* Reflect the use or absence of --unsafe-testing invocation. */
28019    {
28020      int testmode_on = ShellHasFlag(p,SHFLG_TestingMode);
28021      sqlite3_db_config(p->db, SQLITE_DBCONFIG_TRUSTED_SCHEMA, testmode_on,0);
28022      sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, !testmode_on,0);
28023    }
28024
28025#ifndef SQLITE_OMIT_LOAD_EXTENSION
28026    sqlite3_enable_load_extension(p->db, 1);
28027#endif
28028    sqlite3_sha_init(p->db, 0, 0);
28029    sqlite3_shathree_init(p->db, 0, 0);
28030    sqlite3_uint_init(p->db, 0, 0);
28031    sqlite3_stmtrand_init(p->db, 0, 0);
28032    sqlite3_decimal_init(p->db, 0, 0);
28033    sqlite3_base64_init(p->db, 0, 0);
28034    sqlite3_base85_init(p->db, 0, 0);
28035    sqlite3_regexp_init(p->db, 0, 0);
28036    sqlite3_ieee_init(p->db, 0, 0);
28037    sqlite3_series_init(p->db, 0, 0);
28038#ifndef SQLITE_SHELL_FIDDLE
28039    sqlite3_fileio_init(p->db, 0, 0);
28040    sqlite3_completion_init(p->db, 0, 0);
28041#endif
28042#ifdef SQLITE_HAVE_ZLIB
28043    if( !p->bSafeModePersist ){
28044      sqlite3_zipfile_init(p->db, 0, 0);
28045      sqlite3_sqlar_init(p->db, 0, 0);
28046    }
28047#endif
28048#ifdef SQLITE_SHELL_EXTFUNCS
28049    /* Create a preprocessing mechanism for extensions to make
28050     * their own provisions for being built into the shell.
28051     * This is a short-span macro. See further below for usage.
28052     */
28053#define SHELL_SUB_MACRO(base, variant) base ## _ ## variant
28054#define SHELL_SUBMACRO(base, variant) SHELL_SUB_MACRO(base, variant)
28055    /* Let custom-included extensions get their ..._init() called.
28056     * The WHATEVER_INIT( db, pzErrorMsg, pApi ) macro should cause
28057     * the extension's sqlite3_*_init( db, pzErrorMsg, pApi )
28058     * initialization routine to be called.
28059     */
28060    {
28061      int irc = SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, INIT)(p->db);
28062    /* Let custom-included extensions expose their functionality.
28063     * The WHATEVER_EXPOSE( db, pzErrorMsg ) macro should cause
28064     * the SQL functions, virtual tables, collating sequences or
28065     * VFS's implemented by the extension to be registered.
28066     */
28067      if( irc==SQLITE_OK
28068          || irc==SQLITE_OK_LOAD_PERMANENTLY ){
28069        SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, EXPOSE)(p->db, 0);
28070      }
28071#undef SHELL_SUB_MACRO
28072#undef SHELL_SUBMACRO
28073    }
28074#endif
28075
28076    sqlite3_create_function(p->db, "strtod", 1, SQLITE_UTF8, 0,
28077                            shellStrtod, 0, 0);
28078    sqlite3_create_function(p->db, "dtostr", 1, SQLITE_UTF8, 0,
28079                            shellDtostr, 0, 0);
28080    sqlite3_create_function(p->db, "dtostr", 2, SQLITE_UTF8, 0,
28081                            shellDtostr, 0, 0);
28082    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
28083                            shellAddSchemaName, 0, 0);
28084    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, p,
28085                            shellModuleSchema, 0, 0);
28086    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
28087                            shellPutsFunc, 0, 0);
28088    sqlite3_create_function(p->db, "shell_format_schema", 2, SQLITE_UTF8, p,
28089                            shellFormatSchema, 0, 0);
28090    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
28091                            shellUSleepFunc, 0, 0);
28092#ifndef SQLITE_NOHAVE_SYSTEM
28093    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
28094                            editFunc, 0, 0);
28095    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
28096                            editFunc, 0, 0);
28097#endif
28098
28099    if( p->openMode==SHELL_OPEN_ZIPFILE ){
28100      char *zSql = sqlite3_mprintf(
28101         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
28102      shell_check_oom(zSql);
28103      sqlite3_exec(p->db, zSql, 0, 0, 0);
28104      sqlite3_free(zSql);
28105    }
28106#ifndef SQLITE_OMIT_DESERIALIZE
28107    else
28108    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
28109      int rc;
28110      int nData = 0;
28111      unsigned char *aData;
28112      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
28113        aData = (unsigned char*)readFile(zDbFilename, &nData);
28114      }else{
28115        aData = readHexDb(p, &nData);
28116      }
28117      if( aData==0 ){
28118        return;
28119      }
28120      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
28121                   SQLITE_DESERIALIZE_RESIZEABLE |
28122                   SQLITE_DESERIALIZE_FREEONCLOSE);
28123      if( rc ){
28124        cli_printf(stderr,"Error: sqlite3_deserialize() returns %d\n", rc);
28125      }
28126      if( p->szMax>0 ){
28127        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
28128      }
28129    }
28130#endif
28131  }
28132  if( p->db!=0 ){
28133#ifndef SQLITE_OMIT_AUTHORIZATION
28134    if( p->bSafeModePersist ){
28135      sqlite3_set_authorizer(p->db, safeModeAuth, p);
28136    }
28137#endif
28138    sqlite3_db_config(
28139        p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->mode.scanstatsOn, (int*)0
28140    );
28141  }
28142}
28143
28144/*
28145** Attempt to close the database connection.  Report errors.
28146*/
28147void close_db(sqlite3 *db){
28148  int rc = sqlite3_close(db);
28149  if( rc ){
28150    cli_printf(stderr,
28151        "Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
28152  }
28153}
28154
28155#if (HAVE_READLINE || HAVE_EDITLINE) \
28156  && !defined(SQLITE_OMIT_READLINE_COMPLETION)
28157/*
28158** Readline completion callbacks
28159*/
28160static char *readline_completion_generator(const char *text, int state){
28161  static sqlite3_stmt *pStmt = 0;
28162  char *zRet;
28163  if( state==0 ){
28164    char *zSql;
28165    sqlite3_finalize(pStmt);
28166    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
28167                           "  FROM completion(%Q) ORDER BY 1", text);
28168    shell_check_oom(zSql);
28169    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
28170    sqlite3_free(zSql);
28171  }
28172  if( sqlite3_step(pStmt)==SQLITE_ROW ){
28173    const char *z = (const char*)sqlite3_column_text(pStmt,0);
28174    zRet = z ? strdup(z) : 0;
28175  }else{
28176    sqlite3_finalize(pStmt);
28177    pStmt = 0;
28178    zRet = 0;
28179  }
28180  return zRet;
28181}
28182static char **readline_completion(const char *zText, int iStart, int iEnd){
28183  (void)iStart;
28184  (void)iEnd;
28185  rl_attempted_completion_over = 1;
28186  return rl_completion_matches(zText, readline_completion_generator);
28187}
28188
28189#elif HAVE_LINENOISE
28190/*
28191** Linenoise completion callback. Note that the 3rd argument is from
28192** the "msteveb" version of linenoise, not the "antirez" version.
28193*/
28194static void linenoise_completion(
28195  const char *zLine,
28196  linenoiseCompletions *lc
28197#if HAVE_LINENOISE==2
28198  ,void *pUserData
28199#endif
28200){
28201  i64 nLine = strlen(zLine);
28202  i64 i, iStart;
28203  sqlite3_stmt *pStmt = 0;
28204  char *zSql;
28205  char zBuf[1000];
28206
28207#if HAVE_LINENOISE==2
28208  UNUSED_PARAMETER(pUserData);
28209#endif
28210  if( nLine>(i64)sizeof(zBuf)-30 ) return;
28211  if( zLine[0]=='.' || zLine[0]=='#') return;
28212  for(i=nLine-1; i>=0 && (IsAlnum(zLine[i]) || zLine[i]=='_'); i--){}
28213  if( i==nLine-1 ) return;
28214  iStart = i+1;
28215  memcpy(zBuf, zLine, iStart);
28216  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
28217                         "  FROM completion(%Q,%Q) ORDER BY 1",
28218                         &zLine[iStart], zLine);
28219  shell_check_oom(zSql);
28220  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
28221  sqlite3_free(zSql);
28222  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
28223  while( sqlite3_step(pStmt)==SQLITE_ROW ){
28224    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
28225    int nCompletion = sqlite3_column_bytes(pStmt, 0);
28226    if( iStart+nCompletion < (i64)sizeof(zBuf)-1 && zCompletion ){
28227      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
28228      linenoiseAddCompletion(lc, zBuf);
28229    }
28230  }
28231  sqlite3_finalize(pStmt);
28232}
28233#endif
28234
28235/*
28236** Do C-language style dequoting.
28237**
28238**    \a    -> alarm
28239**    \b    -> backspace
28240**    \t    -> tab
28241**    \n    -> newline
28242**    \v    -> vertical tab
28243**    \f    -> form feed
28244**    \r    -> carriage return
28245**    \s    -> space
28246**    \"    -> "
28247**    \'    -> '
28248**    \\    -> backslash
28249**    \NNN  -> ascii character NNN in octal
28250**    \xHH  -> ascii character HH in hexadecimal
28251*/
28252static void resolve_backslashes(char *z){
28253  int i, j;
28254  char c;
28255  while( *z && *z!='\\' ) z++;
28256  for(i=j=0; (c = z[i])!=0; i++, j++){
28257    if( c=='\\' && z[i+1]!=0 ){
28258      c = z[++i];
28259      if( c=='a' ){
28260        c = '\a';
28261      }else if( c=='b' ){
28262        c = '\b';
28263      }else if( c=='t' ){
28264        c = '\t';
28265      }else if( c=='n' ){
28266        c = '\n';
28267      }else if( c=='v' ){
28268        c = '\v';
28269      }else if( c=='f' ){
28270        c = '\f';
28271      }else if( c=='r' ){
28272        c = '\r';
28273      }else if( c=='"' ){
28274        c = '"';
28275      }else if( c=='\'' ){
28276        c = '\'';
28277      }else if( c=='\\' ){
28278        c = '\\';
28279      }else if( c=='x' ){
28280        int nhd = 0, hdv;
28281        u8 hv = 0;
28282        while( nhd<2 && (c=z[i+1+nhd])!=0 && (hdv=hexDigitValue(c))>=0 ){
28283          hv = (u8)((hv<<4)|hdv);
28284          ++nhd;
28285        }
28286        i += nhd;
28287        c = (u8)hv;
28288      }else if( c>='0' && c<='7' ){
28289        c -= '0';
28290        if( z[i+1]>='0' && z[i+1]<='7' ){
28291          i++;
28292          c = (c<<3) + z[i] - '0';
28293          if( z[i+1]>='0' && z[i+1]<='7' ){
28294            i++;
28295            c = (c<<3) + z[i] - '0';
28296          }
28297        }
28298      }
28299    }
28300    z[j] = c;
28301  }
28302  if( j<i ) z[j] = 0;
28303}
28304
28305/*
28306** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
28307** for TRUE and FALSE.  Return the integer value if appropriate.
28308*/
28309static int booleanValue(const char *zArg){
28310  int i;
28311  if( zArg[0]=='0' && zArg[1]=='x' ){
28312    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
28313  }else{
28314    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
28315  }
28316  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
28317  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
28318    return 1;
28319  }
28320  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
28321    return 0;
28322  }
28323  cli_printf(stderr,
28324       "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
28325  return 0;
28326}
28327
28328/*
28329** Set or clear a shell flag according to a boolean value.
28330*/
28331static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
28332  if( booleanValue(zArg) ){
28333    ShellSetFlag(p, mFlag);
28334  }else{
28335    ShellClearFlag(p, mFlag);
28336  }
28337}
28338
28339/*
28340** Close an output file, assuming it is not stderr or stdout
28341*/
28342static void output_file_close(FILE *f){
28343  if( f && f!=stdout && f!=stderr ) fclose(f);
28344}
28345
28346/*
28347** Try to open an output file.   The names "stdout" and "stderr" are
28348** recognized and do the right thing.  NULL is returned if the output
28349** filename is "off".
28350*/
28351static FILE *output_file_open(ShellState *p, const char *zFile){
28352  FILE *f;
28353  if( cli_strcmp(zFile,"stdout")==0 ){
28354    f = stdout;
28355  }else if( cli_strcmp(zFile, "stderr")==0 ){
28356    f = stderr;
28357  }else if( cli_strcmp(zFile, "off")==0 || p->bSafeMode ){
28358    f = 0;
28359  }else{
28360    f = sqlite3_fopen(zFile, "w");
28361    if( f==0 ){
28362      cli_printf(stderr,"Error: cannot open \"%s\"\n", zFile);
28363    }
28364  }
28365  return f;
28366}
28367
28368#ifndef SQLITE_OMIT_TRACE
28369/*
28370** A routine for handling output from sqlite3_trace().
28371*/
28372static int sql_trace_callback(
28373  unsigned mType,         /* The trace type */
28374  void *pArg,             /* The ShellState pointer */
28375  void *pP,               /* Usually a pointer to sqlite_stmt */
28376  void *pX                /* Auxiliary output */
28377){
28378  ShellState *p = (ShellState*)pArg;
28379  sqlite3_stmt *pStmt;
28380  const char *zSql;
28381  i64 nSql;
28382  if( p->traceOut==0 ) return 0;
28383  if( mType==SQLITE_TRACE_CLOSE ){
28384    sputz(p->traceOut, "-- closing database connection\n");
28385    return 0;
28386  }
28387  if( mType!=SQLITE_TRACE_ROW && pX!=0 && ((const char*)pX)[0]=='-' ){
28388    zSql = (const char*)pX;
28389  }else{
28390    pStmt = (sqlite3_stmt*)pP;
28391    switch( p->eTraceType ){
28392      case SHELL_TRACE_EXPANDED: {
28393        zSql = sqlite3_expanded_sql(pStmt);
28394        break;
28395      }
28396#ifdef SQLITE_ENABLE_NORMALIZE
28397      case SHELL_TRACE_NORMALIZED: {
28398        zSql = sqlite3_normalized_sql(pStmt);
28399        break;
28400      }
28401#endif
28402      default: {
28403        zSql = sqlite3_sql(pStmt);
28404        break;
28405      }
28406    }
28407  }
28408  if( zSql==0 ) return 0;
28409  nSql = strlen(zSql);
28410  if( nSql>1000000000 ) nSql = 1000000000;
28411  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
28412  switch( mType ){
28413    case SQLITE_TRACE_ROW:
28414    case SQLITE_TRACE_STMT: {
28415      cli_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
28416      break;
28417    }
28418    case SQLITE_TRACE_PROFILE: {
28419      sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
28420      cli_printf(p->traceOut,
28421                      "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
28422      break;
28423    }
28424  }
28425  return 0;
28426}
28427#endif
28428
28429/*
28430** A no-op routine that runs with the ".breakpoint" doc-command.  This is
28431** a useful spot to set a debugger breakpoint.
28432**
28433** This routine does not do anything practical.  The code are there simply
28434** to prevent the compiler from optimizing this routine out.
28435*/
28436static void test_breakpoint(void){
28437  static unsigned int nCall = 0;
28438  if( (nCall++)==0xffffffff ) printf("Many .breakpoints have run\n");
28439}
28440
28441/*
28442** An object used to read a CSV and other files for import.
28443*/
28444typedef struct ImportCtx ImportCtx;
28445struct ImportCtx {
28446  const char *zFile;  /* Name of the input file */
28447  FILE *in;           /* Read the CSV text from this input stream */
28448  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
28449  char *zIn;          /* Input text */
28450  char *z;            /* Accumulated text for a field */
28451  i64 nUsed;          /* Bytes of zIn[] used so far */
28452  i64 n;              /* Number of bytes in z */
28453  i64 nAlloc;         /* Space allocated for z[] */
28454  int nLine;          /* Current line number */
28455  int nRow;           /* Number of rows imported */
28456  int nErr;           /* Number of errors encountered */
28457  int bNotFirst;      /* True if one or more bytes already read */
28458  int cTerm;          /* Character that terminated the most recent field */
28459  int cColSep;        /* The column separator character.  (Usually ",") */
28460  int cRowSep;        /* The row separator character.  (Usually "\n") */
28461  int cQEscape;       /* Escape character with "...".  0 for none */
28462  int cUQEscape;      /* Escape character not with "...".  0 for none */
28463};
28464
28465/* Clean up resourced used by an ImportCtx */
28466static void import_cleanup(ImportCtx *p){
28467  if( p->in!=0 && p->xCloser!=0 ){
28468    p->xCloser(p->in);
28469    p->in = 0;
28470  }
28471  sqlite3_free(p->z);
28472  p->z = 0;
28473  if( p->zIn ){
28474    sqlite3_free(p->zIn);
28475    p->zIn = 0;
28476  }
28477}
28478
28479/* Read a single character of the .import input text.  Return EOF
28480** at end-of-file.
28481*/
28482static int import_getc(ImportCtx *p){
28483  if( p->in ){
28484    return fgetc(p->in);
28485  }else if( p->zIn && p->zIn[p->nUsed]!=0 ){
28486    return p->zIn[p->nUsed++];
28487  }else{
28488    return EOF;
28489  }
28490}
28491
28492/* Append a single byte to the field value begin constructed
28493** in the p->z[] buffer
28494*/
28495static void import_append_char(ImportCtx *p, int c){
28496  if( p->n+1>=p->nAlloc ){
28497    p->nAlloc += p->nAlloc + 100;
28498    p->z = sqlite3_realloc64(p->z, p->nAlloc);
28499    shell_check_oom(p->z);
28500  }
28501  p->z[p->n++] = (char)c;
28502}
28503
28504/* Read a single field of CSV text.  Compatible with rfc4180 and extended
28505** with the option of having a separator other than ",".
28506**
28507**   +  Input comes from p->in.
28508**   +  Store results in p->z of length p->n.  Space to hold p->z comes
28509**      from sqlite3_malloc64().
28510**   +  Use p->cColSep as the column separator.  The default is ",".
28511**   +  Use p->cRowSep as the row separator.  The default is "\n".
28512**   +  Keep track of the line number in p->nLine.
28513**   +  Store the character that terminates the field in p->cTerm.  Store
28514**      EOF on end-of-file.
28515**   +  Report syntax errors on stderr
28516*/
28517static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
28518  int c;
28519  int cSep = (u8)p->cColSep;
28520  int rSep = (u8)p->cRowSep;
28521  p->n = 0;
28522  c = import_getc(p);
28523  if( c==EOF || seenInterrupt ){
28524    p->cTerm = EOF;
28525    return 0;
28526  }
28527  if( c=='"' ){
28528    int pc, ppc;
28529    int startLine = p->nLine;
28530    int cQuote = c;
28531    int cEsc = (u8)p->cQEscape;
28532    pc = ppc = 0;
28533    while( 1 ){
28534      c = import_getc(p);
28535      if( c==rSep ) p->nLine++;
28536      if( c==cEsc && cEsc!=0 ){
28537        c = import_getc(p);
28538        import_append_char(p, c);
28539        ppc = pc = 0;
28540        continue;
28541      }
28542      if( c==cQuote ){
28543        if( pc==cQuote ){
28544          pc = 0;
28545          continue;
28546        }
28547      }
28548      if( (c==cSep && pc==cQuote)
28549       || (c==rSep && pc==cQuote)
28550       || (c==rSep && pc=='\r' && ppc==cQuote)
28551       || (c==EOF && pc==cQuote)
28552      ){
28553        do{ p->n--; }while( p->z[p->n]!=cQuote );
28554        p->cTerm = c;
28555        break;
28556      }
28557      if( pc==cQuote && c!='\r' ){
28558        cli_printf(stderr,"%s:%d: unescaped %c character\n", 
28559                   p->zFile, p->nLine, cQuote);
28560      }
28561      if( c==EOF ){
28562        cli_printf(stderr,"%s:%d: unterminated %c-quoted field\n",
28563              p->zFile, startLine, cQuote);
28564        p->cTerm = c;
28565        break;
28566      }
28567      import_append_char(p, c);
28568      ppc = pc;
28569      pc = c;
28570    }
28571  }else{
28572    /* If this is the first field being parsed and it begins with the
28573    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
28574    int cEsc = p->cUQEscape;
28575    if( (c&0xff)==0xef && p->bNotFirst==0 ){
28576      import_append_char(p, c);
28577      c = import_getc(p);
28578      if( (c&0xff)==0xbb ){
28579        import_append_char(p, c);
28580        c = import_getc(p);
28581        if( (c&0xff)==0xbf ){
28582          p->bNotFirst = 1;
28583          p->n = 0;
28584          return csv_read_one_field(p);
28585        }
28586      }
28587    }
28588    while( c!=EOF && c!=cSep && c!=rSep ){
28589      if( c==cEsc && cEsc!=0 ) c = import_getc(p);
28590      import_append_char(p, c);
28591      c = import_getc(p);
28592    }
28593    if( c==rSep ){
28594      p->nLine++;
28595      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
28596    }
28597    p->cTerm = c;
28598  }
28599  if( p->z ) p->z[p->n] = 0;
28600  p->bNotFirst = 1;
28601  return p->z;
28602}
28603
28604/* Read a single field of ASCII delimited text.
28605**
28606**   +  Input comes from p->in.
28607**   +  Store results in p->z of length p->n.  Space to hold p->z comes
28608**      from sqlite3_malloc64().
28609**   +  Use p->cColSep as the column separator.  The default is "\x1F".
28610**   +  Use p->cRowSep as the row separator.  The default is "\x1E".
28611**   +  Keep track of the row number in p->nLine.
28612**   +  Store the character that terminates the field in p->cTerm.  Store
28613**      EOF on end-of-file.
28614**   +  Report syntax errors on stderr
28615*/
28616static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
28617  int c;
28618  int cSep = (u8)p->cColSep;
28619  int rSep = (u8)p->cRowSep;
28620  p->n = 0;
28621  c = import_getc(p);
28622  if( c==EOF || seenInterrupt ){
28623    p->cTerm = EOF;
28624    return 0;
28625  }
28626  while( c!=EOF && c!=cSep && c!=rSep ){
28627    import_append_char(p, c);
28628    c = import_getc(p);
28629  }
28630  if( c==rSep ){
28631    p->nLine++;
28632  }
28633  p->cTerm = c;
28634  if( p->z ) p->z[p->n] = 0;
28635  return p->z;
28636}
28637
28638/*
28639** Try to transfer data for table zTable.  If an error is seen while
28640** moving forward, try to go backwards.  The backwards movement won't
28641** work for WITHOUT ROWID tables.
28642*/
28643static void tryToCloneData(
28644  ShellState *p,
28645  sqlite3 *newDb,
28646  const char *zTable
28647){
28648  sqlite3_stmt *pQuery = 0;
28649  sqlite3_stmt *pInsert = 0;
28650  char *zQuery = 0;
28651  char *zInsert = 0;
28652  int rc;
28653  int i, j, n;
28654  int nTable = strlen30(zTable);
28655  int k = 0;
28656  int cnt = 0;
28657  const int spinRate = 10000;
28658
28659  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
28660  shell_check_oom(zQuery);
28661  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
28662  if( rc ){
28663    cli_printf(stderr,"Error %d: %s on [%s]\n",
28664          sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
28665    goto end_data_xfer;
28666  }
28667  n = sqlite3_column_count(pQuery);
28668  zInsert = sqlite3_malloc64(200 + nTable + n*3);
28669  shell_check_oom(zInsert);
28670  sqlite3_snprintf(200+nTable,zInsert,
28671                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
28672  i = strlen30(zInsert);
28673  for(j=1; j<n; j++){
28674    memcpy(zInsert+i, ",?", 2);
28675    i += 2;
28676  }
28677  memcpy(zInsert+i, ");", 3);
28678  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
28679  if( rc ){
28680    cli_printf(stderr,"Error %d: %s on [%s]\n",
28681          sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), zInsert);
28682    goto end_data_xfer;
28683  }
28684  for(k=0; k<2; k++){
28685    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
28686      for(i=0; i<n; i++){
28687        switch( sqlite3_column_type(pQuery, i) ){
28688          case SQLITE_NULL: {
28689            sqlite3_bind_null(pInsert, i+1);
28690            break;
28691          }
28692          case SQLITE_INTEGER: {
28693            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
28694            break;
28695          }
28696          case SQLITE_FLOAT: {
28697            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
28698            break;
28699          }
28700          case SQLITE_TEXT: {
28701            sqlite3_bind_text(pInsert, i+1,
28702                             (const char*)sqlite3_column_text(pQuery,i),
28703                             -1, SQLITE_STATIC);
28704            break;
28705          }
28706          case SQLITE_BLOB: {
28707            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
28708                                            sqlite3_column_bytes(pQuery,i),
28709                                            SQLITE_STATIC);
28710            break;
28711          }
28712        }
28713      } /* End for */
28714      rc = sqlite3_step(pInsert);
28715      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
28716        cli_printf(stderr,"Error %d: %s\n",
28717              sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb));
28718      }
28719      sqlite3_reset(pInsert);
28720      cnt++;
28721      if( (cnt%spinRate)==0 ){
28722        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
28723        fflush(stdout);
28724      }
28725    } /* End while */
28726    if( rc==SQLITE_DONE ) break;
28727    sqlite3_finalize(pQuery);
28728    sqlite3_free(zQuery);
28729    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
28730                             zTable);
28731    shell_check_oom(zQuery);
28732    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
28733    if( rc ){
28734      cli_printf(stderr,"Warning: cannot step \"%s\" backwards", zTable);
28735      break;
28736    }
28737  } /* End for(k=0...) */
28738
28739end_data_xfer:
28740  sqlite3_finalize(pQuery);
28741  sqlite3_finalize(pInsert);
28742  sqlite3_free(zQuery);
28743  sqlite3_free(zInsert);
28744}
28745
28746
28747/*
28748** Try to transfer all rows of the schema that match zWhere.  For
28749** each row, invoke xForEach() on the object defined by that row.
28750** If an error is encountered while moving forward through the
28751** sqlite_schema table, try again moving backwards.
28752*/
28753static void tryToCloneSchema(
28754  ShellState *p,
28755  sqlite3 *newDb,
28756  const char *zWhere,
28757  void (*xForEach)(ShellState*,sqlite3*,const char*)
28758){
28759  sqlite3_stmt *pQuery = 0;
28760  char *zQuery = 0;
28761  int rc;
28762  const unsigned char *zName;
28763  const unsigned char *zSql;
28764  char *zErrMsg = 0;
28765
28766  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
28767                           " WHERE %s ORDER BY rowid ASC", zWhere);
28768  shell_check_oom(zQuery);
28769  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
28770  if( rc ){
28771    cli_printf(stderr,
28772          "Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
28773          sqlite3_errmsg(p->db), zQuery);
28774    goto end_schema_xfer;
28775  }
28776  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
28777    zName = sqlite3_column_text(pQuery, 0);
28778    zSql = sqlite3_column_text(pQuery, 1);
28779    if( zName==0 || zSql==0 ) continue;
28780    if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
28781      cli_printf(stdout, "%s... ", zName); fflush(stdout);
28782      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
28783      if( zErrMsg ){
28784        cli_printf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
28785        sqlite3_free(zErrMsg);
28786        zErrMsg = 0;
28787      }
28788    }
28789    if( xForEach ){
28790      xForEach(p, newDb, (const char*)zName);
28791    }
28792    sputz(stdout, "done\n");
28793  }
28794  if( rc!=SQLITE_DONE ){
28795    sqlite3_finalize(pQuery);
28796    sqlite3_free(zQuery);
28797    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
28798                             " WHERE %s ORDER BY rowid DESC", zWhere);
28799    shell_check_oom(zQuery);
28800    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
28801    if( rc ){
28802      cli_printf(stderr,"Error: (%d) %s on [%s]\n",
28803            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
28804      goto end_schema_xfer;
28805    }
28806    while( sqlite3_step(pQuery)==SQLITE_ROW ){
28807      zName = sqlite3_column_text(pQuery, 0);
28808      zSql = sqlite3_column_text(pQuery, 1);
28809      if( zName==0 || zSql==0 ) continue;
28810      if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
28811      cli_printf(stdout, "%s... ", zName); fflush(stdout);
28812      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
28813      if( zErrMsg ){
28814        cli_printf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
28815        sqlite3_free(zErrMsg);
28816        zErrMsg = 0;
28817      }
28818      if( xForEach ){
28819        xForEach(p, newDb, (const char*)zName);
28820      }
28821      sputz(stdout, "done\n");
28822    }
28823  }
28824end_schema_xfer:
28825  sqlite3_finalize(pQuery);
28826  sqlite3_free(zQuery);
28827}
28828
28829/*
28830** Open a new database file named "zNewDb".  Try to recover as much information
28831** as possible out of the main database (which might be corrupt) and write it
28832** into zNewDb.
28833*/
28834static void tryToClone(ShellState *p, const char *zNewDb){
28835  int rc;
28836  sqlite3 *newDb = 0;
28837  if( access(zNewDb,0)==0 ){
28838    cli_printf(stderr,"File \"%s\" already exists.\n", zNewDb);
28839    return;
28840  }
28841  rc = sqlite3_open(zNewDb, &newDb);
28842  if( rc ){
28843    cli_printf(stderr,
28844        "Cannot create output database: %s\n", sqlite3_errmsg(newDb));
28845  }else{
28846    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
28847    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
28848    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
28849    tryToCloneSchema(p, newDb, "type!='table'", 0);
28850    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
28851    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
28852  }
28853  close_db(newDb);
28854}
28855
28856#ifndef SQLITE_SHELL_FIDDLE
28857/*
28858** Change the output stream (file or pipe or console) to something else.
28859*/
28860static void output_redir(ShellState *p, FILE *pfNew){
28861  if( p->out != stdout ){
28862    cli_puts("Output already redirected.\n", stderr);
28863  }else{
28864    p->out = pfNew;
28865    setCrlfMode(p);
28866    if( p->mode.eMode==MODE_Www ){
28867      cli_puts(
28868        "<!DOCTYPE html>\n"
28869        "<HTML><BODY><PRE>\n",
28870        p->out
28871      );
28872    }
28873  }
28874}
28875
28876/*
28877** Change the output file back to stdout.
28878**
28879** If the p->doXdgOpen flag is set, that means the output was being
28880** redirected to a temporary file named by p->zTempFile.  In that case,
28881** launch start/open/xdg-open on that temporary file.
28882*/
28883static void output_reset(ShellState *p){
28884  if( p->outfile[0]=='|' ){
28885#ifndef SQLITE_OMIT_POPEN
28886    pclose(p->out);
28887#endif
28888  }else{
28889    if( p->mode.eMode==MODE_Www ){
28890      cli_puts("</PRE></BODY></HTML>\n", p->out);
28891    }
28892    output_file_close(p->out);
28893#ifndef SQLITE_NOHAVE_SYSTEM
28894    if( p->doXdgOpen ){
28895      const char *zXdgOpenCmd =
28896#if defined(_WIN32)
28897      "start";
28898#elif defined(__APPLE__)
28899      "open";
28900#else
28901      "xdg-open";
28902#endif
28903      char *zCmd;
28904      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
28905      if( system(zCmd) ){
28906        cli_printf(stderr,"Failed: [%s]\n", zCmd);
28907      }else{
28908        /* Give the start/open/xdg-open command some time to get
28909        ** going before we continue, and potential delete the
28910        ** p->zTempFile data file out from under it */
28911        sqlite3_sleep(2000);
28912      }
28913      sqlite3_free(zCmd);
28914      modePop(p);
28915      p->doXdgOpen = 0;
28916    }
28917#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
28918  }
28919  p->outfile[0] = 0;
28920  p->out = stdout;
28921  setCrlfMode(p);
28922  if( cli_output_capture ){
28923    sqlite3_str_free(cli_output_capture);
28924    cli_output_capture = 0;
28925  }
28926}
28927#else
28928# define output_redir(SS,pfO)
28929# define output_reset(SS)
28930#endif
28931
28932/*
28933** Run an SQL command and return the single integer result.
28934*/
28935static int db_int(sqlite3 *db, const char *zSql, ...){
28936  sqlite3_stmt *pStmt;
28937  int res = 0;
28938  char *z;
28939  va_list ap;
28940  va_start(ap, zSql);
28941  z = sqlite3_vmprintf(zSql, ap);
28942  va_end(ap);
28943  sqlite3_prepare_v2(db, z, -1, &pStmt, 0);
28944  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
28945    res = sqlite3_column_int(pStmt,0);
28946  }
28947  sqlite3_finalize(pStmt);
28948  sqlite3_free(z);
28949  return res;
28950}
28951
28952#if SQLITE_SHELL_HAVE_RECOVER
28953/*
28954** Convert a 2-byte or 4-byte big-endian integer into a native integer
28955*/
28956static unsigned int get2byteInt(unsigned char *a){
28957  return ((unsigned int)a[0]<<8) + (unsigned int)a[1];
28958}
28959static unsigned int get4byteInt(unsigned char *a){
28960  return ((unsigned int)a[0]<<24)
28961       + ((unsigned int)a[1]<<16)
28962       + ((unsigned int)a[2]<<8)
28963       + (unsigned int)a[3];
28964}
28965
28966/*
28967** Implementation of the ".dbinfo" command.
28968**
28969** Return 1 on error, 2 to exit, and 0 otherwise.
28970*/
28971static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
28972  static const struct { const char *zName; int ofst; } aField[] = {
28973     { "file change counter:",  24  },
28974     { "database page count:",  28  },
28975     { "freelist page count:",  36  },
28976     { "schema cookie:",        40  },
28977     { "schema format:",        44  },
28978     { "default cache size:",   48  },
28979     { "autovacuum top root:",  52  },
28980     { "incremental vacuum:",   64  },
28981     { "text encoding:",        56  },
28982     { "user version:",         60  },
28983     { "application id:",       68  },
28984     { "software version:",     96  },
28985  };
28986  static const struct { const char *zName; const char *zSql; } aQuery[] = {
28987     { "number of tables:",
28988       "SELECT count(*) FROM %s WHERE type='table'" },
28989     { "number of indexes:",
28990       "SELECT count(*) FROM %s WHERE type='index'" },
28991     { "number of triggers:",
28992       "SELECT count(*) FROM %s WHERE type='trigger'" },
28993     { "number of views:",
28994       "SELECT count(*) FROM %s WHERE type='view'" },
28995     { "schema size:",
28996       "SELECT total(length(sql)) FROM %s" },
28997  };
28998  int i, rc;
28999  unsigned iDataVersion;
29000  char *zSchemaTab;
29001  char *zDb = nArg>=2 ? azArg[1] : "main";
29002  sqlite3_stmt *pStmt = 0;
29003  unsigned char aHdr[100];
29004  open_db(p, 0);
29005  if( p->db==0 ) return 1;
29006  rc = sqlite3_prepare_v2(p->db,
29007             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
29008             -1, &pStmt, 0);
29009  if( rc ){
29010    cli_printf(stderr,"error: %s\n", sqlite3_errmsg(p->db));
29011    sqlite3_finalize(pStmt);
29012    return 1;
29013  }
29014  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
29015  if( sqlite3_step(pStmt)==SQLITE_ROW
29016   && sqlite3_column_bytes(pStmt,0)>100
29017  ){
29018    const u8 *pb = sqlite3_column_blob(pStmt,0);
29019    shell_check_oom(pb);
29020    memcpy(aHdr, pb, 100);
29021    sqlite3_finalize(pStmt);
29022  }else{
29023    cli_puts("unable to read database header\n", stderr);
29024    sqlite3_finalize(pStmt);
29025    return 1;
29026  }
29027  i = get2byteInt(aHdr+16);
29028  if( i==1 ) i = 65536;
29029  cli_printf(p->out, "%-20s %d\n", "database page size:", i);
29030  cli_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
29031  cli_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
29032  cli_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
29033  for(i=0; i<ArraySize(aField); i++){
29034    int ofst = aField[i].ofst;
29035    unsigned int val = get4byteInt(aHdr + ofst);
29036    cli_printf(p->out, "%-20s %u", aField[i].zName, val);
29037    switch( ofst ){
29038      case 56: {
29039        if( val==1 ) cli_puts(" (utf8)", p->out);
29040        if( val==2 ) cli_puts(" (utf16le)", p->out);
29041        if( val==3 ) cli_puts(" (utf16be)", p->out);
29042      }
29043    }
29044    cli_puts("\n", p->out);
29045  }
29046  if( zDb==0 ){
29047    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
29048  }else if( cli_strcmp(zDb,"temp")==0 ){
29049    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
29050  }else{
29051    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
29052  }
29053  for(i=0; i<ArraySize(aQuery); i++){
29054    int val = db_int(p->db, aQuery[i].zSql, zSchemaTab);
29055    cli_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
29056  }
29057  sqlite3_free(zSchemaTab);
29058  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
29059  cli_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
29060  return 0;
29061}
29062#endif /* SQLITE_SHELL_HAVE_RECOVER */
29063
29064/*
29065** Implementation of the ".dbtotxt" command.
29066**
29067** Return 1 on error, 2 to exit, and 0 otherwise.
29068*/
29069static int shell_dbtotxt_command(ShellState *p, int nArg, char **azArg){
29070  sqlite3_stmt *pStmt = 0;
29071  sqlite3_int64 nPage = 0;
29072  int pgSz = 0;
29073  const char *zTail;
29074  char *zName = 0;
29075  int rc, i, j;
29076  unsigned char bShow[256];   /* Characters ok to display */
29077
29078  UNUSED_PARAMETER(nArg);
29079  UNUSED_PARAMETER(azArg);
29080  memset(bShow, '.', sizeof(bShow));
29081  for(i=' '; i<='~'; i++){
29082    if( i!='{' && i!='}' && i!='"' && i!='\\' ) bShow[i] = (unsigned char)i;
29083  }
29084  rc = sqlite3_prepare_v2(p->db, "PRAGMA page_size", -1, &pStmt, 0);
29085  if( rc ) goto dbtotxt_error;
29086  rc = 0;
29087  if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error;
29088  pgSz = sqlite3_column_int(pStmt, 0);
29089  sqlite3_finalize(pStmt);
29090  pStmt = 0;
29091  if( pgSz<512 || pgSz>65536 || (pgSz&(pgSz-1))!=0 ) goto dbtotxt_error;
29092  rc = sqlite3_prepare_v2(p->db, "PRAGMA page_count", -1, &pStmt, 0);
29093  if( rc ) goto dbtotxt_error;
29094  rc = 0;
29095  if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error;
29096  nPage = sqlite3_column_int64(pStmt, 0);
29097  sqlite3_finalize(pStmt);
29098  pStmt = 0;
29099  if( nPage<1 ) goto dbtotxt_error;
29100  rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
29101  if( rc ) goto dbtotxt_error;
29102  if( sqlite3_step(pStmt)!=SQLITE_ROW ){
29103    zTail = "unk.db";
29104  }else{
29105    const char *zFilename = (const char*)sqlite3_column_text(pStmt, 2);
29106    if( zFilename==0 || zFilename[0]==0 ) zFilename = "unk.db";
29107    zTail = strrchr(zFilename, '/');
29108#if defined(_WIN32)
29109    if( zTail==0 ) zTail = strrchr(zFilename, '\\');
29110#endif
29111    if( zTail==0 ){
29112      zTail = zFilename;
29113    }else if( zTail[1]!=0 ){
29114      zTail++;
29115    }
29116  }
29117  zName = strdup(zTail);
29118  shell_check_oom(zName);
29119  cli_printf(p->out, "| size %lld pagesize %d filename %s\n",
29120                  nPage*pgSz, pgSz, zName);
29121  sqlite3_finalize(pStmt);
29122  pStmt = 0;
29123  rc = sqlite3_prepare_v2(p->db,
29124           "SELECT pgno, data FROM sqlite_dbpage ORDER BY pgno", -1, &pStmt, 0);
29125  if( rc ) goto dbtotxt_error;
29126  while( sqlite3_step(pStmt)==SQLITE_ROW ){
29127    sqlite3_int64 pgno = sqlite3_column_int64(pStmt, 0);
29128    const u8 *aData = sqlite3_column_blob(pStmt, 1);
29129    int seenPageLabel = 0;
29130    for(i=0; i<pgSz; i+=16){
29131      const u8 *aLine = aData+i;
29132      for(j=0; j<16 && aLine[j]==0; j++){}
29133      if( j==16 ) continue;
29134      if( !seenPageLabel ){
29135        cli_printf(p->out, "| page %lld offset %lld\n",pgno,(pgno-1)*pgSz);
29136        seenPageLabel = 1;
29137      }
29138      cli_printf(p->out, "|  %5d:", i);
29139      for(j=0; j<16; j++) cli_printf(p->out, " %02x", aLine[j]);
29140      cli_printf(p->out, "   ");
29141      for(j=0; j<16; j++){
29142        unsigned char c = (unsigned char)aLine[j];
29143        cli_printf(p->out, "%c", bShow[c]);
29144      }
29145      cli_printf(p->out, "\n");
29146    }
29147  }
29148  sqlite3_finalize(pStmt);
29149  cli_printf(p->out, "| end %s\n", zName);
29150  free(zName);
29151  return 0;
29152
29153dbtotxt_error:
29154  if( rc ){
29155    cli_printf(stderr, "ERROR: %s\n", sqlite3_errmsg(p->db));
29156  }
29157  sqlite3_finalize(pStmt);
29158  free(zName);
29159  return 1;
29160}
29161
29162/*
29163** Print the given string as an error message.
29164*/
29165static void shellEmitError(const char *zErr){
29166  cli_printf(stderr,"Error: %s\n", zErr);
29167}
29168/*
29169** Print the current sqlite3_errmsg() value to stderr and return 1.
29170*/
29171static int shellDatabaseError(sqlite3 *db){
29172  shellEmitError(sqlite3_errmsg(db));
29173  return 1;
29174}
29175
29176/*
29177** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
29178** if they match and FALSE (0) if they do not match.
29179**
29180** Globbing rules:
29181**
29182**      '*'       Matches any sequence of zero or more characters.
29183**
29184**      '?'       Matches exactly one character.
29185**
29186**     [...]      Matches one character from the enclosed list of
29187**                characters.
29188**
29189**     [^...]     Matches one character not in the enclosed list.
29190**
29191**      '#'       Matches any sequence of one or more digits with an
29192**                optional + or - sign in front
29193**
29194**      ' '       Any span of whitespace matches any other span of
29195**                whitespace.
29196**
29197** Extra whitespace at the end of z[] is ignored.
29198*/
29199static int testcase_glob(const char *zGlob, const char *z){
29200  int c, c2;
29201  int invert;
29202  int seen;
29203
29204  while( (c = (*(zGlob++)))!=0 ){
29205    if( IsSpace(c) ){
29206      if( !IsSpace(*z) ) return 0;
29207      while( IsSpace(*zGlob) ) zGlob++;
29208      while( IsSpace(*z) ) z++;
29209    }else if( c=='*' ){
29210      while( (c=(*(zGlob++))) == '*' || c=='?' ){
29211        if( c=='?' && (*(z++))==0 ) return 0;
29212      }
29213      if( c==0 ){
29214        return 1;
29215      }else if( c=='[' ){
29216        while( *z && testcase_glob(zGlob-1,z)==0 ){
29217          z++;
29218        }
29219        return (*z)!=0;
29220      }
29221      while( (c2 = (*(z++)))!=0 ){
29222        while( c2!=c ){
29223          c2 = *(z++);
29224          if( c2==0 ) return 0;
29225        }
29226        if( testcase_glob(zGlob,z) ) return 1;
29227      }
29228      return 0;
29229    }else if( c=='?' ){
29230      if( (*(z++))==0 ) return 0;
29231    }else if( c=='[' ){
29232      int prior_c = 0;
29233      seen = 0;
29234      invert = 0;
29235      c = *(z++);
29236      if( c==0 ) return 0;
29237      c2 = *(zGlob++);
29238      if( c2=='^' ){
29239        invert = 1;
29240        c2 = *(zGlob++);
29241      }
29242      if( c2==']' ){
29243        if( c==']' ) seen = 1;
29244        c2 = *(zGlob++);
29245      }
29246      while( c2 && c2!=']' ){
29247        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
29248          c2 = *(zGlob++);
29249          if( c>=prior_c && c<=c2 ) seen = 1;
29250          prior_c = 0;
29251        }else{
29252          if( c==c2 ){
29253            seen = 1;
29254          }
29255          prior_c = c2;
29256        }
29257        c2 = *(zGlob++);
29258      }
29259      if( c2==0 || (seen ^ invert)==0 ) return 0;
29260    }else if( c=='#' ){
29261      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
29262      if( !IsDigit(z[0]) ) return 0;
29263      z++;
29264      while( IsDigit(z[0]) ){ z++; }
29265    }else{
29266      if( c!=(*(z++)) ) return 0;
29267    }
29268  }
29269  while( IsSpace(*z) ){ z++; }
29270  return *z==0;
29271}
29272
29273
29274/*
29275** Compare the string as a command-line option with either one or two
29276** initial "-" characters.
29277*/
29278static int optionMatch(const char *zStr, const char *zOpt){
29279  if( zStr[0]!='-' ) return 0;
29280  zStr++;
29281  if( zStr[0]=='-' ) zStr++;
29282  return cli_strcmp(zStr, zOpt)==0;
29283}
29284
29285/*
29286** The input zFN is guaranteed to start with "file:" and is thus a URI
29287** filename.  Extract the actual filename and return a pointer to that
29288** filename in spaced obtained from sqlite3_malloc().
29289**
29290** The caller is responsible for freeing space using sqlite3_free() when
29291** it has finished with the filename.
29292*/
29293static char *shellFilenameFromUri(const char *zFN){
29294  char *zOut;
29295  int i, j, d1, d2;
29296
29297  assert( cli_strncmp(zFN,"file:",5)==0 );
29298  zOut = sqlite3_mprintf("%s", zFN+5);
29299  shell_check_oom(zOut);
29300  for(i=j=0; zOut[i]!=0 && zOut[i]!='?'; i++){
29301    if( zOut[i]!='%' ){
29302      zOut[j++] = zOut[i];
29303      continue;
29304    }
29305    d1 = hexDigitValue(zOut[i+1]);
29306    if( d1<0 ){
29307      zOut[j] = 0;
29308      break;
29309    }
29310    d2 = hexDigitValue(zOut[i+2]);
29311    if( d2<0 ){
29312      zOut[j] = 0;
29313      break;
29314    }
29315    zOut[j++] = d1*16 + d2;
29316    i += 2;
29317  }
29318  zOut[j] = 0;
29319  return zOut;
29320}
29321
29322/*
29323** Delete a file.
29324*/
29325int shellDeleteFile(const char *zFilename){
29326  int rc;
29327#ifdef _WIN32
29328  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
29329  rc = _wunlink(z);
29330  sqlite3_free(z);
29331#else
29332  rc = unlink(zFilename);
29333#endif
29334  return rc;
29335}
29336
29337/*
29338** Try to delete the temporary file (if there is one) and free the
29339** memory used to hold the name of the temp file.
29340*/
29341static void clearTempFile(ShellState *p){
29342  if( p->zTempFile==0 ) return;
29343  if( p->doXdgOpen ) return;
29344  if( shellDeleteFile(p->zTempFile) ) return;
29345  sqlite3_free(p->zTempFile);
29346  p->zTempFile = 0;
29347}
29348
29349/* Forward reference */
29350static char *find_home_dir(int clearFlag);
29351
29352/*
29353** Create a new temp file name with the given suffix.
29354**
29355** Because the classic temp folders like /tmp are no longer
29356** accessible to web browsers, for security reasons, create the
29357** temp file in the user's home directory.
29358*/
29359static void newTempFile(ShellState *p, const char *zSuffix){
29360  char *zHome;          /* Home directory */
29361  int i;                /* Loop counter */
29362  sqlite3_uint64 r = 0; /* Integer with 64 bits of randomness */
29363  char zRand[32];       /* Text string with 160 bits of randomness */
29364#ifdef _WIN32
29365  const char cDirSep = '\\';
29366#else
29367  const char cDirSep = '/';
29368#endif
29369
29370  for(i=0; i<31; i++){
29371    if( (i%12)==0 ) sqlite3_randomness(sizeof(r),&r);
29372    zRand[i] = "0123456789abcdefghijklmnopqrstuvwxyz"[r%36];
29373    r /= 36;
29374  }
29375  zRand[i] = 0;
29376  clearTempFile(p);
29377  sqlite3_free(p->zTempFile);
29378  p->zTempFile = 0;
29379  zHome = find_home_dir(0);
29380  p->zTempFile = sqlite3_mprintf("%s%ctemp-%s.%s",
29381                                 zHome,cDirSep,zRand,zSuffix);
29382  shell_check_oom(p->zTempFile);
29383}
29384
29385/*
29386** The implementation of SQL scalar function fkey_collate_clause(), used
29387** by the ".lint fkey-indexes" command. This scalar function is always
29388** called with four arguments - the parent table name, the parent column name,
29389** the child table name and the child column name.
29390**
29391**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
29392**
29393** If either of the named tables or columns do not exist, this function
29394** returns an empty string. An empty string is also returned if both tables
29395** and columns exist but have the same default collation sequence. Or,
29396** if both exist but the default collation sequences are different, this
29397** function returns the string " COLLATE <parent-collation>", where
29398** <parent-collation> is the default collation sequence of the parent column.
29399*/
29400static void shellFkeyCollateClause(
29401  sqlite3_context *pCtx,
29402  int nVal,
29403  sqlite3_value **apVal
29404){
29405  sqlite3 *db = sqlite3_context_db_handle(pCtx);
29406  const char *zParent;
29407  const char *zParentCol;
29408  const char *zParentSeq;
29409  const char *zChild;
29410  const char *zChildCol;
29411  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
29412  int rc;
29413
29414  assert( nVal==4 );
29415  zParent = (const char*)sqlite3_value_text(apVal[0]);
29416  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
29417  zChild = (const char*)sqlite3_value_text(apVal[2]);
29418  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
29419
29420  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
29421  rc = sqlite3_table_column_metadata(
29422      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
29423  );
29424  if( rc==SQLITE_OK ){
29425    rc = sqlite3_table_column_metadata(
29426        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
29427    );
29428  }
29429
29430  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
29431    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
29432    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
29433    sqlite3_free(z);
29434  }
29435}
29436
29437
29438/*
29439** The implementation of dot-command ".lint fkey-indexes".
29440*/
29441static int lintFkeyIndexes(
29442  ShellState *pState,             /* Current shell tool state */
29443  char **azArg,                   /* Array of arguments passed to dot command */
29444  int nArg                        /* Number of entries in azArg[] */
29445){
29446  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
29447  int bVerbose = 0;               /* If -verbose is present */
29448  int bGroupByParent = 0;         /* If -groupbyparent is present */
29449  int i;                          /* To iterate through azArg[] */
29450  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
29451  int rc;                         /* Return code */
29452  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
29453  FILE *out = pState->out;        /* Send output here */
29454
29455  /*
29456  ** This SELECT statement returns one row for each foreign key constraint
29457  ** in the schema of the main database. The column values are:
29458  **
29459  ** 0. The text of an SQL statement similar to:
29460  **
29461  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
29462  **
29463  **    This SELECT is similar to the one that the foreign keys implementation
29464  **    needs to run internally on child tables. If there is an index that can
29465  **    be used to optimize this query, then it can also be used by the FK
29466  **    implementation to optimize DELETE or UPDATE statements on the parent
29467  **    table.
29468  **
29469  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
29470  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
29471  **    contains an index that can be used to optimize the query.
29472  **
29473  ** 2. Human readable text that describes the child table and columns. e.g.
29474  **
29475  **       "child_table(child_key1, child_key2)"
29476  **
29477  ** 3. Human readable text that describes the parent table and columns. e.g.
29478  **
29479  **       "parent_table(parent_key1, parent_key2)"
29480  **
29481  ** 4. A full CREATE INDEX statement for an index that could be used to
29482  **    optimize DELETE or UPDATE statements on the parent table. e.g.
29483  **
29484  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
29485  **
29486  ** 5. The name of the parent table.
29487  **
29488  ** These six values are used by the C logic below to generate the report.
29489  */
29490  const char *zSql =
29491  "SELECT "
29492    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
29493    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
29494    "  || fkey_collate_clause("
29495    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
29496    ", "
29497    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
29498    "  || group_concat('*=?', ' AND ') || ')'"
29499    ", "
29500    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
29501    ", "
29502    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
29503    ", "
29504    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
29505    "  || ' ON ' || quote(s.name) || '('"
29506    "  || group_concat(quote(f.[from]) ||"
29507    "        fkey_collate_clause("
29508    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
29509    "  || ');'"
29510    ", "
29511    "     f.[table] "
29512    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
29513    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
29514    "GROUP BY s.name, f.id "
29515    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
29516  ;
29517  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
29518
29519  for(i=2; i<nArg; i++){
29520    int n = strlen30(azArg[i]);
29521    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
29522      bVerbose = 1;
29523    }
29524    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
29525      bGroupByParent = 1;
29526      zIndent = "    ";
29527    }
29528    else{
29529      cli_printf(stderr,
29530           "Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]);
29531      return SQLITE_ERROR;
29532    }
29533  }
29534
29535  /* Register the fkey_collate_clause() SQL function */
29536  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
29537      0, shellFkeyCollateClause, 0, 0
29538  );
29539
29540
29541  if( rc==SQLITE_OK ){
29542    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
29543  }
29544  if( rc==SQLITE_OK ){
29545    sqlite3_bind_int(pSql, 1, bGroupByParent);
29546  }
29547
29548  if( rc==SQLITE_OK ){
29549    int rc2;
29550    char *zPrev = 0;
29551    while( SQLITE_ROW==sqlite3_step(pSql) ){
29552      int res = -1;
29553      sqlite3_stmt *pExplain = 0;
29554      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
29555      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
29556      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
29557      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
29558      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
29559      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
29560
29561      if( zEQP==0 ) continue;
29562      if( zGlob==0 ) continue;
29563      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
29564      if( rc!=SQLITE_OK ) break;
29565      if( SQLITE_ROW==sqlite3_step(pExplain) ){
29566        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
29567        res = zPlan!=0 && (  0==sqlite3_strglob(zGlob, zPlan)
29568                          || 0==sqlite3_strglob(zGlobIPK, zPlan));
29569      }
29570      rc = sqlite3_finalize(pExplain);
29571      if( rc!=SQLITE_OK ) break;
29572
29573      if( res<0 ){
29574        cli_puts("Error: internal error", stderr);
29575        break;
29576      }else{
29577        if( bGroupByParent
29578        && (bVerbose || res==0)
29579        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
29580        ){
29581          cli_printf(out, "-- Parent table %s\n", zParent);
29582          sqlite3_free(zPrev);
29583          zPrev = sqlite3_mprintf("%s", zParent);
29584        }
29585
29586        if( res==0 ){
29587          cli_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
29588        }else if( bVerbose ){
29589          cli_printf(out,
29590                "%s/* no extra indexes required for %s -> %s */\n",
29591                zIndent, zFrom, zTarget
29592          );
29593        }
29594      }
29595    }
29596    sqlite3_free(zPrev);
29597
29598    if( rc!=SQLITE_OK ){
29599      cli_printf(stderr,"%s\n", sqlite3_errmsg(db));
29600    }
29601
29602    rc2 = sqlite3_finalize(pSql);
29603    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
29604      rc = rc2;
29605      cli_printf(stderr,"%s\n", sqlite3_errmsg(db));
29606    }
29607  }else{
29608    cli_printf(stderr,"%s\n", sqlite3_errmsg(db));
29609  }
29610
29611  return rc;
29612}
29613
29614/*
29615** Implementation of ".lint" dot command.
29616*/
29617static int lintDotCommand(
29618  ShellState *pState,             /* Current shell tool state */
29619  char **azArg,                   /* Array of arguments passed to dot command */
29620  int nArg                        /* Number of entries in azArg[] */
29621){
29622  int n;
29623  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
29624  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
29625  return lintFkeyIndexes(pState, azArg, nArg);
29626
29627 usage:
29628  cli_printf(stderr,"Usage %s sub-command ?switches...?\n", azArg[0]);
29629  cli_printf(stderr, "Where sub-commands are:\n");
29630  cli_printf(stderr, "    fkey-indexes\n");
29631  return SQLITE_ERROR;
29632}
29633
29634static void shellPrepare(
29635  sqlite3 *db,
29636  int *pRc,
29637  const char *zSql,
29638  sqlite3_stmt **ppStmt
29639){
29640  *ppStmt = 0;
29641  if( *pRc==SQLITE_OK ){
29642    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
29643    if( rc!=SQLITE_OK ){
29644      cli_printf(stderr,
29645         "sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db));
29646      *pRc = rc;
29647    }
29648  }
29649}
29650
29651/*
29652** Create a prepared statement using printf-style arguments for the SQL.
29653*/
29654static void shellPreparePrintf(
29655  sqlite3 *db,
29656  int *pRc,
29657  sqlite3_stmt **ppStmt,
29658  const char *zFmt,
29659  ...
29660){
29661  *ppStmt = 0;
29662  if( *pRc==SQLITE_OK ){
29663    va_list ap;
29664    char *z;
29665    va_start(ap, zFmt);
29666    z = sqlite3_vmprintf(zFmt, ap);
29667    va_end(ap);
29668    if( z==0 ){
29669      *pRc = SQLITE_NOMEM;
29670    }else{
29671      shellPrepare(db, pRc, z, ppStmt);
29672      sqlite3_free(z);
29673    }
29674  }
29675}
29676
29677/* 
29678** Finalize the prepared statement created using shellPreparePrintf().
29679*/
29680static void shellFinalize(
29681  int *pRc,
29682  sqlite3_stmt *pStmt
29683){
29684  if( pStmt ){
29685    sqlite3 *db = sqlite3_db_handle(pStmt);
29686    int rc = sqlite3_finalize(pStmt);
29687    if( *pRc==SQLITE_OK ){
29688      if( rc!=SQLITE_OK ){
29689        cli_printf(stderr,"SQL error: %s\n", sqlite3_errmsg(db));
29690      }
29691      *pRc = rc;
29692    }
29693  }
29694}
29695
29696#if !defined SQLITE_OMIT_VIRTUALTABLE
29697/* Reset the prepared statement created using shellPreparePrintf().
29698**
29699** This routine is could be marked "static".  But it is not always used,
29700** depending on compile-time options.  By omitting the "static", we avoid
29701** nuisance compiler warnings about "defined but not used".
29702*/
29703void shellReset(
29704  int *pRc,
29705  sqlite3_stmt *pStmt
29706){
29707  int rc = sqlite3_reset(pStmt);
29708  if( *pRc==SQLITE_OK ){
29709    if( rc!=SQLITE_OK ){
29710      sqlite3 *db = sqlite3_db_handle(pStmt);
29711      cli_printf(stderr,"SQL error: %s\n", sqlite3_errmsg(db));
29712    }
29713    *pRc = rc;
29714  }
29715}
29716#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
29717
29718#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
29719/******************************************************************************
29720** The ".archive" or ".ar" command.
29721*/
29722/*
29723** Structure representing a single ".ar" command.
29724*/
29725typedef struct ArCommand ArCommand;
29726struct ArCommand {
29727  u8 eCmd;                        /* An AR_CMD_* value */
29728  u8 bVerbose;                    /* True if --verbose */
29729  u8 bZip;                        /* True if the archive is a ZIP */
29730  u8 bDryRun;                     /* True if --dry-run */
29731  u8 bAppend;                     /* True if --append */
29732  u8 bGlob;                       /* True if --glob */
29733  u8 fromCmdLine;                 /* Run from -A instead of .archive */
29734  int nArg;                       /* Number of command arguments */
29735  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
29736  const char *zFile;              /* --file argument, or NULL */
29737  const char *zDir;               /* --directory argument, or NULL */
29738  char **azArg;                   /* Array of command arguments */
29739  ShellState *p;                  /* Shell state */
29740  FILE *out;                      /* Output to this stream */
29741  sqlite3 *db;                    /* Database containing the archive */
29742};
29743
29744/*
29745** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
29746*/
29747static int arUsage(FILE *f){
29748  showHelp(f,"archive");
29749  return SQLITE_ERROR;
29750}
29751
29752/*
29753** Print an error message for the .ar command to stderr and return
29754** SQLITE_ERROR.
29755*/
29756static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
29757  va_list ap;
29758  char *z;
29759  va_start(ap, zFmt);
29760  z = sqlite3_vmprintf(zFmt, ap);
29761  va_end(ap);
29762  shellEmitError(z);
29763  if( pAr->fromCmdLine ){
29764    cli_puts("Use \"-A\" for more help\n", stderr);
29765  }else{
29766    cli_puts("Use \".archive --help\" for more help\n", stderr);
29767  }
29768  sqlite3_free(z);
29769  return SQLITE_ERROR;
29770}
29771
29772/*
29773** Values for ArCommand.eCmd.
29774*/
29775#define AR_CMD_CREATE       1
29776#define AR_CMD_UPDATE       2
29777#define AR_CMD_INSERT       3
29778#define AR_CMD_EXTRACT      4
29779#define AR_CMD_LIST         5
29780#define AR_CMD_HELP         6
29781#define AR_CMD_REMOVE       7
29782
29783/*
29784** Other (non-command) switches.
29785*/
29786#define AR_SWITCH_VERBOSE     8
29787#define AR_SWITCH_FILE        9
29788#define AR_SWITCH_DIRECTORY  10
29789#define AR_SWITCH_APPEND     11
29790#define AR_SWITCH_DRYRUN     12
29791#define AR_SWITCH_GLOB       13
29792
29793static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
29794  switch( eSwitch ){
29795    case AR_CMD_CREATE:
29796    case AR_CMD_EXTRACT:
29797    case AR_CMD_LIST:
29798    case AR_CMD_REMOVE:
29799    case AR_CMD_UPDATE:
29800    case AR_CMD_INSERT:
29801    case AR_CMD_HELP:
29802      if( pAr->eCmd ){
29803        return arErrorMsg(pAr, "multiple command options");
29804      }
29805      pAr->eCmd = eSwitch;
29806      break;
29807
29808    case AR_SWITCH_DRYRUN:
29809      pAr->bDryRun = 1;
29810      break;
29811    case AR_SWITCH_GLOB:
29812      pAr->bGlob = 1;
29813      break;
29814    case AR_SWITCH_VERBOSE:
29815      pAr->bVerbose = 1;
29816      break;
29817    case AR_SWITCH_APPEND:
29818      pAr->bAppend = 1;
29819      deliberate_fall_through; /* FALLTHRU */
29820    case AR_SWITCH_FILE:
29821      pAr->zFile = zArg;
29822      break;
29823    case AR_SWITCH_DIRECTORY:
29824      pAr->zDir = zArg;
29825      break;
29826  }
29827
29828  return SQLITE_OK;
29829}
29830
29831/*
29832** Parse the command line for an ".ar" command. The results are written into
29833** structure (*pAr). SQLITE_OK is returned if the command line is parsed
29834** successfully, otherwise an error message is written to stderr and
29835** SQLITE_ERROR returned.
29836*/
29837static int arParseCommand(
29838  char **azArg,                   /* Array of arguments passed to dot command */
29839  int nArg,                       /* Number of entries in azArg[] */
29840  ArCommand *pAr                  /* Populate this object */
29841){
29842  struct ArSwitch {
29843    const char *zLong;
29844    char cShort;
29845    u8 eSwitch;
29846    u8 bArg;
29847  } aSwitch[] = {
29848    { "create",    'c', AR_CMD_CREATE,       0 },
29849    { "extract",   'x', AR_CMD_EXTRACT,      0 },
29850    { "insert",    'i', AR_CMD_INSERT,       0 },
29851    { "list",      't', AR_CMD_LIST,         0 },
29852    { "remove",    'r', AR_CMD_REMOVE,       0 },
29853    { "update",    'u', AR_CMD_UPDATE,       0 },
29854    { "help",      'h', AR_CMD_HELP,         0 },
29855    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
29856    { "file",      'f', AR_SWITCH_FILE,      1 },
29857    { "append",    'a', AR_SWITCH_APPEND,    1 },
29858    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
29859    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
29860    { "glob",      'g', AR_SWITCH_GLOB,      0 },
29861  };
29862  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
29863  struct ArSwitch *pEnd = &aSwitch[nSwitch];
29864
29865  if( nArg<=1 ){
29866    cli_printf(stderr, "Wrong number of arguments.  Usage:\n");
29867    return arUsage(stderr);
29868  }else{
29869    char *z = azArg[1];
29870    if( z[0]!='-' ){
29871      /* Traditional style [tar] invocation */
29872      int i;
29873      int iArg = 2;
29874      for(i=0; z[i]; i++){
29875        const char *zArg = 0;
29876        struct ArSwitch *pOpt;
29877        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
29878          if( z[i]==pOpt->cShort ) break;
29879        }
29880        if( pOpt==pEnd ){
29881          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
29882        }
29883        if( pOpt->bArg ){
29884          if( iArg>=nArg ){
29885            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
29886          }
29887          zArg = azArg[iArg++];
29888        }
29889        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
29890      }
29891      pAr->nArg = nArg-iArg;
29892      if( pAr->nArg>0 ){
29893        pAr->azArg = &azArg[iArg];
29894      }
29895    }else{
29896      /* Non-traditional invocation */
29897      int iArg;
29898      for(iArg=1; iArg<nArg; iArg++){
29899        int n;
29900        z = azArg[iArg];
29901        if( z[0]!='-' ){
29902          /* All remaining command line words are command arguments. */
29903          pAr->azArg = &azArg[iArg];
29904          pAr->nArg = nArg-iArg;
29905          break;
29906        }
29907        n = strlen30(z);
29908
29909        if( z[1]!='-' ){
29910          int i;
29911          /* One or more short options */
29912          for(i=1; i<n; i++){
29913            const char *zArg = 0;
29914            struct ArSwitch *pOpt;
29915            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
29916              if( z[i]==pOpt->cShort ) break;
29917            }
29918            if( pOpt==pEnd ){
29919              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
29920            }
29921            if( pOpt->bArg ){
29922              if( i<(n-1) ){
29923                zArg = &z[i+1];
29924                i = n;
29925              }else{
29926                if( iArg>=(nArg-1) ){
29927                  return arErrorMsg(pAr, "option requires an argument: %c",
29928                                    z[i]);
29929                }
29930                zArg = azArg[++iArg];
29931              }
29932            }
29933            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
29934          }
29935        }else if( z[2]=='\0' ){
29936          /* A -- option, indicating that all remaining command line words
29937          ** are command arguments.  */
29938          pAr->azArg = &azArg[iArg+1];
29939          pAr->nArg = nArg-iArg-1;
29940          break;
29941        }else{
29942          /* A long option */
29943          const char *zArg = 0;             /* Argument for option, if any */
29944          struct ArSwitch *pMatch = 0;      /* Matching option */
29945          struct ArSwitch *pOpt;            /* Iterator */
29946          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
29947            const char *zLong = pOpt->zLong;
29948            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
29949              if( pMatch ){
29950                return arErrorMsg(pAr, "ambiguous option: %s",z);
29951              }else{
29952                pMatch = pOpt;
29953              }
29954            }
29955          }
29956
29957          if( pMatch==0 ){
29958            return arErrorMsg(pAr, "unrecognized option: %s", z);
29959          }
29960          if( pMatch->bArg ){
29961            if( iArg>=(nArg-1) ){
29962              return arErrorMsg(pAr, "option requires an argument: %s", z);
29963            }
29964            zArg = azArg[++iArg];
29965          }
29966          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
29967        }
29968      }
29969    }
29970  }
29971  if( pAr->eCmd==0 ){
29972    cli_printf(stderr, "Required argument missing.  Usage:\n");
29973    return arUsage(stderr);
29974  }
29975  return SQLITE_OK;
29976}
29977
29978/*
29979** This function assumes that all arguments within the ArCommand.azArg[]
29980** array refer to archive members, as for the --extract, --list or --remove
29981** commands. It checks that each of them are "present". If any specified
29982** file is not present in the archive, an error is printed to stderr and an
29983** error code returned. Otherwise, if all specified arguments are present
29984** in the archive, SQLITE_OK is returned. Here, "present" means either an
29985** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
29986** when pAr->bGlob is true.
29987**
29988** This function strips any trailing '/' characters from each argument.
29989** This is consistent with the way the [tar] command seems to work on
29990** Linux.
29991*/
29992static int arCheckEntries(ArCommand *pAr){
29993  int rc = SQLITE_OK;
29994  if( pAr->nArg ){
29995    int i, j;
29996    sqlite3_stmt *pTest = 0;
29997    const char *zSel = (pAr->bGlob)
29998      ? "SELECT name FROM %s WHERE glob($name,name)"
29999      : "SELECT name FROM %s WHERE name=$name";
30000
30001    shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
30002    j = sqlite3_bind_parameter_index(pTest, "$name");
30003    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
30004      char *z = pAr->azArg[i];
30005      int n = strlen30(z);
30006      int bOk = 0;
30007      while( n>0 && z[n-1]=='/' ) n--;
30008      z[n] = '\0';
30009      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
30010      if( SQLITE_ROW==sqlite3_step(pTest) ){
30011        bOk = 1;
30012      }
30013      shellReset(&rc, pTest);
30014      if( rc==SQLITE_OK && bOk==0 ){
30015        cli_printf(stderr,"not found in archive: %s\n", z);
30016        rc = SQLITE_ERROR;
30017      }
30018    }
30019    shellFinalize(&rc, pTest);
30020  }
30021  return rc;
30022}
30023
30024/*
30025** Format a WHERE clause that can be used against the "sqlar" table to
30026** identify all archive members that match the command arguments held
30027** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
30028** The caller is responsible for eventually calling sqlite3_free() on
30029** any non-NULL (*pzWhere) value. Here, "match" means strict equality
30030** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
30031*/
30032static void arWhereClause(
30033  int *pRc,
30034  ArCommand *pAr,
30035  char **pzWhere                  /* OUT: New WHERE clause */
30036){
30037  char *zWhere = 0;
30038  if( *pRc==SQLITE_OK ){
30039    if( pAr->nArg==0 ){
30040      zWhere = sqlite3_mprintf("1");
30041    }else{
30042      char *z1 = sqlite3_mprintf(pAr->bGlob ? "" : "name IN(");
30043      char *z2 = sqlite3_mprintf("");
30044      const char *zSep1 = "";
30045      const char *zSep2 = "";
30046
30047      int i;
30048      for(i=0; i<pAr->nArg && z1 && z2; i++){
30049        const char *z = pAr->azArg[i];
30050        int n = strlen30(z);
30051
30052        if( pAr->bGlob ){
30053          z1 = sqlite3_mprintf("%z%sname GLOB '%q'", z1, zSep2, z);
30054          z2 = sqlite3_mprintf(
30055              "%z%ssubstr(name,1,%d) GLOB '%q/'", z2, zSep2, n+1,z
30056          );
30057        }else{
30058          z1 = sqlite3_mprintf("%z%s'%q'", z1, zSep1, z);
30059          z2 = sqlite3_mprintf("%z%ssubstr(name,1,%d) = '%q/'",z2,zSep2,n+1,z);
30060        }
30061        zSep1 = ", ";
30062        zSep2 = " OR ";
30063      }
30064      if( z1==0 || z2==0 ){
30065        *pRc = SQLITE_NOMEM;
30066      }else{
30067        zWhere = sqlite3_mprintf("(%s%s OR (name GLOB '*/*' AND (%s))) ",
30068            z1, pAr->bGlob==0 ? ")" : "", z2
30069        );
30070      }
30071      sqlite3_free(z1);
30072      sqlite3_free(z2);
30073    }
30074  }
30075  *pzWhere = zWhere;
30076}
30077
30078/*
30079** Implementation of .ar "lisT" command.
30080*/
30081static int arListCommand(ArCommand *pAr){
30082  const char *zSql = "SELECT %s FROM %s WHERE %s";
30083  const char *azCols[] = {
30084    "name",
30085    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
30086  };
30087
30088  char *zWhere = 0;
30089  sqlite3_stmt *pSql = 0;
30090  int rc;
30091
30092  rc = arCheckEntries(pAr);
30093  arWhereClause(&rc, pAr, &zWhere);
30094
30095  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
30096                     pAr->zSrcTable, zWhere);
30097  if( pAr->bDryRun ){
30098    cli_printf(pAr->out, "%s\n", sqlite3_sql(pSql));
30099  }else{
30100    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
30101      if( pAr->bVerbose ){
30102        cli_printf(pAr->out, "%s % 10d  %s  %s\n",
30103              sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1),
30104              sqlite3_column_text(pSql, 2),sqlite3_column_text(pSql, 3));
30105      }else{
30106        cli_printf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0));
30107      }
30108    }
30109  }
30110  shellFinalize(&rc, pSql);
30111  sqlite3_free(zWhere);
30112  return rc;
30113}
30114
30115/*
30116** Implementation of .ar "Remove" command.
30117*/
30118static int arRemoveCommand(ArCommand *pAr){
30119  int rc = 0;
30120  char *zSql = 0;
30121  char *zWhere = 0;
30122
30123  if( pAr->nArg ){
30124    /* Verify that args actually exist within the archive before proceeding.
30125    ** And formulate a WHERE clause to match them.  */
30126    rc = arCheckEntries(pAr);
30127    arWhereClause(&rc, pAr, &zWhere);
30128  }
30129  if( rc==SQLITE_OK ){
30130    zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
30131                           pAr->zSrcTable, zWhere);
30132    if( pAr->bDryRun ){
30133      cli_printf(pAr->out, "%s\n", zSql);
30134    }else{
30135      char *zErr = 0;
30136      rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
30137      if( rc==SQLITE_OK ){
30138        rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
30139        if( rc!=SQLITE_OK ){
30140          sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
30141        }else{
30142          rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
30143        }
30144      }
30145      if( zErr ){
30146        cli_printf(stdout, "ERROR: %s\n", zErr); /* stdout? */
30147        sqlite3_free(zErr);
30148      }
30149    }
30150  }
30151  sqlite3_free(zWhere);
30152  sqlite3_free(zSql);
30153  return rc;
30154}
30155
30156/*
30157** Implementation of .ar "eXtract" command.
30158*/
30159static int arExtractCommand(ArCommand *pAr){
30160  const char *zSql1 =
30161    "WITH dest(dpath,dlen) AS (SELECT realpath($dir),length(realpath($dir)))\n"
30162    "SELECT ($dir || name),\n"
30163    "       CASE WHEN $dryrun THEN 0\n"
30164    "            ELSE writefile($dir||name, %s, mode, mtime) END\n"
30165    "  FROM dest CROSS JOIN %s\n"
30166    " WHERE (%s)\n"
30167    "   AND (data IS NULL OR $pass==0)\n"                /* Dirs both passes */
30168    "   AND dpath=substr(realpath($dir||name),1,dlen)\n" /* No escapes */
30169    "   AND name NOT GLOB '*..[/\\]*'\n";                /* No /../ in paths */
30170
30171  const char *azExtraArg[] = {
30172    "sqlar_uncompress(data, sz)",
30173    "data"
30174  };
30175
30176  sqlite3_stmt *pSql = 0;
30177  int rc = SQLITE_OK;
30178  char *zDir = 0;
30179  char *zWhere = 0;
30180  int i, j;
30181
30182  /* If arguments are specified, check that they actually exist within
30183  ** the archive before proceeding. And formulate a WHERE clause to
30184  ** match them.  */
30185  rc = arCheckEntries(pAr);
30186  arWhereClause(&rc, pAr, &zWhere);
30187
30188  if( rc==SQLITE_OK ){
30189    if( pAr->zDir ){
30190      zDir = sqlite3_mprintf("%s/", pAr->zDir);
30191    }else{
30192      zDir = sqlite3_mprintf("");
30193    }
30194    if( zDir==0 ) rc = SQLITE_NOMEM;
30195  }
30196
30197  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
30198      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
30199  );
30200
30201  if( rc==SQLITE_OK ){
30202    j = sqlite3_bind_parameter_index(pSql, "$dir");
30203    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
30204    j = sqlite3_bind_parameter_index(pSql, "$dryrun");
30205    sqlite3_bind_int(pSql, j, pAr->bDryRun);
30206
30207    /* Run the SELECT statement twice
30208    **   (0) writefile() all files and directories
30209    **   (1) writefile() for directory again
30210    ** The second pass is so that the timestamps for extracted directories
30211    ** will be reset to the value in the archive, since populating them
30212    ** in the first pass will have changed the timestamp. */
30213    for(i=0; i<2; i++){
30214      j = sqlite3_bind_parameter_index(pSql, "$pass");
30215      sqlite3_bind_int(pSql, j, i);
30216      if( pAr->bDryRun ){
30217        cli_printf(pAr->out, "%s\n", sqlite3_sql(pSql));
30218        if( pAr->bVerbose==0 ) break;
30219      }
30220      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
30221        if( i==0 && pAr->bVerbose ){
30222          cli_printf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0));
30223        }
30224      }
30225      if( pAr->bDryRun ) break;
30226      shellReset(&rc, pSql);
30227    }
30228    shellFinalize(&rc, pSql);
30229  }
30230
30231  sqlite3_free(zDir);
30232  sqlite3_free(zWhere);
30233  return rc;
30234}
30235
30236/*
30237** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
30238*/
30239static int arExecSql(ArCommand *pAr, const char *zSql){
30240  int rc;
30241  if( pAr->bDryRun ){
30242    cli_printf(pAr->out, "%s\n", zSql);
30243    rc = SQLITE_OK;
30244  }else{
30245    char *zErr = 0;
30246    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
30247    if( zErr ){
30248      cli_printf(stdout, "ERROR: %s\n", zErr);
30249      sqlite3_free(zErr);
30250    }
30251  }
30252  return rc;
30253}
30254
30255
30256/*
30257** Implementation of .ar "create", "insert", and "update" commands.
30258**
30259**     create    ->     Create a new SQL archive
30260**     insert    ->     Insert or reinsert all files listed
30261**     update    ->     Insert files that have changed or that were not
30262**                      previously in the archive
30263**
30264** Create the "sqlar" table in the database if it does not already exist.
30265** Then add each file in the azFile[] array to the archive. Directories
30266** are added recursively. If argument bVerbose is non-zero, a message is
30267** printed on stdout for each file archived.
30268**
30269** The create command is the same as update, except that it drops
30270** any existing "sqlar" table before beginning.  The "insert" command
30271** always overwrites every file named on the command-line, where as
30272** "update" only overwrites if the size or mtime or mode has changed.
30273*/
30274static int arCreateOrUpdateCommand(
30275  ArCommand *pAr,                 /* Command arguments and options */
30276  int bUpdate,                    /* true for a --create. */
30277  int bOnlyIfChanged              /* Only update if file has changed */
30278){
30279  const char *zCreate =
30280      "CREATE TABLE IF NOT EXISTS sqlar(\n"
30281      "  name TEXT PRIMARY KEY,  -- name of the file\n"
30282      "  mode INT,               -- access permissions\n"
30283      "  mtime INT,              -- last modification time\n"
30284      "  sz INT,                 -- original file size\n"
30285      "  data BLOB               -- compressed content\n"
30286      ")";
30287  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
30288  const char *zInsertFmt[2] = {
30289     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
30290     "  SELECT\n"
30291     "    %s,\n"
30292     "    mode,\n"
30293     "    mtime,\n"
30294     "    CASE substr(lsmode(mode),1,1)\n"
30295     "      WHEN '-' THEN length(data)\n"
30296     "      WHEN 'd' THEN 0\n"
30297     "      ELSE -1 END,\n"
30298     "    sqlar_compress(data)\n"
30299     "  FROM fsdir(%Q,%Q) AS disk\n"
30300     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
30301     ,
30302     "REPLACE INTO %s(name,mode,mtime,data)\n"
30303     "  SELECT\n"
30304     "    %s,\n"
30305     "    mode,\n"
30306     "    mtime,\n"
30307     "    data\n"
30308     "  FROM fsdir(%Q,%Q) AS disk\n"
30309     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
30310  };
30311  int i;                          /* For iterating through azFile[] */
30312  int rc;                         /* Return code */
30313  const char *zTab = 0;           /* SQL table into which to insert */
30314  char *zSql;
30315  char zTemp[50];
30316  char *zExists = 0;
30317
30318  arExecSql(pAr, "PRAGMA page_size=512");
30319  rc = arExecSql(pAr, "SAVEPOINT ar;");
30320  if( rc!=SQLITE_OK ) return rc;
30321  zTemp[0] = 0;
30322  if( pAr->bZip ){
30323    /* Initialize the zipfile virtual table, if necessary */
30324    if( pAr->zFile ){
30325      sqlite3_uint64 r;
30326      sqlite3_randomness(sizeof(r),&r);
30327      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
30328      zTab = zTemp;
30329      zSql = sqlite3_mprintf(
30330         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
30331         zTab, pAr->zFile
30332      );
30333      rc = arExecSql(pAr, zSql);
30334      sqlite3_free(zSql);
30335    }else{
30336      zTab = "zip";
30337    }
30338  }else{
30339    /* Initialize the table for an SQLAR */
30340    zTab = "sqlar";
30341    if( bUpdate==0 ){
30342      rc = arExecSql(pAr, zDrop);
30343      if( rc!=SQLITE_OK ) goto end_ar_transaction;
30344    }
30345    rc = arExecSql(pAr, zCreate);
30346  }
30347  if( bOnlyIfChanged ){
30348    zExists = sqlite3_mprintf(
30349      " AND NOT EXISTS("
30350          "SELECT 1 FROM %s AS mem"
30351          " WHERE mem.name=disk.name"
30352          " AND mem.mtime=disk.mtime"
30353          " AND mem.mode=disk.mode)", zTab);
30354  }else{
30355    zExists = sqlite3_mprintf("");
30356  }
30357  if( zExists==0 ) rc = SQLITE_NOMEM;
30358  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
30359    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
30360        pAr->bVerbose ? "shell_putsnl(name)" : "name",
30361        pAr->azArg[i], pAr->zDir, zExists);
30362    rc = arExecSql(pAr, zSql2);
30363    sqlite3_free(zSql2);
30364  }
30365end_ar_transaction:
30366  if( rc!=SQLITE_OK ){
30367    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
30368  }else{
30369    rc = arExecSql(pAr, "RELEASE ar;");
30370    if( pAr->bZip && pAr->zFile ){
30371      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
30372      arExecSql(pAr, zSql);
30373      sqlite3_free(zSql);
30374    }
30375  }
30376  sqlite3_free(zExists);
30377  return rc;
30378}
30379
30380/*
30381** Implementation of ".ar" dot command.
30382*/
30383static int arDotCommand(
30384  ShellState *pState,          /* Current shell tool state */
30385  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
30386  char **azArg,                /* Array of arguments passed to dot command */
30387  int nArg                     /* Number of entries in azArg[] */
30388){
30389  ArCommand cmd;
30390  int rc;
30391  memset(&cmd, 0, sizeof(cmd));
30392  cmd.fromCmdLine = fromCmdLine;
30393  rc = arParseCommand(azArg, nArg, &cmd);
30394  if( rc==SQLITE_OK ){
30395    int eDbType = SHELL_OPEN_UNSPEC;
30396    cmd.p = pState;
30397    cmd.out = pState->out;
30398    cmd.db = pState->db;
30399    if( cmd.zFile ){
30400      eDbType = deduceDatabaseType(cmd.zFile, 1, 0);
30401    }else{
30402      eDbType = pState->openMode;
30403    }
30404    if( eDbType==SHELL_OPEN_ZIPFILE ){
30405      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
30406        if( cmd.zFile==0 ){
30407          cmd.zSrcTable = sqlite3_mprintf("zip");
30408        }else{
30409          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
30410        }
30411      }
30412      cmd.bZip = 1;
30413    }else if( cmd.zFile ){
30414      int flags;
30415      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
30416      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
30417           || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
30418        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
30419      }else{
30420        flags = SQLITE_OPEN_READONLY;
30421      }
30422      cmd.db = 0;
30423      if( cmd.bDryRun ){
30424        cli_printf(cmd.out, "-- open database '%s'%s\n", cmd.zFile,
30425              eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
30426      }
30427      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
30428             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
30429      if( rc!=SQLITE_OK ){
30430        cli_printf(stderr, "cannot open file: %s (%s)\n",
30431                        cmd.zFile, sqlite3_errmsg(cmd.db));
30432        goto end_ar_command;
30433      }
30434      sqlite3_fileio_init(cmd.db, 0, 0);
30435      sqlite3_sqlar_init(cmd.db, 0, 0);
30436      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
30437                              shellPutsFunc, 0, 0);
30438
30439    }
30440    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
30441      if( cmd.eCmd!=AR_CMD_CREATE
30442       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
30443      ){
30444        cli_printf(stderr, "database does not contain an 'sqlar' table\n");
30445        rc = SQLITE_ERROR;
30446        goto end_ar_command;
30447      }
30448      cmd.zSrcTable = sqlite3_mprintf("sqlar");
30449    }
30450
30451    switch( cmd.eCmd ){
30452      case AR_CMD_CREATE:
30453        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
30454        break;
30455
30456      case AR_CMD_EXTRACT:
30457        rc = arExtractCommand(&cmd);
30458        break;
30459
30460      case AR_CMD_LIST:
30461        rc = arListCommand(&cmd);
30462        break;
30463
30464      case AR_CMD_HELP:
30465        arUsage(pState->out);
30466        break;
30467
30468      case AR_CMD_INSERT:
30469        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
30470        break;
30471
30472      case AR_CMD_REMOVE:
30473        rc = arRemoveCommand(&cmd);
30474        break;
30475
30476      default:
30477        assert( cmd.eCmd==AR_CMD_UPDATE );
30478        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
30479        break;
30480    }
30481  }
30482end_ar_command:
30483  if( cmd.db!=pState->db ){
30484    close_db(cmd.db);
30485  }
30486  sqlite3_free(cmd.zSrcTable);
30487
30488  return rc;
30489}
30490/* End of the ".archive" or ".ar" command logic
30491*******************************************************************************/
30492#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
30493
30494#if SQLITE_SHELL_HAVE_RECOVER
30495
30496/*
30497** This function is used as a callback by the recover extension. Simply
30498** print the supplied SQL statement to stdout.
30499*/
30500static int recoverSqlCb(void *pCtx, const char *zSql){
30501  ShellState *pState = (ShellState*)pCtx;
30502  cli_printf(pState->out, "%s;\n", zSql);
30503  return SQLITE_OK;
30504}
30505
30506/*
30507** This function is called to recover data from the database. A script
30508** to construct a new database containing all recovered data is output
30509** on stream pState->out.
30510*/
30511static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
30512  int rc = SQLITE_OK;
30513  const char *zRecoveryDb = "";   /* Name of "recovery" database.  Debug only */
30514  const char *zLAF = "lost_and_found";
30515  int bFreelist = 1;              /* 0 if --ignore-freelist is specified */
30516  int bRowids = 1;                /* 0 if --no-rowids */
30517  sqlite3_recover *p = 0;
30518  int i = 0;
30519
30520  for(i=1; i<nArg; i++){
30521    char *z = azArg[i];
30522    int n;
30523    if( z[0]=='-' && z[1]=='-' ) z++;
30524    n = strlen30(z);
30525    if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){
30526      bFreelist = 0;
30527    }else
30528    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
30529      /* This option determines the name of the ATTACH-ed database used
30530      ** internally by the recovery extension.  The default is "" which
30531      ** means to use a temporary database that is automatically deleted
30532      ** when closed.  This option is undocumented and might disappear at
30533      ** any moment. */
30534      i++;
30535      zRecoveryDb = azArg[i];
30536    }else
30537    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
30538      i++;
30539      zLAF = azArg[i];
30540    }else
30541    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
30542      bRowids = 0;
30543    }
30544    else{
30545      cli_printf(stderr,"unexpected option: %s\n", azArg[i]);
30546      showHelp(pState->out, azArg[0]);
30547      return 1;
30548    }
30549  }
30550
30551  p = sqlite3_recover_init_sql(
30552      pState->db, "main", recoverSqlCb, (void*)pState
30553  );
30554
30555  if( !pState->bSafeMode ){
30556    sqlite3_recover_config(p, 789, (void*)zRecoveryDb);  /* Debug use only */
30557  }
30558  sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
30559  sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
30560  sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
30561
30562  cli_printf(pState->out, ".dbconfig defensive off\n");
30563  sqlite3_recover_run(p);
30564  if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
30565    const char *zErr = sqlite3_recover_errmsg(p);
30566    int errCode = sqlite3_recover_errcode(p);
30567    cli_printf(stderr,"sql error: %s (%d)\n", zErr, errCode);
30568  }
30569  rc = sqlite3_recover_finish(p);
30570  return rc;
30571}
30572#endif /* SQLITE_SHELL_HAVE_RECOVER */
30573
30574/*
30575** Implementation of ".intck STEPS_PER_UNLOCK" command.
30576*/
30577static int intckDatabaseCmd(ShellState *pState, i64 nStepPerUnlock){
30578  sqlite3_intck *p = 0;
30579  int rc = SQLITE_OK;
30580
30581  rc = sqlite3_intck_open(pState->db, "main", &p);
30582  if( rc==SQLITE_OK ){
30583    i64 nStep = 0;
30584    i64 nError = 0;
30585    const char *zErr = 0;
30586    while( SQLITE_OK==sqlite3_intck_step(p) ){
30587      const char *zMsg = sqlite3_intck_message(p);
30588      if( zMsg ){
30589        cli_printf(pState->out, "%s\n", zMsg);
30590        nError++;
30591      }
30592      nStep++;
30593      if( nStepPerUnlock && (nStep % nStepPerUnlock)==0 ){
30594        sqlite3_intck_unlock(p);
30595      }
30596    }
30597    rc = sqlite3_intck_error(p, &zErr);
30598    if( zErr ){
30599      cli_printf(stderr,"%s\n", zErr);
30600    }
30601    sqlite3_intck_close(p);
30602
30603    cli_printf(pState->out, "%lld steps, %lld errors\n", nStep, nError);
30604  }
30605
30606  return rc;
30607}
30608
30609/*
30610 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
30611 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
30612 *   close db and set it to 0, and return the columns spec, to later
30613 *   be sqlite3_free()'ed by the caller.
30614 * The return is 0 when either:
30615 *   (a) The db was not initialized and zCol==0 (There are no columns.)
30616 *   (b) zCol!=0  (Column was added, db initialized as needed.)
30617 * The 3rd argument, pRenamed, references an out parameter. If the
30618 * pointer is non-zero, its referent will be set to a summary of renames
30619 * done if renaming was necessary, or set to 0 if none was done. The out
30620 * string (if any) must be sqlite3_free()'ed by the caller.
30621 */
30622#ifdef SHELL_DEBUG
30623#define rc_err_oom_die(rc) \
30624  if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
30625  else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
30626    cli_printf(stderr,"E:%d\n",rc), assert(0)
30627#else
30628static void rc_err_oom_die(int rc){
30629  if( rc==SQLITE_NOMEM ) shell_check_oom(0);
30630  assert(rc==SQLITE_OK||rc==SQLITE_DONE);
30631}
30632#endif
30633
30634#ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
30635static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);
30636#else  /* Otherwise, memory is faster/better for the transient DB. */
30637static const char *zCOL_DB = ":memory:";
30638#endif
30639
30640/* Define character (as C string) to separate generated column ordinal
30641 * from protected part of incoming column names. This defaults to "_"
30642 * so that incoming column identifiers that did not need not be quoted
30643 * remain usable without being quoted. It must be one character.
30644 */
30645#ifndef SHELL_AUTOCOLUMN_SEP
30646# define AUTOCOLUMN_SEP "_"
30647#else
30648# define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
30649#endif
30650
30651static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
30652  /* Queries and D{D,M}L used here */
30653  static const char * const zTabMake = "\
30654CREATE TABLE ColNames(\
30655 cpos INTEGER PRIMARY KEY,\
30656 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
30657CREATE VIEW RepeatedNames AS \
30658SELECT DISTINCT t.name FROM ColNames t \
30659WHERE t.name COLLATE NOCASE IN (\
30660 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
30661);\
30662";
30663  static const char * const zTabFill = "\
30664INSERT INTO ColNames(name,nlen,chop,reps,suff)\
30665 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
30666";
30667  static const char * const zHasDupes = "\
30668SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
30669 <count(name) FROM ColNames\
30670";
30671#ifdef SHELL_COLUMN_RENAME_CLEAN
30672  static const char * const zDedoctor = "\
30673UPDATE ColNames SET chop=iif(\
30674  (substring(name,nlen,1) BETWEEN '0' AND '9')\
30675  AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
30676 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
30677 0\
30678)\
30679";
30680#endif
30681  static const char * const zSetReps = "\
30682UPDATE ColNames AS t SET reps=\
30683(SELECT count(*) FROM ColNames d \
30684 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
30685 COLLATE NOCASE\
30686)\
30687";
30688#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
30689  static const char * const zColDigits = "\
30690SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
30691";
30692#else
30693  /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
30694  static const char * const zColDigits = "\
30695SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
30696 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
30697 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
30698";
30699#endif
30700  static const char * const zRenameRank =
30701#ifdef SHELL_COLUMN_RENAME_CLEAN
30702    "UPDATE ColNames AS t SET suff="
30703    "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
30704#else /* ...RENAME_MINIMAL_ONE_PASS */
30705"WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
30706"  SELECT 0 AS nlz"
30707"  UNION"
30708"  SELECT nlz+1 AS nlz FROM Lzn"
30709"  WHERE EXISTS("
30710"   SELECT 1"
30711"   FROM ColNames t, ColNames o"
30712"   WHERE"
30713"    iif(t.name IN (SELECT * FROM RepeatedNames),"
30714"     printf('%s"AUTOCOLUMN_SEP"%s',"
30715"      t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
30716"     t.name"
30717"    )"
30718"    ="
30719"    iif(o.name IN (SELECT * FROM RepeatedNames),"
30720"     printf('%s"AUTOCOLUMN_SEP"%s',"
30721"      o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
30722"     o.name"
30723"    )"
30724"    COLLATE NOCASE"
30725"    AND o.cpos<>t.cpos"
30726"   GROUP BY t.cpos"
30727"  )"
30728") UPDATE Colnames AS t SET"
30729" chop = 0," /* No chopping, never touch incoming names. */
30730" suff = iif(name IN (SELECT * FROM RepeatedNames),"
30731"  printf('"AUTOCOLUMN_SEP"%s', substring("
30732"   printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
30733"  ''"
30734" )"
30735#endif
30736    ;
30737  static const char * const zCollectVar = "\
30738SELECT\
30739 '('||x'0a'\
30740 || group_concat(\
30741  cname||' ANY',\
30742  ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
30743 ||')' AS ColsSpec \
30744FROM (\
30745 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
30746 FROM ColNames ORDER BY cpos\
30747)";
30748  static const char * const zRenamesDone =
30749    "SELECT group_concat("
30750    " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
30751    " ','||x'0a')"
30752    "FROM ColNames WHERE suff<>'' OR chop!=0"
30753    ;
30754  int rc;
30755  sqlite3_stmt *pStmt = 0;
30756  assert(pDb!=0);
30757  if( zColNew ){
30758    /* Add initial or additional column. Init db if necessary. */
30759    if( *pDb==0 ){
30760      if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
30761#ifdef SHELL_COLFIX_DB
30762      if(*zCOL_DB!=':')
30763        sqlite3_exec(*pDb,"drop table if exists ColNames;"
30764                     "drop view if exists RepeatedNames;",0,0,0);
30765#endif
30766#undef SHELL_COLFIX_DB
30767      rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
30768      rc_err_oom_die(rc);
30769    }
30770    assert(*pDb!=0);
30771    rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
30772    rc_err_oom_die(rc);
30773    rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
30774    rc_err_oom_die(rc);
30775    rc = sqlite3_step(pStmt);
30776    rc_err_oom_die(rc);
30777    sqlite3_finalize(pStmt);
30778    return 0;
30779  }else if( *pDb==0 ){
30780    return 0;
30781  }else{
30782    /* Formulate the columns spec, close the DB, zero *pDb. */
30783    char *zColsSpec = 0;
30784    int hasDupes = db_int(*pDb, "%s", zHasDupes);
30785    int nDigits = (hasDupes)? db_int(*pDb, "%s", zColDigits) : 0;
30786    if( hasDupes ){
30787#ifdef SHELL_COLUMN_RENAME_CLEAN
30788      rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
30789      rc_err_oom_die(rc);
30790#endif
30791      rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
30792      rc_err_oom_die(rc);
30793      rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
30794      rc_err_oom_die(rc);
30795      sqlite3_bind_int(pStmt, 1, nDigits);
30796      rc = sqlite3_step(pStmt);
30797      sqlite3_finalize(pStmt);
30798      if( rc!=SQLITE_DONE ) rc_err_oom_die(SQLITE_NOMEM);
30799    }
30800    assert(db_int(*pDb, "%s", zHasDupes)==0); /* Consider: remove this */
30801    rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
30802    rc_err_oom_die(rc);
30803    rc = sqlite3_step(pStmt);
30804    if( rc==SQLITE_ROW ){
30805      zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
30806    }else{
30807      zColsSpec = 0;
30808    }
30809    if( pzRenamed!=0 ){
30810      if( !hasDupes ) *pzRenamed = 0;
30811      else{
30812        sqlite3_finalize(pStmt);
30813        if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)
30814            && SQLITE_ROW==sqlite3_step(pStmt) ){
30815          *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
30816        }else
30817          *pzRenamed = 0;
30818      }
30819    }
30820    sqlite3_finalize(pStmt);
30821    sqlite3_close(*pDb);
30822    *pDb = 0;
30823    return zColsSpec;
30824  }
30825}
30826
30827/*
30828** Check if the sqlite_schema table contains one or more virtual tables. If
30829** parameter zLike is not NULL, then it is an SQL expression that the
30830** sqlite_schema row must also match. If one or more such rows are found,
30831** print the following warning to the output:
30832**
30833** WARNING: Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled
30834*/
30835static int outputDumpWarning(ShellState *p, const char *zLike){
30836  int rc = SQLITE_OK;
30837  sqlite3_stmt *pStmt = 0;
30838  shellPreparePrintf(p->db, &rc, &pStmt,
30839    "SELECT 1 FROM sqlite_schema o WHERE "
30840    "sql LIKE 'CREATE VIRTUAL TABLE%%' AND (%s)", zLike ? zLike : "true"
30841  );
30842  if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
30843    cli_puts("/* WARNING: "
30844          "Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */\n",
30845          p->out
30846    );
30847  }
30848  shellFinalize(&rc, pStmt);
30849  return rc;
30850}
30851
30852/*
30853** Fault-Simulator state and logic.
30854*/
30855static struct {
30856  int iId;           /* ID that triggers a simulated fault.  -1 means "any" */
30857  int iErr;          /* The error code to return on a fault */
30858  int iCnt;          /* Trigger the fault only if iCnt is already zero */
30859  int iInterval;     /* Reset iCnt to this value after each fault */
30860  int eVerbose;      /* When to print output */
30861  int nHit;          /* Number of hits seen so far */
30862  int nRepeat;       /* Turn off after this many hits.  0 for never */
30863  int nSkip;         /* Skip this many before first fault */
30864} faultsim_state = {-1, 0, 0, 0, 0, 0, 0, 0};
30865
30866/*
30867** This is the fault-sim callback
30868*/
30869static int faultsim_callback(int iArg){
30870  if( faultsim_state.iId>0 && faultsim_state.iId!=iArg ){
30871    return SQLITE_OK;
30872  }
30873  if( faultsim_state.iCnt ){
30874    if( faultsim_state.iCnt>0 ) faultsim_state.iCnt--;
30875    if( faultsim_state.eVerbose>=2 ){
30876      cli_printf(stdout, 
30877         "FAULT-SIM id=%d no-fault (cnt=%d)\n", iArg, faultsim_state.iCnt);
30878    }
30879    return SQLITE_OK;
30880  }
30881  if( faultsim_state.eVerbose>=1 ){
30882    cli_printf(stdout, 
30883         "FAULT-SIM id=%d returns %d\n", iArg, faultsim_state.iErr);
30884  }
30885  faultsim_state.iCnt = faultsim_state.iInterval;
30886  faultsim_state.nHit++;
30887  if( faultsim_state.nRepeat>0 && faultsim_state.nRepeat<=faultsim_state.nHit ){
30888    faultsim_state.iCnt = -1;
30889  }
30890  return faultsim_state.iErr;
30891}
30892
30893/*
30894** pickStr(zArg, &zErr, zS1, zS2, ..., "");
30895**
30896** Try to match zArg against zS1, zS2, and so forth until the first
30897** emptry string.  Return the index of the match or -1 if none is found.
30898** If no match is found, and &zErr is not NULL, then write into
30899** zErr a message describing the valid choices.
30900*/
30901static int pickStr(const char *zArg, char **pzErr, ...){
30902  int i, n;
30903  const char *z;
30904  sqlite3_str *pMsg;
30905  va_list ap;
30906  va_start(ap, pzErr);
30907  i = 0;
30908  while( (z = va_arg(ap,const char*))!=0 && z[0]!=0 ){
30909    if( cli_strcmp(zArg, z)==0 ) return i;
30910    i++;
30911  }
30912  va_end(ap);
30913  if( pzErr==0 ) return -1;
30914  n = i;
30915  pMsg = sqlite3_str_new(0);
30916  va_start(ap, pzErr);
30917  sqlite3_str_appendall(pMsg, "should be");
30918  i = 0;
30919  while( (z = va_arg(ap, const char*))!=0 && z[0]!=0 ){
30920    if( i==n-1 ){
30921      sqlite3_str_append(pMsg,", or",4);
30922    }else if( i>0 ){
30923      sqlite3_str_append(pMsg, ",", 1);
30924    }
30925    sqlite3_str_appendf(pMsg, " %s", z);
30926    i++;
30927  }
30928  va_end(ap);
30929  *pzErr = sqlite3_str_finish(pMsg);
30930  return -1;
30931}
30932
30933/*
30934** DOT-COMMAND: .import
30935**
30936** USAGE: .import [OPTIONS] FILE TABLE
30937**
30938** Import CSV or similar text from FILE into TABLE.  If TABLE does
30939** not exist, it is created using the first row of FILE as the column
30940** names.  If FILE begins with "|" then it is a command that is run
30941** and the output from the command is used as the input data.  If
30942** FILE begins with "<<" followed by a label, then content is read from
30943** the script until the first line that matches the label.
30944**
30945** The content of FILE is interpreted using RFC-4180 ("CSV") quoting
30946** rules unless the current mode is "ascii" or "tabs" or unless one
30947** the --ascii option is used.
30948**
30949** The column and row separators must be single ASCII characters.  If
30950** multiple characters or a Unicode character are specified for the
30951** separators, then only the first byte of the separator is used.  Except,
30952** if the row separator is \n and the mode is not --ascii, then \r\n is
30953** understood as a row separator too.
30954**
30955** Options:
30956**   --ascii         Do not use RFC-4180 quoting.  Use \037 and \036
30957**                   as column and row separators on input, unless other
30958**                   delimiters are specified using --colsep and/or --rowsep
30959**   --colsep CHAR   Use CHAR as the column separator.
30960**   --csv           Input is standard RFC-4180 CSV.
30961**   --esc CHAR      Use CHAR as an escape character in unquoted CSV inputs.
30962**   --qesc CHAR     Use CHAR as an escape character in quoted CSV inputs.
30963**   --rowsep CHAR   Use CHAR as the row separator.
30964**   --schema S      When creating TABLE, put it in schema S
30965**   --skip N        Ignore the first N rows of input
30966**   -v              Verbose mode
30967*/
30968static int dotCmdImport(ShellState *p){
30969  int nArg = p->dot.nArg;     /* Number of arguments */
30970  char **azArg = p->dot.azArg;/* Argument list */
30971  char *zTable = 0;           /* Insert data into this table */
30972  char *zSchema = 0;          /* Schema of zTable */
30973  char *zFile = 0;            /* Name of file to extra content from */
30974  sqlite3_stmt *pStmt = NULL; /* A statement */
30975  int nCol;                   /* Number of columns in the table */
30976  i64 nByte;                  /* Number of bytes in an SQL string */
30977  int i, j;                   /* Loop counters */
30978  int needCommit;             /* True to COMMIT or ROLLBACK at end */
30979  char *zSql = 0;             /* An SQL statement */
30980  ImportCtx sCtx;             /* Reader context */
30981  char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
30982  int eVerbose = 0;           /* Larger for more console output */
30983  i64 nSkip = 0;              /* Initial lines to skip */
30984  i64 iLineOffset = 0;        /* Offset to the first line of input */
30985  char *zCreate = 0;          /* CREATE TABLE statement text */
30986  int rc;                     /* Result code */
30987
30988  failIfSafeMode(p, "cannot run .import in safe mode");
30989  memset(&sCtx, 0, sizeof(sCtx));
30990  if( p->mode.eMode==MODE_Ascii ){
30991    xRead = ascii_read_one_field;
30992  }else{
30993    xRead = csv_read_one_field;
30994  }
30995  for(i=1; i<nArg; i++){
30996    char *z = azArg[i];
30997    if( z[0]=='-' && z[1]=='-' ) z++;
30998    if( z[0]!='-' ){
30999      if( zFile==0 ){
31000        zFile = z;
31001      }else if( zTable==0 ){
31002        zTable = z;
31003      }else{
31004        dotCmdError(p, i, "unknown argument", 0);
31005        return 1;
31006      }
31007    }else if( cli_strcmp(z,"-v")==0 ){
31008      eVerbose++;
31009    }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){
31010      zSchema = azArg[++i];
31011    }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){
31012      nSkip = integerValue(azArg[++i]);
31013    }else if( cli_strcmp(z,"-ascii")==0 ){
31014      if( sCtx.cColSep==0 ) sCtx.cColSep = SEP_Unit[0];
31015      if( sCtx.cRowSep==0 ) sCtx.cRowSep = SEP_Record[0];
31016      xRead = ascii_read_one_field;
31017    }else if( cli_strcmp(z,"-csv")==0 ){
31018      if( sCtx.cColSep==0 ) sCtx.cColSep = ',';
31019      if( sCtx.cRowSep==0 ) sCtx.cRowSep = '\n';
31020      xRead = csv_read_one_field;
31021    }else if( cli_strcmp(z,"-esc")==0 ){
31022      sCtx.cUQEscape = azArg[++i][0];
31023    }else if( cli_strcmp(z,"-qesc")==0 ){
31024      sCtx.cQEscape = azArg[++i][0];
31025    }else if( cli_strcmp(z,"-colsep")==0 ){
31026      if( i==nArg-1 ){
31027        dotCmdError(p, i, "missing argument", 0);
31028        return 1;
31029      }
31030      i++;
31031      sCtx.cColSep = azArg[i][0];
31032    }else if( cli_strcmp(z,"-rowsep")==0 ){
31033      if( i==nArg-1 ){
31034        dotCmdError(p, i, "missing argument", 0);
31035        return 1;
31036      }
31037      i++;
31038      sCtx.cRowSep = azArg[i][0];
31039    }else{
31040      dotCmdError(p, i, "unknown option", 0);
31041      return 1;
31042    }
31043  }
31044  if( zTable==0 ){
31045    dotCmdError(p, nArg, 0, "Missing %s argument\n",
31046          zFile==0 ? "FILE" : "TABLE");
31047    return 1;
31048  }
31049  seenInterrupt = 0;
31050  open_db(p, 0);
31051  if( sCtx.cColSep==0 ){
31052    if( p->mode.spec.zColumnSep && p->mode.spec.zColumnSep[0]!=0 ){
31053      sCtx.cColSep = p->mode.spec.zColumnSep[0];
31054    }else{
31055      sCtx.cColSep = ',';
31056    }
31057  }
31058  if( (sCtx.cColSep & 0x80)!=0 ){
31059    eputz("Error: .import column separator must be ASCII\n");
31060    return 1;
31061  }
31062  if( sCtx.cRowSep==0 ){
31063    if( p->mode.spec.zRowSep && p->mode.spec.zRowSep[0]!=0 ){
31064      sCtx.cRowSep = p->mode.spec.zRowSep[0];
31065    }else{
31066      sCtx.cRowSep = '\n';
31067    }
31068  }
31069  if( sCtx.cRowSep=='\r' && xRead!=ascii_read_one_field ){
31070    sCtx.cRowSep = '\n';
31071  }
31072  if( (sCtx.cRowSep & 0x80)!=0 ){
31073    eputz("Error: .import row separator must be ASCII\n");
31074    return 1;
31075  }
31076  sCtx.zFile = zFile;
31077  sCtx.nLine = 1;
31078  if( sCtx.zFile[0]=='|' ){
31079#ifdef SQLITE_OMIT_POPEN
31080    eputz("Error: pipes are not supported in this OS\n");
31081    return 1;
31082#else
31083    sCtx.in = sqlite3_popen(sCtx.zFile+1, "r");
31084    sCtx.zFile = "<pipe>";
31085    sCtx.xCloser = pclose;
31086#endif
31087  }else if( sCtx.zFile[0]=='<' && sCtx.zFile[1]=='<' && sCtx.zFile[2]!=0 ){
31088    /* Input text comes from subsequent lines of script until the zFile
31089    ** delimiter */
31090    int nEndMark = strlen30(zFile)-2;
31091    char *zEndMark = &zFile[2];
31092    sqlite3_str *pContent = sqlite3_str_new(p->db);
31093    int ckEnd = 1;
31094    i64 iStart = p->lineno;
31095    char zLine[2000];
31096    sCtx.zFile = p->zInFile;
31097    sCtx.nLine = p->lineno+1;
31098    iLineOffset = p->lineno;
31099    while( sqlite3_fgets(zLine,sizeof(zLine),p->in) ){
31100      if( ckEnd && cli_strncmp(zLine,zEndMark,nEndMark)==0 ){
31101        ckEnd = 2;
31102        if( strchr(zLine,'\n') ) p->lineno++;
31103        break;
31104      }
31105      if( strchr(zLine,'\n') ){
31106        p->lineno++;
31107        ckEnd = 1;
31108      }else{
31109        ckEnd = 0;
31110      }
31111      sqlite3_str_appendall(pContent, zLine);
31112    }
31113    sCtx.zIn = sqlite3_str_finish(pContent);
31114    if( sCtx.zIn==0 ){
31115      sCtx.zIn = sqlite3_mprintf("");
31116    }
31117    if( ckEnd<2 ){
31118      i64 savedLn = p->lineno;
31119      p->lineno = iStart;
31120      dotCmdError(p, 0, 0,"Content terminator \"%s\" not found.",zEndMark);
31121      p->lineno = savedLn;
31122      import_cleanup(&sCtx);
31123      return 1;
31124    }
31125  }else{
31126    sCtx.in = sqlite3_fopen(sCtx.zFile, "rb");
31127    sCtx.xCloser = fclose;
31128  }
31129  if( sCtx.in==0 && sCtx.zIn==0 ){
31130    dotCmdError(p, 0, 0, "cannot open \"%s\"", zFile);
31131    import_cleanup(&sCtx);
31132    return 1;
31133  }
31134  if( eVerbose>=1 ){
31135    char zSep[2];
31136    zSep[1] = 0;
31137    zSep[0] = sCtx.cColSep;
31138    cli_puts("Column separator ", p->out);
31139    output_c_string(p->out, zSep);
31140    cli_puts(", row separator ", p->out);
31141    zSep[0] = sCtx.cRowSep;
31142    output_c_string(p->out, zSep);
31143    cli_puts("\n", p->out);
31144  }
31145  sCtx.z = sqlite3_malloc64(120);
31146  if( sCtx.z==0 ){
31147    import_cleanup(&sCtx);
31148    shell_out_of_memory();
31149  }
31150  /* Below, resources must be freed before exit. */
31151  while( nSkip>0 ){
31152    nSkip--;
31153    while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
31154  }
31155  import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
31156  if( sqlite3_table_column_metadata(p->db, zSchema, zTable,0,0,0,0,0,0) 
31157   && 0==db_int(p->db, "SELECT count(*) FROM \"%w\".sqlite_schema"
31158                       " WHERE name=%Q AND type='view'",
31159                       zSchema ? zSchema : "main", zTable)
31160  ){
31161    /* Table does not exist.  Create it. */
31162    sqlite3 *dbCols = 0;
31163    char *zRenames = 0;
31164    char *zColDefs;
31165    zCreate = sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"", 
31166                  zSchema ? zSchema : "main", zTable);
31167    while( xRead(&sCtx) ){
31168      zAutoColumn(sCtx.z, &dbCols, 0);
31169      if( sCtx.cTerm!=sCtx.cColSep ) break;
31170    }
31171    zColDefs = zAutoColumn(0, &dbCols, &zRenames);
31172    if( zRenames!=0 ){
31173      cli_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
31174            "Columns renamed during .import %s due to duplicates:\n"
31175            "%s\n", sCtx.zFile, zRenames);
31176      sqlite3_free(zRenames);
31177    }
31178    assert(dbCols==0);
31179    if( zColDefs==0 ){
31180      cli_printf(stderr,"%s: empty file\n", sCtx.zFile);
31181      import_cleanup(&sCtx);
31182      sqlite3_free(zCreate);
31183      return 1;
31184    }
31185    zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
31186    if( zCreate==0 ){
31187      import_cleanup(&sCtx);
31188      shell_out_of_memory();
31189    }
31190    if( eVerbose>=1 ){
31191      cli_printf(p->out, "%s\n", zCreate);
31192    }
31193    rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
31194    if( rc ){
31195      cli_printf(stderr,
31196           "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
31197    }
31198    sqlite3_free(zCreate);
31199    zCreate = 0;
31200    if( rc ){
31201      import_cleanup(&sCtx);
31202      return 1;
31203    }
31204  }
31205  zSql = sqlite3_mprintf("SELECT count(*) FROM pragma_table_info(%Q,%Q);",
31206                         zTable, zSchema);
31207  if( zSql==0 ){
31208    import_cleanup(&sCtx);
31209    shell_out_of_memory();
31210  }
31211  rc =  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
31212  sqlite3_free(zSql);
31213  zSql = 0;
31214  if( rc ){
31215    if (pStmt) sqlite3_finalize(pStmt);
31216    shellDatabaseError(p->db);
31217    import_cleanup(&sCtx);
31218    return 1;
31219  }
31220  if( sqlite3_step(pStmt)==SQLITE_ROW ){
31221    nCol = sqlite3_column_int(pStmt, 0);
31222  }else{
31223    nCol = 0;
31224  }
31225  sqlite3_finalize(pStmt);
31226  pStmt = 0;
31227  if( nCol==0 ) return 0; /* no columns, no error */
31228
31229  nByte = 64                 /* space for "INSERT INTO", "VALUES(", ")\0" */
31230        + (zSchema ? strlen(zSchema)*2 + 2: 0)  /* Quoted schema name */
31231        + strlen(zTable)*2 + 2                  /* Quoted table name */
31232        + nCol*2;            /* Space for ",?" for each column */
31233  zSql = sqlite3_malloc64( nByte );
31234  if( zSql==0 ){
31235    import_cleanup(&sCtx);
31236    shell_out_of_memory();
31237  }
31238  if( zSchema ){
31239    sqlite3_snprintf(nByte, zSql, "INSERT INTO \"%w\".\"%w\" VALUES(?", 
31240                     zSchema, zTable);
31241  }else{
31242    sqlite3_snprintf(nByte, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
31243  }
31244  j = strlen30(zSql);
31245  for(i=1; i<nCol; i++){
31246    zSql[j++] = ',';
31247    zSql[j++] = '?';
31248  }
31249  zSql[j++] = ')';
31250  zSql[j] = 0;
31251  assert( j<nByte );
31252  if( eVerbose>=2 ){
31253    cli_printf(p->out, "Insert using: %s\n", zSql);
31254  }
31255  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
31256  sqlite3_free(zSql);
31257  zSql = 0;
31258  if( rc ){
31259    shellDatabaseError(p->db);
31260    if (pStmt) sqlite3_finalize(pStmt);
31261    import_cleanup(&sCtx);
31262    return 1;
31263  }
31264  needCommit = sqlite3_get_autocommit(p->db);
31265  if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
31266  do{
31267    int startLine = sCtx.nLine;
31268    for(i=0; i<nCol; i++){
31269      char *z = xRead(&sCtx);
31270      /*
31271      ** Did we reach end-of-file before finding any columns?
31272      ** If so, stop instead of NULL filling the remaining columns.
31273      */
31274      if( z==0 && i==0 ) break;
31275      /*
31276      ** Did we reach end-of-file OR end-of-line before finding any
31277      ** columns in ASCII mode?  If so, stop instead of NULL filling
31278      ** the remaining columns.
31279      */
31280      if( p->mode.eMode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
31281      /*
31282      ** For CSV mode, per RFC 4180, accept EOF in lieu of final
31283      ** record terminator but only for last field of multi-field row.
31284      ** (If there are too few fields, it's not valid CSV anyway.)
31285      */
31286      if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){
31287        z = "";
31288      }
31289      sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
31290      if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
31291        if( i==0 && (strcmp(z,"\n")==0 || strcmp(z,"\r\n")==0) ){
31292          /* Ignore trailing \n or \r\n when some other row separator */
31293          break;
31294        }
31295        cli_printf(stderr,"%s:%d: expected %d columns but found %d"
31296              " - filling the rest with NULL\n",
31297              sCtx.zFile, startLine, nCol, i+1);
31298        i += 2;
31299        while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
31300      }
31301    }
31302    if( sCtx.cTerm==sCtx.cColSep ){
31303      do{
31304        xRead(&sCtx);
31305        i++;
31306      }while( sCtx.cTerm==sCtx.cColSep );
31307      cli_printf(stderr,
31308            "%s:%d: expected %d columns but found %d - extras ignored\n",
31309            sCtx.zFile, startLine, nCol, i);
31310    }
31311    if( i>=nCol ){
31312      sqlite3_step(pStmt);
31313      rc = sqlite3_reset(pStmt);
31314      if( rc!=SQLITE_OK ){
31315        cli_printf(stderr,"%s:%d: INSERT failed: %s\n",
31316              sCtx.zFile, startLine, sqlite3_errmsg(p->db));
31317        sCtx.nErr++;
31318        if( bail_on_error ) break;
31319      }else{
31320        sCtx.nRow++;
31321      }
31322    }
31323  }while( sCtx.cTerm!=EOF );
31324
31325  import_cleanup(&sCtx);
31326  sqlite3_finalize(pStmt);
31327  if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
31328  if( eVerbose>0 ){
31329    cli_printf(p->out,
31330          "Added %d rows with %d errors using %d lines of input\n",
31331          sCtx.nRow, sCtx.nErr, sCtx.nLine-1-iLineOffset);
31332  }
31333  return sCtx.nErr ? 1 : 0;
31334}
31335
31336
31337/*
31338** This function computes what to show the user about the configured
31339** titles (or column-names).  Output is an integer between 0 and 3:
31340**
31341**    0:     The titles do not matter.  Never show anything.
31342**    1:     Show  "--titles off"
31343**    2:     Show  "--titles on"
31344**    3:     Show  "--title VALUE"  where VALUE is an encoding method
31345**             to use, one of: plain sql csv html tcl json
31346**
31347** Inputs are:
31348**
31349**    spec.bTitles   (bT)     Whether or not to show the titles
31350**    spec.eTitle    (eT)     The actual encoding to be used for titles
31351**    ModeInfo.bHdr  (bH)     Default value for spec.bTitles
31352**    ModeInfo.eHdr  (eH)     Default value for spec.eTitle
31353**    bAll                    Whether the -v option is used
31354*/
31355static int modeTitleDsply(ShellState *p, int bAll){
31356  int eMode = p->mode.eMode;
31357  const ModeInfo *pI = &aModeInfo[eMode];
31358  int bT = p->mode.spec.bTitles;
31359  int eT = p->mode.spec.eTitle;
31360  int bH = pI->bHdr;
31361  int eH = pI->eHdr;
31362
31363  /* Variable "v" is the truth table that will determine the answer
31364  **
31365  **                   Actual encoding is different from default 
31366  **                            vvvvvvvv                                    */
31367  sqlite3_uint64 v = UINT64_C(0x0133013311220102);
31368  /*                            ^^^^    ^^^^
31369  **                   Upper 2-byte groups for when ON/OFF disagrees with
31370  **                   the default.                                         */
31371
31372  if( bH==0 ) return 0;  /* Header not appliable.  Ex: off, count */
31373
31374  if( eT==0 ) eT = eH;   /* Fill in missing spec.eTitle */
31375  if( bT==0 ) bT = bH;   /* Fill in missing spec.bTitles */
31376
31377  if( eT!=eH ) v >>= 32; /* Encoding disagree in upper 4-bytes */
31378  if( bT!=bH ) v >>= 16; /* ON/OFF disagree in upper 2-byte pairs */
31379  if( bT<2 ) v >>= 8;    /* ON in even bytes, OFF in odd bytes (1st byte 0) */
31380  if( !bAll ) v >>= 4;   /* bAll values are in the lower half-byte */
31381
31382  return v & 3;          /* Return the selected truth-table entry */
31383}
31384
31385/*
31386** DOT-COMMAND:  .mode
31387**
31388** USAGE: .mode [MODE] [OPTIONS]
31389**
31390** Change the output mode to MODE and/or apply OPTIONS to the output mode.
31391** Arguments are processed from left to right.  If no arguments, show the
31392** current output mode and relevant options.
31393**
31394** Options:
31395**   --align STRING           Set the alignment of text in columnar modes
31396**                            String consists of characters 'L', 'C', 'R'
31397**                            meaning "left", "centered", and "right", with
31398**                            one letter per column starting from the left.
31399**                            Unspecified alignment defaults to 'L'.
31400**   --blob-quote ARG         ARG can be "auto", "text", "sql", "hex", "tcl",
31401**                            "json", or "size".  Default is "auto".
31402**   --border on|off          Show outer border on "box" and "table" modes.
31403**   --charlimit N            Set the maximum number of output characters to
31404**                            show for any single SQL value to N. Longer values
31405**                            truncated. Zero means "no limit".
31406**   --colsep STRING          Use STRING as the column separator
31407**   --escape ESC             Enable/disable escaping of control characters
31408**                            found in the output. ESC can be "off", "ascii",
31409**                            or "symbol".
31410**   --linelimit N            Set the maximum number of output lines to show for
31411**                            any single SQL value to N. Longer values are
31412**                            truncated. Zero means "no limit". Only works
31413**                            in "line" mode and in columnar modes.
31414**   --limits L,C,T           Shorthand for "--linelimit L --charlimit C
31415**                            --titlelimit T". The ",T" can be omitted in which
31416**                            case the --titlelimit is unchanged.  The argument
31417**                            can also be "off" to mean "0,0,0" or "on" to
31418**                            mean "5,300,20".
31419**   --list                   List available modes
31420**   --multiinsert N          In "insert" mode, put multiple rows on a single
31421**                            INSERT statement until the size exceeds N bytes.
31422**   --null STRING            Render SQL NULL values as the given string
31423**   --once                   Setting changes to the right are reverted after
31424**                            the next SQL command.
31425**   --quote ARG              Enable/disable quoting of text. ARG can be
31426**                            "off", "on", "sql", "relaxed", "csv", "html",
31427**                            "tcl", or "json". "off" means show the text as-is.
31428**                            "on" is an alias for "sql".
31429**   --reset                  Changes all mode settings back to their default.
31430**   --rowsep STRING          Use STRING as the row separator
31431**   --sw|--screenwidth N     Declare the screen width of the output device
31432**                            to be N characters.  An attempt may be made to
31433**                            wrap output text to fit within this limit. Zero
31434**                            means "no limit".  Or N can be "auto" to set the
31435**                            width automatically.
31436**   --tablename NAME         Set the name of the table for "insert" mode.
31437**   --tag NAME               Save mode to the left as NAME.
31438**   --textjsonb BOOLEAN      If enabled, JSONB text is displayed as text JSON.
31439**   --title ARG              Whether or not to show column headers, and if so
31440**                            how to encode them.  ARG can be "off", "on",
31441**                            "sql", "csv", "html", "tcl", or "json".
31442**   --titlelimit N           Limit the length of column titles to N characters.
31443**   -v|--verbose             Verbose output
31444**   --widths LIST            Set the columns widths for columnar modes. The
31445**                            argument is a list of integers, one for each
31446**                            column. A "0" width means use a dynamic width
31447**                            based on the actual width of data. If there are
31448**                            fewer entries in LIST than columns, "0" is used
31449**                            for the unspecified widths.
31450**   --wordwrap BOOLEAN       Enable/disable word wrapping
31451**   --wrap N                 Wrap columns wider than N characters
31452**   --ww                     Shorthand for "--wordwrap on"
31453*/
31454static int dotCmdMode(ShellState *p){
31455  int nArg = p->dot.nArg;     /* Number of arguments */
31456  char **azArg = p->dot.azArg;/* Argument list */
31457  int eMode = -1;             /* New mode value, or -1 for none */
31458  int iMode = -1;             /* Index of the argument that is the mode name */
31459  int i;                      /* Loop counter */
31460  int k;                      /* Misc index variable */
31461  int chng = 0;               /* True if anything has changed */
31462  int bAll = 0;               /* Show all details of the mode */
31463
31464  for(i=1; i<nArg; i++){
31465    const char *z = azArg[i];
31466    if( z[0]=='-' && z[1]=='-' ) z++;
31467    if( z[0]!='-'
31468     && iMode<0
31469     && (eMode = modeFind(p, azArg[i]))>=0
31470     && eMode!=MODE_Www
31471    ){
31472      iMode = i;
31473      modeChange(p, eMode);
31474      /* (Legacy) If the mode is MODE_Insert and the next argument
31475      ** is not an option, then the next argument must be the table
31476      ** name.
31477      */
31478      if( i+1<nArg && azArg[i+1][0]!='-' ){
31479        i++;
31480        modeSetStr(&p->mode.spec.zTableName, azArg[i]);
31481      }
31482      chng = 1;
31483    }else if( optionMatch(z,"align") ){
31484      char *zAlign;
31485      int nAlign;
31486      int nErr = 0;
31487      if( i+1>=nArg ){
31488        dotCmdError(p, i, "missing argument", 0);
31489        return 1;
31490      }
31491      i++;
31492      zAlign = azArg[i];
31493      nAlign = 0x3fff & strlen(zAlign);
31494      free(p->mode.spec.aAlign);
31495      p->mode.spec.aAlign = malloc(nAlign);
31496      shell_check_oom(p->mode.spec.aAlign);
31497      for(k=0; k<nAlign; k++){
31498        unsigned char c = 0;
31499        switch( zAlign[k] ){
31500          case 'l': case 'L':   c = QRF_ALIGN_Left;   break;
31501          case 'c': case 'C':   c = QRF_ALIGN_Center; break;
31502          case 'r': case 'R':   c = QRF_ALIGN_Right;  break;
31503          default:  nErr++; break;
31504        }
31505        p->mode.spec.aAlign[k] = c;
31506      }
31507      p->mode.spec.nAlign = nAlign;
31508      chng = 1;
31509      if( nErr ){
31510        dotCmdError(p, i, "bad alignment string",
31511             "Should contain only characters L, C, and R.");
31512        return 1;
31513      }
31514    }else if( pickStr(z,0,"-blob","-blob-quote","")>=0 ){
31515      if( (++i)>=nArg ){
31516        dotCmdError(p, i-1, "missing argument", 0);
31517        return 1;
31518      }
31519      k = pickStr(azArg[i], 0,
31520                    "auto", "text", "sql", "hex", "tcl", "json", "size", "");
31521        /*           0       1       2      3      4      5       6
31522        ** Must match QRF_BLOB_xxxx values.  See also tag-20251124a */
31523      if( k>=0 ){
31524        p->mode.spec.eBlob = k & 0xff;
31525      }
31526      chng = 1;
31527    }else if( optionMatch(z,"border") ){
31528      if( (++i)>=nArg ){
31529        dotCmdError(p, i-1, "missing argument", 0);
31530        return 1;
31531      }
31532      k = pickStr(azArg[i], 0, "auto", "off", "on", "");
31533      if( k>=0 ){
31534        p->mode.spec.bBorder = k & 0x3;
31535      }
31536      chng = 1;
31537    }else if( 0<=(k=pickStr(z,0,
31538                  "-charlimit","-linelimit","-titlelimit","-multiinsert","")) ){
31539      int w;   /*  0            1            2             3 */
31540      if( i+1>=nArg ){
31541        dotCmdError(p, i, "missing argument", 0);
31542        return 1;
31543      }
31544      w = integerValue(azArg[++i]);
31545      switch( k ){
31546        case 0:   p->mode.spec.nCharLimit = w;    break;
31547        case 1:   p->mode.spec.nLineLimit = w;    break;
31548        case 2:   p->mode.spec.nTitleLimit = w;   break;
31549        default:  p->mode.spec.nMultiInsert = w;  break;
31550      }
31551      chng = 1;
31552    }else if( 0<=(k=pickStr(z,0,"-tablename","-rowsep","-colsep","-null","")) ){
31553                            /*   0            1         2         3 */
31554      if( i+1>=nArg ){
31555        dotCmdError(p, i, "missing argument", 0);
31556        return 1;
31557      }
31558      i++;
31559      switch( k ){
31560        case 0: modeSetStr(&p->mode.spec.zTableName, azArg[i]); break;
31561        case 1: modeSetStr(&p->mode.spec.zRowSep, azArg[i]);    break;
31562        case 2: modeSetStr(&p->mode.spec.zColumnSep, azArg[i]); break;
31563        case 3: modeSetStr(&p->mode.spec.zNull, azArg[i]);      break;
31564      }
31565      chng = 1;
31566    }else if( optionMatch(z,"escape") ){
31567      /* See similar code at tag-20250224-1 */
31568      char *zErr = 0;
31569      if( (++i)>=nArg ){
31570        dotCmdError(p, i-1, "missing argument", 0);
31571        return 1;
31572      }                       /*  0     1       2  <-- One less than QRF_ESC_ */
31573      k = pickStr(azArg[i],&zErr,"off","ascii","symbol","");
31574      if( k<0 ){
31575        dotCmdError(p, i, "unknown escape type", "%s", zErr);
31576        sqlite3_free(zErr);
31577        return 1;
31578      }
31579      p->mode.spec.eEsc = k+1;
31580      chng = 1;
31581    }else if( optionMatch(z,"limits") ){
31582      if( (++i)>=nArg ){
31583        dotCmdError(p, i-1, "missing argument", 0);
31584        return 1;
31585      }
31586      k = pickStr(azArg[i],0,"on","off","");
31587      if( k==0 ){
31588        p->mode.spec.nLineLimit = DFLT_LINE_LIMIT;
31589        p->mode.spec.nCharLimit = DFLT_CHAR_LIMIT;
31590        p->mode.spec.nTitleLimit = DFLT_TITLE_LIMIT;
31591      }else if( k==1 ){
31592        p->mode.spec.nLineLimit = 0;
31593        p->mode.spec.nCharLimit = 0;
31594        p->mode.spec.nTitleLimit = 0;
31595      }else{
31596        int L, C, T = 0;
31597        int nNum = sscanf(azArg[i], "%d,%d,%d", &L, &C, &T);
31598        if( nNum<2 || L<0 || C<0 || T<0){
31599          dotCmdError(p, i, "bad argument", "Should be \"L,C,T\" where L, C"
31600                            " and T are unsigned integers");
31601          return 1;
31602        }        
31603        p->mode.spec.nLineLimit = L;
31604        p->mode.spec.nCharLimit = C;
31605        if( nNum==3 ) p->mode.spec.nTitleLimit = T;
31606      }
31607      chng = 1;
31608    }else if( optionMatch(z,"list") ){
31609      int ii;
31610      cli_puts("available modes:", p->out);
31611      for(ii=0; ii<ArraySize(aModeInfo); ii++){
31612        if( ii==MODE_Www ) continue;
31613        cli_printf(p->out, " %s", aModeInfo[ii].zName);
31614      }
31615      for(ii=0; ii<p->nSavedModes; ii++){
31616        cli_printf(p->out, " %s", p->aSavedModes[ii].zTag);
31617      }
31618      cli_puts(" batch tty\n", p->out);
31619      chng = 1;  /* Not really a change, but we still want to suppress the
31620                 ** "current mode" output */
31621    }else if( optionMatch(z,"once") ){
31622      p->nPopMode = 0;
31623      modePush(p);
31624      p->nPopMode = 1;
31625    }else if( optionMatch(z,"noquote") ){
31626      /* (undocumented legacy) --noquote always turns quoting off */
31627      p->mode.spec.eText = QRF_TEXT_Plain;
31628      p->mode.spec.eBlob = QRF_BLOB_Auto;
31629      chng = 1;
31630    }else if( optionMatch(z,"quote") ){
31631      if( i+1<nArg
31632       && azArg[i+1][0]!='-'
31633       && (iMode>0 || strcmp(azArg[i+1],"off")==0 || modeFind(p, azArg[i+1])<0)
31634      ){
31635        /* --quote is followed by an argument other that is not an option
31636        ** or a mode name.  See it must be a boolean or a keyword to describe
31637        ** how to set quoting. */
31638        i++;
31639        if( (k = pickStr(azArg[i],0,"no","yes","0","1",""))>=0 ){
31640          k &= 1;   /* 0 for "off".  1 for "on". */
31641        }else{
31642          char *zErr = 0;
31643          k = pickStr(azArg[i],&zErr,
31644                 "off","on","sql","csv","html","tcl","json","relaxed","");
31645              /*  0     1    2     3     4      5     6      7   */
31646          if( k<0 ){
31647            dotCmdError(p, i, "unknown", "%z", zErr);
31648            return 1;
31649          }
31650        }
31651      }else{
31652        /* (Legacy) no following boolean argument.  Turn quoting on */ 
31653        k = 1;
31654      }
31655      switch( k ){
31656        case 1:  /* on */
31657          modeSetStr(&p->mode.spec.zNull, "NULL");
31658          /* Fall through */
31659        case 2:  /* sql */
31660          p->mode.spec.eText = QRF_TEXT_Sql;
31661          break;
31662        case 3:  /* csv */
31663          p->mode.spec.eText = QRF_TEXT_Csv;
31664          break;
31665        case 4:  /* html */
31666          p->mode.spec.eText = QRF_TEXT_Html;
31667          break;
31668        case 5:  /* tcl */
31669          p->mode.spec.eText = QRF_TEXT_Tcl;
31670          break;
31671        case 6:  /* json */
31672          p->mode.spec.eText = QRF_TEXT_Json;
31673          break;
31674        case 7:  /* relaxed */
31675          p->mode.spec.eText = QRF_TEXT_Relaxed;
31676          break;
31677        default: /* off */
31678          p->mode.spec.eText = QRF_TEXT_Plain;
31679          break;
31680      }
31681      chng = 1;
31682    }else if( optionMatch(z,"reset") ){
31683      int saved_eMode = p->mode.eMode;
31684      modeFree(&p->mode);
31685      modeChange(p, saved_eMode);
31686    }else if( optionMatch(z,"screenwidth") || optionMatch(z,"sw") ){
31687      if( (++i)>=nArg ){
31688        dotCmdError(p, i-1, "missing argument", 0);
31689        return 1;
31690      }
31691      k = pickStr(azArg[i],0,"off","auto","");
31692      if( k==0 ){
31693        p->mode.bAutoScreenWidth = 0;
31694        p->mode.spec.nScreenWidth = 0;
31695      }else if( k==1 ){
31696        p->mode.bAutoScreenWidth = 1;
31697      }else{
31698        i64 w = integerValue(azArg[i]);
31699        p->mode.bAutoScreenWidth = 0;
31700        if( w<0 ) w = 0;
31701        if( w>QRF_MAX_WIDTH ) w = QRF_MAX_WIDTH;
31702        p->mode.spec.nScreenWidth = w;
31703      }
31704      chng = 1;
31705    }else if( optionMatch(z,"tag") ){
31706      size_t nByte;
31707      int n;
31708      const char *zTag;
31709      if( i+1>=nArg ){
31710        dotCmdError(p, i, "missing argument", 0);
31711        return 1;
31712      }
31713      zTag = azArg[++i];
31714      if( modeFind(p, zTag)>=0 ){
31715        dotCmdError(p, i, "mode already exists", 0);
31716        return 1;
31717      }
31718      if( p->nSavedModes > MODE_N_USER ){
31719        dotCmdError(p, i-1, "cannot add more modes", 0);
31720        return 1;
31721      }
31722      n = p->nSavedModes++;
31723      nByte = sizeof(p->aSavedModes[0]);
31724      nByte *= n+1;
31725      p->aSavedModes = realloc( p->aSavedModes, nByte );
31726      shell_check_oom(p->aSavedModes);
31727      p->aSavedModes[n].zTag = strdup(zTag);
31728      shell_check_oom(p->aSavedModes[n].zTag);
31729      modeDup(&p->aSavedModes[n].mode, &p->mode);
31730      chng = 1;
31731    }else if( optionMatch(z,"textjsonb") ){
31732      if( i+1>=nArg ){
31733        dotCmdError(p, i, "missing argument", 0);
31734        return 1;
31735      }
31736      p->mode.spec.bTextJsonb = booleanValue(azArg[++i]) ? QRF_Yes : QRF_No;
31737      chng = 1;
31738    }else if( optionMatch(z,"titles") || optionMatch(z,"title") ){
31739      char *zErr = 0;
31740      if( i+1>=nArg ){
31741        dotCmdError(p, i, "missing argument", 0);
31742        return 1;
31743      }
31744      k = pickStr(azArg[++i],&zErr,
31745              "off","on","plain","sql","csv","html","tcl","json","");
31746          /*   0     1    2       3     4     5     6      7 */
31747      if( k<0 ){
31748        dotCmdError(p, i, "bad --titles value","%z", zErr);
31749        return 1;
31750      }
31751      p->mode.spec.bTitles = k>=1 ? QRF_Yes : QRF_No;
31752      p->mode.mFlags &= ~MFLG_HDR;
31753      p->mode.spec.eTitle = k>1 ? k-1 : aModeInfo[p->mode.eMode].eHdr;
31754      chng = 1;
31755    }else if( optionMatch(z,"widths") || optionMatch(z,"width") ){
31756      int nWidth = 0;
31757      short int *aWidth;
31758      const char *zW;
31759      if( i+1>=nArg ){
31760        dotCmdError(p, i, "missing argument", 0);
31761        return 1;
31762      }
31763      zW = azArg[++i];
31764      /* Every width value takes at least 2 bytes in the input string to
31765      ** specify, so strlen(zW) bytes should be plenty of space to hold the
31766      ** result. */
31767      aWidth = malloc( strlen(zW) );
31768      while( IsSpace(zW[0]) ) zW++;
31769      while( zW[0] ){
31770        int w = 0;
31771        int nDigit = 0;
31772        k = zW[0]=='-' && IsDigit(zW[1]);
31773        while( IsDigit(zW[k]) ){
31774          w = w*10 + zW[k] - '0';
31775          if( w>QRF_MAX_WIDTH ){
31776            dotCmdError(p,i+1,"width too big",
31777                        "Maximum column width is %d", QRF_MAX_WIDTH);
31778            free(aWidth);
31779            return 1;
31780          }
31781          nDigit++;
31782          k++;
31783        }
31784        if( nDigit==0 ){
31785          dotCmdError(p,i+1,"syntax error",
31786                      "should be a comma-separated list if integers");
31787          free(aWidth);
31788          return 1;
31789        }
31790        if( zW[0]=='-' ) w = -w;
31791        aWidth[nWidth++] = w;
31792        zW += k;
31793        if( zW[0]==',' ) zW++;
31794        while( IsSpace(zW[0]) ) zW++;
31795      }
31796      free(p->mode.spec.aWidth);
31797      p->mode.spec.aWidth = aWidth;
31798      p->mode.spec.nWidth = nWidth;
31799      chng = 1;
31800    }else if( optionMatch(z,"wrap") ){
31801      int w;
31802      if( i+1>=nArg ){
31803        dotCmdError(p, i, "missing argument", 0);
31804        return 1;
31805      }
31806      w = integerValue(azArg[++i]);
31807      if( w<(-QRF_MAX_WIDTH) ) w = -QRF_MAX_WIDTH;
31808      if( w>QRF_MAX_WIDTH ) w = QRF_MAX_WIDTH;
31809      p->mode.spec.nWrap = w;
31810      chng = 1;
31811    }else if( optionMatch(z,"ww") ){
31812      p->mode.spec.bWordWrap = QRF_Yes;
31813      chng = 1;
31814    }else if( optionMatch(z,"wordwrap") ){
31815      if( i+1>=nArg ){
31816        dotCmdError(p, i, "missing argument", 0);
31817        return 1;
31818      }
31819      p->mode.spec.bWordWrap = (u8)booleanValue(azArg[++i]) ? QRF_Yes : QRF_No;
31820      chng = 1;
31821    }else if( optionMatch(z,"v") || optionMatch(z,"verbose") ){
31822      bAll = 1;
31823    }else if( z[0]=='-' ){
31824      dotCmdError(p, i, "bad option", "Use \".help .mode\" for more info");
31825      return 1;
31826    }else{
31827      dotCmdError(p, i, iMode>0?"bad argument":"unknown mode",
31828                  "Use \".help .mode\" for more info");
31829      return 1;
31830    }
31831  }
31832  if( !chng || bAll ){
31833    const ModeInfo *pI = aModeInfo + p->mode.eMode;
31834    sqlite3_str *pDesc = sqlite3_str_new(p->db);
31835    char *zDesc;
31836    const char *zSetting;
31837
31838    if( p->nPopMode ) sqlite3_str_appendall(pDesc, "--once ");
31839    sqlite3_str_appendall(pDesc,pI->zName);
31840    if( bAll || (p->mode.spec.nAlign && pI->eCx==2) ){
31841      int ii;
31842      sqlite3_str_appendall(pDesc, " --align \"");
31843      for(ii=0; ii<p->mode.spec.nAlign; ii++){
31844        unsigned char a = p->mode.spec.aAlign[ii];
31845        sqlite3_str_appendchar(pDesc, 1, "LLCR"[a&3]);
31846      }
31847      sqlite3_str_append(pDesc, "\"", 1);
31848    }
31849    if( bAll
31850     || (p->mode.spec.bBorder==QRF_No) != ((pI->mFlg&1)!=0)
31851    ){
31852      sqlite3_str_appendf(pDesc," --border %s",
31853             p->mode.spec.bBorder==QRF_No ? "off" : "on");
31854    }
31855    if( bAll || p->mode.spec.eBlob!=QRF_BLOB_Auto ){
31856      const char *azBQuote[] =
31857           { "auto", "text", "sql", "hex", "tcl", "json", "size" };
31858      /*      0       1       2      3      4      5       6
31859      ** Must match QRF_BLOB_xxxx values.  See all instances of tag-20251124a */
31860      u8 e = p->mode.spec.eBlob;
31861      sqlite3_str_appendf(pDesc, " --blob-quote %s", azBQuote[e]);
31862    }
31863    zSetting = aModeStr[pI->eCSep];
31864    if( bAll || (zSetting && cli_strcmp(zSetting,p->mode.spec.zColumnSep)!=0) ){
31865      sqlite3_str_appendf(pDesc, " --colsep ");
31866      append_c_string(pDesc, p->mode.spec.zColumnSep);
31867    }
31868    if( bAll || p->mode.spec.eEsc!=QRF_Auto ){
31869      sqlite3_str_appendf(pDesc, " --escape %s",qrfEscNames[p->mode.spec.eEsc]);
31870    }
31871    if( bAll
31872     || (p->mode.spec.nLineLimit>0 && pI->eCx>0)
31873     || p->mode.spec.nCharLimit>0
31874     || (p->mode.spec.nTitleLimit>0 && pI->eCx>0)
31875    ){
31876      if( p->mode.spec.nLineLimit==0
31877       && p->mode.spec.nCharLimit==0
31878       && p->mode.spec.nTitleLimit==0
31879      ){
31880        sqlite3_str_appendf(pDesc, " --limits off");
31881      }else if( p->mode.spec.nLineLimit==DFLT_LINE_LIMIT
31882       && p->mode.spec.nCharLimit==DFLT_CHAR_LIMIT
31883       && p->mode.spec.nTitleLimit==DFLT_TITLE_LIMIT
31884      ){
31885        sqlite3_str_appendf(pDesc, " --limits on");
31886      }else{
31887        sqlite3_str_appendf(pDesc, " --limits %d,%d,%d",
31888           p->mode.spec.nLineLimit, p->mode.spec.nCharLimit,
31889           p->mode.spec.nTitleLimit);
31890      }
31891    }
31892    if( bAll
31893     || (p->mode.spec.nMultiInsert && p->mode.spec.eStyle==QRF_STYLE_Insert)
31894    ){
31895      sqlite3_str_appendf(pDesc, " --multiinsert %u",
31896                          p->mode.spec.nMultiInsert);
31897    }
31898    zSetting = aModeStr[pI->eNull];
31899    if( bAll || (zSetting && cli_strcmp(zSetting,p->mode.spec.zNull)!=0) ){
31900      sqlite3_str_appendf(pDesc, " --null ");
31901      append_c_string(pDesc, p->mode.spec.zNull);
31902    }
31903    if( bAll 
31904     || (pI->eText!=p->mode.spec.eText && (pI->eText>1 || p->mode.spec.eText>1))
31905    ){
31906      sqlite3_str_appendf(pDesc," --quote %s",qrfQuoteNames[p->mode.spec.eText]);
31907    }
31908    zSetting = aModeStr[pI->eRSep];
31909    if( bAll || (zSetting && cli_strcmp(zSetting,p->mode.spec.zRowSep)!=0) ){
31910      sqlite3_str_appendf(pDesc, " --rowsep ");
31911      append_c_string(pDesc, p->mode.spec.zRowSep);
31912    }
31913    if( bAll
31914     || (pI->eCx && (p->mode.spec.nScreenWidth>0 || p->mode.bAutoScreenWidth))
31915    ){
31916      if( p->mode.bAutoScreenWidth ){
31917        sqlite3_str_appendall(pDesc, " --sw auto");
31918      }else{
31919        sqlite3_str_appendf(pDesc," --sw %d",
31920                            p->mode.spec.nScreenWidth);
31921      }
31922    }
31923    if( bAll || p->mode.eMode==MODE_Insert ){
31924      sqlite3_str_appendf(pDesc," --tablename ");
31925      append_c_string(pDesc, p->mode.spec.zTableName);
31926    }
31927    if( bAll || p->mode.spec.bTextJsonb ){
31928      sqlite3_str_appendf(pDesc," --textjsonb %s",
31929             p->mode.spec.bTextJsonb==QRF_Yes ? "on" : "off");
31930    }
31931    k = modeTitleDsply(p, bAll);
31932    if( k==1 ){
31933      sqlite3_str_appendall(pDesc, " --titles off");
31934    }else if( k==2 ){
31935      sqlite3_str_appendall(pDesc, " --titles on");
31936    }else if( k==3 ){
31937      static const char *azTitle[] =
31938          { "plain", "sql", "csv", "html", "tcl", "json"};
31939      sqlite3_str_appendf(pDesc, " --titles %s",
31940                   azTitle[p->mode.spec.eTitle-1]);
31941    }    
31942    if( p->mode.spec.nWidth>0 && (bAll || pI->eCx==2) ){
31943      int ii;
31944      const char *zSep = " --widths ";
31945      for(ii=0; ii<p->mode.spec.nWidth; ii++){
31946        sqlite3_str_appendf(pDesc, "%s%d", zSep, (int)p->mode.spec.aWidth[ii]);
31947        zSep = ",";
31948      }
31949    }else if( bAll ){
31950      sqlite3_str_appendall(pDesc, " --widths \"\"");
31951    }
31952    if( bAll || (pI->eCx>0 && p->mode.spec.bWordWrap) ){
31953      if( bAll ){
31954        sqlite3_str_appendf(pDesc, " --wordwrap %s",
31955          p->mode.spec.bWordWrap==QRF_Yes ? "on" : "off");
31956      }
31957      if( p->mode.spec.nWrap ){
31958        sqlite3_str_appendf(pDesc, " --wrap %d", p->mode.spec.nWrap);
31959      }
31960      if( !bAll ) sqlite3_str_append(pDesc, " --ww", 5);
31961    }
31962    zDesc = sqlite3_str_finish(pDesc);
31963    cli_printf(p->out, ".mode %s\n", zDesc);
31964    fflush(p->out);
31965    sqlite3_free(zDesc);
31966  }
31967  return 0;
31968}
31969
31970/*
31971** DOT-COMMAND: .output
31972** USAGE: .output [OPTIONS] [FILE]
31973**
31974** Begin redirecting output to FILE.  Or if FILE is omitted, revert
31975** to sending output to the console.  If FILE begins with "|" then
31976** the remainder of file is taken as a pipe and output is directed
31977** into that pipe.  If FILE is "memory" then output is captured in an
31978** internal memory buffer.  If FILE is "off" then output is redirected
31979** into /dev/null or the equivalent.
31980**
31981** Options:
31982**   --bom             Prepend a byte-order mark to the output
31983**   -e                Accumulate output in a temporary text file then
31984**                     launch a text editor when the redirection ends.
31985**   --error-prefix X  Use X as the left-margin prefix for error messages.
31986**                     Set to an empty string to restore the default.
31987**   --keep            Keep redirecting output to its current destination.
31988**                     Use this option in combination with --show or
31989**                     with --error-prefix when you do not want to stop
31990**                     a current redirection.
31991**   --plain           Use plain text rather than HTML tables with -w
31992**   --show            Show output text captured by .testcase or by
31993**                     redirecting to "memory".
31994**   -w                Show the output in a web browser.  Output is
31995**                     written into a temporary HTML file until the
31996**                     redirect ends, then the web browser is launched.
31997**                     Query results  are shown as HTML tables, unless
31998**                     the --plain is used too.
31999**   -x                Show the output in a spreadsheet.  Output is
32000**                     written to a temp file as CSV then the spreadsheet
32001**                     is launched when
32002**
32003** DOT-COMMAND: .once
32004** USAGE: .once [OPTIONS] FILE ...
32005**
32006** Write the output for the next line of SQL or the next dot-command into
32007** FILE.  If FILE begins with "|" then it is a program into which output
32008** is written. The FILE argument should be omitted if one of the -e, -w,
32009** or -x options is used.
32010**
32011** Options:
32012**   -e                Capture output into a temporary file then bring up
32013**                     a text editor on that temporary file.
32014**   --plain           Use plain text rather than HTML tables with -w
32015**   -w                Capture output into an HTML file then bring up that
32016**                     file in a web browser
32017**   -x                Show the output in a spreadsheet.  Output is
32018**                     written to a temp file as CSV then the spreadsheet
32019**                     is launched when
32020**
32021** DOT-COMMAND: .excel
32022** Shorthand for ".once -x"
32023**
32024** DOT-COMMAND: .www [--plain]
32025** Shorthand for ".once -w" or ".once --plain -w"
32026*/
32027static int dotCmdOutput(ShellState *p){
32028  int nArg = p->dot.nArg;        /* Number of arguments */
32029  char **azArg = p->dot.azArg;   /* Text of the arguments */
32030  char *zFile = 0;               /* The FILE argument */
32031  int i;                  /* Loop counter */
32032  int eMode = 0;          /* 0: .outout/.once, 'x'=.excel, 'w'=.www */
32033  int bOnce = 0;          /* 0: .output, 1: .once, 2: .excel/.www */
32034  int bPlain = 0;         /* --plain option */
32035  int bKeep = 0;          /* Keep redirecting */
32036  static const char *zBomUtf8 = "\357\273\277";
32037  const char *zBom = 0;
32038  char c = azArg[0][0];
32039  int n = strlen30(azArg[0]);
32040
32041  failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
32042  if( c=='e' ){
32043    eMode = 'x';
32044    bOnce = 2;
32045  }else if( c=='w' ){
32046    eMode = 'w';
32047    bOnce = 2;
32048  }else if( n>=2 && cli_strncmp(azArg[0],"once",n)==0 ){
32049    bOnce = 1;
32050  }
32051  for(i=1; i<nArg; i++){
32052    char *z = azArg[i];
32053    if( z[0]=='-' ){
32054      if( z[1]=='-' ) z++;
32055      if( cli_strcmp(z,"-bom")==0 ){
32056        zBom = zBomUtf8;
32057      }else if( cli_strcmp(z,"-plain")==0 ){
32058        bPlain = 1;
32059      }else if( c=='o' && sqlite3_strglob("-[ewx]",z)==0 ){
32060        if( bKeep || eMode ){
32061          dotCmdError(p, i, "incompatible with prior options",0);
32062          goto dotCmdOutput_error;
32063        }
32064        eMode = z[1];
32065      }else if( cli_strcmp(z,"-show")==0 ){
32066        if( cli_output_capture ){
32067          sqlite3_fprintf(stdout, "%s", sqlite3_str_value(cli_output_capture));
32068        }
32069      }else if( cli_strcmp(z,"-keep")==0 ){
32070        bKeep = 1;
32071      }else if( optionMatch(z,"error-prefix") ){
32072        if( i+1>=nArg ){
32073          dotCmdError(p, i, "missing argument", 0);
32074          return 1;
32075        }
32076        free(p->zErrPrefix);
32077        i++;
32078        p->zErrPrefix = azArg[i][0]==0 ? 0 : strdup(azArg[i]);
32079      }else{
32080        dotCmdError(p, i, "unknown option", 0);
32081        sqlite3_free(zFile);
32082        return 1;
32083      }
32084    }else if( zFile==0 && eMode==0 ){
32085      if( bKeep ){
32086        dotCmdError(p, i, "incompatible with prior options",0);
32087        goto dotCmdOutput_error;
32088      }
32089      if( cli_strcmp(z, "memory")==0 && bOnce ){
32090        dotCmdError(p, 0, "cannot redirect to \"memory\"", 0);
32091        goto dotCmdOutput_error;
32092      }
32093      if( cli_strcmp(z, "off")==0 ){
32094#ifdef _WIN32
32095        zFile = sqlite3_mprintf("nul");
32096#else
32097        zFile = sqlite3_mprintf("/dev/null");
32098#endif
32099      }else{
32100        zFile = sqlite3_mprintf("%s", z);
32101      }
32102      if( zFile && zFile[0]=='|' ){
32103        while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
32104        break;
32105      }
32106    }else{
32107      dotCmdError(p, i, "surplus argument", 0);
32108      sqlite3_free(zFile);
32109      return 1;
32110    }
32111  }
32112  if( zFile==0 && !bKeep ){
32113    zFile = sqlite3_mprintf("stdout");
32114    shell_check_oom(zFile);
32115  }
32116  if( bOnce ){
32117    p->nPopOutput = 2;
32118  }else{
32119    p->nPopOutput = 0;
32120  }
32121  if( !bKeep ) output_reset(p);
32122#ifndef SQLITE_NOHAVE_SYSTEM
32123  if( eMode=='e' || eMode=='x' || eMode=='w' ){
32124    p->doXdgOpen = 1;
32125    modePush(p);
32126    if( eMode=='x' ){
32127      /* spreadsheet mode.  Output as CSV. */
32128      newTempFile(p, "csv");
32129      p->mode.mFlags &= ~MFLG_ECHO;
32130      p->mode.eMode = MODE_Csv;
32131      modeSetStr(&p->mode.spec.zColumnSep, SEP_Comma);
32132      modeSetStr(&p->mode.spec.zRowSep, SEP_CrLf);
32133#ifdef _WIN32
32134      zBom = zBomUtf8;  /* Always include the BOM on Windows, as Excel does
32135                        ** not work without it. */
32136#endif
32137    }else if( eMode=='w' ){
32138      /* web-browser mode. */
32139      newTempFile(p, "html");
32140      if( !bPlain ) p->mode.eMode = MODE_Www;
32141    }else{
32142      /* text editor mode */
32143      newTempFile(p, "txt");
32144    }
32145    sqlite3_free(zFile);
32146    zFile = sqlite3_mprintf("%s", p->zTempFile);
32147  }
32148#endif /* SQLITE_NOHAVE_SYSTEM */
32149  if( !bKeep ) shell_check_oom(zFile);
32150  if( bKeep ){
32151    /* no-op */
32152  }else if( cli_strcmp(zFile,"memory")==0 ){
32153    if( cli_output_capture ){
32154      sqlite3_str_free(cli_output_capture);
32155    }
32156    cli_output_capture = sqlite3_str_new(0);
32157  }else if( zFile[0]=='|' ){
32158#ifdef SQLITE_OMIT_POPEN
32159    eputz("Error: pipes are not supported in this OS\n");
32160    output_redir(p, stdout);
32161    goto dotCmdOutput_error;
32162#else
32163    FILE *pfPipe = sqlite3_popen(zFile + 1, "w");
32164    if( pfPipe==0 ){
32165      assert( stderr!=NULL );
32166      cli_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
32167      goto dotCmdOutput_error;
32168    }else{
32169      output_redir(p, pfPipe);
32170      if( zBom ) cli_puts(zBom, pfPipe);
32171      sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
32172    }
32173#endif
32174  }else{
32175    FILE *pfFile = output_file_open(p, zFile);
32176    if( pfFile==0 ){
32177      if( cli_strcmp(zFile,"off")!=0 ){
32178       assert( stderr!=NULL );
32179       cli_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
32180      }
32181      goto dotCmdOutput_error;
32182    } else {
32183      output_redir(p, pfFile);
32184      if( zBom ) cli_puts(zBom, pfFile);
32185      if( bPlain && eMode=='w' ){
32186        cli_puts(
32187          "<!DOCTYPE html>\n<BODY>\n<PLAINTEXT>\n",
32188          pfFile
32189        );
32190      }
32191      sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
32192    }
32193  }
32194  sqlite3_free(zFile);
32195  return 0;
32196
32197dotCmdOutput_error:
32198  sqlite3_free(zFile);
32199  return 1;
32200}
32201
32202/*
32203** DOT-COMMAND: .check
32204** USAGE: .check [OPTIONS] PATTERN
32205**
32206** Verify results of commands since the most recent .testcase command.
32207** Restore output to the console, unless --keep is used.
32208**
32209** If PATTERN starts with "<<ENDMARK" then the actual pattern is taken from
32210** subsequent lines of text up to the first line that begins with ENDMARK.
32211** All pattern lines and the ENDMARK are discarded.
32212**
32213** Options:
32214**   --exact                Do an exact comparison including leading and
32215**                          trailing whitespace.
32216**   --glob                 Treat PATTERN as a GLOB
32217**   --keep                 Do not reset the testcase.  More .check commands
32218**                          will follow.
32219**   --notglob              Output should not match PATTERN
32220**   --show                 Write testcase output to the screen, for debugging.
32221*/
32222static int dotCmdCheck(ShellState *p){
32223  int nArg = p->dot.nArg;            /* Number of arguments */
32224  char **azArg = p->dot.azArg;       /* Text of the arguments */
32225  int i;                             /* Loop counter */
32226  int k;                             /* Result of pickStr() */
32227  char *zTest;                       /* Textcase result */
32228  int bKeep = 0;                     /* --keep option */
32229  char *zCheck = 0;                  /* PATTERN argument */
32230  char *zPattern = 0;                /* Actual test pattern */
32231  int eCheck = 0;                    /* 1: --glob,  2: --notglob,  3: --exact */
32232  int isOk;                          /* True if results are OK */
32233  sqlite3_int64 iStart = p->lineno;  /* Line number of .check statement */
32234
32235  if( p->zTestcase[0]==0 ){
32236    dotCmdError(p, 0, "no .testcase is active", 0);
32237    return 1;
32238  }
32239  for(i=1; i<nArg; i++){
32240    char *z = azArg[i];
32241    if( z[0]=='-' && z[1]=='-' && z[2]!=0 ) z++;
32242    if( cli_strcmp(z,"-keep")==0 ){
32243      bKeep = 1;
32244    }else if( cli_strcmp(z,"-show")==0 ){
32245      if( cli_output_capture ){
32246        sqlite3_fprintf(stdout, "%s", sqlite3_str_value(cli_output_capture));
32247      }
32248      bKeep = 1;
32249    }else if( z[0]=='-'
32250          && (k = pickStr(&z[1],0,"glob","notglob","exact",""))>=0
32251    ){
32252      if( eCheck && eCheck!=k+1 ){
32253        dotCmdError(p, i, "incompatible with prior options",0);
32254        return 1;
32255      }
32256      eCheck = k+1;
32257    }else if( zCheck ){
32258      dotCmdError(p, i, "unknown option", 0);
32259      return 1;
32260    }else{
32261      zCheck = azArg[i];
32262    }
32263  }
32264  if( zCheck==0 ){
32265    dotCmdError(p, 0, "no PATTERN specified", 0);
32266    return 1;
32267  }
32268  if( cli_output_capture && sqlite3_str_length(cli_output_capture) ){
32269    zTest = sqlite3_str_value(cli_output_capture);
32270    shell_check_oom(zTest);
32271  }else{
32272    zTest = "";
32273  }
32274  p->nTestRun++;
32275  if( zCheck[0]=='<' && zCheck[1]=='<' && zCheck[2]!=0 ){
32276    int nCheck = strlen30(zCheck);
32277    sqlite3_str *pPattern = sqlite3_str_new(p->db);
32278    char zLine[2000];
32279    while( sqlite3_fgets(zLine,sizeof(zLine),p->in) ){
32280      if( strchr(zLine,'\n') ) p->lineno++;
32281      if( cli_strncmp(&zCheck[2],zLine,nCheck-2)==0 ) break;
32282      sqlite3_str_appendall(pPattern, zLine);
32283    }
32284    zPattern = sqlite3_str_finish(pPattern);
32285    if( zPattern==0 ){
32286      zPattern = sqlite3_mprintf("");
32287    }
32288  }else{
32289    zPattern = zCheck;
32290  }
32291  shell_check_oom(zPattern);
32292  switch( eCheck ){
32293    case 1: {
32294      char *zGlob = sqlite3_mprintf("*%s*", zPattern);
32295      isOk = testcase_glob(zGlob, zTest)!=0;
32296      sqlite3_free(zGlob);
32297      break;
32298    }
32299    case 2: {
32300      char *zGlob = sqlite3_mprintf("*%s*", zPattern);
32301      isOk = testcase_glob(zGlob, zTest)==0;
32302      sqlite3_free(zGlob);
32303      break;
32304    }
32305    case 3: {
32306      isOk = cli_strcmp(zTest,zPattern)==0;
32307      break;
32308    }
32309    default: {
32310      /* Skip leading and trailing \n and \r on both pattern and test output */
32311      const char *z1 = zPattern;
32312      const char *z2 = zTest;
32313      size_t n1, n2;
32314      while( z1[0]=='\n' || z1[0]=='\r' ) z1++;
32315      n1 = strlen(z1);
32316      while( n1>0 && (z1[n1-1]=='\n' || z1[n1-1]=='\r') ) n1--;
32317      while( z2[0]=='\n' || z2[0]=='\r' ) z2++;
32318      n2 = strlen(z2);
32319      while( n2>0 && (z2[n2-1]=='\n' || z2[n2-1]=='\r') ) n2--;
32320      isOk = n1==n2 && memcmp(z1,z2,n1)==0;
32321      break;
32322    }
32323  }
32324  if( !isOk ){
32325    sqlite3_fprintf(stderr,
32326          "%s:%lld: .check failed for testcase %s\n",
32327        p->zInFile, iStart, p->zTestcase);
32328    p->nTestErr++;
32329    sqlite3_fprintf(stderr, "Expected: [%s]\n", zPattern);
32330    sqlite3_fprintf(stderr, "Got:      [%s]\n", zTest);
32331  }
32332  if( zPattern!=zCheck ){
32333    sqlite3_free(zPattern);
32334  }
32335  if( !bKeep ){
32336    output_reset(p);
32337    p->zTestcase[0] = 0;
32338  }
32339  return 0;
32340}
32341
32342/*
32343** DOT-COMMAND: .testcase
32344** USAGE: .testcase [OPTIONS] NAME
32345**
32346** Start a new test case identified by NAME.  All output
32347** through the next ".check" command is captured for comparison. See the
32348** ".check" commandn for additional informatioon.
32349**
32350** Options:
32351**   --error-prefix TEXT       Change error message prefix text to TEXT
32352*/
32353static int dotCmdTestcase(ShellState *p){
32354  int nArg = p->dot.nArg;        /* Number of arguments */
32355  char **azArg = p->dot.azArg;   /* Text of the arguments */
32356  int i;                         /* Loop counter */
32357  const char *zName = 0;         /* Testcase name */
32358
32359  for(i=1; i<nArg; i++){
32360    char *z = azArg[i];
32361    if( z[0]=='-' && z[1]=='-' && z[2]!=0 ) z++;
32362    if( optionMatch(z,"error-prefix") ){
32363      if( i+1>=nArg ){
32364        dotCmdError(p, i, "missing argument", 0);
32365        return 1;
32366      }
32367      free(p->zErrPrefix);
32368      i++;
32369      p->zErrPrefix = azArg[i][0]==0 ? 0 : strdup(azArg[i]);
32370    }else if( zName ){
32371      dotCmdError(p, i, "unknown option", 0);
32372      return 1;
32373    }else{
32374      zName = azArg[i];
32375    }
32376  }
32377  output_reset(p);
32378  if( cli_output_capture ){
32379    sqlite3_str_free(cli_output_capture);
32380  }
32381  cli_output_capture = sqlite3_str_new(0);
32382  if( zName ){
32383    sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", zName);
32384  }else{
32385    sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s:%lld",
32386                     p->zInFile, p->lineno);
32387  }
32388  return 0;
32389}
32390
32391/*
32392** Enlarge the space allocated in p->dot so that it can hold more
32393** than nArg parsed command-line arguments.
32394*/
32395static void parseDotRealloc(ShellState *p, int nArg){
32396  p->dot.nAlloc = nArg+22;
32397  p->dot.azArg = realloc(p->dot.azArg,p->dot.nAlloc*sizeof(char*));
32398  shell_check_oom(p->dot.azArg);
32399  p->dot.aiOfst = realloc(p->dot.aiOfst,p->dot.nAlloc*sizeof(int));
32400  shell_check_oom(p->dot.aiOfst);
32401  p->dot.abQuot = realloc(p->dot.abQuot,p->dot.nAlloc);
32402  shell_check_oom(p->dot.abQuot);
32403}
32404
32405
32406/*
32407** Parse input line zLine up into individual arguments.  Retain the
32408** parse in the p->dot substructure.
32409*/
32410static void parseDotCmdArgs(const char *zLine, ShellState *p){
32411  char *z;
32412  int h = 1;
32413  int nArg = 0;
32414  size_t szLine;
32415
32416  p->dot.zOrig = zLine;
32417  free(p->dot.zCopy);
32418  z = p->dot.zCopy = strdup(zLine);
32419  shell_check_oom(z);
32420  szLine = strlen(z);
32421  while( szLine>0 && IsSpace(z[szLine-1]) ) szLine--;
32422  if( szLine>0 && z[szLine-1]==';' ){
32423    szLine--;
32424    while( szLine>0 && IsSpace(z[szLine-1]) ) szLine--;
32425  }
32426  z[szLine] = 0;
32427  parseDotRealloc(p, 2);
32428  while( z[h] ){
32429    while( IsSpace(z[h]) ){ h++; }
32430    if( z[h]==0 ) break;
32431    if( nArg+2>p->dot.nAlloc ){
32432      parseDotRealloc(p, nArg);
32433    }
32434    if( z[h]=='\'' || z[h]=='"' ){
32435      int delim = z[h++];
32436      p->dot.abQuot[nArg] = 1;
32437      p->dot.azArg[nArg] = &z[h];
32438      p->dot.aiOfst[nArg] = h;
32439      while( z[h] && z[h]!=delim ){
32440        if( z[h]=='\\' && delim=='"' && z[h+1]!=0 ) h++;
32441        h++;
32442      }
32443      if( z[h]==delim ){
32444        z[h++] = 0;
32445      }
32446      if( delim=='"' ) resolve_backslashes(p->dot.azArg[nArg]);
32447    }else{
32448      p->dot.abQuot[nArg] = 0;
32449      p->dot.azArg[nArg] = &z[h];
32450      p->dot.aiOfst[nArg] = h;
32451      while( z[h] && !IsSpace(z[h]) ){ h++; }
32452      if( z[h] ) z[h++] = 0;
32453    }
32454    nArg++;
32455  }
32456  p->dot.nArg = nArg;
32457  p->dot.azArg[nArg] = 0;
32458}
32459
32460/*
32461** If an input line begins with "." then invoke this routine to
32462** process that line.
32463**
32464** Return 1 on error, 2 to exit, and 0 otherwise.
32465*/
32466static int do_meta_command(const char *zLine, ShellState *p){
32467  int nArg;
32468  int n, c;
32469  int rc = 0;
32470  char **azArg;
32471
32472#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
32473  if( p->expert.pExpert ){
32474    expertFinish(p, 1, 0);
32475  }
32476#endif
32477
32478  /* Parse the input line into tokens stored in p->dot.
32479  */
32480  parseDotCmdArgs(zLine, p);
32481  nArg = p->dot.nArg;
32482  azArg = p->dot.azArg;
32483
32484  /* Process the input line.
32485  */
32486  if( nArg==0 ) return 0; /* no tokens, no error */
32487  n = strlen30(azArg[0]);
32488  c = azArg[0][0];
32489  clearTempFile(p);
32490
32491#ifndef SQLITE_OMIT_AUTHORIZATION
32492  if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
32493    if( nArg!=2 ){
32494      cli_printf(stderr, "Usage: .auth ON|OFF\n");
32495      rc = 1;
32496      goto meta_command_exit;
32497    }
32498    open_db(p, 0);
32499    if( booleanValue(azArg[1]) ){
32500      sqlite3_set_authorizer(p->db, shellAuth, p);
32501    }else if( p->bSafeModePersist ){
32502      sqlite3_set_authorizer(p->db, safeModeAuth, p);
32503    }else{
32504      sqlite3_set_authorizer(p->db, 0, 0);
32505    }
32506  }else
32507#endif
32508
32509#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
32510  && !defined(SQLITE_SHELL_FIDDLE)
32511  if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){
32512    open_db(p, 0);
32513    failIfSafeMode(p, "cannot run .archive in safe mode");
32514    rc = arDotCommand(p, 0, azArg, nArg);
32515  }else
32516#endif
32517
32518#ifndef SQLITE_SHELL_FIDDLE
32519  if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0)
32520   || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0)
32521  ){
32522    const char *zDestFile = 0;
32523    const char *zDb = 0;
32524    sqlite3 *pDest;
32525    sqlite3_backup *pBackup;
32526    int j;
32527    int bAsync = 0;
32528    const char *zVfs = 0;
32529    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
32530    for(j=1; j<nArg; j++){
32531      const char *z = azArg[j];
32532      if( z[0]=='-' ){
32533        if( z[1]=='-' ) z++;
32534        if( cli_strcmp(z, "-append")==0 ){
32535          zVfs = "apndvfs";
32536        }else
32537        if( cli_strcmp(z, "-async")==0 ){
32538          bAsync = 1;
32539        }else
32540        {
32541          dotCmdError(p, j, "unknown option", "should be -append or -async");
32542          return 1;
32543        }
32544      }else if( zDestFile==0 ){
32545        zDestFile = azArg[j];
32546      }else if( zDb==0 ){
32547        zDb = zDestFile;
32548        zDestFile = azArg[j];
32549      }else{
32550        cli_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
32551        return 1;
32552      }
32553    }
32554    if( zDestFile==0 ){
32555      cli_printf(stderr, "missing FILENAME argument on .backup\n");
32556      return 1;
32557    }
32558    if( zDb==0 ) zDb = "main";
32559    rc = sqlite3_open_v2(zDestFile, &pDest,
32560                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
32561    if( rc!=SQLITE_OK ){
32562      cli_printf(stderr,"Error: cannot open \"%s\"\n", zDestFile);
32563      close_db(pDest);
32564      return 1;
32565    }
32566    if( bAsync ){
32567      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
32568                   0, 0, 0);
32569    }
32570    open_db(p, 0);
32571    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
32572    if( pBackup==0 ){
32573      shellDatabaseError(pDest);
32574      close_db(pDest);
32575      return 1;
32576    }
32577    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
32578    sqlite3_backup_finish(pBackup);
32579    if( rc==SQLITE_DONE ){
32580      rc = 0;
32581    }else{
32582      shellDatabaseError(pDest);
32583      rc = 1;
32584    }
32585    close_db(pDest);
32586  }else
32587#endif /* !defined(SQLITE_SHELL_FIDDLE) */
32588
32589  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
32590    if( nArg==2 ){
32591      bail_on_error = booleanValue(azArg[1]);
32592    }else{
32593      eputz("Usage: .bail on|off\n");
32594      rc = 1;
32595    }
32596  }else
32597
32598  /* Undocumented.  Legacy only.  See "crlf" below */
32599  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
32600    eputz("The \".binary\" command is deprecated.\n");
32601    rc = 1;
32602  }else
32603
32604  /* The undocumented ".breakpoint" command causes a call to the no-op
32605  ** routine named test_breakpoint().
32606  */
32607  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){
32608    test_breakpoint();
32609  }else
32610
32611#ifndef SQLITE_SHELL_FIDDLE
32612  if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){
32613    failIfSafeMode(p, "cannot run .cd in safe mode");
32614    if( nArg==2 ){
32615#if defined(_WIN32) || defined(WIN32)
32616      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
32617      rc = !SetCurrentDirectoryW(z);
32618      sqlite3_free(z);
32619#else
32620      rc = chdir(azArg[1]);
32621#endif
32622      if( rc ){
32623        cli_printf(stderr,"Cannot change to directory \"%s\"\n", azArg[1]);
32624        rc = 1;
32625      }
32626    }else{
32627      eputz("Usage: .cd DIRECTORY\n");
32628      rc = 1;
32629    }
32630  }else
32631#endif /* !defined(SQLITE_SHELL_FIDDLE) */
32632
32633  if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
32634    if( nArg==2 ){
32635      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
32636    }else{
32637      eputz("Usage: .changes on|off\n");
32638      rc = 1;
32639    }
32640  }else
32641
32642  /* Cancel output redirection, if it is currently set (by .testcase)
32643  ** Then read the content of the testcase-out.txt file and compare against
32644  ** azArg[1].  If there are differences, report an error and exit.
32645  */
32646  if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
32647    rc = dotCmdCheck(p);
32648  }else
32649
32650#ifndef SQLITE_SHELL_FIDDLE
32651  if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
32652    failIfSafeMode(p, "cannot run .clone in safe mode");
32653    if( nArg==2 ){
32654      tryToClone(p, azArg[1]);
32655    }else{
32656      eputz("Usage: .clone FILENAME\n");
32657      rc = 1;
32658    }
32659  }else
32660#endif /* !defined(SQLITE_SHELL_FIDDLE) */
32661
32662  if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
32663    if( nArg==1 ){
32664      /* List available connections */
32665      int i;
32666      for(i=0; i<ArraySize(p->aAuxDb); i++){
32667        const char *zFile = p->aAuxDb[i].zDbFilename;
32668        if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
32669          zFile = "(not open)";
32670        }else if( zFile==0 ){
32671          zFile = "(memory)";
32672        }else if( zFile[0]==0 ){
32673          zFile = "(temporary-file)";
32674        }
32675        if( p->pAuxDb == &p->aAuxDb[i] ){
32676          cli_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
32677        }else if( p->aAuxDb[i].db!=0 ){
32678          cli_printf(stdout, "       %d: %s\n", i, zFile);
32679        }
32680      }
32681    }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
32682      int i = azArg[1][0] - '0';
32683      if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
32684        p->pAuxDb->db = p->db;
32685        p->pAuxDb = &p->aAuxDb[i];
32686        globalDb = p->db = p->pAuxDb->db;
32687        p->pAuxDb->db = 0;
32688      }
32689    }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0
32690           && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
32691      int i = azArg[2][0] - '0';
32692      if( i<0 || i>=ArraySize(p->aAuxDb) ){
32693        /* No-op */
32694      }else if( p->pAuxDb == &p->aAuxDb[i] ){
32695        eputz("cannot close the active database connection\n");
32696        rc = 1;
32697      }else if( p->aAuxDb[i].db ){
32698        session_close_all(p, i);
32699        close_db(p->aAuxDb[i].db);
32700        p->aAuxDb[i].db = 0;
32701      }
32702    }else{
32703      eputz("Usage: .connection [close] [CONNECTION-NUMBER]\n");
32704      rc = 1;
32705    }
32706  }else
32707
32708  if( c=='c' && n==4
32709   && (cli_strncmp(azArg[0], "crlf", n)==0
32710       || cli_strncmp(azArg[0], "crnl",n)==0)
32711  ){
32712    if( nArg==2 ){
32713#ifdef _WIN32
32714      if( booleanValue(azArg[1]) ){
32715        p->mode.mFlags |= MFLG_CRLF;
32716      }else{
32717        p->mode.mFlags &= ~MFLG_CRLF;
32718      }
32719#else
32720      p->mode.mFlags &= ~MFLG_CRLF;
32721#endif
32722    }
32723    cli_printf(stderr, "crlf is %s\n", 
32724       (p->mode.mFlags & MFLG_CRLF)!=0 ? "ON" : "OFF");
32725  }else
32726
32727  if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
32728    char **azName = 0;
32729    int nName = 0;
32730    sqlite3_stmt *pStmt;
32731    int i;
32732    open_db(p, 0);
32733    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
32734    if( rc ){
32735      shellDatabaseError(p->db);
32736      rc = 1;
32737    }else{
32738      while( sqlite3_step(pStmt)==SQLITE_ROW ){
32739        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
32740        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
32741        if( zSchema==0 || zFile==0 ) continue;
32742        azName = sqlite3_realloc64(azName, (nName+1)*2*sizeof(char*));
32743        shell_check_oom(azName);
32744        azName[nName*2] = strdup(zSchema);
32745        azName[nName*2+1] = strdup(zFile);
32746        nName++;
32747      }
32748    }
32749    sqlite3_finalize(pStmt);
32750    for(i=0; i<nName; i++){
32751      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
32752      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
32753      const char *z = azName[i*2+1];
32754      cli_printf(p->out, "%s: %s %s%s\n",
32755            azName[i*2], z && z[0] ? z : "\"\"", bRdonly ? "r/o" : "r/w",
32756            eTxn==SQLITE_TXN_NONE ? "" :
32757            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
32758      free(azName[i*2]);
32759      free(azName[i*2+1]);
32760    }
32761    sqlite3_free(azName);
32762  }else
32763
32764  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){
32765    static const struct DbConfigChoices {
32766      const char *zName;
32767      int op;
32768    } aDbConfig[] = {
32769        { "attach_create",      SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE  },
32770        { "attach_write",       SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE   },
32771        { "comments",           SQLITE_DBCONFIG_ENABLE_COMMENTS       },
32772        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
32773        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
32774        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
32775        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
32776        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
32777        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
32778        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
32779        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
32780        { "fp_digits",          SQLITE_DBCONFIG_FP_DIGITS             },
32781        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
32782        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
32783        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
32784        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
32785        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
32786        { "reverse_scanorder",  SQLITE_DBCONFIG_REVERSE_SCANORDER     },
32787        { "stmt_scanstatus",    SQLITE_DBCONFIG_STMT_SCANSTATUS       },
32788        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
32789        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
32790        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
32791    };
32792    int ii, v;
32793    open_db(p, 0);
32794    for(ii=0; ii<ArraySize(aDbConfig); ii++){
32795      if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
32796      if( nArg>=3 ){
32797        if( aDbConfig[ii].op==SQLITE_DBCONFIG_FP_DIGITS ){
32798          sqlite3_db_config(p->db, aDbConfig[ii].op, atoi(azArg[2]), 0);
32799        }else{
32800          sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
32801        }
32802      }
32803      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
32804      if( aDbConfig[ii].op==SQLITE_DBCONFIG_FP_DIGITS ){
32805        cli_printf(p->out, "%19s %d\n", aDbConfig[ii].zName, v);
32806      }else{
32807        cli_printf(p->out, "%19s %s\n",
32808                        aDbConfig[ii].zName, v ? "on" : "off");
32809      }
32810      if( nArg>1 ) break;
32811    }
32812    if( nArg>1 && ii==ArraySize(aDbConfig) ){
32813      dotCmdError(p, 1, "unknown dbconfig",
32814         "Enter \".dbconfig\" with no arguments for a list");
32815    }
32816  }else
32817
32818#if SQLITE_SHELL_HAVE_RECOVER
32819  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
32820    rc = shell_dbinfo_command(p, nArg, azArg);
32821  }else
32822
32823  if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
32824    open_db(p, 0);
32825    rc = recoverDatabaseCmd(p, nArg, azArg);
32826  }else
32827#endif /* SQLITE_SHELL_HAVE_RECOVER */
32828
32829  if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
32830    char *zLike = 0;
32831    char *zSql;
32832    int i;
32833    int savedShellFlags = p->shellFlgs;
32834    Mode saved_mode;
32835    ShellClearFlag(p,
32836       SHFLG_PreserveRowid|SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
32837    for(i=1; i<nArg; i++){
32838      if( azArg[i][0]=='-' ){
32839        const char *z = azArg[i]+1;
32840        if( z[0]=='-' ) z++;
32841        if( cli_strcmp(z,"preserve-rowids")==0 ){
32842#ifdef SQLITE_OMIT_VIRTUALTABLE
32843          dotCmdError(p, i, "unable",
32844            "The --preserve-rowids option is not compatible"
32845                " with SQLITE_OMIT_VIRTUALTABLE");
32846          rc = 1;
32847          sqlite3_free(zLike);
32848          goto meta_command_exit;
32849#else
32850          ShellSetFlag(p, SHFLG_PreserveRowid);
32851#endif
32852        }else
32853        if( cli_strcmp(z,"newlines")==0 ){
32854          /*ShellSetFlag(p, SHFLG_Newlines);*/
32855        }else
32856        if( cli_strcmp(z,"data-only")==0 ){
32857          ShellSetFlag(p, SHFLG_DumpDataOnly);
32858        }else
32859        if( cli_strcmp(z,"nosys")==0 ){
32860          ShellSetFlag(p, SHFLG_DumpNoSys);
32861        }else
32862        {
32863          dotCmdError(p, i, "unknown option", 0);
32864          rc = 1;
32865          sqlite3_free(zLike);
32866          goto meta_command_exit;
32867        }
32868      }else{
32869        /* azArg[i] contains a LIKE pattern. This ".dump" request should
32870        ** only dump data for tables for which either the table name matches
32871        ** the LIKE pattern, or the table appears to be a shadow table of
32872        ** a virtual table for which the name matches the LIKE pattern.
32873        */
32874        char *zExpr = sqlite3_mprintf(
32875            "name LIKE %Q ESCAPE '\\' OR EXISTS ("
32876            "  SELECT 1 FROM sqlite_schema WHERE "
32877            "    name LIKE %Q ESCAPE '\\' AND"
32878            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
32879            "    substr(o.name, 1, length(name)+1) == (name||'_')"
32880            ")", azArg[i], azArg[i]
32881        );
32882
32883        if( zLike ){
32884          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
32885        }else{
32886          zLike = zExpr;
32887        }
32888      }
32889    }
32890
32891    open_db(p, 0);
32892
32893    modeDup(&saved_mode, &p->mode);
32894    outputDumpWarning(p, zLike);
32895    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
32896      /* When playing back a "dump", the content might appear in an order
32897      ** which causes immediate foreign key constraints to be violated.
32898      ** So disable foreign-key constraint enforcement to prevent problems. */
32899      cli_puts("PRAGMA foreign_keys=OFF;\n", p->out);
32900      cli_puts("BEGIN TRANSACTION;\n", p->out);
32901    }
32902    p->writableSchema = 0;
32903    p->mode.spec.bTitles = QRF_No;
32904    /* Set writable_schema=ON since doing so forces SQLite to initialize
32905    ** as much of the schema as it can even if the sqlite_schema table is
32906    ** corrupt. */
32907    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
32908    p->nErr = 0;
32909    if( zLike==0 ) zLike = sqlite3_mprintf("true");
32910    zSql = sqlite3_mprintf(
32911      "SELECT name, type, sql FROM sqlite_schema AS o "
32912      "WHERE (%s) AND type=='table'"
32913      "  AND sql NOT NULL"
32914      " ORDER BY tbl_name='sqlite_sequence', rowid",
32915      zLike
32916    );
32917    run_schema_dump_query(p,zSql);
32918    sqlite3_free(zSql);
32919    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
32920      zSql = sqlite3_mprintf(
32921        "SELECT sql FROM sqlite_schema AS o "
32922        "WHERE (%s) AND sql NOT NULL"
32923        "  AND type IN ('index','trigger','view') "
32924        "ORDER BY type COLLATE NOCASE DESC",
32925        zLike
32926      );
32927      run_table_dump_query(p, zSql);
32928      sqlite3_free(zSql);
32929    }
32930    sqlite3_free(zLike);
32931    if( p->writableSchema ){
32932      cli_puts("PRAGMA writable_schema=OFF;\n", p->out);
32933      p->writableSchema = 0;
32934    }
32935    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
32936    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
32937    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
32938      cli_puts(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n", p->out);
32939    }
32940    p->shellFlgs = savedShellFlags;
32941    modeFree(&p->mode);
32942    p->mode = saved_mode;
32943    rc = p->nErr>0;
32944  }else
32945
32946  if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
32947    if( nArg==2 ){
32948      if( booleanValue(azArg[1]) ){
32949        p->mode.mFlags |= MFLG_ECHO;
32950      }else{
32951        p->mode.mFlags &= ~MFLG_ECHO;
32952      }
32953    }else{
32954      eputz("Usage: .echo on|off\n");
32955      rc = 1;
32956    }
32957  }else
32958
32959  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbtotxt", n)==0 ){
32960    open_db(p, 0);
32961    rc = shell_dbtotxt_command(p, nArg, azArg);
32962  }else
32963
32964  if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
32965    if( nArg==2 ){
32966      if( p->mode.autoEQPtrace ){
32967        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
32968        p->mode.autoEQPtrace = 0;
32969      }
32970      if( cli_strcmp(azArg[1],"full")==0 ){
32971        p->mode.autoEQP = AUTOEQP_full;
32972      }else if( cli_strcmp(azArg[1],"trigger")==0 ){
32973        p->mode.autoEQP = AUTOEQP_trigger;
32974#ifdef SQLITE_DEBUG
32975      }else if( cli_strcmp(azArg[1],"trace")==0 ){
32976        p->mode.autoEQP = AUTOEQP_full;
32977        p->mode.autoEQPtrace = 1;
32978        open_db(p, 0);
32979        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
32980        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
32981#endif
32982      }else{
32983        p->mode.autoEQP = (u8)booleanValue(azArg[1]);
32984      }
32985    }else{
32986      eputz("Usage: .eqp off|on|trace|trigger|full\n");
32987      rc = 1;
32988    }
32989  }else
32990
32991#ifndef SQLITE_SHELL_FIDDLE
32992  if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){
32993    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) cli_exit(rc);
32994    rc = 2;
32995  }else
32996#endif
32997
32998  /* The ".explain" command is automatic now.  It is largely pointless.  It
32999  ** retained purely for backwards compatibility */
33000  if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){
33001    if( nArg>=2 ){
33002      if( cli_strcmp(azArg[1],"auto")==0 ){
33003        p->mode.autoExplain = 1;
33004      }else{
33005        p->mode.autoExplain = booleanValue(azArg[1]);
33006      }
33007    }
33008  }else
33009
33010#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
33011  if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
33012    if( p->bSafeMode ){
33013      cli_printf(stderr,
33014            "Cannot run experimental commands such as \"%s\" in safe mode\n",
33015            azArg[0]);
33016      rc = 1;
33017    }else{
33018      open_db(p, 0);
33019      expertDotCommand(p, azArg, nArg);
33020    }
33021  }else
33022#endif
33023
33024  if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){
33025    static const struct {
33026       const char *zCtrlName;   /* Name of a test-control option */
33027       int ctrlCode;            /* Integer code for that option */
33028       const char *zUsage;      /* Usage notes */
33029    } aCtrl[] = {
33030      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
33031      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
33032      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
33033      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
33034      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
33035   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
33036      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
33037      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
33038      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
33039      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
33040   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
33041    };
33042    int filectrl = -1;
33043    int iCtrl = -1;
33044    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
33045    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
33046    int n2, i;
33047    const char *zCmd = 0;
33048    const char *zSchema = 0;
33049
33050    open_db(p, 0);
33051    zCmd = nArg>=2 ? azArg[1] : "help";
33052
33053    if( zCmd[0]=='-'
33054     && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0)
33055     && nArg>=4
33056    ){
33057      zSchema = azArg[2];
33058      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
33059      nArg -= 2;
33060      zCmd = azArg[1];
33061    }
33062
33063    /* The argument can optionally begin with "-" or "--" */
33064    if( zCmd[0]=='-' && zCmd[1] ){
33065      zCmd++;
33066      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
33067    }
33068
33069    /* --help lists all file-controls */
33070    if( cli_strcmp(zCmd,"help")==0 ){
33071      cli_puts("Available file-controls:\n", p->out);
33072      for(i=0; i<ArraySize(aCtrl); i++){
33073        cli_printf(p->out,
33074               "  .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage);
33075      }
33076      rc = 1;
33077      goto meta_command_exit;
33078    }
33079
33080    /* convert filectrl text option to value. allow any unique prefix
33081    ** of the option name, or a numerical value. */
33082    n2 = strlen30(zCmd);
33083    for(i=0; i<ArraySize(aCtrl); i++){
33084      if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
33085        if( filectrl<0 ){
33086          filectrl = aCtrl[i].ctrlCode;
33087          iCtrl = i;
33088        }else{
33089          cli_printf(stderr,"Error: ambiguous file-control: \"%s\"\n"
33090                "Use \".filectrl --help\" for help\n", zCmd);
33091          rc = 1;
33092          goto meta_command_exit;
33093        }
33094      }
33095    }
33096    if( filectrl<0 ){
33097      cli_printf(stderr,"Error: unknown file-control: %s\n"
33098            "Use \".filectrl --help\" for help\n", zCmd);
33099    }else{
33100      switch(filectrl){
33101        case SQLITE_FCNTL_SIZE_LIMIT: {
33102          if( nArg!=2 && nArg!=3 ) break;
33103          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
33104          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
33105          isOk = 1;
33106          break;
33107        }
33108        case SQLITE_FCNTL_LOCK_TIMEOUT:
33109        case SQLITE_FCNTL_CHUNK_SIZE: {
33110          int x;
33111          if( nArg!=3 ) break;
33112          x = (int)integerValue(azArg[2]);
33113          sqlite3_file_control(p->db, zSchema, filectrl, &x);
33114          isOk = 2;
33115          break;
33116        }
33117        case SQLITE_FCNTL_PERSIST_WAL:
33118        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
33119          int x;
33120          if( nArg!=2 && nArg!=3 ) break;
33121          x = nArg==3 ? booleanValue(azArg[2]) : -1;
33122          sqlite3_file_control(p->db, zSchema, filectrl, &x);
33123          iRes = x;
33124          isOk = 1;
33125          break;
33126        }
33127        case SQLITE_FCNTL_DATA_VERSION:
33128        case SQLITE_FCNTL_HAS_MOVED: {
33129          int x;
33130          if( nArg!=2 ) break;
33131          sqlite3_file_control(p->db, zSchema, filectrl, &x);
33132          iRes = x;
33133          isOk = 1;
33134          break;
33135        }
33136        case SQLITE_FCNTL_TEMPFILENAME: {
33137          char *z = 0;
33138          if( nArg!=2 ) break;
33139          sqlite3_file_control(p->db, zSchema, filectrl, &z);
33140          if( z ){
33141            cli_printf(p->out, "%s\n", z);
33142            sqlite3_free(z);
33143          }
33144          isOk = 2;
33145          break;
33146        }
33147        case SQLITE_FCNTL_RESERVE_BYTES: {
33148          int x;
33149          if( nArg>=3 ){
33150            x = atoi(azArg[2]);
33151            sqlite3_file_control(p->db, zSchema, filectrl, &x);
33152          }
33153          x = -1;
33154          sqlite3_file_control(p->db, zSchema, filectrl, &x);
33155          cli_printf(p->out, "%d\n", x);
33156          isOk = 2;
33157          break;
33158        }
33159      }
33160    }
33161    if( isOk==0 && iCtrl>=0 ){
33162      cli_printf(p->out, "Usage: .filectrl %s %s\n",
33163                      zCmd, aCtrl[iCtrl].zUsage);
33164      rc = 1;
33165    }else if( isOk==1 ){
33166      char zBuf[100];
33167      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
33168      cli_printf(p->out, "%s\n", zBuf);
33169    }
33170  }else
33171
33172  if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
33173    ShellState data;
33174    int doStats = 0;
33175    int hasStat[5];
33176    int flgs = 0;
33177    char *zSql;
33178    if( nArg==2 && optionMatch(azArg[1], "indent") ){
33179      nArg = 1;
33180    }
33181    if( nArg!=1 ){
33182      eputz("Usage: .fullschema ?--indent?\n");
33183      rc = 1;
33184      goto meta_command_exit;
33185    }
33186    open_db(p, 0);
33187    zSql = sqlite3_mprintf(
33188       "SELECT shell_format_schema(sql,%d) FROM"
33189       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
33190       "     FROM sqlite_schema UNION ALL"
33191       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
33192       "WHERE type!='meta' AND sql NOTNULL"
33193       "  AND name NOT LIKE 'sqlite__%%' ESCAPE '_' "
33194       "ORDER BY x", flgs);
33195    memcpy(&data, p, sizeof(data));
33196    data.mode.spec.bTitles = QRF_No;
33197    data.mode.eMode = MODE_List;
33198    data.mode.spec.eText = QRF_TEXT_Plain;
33199    data.mode.spec.nCharLimit = 0;
33200    data.mode.spec.zRowSep = "\n";
33201    rc = shell_exec(&data,zSql,0);
33202    sqlite3_free(zSql);
33203    if( rc==SQLITE_OK ){
33204      sqlite3_stmt *pStmt;
33205      memset(hasStat, 0, sizeof(hasStat));
33206      rc = sqlite3_prepare_v2(p->db,
33207               "SELECT substr(name,12,1) FROM sqlite_schema"
33208               " WHERE name GLOB 'sqlite_stat[134]'",
33209               -1, &pStmt, 0);
33210      if( rc==SQLITE_OK ){
33211        while( sqlite3_step(pStmt)==SQLITE_ROW ){
33212          int k = sqlite3_column_int(pStmt,0);
33213          assert( k==1 || k==3 || k==4 );
33214          hasStat[k] = 1;
33215          doStats = 1;
33216        }
33217      }
33218      sqlite3_finalize(pStmt);
33219    }
33220    if( doStats==0 ){
33221      cli_puts("/* No STAT tables available */\n", p->out);
33222    }else{
33223      cli_puts("ANALYZE sqlite_schema;\n", p->out);
33224      data.mode.eMode = MODE_Insert;
33225      if( hasStat[1] ){
33226        data.mode.spec.zTableName = "sqlite_stat1";
33227        shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
33228      }
33229      if( hasStat[4] ){
33230        data.mode.spec.zTableName = "sqlite_stat4";
33231        shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
33232      }
33233      cli_puts("ANALYZE sqlite_schema;\n", p->out);
33234    }
33235  }else
33236
33237  if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
33238    if( nArg==2 ){
33239      p->mode.spec.bTitles = booleanValue(azArg[1]) ? QRF_Yes : QRF_No;
33240      p->mode.mFlags |= MFLG_HDR;
33241      p->mode.spec.eTitle = aModeInfo[p->mode.eMode].eHdr;
33242    }else{
33243      eputz("Usage: .headers on|off\n");
33244      rc = 1;
33245    }
33246  }else
33247
33248  if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
33249    if( nArg>=2 ){
33250      n = showHelp(p->out, azArg[1]);
33251      if( n==0 ){
33252        cli_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
33253      }
33254    }else{
33255      showHelp(p->out, 0);
33256    }
33257  }else
33258
33259#ifndef SQLITE_SHELL_FIDDLE
33260  if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
33261    rc = dotCmdImport(p);
33262  }else
33263#endif /* !defined(SQLITE_SHELL_FIDDLE) */
33264
33265#ifndef SQLITE_UNTESTABLE
33266  if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){
33267    char *zSql;
33268    char *zCollist = 0;
33269    sqlite3_stmt *pStmt;
33270    int tnum = 0;
33271    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
33272    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
33273    int i;
33274    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
33275      eputz("Usage: .imposter INDEX IMPOSTER\n"
33276            "       .imposter off\n");
33277      /* Also allowed, but not documented:
33278      **
33279      **    .imposter TABLE IMPOSTER
33280      **
33281      ** where TABLE is a WITHOUT ROWID table.  In that case, the
33282      ** imposter is another WITHOUT ROWID table with the columns in
33283      ** storage order. */
33284      rc = 1;
33285      goto meta_command_exit;
33286    }
33287    open_db(p, 0);
33288    if( nArg==2 ){
33289      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
33290      goto meta_command_exit;
33291    }
33292    zSql = sqlite3_mprintf(
33293      "SELECT rootpage, 0 FROM sqlite_schema"
33294      " WHERE type='index' AND lower(name)=lower('%q')"
33295      "UNION ALL "
33296      "SELECT rootpage, 1 FROM sqlite_schema"
33297      " WHERE type='table' AND lower(name)=lower('%q')"
33298      "   AND sql LIKE '%%without%%rowid%%'",
33299      azArg[1], azArg[1]
33300    );
33301    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
33302    sqlite3_free(zSql);
33303    if( sqlite3_step(pStmt)==SQLITE_ROW ){
33304      tnum = sqlite3_column_int(pStmt, 0);
33305      isWO = sqlite3_column_int(pStmt, 1);
33306    }
33307    sqlite3_finalize(pStmt);
33308    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
33309    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
33310    sqlite3_free(zSql);
33311    i = 0;
33312    while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
33313      char zLabel[20];
33314      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
33315      i++;
33316      if( zCol==0 ){
33317        if( sqlite3_column_int(pStmt,1)==-1 ){
33318          zCol = "_ROWID_";
33319        }else{
33320          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
33321          zCol = zLabel;
33322        }
33323      }
33324      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
33325        lenPK = (int)strlen(zCollist);
33326      }
33327      if( zCollist==0 ){
33328        zCollist = sqlite3_mprintf("\"%w\"", zCol);
33329      }else{
33330        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
33331      }
33332    }
33333    sqlite3_finalize(pStmt);
33334    if( i==0 || tnum==0 ){
33335      cli_printf(stderr,"no such index: \"%s\"\n", azArg[1]);
33336      rc = 1;
33337      sqlite3_free(zCollist);
33338      goto meta_command_exit;
33339    }
33340    if( lenPK==0 ) lenPK = 100000;
33341    zSql = sqlite3_mprintf(
33342          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
33343          azArg[2], zCollist, lenPK, zCollist);
33344    sqlite3_free(zCollist);
33345    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 2, tnum);
33346    if( rc==SQLITE_OK ){
33347      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
33348      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
33349      if( rc ){
33350        cli_printf(stderr,
33351              "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
33352      }else{
33353        cli_printf(stdout, "%s;\n", zSql);
33354      }
33355    }else{
33356      cli_printf(stderr,"SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
33357      rc = 1;
33358    }
33359    sqlite3_free(zSql);
33360  }else
33361#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
33362
33363  if( c=='i' && (cli_strncmp(azArg[0], "indices", n)==0
33364                 || cli_strncmp(azArg[0], "indexes", n)==0)
33365  ){
33366    sqlite3_str *pSql;
33367    int i;
33368    int allFlag = 0;
33369    int sysFlag = 0;
33370    int exprFlag = 0;
33371    int debugFlag = 0;  /* Undocument --debug flag */
33372    const char *zPattern = 0;
33373    const char *zSep = "WHERE";
33374
33375    for(i=1; i<nArg; i++){
33376      if( azArg[i][0]=='-' ){
33377        const char *z = azArg[i]+1;
33378        if( z[0]=='-' ) z++;
33379        if( cli_strcmp(z,"all")==0 || cli_strcmp(z,"a")==0 ){
33380          allFlag = 1;
33381        }else
33382        if( cli_strcmp(z,"sys")==0 ){
33383          sysFlag = 1;
33384          allFlag = 0;
33385        }else
33386        if( cli_strcmp(z,"expr")==0 ){
33387          exprFlag = 1;
33388          allFlag = 0;
33389        }else
33390        if( cli_strcmp(z,"debug")==0 ){
33391          debugFlag = 1;
33392        }else
33393        {
33394          dotCmdError(p, i, "unknown option", 0);
33395          rc = 1;
33396          goto meta_command_exit;
33397        }
33398      }else if( zPattern==0 ){
33399        zPattern = azArg[i];
33400      }else{
33401        dotCmdError(p, i, "unknown argument", 0);
33402        rc = 1;
33403        goto meta_command_exit;
33404      }
33405    }
33406
33407    open_db(p, 0);
33408    pSql = sqlite3_str_new(p->db);
33409    sqlite3_str_appendf(pSql,
33410      "SELECT if(t.schema='main',i.name,t.schema||'.'||i.name)\n"
33411      "FROM pragma_table_list t, pragma_index_list(t.name,t.schema) i\n"
33412    );
33413    if( exprFlag ){
33414      allFlag = 0;
33415      sqlite3_str_appendf(pSql,
33416        "%s (EXISTS(SELECT 1 FROM pragma_index_xinfo(i.name) WHERE cid=-2)\n"
33417        "       OR\n"
33418        "       EXISTS(SELECT cid FROM pragma_table_xinfo(t.name) WHERE hidden=2"
33419        " INTERSECT "
33420        " SELECT cid FROM pragma_index_info(i.name)))\n", zSep);
33421      zSep = "AND";
33422    }
33423    if( sysFlag ){
33424      sqlite3_str_appendf(pSql,
33425           "%s i.name LIKE 'sqlite__autoindex__%%' ESCAPE '_'\n", zSep);
33426      zSep = "AND";
33427    }else if( !allFlag ){
33428      sqlite3_str_appendf(pSql,
33429           "%s i.name NOT LIKE 'sqlite__%%' ESCAPE '_'\n", zSep);
33430      zSep = "AND";
33431    }
33432    if( zPattern ){
33433      sqlite3_str_appendf(pSql, "%s i.name LIKE '%%%q%%'\n", zSep, zPattern);
33434    }
33435    sqlite3_str_appendf(pSql, "ORDER BY 1");
33436
33437    /* Run the SQL statement in "split" mode. */
33438    if( debugFlag ){
33439      cli_printf(stdout,"%s;\n", sqlite3_str_value(pSql));
33440    }else{
33441      modePush(p);
33442      modeChange(p, MODE_Split);
33443      shell_exec(p, sqlite3_str_value(pSql), 0);
33444      modePop(p);
33445    }
33446    sqlite3_str_free(pSql);
33447  }else
33448
33449  if( c=='i' && cli_strncmp(azArg[0], "intck", n)==0 ){
33450    i64 iArg = 0;
33451    if( nArg==2 ){
33452      iArg = integerValue(azArg[1]);
33453      if( iArg==0 ) iArg = -1;
33454    }
33455    if( (nArg!=1 && nArg!=2) || iArg<0 ){
33456      cli_printf(stderr,"%s","Usage: .intck STEPS_PER_UNLOCK\n");
33457      rc = 1;
33458      goto meta_command_exit;
33459    }
33460    open_db(p, 0);
33461    rc = intckDatabaseCmd(p, iArg);
33462  }else
33463
33464#ifdef SQLITE_ENABLE_IOTRACE
33465  if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){
33466    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
33467    if( iotrace && iotrace!=stdout ) fclose(iotrace);
33468    iotrace = 0;
33469    if( nArg<2 ){
33470      sqlite3IoTrace = 0;
33471    }else if( cli_strcmp(azArg[1], "-")==0 ){
33472      sqlite3IoTrace = iotracePrintf;
33473      iotrace = stdout;
33474    }else{
33475      iotrace = sqlite3_fopen(azArg[1], "w");
33476      if( iotrace==0 ){
33477        cli_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
33478        sqlite3IoTrace = 0;
33479        rc = 1;
33480      }else{
33481        sqlite3IoTrace = iotracePrintf;
33482      }
33483    }
33484  }else
33485#endif
33486
33487  if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){
33488    static const struct {
33489       const char *zLimitName;   /* Name of a limit */
33490       int limitCode;            /* Integer code for that limit */
33491    } aLimit[] = {
33492      { "length",                SQLITE_LIMIT_LENGTH                    },
33493      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
33494      { "column",                SQLITE_LIMIT_COLUMN                    },
33495      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
33496      { "parser_depth",          SQLITE_LIMIT_PARSER_DEPTH              },
33497      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
33498      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
33499      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
33500      { "attached",              SQLITE_LIMIT_ATTACHED                  },
33501      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
33502      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
33503      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
33504      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
33505    };
33506    int i, n2;
33507    open_db(p, 0);
33508    if( nArg==1 ){
33509      for(i=0; i<ArraySize(aLimit); i++){
33510        cli_printf(stdout, "%20s %d\n", aLimit[i].zLimitName,
33511              sqlite3_limit(p->db, aLimit[i].limitCode, -1));
33512      }
33513    }else if( nArg>3 ){
33514      eputz("Usage: .limit NAME ?NEW-VALUE?\n");
33515      rc = 1;
33516      goto meta_command_exit;
33517    }else{
33518      int iLimit = -1;
33519      n2 = strlen30(azArg[1]);
33520      for(i=0; i<ArraySize(aLimit); i++){
33521        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
33522          if( iLimit<0 ){
33523            iLimit = i;
33524          }else{
33525            cli_printf(stderr,"ambiguous limit: \"%s\"\n", azArg[1]);
33526            rc = 1;
33527            goto meta_command_exit;
33528          }
33529        }
33530      }
33531      if( iLimit<0 ){
33532        cli_printf(stderr,"unknown limit: \"%s\"\n"
33533              "enter \".limits\" with no arguments for a list.\n",
33534              azArg[1]);
33535        rc = 1;
33536        goto meta_command_exit;
33537      }
33538      if( nArg==3 ){
33539        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
33540                      (int)integerValue(azArg[2]));
33541      }else{
33542        cli_printf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
33543              sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
33544      }
33545    }
33546  }else
33547
33548  if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
33549    open_db(p, 0);
33550    lintDotCommand(p, azArg, nArg);
33551  }else
33552
33553#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
33554  if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){
33555    const char *zFile, *zProc;
33556    char *zErrMsg = 0;
33557    failIfSafeMode(p, "cannot run .load in safe mode");
33558    if( nArg<2 || azArg[1][0]==0 ){
33559      /* Must have a non-empty FILE. (Will not load self.) */
33560      eputz("Usage: .load FILE ?ENTRYPOINT?\n");
33561      rc = 1;
33562      goto meta_command_exit;
33563    }
33564    zFile = azArg[1];
33565    zProc = nArg>=3 ? azArg[2] : 0;
33566    open_db(p, 0);
33567    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
33568    if( rc!=SQLITE_OK ){
33569      shellEmitError(zErrMsg);
33570      sqlite3_free(zErrMsg);
33571      rc = 1;
33572    }
33573  }else
33574#endif
33575
33576  if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
33577    if( nArg!=2 ){
33578      eputz("Usage: .log FILENAME\n");
33579      rc = 1;
33580    }else{
33581      const char *zFile = azArg[1];
33582      if( p->bSafeMode
33583       && cli_strcmp(zFile,"on")!=0
33584       && cli_strcmp(zFile,"off")!=0
33585      ){
33586        sputz(stdout, "cannot set .log to anything other"
33587              " than \"on\" or \"off\"\n");
33588        zFile = "off";
33589      }
33590      output_file_close(p->pLog);
33591      if( cli_strcmp(zFile,"on")==0 ) zFile = "stdout";
33592      p->pLog = output_file_open(p, zFile);
33593    }
33594  }else
33595
33596  if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
33597    rc = dotCmdMode(p);
33598  }else
33599
33600#ifndef SQLITE_SHELL_FIDDLE
33601  if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
33602    if( nArg!=2 ){
33603      eputz("Usage: .nonce NONCE\n");
33604      rc = 1;
33605    }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
33606      cli_printf(stderr,"line %lld: incorrect nonce: \"%s\"\n",
33607            p->lineno, azArg[1]);
33608      cli_exit(1);
33609    }else{
33610      p->bSafeMode = 0;
33611      return 0;  /* Return immediately to bypass the safe mode reset
33612                 ** at the end of this procedure */
33613    }
33614  }else
33615#endif /* !defined(SQLITE_SHELL_FIDDLE) */
33616
33617  if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
33618    if( nArg==2 ){
33619      modeSetStr(&p->mode.spec.zNull, azArg[1]);
33620    }else{
33621      eputz("Usage: .nullvalue STRING\n");
33622      rc = 1;
33623    }
33624  }else
33625
33626  if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
33627    const char *zFN = 0;     /* Pointer to constant filename */
33628    char *zNewFilename = 0;  /* Name of the database file to open */
33629    int iName = 1;           /* Index in azArg[] of the filename */
33630    int newFlag = 0;         /* True to delete file before opening */
33631    int openMode = SHELL_OPEN_UNSPEC;
33632    int openFlags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
33633
33634    if( p->bSafeMode ) openFlags = SQLITE_OPEN_READONLY;
33635
33636    /* Check for command-line arguments */
33637    for(iName=1; iName<nArg; iName++){
33638      const char *z = azArg[iName];
33639#ifndef SQLITE_SHELL_FIDDLE
33640      if( optionMatch(z,"new") ){
33641        newFlag = 1;
33642#ifdef SQLITE_HAVE_ZLIB
33643      }else if( optionMatch(z, "zip") && !p->bSafeMode ){
33644        openMode = SHELL_OPEN_ZIPFILE;
33645#endif
33646      }else if( optionMatch(z, "append") && !p->bSafeMode ){
33647        openMode = SHELL_OPEN_APPENDVFS;
33648      }else if( optionMatch(z, "readonly") ){
33649        openFlags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
33650        openFlags |= SQLITE_OPEN_READONLY;
33651      }else if( optionMatch(z, "exclusive") ){
33652        openFlags |= SQLITE_OPEN_EXCLUSIVE;
33653      }else if( optionMatch(z, "ifexists") ){
33654        openFlags &= ~(SQLITE_OPEN_CREATE);
33655      }else if( optionMatch(z, "nofollow") ){
33656        openFlags |= SQLITE_OPEN_NOFOLLOW;
33657#ifndef SQLITE_OMIT_DESERIALIZE
33658      }else if( optionMatch(z, "deserialize") ){
33659        openMode = SHELL_OPEN_DESERIALIZE;
33660      }else if( optionMatch(z, "hexdb") ){
33661        openMode = SHELL_OPEN_HEXDB;
33662      }else if( optionMatch(z, "normal") ){
33663        openMode = SHELL_OPEN_NORMAL;
33664      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
33665        p->szMax = integerValue(azArg[++iName]);
33666#endif /* SQLITE_OMIT_DESERIALIZE */
33667      }else
33668#endif /* !SQLITE_SHELL_FIDDLE */
33669      if( z[0]=='-' ){
33670        cli_printf(stderr,"unknown option: %s\n", z);
33671        rc = 1;
33672        goto meta_command_exit;
33673      }else if( zFN ){
33674        cli_printf(stderr,"extra argument: \"%s\"\n", z);
33675        rc = 1;
33676        goto meta_command_exit;
33677      }else{
33678        zFN = z;
33679      }
33680    }
33681
33682    /* Close the existing database */
33683    session_close_all(p, -1);
33684    close_db(p->db);
33685    p->db = 0;
33686    p->pAuxDb->zDbFilename = 0;
33687    sqlite3_free(p->pAuxDb->zFreeOnClose);
33688    p->pAuxDb->zFreeOnClose = 0;
33689    p->openMode = openMode;
33690    p->openFlags = openFlags;
33691    p->szMax = 0;
33692
33693    /* If a filename is specified, try to open it first */
33694    if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
33695      if( newFlag && zFN && !p->bSafeMode ){
33696        if( cli_strncmp(zFN,"file:",5)==0 ){
33697          char *zDel = shellFilenameFromUri(zFN);
33698          shell_check_oom(zDel);
33699          shellDeleteFile(zDel);
33700          sqlite3_free(zDel);
33701        }else{
33702          shellDeleteFile(zFN);
33703        }
33704      }
33705#ifndef SQLITE_SHELL_FIDDLE
33706      if( p->bSafeMode
33707       && p->openMode!=SHELL_OPEN_HEXDB
33708       && zFN
33709       && cli_strcmp(zFN,":memory:")!=0
33710      ){
33711        failIfSafeMode(p, "cannot open disk-based database files in safe mode");
33712      }
33713#else
33714      /* WASM mode has its own sandboxed pseudo-filesystem. */
33715#endif
33716      if( zFN ){
33717        zNewFilename = sqlite3_mprintf("%s", zFN);
33718        shell_check_oom(zNewFilename);
33719      }else{
33720        zNewFilename = 0;
33721      }
33722      p->pAuxDb->zDbFilename = zNewFilename;
33723      open_db(p, OPEN_DB_KEEPALIVE);
33724      if( p->db==0 ){
33725        cli_printf(stderr,"Error: cannot open '%s'\n", zNewFilename);
33726        sqlite3_free(zNewFilename);
33727      }else{
33728        p->pAuxDb->zFreeOnClose = zNewFilename;
33729      }
33730    }
33731    if( p->db==0 ){
33732      /* As a fall-back open a TEMP database */
33733      p->pAuxDb->zDbFilename = 0;
33734      open_db(p, 0);
33735    }
33736  }else
33737
33738#ifndef SQLITE_SHELL_FIDDLE
33739  if( (c=='o'
33740        && (cli_strncmp(azArg[0], "output", n)==0
33741            || cli_strncmp(azArg[0], "once", n)==0))
33742   || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
33743   || (c=='w' && n==3 && cli_strcmp(azArg[0],"www")==0)
33744  ){
33745    rc = dotCmdOutput(p);
33746  }else
33747#endif /* !defined(SQLITE_SHELL_FIDDLE) */
33748
33749  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){
33750    open_db(p,0);
33751    if( nArg<=1 ) goto parameter_syntax_error;
33752
33753    /* .parameter clear
33754    ** Clear all bind parameters by dropping the TEMP table that holds them.
33755    */
33756    if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){
33757      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
33758                   0, 0, 0);
33759    }else
33760
33761    /* .parameter list
33762    ** List all bind parameters.
33763    */
33764    if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){
33765      sqlite3_stmt *pStmt = 0;
33766      int rx;
33767      int len = 0;
33768      rx = sqlite3_prepare_v2(p->db,
33769             "SELECT max(length(key)) "
33770             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
33771      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
33772        len = sqlite3_column_int(pStmt, 0);
33773        if( len>40 ) len = 40;
33774      }
33775      sqlite3_finalize(pStmt);
33776      pStmt = 0;
33777      if( len ){
33778        rx = sqlite3_prepare_v2(p->db,
33779             "SELECT key, quote(value) "
33780             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
33781        while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
33782          cli_printf(p->out,
33783                "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
33784                sqlite3_column_text(pStmt,1));
33785        }
33786        sqlite3_finalize(pStmt);
33787      }
33788    }else
33789
33790    /* .parameter init
33791    ** Make sure the TEMP table used to hold bind parameters exists.
33792    ** Create it if necessary.
33793    */
33794    if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){
33795      bind_table_init(p);
33796    }else
33797
33798    /* .parameter set NAME VALUE
33799    ** Set or reset a bind parameter.  NAME should be the full parameter
33800    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
33801    ** VALUE can be in either SQL literal notation, or if not it will be
33802    ** understood to be a text string.
33803    */
33804    if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){
33805      int rx;
33806      char *zSql;
33807      sqlite3_stmt *pStmt;
33808      const char *zKey = azArg[2];
33809      const char *zValue = azArg[3];
33810      bind_table_init(p);
33811      zSql = sqlite3_mprintf(
33812                  "REPLACE INTO temp.sqlite_parameters(key,value)"
33813                  "VALUES(%Q,%s);", zKey, zValue);
33814      shell_check_oom(zSql);
33815      pStmt = 0;
33816      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
33817      sqlite3_free(zSql);
33818      if( rx!=SQLITE_OK ){
33819        sqlite3_finalize(pStmt);
33820        pStmt = 0;
33821        zSql = sqlite3_mprintf(
33822                   "REPLACE INTO temp.sqlite_parameters(key,value)"
33823                   "VALUES(%Q,%Q);", zKey, zValue);
33824        shell_check_oom(zSql);
33825        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
33826        sqlite3_free(zSql);
33827        if( rx!=SQLITE_OK ){
33828          cli_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
33829          sqlite3_finalize(pStmt);
33830          pStmt = 0;
33831          rc = 1;
33832        }
33833      }
33834      bind_prepared_stmt(p, pStmt);
33835      sqlite3_step(pStmt);
33836      sqlite3_finalize(pStmt);
33837    }else
33838
33839    /* .parameter unset NAME
33840    ** Remove the NAME binding from the parameter binding table, if it
33841    ** exists.
33842    */
33843    if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){
33844      char *zSql = sqlite3_mprintf(
33845          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
33846      shell_check_oom(zSql);
33847      sqlite3_exec(p->db, zSql, 0, 0, 0);
33848      sqlite3_free(zSql);
33849    }else
33850    /* If no command name matches, show a syntax error */
33851    parameter_syntax_error:
33852    showHelp(p->out, "parameter");
33853  }else
33854
33855  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
33856    int i;
33857    for(i=1; i<nArg; i++){
33858      if( i>1 ) cli_puts(" ", p->out);
33859      cli_puts(azArg[i], p->out);
33860    }
33861    cli_puts("\n", p->out);
33862  }else
33863
33864#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
33865  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
33866    int i;
33867    int nn = 0;
33868    p->flgProgress = 0;
33869    p->mxProgress = 0;
33870    p->nProgress = 0;
33871    for(i=1; i<nArg; i++){
33872      const char *z = azArg[i];
33873      if( z[0]=='-' ){
33874        z++;
33875        if( z[0]=='-' ) z++;
33876        if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){
33877          p->flgProgress |= SHELL_PROGRESS_QUIET;
33878          continue;
33879        }
33880        if( cli_strcmp(z,"reset")==0 ){
33881          p->flgProgress |= SHELL_PROGRESS_RESET;
33882          continue;
33883        }
33884        if( cli_strcmp(z,"once")==0 ){
33885          p->flgProgress |= SHELL_PROGRESS_ONCE;
33886          continue;
33887        }
33888        if( cli_strcmp(z,"timeout")==0 ){
33889          if( i==nArg-1 ){
33890            dotCmdError(p, i, "missing argument", 0);
33891            return 1;
33892          }
33893          i++;
33894          p->tmProgress = atof(azArg[i]);
33895          if( p->tmProgress>0.0 ){
33896            p->flgProgress = SHELL_PROGRESS_QUIET|SHELL_PROGRESS_TMOUT;
33897            if( nn==0 ) nn = 100;
33898          }
33899          continue;
33900        }
33901        if( cli_strcmp(z,"limit")==0 ){
33902          if( i+1>=nArg ){
33903            eputz("Error: missing argument on --limit\n");
33904            rc = 1;
33905            goto meta_command_exit;
33906          }else{
33907            p->mxProgress = (int)integerValue(azArg[++i]);
33908          }
33909          continue;
33910        }
33911        cli_printf(stderr,"Error: unknown option: \"%s\"\n", azArg[i]);
33912        rc = 1;
33913        goto meta_command_exit;
33914      }else{
33915        nn = (int)integerValue(z);
33916      }
33917    }
33918    open_db(p, 0);
33919    sqlite3_progress_handler(p->db, nn, progress_handler, p);
33920  }else
33921#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
33922
33923  if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){
33924    if( nArg >= 2) {
33925      shell_strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
33926    }
33927    if( nArg >= 3) {
33928      shell_strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
33929    }
33930  }else
33931
33932#ifndef SQLITE_SHELL_FIDDLE
33933  if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){
33934    rc = 2;
33935  }else
33936#endif
33937
33938#ifndef SQLITE_SHELL_FIDDLE
33939  if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
33940    FILE *inSaved = p->in;
33941    i64 savedLineno = p->lineno;
33942    failIfSafeMode(p, "cannot run .read in safe mode");
33943    if( nArg!=2 ){
33944      eputz("Usage: .read FILE\n");
33945      rc = 1;
33946      goto meta_command_exit;
33947    }
33948    if( azArg[1][0]=='|' ){
33949#ifdef SQLITE_OMIT_POPEN
33950      eputz("Error: pipes are not supported in this OS\n");
33951      rc = 1;
33952#else
33953      p->in = sqlite3_popen(azArg[1]+1, "r");
33954      if( p->in==0 ){
33955        cli_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
33956        rc = 1;
33957      }else{
33958        rc = process_input(p, "<pipe>");
33959        pclose(p->in);
33960      }
33961#endif
33962    }else if( (p->in = openChrSource(azArg[1]))==0 ){
33963      cli_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
33964      rc = 1;
33965    }else{
33966      char *zFilename = strdup(azArg[1]);
33967      rc = process_input(p, zFilename);
33968      free(zFilename);
33969      fclose(p->in);
33970    }
33971    p->in = inSaved;
33972    p->lineno = savedLineno;
33973  }else
33974#endif /* !defined(SQLITE_SHELL_FIDDLE) */
33975
33976#ifndef SQLITE_SHELL_FIDDLE
33977  if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){
33978    const char *zSrcFile;
33979    const char *zDb;
33980    sqlite3 *pSrc;
33981    sqlite3_backup *pBackup;
33982    int nTimeout = 0;
33983
33984    failIfSafeMode(p, "cannot run .restore in safe mode");
33985    if( nArg==2 ){
33986      zSrcFile = azArg[1];
33987      zDb = "main";
33988    }else if( nArg==3 ){
33989      zSrcFile = azArg[2];
33990      zDb = azArg[1];
33991    }else{
33992      eputz("Usage: .restore ?DB? FILE\n");
33993      rc = 1;
33994      goto meta_command_exit;
33995    }
33996    rc = sqlite3_open(zSrcFile, &pSrc);
33997    if( rc!=SQLITE_OK ){
33998      cli_printf(stderr,"Error: cannot open \"%s\"\n", zSrcFile);
33999      close_db(pSrc);
34000      return 1;
34001    }
34002    open_db(p, 0);
34003    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
34004    if( pBackup==0 ){
34005      shellDatabaseError(p->db);
34006      close_db(pSrc);
34007      return 1;
34008    }
34009    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
34010          || rc==SQLITE_BUSY  ){
34011      if( rc==SQLITE_BUSY ){
34012        if( nTimeout++ >= 3 ) break;
34013        sqlite3_sleep(100);
34014      }
34015    }
34016    sqlite3_backup_finish(pBackup);
34017    if( rc==SQLITE_DONE ){
34018      rc = 0;
34019    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
34020      eputz("Error: source database is busy\n");
34021      rc = 1;
34022    }else{
34023      shellDatabaseError(p->db);
34024      rc = 1;
34025    }
34026    close_db(pSrc);
34027  }else
34028#endif /* !defined(SQLITE_SHELL_FIDDLE) */
34029
34030  if( c=='s' &&
34031     (cli_strncmp(azArg[0], "scanstats",  n)==0 ||
34032      cli_strncmp(azArg[0], "scanstatus", n)==0)
34033  ){
34034    if( nArg==2 ){
34035      if( cli_strcmp(azArg[1], "vm")==0 ){
34036        p->mode.scanstatsOn = 3;
34037      }else
34038      if( cli_strcmp(azArg[1], "est")==0 ){
34039        p->mode.scanstatsOn = 2;
34040      }else{
34041        p->mode.scanstatsOn = (u8)booleanValue(azArg[1]);
34042      }
34043      open_db(p, 0);
34044      sqlite3_db_config(
34045          p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->mode.scanstatsOn, (int*)0
34046      );
34047#if !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
34048      eputz("Warning: .scanstats not available in this build.\n");
34049#elif !defined(SQLITE_ENABLE_BYTECODE_VTAB)
34050      if( p->mode.scanstatsOn==3 ){
34051        eputz("Warning: \".scanstats vm\" not available in this build.\n");
34052      }
34053#endif
34054    }else{
34055      eputz("Usage: .scanstats on|off|est\n");
34056      rc = 1;
34057    }
34058  }else
34059
34060  if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
34061    ShellState data;
34062    char *zErrMsg = 0;
34063    const char *zDiv = "(";
34064    const char *zName = 0;
34065    int iSchema = 0;
34066    int bDebug = 0;
34067    int bNoSystemTabs = 0;
34068    int bIndent = 0;
34069    int ii;
34070    sqlite3_str *pSql;
34071    sqlite3_stmt *pStmt = 0;
34072 
34073    open_db(p, 0);
34074    memcpy(&data, p, sizeof(data));
34075    data.mode.spec.bTitles = QRF_No;
34076    data.mode.eMode = MODE_List;
34077    data.mode.spec.eText = QRF_TEXT_Plain;
34078    data.mode.spec.nCharLimit = 0;
34079    data.mode.spec.zRowSep = "\n";
34080    for(ii=1; ii<nArg; ii++){
34081      if( optionMatch(azArg[ii],"indent") ){
34082        bIndent = 1;
34083      }else if( optionMatch(azArg[ii],"debug") ){
34084        bDebug = 1;
34085      }else if( optionMatch(azArg[ii],"nosys") ){
34086        bNoSystemTabs = 1;
34087      }else if( azArg[ii][0]=='-' ){
34088        cli_printf(stderr,"Unknown option: \"%s\"\n", azArg[ii]);
34089        rc = 1;
34090        goto meta_command_exit;
34091      }else if( zName==0 ){
34092        zName = azArg[ii];
34093      }else{
34094        eputz("Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
34095        rc = 1;
34096        goto meta_command_exit;
34097      }
34098    }
34099    if( zName!=0 ){
34100      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
34101                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
34102                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
34103                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
34104      if( isSchema ){
34105        cli_printf(p->out,
34106                      "CREATE TABLE %ssqlite_schema (\n"
34107                      "  type text,\n"
34108                      "  name text,\n"
34109                      "  tbl_name text,\n"
34110                      "  rootpage integer,\n"
34111                      "  sql text\n"
34112                      ");\n",
34113               sqlite3_strlike("sqlite_t%",zName,0)==0 ? "temp." : ""
34114        );
34115      }
34116    }
34117    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
34118    if( rc ){
34119      shellDatabaseError(p->db);
34120      sqlite3_finalize(pStmt);
34121      
34122      rc = 1;
34123      goto meta_command_exit;
34124    }
34125    pSql = sqlite3_str_new(p->db);
34126    sqlite3_str_appendf(pSql, "SELECT sql FROM", 0);
34127    iSchema = 0;
34128    while( sqlite3_step(pStmt)==SQLITE_ROW ){
34129      const char *zDb = (const char*)sqlite3_column_text(pStmt, 1);
34130      char zScNum[30];
34131      sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
34132      sqlite3_str_appendall(pSql, zDiv);
34133      zDiv = " UNION ALL ";
34134      if( sqlite3_stricmp(zDb, "main")==0 ){
34135        sqlite3_str_appendf(pSql,
34136            "SELECT shell_format_schema(shell_add_schema(sql,NULL,name),%d)",
34137            bIndent);
34138      }else{
34139        sqlite3_str_appendf(pSql,
34140            "SELECT shell_format_schema(shell_add_schema(sql,%Q,name),%d)",
34141            zDb, bIndent);
34142      }
34143      sqlite3_str_appendf(pSql,
34144         " AS sql, type, tbl_name, name, rowid, %d AS snum, %Q as sname",
34145         ++iSchema, zDb);
34146      sqlite3_str_appendf(pSql," FROM \"%w\".sqlite_schema", zDb);
34147    }
34148    sqlite3_finalize(pStmt);
34149#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS) \
34150 && !defined(SQLITE_OMIT_VIRTUALTABLE)
34151    if( zName ){
34152      sqlite3_str_appendall(pSql,
34153         " UNION ALL SELECT shell_module_schema(name),"
34154         " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list");
34155    }
34156#endif
34157    sqlite3_str_appendf(pSql, ") WHERE ", 0);
34158    if( zName ){
34159      int bGlob;
34160      bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
34161              strchr(zName, '[') != 0;
34162      if( strchr(zName, '.') ){
34163        sqlite3_str_appendall(pSql, "lower(format('%%s.%%s',sname,tbl_name))");
34164      }else{
34165        sqlite3_str_appendall(pSql, "lower(tbl_name)");
34166      }
34167      if( bGlob ){
34168        sqlite3_str_appendf(pSql, " GLOB %Q AND ", zName);
34169      }else{
34170        sqlite3_str_appendf(pSql, " LIKE %Q ESCAPE '\\' AND ", zName);
34171      }
34172    }
34173    if( bNoSystemTabs ){
34174      sqlite3_str_appendf(pSql, " name NOT LIKE 'sqlite__%%' ESCAPE '_' AND ");
34175    }
34176    sqlite3_str_appendf(pSql, "sql IS NOT NULL ORDER BY snum, rowid");
34177    if( bDebug ){
34178      cli_printf(p->out, "SQL: %s;\n", sqlite3_str_value(pSql));
34179    }else{
34180      rc = shell_exec(&data, sqlite3_str_value(pSql), &zErrMsg);
34181    }
34182    sqlite3_str_free(pSql);
34183
34184    if( zErrMsg ){
34185      shellEmitError(zErrMsg);
34186      sqlite3_free(zErrMsg);
34187      rc = 1;
34188    }else if( rc != SQLITE_OK ){
34189      eputz("Error: querying schema information\n");
34190      rc = 1;
34191    }else{
34192      rc = 0;
34193    }
34194  }else
34195
34196  if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0)
34197   || (c=='t' && n==9  && cli_strncmp(azArg[0], "treetrace", n)==0)
34198  ){
34199    unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
34200    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
34201  }else
34202
34203#if defined(SQLITE_ENABLE_SESSION)
34204  if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){
34205    struct AuxDb *pAuxDb = p->pAuxDb;
34206    OpenSession *pSession = &pAuxDb->aSession[0];
34207    char **azCmd = &azArg[1];
34208    int iSes = 0;
34209    int nCmd = nArg - 1;
34210    int i;
34211    if( nArg<=1 ) goto session_syntax_error;
34212    open_db(p, 0);
34213    if( nArg>=3 ){
34214      for(iSes=0; iSes<pAuxDb->nSession; iSes++){
34215        if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
34216      }
34217      if( iSes<pAuxDb->nSession ){
34218        pSession = &pAuxDb->aSession[iSes];
34219        azCmd++;
34220        nCmd--;
34221      }else{
34222        pSession = &pAuxDb->aSession[0];
34223        iSes = 0;
34224      }
34225    }
34226
34227    /* .session attach TABLE
34228    ** Invoke the sqlite3session_attach() interface to attach a particular
34229    ** table so that it is never filtered.
34230    */
34231    if( cli_strcmp(azCmd[0],"attach")==0 ){
34232      if( nCmd!=2 ) goto session_syntax_error;
34233      if( pSession->p==0 ){
34234        session_not_open:
34235        eputz("ERROR: No sessions are open\n");
34236      }else{
34237        rc = sqlite3session_attach(pSession->p, azCmd[1]);
34238        if( rc ){
34239          cli_printf(stderr,
34240               "ERROR: sqlite3session_attach() returns %d\n",rc);
34241          rc = 0;
34242        }
34243      }
34244    }else
34245
34246    /* .session changeset FILE
34247    ** .session patchset FILE
34248    ** Write a changeset or patchset into a file.  The file is overwritten.
34249    */
34250    if( cli_strcmp(azCmd[0],"changeset")==0
34251     || cli_strcmp(azCmd[0],"patchset")==0
34252    ){
34253      FILE *out = 0;
34254      failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
34255      if( nCmd!=2 ) goto session_syntax_error;
34256      if( pSession->p==0 ) goto session_not_open;
34257      out = sqlite3_fopen(azCmd[1], "wb");
34258      if( out==0 ){
34259        cli_printf(stderr,"ERROR: cannot open \"%s\" for writing\n",
34260              azCmd[1]);
34261      }else{
34262        int szChng;
34263        void *pChng;
34264        if( azCmd[0][0]=='c' ){
34265          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
34266        }else{
34267          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
34268        }
34269        if( rc ){
34270          cli_printf(stdout, "Error: error code %d\n", rc);
34271          rc = 0;
34272        }
34273        if( pChng
34274          && fwrite(pChng, szChng, 1, out)!=1 ){
34275          cli_printf(stderr,
34276              "ERROR: Failed to write entire %d-byte output\n", szChng);
34277        }
34278        sqlite3_free(pChng);
34279        fclose(out);
34280      }
34281    }else
34282
34283    /* .session close
34284    ** Close the identified session
34285    */
34286    if( cli_strcmp(azCmd[0], "close")==0 ){
34287      if( nCmd!=1 ) goto session_syntax_error;
34288      if( pAuxDb->nSession ){
34289        session_close(pSession);
34290        pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
34291      }
34292    }else
34293
34294    /* .session enable ?BOOLEAN?
34295    ** Query or set the enable flag
34296    */
34297    if( cli_strcmp(azCmd[0], "enable")==0 ){
34298      int ii;
34299      if( nCmd>2 ) goto session_syntax_error;
34300      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
34301      if( pAuxDb->nSession ){
34302        ii = sqlite3session_enable(pSession->p, ii);
34303        cli_printf(p->out,
34304            "session %s enable flag = %d\n", pSession->zName, ii);
34305      }
34306    }else
34307
34308    /* .session filter GLOB ....
34309    ** Set a list of GLOB patterns of table names to be excluded.
34310    */
34311    if( cli_strcmp(azCmd[0], "filter")==0 ){
34312      int ii;
34313      i64 nByte;
34314      if( nCmd<2 ) goto session_syntax_error;
34315      if( pAuxDb->nSession ){
34316        for(ii=0; ii<pSession->nFilter; ii++){
34317          sqlite3_free(pSession->azFilter[ii]);
34318        }
34319        sqlite3_free(pSession->azFilter);
34320        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
34321        pSession->azFilter = sqlite3_malloc64( nByte );
34322        shell_check_oom( pSession->azFilter );
34323        for(ii=1; ii<nCmd; ii++){
34324          char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
34325          shell_check_oom(x);
34326        }
34327        pSession->nFilter = ii-1;
34328      }
34329    }else
34330
34331    /* .session indirect ?BOOLEAN?
34332    ** Query or set the indirect flag
34333    */
34334    if( cli_strcmp(azCmd[0], "indirect")==0 ){
34335      int ii;
34336      if( nCmd>2 ) goto session_syntax_error;
34337      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
34338      if( pAuxDb->nSession ){
34339        ii = sqlite3session_indirect(pSession->p, ii);
34340        cli_printf(p->out,
34341            "session %s indirect flag = %d\n", pSession->zName, ii);
34342      }
34343    }else
34344
34345    /* .session isempty
34346    ** Determine if the session is empty
34347    */
34348    if( cli_strcmp(azCmd[0], "isempty")==0 ){
34349      int ii;
34350      if( nCmd!=1 ) goto session_syntax_error;
34351      if( pAuxDb->nSession ){
34352        ii = sqlite3session_isempty(pSession->p);
34353        cli_printf(p->out,
34354             "session %s isempty flag = %d\n", pSession->zName, ii);
34355      }
34356    }else
34357
34358    /* .session list
34359    ** List all currently open sessions
34360    */
34361    if( cli_strcmp(azCmd[0],"list")==0 ){
34362      for(i=0; i<pAuxDb->nSession; i++){
34363        cli_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
34364      }
34365    }else
34366
34367    /* .session open DB NAME
34368    ** Open a new session called NAME on the attached database DB.
34369    ** DB is normally "main".
34370    */
34371    if( cli_strcmp(azCmd[0],"open")==0 ){
34372      char *zName;
34373      if( nCmd!=3 ) goto session_syntax_error;
34374      zName = azCmd[2];
34375      if( zName[0]==0 ) goto session_syntax_error;
34376      for(i=0; i<pAuxDb->nSession; i++){
34377        if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
34378          cli_printf(stderr,"Session \"%s\" already exists\n", zName);
34379          goto meta_command_exit;
34380        }
34381      }
34382      if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
34383        cli_printf(stderr,
34384           "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
34385        goto meta_command_exit;
34386      }
34387      pSession = &pAuxDb->aSession[pAuxDb->nSession];
34388      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
34389      if( rc ){
34390        cli_printf(stderr,"Cannot open session: error code=%d\n", rc);
34391        rc = 0;
34392        goto meta_command_exit;
34393      }
34394      pSession->nFilter = 0;
34395      sqlite3session_table_filter(pSession->p, session_filter, pSession);
34396      pAuxDb->nSession++;
34397      pSession->zName = sqlite3_mprintf("%s", zName);
34398      shell_check_oom(pSession->zName);
34399    }else
34400    /* If no command name matches, show a syntax error */
34401    session_syntax_error:
34402    showHelp(p->out, "session");
34403  }else
34404#endif
34405
34406#ifdef SQLITE_DEBUG
34407  /* Undocumented commands for internal testing.  Subject to change
34408  ** without notice. */
34409  if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
34410    if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
34411      int i, v;
34412      for(i=1; i<nArg; i++){
34413        v = booleanValue(azArg[i]);
34414        cli_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
34415      }
34416    }
34417    if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
34418      int i; sqlite3_int64 v;
34419      for(i=1; i<nArg; i++){
34420        char zBuf[200];
34421        v = integerValue(azArg[i]);
34422        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
34423        cli_puts(zBuf, p->out);
34424      }
34425    }
34426  }else
34427#endif
34428
34429  if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){
34430    int bIsInit = 0;         /* True to initialize the SELFTEST table */
34431    int bVerbose = 0;        /* Verbose output */
34432    int bSelftestExists;     /* True if SELFTEST already exists */
34433    int i, k;                /* Loop counters */
34434    int nTest = 0;           /* Number of tests runs */
34435    int nErr = 0;            /* Number of errors seen */
34436    ShellText str;           /* Answer for a query */
34437    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
34438
34439    open_db(p,0);
34440    for(i=1; i<nArg; i++){
34441      const char *z = azArg[i];
34442      if( z[0]=='-' && z[1]=='-' ) z++;
34443      if( cli_strcmp(z,"-init")==0 ){
34444        bIsInit = 1;
34445      }else
34446      if( cli_strcmp(z,"-v")==0 ){
34447        bVerbose++;
34448      }else
34449      {
34450        cli_printf(stderr,
34451              "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
34452        cli_puts("Should be one of: --init -v\n", stderr);
34453        rc = 1;
34454        goto meta_command_exit;
34455      }
34456    }
34457    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
34458           != SQLITE_OK ){
34459      bSelftestExists = 0;
34460    }else{
34461      bSelftestExists = 1;
34462    }
34463    if( bIsInit ){
34464      createSelftestTable(p);
34465      bSelftestExists = 1;
34466    }
34467    initText(&str);
34468    appendText(&str, "x", 0);
34469    for(k=bSelftestExists; k>=0; k--){
34470      if( k==1 ){
34471        rc = sqlite3_prepare_v2(p->db,
34472            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
34473            -1, &pStmt, 0);
34474      }else{
34475        rc = sqlite3_prepare_v2(p->db,
34476          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
34477          "      (1,'run','PRAGMA integrity_check','ok')",
34478          -1, &pStmt, 0);
34479      }
34480      if( rc ){
34481        eputz("Error querying the selftest table\n");
34482        rc = 1;
34483        sqlite3_finalize(pStmt);
34484        goto meta_command_exit;
34485      }
34486      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
34487        int tno = sqlite3_column_int(pStmt, 0);
34488        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
34489        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
34490        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
34491
34492        if( zOp==0 ) continue;
34493        if( zSql==0 ) continue;
34494        if( zAns==0 ) continue;
34495        k = 0;
34496        if( bVerbose>0 ){
34497          cli_printf(stdout, "%d: %s %s\n", tno, zOp, zSql);
34498        }
34499        if( cli_strcmp(zOp,"memo")==0 ){
34500          cli_printf(p->out, "%s\n", zSql);
34501        }else
34502        if( cli_strcmp(zOp,"run")==0 ){
34503          char *zErrMsg = 0;
34504          str.n = 0;
34505          str.zTxt[0] = 0;
34506          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
34507          nTest++;
34508          if( bVerbose ){
34509            cli_printf(p->out, "Result: %s\n", str.zTxt);
34510          }
34511          if( rc || zErrMsg ){
34512            nErr++;
34513            rc = 1;
34514            cli_printf(p->out, "%d: error-code-%d: %s\n", tno, rc,zErrMsg);
34515            sqlite3_free(zErrMsg);
34516          }else if( cli_strcmp(zAns,str.zTxt)!=0 ){
34517            nErr++;
34518            rc = 1;
34519            cli_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
34520            cli_printf(p->out, "%d:      Got: [%s]\n", tno, str.zTxt);
34521          }
34522        }
34523        else{
34524          cli_printf(stderr,
34525                "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
34526          rc = 1;
34527          break;
34528        }
34529      } /* End loop over rows of content from SELFTEST */
34530      sqlite3_finalize(pStmt);
34531    } /* End loop over k */
34532    freeText(&str);
34533    cli_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
34534  }else
34535
34536  if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
34537    if( nArg<2 || nArg>3 ){
34538      eputz("Usage: .separator COL ?ROW?\n");
34539      rc = 1;
34540    }
34541    if( nArg>=2 ){
34542      modeSetStr(&p->mode.spec.zColumnSep, azArg[1]);
34543    }
34544    if( nArg>=3 ){
34545      modeSetStr(&p->mode.spec.zRowSep,azArg[2]);
34546    }
34547  }else
34548
34549  if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){
34550    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
34551    int i;                   /* Loop counter */
34552    int bSchema = 0;         /* Also hash the schema */
34553    int bSeparate = 0;       /* Hash each table separately */
34554    int iSize = 224;         /* Hash algorithm to use */
34555    int bDebug = 0;          /* Only show the query that would have run */
34556    sqlite3_stmt *pStmt;     /* For querying tables names */
34557    char *zSql;              /* SQL to be run */
34558    char *zSep;              /* Separator */
34559    ShellText sSql;          /* Complete SQL for the query to run the hash */
34560    ShellText sQuery;        /* Set of queries used to read all content */
34561    open_db(p, 0);
34562    for(i=1; i<nArg; i++){
34563      const char *z = azArg[i];
34564      if( z[0]=='-' ){
34565        z++;
34566        if( z[0]=='-' ) z++;
34567        if( cli_strcmp(z,"schema")==0 ){
34568          bSchema = 1;
34569        }else
34570        if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0
34571         || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0
34572        ){
34573          iSize = atoi(&z[5]);
34574        }else
34575        if( cli_strcmp(z,"debug")==0 ){
34576          bDebug = 1;
34577        }else
34578        {
34579          cli_printf(stderr,
34580                  "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
34581          showHelp(p->out, azArg[0]);
34582          rc = 1;
34583          goto meta_command_exit;
34584        }
34585      }else if( zLike ){
34586        eputz("Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
34587        rc = 1;
34588        goto meta_command_exit;
34589      }else{
34590        zLike = z;
34591        bSeparate = 1;
34592        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
34593      }
34594    }
34595    if( bSchema ){
34596      zSql = "SELECT lower(name) as tname FROM sqlite_schema"
34597             " WHERE type='table' AND coalesce(rootpage,0)>1"
34598             " UNION ALL SELECT 'sqlite_schema'"
34599             " ORDER BY 1 collate nocase";
34600    }else{
34601      zSql = "SELECT lower(name) as tname FROM sqlite_schema"
34602             " WHERE type='table' AND coalesce(rootpage,0)>1"
34603             " AND name NOT LIKE 'sqlite__%' ESCAPE '_'"
34604             " ORDER BY 1 collate nocase";
34605    }
34606    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
34607    initText(&sQuery);
34608    initText(&sSql);
34609    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
34610    zSep = "VALUES(";
34611    while( SQLITE_ROW==sqlite3_step(pStmt) ){
34612      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
34613      if( zTab==0 ) continue;
34614      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
34615      if( cli_strncmp(zTab, "sqlite_",7)!=0 ){
34616        appendText(&sQuery,"SELECT * FROM ", 0);
34617        appendText(&sQuery,zTab,'"');
34618        appendText(&sQuery," NOT INDEXED;", 0);
34619      }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){
34620        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
34621                           " ORDER BY name;", 0);
34622      }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){
34623        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
34624                           " ORDER BY name;", 0);
34625      }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){
34626        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
34627                           " ORDER BY tbl,idx;", 0);
34628      }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){
34629        appendText(&sQuery, "SELECT * FROM ", 0);
34630        appendText(&sQuery, zTab, 0);
34631        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
34632      }
34633      appendText(&sSql, zSep, 0);
34634      appendText(&sSql, sQuery.zTxt, '\'');
34635      sQuery.n = 0;
34636      appendText(&sSql, ",", 0);
34637      appendText(&sSql, zTab, '\'');
34638      zSep = "),(";
34639    }
34640    sqlite3_finalize(pStmt);
34641    if( bSeparate ){
34642      zSql = sqlite3_mprintf(
34643          "%s))"
34644          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
34645          "   FROM [sha3sum$query]",
34646          sSql.zTxt, iSize);
34647    }else{
34648      zSql = sqlite3_mprintf(
34649          "%s))"
34650          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
34651          "   FROM [sha3sum$query]",
34652          sSql.zTxt, iSize);
34653    }
34654    shell_check_oom(zSql);
34655    freeText(&sQuery);
34656    freeText(&sSql);
34657    if( bDebug ){
34658      cli_printf(p->out, "%s\n", zSql);
34659    }else{
34660      shell_exec(p, zSql, 0);
34661    }
34662#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
34663    {
34664      int lrc;
34665      char *zRevText = /* Query for reversible to-blob-to-text check */
34666        "SELECT lower(name) as tname FROM sqlite_schema\n"
34667        "WHERE type='table' AND coalesce(rootpage,0)>1\n"
34668        "AND name NOT LIKE 'sqlite__%%' ESCAPE '_'%s\n"
34669        "ORDER BY 1 collate nocase";
34670      zRevText = sqlite3_mprintf(zRevText, zLike? " AND name LIKE $tspec" : "");
34671      zRevText = sqlite3_mprintf(
34672          /* lower-case query is first run, producing upper-case query. */
34673          "with tabcols as materialized(\n"
34674          "select tname, cname\n"
34675          "from ("
34676          " select printf('\"%%w\"',ss.tname) as tname,"
34677          " printf('\"%%w\"',ti.name) as cname\n"
34678          " from (%z) ss\n inner join pragma_table_info(tname) ti))\n"
34679          "select 'SELECT total(bad_text_count) AS bad_text_count\n"
34680          "FROM ('||group_concat(query, ' UNION ALL ')||')' as btc_query\n"
34681          " from (select 'SELECT COUNT(*) AS bad_text_count\n"
34682          "FROM '||tname||' WHERE '\n"
34683          "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
34684          "|| ' AND typeof('||cname||')=''text'' ',\n"
34685          "' OR ') as query, tname from tabcols group by tname)"
34686          , zRevText);
34687      shell_check_oom(zRevText);
34688      if( bDebug ) cli_printf(p->out, "%s\n", zRevText);
34689      lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0);
34690      if( lrc!=SQLITE_OK ){
34691        /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
34692        ** user does cruel and unnatural things like ".limit expr_depth 0". */
34693        rc = 1;
34694      }else{
34695        if( zLike ) sqlite3_bind_text(pStmt,1,zLike,-1,SQLITE_STATIC);
34696        lrc = SQLITE_ROW==sqlite3_step(pStmt);
34697        if( lrc ){
34698          const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0);
34699          sqlite3_stmt *pCheckStmt;
34700          lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0);
34701          if( bDebug ) cli_printf(p->out, "%s\n", zGenQuery);
34702          if( lrc!=SQLITE_OK ){
34703            rc = 1;
34704          }else{
34705            if( SQLITE_ROW==sqlite3_step(pCheckStmt) ){
34706              double countIrreversible = sqlite3_column_double(pCheckStmt, 0);
34707              if( countIrreversible>0 ){
34708                int sz = (int)(countIrreversible + 0.5);
34709                cli_printf(stderr,
34710                      "Digest includes %d invalidly encoded text field%s.\n",
34711                      sz, (sz>1)? "s": "");
34712              }
34713            }
34714            sqlite3_finalize(pCheckStmt);
34715          }
34716          sqlite3_finalize(pStmt);
34717        }
34718      }
34719      if( rc ) eputz(".sha3sum failed.\n");
34720      sqlite3_free(zRevText);
34721    }
34722#endif /* !defined(*_OMIT_SCHEMA_PRAGMAS) && !defined(*_OMIT_VIRTUALTABLE) */
34723    sqlite3_free(zSql);
34724  }else
34725
34726#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
34727  if( c=='s'
34728   && (cli_strncmp(azArg[0], "shell", n)==0
34729       || cli_strncmp(azArg[0],"system",n)==0)
34730  ){
34731    char *zCmd;
34732    int i, x;
34733    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
34734    if( nArg<2 ){
34735      eputz("Usage: .system COMMAND\n");
34736      rc = 1;
34737      goto meta_command_exit;
34738    }
34739    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
34740    for(i=2; i<nArg && zCmd!=0; i++){
34741      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
34742                             zCmd, azArg[i]);
34743    }
34744    /*consoleRestore();*/
34745    x = zCmd!=0 ? system(zCmd) : 1;
34746    /*consoleRenewSetup();*/
34747    sqlite3_free(zCmd);
34748    if( x ) cli_printf(stderr,"System command returns %d\n", x);
34749  }else
34750#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
34751
34752  if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
34753    static const char *azBool[] = { "off", "on", "trigger", "full"};
34754    const char *zOut;
34755    int i;
34756    if( nArg!=1 ){
34757      eputz("Usage: .show\n");
34758      rc = 1;
34759      goto meta_command_exit;
34760    }
34761    cli_printf(p->out, "%12.12s: %s\n","echo", 
34762            azBool[(p->mode.mFlags & MFLG_ECHO)!=0]);
34763    cli_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->mode.autoEQP&3]);
34764    cli_printf(p->out, "%12.12s: %s\n","explain",
34765                             p->mode.autoExplain ? "auto" : "off");
34766    cli_printf(p->out, "%12.12s: %s\n","headers",
34767          azBool[p->mode.spec.bTitles==QRF_Yes]);
34768    if( p->mode.spec.eStyle==QRF_STYLE_Column
34769     || p->mode.spec.eStyle==QRF_STYLE_Box
34770     || p->mode.spec.eStyle==QRF_STYLE_Table
34771     || p->mode.spec.eStyle==QRF_STYLE_Markdown
34772    ){
34773      cli_printf(p->out,
34774            "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
34775            aModeInfo[p->mode.eMode].zName, p->mode.spec.nWrap,
34776            p->mode.spec.bWordWrap==QRF_Yes ? "on" : "off",
34777            p->mode.spec.eText==QRF_TEXT_Sql ? "" : "no");
34778    }else{
34779      cli_printf(p->out, "%12.12s: %s\n","mode", 
34780                 aModeInfo[p->mode.eMode].zName);
34781    }
34782    cli_printf(p->out, "%12.12s: ", "nullvalue");
34783    output_c_string(p->out, p->mode.spec.zNull);
34784    cli_puts("\n", p->out);
34785    cli_printf(p->out, "%12.12s: %s\n","output",
34786          strlen30(p->outfile) ? p->outfile : "stdout");
34787    cli_printf(p->out, "%12.12s: ", "colseparator");
34788    output_c_string(p->out, p->mode.spec.zColumnSep);
34789    cli_puts("\n", p->out);
34790    cli_printf(p->out, "%12.12s: ", "rowseparator");
34791    output_c_string(p->out, p->mode.spec.zRowSep);
34792    cli_puts("\n", p->out);
34793    switch( p->statsOn ){
34794      case 0:  zOut = "off";     break;
34795      default: zOut = "on";      break;
34796      case 2:  zOut = "stmt";    break;
34797      case 3:  zOut = "vmstep";  break;
34798    }
34799    cli_printf(p->out, "%12.12s: %s\n","stats", zOut);
34800    cli_printf(p->out, "%12.12s: ", "width");
34801    for(i=0; i<p->mode.spec.nWidth; i++){
34802      cli_printf(p->out, "%d ", (int)p->mode.spec.aWidth[i]);
34803    }
34804    cli_puts("\n", p->out);
34805    cli_printf(p->out, "%12.12s: %s\n", "filename",
34806          p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
34807  }else
34808
34809  if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
34810    if( nArg==2 ){
34811      if( cli_strcmp(azArg[1],"stmt")==0 ){
34812        p->statsOn = 2;
34813      }else if( cli_strcmp(azArg[1],"vmstep")==0 ){
34814        p->statsOn = 3;
34815      }else{
34816        p->statsOn = (u8)booleanValue(azArg[1]);
34817      }
34818    }else if( nArg==1 ){
34819      display_stats(p->db, p, 0);
34820    }else{
34821      eputz("Usage: .stats ?on|off|stmt|vmstep?\n");
34822      rc = 1;
34823    }
34824  }else
34825
34826  if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0) ){
34827    sqlite3_stmt *pStmt;
34828    sqlite3_str *pSql;
34829    const char *zPattern = nArg>1 ? azArg[1] : 0;
34830
34831    open_db(p, 0);
34832    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
34833    if( rc ){
34834      sqlite3_finalize(pStmt);
34835      return shellDatabaseError(p->db);
34836    }
34837
34838    pSql = sqlite3_str_new(p->db);
34839    while( sqlite3_step(pStmt)==SQLITE_ROW ){
34840      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
34841      if( zDbName==0 ) continue;
34842      if( sqlite3_str_length(pSql) ){
34843        sqlite3_str_appendall(pSql, " UNION ALL ");
34844      }
34845      if( sqlite3_stricmp(zDbName, "main")==0 ){
34846        sqlite3_str_appendall(pSql, "SELECT name FROM ");
34847      }else{
34848        sqlite3_str_appendf(pSql, "SELECT %Q||'.'||name FROM ", zDbName);
34849      }
34850      sqlite3_str_appendf(pSql, "\"%w\".sqlite_schema", zDbName);
34851      sqlite3_str_appendf(pSql,
34852          " WHERE type IN ('table','view')"
34853          "   AND name NOT LIKE 'sqlite__%%' ESCAPE '_'"
34854      );
34855      if( zPattern ){
34856        sqlite3_str_appendf(pSql," AND name LIKE %Q", zPattern);
34857      }
34858    }
34859    rc = sqlite3_finalize(pStmt);
34860    if( rc==SQLITE_OK ){
34861      sqlite3_str_appendall(pSql, " ORDER BY 1");
34862    }
34863
34864    /* Run the SQL statement in "split" mode. */
34865    modePush(p);
34866    modeChange(p, MODE_Split);
34867    shell_exec(p, sqlite3_str_value(pSql), 0);
34868    sqlite3_str_free(pSql);
34869    modePop(p);
34870    if( rc ) return shellDatabaseError(p->db);
34871  }else
34872
34873  /* Set the p->zTestcase name and begin redirecting output into
34874  ** the cli_output_capture sqlite3_str */
34875  if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
34876    rc = dotCmdTestcase(p);
34877  }else
34878
34879#ifndef SQLITE_UNTESTABLE
34880  if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
34881    static const struct {
34882       const char *zCtrlName;   /* Name of a test-control option */
34883       int ctrlCode;            /* Integer code for that option */
34884       int unSafe;              /* Not valid unless --unsafe-testing */
34885       const char *zUsage;      /* Usage notes */
34886    } aCtrl[] = {
34887    {"always",             SQLITE_TESTCTRL_ALWAYS, 1,     "BOOLEAN"         },
34888    {"assert",             SQLITE_TESTCTRL_ASSERT, 1,     "BOOLEAN"         },
34889  /*{"benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, ""        },*/
34890    {"bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST, 1, "SIZE INT-ARRAY"},
34891    {"byteorder",          SQLITE_TESTCTRL_BYTEORDER, 0,  ""                },
34892    {"extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN"  },
34893    {"fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, 1,"args..."       },
34894    {"fk_no_action",       SQLITE_TESTCTRL_FK_NO_ACTION, 0, "BOOLEAN"       },
34895    {"imposter",         SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
34896    {"internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,""          },
34897    {"json_selfcheck",     SQLITE_TESTCTRL_JSON_SELFCHECK ,0,"BOOLEAN"      },
34898    {"localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN"      },
34899    {"never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN"       },
34900    {"optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK ..."},
34901#ifdef YYCOVERAGE
34902    {"parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE,0,""             },
34903#endif
34904    {"pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,1, "OFFSET  "       },
34905    {"prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,0, ""               },
34906    {"prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,   0, ""               },
34907    {"prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,   0, "SEED ?db?"      },
34908    {"seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,  0, ""               },
34909    {"sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX"           },
34910    {"tune",               SQLITE_TESTCTRL_TUNE,        1, "ID VALUE"       },
34911    };
34912    int testctrl = -1;
34913    int iCtrl = -1;
34914    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
34915    int isOk = 0;
34916    int i, n2;
34917    const char *zCmd = 0;
34918
34919    open_db(p, 0);
34920    zCmd = nArg>=2 ? azArg[1] : "help";
34921
34922    /* The argument can optionally begin with "-" or "--" */
34923    if( zCmd[0]=='-' && zCmd[1] ){
34924      zCmd++;
34925      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
34926    }
34927
34928    /* --help lists all test-controls */
34929    if( cli_strcmp(zCmd,"help")==0 ){
34930      cli_puts("Available test-controls:\n", p->out);
34931      for(i=0; i<ArraySize(aCtrl); i++){
34932        if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
34933        cli_printf(p->out, "  .testctrl %s %s\n",
34934              aCtrl[i].zCtrlName, aCtrl[i].zUsage);
34935      }
34936      rc = 1;
34937      goto meta_command_exit;
34938    }
34939
34940    /* convert testctrl text option to value. allow any unique prefix
34941    ** of the option name, or a numerical value. */
34942    n2 = strlen30(zCmd);
34943    for(i=0; i<ArraySize(aCtrl); i++){
34944      if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
34945      if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
34946        if( testctrl<0 ){
34947          testctrl = aCtrl[i].ctrlCode;
34948          iCtrl = i;
34949        }else{
34950          cli_printf(stderr,"Error: ambiguous test-control: \"%s\"\n"
34951                "Use \".testctrl --help\" for help\n", zCmd);
34952          rc = 1;
34953          goto meta_command_exit;
34954        }
34955      }
34956    }
34957    if( testctrl<0 ){
34958      cli_printf(stderr,"Error: unknown test-control: %s\n"
34959            "Use \".testctrl --help\" for help\n", zCmd);
34960    }else{
34961      switch(testctrl){
34962
34963        /* Special processing for .testctrl opt MASK ...
34964        ** Each MASK argument can be one of:
34965        **
34966        **      +LABEL       Enable the named optimization 
34967        **
34968        **      -LABEL       Disable the named optimization
34969        **
34970        **      INTEGER      Mask of optimizations to disable
34971        */
34972        case SQLITE_TESTCTRL_OPTIMIZATIONS: {
34973          static const struct {
34974             unsigned int mask;    /* Mask for this optimization */
34975             unsigned int bDsply;  /* Display this on output */
34976             const char *zLabel;   /* Name of optimization */
34977          } aLabel[] = {
34978            { 0x00000001, 1, "QueryFlattener" },
34979            { 0x00000001, 0, "Flatten" },
34980            { 0x00000002, 1, "WindowFunc" },
34981            { 0x00000004, 1, "GroupByOrder" },
34982            { 0x00000008, 1, "FactorOutConst" },
34983            { 0x00000010, 1, "DistinctOpt" },
34984            { 0x00000020, 1, "CoverIdxScan" },
34985            { 0x00000040, 1, "OrderByIdxJoin" },
34986            { 0x00000080, 1, "Transitive" },
34987            { 0x00000100, 1, "OmitNoopJoin" },
34988            { 0x00000200, 1, "CountOfView" },
34989            { 0x00000400, 1, "CursorHints" },
34990            { 0x00000800, 1, "Stat4" },
34991            { 0x00001000, 1, "PushDown" },
34992            { 0x00002000, 1, "SimplifyJoin" },
34993            { 0x00004000, 1, "SkipScan" },
34994            { 0x00008000, 1, "PropagateConst" },
34995            { 0x00010000, 1, "MinMaxOpt" },
34996            { 0x00020000, 1, "SeekScan" },
34997            { 0x00040000, 1, "OmitOrderBy" },
34998            { 0x00080000, 1, "BloomFilter" },
34999            { 0x00100000, 1, "BloomPulldown" },
35000            { 0x00200000, 1, "BalancedMerge" },
35001            { 0x00400000, 1, "ReleaseReg" },
35002            { 0x00800000, 1, "FlttnUnionAll" },
35003            { 0x01000000, 1, "IndexedEXpr" },
35004            { 0x02000000, 1, "Coroutines" },
35005            { 0x04000000, 1, "NullUnusedCols" },
35006            { 0x08000000, 1, "OnePass" },
35007            { 0x10000000, 1, "OrderBySubq" },
35008            { 0x20000000, 1, "StarQuery" },
35009            { 0x40000000, 1, "ExistsToJoin" },
35010            { 0xffffffff, 0, "All" },
35011          };
35012          unsigned int curOpt;
35013          unsigned int newOpt;
35014          unsigned int m;
35015          int ii;
35016          int nOff;
35017          sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, p->db, &curOpt);
35018          newOpt = curOpt;
35019          for(ii=2; ii<nArg; ii++){
35020            const char *z = azArg[ii];
35021            int useLabel = 0;
35022            const char *zLabel = 0;
35023            if( (z[0]=='+'|| z[0]=='-') && !IsDigit(z[1]) ){
35024              useLabel = z[0];
35025              zLabel = &z[1];
35026            }else if( !IsDigit(z[0]) && z[0]!=0 && !IsDigit(z[1]) ){
35027              useLabel = '+';
35028              zLabel = z;
35029            }else{
35030              newOpt = (unsigned int)strtol(z,0,0);
35031            }
35032            if( useLabel ){
35033              int jj;
35034              for(jj=0; jj<ArraySize(aLabel); jj++){
35035                if( sqlite3_stricmp(zLabel, aLabel[jj].zLabel)==0 ) break;
35036              }
35037              if( jj>=ArraySize(aLabel) ){
35038                cli_printf(stderr,
35039                    "Error: no such optimization: \"%s\"\n", zLabel);
35040                cli_puts("Should be one of:", stderr);
35041                for(jj=0; jj<ArraySize(aLabel); jj++){
35042                  cli_printf(stderr," %s", aLabel[jj].zLabel);
35043                }
35044                cli_puts("\n", stderr);
35045                rc = 1;
35046                goto meta_command_exit;
35047              }
35048              if( useLabel=='+' ){
35049                newOpt &= ~aLabel[jj].mask;
35050              }else{
35051                newOpt |= aLabel[jj].mask;
35052              }
35053            }
35054          }
35055          if( curOpt!=newOpt ){
35056            sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,p->db,newOpt);
35057          }
35058          for(ii=nOff=0, m=1; ii<32; ii++, m <<= 1){
35059            if( m & newOpt ) nOff++;
35060          }
35061          if( nOff<12 ){
35062            cli_puts("+All", p->out);
35063            for(ii=0; ii<ArraySize(aLabel); ii++){
35064              if( !aLabel[ii].bDsply  ) continue;
35065              if( (newOpt & aLabel[ii].mask)!=0 ){
35066                cli_printf(p->out, " -%s", aLabel[ii].zLabel);
35067              }
35068            }
35069          }else{
35070            cli_puts("-All", p->out);
35071            for(ii=0; ii<ArraySize(aLabel); ii++){
35072              if( !aLabel[ii].bDsply  ) continue;
35073              if( (newOpt & aLabel[ii].mask)==0 ){
35074                cli_printf(p->out, " +%s", aLabel[ii].zLabel);
35075              }
35076            }
35077          }
35078          cli_puts("\n", p->out);
35079          rc2 = isOk = 3;
35080          break;
35081        }
35082
35083        /* sqlite3_test_control(int, db, int) */
35084        case SQLITE_TESTCTRL_FK_NO_ACTION:
35085          if( nArg==3 ){
35086            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
35087            rc2 = sqlite3_test_control(testctrl, p->db, opt);
35088            isOk = 3;
35089          }
35090          break;
35091
35092        /* sqlite3_test_control(int) */
35093        case SQLITE_TESTCTRL_PRNG_SAVE:
35094        case SQLITE_TESTCTRL_PRNG_RESTORE:
35095        case SQLITE_TESTCTRL_BYTEORDER:
35096          if( nArg==2 ){
35097            rc2 = sqlite3_test_control(testctrl);
35098            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
35099          }
35100          break;
35101
35102        /* sqlite3_test_control(int, uint) */
35103        case SQLITE_TESTCTRL_PENDING_BYTE:
35104          if( nArg==3 ){
35105            unsigned int opt = (unsigned int)integerValue(azArg[2]);
35106            rc2 = sqlite3_test_control(testctrl, opt);
35107            isOk = 3;
35108          }
35109          break;
35110
35111        /* sqlite3_test_control(int, int, sqlite3*) */
35112        case SQLITE_TESTCTRL_PRNG_SEED:
35113          if( nArg==3 || nArg==4 ){
35114            int ii = (int)integerValue(azArg[2]);
35115            sqlite3 *db;
35116            if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
35117              sqlite3_randomness(sizeof(ii),&ii);
35118              cli_printf(stdout, "-- random seed: %d\n", ii);
35119            }
35120            if( nArg==3 ){
35121              db = 0;
35122            }else{
35123              db = p->db;
35124              /* Make sure the schema has been loaded */
35125              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
35126            }
35127            rc2 = sqlite3_test_control(testctrl, ii, db);
35128            isOk = 3;
35129          }
35130          break;
35131
35132        /* sqlite3_test_control(int, int) */
35133        case SQLITE_TESTCTRL_ASSERT:
35134        case SQLITE_TESTCTRL_ALWAYS:
35135          if( nArg==3 ){
35136            int opt = booleanValue(azArg[2]);
35137            rc2 = sqlite3_test_control(testctrl, opt);
35138            isOk = 1;
35139          }
35140          break;
35141
35142        /* sqlite3_test_control(int, int) */
35143        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
35144        case SQLITE_TESTCTRL_NEVER_CORRUPT:
35145          if( nArg==3 ){
35146            int opt = booleanValue(azArg[2]);
35147            rc2 = sqlite3_test_control(testctrl, opt);
35148            isOk = 3;
35149          }
35150          break;
35151
35152        /* sqlite3_test_control(sqlite3*) */
35153        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
35154          rc2 = sqlite3_test_control(testctrl, p->db);
35155          isOk = 3;
35156          break;
35157
35158        case SQLITE_TESTCTRL_IMPOSTER:
35159          if( nArg==5 ){
35160            rc2 = sqlite3_test_control(testctrl, p->db,
35161                          azArg[2],
35162                          integerValue(azArg[3]),
35163                          integerValue(azArg[4]));
35164            isOk = 3;
35165          }
35166          break;
35167
35168        case SQLITE_TESTCTRL_SEEK_COUNT: {
35169          u64 x = 0;
35170          rc2 = sqlite3_test_control(testctrl, p->db, &x);
35171          cli_printf(p->out, "%llu\n", x);
35172          isOk = 3;
35173          break;
35174        }
35175
35176#ifdef YYCOVERAGE
35177        case SQLITE_TESTCTRL_PARSER_COVERAGE: {
35178          if( nArg==2 ){
35179            sqlite3_test_control(testctrl, p->out);
35180            isOk = 3;
35181          }
35182          break;
35183        }
35184#endif
35185#ifdef SQLITE_DEBUG
35186        case SQLITE_TESTCTRL_TUNE: {
35187          if( nArg==4 ){
35188            int id = (int)integerValue(azArg[2]);
35189            int val = (int)integerValue(azArg[3]);
35190            sqlite3_test_control(testctrl, id, &val);
35191            isOk = 3;
35192          }else if( nArg==3 ){
35193            int id = (int)integerValue(azArg[2]);
35194            sqlite3_test_control(testctrl, -id, &rc2);
35195            isOk = 1;
35196          }else if( nArg==2 ){
35197            int id = 1;
35198            while(1){
35199              int val = 0;
35200              rc2 = sqlite3_test_control(testctrl, -id, &val);
35201              if( rc2!=SQLITE_OK ) break;
35202              if( id>1 ) cli_puts("  ", p->out);
35203              cli_printf(p->out, "%d: %d", id, val);
35204              id++;
35205            }
35206            if( id>1 ) cli_puts("\n", p->out);
35207            isOk = 3;
35208          }
35209          break;
35210        }
35211#endif
35212        case SQLITE_TESTCTRL_SORTER_MMAP:
35213          if( nArg==3 ){
35214            int opt = (unsigned int)integerValue(azArg[2]);
35215            rc2 = sqlite3_test_control(testctrl, p->db, opt);
35216            isOk = 3;
35217          }
35218          break;
35219        case SQLITE_TESTCTRL_JSON_SELFCHECK:
35220          if( nArg==2 ){
35221            rc2 = -1;
35222            isOk = 1;
35223          }else{
35224            rc2 = booleanValue(azArg[2]);
35225            isOk = 3;
35226          }
35227          sqlite3_test_control(testctrl, &rc2);
35228          break;
35229        case SQLITE_TESTCTRL_BITVEC_TEST: {
35230          /* Examples:
35231          **   .testctrl bitvec_test 100   6,1       -- Show BITVEC constants
35232          **   .testctrl bitvec_test 1000  1,12,7,3  -- Simple test
35233          **                         ----  --------
35234          **      size of Bitvec -----^        ^---  aOp array. 0 added at end.
35235          **
35236          ** See comments on sqlite3BitvecBuiltinTest() for more information
35237          ** about the aOp[] array.
35238          */
35239          int iSize;
35240          const char *zTestArg;
35241          int nOp;
35242          int ii, jj, x;
35243          int *aOp;
35244          if( nArg!=4 ){
35245            cli_printf(stderr,
35246              "ERROR - should be:  \".testctrl bitvec_test SIZE  INT-ARRAY\"\n"
35247            );
35248            rc = 1;
35249            goto meta_command_exit;
35250          }
35251          isOk = 3;
35252          iSize = (int)integerValue(azArg[2]);
35253          zTestArg = azArg[3];
35254          nOp = (int)strlen(zTestArg)+1;
35255          aOp = malloc( sizeof(int)*(nOp+1) );
35256          shell_check_oom(aOp);
35257          memset(aOp, 0, sizeof(int)*(nOp+1) );
35258          for(ii = jj = x = 0; zTestArg[ii]!=0; ii++){
35259            if( IsDigit(zTestArg[ii]) ){
35260              x = x*10 + zTestArg[ii] - '0';
35261            }else{
35262              aOp[jj++] = x;
35263              x = 0;
35264            }
35265          }
35266          aOp[jj] = x;
35267          x = sqlite3_test_control(testctrl, iSize, aOp);
35268          cli_printf(p->out, "result: %d\n", x);
35269          free(aOp);
35270          break;
35271        }
35272        case SQLITE_TESTCTRL_FAULT_INSTALL: {
35273          int kk;
35274          int bShowHelp = nArg<=2;
35275          isOk = 3;
35276          for(kk=2; kk<nArg; kk++){
35277            const char *z = azArg[kk];
35278            if( z[0]=='-' && z[1]=='-' ) z++;
35279            if( cli_strcmp(z,"off")==0 ){
35280              sqlite3_test_control(testctrl, 0);
35281            }else if( cli_strcmp(z,"on")==0 ){
35282              faultsim_state.iCnt = faultsim_state.nSkip;
35283              if( faultsim_state.iErr==0 ) faultsim_state.iErr = 1;
35284              faultsim_state.nHit = 0;
35285              sqlite3_test_control(testctrl, faultsim_callback);
35286            }else if( cli_strcmp(z,"reset")==0 ){
35287              faultsim_state.iCnt = faultsim_state.nSkip;
35288              faultsim_state.nHit = 0;
35289              sqlite3_test_control(testctrl, faultsim_callback);
35290            }else if( cli_strcmp(z,"status")==0 ){
35291              cli_printf(p->out, "faultsim.iId:       %d\n",
35292                              faultsim_state.iId);
35293              cli_printf(p->out, "faultsim.iErr:      %d\n",
35294                              faultsim_state.iErr);
35295              cli_printf(p->out, "faultsim.iCnt:      %d\n",
35296                              faultsim_state.iCnt);
35297              cli_printf(p->out, "faultsim.nHit:      %d\n",
35298                              faultsim_state.nHit);
35299              cli_printf(p->out, "faultsim.iInterval: %d\n",
35300                              faultsim_state.iInterval);
35301              cli_printf(p->out, "faultsim.eVerbose:  %d\n",
35302                              faultsim_state.eVerbose);
35303              cli_printf(p->out, "faultsim.nRepeat:   %d\n",
35304                              faultsim_state.nRepeat);
35305              cli_printf(p->out, "faultsim.nSkip:     %d\n",
35306                              faultsim_state.nSkip);
35307            }else if( cli_strcmp(z,"-v")==0 ){
35308              if( faultsim_state.eVerbose<2 ) faultsim_state.eVerbose++;
35309            }else if( cli_strcmp(z,"-q")==0 ){
35310              if( faultsim_state.eVerbose>0 ) faultsim_state.eVerbose--;
35311            }else if( cli_strcmp(z,"-id")==0 && kk+1<nArg ){
35312              faultsim_state.iId = atoi(azArg[++kk]);
35313            }else if( cli_strcmp(z,"-errcode")==0 && kk+1<nArg ){
35314              faultsim_state.iErr = atoi(azArg[++kk]);
35315            }else if( cli_strcmp(z,"-interval")==0 && kk+1<nArg ){
35316              faultsim_state.iInterval = atoi(azArg[++kk]);
35317            }else if( cli_strcmp(z,"-repeat")==0 && kk+1<nArg ){
35318              faultsim_state.nRepeat = atoi(azArg[++kk]);
35319           }else if( cli_strcmp(z,"-skip")==0 && kk+1<nArg ){
35320              faultsim_state.nSkip = atoi(azArg[++kk]);
35321            }else if( cli_strcmp(z,"-?")==0 || sqlite3_strglob("*help*",z)==0){
35322              bShowHelp = 1;
35323            }else{
35324              cli_printf(stderr,
35325                  "Unrecognized fault_install argument: \"%s\"\n",
35326                  azArg[kk]);
35327              rc = 1;
35328              bShowHelp = 1;
35329              break;
35330            }
35331          }
35332          if( bShowHelp ){
35333            cli_puts(
35334               "Usage: .testctrl fault_install ARGS\n"
35335               "Possible arguments:\n"
35336               "   off               Disable faultsim\n"
35337               "   on                Activate faultsim\n"
35338               "   reset             Reset the trigger counter\n"
35339               "   status            Show current status\n"
35340               "   -v                Increase verbosity\n"
35341               "   -q                Decrease verbosity\n"
35342               "   --errcode N       When triggered, return N as error code\n"
35343               "   --id ID           Trigger only for the ID specified\n"
35344               "   --interval N      Trigger only after every N-th call\n"
35345               "   --repeat N        Turn off after N hits.  0 means never\n"
35346               "   --skip N          Skip the first N encounters\n"
35347               ,p->out
35348            );
35349          }
35350          break;
35351        }
35352      }
35353    }
35354    if( isOk==0 && iCtrl>=0 ){
35355      cli_printf(p->out,
35356          "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
35357      rc = 1;
35358    }else if( isOk==1 ){
35359      cli_printf(p->out, "%d\n", rc2);
35360    }else if( isOk==2 ){
35361      cli_printf(p->out, "0x%08x\n", rc2);
35362    }
35363  }else
35364#endif /* !defined(SQLITE_UNTESTABLE) */
35365
35366  if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
35367    open_db(p, 0);
35368    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
35369  }else
35370
35371  if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
35372    if( nArg==2 ){
35373      if( cli_strcmp(azArg[1],"once")==0 ){
35374        p->enableTimer = 1;
35375      }else{
35376        p->enableTimer = 2*booleanValue(azArg[1]);
35377      }
35378      if( p->enableTimer && !HAS_TIMER ){
35379        eputz("Error: timer not available on this system.\n");
35380        p->enableTimer = 0;
35381      }
35382    }else{
35383      eputz("Usage: .timer on|off|once\n");
35384      rc = 1;
35385    }
35386  }else
35387
35388#ifndef SQLITE_OMIT_TRACE
35389  if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){
35390    int mType = 0;
35391    int jj;
35392    open_db(p, 0);
35393    for(jj=1; jj<nArg; jj++){
35394      const char *z = azArg[jj];
35395      if( z[0]=='-' ){
35396        if( optionMatch(z, "expanded") ){
35397          p->eTraceType = SHELL_TRACE_EXPANDED;
35398        }
35399#ifdef SQLITE_ENABLE_NORMALIZE
35400        else if( optionMatch(z, "normalized") ){
35401          p->eTraceType = SHELL_TRACE_NORMALIZED;
35402        }
35403#endif
35404        else if( optionMatch(z, "plain") ){
35405          p->eTraceType = SHELL_TRACE_PLAIN;
35406        }
35407        else if( optionMatch(z, "profile") ){
35408          mType |= SQLITE_TRACE_PROFILE;
35409        }
35410        else if( optionMatch(z, "row") ){
35411          mType |= SQLITE_TRACE_ROW;
35412        }
35413        else if( optionMatch(z, "stmt") ){
35414          mType |= SQLITE_TRACE_STMT;
35415        }
35416        else if( optionMatch(z, "close") ){
35417          mType |= SQLITE_TRACE_CLOSE;
35418        }
35419        else {
35420          cli_printf(stderr,"Unknown option \"%s\" on \".trace\"\n", z);
35421          rc = 1;
35422          goto meta_command_exit;
35423        }
35424      }else{
35425        output_file_close(p->traceOut);
35426        p->traceOut = output_file_open(p, z);
35427      }
35428    }
35429    if( p->traceOut==0 ){
35430      sqlite3_trace_v2(p->db, 0, 0, 0);
35431    }else{
35432      if( mType==0 ) mType = SQLITE_TRACE_STMT;
35433      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
35434    }
35435  }else
35436#endif /* !defined(SQLITE_OMIT_TRACE) */
35437
35438#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
35439  if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
35440    int ii;
35441    int lenOpt;
35442    char *zOpt;
35443    if( nArg<2 ){
35444      eputz("Usage: .unmodule [--allexcept] NAME ...\n");
35445      rc = 1;
35446      goto meta_command_exit;
35447    }
35448    open_db(p, 0);
35449    zOpt = azArg[1];
35450    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
35451    lenOpt = (int)strlen(zOpt);
35452    if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){
35453      assert( azArg[nArg]==0 );
35454      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
35455    }else{
35456      for(ii=1; ii<nArg; ii++){
35457        sqlite3_create_module(p->db, azArg[ii], 0, 0);
35458      }
35459    }
35460  }else
35461#endif
35462
35463  if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
35464    char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
35465    cli_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
35466          sqlite3_libversion(), sqlite3_sourceid());
35467#if SQLITE_HAVE_ZLIB
35468    cli_printf(p->out, "zlib version %s\n", zlibVersion());
35469#endif
35470#define CTIMEOPT_VAL_(opt) #opt
35471#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
35472#if defined(__clang__) && defined(__clang_major__)
35473    cli_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
35474          CTIMEOPT_VAL(__clang_minor__) "."
35475          CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz);
35476#elif defined(_MSC_VER)
35477    cli_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
35478#elif defined(__GNUC__) && defined(__VERSION__)
35479    cli_printf(p->out, "gcc-" __VERSION__ " (%s)\n", zPtrSz);
35480#endif
35481  }else
35482
35483  if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
35484    const char *zDbName = nArg==2 ? azArg[1] : "main";
35485    sqlite3_vfs *pVfs = 0;
35486    if( p->db ){
35487      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
35488      if( pVfs ){
35489        cli_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
35490        cli_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
35491        cli_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
35492        cli_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
35493      }
35494    }
35495  }else
35496
35497  if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
35498    sqlite3_vfs *pVfs;
35499    sqlite3_vfs *pCurrent = 0;
35500    if( p->db ){
35501      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
35502    }
35503    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
35504      cli_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
35505            pVfs==pCurrent ? "  <--- CURRENT" : "");
35506      cli_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
35507      cli_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
35508      cli_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
35509      if( pVfs->pNext ){
35510        cli_puts("-----------------------------------\n", p->out);
35511      }
35512    }
35513  }else
35514
35515  if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
35516    const char *zDbName = nArg==2 ? azArg[1] : "main";
35517    char *zVfsName = 0;
35518    if( p->db ){
35519      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
35520      if( zVfsName ){
35521        cli_printf(p->out, "%s\n", zVfsName);
35522        sqlite3_free(zVfsName);
35523      }
35524    }
35525  }else
35526
35527  if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){
35528    unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
35529    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
35530  }else
35531
35532  if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){
35533    int j;
35534    p->mode.spec.nWidth = nArg-1;
35535    p->mode.spec.aWidth = realloc(p->mode.spec.aWidth,
35536                                  (p->mode.spec.nWidth+1)*sizeof(short int));
35537    shell_check_oom(p->mode.spec.aWidth);
35538    for(j=1; j<nArg; j++){
35539      i64 w = integerValue(azArg[j]);
35540      if( w < -QRF_MAX_WIDTH ) w = -QRF_MAX_WIDTH;
35541      if( w > QRF_MAX_WIDTH ) w = QRF_MAX_WIDTH;
35542      p->mode.spec.aWidth[j-1] = (short int)w;
35543    }
35544  }else
35545
35546  {
35547    cli_printf(stderr,"Error: unknown command or invalid arguments: "
35548          " \"%s\". Enter \".help\" for help\n", azArg[0]);
35549    rc = 1;
35550  }
35551
35552meta_command_exit:
35553  if( p->nPopOutput ){
35554    p->nPopOutput--;
35555    if( p->nPopOutput==0 ) output_reset(p);
35556  }
35557  p->bSafeMode = p->bSafeModePersist;
35558  p->dot.nArg = 0;
35559  return rc;
35560}
35561
35562/* Line scan result and intermediate states (supporting scan resumption)
35563*/
35564#ifndef CHAR_BIT
35565# define CHAR_BIT 8
35566#endif
35567typedef enum {
35568  QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
35569  QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
35570  QSS_Start = 0
35571} QuickScanState;
35572#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
35573#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
35574#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
35575#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
35576#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
35577
35578/*
35579** Scan line for classification to guide shell's handling.
35580** The scan is resumable for subsequent lines when prior
35581** return values are passed as the 2nd argument.
35582*/
35583static QuickScanState quickscan(char *zLine, QuickScanState qss,
35584                                SCAN_TRACKER_REFTYPE pst){
35585  char cin;
35586  char cWait = (char)qss; /* intentional narrowing loss */
35587  if( cWait==0 ){
35588  PlainScan:
35589    while( (cin = *zLine++)!=0 ){
35590      if( IsSpace(cin) )
35591        continue;
35592      switch (cin){
35593      case '-':
35594        if( *zLine!='-' )
35595          break;
35596        while((cin = *++zLine)!=0 )
35597          if( cin=='\n')
35598            goto PlainScan;
35599        return qss;
35600      case ';':
35601        qss |= QSS_EndingSemi;
35602        continue;
35603      case '/':
35604        if( *zLine=='*' ){
35605          ++zLine;
35606          cWait = '*';
35607          CONTINUE_PROMPT_AWAITS(pst, "/*");
35608          qss = QSS_SETV(qss, cWait);
35609          goto TermScan;
35610        }
35611        break;
35612      case '[':
35613        cin = ']';
35614        deliberate_fall_through; /* FALLTHRU */
35615      case '`': case '\'': case '"':
35616        cWait = cin;
35617        qss = QSS_HasDark | cWait;
35618        CONTINUE_PROMPT_AWAITC(pst, cin);
35619        goto TermScan;
35620      case '(':
35621        CONTINUE_PAREN_INCR(pst, 1);
35622        break;
35623      case ')':
35624        CONTINUE_PAREN_INCR(pst, -1);
35625        break;
35626      default:
35627        break;
35628      }
35629      qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
35630    }
35631  }else{
35632  TermScan:
35633    while( (cin = *zLine++)!=0 ){
35634      if( cin==cWait ){
35635        switch( cWait ){
35636        case '*':
35637          if( *zLine != '/' )
35638            continue;
35639          ++zLine;
35640          CONTINUE_PROMPT_AWAITC(pst, 0);
35641          qss = QSS_SETV(qss, 0);
35642          goto PlainScan;
35643        case '`': case '\'': case '"':
35644          if(*zLine==cWait){
35645            /* Swallow doubled end-delimiter.*/
35646            ++zLine;
35647            continue;
35648          }
35649          deliberate_fall_through; /* FALLTHRU */
35650        case ']':
35651          CONTINUE_PROMPT_AWAITC(pst, 0);
35652          qss = QSS_SETV(qss, 0);
35653          goto PlainScan;
35654        default: assert(0);
35655        }
35656      }
35657    }
35658  }
35659  return qss;
35660}
35661
35662/*
35663** Return TRUE if the line typed in is an SQL command terminator other
35664** than a semi-colon.  The SQL Server style "go" command is understood
35665** as is the Oracle "/".
35666*/
35667static int line_is_command_terminator(char *zLine){
35668  while( IsSpace(zLine[0]) ){ zLine++; };
35669  if( zLine[0]=='/' )
35670    zLine += 1; /* Oracle */
35671  else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
35672    zLine += 2; /* SQL Server */
35673  else
35674    return 0;
35675  return quickscan(zLine, QSS_Start, 0)==QSS_Start;
35676}
35677
35678/*
35679** The CLI needs a working sqlite3_complete() to work properly.  So error
35680** out of the build if compiling with SQLITE_OMIT_COMPLETE.
35681*/
35682#ifdef SQLITE_OMIT_COMPLETE
35683# error the CLI application is incompatible with SQLITE_OMIT_COMPLETE.
35684#endif
35685
35686/*
35687** Return true if zSql is a complete SQL statement.  Return false if it
35688** ends in the middle of a string literal or C-style comment.
35689*/
35690static int line_is_complete(char *zSql, int nSql){
35691  int rc;
35692  if( zSql==0 ) return 1;
35693  zSql[nSql] = ';';
35694  zSql[nSql+1] = 0;
35695  rc = sqlite3_complete(zSql);
35696  zSql[nSql] = 0;
35697  return rc;
35698}
35699
35700/*
35701** This function is called after processing each line of SQL in the
35702** runOneSqlLine() function. Its purpose is to detect scenarios where
35703** defensive mode should be automatically turned off. Specifically, when
35704**
35705**   1. The first line of input is "PRAGMA foreign_keys=OFF;",
35706**   2. The second line of input is "BEGIN TRANSACTION;",
35707**   3. The database is empty, and
35708**   4. The shell is not running in --safe mode.
35709** 
35710** The implementation uses the ShellState.eRestoreState to maintain state:
35711**
35712**    0: Have not seen any SQL.
35713**    1: Have seen "PRAGMA foreign_keys=OFF;".
35714**    2-6: Currently running .dump transaction. If the "2" bit is set,
35715**         disable DEFENSIVE when done. If "4" is set, disable DQS_DDL.
35716**    7: Nothing left to do. This function becomes a no-op.
35717*/
35718static int doAutoDetectRestore(ShellState *p, const char *zSql){
35719  int rc = SQLITE_OK;
35720
35721  if( p->eRestoreState<7 ){
35722    switch( p->eRestoreState ){
35723      case 0: {
35724        const char *zExpect = "PRAGMA foreign_keys=OFF;";
35725        assert( strlen(zExpect)==24 );
35726        if( p->bSafeMode==0
35727         && strlen(zSql)>=24
35728         && memcmp(zSql, zExpect, 25)==0
35729        ){
35730          p->eRestoreState = 1;
35731        }else{
35732          p->eRestoreState = 7;
35733        }
35734        break;
35735      };
35736  
35737      case 1: {
35738        int bIsDump = 0;
35739        const char *zExpect = "BEGIN TRANSACTION;";
35740        assert( strlen(zExpect)==18 );
35741        if( memcmp(zSql, zExpect, 19)==0 ){
35742          /* Now check if the database is empty. */
35743          const char *zQuery = "SELECT 1 FROM sqlite_schema LIMIT 1";
35744          sqlite3_stmt *pStmt = 0;
35745  
35746          bIsDump = 1;
35747          shellPrepare(p->db, &rc, zQuery, &pStmt);
35748          if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
35749            bIsDump = 0;
35750          }
35751          shellFinalize(&rc, pStmt);
35752        }
35753        if( bIsDump && rc==SQLITE_OK ){
35754          int bDefense = 0;
35755          int bDqsDdl = 0;
35756          sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &bDefense);
35757          sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, -1, &bDqsDdl);
35758          sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
35759          sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 1, 0);
35760          p->eRestoreState = (bDefense ? 2 : 0) + (bDqsDdl ? 4 : 0);
35761        }else{
35762          p->eRestoreState = 7;
35763        }
35764        break;
35765      }
35766  
35767      default: {
35768        if( sqlite3_get_autocommit(p->db) ){
35769          if( (p->eRestoreState & 2) ){
35770            sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 1, 0);
35771          }
35772          if( (p->eRestoreState & 4) ){
35773            sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 0, 0);
35774          }
35775          p->eRestoreState = 7;
35776        }
35777        break;
35778      }
35779    }
35780  }
35781
35782  return rc;
35783}
35784
35785/*
35786** Run a single line of SQL.  Return the number of errors.
35787*/
35788static int runOneSqlLine(
35789  ShellState *p,            /* Execution context */
35790  char *zSql,               /* SQL to be run */
35791  const char *zFilename,    /* Source file of the sql */
35792  int startline             /* linenumber */
35793){
35794  int rc;
35795  char *zErrMsg = 0;
35796
35797  open_db(p, 0);
35798  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
35799  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
35800  BEGIN_TIMER(p);
35801  rc = shell_exec(p, zSql, &zErrMsg);
35802  END_TIMER(p);
35803  if( rc || zErrMsg ){
35804    char zPrefix[2048
35805                 /* must be relatively large:
35806                 ** https://sqlite.org/forum/forumpost/205f73db1b2806f5 */];
35807    const char *zErrorTail;
35808    const char *zErrorType;
35809    if( zErrMsg==0 ){
35810      zErrorType = "Error";
35811      zErrorTail = sqlite3_errmsg(p->db);
35812    }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){
35813      zErrorType = "Parse error";
35814      zErrorTail = &zErrMsg[12];
35815    }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){
35816      zErrorType = "Runtime error";
35817      zErrorTail = &zErrMsg[10];
35818    }else{
35819      zErrorType = "Error";
35820      zErrorTail = zErrMsg;
35821    }
35822    if( zFilename || !stdin_is_interactive ){
35823      if( cli_strcmp(zFilename,"cmdline")==0 ){
35824        sqlite3_snprintf(sizeof(zPrefix), zPrefix,
35825                  "%s in %r command line argument:", zErrorType, startline);
35826      }else if( cli_strcmp(zFilename,"<stdin>")==0 ){
35827        sqlite3_snprintf(sizeof(zPrefix), zPrefix,
35828                  "%s near line %d:", zErrorType, startline);
35829      }else{
35830        sqlite3_snprintf(sizeof(zPrefix), zPrefix,
35831                  "%s near line %d of %s:", zErrorType, startline, zFilename);
35832      }
35833    }else{
35834      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
35835    }
35836    cli_printf(stderr,"%s %s\n", zPrefix, zErrorTail);
35837    sqlite3_free(zErrMsg);
35838    zErrMsg = 0;
35839    return 1;
35840  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
35841    char zLineBuf[2000];
35842    sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
35843            "changes: %lld   total_changes: %lld",
35844            sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
35845    cli_printf(p->out, "%s\n", zLineBuf);
35846  }
35847
35848  if( doAutoDetectRestore(p, zSql) ) return 1;
35849  return 0;
35850}
35851
35852static void echo_group_input(ShellState *p, const char *zDo){
35853  if( p->mode.mFlags & MFLG_ECHO ){
35854    cli_printf(p->out, "%s\n", zDo);
35855    fflush(p->out);
35856  }
35857}
35858
35859#ifdef SQLITE_SHELL_FIDDLE
35860/*
35861** Alternate one_input_line() impl for wasm mode. This is not in the primary
35862** impl because we need the global shellState and cannot access it from that
35863** function without moving lots of code around (creating a larger/messier diff).
35864*/
35865static char *one_input_line(ShellState *p, char *zPrior, int isContinuation){
35866  /* Parse the next line from shellState.wasm.zInput. */
35867  const char *zBegin = shellState.wasm.zPos;
35868  const char *z = zBegin;
35869  char *zLine = 0;
35870  i64 nZ = 0;
35871  FILE *in = p->in;
35872
35873  UNUSED_PARAMETER(in);
35874  UNUSED_PARAMETER(isContinuation);
35875  if(!z || !*z){
35876    return 0;
35877  }
35878  while(*z && IsSpace(*z)) ++z;
35879  zBegin = z;
35880  for(; *z && '\n'!=*z; ++nZ, ++z){}
35881  if(nZ>0 && '\r'==zBegin[nZ-1]){
35882    --nZ;
35883  }
35884  shellState.wasm.zPos = z;
35885  zLine = realloc(zPrior, nZ+1);
35886  shell_check_oom(zLine);
35887  memcpy(zLine, zBegin, nZ);
35888  zLine[nZ] = 0;
35889  return zLine;
35890}
35891#endif /* SQLITE_SHELL_FIDDLE */
35892
35893/*
35894** Read input from *in and process it.  If *in==0 then input
35895** is interactive - the user is typing it it.  Otherwise, input
35896** is coming from a file or device.  A prompt is issued and history
35897** is saved only if input is interactive.  An interrupt signal will
35898** cause this routine to exit immediately, unless input is interactive.
35899**
35900** Return the number of errors.
35901*/
35902static int process_input(ShellState *p, const char *zSrc){
35903  char *zLine = 0;          /* A single input line */
35904  char *zSql = 0;           /* Accumulated SQL text */
35905  i64 nLine;                /* Length of current line */
35906  i64 nSql = 0;             /* Bytes of zSql[] used */
35907  i64 nAlloc = 0;           /* Allocated zSql[] space */
35908  int rc;                   /* Error code */
35909  int errCnt = 0;           /* Number of errors seen */
35910  i64 startline = 0;        /* Line number for start of current input */
35911  QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
35912  const char *saved_zInFile;      /* Prior value of p->zInFile */
35913  i64 saved_lineno;               /* Prior value of p->lineno */
35914
35915  if( p->inputNesting==MAX_INPUT_NESTING ){
35916    /* This will be more informative in a later version. */
35917    cli_printf(stderr,"%s: Input nesting limit (%d) reached at line %lld."
35918          " Check recursion.\n", zSrc, MAX_INPUT_NESTING, p->lineno);
35919    return 1;
35920  }
35921  ++p->inputNesting;
35922  saved_zInFile = p->zInFile;
35923  p->zInFile = zSrc;
35924  saved_lineno = p->lineno;
35925  p->lineno = 0;
35926  CONTINUE_PROMPT_RESET;
35927  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
35928    fflush(p->out);
35929    zLine = one_input_line(p, zLine, nSql>0);
35930    if( zLine==0 ){
35931      /* End of input */
35932      if( p->in==0 && stdin_is_interactive ) cli_puts("\n", p->out);
35933      break;
35934    }
35935    if( seenInterrupt ){
35936      if( p->in!=0 ) break;
35937      seenInterrupt = 0;
35938    }
35939    p->lineno++;
35940    if( QSS_INPLAIN(qss)
35941        && line_is_command_terminator(zLine)
35942        && line_is_complete(zSql, nSql) ){
35943      memcpy(zLine,";",2);
35944    }
35945    qss = quickscan(zLine, qss, CONTINUE_PROMPT_PSTATE);
35946    if( QSS_PLAINWHITE(qss) && nSql==0 ){
35947      /* Just swallow single-line whitespace */
35948      echo_group_input(p, zLine);
35949      qss = QSS_Start;
35950      continue;
35951    }
35952    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
35953      CONTINUE_PROMPT_RESET;
35954      echo_group_input(p, zLine);
35955      if( zLine[0]=='.' ){
35956        rc = do_meta_command(zLine, p);
35957        if( rc==2 ){ /* exit requested */
35958          break;
35959        }else if( rc ){
35960          errCnt++;
35961        }
35962      }
35963      qss = QSS_Start;
35964      continue;
35965    }
35966    /* No single-line dispositions remain; accumulate line(s). */
35967    nLine = strlen(zLine);
35968    if( nSql+nLine+2>=nAlloc ){
35969      /* Grow buffer by half-again increments when big. */
35970      nAlloc = nSql+(nSql>>1)+nLine+100;
35971      zSql = realloc(zSql, nAlloc);
35972      shell_check_oom(zSql);
35973    }
35974    if( nSql==0 ){
35975      i64 i;
35976      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
35977      assert( nAlloc>0 && zSql!=0 );
35978      memcpy(zSql, zLine+i, nLine+1-i);
35979      startline = p->lineno;
35980      nSql = nLine-i;
35981    }else{
35982      zSql[nSql++] = '\n';
35983      memcpy(zSql+nSql, zLine, nLine+1);
35984      nSql += nLine;
35985    }
35986    if( nSql>0x7fff0000 ){
35987      char zSize[100];
35988      sqlite3_snprintf(sizeof(zSize),zSize,"%,lld",nSql);
35989      cli_printf(stderr, "%s:%lld: Input SQL is too big: %s bytes\n",
35990                      zSrc, startline, zSize);
35991      nSql = 0;
35992      errCnt++;
35993      break;
35994    }else if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
35995      echo_group_input(p, zSql);
35996      errCnt += runOneSqlLine(p, zSql, p->zInFile, startline);
35997      CONTINUE_PROMPT_RESET;
35998      nSql = 0;
35999      if( p->nPopOutput ){
36000        output_reset(p);
36001        p->nPopOutput = 0;
36002      }else{
36003        clearTempFile(p);
36004      }
36005      if( p->nPopMode ){
36006        modePop(p);
36007        p->nPopMode = 0;
36008      }
36009      p->bSafeMode = p->bSafeModePersist;
36010      qss = QSS_Start;
36011    }else if( nSql && QSS_PLAINWHITE(qss) ){
36012      echo_group_input(p, zSql);
36013      nSql = 0;
36014      qss = QSS_Start;
36015    }
36016  }
36017  if( nSql ){
36018    /* This may be incomplete. Let the SQL parser deal with that. */
36019    echo_group_input(p, zSql);
36020    errCnt += runOneSqlLine(p, zSql, p->zInFile, startline);
36021    CONTINUE_PROMPT_RESET;
36022  }
36023  free(zSql);
36024  free(zLine);
36025  --p->inputNesting;
36026  p->zInFile = saved_zInFile;
36027  p->lineno = saved_lineno;
36028  return errCnt>0;
36029}
36030
36031/*
36032** Return a pathname which is the user's home directory.  A
36033** 0 return indicates an error of some kind.
36034*/
36035static char *find_home_dir(int clearFlag){
36036  static char *home_dir = NULL;
36037  if( clearFlag ){
36038    free(home_dir);
36039    home_dir = 0;
36040    return 0;
36041  }
36042  if( home_dir ) return home_dir;
36043
36044#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
36045     && !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
36046  {
36047    struct passwd *pwent;
36048    uid_t uid = getuid();
36049    if( (pwent=getpwuid(uid)) != NULL) {
36050      home_dir = pwent->pw_dir;
36051    }
36052  }
36053#endif
36054
36055#if defined(_WIN32_WCE)
36056  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
36057   */
36058  home_dir = "/";
36059#else
36060
36061#if defined(_WIN32) || defined(WIN32)
36062  if (!home_dir) {
36063    home_dir = getenv("USERPROFILE");
36064  }
36065#endif
36066
36067  if (!home_dir) {
36068    home_dir = getenv("HOME");
36069  }
36070
36071#if defined(_WIN32) || defined(WIN32)
36072  if (!home_dir) {
36073    char *zDrive, *zPath;
36074    int n;
36075    zDrive = getenv("HOMEDRIVE");
36076    zPath = getenv("HOMEPATH");
36077    if( zDrive && zPath ){
36078      n = strlen30(zDrive) + strlen30(zPath) + 1;
36079      home_dir = malloc( n );
36080      if( home_dir==0 ) return 0;
36081      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
36082      return home_dir;
36083    }
36084    home_dir = "c:\\";
36085  }
36086#endif
36087
36088#endif /* !_WIN32_WCE */
36089
36090  if( home_dir ){
36091    i64 n = strlen(home_dir) + 1;
36092    char *z = malloc( n );
36093    if( z ) memcpy(z, home_dir, n);
36094    home_dir = z;
36095  }
36096
36097  return home_dir;
36098}
36099
36100/*
36101** On non-Windows platforms, look for:
36102**
36103** - ${zEnvVar}/${zBaseName}
36104** - ${HOME}/${zSubdir}/${zBaseName}
36105**
36106** $zEnvVar is intended to be the name of an XDG_... environment
36107** variable, e.g. XDG_CONFIG_HOME or XDG_STATE_HOME.  If zEnvVar is
36108** NULL or getenv(zEnvVar) is NULL then fall back to the second
36109** option. If the selected option is not found in the filesystem,
36110** return 0.
36111**
36112** zSubdir may be NULL or empty, in which case ${HOME}/${zBaseName}
36113** becomes the fallback.
36114**
36115** Both zSubdir and zBaseName may contain subdirectory parts. zSubdir
36116** will conventionally be ".config" or ".local/state", which, not
36117** coincidentally, is the typical subdir of the corresponding XDG_...
36118** var with the XDG var's $HOME prefix.
36119**
36120** The returned string is obtained from sqlite3_malloc() and should be
36121** sqlite3_free()'d by the caller.
36122*/
36123static char *find_xdg_file(const char *zEnvVar, const char *zSubdir,
36124                           const char *zBaseName){
36125#if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
36126     || defined(__RTP__) || defined(_WRS_KERNEL)
36127  return 0;
36128#else
36129  char *zConfigFile = 0;
36130  const char *zXdgDir;
36131
36132  zXdgDir = zEnvVar ? getenv(zEnvVar) : 0;
36133  if( zXdgDir ){
36134    zConfigFile = sqlite3_mprintf("%s/%s", zXdgDir, zBaseName);
36135  }else{
36136    const char * zHome = find_home_dir(0);
36137    if( zHome==0 ) return 0;
36138    zConfigFile = (zSubdir && *zSubdir)
36139      ? sqlite3_mprintf("%s/%s/%s", zHome, zSubdir, zBaseName)
36140      : sqlite3_mprintf("%s/%s", zHome, zBaseName);
36141  }
36142  shell_check_oom(zConfigFile);
36143  if( access(zConfigFile,0)!=0 ){
36144    sqlite3_free(zConfigFile);
36145    zConfigFile = 0;
36146  }
36147  return zConfigFile;
36148#endif
36149}
36150
36151/*
36152** Read input from the file sqliterc_override.  If that parameter is
36153** NULL, take it from find_xdg_file(), if found, or fall back to
36154** ~/.sqliterc.
36155**
36156** Failure to read the config is only considered a failure if
36157** sqliterc_override is not NULL, in which case this function may emit
36158** a warning or, if ::bail_on_error is true, fail fatally if the file
36159** named by sqliterc_override is not found.
36160*/
36161static void process_sqliterc(
36162  ShellState *p,                  /* Configuration data */
36163  const char *sqliterc_override   /* Name of config file. NULL to use default */
36164){
36165  char *home_dir = NULL;
36166  char *sqliterc = (char*)sqliterc_override;
36167  FILE *inSaved = p->in;
36168  i64 savedLineno = p->lineno;
36169
36170  if( sqliterc == NULL ){
36171    sqliterc = find_xdg_file("XDG_CONFIG_HOME",
36172                             ".config",
36173                             "sqlite3/sqliterc");
36174  }
36175  if( sqliterc == NULL ){
36176    home_dir = find_home_dir(0);
36177    if( home_dir==0 ){
36178      eputz("-- warning: cannot find home directory;"
36179            " cannot read ~/.sqliterc\n");
36180      return;
36181    }
36182    sqliterc = sqlite3_mprintf("%s/.sqliterc",home_dir);
36183    shell_check_oom(sqliterc);
36184  }
36185  p->in = sqliterc ? sqlite3_fopen(sqliterc,"rb") : 0;
36186  if( p->in ){
36187    if( stdin_is_interactive ){
36188      cli_printf(stderr,"-- Loading resources from %s\n", sqliterc);
36189    }
36190    if( process_input(p, sqliterc) && bail_on_error ) cli_exit(1);
36191    fclose(p->in);
36192  }else if( sqliterc_override!=0 ){
36193    cli_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
36194    if( bail_on_error ) cli_exit(1);
36195  }
36196  p->in = inSaved;
36197  p->lineno = savedLineno;
36198  if( sqliterc != sqliterc_override ){
36199    sqlite3_free(sqliterc);
36200  }
36201}
36202
36203/*
36204** Show available command line options
36205*/
36206static const char zOptions[] =
36207  "   --                   treat no subsequent arguments as options\n"
36208#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
36209  "   -A ARGS...           run \".archive ARGS\" and exit\n"
36210#endif
36211  "   -append              append the database to the end of the file\n"
36212  "   -ascii               set output mode to 'ascii'\n"
36213  "   -bail                stop after hitting an error\n"
36214  "   -batch               force batch I/O\n"
36215  "   -box                 set output mode to 'box'\n"
36216  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
36217  "   -column              set output mode to 'column'\n"
36218  "   -csv                 set output mode to 'csv'\n"
36219#if !defined(SQLITE_OMIT_DESERIALIZE)
36220  "   -deserialize         open the database using sqlite3_deserialize()\n"
36221#endif
36222  "   -echo                print inputs before execution\n"
36223  "   -escape T            ctrl-char escape; T is one of: symbol, ascii, off\n"
36224  "   -init FILENAME       read/process named file\n"
36225  "   -[no]header          turn headers on or off\n"
36226#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
36227  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
36228#endif
36229  "   -help                show this message\n"
36230  "   -html                set output mode to HTML\n"
36231  "   -ifexists            only open if database already exists\n"
36232  "   -interactive         force interactive I/O\n"
36233  "   -json                set output mode to 'json'\n"
36234  "   -line                set output mode to 'line'\n"
36235  "   -list                set output mode to 'list'\n"
36236  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
36237  "   -markdown            set output mode to 'markdown'\n"
36238#if !defined(SQLITE_OMIT_DESERIALIZE)
36239  "   -maxsize N           maximum size for a --deserialize database\n"
36240#endif
36241  "   -memtrace            trace all memory allocations and deallocations\n"
36242  "   -mmap N              default mmap size set to N\n"
36243#ifdef SQLITE_ENABLE_MULTIPLEX
36244  "   -multiplex           enable the multiplexor VFS\n"
36245#endif
36246  "   -newline SEP         set output row separator. Default: '\\n'\n"
36247  "   -nofollow            refuse to open symbolic links to database files\n"
36248  "   -noinit              Do not read the ~/.sqliterc file at startup\n"
36249  "   -nonce STRING        set the safe-mode escape nonce\n"
36250  "   -no-rowid-in-view    Disable rowid-in-view using sqlite3_config()\n"
36251  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
36252  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
36253  "   -pcachetrace         trace all page cache operations\n"
36254  "   -quote               set output mode to 'quote'\n"
36255  "   -readonly            open the database read-only\n"
36256  "   -safe                enable safe-mode\n"
36257  "   -screenwidth N       use N as the default screenwidth \n"
36258  "   -separator SEP       set output column separator. Default: '|'\n"
36259#ifdef SQLITE_ENABLE_SORTER_REFERENCES
36260  "   -sorterref SIZE      sorter references threshold size\n"
36261#endif
36262  "   -stats               print memory stats before each finalize\n"
36263  "   -table               set output mode to 'table'\n"
36264  "   -tabs                set output mode to 'tabs'\n"
36265  "   -unsafe-testing      allow unsafe commands and modes for testing\n"
36266  "   -version             show SQLite version\n"
36267  "   -vfs NAME            use NAME as the default VFS\n"
36268  "   -vfstrace            enable tracing of all VFS calls\n"
36269#ifdef SQLITE_HAVE_ZLIB
36270  "   -zip                 open the file as a ZIP Archive\n"
36271#endif
36272;
36273static void usage(int showDetail){
36274  cli_printf(stderr,"Usage: %s [OPTIONS] [FILENAME [SQL...]]\n"
36275       "FILENAME is the name of an SQLite database. A new database is created\n"
36276       "if the file does not previously exist. Defaults to :memory:.\n", Argv0);
36277  if( showDetail ){
36278    cli_printf(stderr,"OPTIONS include:\n%s", zOptions);
36279  }else{
36280    eputz("Use the -help option for additional information\n");
36281  }
36282  exit(0);
36283}
36284
36285/*
36286** Internal check:  Verify that the SQLite is uninitialized.  Print a
36287** error message if it is initialized.
36288*/
36289static void verify_uninitialized(void){
36290  if( sqlite3_config(-1)==SQLITE_MISUSE ){
36291    sputz(stdout, "WARNING: attempt to configure SQLite after"
36292          " initialization.\n");
36293  }
36294}
36295
36296/*
36297** Initialize the state information in data
36298*/
36299static void main_init(ShellState *p) {
36300  memset(p, 0, sizeof(*p));
36301  p->pAuxDb = &p->aAuxDb[0];
36302  p->shellFlgs = SHFLG_Lookaside;
36303  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, p);
36304#if !defined(SQLITE_SHELL_FIDDLE)
36305  verify_uninitialized();
36306#endif
36307  sqlite3_config(SQLITE_CONFIG_URI, 1);
36308  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
36309  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
36310  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
36311}
36312
36313/*
36314** Output text to the console in a font that attracts extra attention.
36315*/
36316#if defined(_WIN32) || defined(WIN32)
36317static void printBold(const char *zText){
36318  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
36319  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
36320  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
36321  SetConsoleTextAttribute(out,
36322         FOREGROUND_RED|FOREGROUND_INTENSITY
36323  );
36324  sputz(stdout, zText);
36325  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
36326}
36327#else
36328static void printBold(const char *zText){
36329  cli_printf(stdout, "\033[1m%s\033[0m", zText);
36330}
36331#endif
36332
36333/*
36334** Get the argument to an --option.  Throw an error and die if no argument
36335** is available.
36336*/
36337static char *cmdline_option_value(int argc, char **argv, int i){
36338  if( i==argc ){
36339    cli_printf(stderr,
36340            "%s: Error: missing argument to %s\n", argv[0], argv[argc-1]);
36341    cli_exit(1);
36342  }
36343  return argv[i];
36344}
36345
36346static void sayAbnormalExit(void){
36347  if( seenInterrupt ) eputz("Program interrupted.\n");
36348}
36349
36350/* Routine to output from vfstrace
36351*/
36352static int vfstraceOut(const char *z, void *pArg){
36353  ShellState *p = (ShellState*)pArg;
36354  cli_puts(z, p->out);
36355  fflush(p->out);
36356  return 1;
36357}
36358
36359#if defined(SQLITE_DEBUG) && !defined(SQLITE_SHELL_FIDDLE)
36360/* Ensure that sqlite3_reset_auto_extension() clears auto-extension
36361** memory. https://sqlite.org/forum/forumpost/310cb231b07c80d1.
36362** Testing this: if we (inadvertently) remove the
36363** sqlite3_reset_auto_extension() call from main(), most shell tests
36364** will fail because of a leak message in their output. */
36365static int auto_ext_leak_tester(
36366  sqlite3 *db,
36367  char **pzErrMsg,
36368  const struct sqlite3_api_routines *pThunk
36369){
36370  (void)db; (void)pzErrMsg; (void)pThunk;
36371  return SQLITE_OK;
36372}
36373#endif
36374
36375/* Alternative name to the entry point for Fiddle */
36376#ifdef SQLITE_SHELL_FIDDLE
36377#  define main fiddle_main
36378#endif
36379
36380/* Use the wmain() entry point on Windows.  Translate arguments to
36381** UTF8, then invoke the traditional main() entry point which is
36382** renamed using a #define to utf8_main() .
36383*/
36384#if defined(_WIN32) && !defined(__MINGW32__) && !defined(main)
36385#  define main utf8_main                 /* Rename entry point to utf_main() */
36386int SQLITE_CDECL utf8_main(int,char**);  /* Forward declaration */  
36387int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
36388  int rc, i;
36389  char **argv = malloc( sizeof(char*) * (argc+1) );
36390  char **orig = argv;
36391  if( argv==0 ){
36392    fprintf(stderr, "malloc failed\n");
36393    exit(1);
36394  }
36395  for(i=0; i<argc; i++){
36396    int nByte = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, 0, 0, 0, 0);
36397    if( nByte==0 ){
36398      argv[i] = 0;
36399    }else{
36400      argv[i] = malloc( nByte );
36401      if( argv[i]==0 ){
36402        fprintf(stderr, "malloc failed\n");
36403        exit(1);
36404      }
36405      nByte = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, argv[i],nByte,0,0);
36406      if( nByte==0 ){
36407        free(argv[i]);
36408        argv[i] = 0;
36409      }
36410    }
36411  }
36412  argv[argc] = 0;
36413  rc = utf8_main(argc, argv);
36414  for(i=0; i<argc; i++) free(orig[i]);
36415  free(argv);
36416  return rc;
36417}
36418#endif /* WIN32 */
36419
36420/*
36421** This is the main entry point for the process.  Everything starts here.
36422**
36423** The "main" identifier may have been #defined to something else:
36424**
36425**     utf8_main            On Windows
36426**     fiddle_main          In Fiddle
36427**     sqlite3_shell        Other projects that use shell.c as a subroutine
36428*/
36429int SQLITE_CDECL main(int argc, char **argv){
36430#ifdef SQLITE_DEBUG
36431  sqlite3_int64 mem_main_enter = 0;
36432#endif
36433#ifdef SQLITE_SHELL_FIDDLE
36434#  define data shellState
36435#else
36436  ShellState data;
36437#endif
36438  const char *zInitFile = 0;
36439  int i;
36440  int rc = 0;
36441  int warnInmemoryDb = 0;
36442  int readStdin = 1;
36443  int noInit = 0;                 /* Do not read ~/.sqliterc if true */
36444  int nCmd = 0;
36445  int nOptsEnd = argc;
36446  int bEnableVfstrace = 0;
36447  char **azCmd = 0;
36448  int *aiCmd = 0;
36449  const char *zVfs = 0;           /* Value of -vfs command-line option */
36450  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
36451
36452#ifdef SQLITE_SHELL_FIDDLE
36453  stdin_is_interactive = 0;
36454  stdout_is_console = 1;
36455  data.wasm.zDefaultDbName = "/fiddle.sqlite3";
36456#else
36457  stdin_is_interactive = isatty(0);
36458  stdout_is_console = isatty(1);
36459#endif
36460  atexit(sayAbnormalExit);
36461#ifdef SQLITE_DEBUG
36462  mem_main_enter = sqlite3_memory_used();
36463#endif
36464#if !defined(_WIN32_WCE)
36465  if( getenv("SQLITE_DEBUG_BREAK") ){
36466    if( isatty(0) && isatty(2) ){
36467      char zLine[100];
36468      cli_printf(stderr,
36469            "attach debugger to process %d and press ENTER to continue...",
36470            GETPID());
36471      if( sqlite3_fgets(zLine, sizeof(zLine), stdin)!=0
36472       && cli_strcmp(zLine,"stop")==0
36473      ){
36474        exit(1);
36475      }
36476    }else{
36477#if defined(_WIN32) || defined(WIN32)
36478      DebugBreak();
36479#elif defined(SIGTRAP)
36480      raise(SIGTRAP);
36481#endif
36482    }
36483  }
36484#endif
36485  /* Register a valid signal handler early, before much else is done. */
36486#ifdef SIGINT
36487  signal(SIGINT, interrupt_handler);
36488#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
36489  if( !SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE) ){
36490    eputz("No ^C handler.\n");
36491  }
36492#endif
36493
36494#if USE_SYSTEM_SQLITE+0!=1
36495  if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
36496    cli_printf(stderr,
36497          "SQLite header and source version mismatch\n%s\n%s\n",
36498          sqlite3_sourceid(), SQLITE_SOURCE_ID);
36499    exit(1);
36500  }
36501#endif
36502  main_init(&data);
36503
36504  assert( argc>=1 && argv && argv[0] );
36505  Argv0 = argv[0];
36506
36507#ifdef SQLITE_SHELL_DBNAME_PROC
36508  {
36509    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
36510    ** of a C-function that will provide the name of the database file.  Use
36511    ** this compile-time option to embed this shell program in larger
36512    ** applications. */
36513    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
36514    SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
36515    warnInmemoryDb = 0;
36516  }
36517#endif
36518
36519  /* Do an initial pass through the command-line arguments to locate
36520  ** the name of the database file, the name of the initialization file,
36521  ** the size of the alternative malloc heap, options affecting commands
36522  ** or SQL run from the command line, and the first command to execute.
36523  */
36524#ifndef SQLITE_SHELL_FIDDLE
36525  verify_uninitialized();
36526#endif
36527  for(i=1; i<argc; i++){
36528    char *z;
36529    z = argv[i];
36530    if( z[0]!='-' || i>nOptsEnd ){
36531      if( data.aAuxDb->zDbFilename==0 && !isScriptFile(z,1) ){
36532        data.aAuxDb->zDbFilename = z;
36533      }else{
36534        /* Excess arguments are interpreted as SQL (or dot-commands) and
36535        ** mean that nothing is read from stdin */
36536        readStdin = 0;
36537        stdin_is_interactive = 0;
36538        nCmd++;
36539        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
36540        shell_check_oom(azCmd);
36541        aiCmd = realloc(aiCmd, sizeof(aiCmd[0])*nCmd);
36542        shell_check_oom(azCmd);
36543        azCmd[nCmd-1] = z;
36544        aiCmd[nCmd-1] = i;
36545      }
36546      continue;
36547    }
36548    if( z[1]=='-' ) z++;
36549    if( cli_strcmp(z, "-")==0 ){
36550      nOptsEnd = i;
36551      continue;
36552    }else if( cli_strcmp(z,"-separator")==0
36553     || cli_strcmp(z,"-nullvalue")==0
36554     || cli_strcmp(z,"-newline")==0
36555     || cli_strcmp(z,"-cmd")==0
36556    ){
36557      (void)cmdline_option_value(argc, argv, ++i);
36558    }else if( cli_strcmp(z,"-init")==0 ){
36559      zInitFile = cmdline_option_value(argc, argv, ++i);
36560    }else if( cli_strcmp(z,"-interactive")==0 ){
36561    }else if( cli_strcmp(z,"-batch")==0 ){
36562      /* Need to check for batch mode here to so we can avoid printing
36563      ** informational messages (like from process_sqliterc) before
36564      ** we do the actual processing of arguments later in a second pass.
36565      */
36566      stdin_is_interactive = 0;
36567      stdout_is_console = 0;
36568      modeChange(&data, MODE_BATCH);
36569    }else if( cli_strcmp(z,"-screenwidth")==0 ){
36570      int n = atoi(cmdline_option_value(argc, argv, ++i));
36571      if( n<2 ){
36572        sqlite3_fprintf(stderr,"minimum --screenwidth is 2\n");
36573        exit(1);
36574      }
36575      stdout_tty_width = n;
36576    }else if( cli_strcmp(z,"-utf8")==0 ){
36577    }else if( cli_strcmp(z,"-no-utf8")==0 ){
36578    }else if( cli_strcmp(z,"-no-rowid-in-view")==0 ){
36579      int val = 0;
36580      sqlite3_config(SQLITE_CONFIG_ROWID_IN_VIEW, &val);
36581      assert( val==0 );
36582    }else if( cli_strcmp(z,"-heap")==0 ){
36583#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
36584      const char *zSize;
36585      sqlite3_int64 szHeap;
36586
36587      zSize = cmdline_option_value(argc, argv, ++i);
36588      szHeap = integerValue(zSize);
36589      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
36590      verify_uninitialized();
36591      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
36592#else
36593      (void)cmdline_option_value(argc, argv, ++i);
36594#endif
36595    }else if( cli_strcmp(z,"-pagecache")==0 ){
36596      sqlite3_int64 n, sz;
36597      sz = integerValue(cmdline_option_value(argc,argv,++i));
36598      if( sz>65536 ) sz = 65536;
36599      if( sz<0 ) sz = 0;
36600      n = integerValue(cmdline_option_value(argc,argv,++i));
36601      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
36602        n = 0xffffffffffffLL/sz;
36603      }
36604      if( sz>0 && (sz & (sz-1))==0 ){
36605        /* If SIZE is a power of two, round it up by the PCACHE_HDRSZ */
36606        int szHdr = 0;
36607        sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &szHdr);
36608        sz += szHdr;
36609        cli_printf(stdout, "Page cache size increased to %d to accommodate"
36610                        " the %d-byte headers\n", (int)sz, szHdr);
36611      }
36612      verify_uninitialized();
36613      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
36614                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
36615      data.shellFlgs |= SHFLG_Pagecache;
36616    }else if( cli_strcmp(z,"-lookaside")==0 ){
36617      int n, sz;
36618      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
36619      if( sz<0 ) sz = 0;
36620      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
36621      if( n<0 ) n = 0;
36622      verify_uninitialized();
36623      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
36624      if( (i64)sz*(i64)n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
36625    }else if( cli_strcmp(z,"-threadsafe")==0 ){
36626      int n;
36627      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
36628      verify_uninitialized();
36629      switch( n ){
36630         case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;
36631         case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;
36632         default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;
36633      }
36634    }else if( cli_strcmp(z,"-vfstrace")==0 ){
36635      bEnableVfstrace = 1;
36636#ifdef SQLITE_ENABLE_MULTIPLEX
36637    }else if( cli_strcmp(z,"-multiplex")==0 ){
36638      extern int sqlite3_multiplex_initialize(const char*,int);
36639      sqlite3_multiplex_initialize(0, 1);
36640#endif
36641    }else if( cli_strcmp(z,"-mmap")==0 ){
36642      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
36643      verify_uninitialized();
36644      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
36645#if defined(SQLITE_ENABLE_SORTER_REFERENCES)
36646    }else if( cli_strcmp(z,"-sorterref")==0 ){
36647      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
36648      verify_uninitialized();
36649      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
36650#endif
36651    }else if( cli_strcmp(z,"-vfs")==0 ){
36652      zVfs = cmdline_option_value(argc, argv, ++i);
36653#ifdef SQLITE_HAVE_ZLIB
36654    }else if( cli_strcmp(z,"-zip")==0 ){
36655      data.openMode = SHELL_OPEN_ZIPFILE;
36656#endif
36657    }else if( cli_strcmp(z,"-append")==0 ){
36658      data.openMode = SHELL_OPEN_APPENDVFS;
36659#ifndef SQLITE_OMIT_DESERIALIZE
36660    }else if( cli_strcmp(z,"-deserialize")==0 ){
36661      data.openMode = SHELL_OPEN_DESERIALIZE;
36662    }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
36663      data.szMax = integerValue(argv[++i]);
36664#endif
36665    }else if( cli_strcmp(z,"-readonly")==0 ){
36666      data.openFlags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
36667      data.openFlags |= SQLITE_OPEN_READONLY;
36668    }else if( cli_strcmp(z,"-nofollow")==0 ){
36669      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
36670    }else if( cli_strcmp(z,"-noinit")==0 ){
36671      noInit = 1;
36672    }else if( cli_strcmp(z,"-exclusive")==0 ){  /* UNDOCUMENTED */
36673      data.openFlags |= SQLITE_OPEN_EXCLUSIVE;
36674    }else if( cli_strcmp(z,"-ifexists")==0 ){
36675      data.openFlags &= ~(SQLITE_OPEN_CREATE);
36676      if( data.openFlags==0 ) data.openFlags = SQLITE_OPEN_READWRITE;
36677#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
36678    }else if( cli_strncmp(z, "-A",2)==0 ){
36679      /* All remaining command-line arguments are passed to the ".archive"
36680      ** command, so ignore them */
36681      break;
36682#endif
36683    }else if( cli_strcmp(z, "-memtrace")==0 ){
36684      sqlite3MemTraceActivate(stderr);
36685    }else if( cli_strcmp(z, "-pcachetrace")==0 ){
36686      sqlite3PcacheTraceActivate(stderr);
36687    }else if( cli_strcmp(z,"-bail")==0 ){
36688      bail_on_error = 1;
36689    }else if( cli_strcmp(z,"-nonce")==0 ){
36690      free(data.zNonce);
36691      data.zNonce = strdup(cmdline_option_value(argc, argv, ++i));
36692    }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
36693      ShellSetFlag(&data,SHFLG_TestingMode);
36694    }else if( cli_strcmp(z,"-safe")==0 ){
36695      /* no-op - catch this on the second pass */
36696    }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){
36697      /* skip over the argument */
36698      i++;
36699    }else if( cli_strcmp(z,"-test-argv")==0 ){
36700      /* Undocumented test option.  Print the values in argv[] and exit.
36701      ** Use this to verify that any translation of the argv[], for example
36702      ** on Windows that receives wargv[] from the OS and must convert
36703      ** to UTF8 prior to calling this routine. */
36704      int kk;
36705      for(kk=0; kk<argc; kk++){
36706        sqlite3_fprintf(stdout,"argv[%d] = \"%s\"\n", kk, argv[kk]);
36707      }
36708      return 0;
36709    }
36710  }
36711#ifndef SQLITE_SHELL_FIDDLE
36712  if( !bEnableVfstrace ) verify_uninitialized();
36713#endif
36714
36715
36716#ifdef SQLITE_SHELL_INIT_PROC
36717  {
36718    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
36719    ** of a C-function that will perform initialization actions on SQLite that
36720    ** occur just before or after sqlite3_initialize(). Use this compile-time
36721    ** option to embed this shell program in larger applications. */
36722    extern void SQLITE_SHELL_INIT_PROC(void);
36723    SQLITE_SHELL_INIT_PROC();
36724  }
36725#else
36726  /* All the sqlite3_config() calls have now been made. So it is safe
36727  ** to call sqlite3_initialize() and process any command line -vfs option. */
36728  sqlite3_initialize();
36729#endif
36730
36731  if( zVfs ){
36732    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
36733    if( pVfs ){
36734      sqlite3_vfs_register(pVfs, 1);
36735    }
36736#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
36737    else if( access(zVfs,0)==0 ){
36738      /* If the VFS name is not the name of an existing VFS, but it is
36739      ** the name of a file, then try to load that file as an extension.
36740      ** Presumably the extension implements the desired VFS. */
36741      sqlite3 *db = 0;
36742      char *zErr = 0;
36743      sqlite3_open(":memory:", &db);
36744      sqlite3_enable_load_extension(db, 1);
36745      rc = sqlite3_load_extension(db, zVfs, 0, &zErr);
36746      sqlite3_close(db);
36747      if( (rc&0xff)!=SQLITE_OK ){
36748        cli_printf(stderr, "could not load extension VFS \"%s\": %s\n",
36749                   zVfs, zErr);
36750        exit(1);
36751      }
36752    }
36753#endif
36754    else{
36755      cli_printf(stderr,"no such VFS: \"%s\"\n", zVfs);
36756      exit(1);
36757    }
36758  }
36759
36760  if( data.pAuxDb->zDbFilename==0 ){
36761#ifndef SQLITE_OMIT_MEMORYDB
36762    data.pAuxDb->zDbFilename = ":memory:";
36763    warnInmemoryDb = argc==1;
36764#else
36765    cli_printf(stderr,
36766                    "%s: Error: no database filename specified\n", Argv0);
36767    rc = 1;
36768    goto shell_main_exit;
36769#endif
36770  }
36771  data.out = stdout;
36772  if( bEnableVfstrace ){
36773    vfstrace_register("trace",0,vfstraceOut, &data, 1);
36774  }
36775#ifndef SQLITE_SHELL_FIDDLE
36776  sqlite3_appendvfs_init(0,0,0);
36777#ifdef SQLITE_DEBUG
36778  sqlite3_auto_extension( (void (*)(void))auto_ext_leak_tester );
36779#endif
36780#endif
36781  modeDefault(&data);
36782
36783  /* Go ahead and open the database file if it already exists.  If the
36784  ** file does not exist, delay opening it.  This prevents empty database
36785  ** files from being created if a user mistypes the database name argument
36786  ** to the sqlite command-line tool.
36787  */
36788  if( access(data.pAuxDb->zDbFilename, 0)==0 ){
36789    open_db(&data, 0);
36790  }
36791
36792  /* Process the initialization file if there is one.  If no -init option
36793  ** is given on the command line, look for a file named ~/.sqliterc and
36794  ** try to process it.
36795  */
36796  if( !noInit ) process_sqliterc(&data,zInitFile);
36797
36798  /* Make a second pass through the command-line arguments and set
36799  ** options.  This second pass is delayed until after the initialization
36800  ** file is processed so that the command-line arguments will override
36801  ** settings in the initialization file.
36802  */
36803  for(i=1; i<argc; i++){
36804    char *z = argv[i];
36805    if( z[0]!='-' || i>=nOptsEnd ) continue;
36806    if( z[1]=='-' ){ z++; }
36807    if( cli_strcmp(z,"-init")==0 ){
36808      i++;
36809    }else if( cli_strcmp(z,"-html")==0 ){
36810      modeChange(&data, MODE_Html);
36811    }else if( cli_strcmp(z,"-list")==0 ){
36812      modeChange(&data, MODE_List);
36813    }else if( cli_strcmp(z,"-quote")==0 ){
36814      modeChange(&data, MODE_Quote);
36815    }else if( cli_strcmp(z,"-line")==0 ){
36816      modeChange(&data, MODE_Line);
36817    }else if( cli_strcmp(z,"-column")==0 ){
36818      modeChange(&data, MODE_Column);
36819    }else if( cli_strcmp(z,"-json")==0 ){
36820      modeChange(&data, MODE_Json);
36821    }else if( cli_strcmp(z,"-markdown")==0 ){
36822      modeChange(&data, MODE_Markdown);
36823    }else if( cli_strcmp(z,"-table")==0 ){
36824      modeChange(&data, MODE_Table);
36825    }else if( cli_strcmp(z,"-psql")==0 ){
36826      modeChange(&data, MODE_Psql);
36827    }else if( cli_strcmp(z,"-box")==0 ){
36828      modeChange(&data, MODE_Box);
36829    }else if( cli_strcmp(z,"-csv")==0 ){
36830      modeChange(&data, MODE_Csv);
36831    }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){
36832      /* See similar code at tag-20250224-1 */
36833      const char *zEsc = argv[++i];
36834      int k;
36835      for(k=0; k<ArraySize(qrfEscNames); k++){
36836        if( sqlite3_stricmp(zEsc,qrfEscNames[k])==0 ){
36837          data.mode.spec.eEsc = k;
36838          break;
36839        }
36840      }
36841      if( k>=ArraySize(qrfEscNames) ){
36842        cli_printf(stderr, "unknown control character escape mode \"%s\""
36843                                " - choices:", zEsc);
36844        for(k=0; k<ArraySize(qrfEscNames); k++){
36845          cli_printf(stderr, " %s", qrfEscNames[k]);
36846        }
36847        cli_printf(stderr, "\n");
36848        exit(1);
36849      }
36850#ifdef SQLITE_HAVE_ZLIB
36851    }else if( cli_strcmp(z,"-zip")==0 ){
36852      data.openMode = SHELL_OPEN_ZIPFILE;
36853#endif
36854    }else if( cli_strcmp(z,"-append")==0 ){
36855      data.openMode = SHELL_OPEN_APPENDVFS;
36856#ifndef SQLITE_OMIT_DESERIALIZE
36857    }else if( cli_strcmp(z,"-deserialize")==0 ){
36858      data.openMode = SHELL_OPEN_DESERIALIZE;
36859    }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
36860      data.szMax = integerValue(argv[++i]);
36861#endif
36862    }else if( cli_strcmp(z,"-readonly")==0 ){
36863      data.openFlags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
36864      data.openFlags |= SQLITE_OPEN_READONLY;
36865    }else if( cli_strcmp(z,"-nofollow")==0 ){
36866      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
36867    }else if( cli_strcmp(z,"-noinit")==0 ){
36868      /* No-op */
36869    }else if( cli_strcmp(z,"-exclusive")==0 ){  /* UNDOCUMENTED */
36870      data.openFlags |= SQLITE_OPEN_EXCLUSIVE;
36871    }else if( cli_strcmp(z,"-ifexists")==0 ){
36872      data.openFlags &= ~(SQLITE_OPEN_CREATE);
36873      if( data.openFlags==0 ) data.openFlags = SQLITE_OPEN_READWRITE;
36874    }else if( cli_strcmp(z,"-ascii")==0 ){
36875      modeChange(&data, MODE_Ascii);
36876    }else if( cli_strcmp(z,"-tabs")==0 ){
36877      modeChange(&data, MODE_Tabs);
36878    }else if( cli_strcmp(z,"-separator")==0 ){
36879      modeSetStr(&data.mode.spec.zColumnSep, 
36880                       cmdline_option_value(argc,argv,++i));
36881    }else if( cli_strcmp(z,"-newline")==0 ){
36882      modeSetStr(&data.mode.spec.zRowSep, 
36883                       cmdline_option_value(argc,argv,++i));
36884    }else if( cli_strcmp(z,"-nullvalue")==0 ){
36885     modeSetStr(&data.mode.spec.zNull, 
36886                       cmdline_option_value(argc,argv,++i));
36887    }else if( cli_strcmp(z,"-header")==0 ){
36888      data.mode.spec.bTitles = QRF_Yes;
36889     }else if( cli_strcmp(z,"-noheader")==0 ){
36890      data.mode.spec.bTitles = QRF_No;
36891    }else if( cli_strcmp(z,"-echo")==0 ){
36892      data.mode.mFlags |= MFLG_ECHO;
36893    }else if( cli_strcmp(z,"-eqp")==0 ){
36894      data.mode.autoEQP = AUTOEQP_on;
36895    }else if( cli_strcmp(z,"-eqpfull")==0 ){
36896      data.mode.autoEQP = AUTOEQP_full;
36897    }else if( cli_strcmp(z,"-stats")==0 ){
36898      data.statsOn = 1;
36899    }else if( cli_strcmp(z,"-scanstats")==0 ){
36900      data.mode.scanstatsOn = 1;
36901    }else if( cli_strcmp(z,"-backslash")==0 ){
36902      /* Undocumented command-line option: -backslash
36903      ** Causes C-style backslash escapes to be evaluated in SQL statements
36904      ** prior to sending the SQL into SQLite.  Useful for injecting
36905      ** crazy bytes in the middle of SQL statements for testing and debugging.
36906      */
36907      ShellSetFlag(&data, SHFLG_Backslash);
36908    }else if( cli_strcmp(z,"-bail")==0 ){
36909      /* No-op.  The bail_on_error flag should already be set. */
36910    }else if( cli_strcmp(z,"-version")==0 ){
36911      cli_printf(stdout, "%s %s (%d-bit)\n",
36912            sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*));
36913      rc = 0;
36914      goto shell_main_exit;
36915    }else if( cli_strcmp(z,"-interactive")==0 ){
36916      /* Need to check for interactive override here to so that it can
36917      ** affect console setup (for Windows only) and testing thereof.
36918      */
36919      stdin_is_interactive = 1;
36920    }else if( cli_strcmp(z,"-batch")==0 ){
36921      /* already handled */
36922    }else if( cli_strcmp(z,"-screenwidth")==0 ){
36923      i++;      
36924    }else if( cli_strcmp(z,"-utf8")==0 ){
36925      /* already handled */
36926    }else if( cli_strcmp(z,"-no-utf8")==0 ){
36927      /* already handled */
36928    }else if( cli_strcmp(z,"-no-rowid-in-view")==0 ){
36929      /* already handled */
36930    }else if( cli_strcmp(z,"-heap")==0 ){
36931      i++;
36932    }else if( cli_strcmp(z,"-pagecache")==0 ){
36933      i+=2;
36934    }else if( cli_strcmp(z,"-lookaside")==0 ){
36935      i+=2;
36936    }else if( cli_strcmp(z,"-threadsafe")==0 ){
36937      i+=2;
36938    }else if( cli_strcmp(z,"-nonce")==0 ){
36939      i += 2;
36940    }else if( cli_strcmp(z,"-mmap")==0 ){
36941      i++;
36942    }else if( cli_strcmp(z,"-memtrace")==0 ){
36943      i++;
36944    }else if( cli_strcmp(z,"-pcachetrace")==0 ){
36945      i++;
36946#ifdef SQLITE_ENABLE_SORTER_REFERENCES
36947    }else if( cli_strcmp(z,"-sorterref")==0 ){
36948      i++;
36949#endif
36950    }else if( cli_strcmp(z,"-vfs")==0 ){
36951      i++;
36952    }else if( cli_strcmp(z,"-vfstrace")==0 ){
36953      i++;
36954#ifdef SQLITE_ENABLE_MULTIPLEX
36955    }else if( cli_strcmp(z,"-multiplex")==0 ){
36956      i++;
36957#endif
36958    }else if( cli_strcmp(z,"-help")==0 ){
36959      usage(1);
36960    }else if( cli_strcmp(z,"-cmd")==0 ){
36961      /* Run commands that follow -cmd first and separately from commands
36962      ** that simply appear on the command-line.  This seems goofy.  It would
36963      ** be better if all commands ran in the order that they appear.  But
36964      ** we retain the goofy behavior for historical compatibility. */
36965      if( i==argc-1 ) break;
36966      z = cmdline_option_value(argc,argv,++i);
36967      if( z[0]=='.' ){
36968        rc = do_meta_command(z, &data);
36969        if( rc && (bail_on_error || rc==2) ){
36970          if( rc==2 ) rc = 0;
36971          goto shell_main_exit;
36972        }
36973      }else{
36974        rc = runOneSqlLine(&data, z, "cmdline", i);
36975        if( bail_on_error ) goto shell_main_exit;
36976      }
36977#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
36978    }else if( cli_strncmp(z, "-A", 2)==0 ){
36979      if( nCmd>0 ){
36980        cli_printf(stderr,"Error: cannot mix regular SQL or dot-commands"
36981              " with \"%s\"\n", z);
36982        rc = 1;
36983        goto shell_main_exit;
36984      }
36985      open_db(&data, OPEN_DB_ZIPFILE);
36986      if( z[2] ){
36987        argv[i] = &z[2];
36988        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
36989      }else{
36990        arDotCommand(&data, 1, argv+i, argc-i);
36991      }
36992      readStdin = 0;
36993      stdin_is_interactive = 0;
36994      break;
36995#endif
36996    }else if( cli_strcmp(z,"-safe")==0 ){
36997      data.bSafeMode = data.bSafeModePersist = 1;
36998    }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
36999      /* Acted upon in first pass. */
37000    }else{
37001      cli_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
37002      eputz("Use -help for a list of options.\n");
37003      rc = 1;
37004      goto shell_main_exit;
37005    }
37006  }
37007
37008  if( !readStdin ){
37009    /* Run all arguments that do not begin with '-' as if they were separate
37010    ** command-line inputs, except for the argToSkip argument which contains
37011    ** the database filename.
37012    */
37013    for(i=0; i<nCmd; i++){
37014      echo_group_input(&data, azCmd[i]);
37015      if( isScriptFile(azCmd[i],0) ){
37016        FILE *inSaved = data.in;
37017        i64 savedLineno = data.lineno;
37018        int res = 1;
37019        if( (data.in = openChrSource(azCmd[i]))!=0 ){
37020          res = process_input(&data, azCmd[i]);
37021          fclose(data.in);
37022        }
37023        data.in = inSaved;
37024        data.lineno = savedLineno;
37025        if( res ) i = nCmd;
37026      }else if( azCmd[i][0]=='.' ){
37027        char *zErrCtx = malloc( 64 );
37028        shell_check_oom(zErrCtx);
37029        sqlite3_snprintf(64,zErrCtx,"argv[%i]:",aiCmd[i]);
37030        data.zInFile = "<cmdline>";
37031        data.zErrPrefix = zErrCtx;
37032        rc = do_meta_command(azCmd[i], &data);
37033        free(data.zErrPrefix);
37034        data.zErrPrefix = 0;
37035        if( rc ){
37036          if( rc==2 ) rc = 0;
37037          goto shell_main_exit;
37038        }
37039      }else{
37040        rc = runOneSqlLine(&data, azCmd[i], "cmdline", aiCmd[i]);
37041        if( data.nPopMode ){
37042          modePop(&data);
37043          data.nPopMode = 0;
37044        }
37045        if( rc ){
37046          goto shell_main_exit;
37047        }
37048
37049      }
37050      if( data.nPopOutput && azCmd[i][0]!='.' ){
37051        output_reset(&data);
37052        data.nPopOutput = 0;
37053      }else{
37054        clearTempFile(&data);
37055      }
37056    }
37057  }else{
37058    /* Run commands received from standard input
37059    */
37060    if( stdin_is_interactive ){
37061      char *zHome;
37062      char *zHistory;
37063      cli_printf(stdout,
37064            "SQLite version %s %.19s\n" /*extra-version-info*/
37065            "Enter \".help\" for usage hints.\n",
37066            sqlite3_libversion(), sqlite3_sourceid());
37067      if( warnInmemoryDb ){
37068        sputz(stdout, "Connected to a ");
37069        printBold("transient in-memory database");
37070        sputz(stdout, ".\nUse \".open FILENAME\" to reopen on a"
37071              " persistent database.\n");
37072      }
37073      zHistory = getenv("SQLITE_HISTORY");
37074      if( zHistory ){
37075        zHistory = sqlite3_mprintf("%s", zHistory);
37076        shell_check_oom(zHistory);
37077      }else{
37078        zHistory = find_xdg_file("XDG_STATE_HOME",
37079                                 ".local/state",
37080                                 "sqlite_history");
37081        if( 0==zHistory && (zHome = find_home_dir(0))!=0 ){
37082          zHistory = sqlite3_mprintf("%s/.sqlite_history", zHome);
37083          shell_check_oom(zHistory);
37084        }
37085      }
37086      if( zHistory ){ shell_read_history(zHistory); }
37087#if (HAVE_READLINE || HAVE_EDITLINE) && !defined(SQLITE_OMIT_READLINE_COMPLETION)
37088      rl_attempted_completion_function = readline_completion;
37089#elif HAVE_LINENOISE==1
37090      linenoiseSetCompletionCallback(linenoise_completion);
37091#elif HAVE_LINENOISE==2
37092      linenoiseSetCompletionCallback(linenoise_completion, NULL);
37093#endif
37094      data.in = 0;
37095      rc = process_input(&data, "<stdin>");
37096      if( zHistory ){
37097        shell_stifle_history(2000);
37098        shell_write_history(zHistory);
37099        sqlite3_free(zHistory);
37100      }
37101    }else{
37102      data.in = stdin;
37103      rc = process_input(&data, "<stdin>");
37104    }
37105  }
37106#ifndef SQLITE_SHELL_FIDDLE
37107  /* In WASM mode we have to leave the db state in place so that
37108  ** client code can "push" SQL into it after this call returns. */
37109#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
37110  if( data.expert.pExpert ){
37111    expertFinish(&data, 1, 0);
37112  }
37113#endif
37114 shell_main_exit:
37115  free(azCmd);
37116  free(aiCmd);
37117  set_table_name(&data, 0);
37118  if( data.db ){
37119    session_close_all(&data, -1);
37120    close_db(data.db);
37121  }
37122  for(i=0; i<ArraySize(data.aAuxDb); i++){
37123    sqlite3_free(data.aAuxDb[i].zFreeOnClose);
37124    if( data.aAuxDb[i].db ){
37125      session_close_all(&data, i);
37126      close_db(data.aAuxDb[i].db);
37127    }
37128  }
37129  find_home_dir(1);
37130  output_reset(&data);
37131  data.doXdgOpen = 0;
37132  clearTempFile(&data);
37133  modeFree(&data.mode);
37134  if( data.nSavedModes ){
37135    int ii;
37136    for(ii=0; ii<data.nSavedModes; ii++){
37137      modeFree(&data.aSavedModes[ii].mode);
37138      free(data.aSavedModes[ii].zTag);
37139    }
37140    free(data.aSavedModes);
37141  }
37142  free(data.zErrPrefix);
37143  free(data.zNonce);
37144  free(data.dot.zCopy);
37145  free(data.dot.azArg);
37146  free(data.dot.aiOfst);
37147  free(data.dot.abQuot);
37148  if( data.nTestRun ){
37149    sqlite3_fprintf(stdout, "%d test%s run with %d error%s\n",
37150       data.nTestRun, data.nTestRun==1 ? "" : "s",
37151       data.nTestErr, data.nTestErr==1 ? "" : "s");
37152    fflush(stdout);
37153    rc = data.nTestErr>0;
37154  }
37155  /* Clear the global data structure so that valgrind will detect memory
37156  ** leaks */
37157  memset(&data, 0, sizeof(data));
37158  if( bEnableVfstrace ){
37159    vfstrace_unregister("trace");
37160  }
37161  sqlite3_reset_auto_extension();
37162#ifdef SQLITE_DEBUG
37163  if( sqlite3_memory_used()>mem_main_enter ){
37164    cli_printf(stderr,"Memory leaked: %u bytes\n",
37165          (unsigned int)(sqlite3_memory_used()-mem_main_enter));
37166  }
37167#endif
37168#else /* SQLITE_SHELL_FIDDLE... */
37169  shell_main_exit:
37170#endif
37171  return rc;
37172}
37173
37174
37175#ifdef SQLITE_SHELL_FIDDLE
37176/* Only for emcc experimentation purposes. */
37177int fiddle_experiment(int a,int b){
37178  return a + b;
37179}
37180
37181/*
37182** Returns a pointer to the current DB handle.
37183*/
37184sqlite3 * fiddle_db_handle(){
37185  return globalDb;
37186}
37187
37188/*
37189** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
37190** "main" is assumed. Returns 0 if no db with the given name is
37191** open.
37192*/
37193sqlite3_vfs * fiddle_db_vfs(const char *zDbName){
37194  sqlite3_vfs * pVfs = 0;
37195  if(globalDb){
37196    sqlite3_file_control(globalDb, zDbName ? zDbName : "main",
37197                         SQLITE_FCNTL_VFS_POINTER, &pVfs);
37198  }
37199  return pVfs;
37200}
37201
37202/* Only for emcc experimentation purposes. */
37203sqlite3 * fiddle_db_arg(sqlite3 *arg){
37204    cli_printf(stdout, "fiddle_db_arg(%p)\n", (const void*)arg);
37205    return arg;
37206}
37207
37208/*
37209** Intended to be called via a SharedWorker() while a separate
37210** SharedWorker() (which manages the wasm module) is performing work
37211** which should be interrupted. Unfortunately, SharedWorker is not
37212** portable enough to make real use of.
37213*/
37214void fiddle_interrupt(void){
37215  if( globalDb ) sqlite3_interrupt(globalDb);
37216}
37217
37218/*
37219** Returns the filename of the given db name, assuming "main" if
37220** zDbName is NULL. Returns NULL if globalDb is not opened.
37221*/
37222const char * fiddle_db_filename(const char * zDbName){
37223    return globalDb
37224      ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
37225      : NULL;
37226}
37227
37228/*
37229** Completely wipes out the contents of the currently-opened database
37230** but leaves its storage intact for reuse. If any transactions are
37231** active, they are forcibly rolled back.
37232*/
37233void fiddle_reset_db(void){
37234  if( globalDb ){
37235    int rc;
37236    while( sqlite3_txn_state(globalDb,0)>0 ){
37237      /*
37238      ** Resolve problem reported in
37239      ** https://sqlite.org/forum/forumpost/0b41a25d65
37240      */
37241      cli_puts("Rolling back in-progress transaction.\n", stdout);
37242      sqlite3_exec(globalDb,"ROLLBACK", 0, 0, 0);
37243    }
37244    rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
37245    if( 0==rc ) sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
37246    sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
37247  }
37248}
37249
37250/*
37251** Uses the current database's VFS xRead to stream the db file's
37252** contents out to the given callback. The callback gets a single
37253** chunk of size n (its 2nd argument) on each call and must return 0
37254** on success, non-0 on error. This function returns 0 on success,
37255** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
37256** code from the callback. Note that this is not thread-friendly: it
37257** expects that it will be the only thread reading the db file and
37258** takes no measures to ensure that is the case.
37259*/
37260int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){
37261  sqlite3_int64 nSize = 0;
37262  sqlite3_int64 nPos = 0;
37263  sqlite3_file * pFile = 0;
37264  unsigned char buf[1024 * 8];
37265  int nBuf = (int)sizeof(buf);
37266  int rc = shellState.db
37267    ? sqlite3_file_control(shellState.db, "main",
37268                           SQLITE_FCNTL_FILE_POINTER, &pFile)
37269    : SQLITE_NOTFOUND;
37270  if( rc ) return rc;
37271  rc = pFile->pMethods->xFileSize(pFile, &nSize);
37272  if( rc ) return rc;
37273  if(nSize % nBuf){
37274    /* DB size is not an even multiple of the buffer size. Reduce
37275    ** buffer size so that we do not unduly inflate the db size when
37276    ** exporting. */
37277    if(0 == nSize % 4096) nBuf = 4096;
37278    else if(0 == nSize % 2048) nBuf = 2048;
37279    else if(0 == nSize % 1024) nBuf = 1024;
37280    else nBuf = 512;
37281  }
37282  for( ; 0==rc && nPos<nSize; nPos += nBuf ){
37283    rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
37284    if(SQLITE_IOERR_SHORT_READ == rc){
37285      rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
37286    }
37287    if( 0==rc ) rc = xCallback(buf, nBuf);
37288  }
37289  return rc;
37290}
37291
37292/*
37293** Trivial exportable function for emscripten. It processes zSql as if
37294** it were input to the sqlite3 shell and redirects all output to the
37295** wasm binding. fiddle_main() must have been called before this
37296** is called, or results are undefined.
37297*/
37298void fiddle_exec(const char * zSql){
37299  if(zSql && *zSql){
37300    if('.'==*zSql) puts(zSql);
37301    shellState.wasm.zInput = zSql;
37302    shellState.wasm.zPos = zSql;
37303    process_input(&shellState, "<stdin>");
37304    shellState.wasm.zInput = shellState.wasm.zPos = 0;
37305  }
37306}
37307#endif /* SQLITE_SHELL_FIDDLE */
37308/************************* End src/shell.c.in ******************/