Merge changes Iba1dd754,I85038560,Ia61ba689 into main

* changes:
  Consume build flag in sqlite Android.bp
  Install sqlite 3.42.0
  Allow multiple releases of sqlite
diff --git a/README-upgrade.md b/README-upgrade.md
index fc93cf5..c14b1e3 100644
--- a/README-upgrade.md
+++ b/README-upgrade.md
@@ -13,19 +13,42 @@
 
 The upgrade steps are:
 
-*   Select a version for the upgrade.
+*   Select a version for the upgrade.  Note the year it was released by sqlite.org.
 *   Find the autoconf amalgamation tarball. For release 3.42.0, the URL is
     [sqlite-autoconf-3420000.tar.gz](https://sqlite.org/2023/sqlite-autoconf-3420000.tar.gz).
 *   Change to the directory `external/sqlite` in the workspace.
 *   Run the script `UPDATE-SOURCE.bash`. This script is executable. The
-    arguments are the tarball URL and the version. Invoke the script without
+    arguments are the sqlite release year and the version. Invoke the script without
     arguments for an example.
 
 `UPDATE-SOURCE.bash` may fail if the Android patch cannot be applied cleanly. If
 this happens, correct the patch failures by hand and rebuild the Android patch
 file. Use the script `REBUILD-ANDROID-PATCH.bash` to rebuild the patch file.
-Then rerun `UPDATE-SOURCE.bash`. It is important that `UPDATE-SOURCE.bash` run
-without errors.
+This script takes the same arguments as `UPDATE-SOURCE.bash`.  Then rerun
+`UPDATE-SOURCE.bash`. It is important that `UPDATE-SOURCE.bash` run without
+errors.
+
+Once the scripts have completed, there will be a directory containing the new
+source files.  The directory is named after the sqlite release and exists in
+parallel with other sqlite release directories.  For release 3.42.0, the
+directory name is `external/sqlite/dist/sqlite-autoconf-3420000`.
+
+## Flagging
+
+The release of sqlite can be controlled by trunk-stable build flags.  The flag
+is `RELEASE_PACKAGE_LIBSQLITE3`.  The value of that flag is the 7-digit sqlite
+release number (e.g., 3420000).  Any target that respects trunk-stable flags
+will use the source in `external/sqlite/dist/sqlite-autoconf-FLAG`.  Not all
+targets respect the trunk-stable flags, however.  Such targets use the directory
+`external/sqlite/dist/sqlite-default`.  
+
+A new release of sqlite can be promoted to `trunk` by setting the flag to the
+proper release string.  
+
+A release of sqlite is promoted to `next` by setting the flag to the release
+string AND by copying the associated release directory to `sqlite-default`.
+This moves all targets that do not honor build flags to use the newly promoted
+release. 
 
 ## LICENSE
 
diff --git a/REBUILD-ANDROID-PATCH.bash b/REBUILD-ANDROID-PATCH.bash
index 734e296..53d0990 100755
--- a/REBUILD-ANDROID-PATCH.bash
+++ b/REBUILD-ANDROID-PATCH.bash
@@ -16,7 +16,7 @@
 
 # This script updates SQLite source files with a SQLite tarball.
 #
-# Usage: REBUILD-ANDROID_PATCH.bash
+# Usage: REBUILD-ANDROID_PATCH.bash <release>
 #
 # This script must be executed in $ANDROID_BUILD_TOP/external/sqlite/
 #
@@ -24,9 +24,7 @@
 set -e
 
 script_name="$(basename "$0")"
-
-source_tgz="$1"
-source_ext_dir="$1.extracted"
+script_dir=$(dirname $(realpath ${BASH_SOURCE[0]}))
 
 die() {
     echo "$script_name: $*"
@@ -38,20 +36,38 @@
     "$@"
 }
 
-# Make sure the source tgz file exists.
-pwd="$(pwd)"
-if [[ ! "$pwd" =~ .*/external/sqlite/? ]] ; then
-    die 'Execute this script in $ANDROID_BUILD_TOP/external/sqlite/'
-fi
+# This function converts a release string like "3.42.0" to the canonical 7-digit
+# format used by sqlite.org for downloads: "3420000".  A hypothetical release
+# number of 3.45.6 is converted to "3450600".  A hypothetical release number of
+# 3.45.17 is converted to "3451700".  The last two digits are assumed to be
+# "00" for now, as there are no known counter-examples.
+function normalize_release {
+  local version=$1
+  local -a fields
+  fields=($(echo "$version" | sed 's/\./ /g'))
+  if [[ ${#fields[*]} -lt 2 || ${#fields[*]} -gt 3 ]]; then
+    echo "cannot parse version: $version"
+    return 1
+  elif [[ ${#fields[*]} -eq 2 ]]; then
+    fields+=(0)
+  fi
+  printf "%d%02d%02d00" ${fields[*]}
+  return 0
+}
 
-# No parameters are permitted
-if [[ ! $# -eq 0 ]]; then
-    die "Unexpected arguments on the command line"
+if [[ $# -lt 1 ]]; then
+  die "missing required arguments"
+elif [[ $# -gt 1 ]]; then
+  die "extra arguments on command line"
 fi
+sqlite_release=$(normalize_release "$1") || die "invalid release"
+sqlite_base="sqlite-autoconf-${sqlite_release}"
 
+export patch_dir=${script_dir}/dist
 echo
 echo "# Regenerating Android.patch ..."
 (
-    cd dist
+    cd dist/$sqlite_base || die "release directory not found"
     echo_and_exec bash -c '(for x in orig/*; do diff -u -d $x ${x#orig/}; done) > Android.patch'
-)
+    echo_and_exec cp Android.patch ${patch_dir}/
+) 
diff --git a/UPDATE-SOURCE.bash b/UPDATE-SOURCE.bash
index fdd9093..24e3a1a 100755
--- a/UPDATE-SOURCE.bash
+++ b/UPDATE-SOURCE.bash
@@ -14,23 +14,32 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# This script updates SQLite source files with a SQLite tarball.
+# This script updates SQLite source files with a SQLite tarball.  The tarball is
+# downloaded from the sqlite website.
 #
-# Usage: UPDATE-SOURCE.bash SQLITE-SOURCE.tgz
+# Usage: UPDATE-SOURCE.bash [-nF] <year> <sqlite-release>
 #
-# This script must be executed in $ANDROID_BUILD_TOP/external/sqlite/
+# This script must be executed in $ANDROID_BUILD_TOP/external/sqlite/.  However,
+# for testing it can run anywhere: use the -F switch.
 #
 
 set -e
 
 script_name="$(basename "$0")"
+script_dir=$(dirname $(realpath ${BASH_SOURCE[0]}))
 
-if [ $# -eq 0 ]; then
-  echo "Usage: ${script_name} [src_tarball_url] [sqlite_version]"
+usage() {
+  if [[ $# -gt 0 ]]; then echo "$*" >&2; fi
+  echo "Usage: ${script_name} -nF <year> <version>"
+  echo "  year    the 4-digit year the sqlite version was released"
+  echo "  version the sqlite version as <major>.<minor>[.<patch>]"
+  echo "          the patch level defaults to 0"
+  echo "  -n      dry-run: evaluate arguments but d not change anything"
+  echo "  -F      force execution even if not in external/sqlite"
+  echo 
   echo "Example:"
-  echo "${script_name} https://sqlite.org/2023/sqlite-autoconf-3420000.tar.gz 3.42.0"
-  exit 1
-fi
+  echo "${script_name} 2023 3.42"
+}
 
 die() {
     echo "$script_name: $*"
@@ -42,20 +51,89 @@
     "$@"
 }
 
+validate_year() {
+  local year=$1
+  if [[ "$year" =~ ^2[0-9][0-9][0-9]$ ]]; then
+    return 0;
+  else
+    return 1;
+  fi
+}
+
+# This function converts a release string like "3.42.0" to the canonical 7-digit
+# format used by sqlite.org for downloads: "3420000".  A hypothetical release
+# number of 3.45.6 is converted to "3450600".  A hypothetical release number of
+# 3.45.17 is converted to "3451700".  The last two digits are assumed to be
+# "00" for now, as there are no known counter-examples.
+function normalize_release {
+  local version=$1
+  local -a fields
+  fields=($(echo "$version" | sed 's/\./ /g'))
+  if [[ ${#fields[*]} -lt 2 || ${#fields[*]} -gt 3 ]]; then
+    echo "cannot parse version: $version"
+    return 1
+  elif [[ ${#fields[*]} -eq 2 ]]; then
+    fields+=(0)
+  fi
+  printf "%d%02d%02d00" ${fields[*]}
+  return 0
+}
+
+function prettify_release {
+  local version=$1
+  local patch=$((version % 100))
+  version=$((version / 100))
+  local minor=$((version % 100))
+  version=$((version / 100))
+  local major=$((version % 100))
+  version=$((version / 100))
+  # version now contains the generation number.
+  printf "%d.%d.%d" $version $major $minor
+}
+
+dry_run=
+force=
+while getopts "hnF" option; do
+  case $option in
+    h) usage; exit 0;;
+    n) dry_run=y;;
+    F) force=y;;
+    *) usage "unknown switch"; exit 1;;
+  esac
+done
+shift $((OPTIND- 1))
+
+if [[ $# -lt 2 ]]; then
+  usage; die "missing required arguments"
+elif [[ $# -gt 2 ]]; then
+  die "extra arguments on command line"
+fi
+year=$1
+validate_year "$year" || die "invalid year"
+sqlite_release=$(normalize_release "$2") || die "invalid release"
+
+sqlite_base="sqlite-autoconf-${sqlite_release}"
+sqlite_file="${sqlite_base}.tar.gz"
+src_tarball_url="https://www.sqlite.org/$year/${sqlite_file}"
+
+if [[ -n $dry_run ]]; then
+  echo "fetching $src_tarball_url"
+  echo "installing in dist/$sqlite_base"
+  exit 0
+fi
+
 pwd="$(pwd)"
-if [[ ! "$pwd" =~ .*/external/sqlite/? ]] ; then
+if [[ -z $force && ! "$pwd" =~ .*/external/sqlite/? ]] ; then
     die 'Execute this script in $ANDROID_BUILD_TOP/external/sqlite/'
 fi
 
-src_tarball_url="$1"
-sqlite_version="$2"
-
-source_tgz=$(mktemp /tmp/sqlite-${sqlite_version}.zip.XXXXXX)
+source_tgz=$(mktemp /tmp/sqlite-${sqlite_release}.zip.XXXXXX)
+source_ext_dir="${source_tgz}.extracted"
+trap "rm -r ${source_tgz} ${source_ext_dir}" EXIT
 wget ${src_tarball_url} -O ${source_tgz}
 
 echo
 echo "# Extracting the source tgz..."
-source_ext_dir="${source_tgz}.extracted"
 echo_and_exec rm -fr "$source_ext_dir"
 echo_and_exec mkdir -p "$source_ext_dir"
 echo_and_exec tar xvf "$source_tgz" -C "$source_ext_dir" --strip-components=1
@@ -68,20 +146,23 @@
     echo_and_exec make -j 4 sqlite3.c
 )
 
-dist_dir="dist-${sqlite_version}"
+export dist_dir="dist/${sqlite_base}"
 echo
 echo "# Copying the source files ..."
+echo_and_exec rm -rf ${dist_dir}
 echo_and_exec mkdir -p "${dist_dir}"
 echo_and_exec mkdir -p "${dist_dir}/orig"
 for to in ${dist_dir}/orig/ ${dist_dir}/ ; do
     echo_and_exec cp "$source_ext_dir/"{shell.c,sqlite3.c,sqlite3.h,sqlite3ext.h} "$to"
 done
 
+export patch_dir=${script_dir}/dist
 echo
 echo "# Applying Android.patch ..."
 (
     cd ${dist_dir}
-    echo_and_exec patch -i ../Android.patch
+    echo "PATCHING IN $dist_dir" >&2
+    echo_and_exec patch -i ${patch_dir}/Android.patch
 )
 
 echo
@@ -89,13 +170,14 @@
 (
     cd ${dist_dir}
     echo_and_exec bash -c '(for x in orig/*; do diff -u -d $x ${x#orig/}; done) > Android.patch'
+    echo_and_exec cp Android.patch ${patch_dir}/
 )
 
 echo
 echo "# Generating metadata ..."
 (
     export SQLITE_URL=${src_tarball_url}
-    export SQLITE_VERSION=${sqlite_version}
+    export SQLITE_VERSION=$(prettify_release ${sqlite_release})
     export YEAR=$(date +%Y)
     export MONTH=$(date +%M)
     export DAY=$(date +%D)
diff --git a/android/Android.bp b/android/Android.bp
index f0a6da6..b485f0c 100644
--- a/android/Android.bp
+++ b/android/Android.bp
@@ -15,8 +15,29 @@
     ],
 }
 
-cc_defaults {
+soong_config_module_type {
+    name: "release_package_libsqlite3_android_defaults_config",
+    module_type: "cc_defaults",
+    config_namespace: "libsqlite3",
+    value_variables: ["release_package_libsqlite3"],
+    properties: [
+        "include_dirs",
+    ],
+}
+
+// Construct the correct source and includes for sqlite libraries,
+// based on the build flag.
+release_package_libsqlite3_android_defaults_config {
     name: "libsqlite3_android_defaults",
+    soong_config_variables: {
+        release_package_libsqlite3: {
+            include_dirs: ["external/sqlite/dist/sqlite-autoconf-%s"],
+            conditions_default: {
+                include_dirs: ["external/sqlite/dist/sqlite-default"],
+            },
+        },
+    },
+
     host_supported: true,
     cflags: [
         "-Wall",
@@ -29,7 +50,6 @@
         "OldPhoneNumberUtils.cpp",
         "sqlite3_android.cpp",
     ],
-    include_dirs: ["external/sqlite/dist"],
     shared_libs: [
         "liblog",
     ],
diff --git a/dist/Android.bp b/dist/Android.bp
index 6ca2767..c47dfe5 100644
--- a/dist/Android.bp
+++ b/dist/Android.bp
@@ -101,17 +101,53 @@
     },
 }
 
+soong_config_module_type {
+    name: "release_package_libsqlite3_library_defaults_config",
+    module_type: "cc_defaults",
+    config_namespace: "libsqlite3",
+    value_variables: ["release_package_libsqlite3"],
+    properties: [
+        "export_include_dirs",
+        "srcs",
+    ],
+}
+
+// Construct the correct source and includes for sqlite libraries,
+// based on the build flag.
+release_package_libsqlite3_library_defaults_config {
+    name: "release_package_libsqlite3_library_defaults",
+    soong_config_variables: {
+        release_package_libsqlite3: {
+            export_include_dirs: ["sqlite-autoconf-%s"],
+            srcs: ["sqlite-autoconf-%s/sqlite3.c"],
+            conditions_default: {
+                export_include_dirs: ["sqlite-default"],
+                srcs: ["sqlite-default/sqlite3.c"],
+            },
+        },
+    },
+}
+
+cc_defaults {
+    name: "libsqlite_defaults",
+    defaults: [
+        "sqlite-defaults",
+        "release_package_libsqlite3_library_defaults",
+    ],
+}
+
 cc_library {
     name: "libsqlite",
-    defaults: ["sqlite-defaults"],
+    defaults: [
+        "sqlite-defaults",
+        "release_package_libsqlite3_library_defaults",
+    ],
     vendor_available: true,
     native_bridge_supported: true,
     vndk: {
         enabled: true,
     },
 
-    srcs: ["sqlite3.c"],
-
     target: {
         android: {
             shared_libs: [
@@ -145,7 +181,6 @@
             exclude_static_libs: ["libsqlite3_android"],
         },
     },
-    export_include_dirs: ["."],
     apex_available: [
         "//apex_available:platform",
         "com.android.virt",
@@ -160,11 +195,12 @@
 // desirable, like microdroid.
 cc_library_static {
     name: "libsqlite_static_noicu",
-    defaults: ["sqlite-defaults"],
-    srcs: ["sqlite3.c"],
+    defaults: [
+        "sqlite-defaults",
+        "release_package_libsqlite3_library_defaults",
+    ],
     whole_static_libs: ["libsqlite3_android_noicu"],
     // Not define SQLITE_ENABLE_ICU
-    export_include_dirs: ["."],
     apex_available: [
         "//apex_available:platform",
         "com.android.os.statsd",
@@ -174,17 +210,74 @@
     host_supported: true,
 }
 
+// Build a minimal version of sqlite3 without any android specific
+// features against the NDK. This is used by libcore's JDBC related
+// unit tests.
+cc_library_static {
+    name: "libsqlite_static_minimal",
+    defaults: [
+        "sqlite-minimal-defaults",
+        "release_package_libsqlite3_library_defaults",
+    ],
+    sdk_version: "23",
+}
+
+soong_config_module_type {
+    name: "release_package_libsqlite3_library_percentile_config",
+    module_type: "cc_library_static",
+    config_namespace: "libsqlite3",
+    value_variables: ["release_package_libsqlite3"],
+    properties: [
+        "export_include_dirs",
+    ],
+}
+
+release_package_libsqlite3_library_percentile_config {
+    name: "sqlite_ext_percentile",
+    soong_config_variables: {
+        release_package_libsqlite3: {
+            export_include_dirs: ["sqlite-autoconf-%s"],
+            conditions_default: {
+                export_include_dirs: ["sqlite-default"],
+            },
+        },
+    },
+    srcs: [
+        "ext/misc/percentile.c",
+    ],
+    defaults: ["sqlite-defaults"],
+    host_supported: true,
+}
+
 //
 //
 // Build the device command line tool sqlite3
 //
 //
 
-cc_binary {
-    name: "sqlite3",
-    defaults: ["sqlite-defaults"],
+soong_config_module_type {
+    name: "release_package_libsqlite3_library_shell_config",
+    module_type: "cc_binary",
+    config_namespace: "libsqlite3",
+    value_variables: ["release_package_libsqlite3"],
+    properties: [
+        "srcs",
+    ],
+}
 
-    srcs: ["shell.c"],
+// Construct the correct source and includes for sqlite libraries,
+// based on the build flag.
+release_package_libsqlite3_library_shell_config {
+    name: "sqlite3",
+    soong_config_variables: {
+        release_package_libsqlite3: {
+            srcs: ["sqlite-autoconf-%s/shell.c"],
+            conditions_default: {
+                srcs: ["sqlite-default/shell.c"],
+            },
+        },
+    },
+    defaults: ["sqlite-defaults"],
 
     target: {
         android: {
@@ -211,24 +304,3 @@
         },
     },
 }
-
-// Build a minimal version of sqlite3 without any android specific
-// features against the NDK. This is used by libcore's JDBC related
-// unit tests.
-cc_library_static {
-    name: "libsqlite_static_minimal",
-    defaults: ["sqlite-minimal-defaults"],
-    srcs: ["sqlite3.c"],
-    sdk_version: "23",
-    export_include_dirs: ["."],
-}
-
-cc_library_static {
-    name: "sqlite_ext_percentile",
-    srcs: [
-        "ext/misc/percentile.c",
-    ],
-    defaults: ["sqlite-defaults"],
-    host_supported: true,
-    export_include_dirs: ["."],
-}
diff --git a/dist/Android.patch b/dist/Android.patch
index cd39abc..f38906c 100644
--- a/dist/Android.patch
+++ b/dist/Android.patch
@@ -1,5 +1,5 @@
---- orig/shell.c	2023-06-13 13:30:22.470500137 -0700
-+++ shell.c	2023-06-13 13:30:22.546500365 -0700
+--- orig/shell.c	2024-03-14 11:00:02.445940614 -0700
++++ shell.c	2024-03-14 11:00:02.477940578 -0700
 @@ -127,6 +127,11 @@
  #endif
  #include <ctype.h>
@@ -44,8 +44,8 @@
      if( p->openMode==SHELL_OPEN_ZIPFILE ){
        char *zSql = sqlite3_mprintf(
           "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
---- orig/sqlite3.c	2023-06-13 13:30:22.494500209 -0700
-+++ sqlite3.c	2023-06-13 13:30:22.590500496 -0700
+--- orig/sqlite3.c	2024-03-14 11:00:02.457940601 -0700
++++ sqlite3.c	2024-03-14 11:00:02.509940542 -0700
 @@ -37314,6 +37314,10 @@
  # include <sys/mount.h>
  #endif
diff --git a/dist/sqlite-autoconf-3420000/Android.patch b/dist/sqlite-autoconf-3420000/Android.patch
new file mode 100644
index 0000000..f38906c
--- /dev/null
+++ b/dist/sqlite-autoconf-3420000/Android.patch
@@ -0,0 +1,153 @@
+--- orig/shell.c	2024-03-14 11:00:02.445940614 -0700
++++ shell.c	2024-03-14 11:00:02.477940578 -0700
+@@ -127,6 +127,11 @@
+ #endif
+ #include <ctype.h>
+ #include <stdarg.h>
++// Begin Android Add
++#ifndef NO_ANDROID_FUNCS
++#include <sqlite3_android.h>
++#endif
++// End Android Add
+ 
+ #if !defined(_WIN32) && !defined(WIN32)
+ # include <signal.h>
+@@ -246,7 +251,9 @@
+ #if SQLITE_OS_WINRT
+ #include <intrin.h>
+ #endif
++#ifndef WIN32_LEAN_AND_MEAN
+ #define WIN32_LEAN_AND_MEAN
++#endif
+ #include <windows.h>
+ 
+ /* string conversion routines only needed on Win32 */
+@@ -20793,6 +20800,21 @@
+                             editFunc, 0, 0);
+ #endif
+ 
++// Begin Android Add
++#ifndef NO_ANDROID_FUNCS
++    int err = register_localized_collators(p->db, "en_US", 0);
++    if (err != SQLITE_OK) {
++      fprintf(stderr, "register_localized_collators() failed\n");
++      exit(1);
++    }
++    err = register_android_functions(p->db, 0);
++    if (err != SQLITE_OK) {
++      fprintf(stderr, "register_android_functions() failed\n");
++      exit(1);
++    }
++#endif
++// End Android Add
++
+     if( p->openMode==SHELL_OPEN_ZIPFILE ){
+       char *zSql = sqlite3_mprintf(
+          "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
+--- orig/sqlite3.c	2024-03-14 11:00:02.457940601 -0700
++++ sqlite3.c	2024-03-14 11:00:02.509940542 -0700
+@@ -37314,6 +37314,10 @@
+ # include <sys/mount.h>
+ #endif
+ 
++#if defined(__BIONIC__)
++# include <android/fdsan.h>
++#endif
++
+ #ifdef HAVE_UTIME
+ # include <utime.h>
+ #endif
+@@ -37922,6 +37926,12 @@
+ #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
+     osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
+ #endif
++
++#if defined(__BIONIC__) && __ANDROID_API__ >= __ANDROID_API_Q__
++    uint64_t tag = android_fdsan_create_owner_tag(
++        ANDROID_FDSAN_OWNER_TYPE_SQLITE, fd);
++    android_fdsan_exchange_owner_tag(fd, 0, tag);
++#endif
+   }
+   return fd;
+ }
+@@ -38502,7 +38512,13 @@
+ ** and move on.
+ */
+ static void robust_close(unixFile *pFile, int h, int lineno){
++#if defined(__BIONIC__) && __ANDROID_API__ >= __ANDROID_API_Q__
++  uint64_t tag = android_fdsan_create_owner_tag(
++      ANDROID_FDSAN_OWNER_TYPE_SQLITE, h);
++  if( android_fdsan_close_with_tag(h, tag) ){
++#else
+   if( osClose(h) ){
++#endif
+     unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
+                        pFile ? pFile->zPath : 0, lineno);
+   }
+@@ -41051,7 +41067,7 @@
+   SimulateIOError( rc=1 );
+   if( rc!=0 ){
+     storeLastErrno((unixFile*)id, errno);
+-    return SQLITE_IOERR_FSTAT;
++    return unixLogError(SQLITE_IOERR_FSTAT, "fstat", ((unixFile*)id)->zPath);
+   }
+   *pSize = buf.st_size;
+ 
+@@ -41087,7 +41103,7 @@
+     struct stat buf;              /* Used to hold return values of fstat() */
+ 
+     if( osFstat(pFile->h, &buf) ){
+-      return SQLITE_IOERR_FSTAT;
++      return unixLogError(SQLITE_IOERR_FSTAT, "fstat", pFile->zPath);
+     }
+ 
+     nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
+@@ -41829,7 +41845,7 @@
+     ** with the same permissions.
+     */
+     if( osFstat(pDbFd->h, &sStat) ){
+-      rc = SQLITE_IOERR_FSTAT;
++      rc = unixLogError(SQLITE_IOERR_FSTAT, "fstat", pDbFd->zPath);
+       goto shm_open_err;
+     }
+ 
+@@ -138266,7 +138282,7 @@
+   }
+   if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
+     sqlite3SetString(pzErrMsg, db, "unsupported file format");
+-    rc = SQLITE_ERROR;
++    rc = SQLITE_CORRUPT_BKPT; // Android Change from "rc = SQLITE_ERROR;";
+     goto initone_error_out;
+   }
+ 
+@@ -185208,7 +185224,9 @@
+   ** module with sqlite.
+   */
+   if( SQLITE_OK==rc
++#ifndef ANDROID    /* fts3_tokenizer disabled for security reasons */
+    && SQLITE_OK==(rc=sqlite3Fts3InitHashTable(db,&pHash->hash,"fts3_tokenizer"))
++#endif
+    && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
+    && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
+    && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
+@@ -185219,6 +185237,20 @@
+     rc = sqlite3_create_module_v2(
+         db, "fts3", &fts3Module, (void *)pHash, hashDestroy
+     );
++#ifdef SQLITE_ENABLE_FTS3_BACKWARDS
++    if( rc==SQLITE_OK ){
++      pHash->nRef++;
++      rc = sqlite3_create_module_v2(
++          db, "fts1", &fts3Module, (void *)pHash, hashDestroy
++      );
++    }
++    if( rc==SQLITE_OK ){
++      pHash->nRef++;
++      rc = sqlite3_create_module_v2(
++          db, "fts2", &fts3Module, (void *)pHash, hashDestroy
++      );
++    }
++#endif
+     if( rc==SQLITE_OK ){
+       pHash->nRef++;
+       rc = sqlite3_create_module_v2(
diff --git a/dist/sqlite-autoconf-3420000/METADATA b/dist/sqlite-autoconf-3420000/METADATA
new file mode 100644
index 0000000..76cc062
--- /dev/null
+++ b/dist/sqlite-autoconf-3420000/METADATA
@@ -0,0 +1,21 @@
+name: "SQLite"
+description:
+    "SQLite database"
+
+third_party {
+  url {
+    type: HOMEPAGE
+    value: "https://www.sqlite.org"
+  }
+  url {
+    type: ARCHIVE
+    value: "https://www.sqlite.org/2023/sqlite-autoconf-3420000.tar.gz"
+  }
+  version: "3.42.0"
+  last_upgrade_date { year: 2024 month: 00 day: 03/14/24 }
+  license_type: NOTICE
+
+  security: {
+    tag: "NVD-CPE2.3:cpe:/a:sqlite:sqlite"  # see http://go/metadata-cpe
+  }
+}
diff --git a/dist/sqlite-autoconf-3420000/README.version b/dist/sqlite-autoconf-3420000/README.version
new file mode 100644
index 0000000..c892550
--- /dev/null
+++ b/dist/sqlite-autoconf-3420000/README.version
@@ -0,0 +1,3 @@
+URL: https://www.sqlite.org/2023/sqlite-autoconf-3420000.tar.gz
+Version: 3.42.0
+BugComponent: 24950
diff --git a/dist/orig/shell.c b/dist/sqlite-autoconf-3420000/orig/shell.c
similarity index 100%
copy from dist/orig/shell.c
copy to dist/sqlite-autoconf-3420000/orig/shell.c
diff --git a/dist/orig/sqlite3.c b/dist/sqlite-autoconf-3420000/orig/sqlite3.c
similarity index 100%
copy from dist/orig/sqlite3.c
copy to dist/sqlite-autoconf-3420000/orig/sqlite3.c
diff --git a/dist/orig/sqlite3.h b/dist/sqlite-autoconf-3420000/orig/sqlite3.h
similarity index 100%
copy from dist/orig/sqlite3.h
copy to dist/sqlite-autoconf-3420000/orig/sqlite3.h
diff --git a/dist/orig/sqlite3ext.h b/dist/sqlite-autoconf-3420000/orig/sqlite3ext.h
similarity index 100%
copy from dist/orig/sqlite3ext.h
copy to dist/sqlite-autoconf-3420000/orig/sqlite3ext.h
diff --git a/dist/shell.c b/dist/sqlite-autoconf-3420000/shell.c
similarity index 100%
copy from dist/shell.c
copy to dist/sqlite-autoconf-3420000/shell.c
diff --git a/dist/sqlite3.c b/dist/sqlite-autoconf-3420000/sqlite3.c
similarity index 100%
copy from dist/sqlite3.c
copy to dist/sqlite-autoconf-3420000/sqlite3.c
diff --git a/dist/orig/sqlite3.h b/dist/sqlite-autoconf-3420000/sqlite3.h
similarity index 100%
copy from dist/orig/sqlite3.h
copy to dist/sqlite-autoconf-3420000/sqlite3.h
diff --git a/dist/orig/sqlite3ext.h b/dist/sqlite-autoconf-3420000/sqlite3ext.h
similarity index 100%
copy from dist/orig/sqlite3ext.h
copy to dist/sqlite-autoconf-3420000/sqlite3ext.h
diff --git a/dist/sqlite-default/Android.patch b/dist/sqlite-default/Android.patch
new file mode 100644
index 0000000..f38906c
--- /dev/null
+++ b/dist/sqlite-default/Android.patch
@@ -0,0 +1,153 @@
+--- orig/shell.c	2024-03-14 11:00:02.445940614 -0700
++++ shell.c	2024-03-14 11:00:02.477940578 -0700
+@@ -127,6 +127,11 @@
+ #endif
+ #include <ctype.h>
+ #include <stdarg.h>
++// Begin Android Add
++#ifndef NO_ANDROID_FUNCS
++#include <sqlite3_android.h>
++#endif
++// End Android Add
+ 
+ #if !defined(_WIN32) && !defined(WIN32)
+ # include <signal.h>
+@@ -246,7 +251,9 @@
+ #if SQLITE_OS_WINRT
+ #include <intrin.h>
+ #endif
++#ifndef WIN32_LEAN_AND_MEAN
+ #define WIN32_LEAN_AND_MEAN
++#endif
+ #include <windows.h>
+ 
+ /* string conversion routines only needed on Win32 */
+@@ -20793,6 +20800,21 @@
+                             editFunc, 0, 0);
+ #endif
+ 
++// Begin Android Add
++#ifndef NO_ANDROID_FUNCS
++    int err = register_localized_collators(p->db, "en_US", 0);
++    if (err != SQLITE_OK) {
++      fprintf(stderr, "register_localized_collators() failed\n");
++      exit(1);
++    }
++    err = register_android_functions(p->db, 0);
++    if (err != SQLITE_OK) {
++      fprintf(stderr, "register_android_functions() failed\n");
++      exit(1);
++    }
++#endif
++// End Android Add
++
+     if( p->openMode==SHELL_OPEN_ZIPFILE ){
+       char *zSql = sqlite3_mprintf(
+          "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
+--- orig/sqlite3.c	2024-03-14 11:00:02.457940601 -0700
++++ sqlite3.c	2024-03-14 11:00:02.509940542 -0700
+@@ -37314,6 +37314,10 @@
+ # include <sys/mount.h>
+ #endif
+ 
++#if defined(__BIONIC__)
++# include <android/fdsan.h>
++#endif
++
+ #ifdef HAVE_UTIME
+ # include <utime.h>
+ #endif
+@@ -37922,6 +37926,12 @@
+ #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
+     osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
+ #endif
++
++#if defined(__BIONIC__) && __ANDROID_API__ >= __ANDROID_API_Q__
++    uint64_t tag = android_fdsan_create_owner_tag(
++        ANDROID_FDSAN_OWNER_TYPE_SQLITE, fd);
++    android_fdsan_exchange_owner_tag(fd, 0, tag);
++#endif
+   }
+   return fd;
+ }
+@@ -38502,7 +38512,13 @@
+ ** and move on.
+ */
+ static void robust_close(unixFile *pFile, int h, int lineno){
++#if defined(__BIONIC__) && __ANDROID_API__ >= __ANDROID_API_Q__
++  uint64_t tag = android_fdsan_create_owner_tag(
++      ANDROID_FDSAN_OWNER_TYPE_SQLITE, h);
++  if( android_fdsan_close_with_tag(h, tag) ){
++#else
+   if( osClose(h) ){
++#endif
+     unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
+                        pFile ? pFile->zPath : 0, lineno);
+   }
+@@ -41051,7 +41067,7 @@
+   SimulateIOError( rc=1 );
+   if( rc!=0 ){
+     storeLastErrno((unixFile*)id, errno);
+-    return SQLITE_IOERR_FSTAT;
++    return unixLogError(SQLITE_IOERR_FSTAT, "fstat", ((unixFile*)id)->zPath);
+   }
+   *pSize = buf.st_size;
+ 
+@@ -41087,7 +41103,7 @@
+     struct stat buf;              /* Used to hold return values of fstat() */
+ 
+     if( osFstat(pFile->h, &buf) ){
+-      return SQLITE_IOERR_FSTAT;
++      return unixLogError(SQLITE_IOERR_FSTAT, "fstat", pFile->zPath);
+     }
+ 
+     nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
+@@ -41829,7 +41845,7 @@
+     ** with the same permissions.
+     */
+     if( osFstat(pDbFd->h, &sStat) ){
+-      rc = SQLITE_IOERR_FSTAT;
++      rc = unixLogError(SQLITE_IOERR_FSTAT, "fstat", pDbFd->zPath);
+       goto shm_open_err;
+     }
+ 
+@@ -138266,7 +138282,7 @@
+   }
+   if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
+     sqlite3SetString(pzErrMsg, db, "unsupported file format");
+-    rc = SQLITE_ERROR;
++    rc = SQLITE_CORRUPT_BKPT; // Android Change from "rc = SQLITE_ERROR;";
+     goto initone_error_out;
+   }
+ 
+@@ -185208,7 +185224,9 @@
+   ** module with sqlite.
+   */
+   if( SQLITE_OK==rc
++#ifndef ANDROID    /* fts3_tokenizer disabled for security reasons */
+    && SQLITE_OK==(rc=sqlite3Fts3InitHashTable(db,&pHash->hash,"fts3_tokenizer"))
++#endif
+    && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
+    && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
+    && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
+@@ -185219,6 +185237,20 @@
+     rc = sqlite3_create_module_v2(
+         db, "fts3", &fts3Module, (void *)pHash, hashDestroy
+     );
++#ifdef SQLITE_ENABLE_FTS3_BACKWARDS
++    if( rc==SQLITE_OK ){
++      pHash->nRef++;
++      rc = sqlite3_create_module_v2(
++          db, "fts1", &fts3Module, (void *)pHash, hashDestroy
++      );
++    }
++    if( rc==SQLITE_OK ){
++      pHash->nRef++;
++      rc = sqlite3_create_module_v2(
++          db, "fts2", &fts3Module, (void *)pHash, hashDestroy
++      );
++    }
++#endif
+     if( rc==SQLITE_OK ){
+       pHash->nRef++;
+       rc = sqlite3_create_module_v2(
diff --git a/dist/sqlite-default/METADATA b/dist/sqlite-default/METADATA
new file mode 100644
index 0000000..76cc062
--- /dev/null
+++ b/dist/sqlite-default/METADATA
@@ -0,0 +1,21 @@
+name: "SQLite"
+description:
+    "SQLite database"
+
+third_party {
+  url {
+    type: HOMEPAGE
+    value: "https://www.sqlite.org"
+  }
+  url {
+    type: ARCHIVE
+    value: "https://www.sqlite.org/2023/sqlite-autoconf-3420000.tar.gz"
+  }
+  version: "3.42.0"
+  last_upgrade_date { year: 2024 month: 00 day: 03/14/24 }
+  license_type: NOTICE
+
+  security: {
+    tag: "NVD-CPE2.3:cpe:/a:sqlite:sqlite"  # see http://go/metadata-cpe
+  }
+}
diff --git a/dist/sqlite-default/README.version b/dist/sqlite-default/README.version
new file mode 100644
index 0000000..c892550
--- /dev/null
+++ b/dist/sqlite-default/README.version
@@ -0,0 +1,3 @@
+URL: https://www.sqlite.org/2023/sqlite-autoconf-3420000.tar.gz
+Version: 3.42.0
+BugComponent: 24950
diff --git a/dist/orig/shell.c b/dist/sqlite-default/orig/shell.c
similarity index 100%
rename from dist/orig/shell.c
rename to dist/sqlite-default/orig/shell.c
diff --git a/dist/orig/sqlite3.c b/dist/sqlite-default/orig/sqlite3.c
similarity index 100%
rename from dist/orig/sqlite3.c
rename to dist/sqlite-default/orig/sqlite3.c
diff --git a/dist/orig/sqlite3.h b/dist/sqlite-default/orig/sqlite3.h
similarity index 100%
rename from dist/orig/sqlite3.h
rename to dist/sqlite-default/orig/sqlite3.h
diff --git a/dist/orig/sqlite3ext.h b/dist/sqlite-default/orig/sqlite3ext.h
similarity index 100%
rename from dist/orig/sqlite3ext.h
rename to dist/sqlite-default/orig/sqlite3ext.h
diff --git a/dist/shell.c b/dist/sqlite-default/shell.c
similarity index 100%
rename from dist/shell.c
rename to dist/sqlite-default/shell.c
diff --git a/dist/sqlite3.c b/dist/sqlite-default/sqlite3.c
similarity index 100%
rename from dist/sqlite3.c
rename to dist/sqlite-default/sqlite3.c
diff --git a/dist/sqlite3.h b/dist/sqlite-default/sqlite3.h
similarity index 100%
rename from dist/sqlite3.h
rename to dist/sqlite-default/sqlite3.h
diff --git a/dist/sqlite3ext.h b/dist/sqlite-default/sqlite3ext.h
similarity index 100%
rename from dist/sqlite3ext.h
rename to dist/sqlite-default/sqlite3ext.h