Skip to content

ADFA-6635: Add sqlite-brotli, Brotli functions for sqlite3 - #5

Merged
davidschachterADFA merged 3 commits into
mainfrom
feat/ADFA-6635-sqlite-brotli
Nov 16, 2026
Merged

davidschachterADFA merged 3 commits into
mainfrom
feat/ADFA-6635-sqlite-brotli

Conversation

@davidschachterADFA

@davidschachterADFA davidschachterADFA commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

ADFA-6635

Adds sqlite-brotli, a SQLite loadable extension for the sqlite3 shell with encodeBrotli(data [, dictionary [, quality [, lgwin]]]) and decodeBrotli(data [, dictionary]), to maintain documentation.db. It installs $PREFIX/lib/sqlite-brotli.so (.load sqlite-brotli), a README and its acceptance tests.

  • The dictionary is the raw kind that brotli -D and brotli4j's PreparedDictionary use, so data moves freely between the extension, the brotli tool and the app. Each connection caches the prepared encoder dictionary.
  • Invalid arguments, corrupt or truncated input, trailing data and results over SQLITE_LIMIT_LENGTH are SQL errors.
  • A Brotli stream has no checksum or dictionary id, so a wrong dictionary of the same length can decode to wrong bytes with no error: 21% of documentation.db rows do with another version's dictionary. Acceptance criterion 5 was amended to match, and the README says so.
  • Our own packages: termux-packages has no such package, so build.sh now copies packages/<name>/ into termux-packages/packages/ on every run, applies the com.termux rename, removes copies whose source is gone, and stops if a name is also an upstream package or subpackage (git says which package folders are upstream). It reads repo.json with jq, which is now required. README: new "Our own packages" section.
  • packages.sh: adds sqlite-brotli to COTG_PACKAGES.

Tested, first commit (2191349, version 1.0.0):

  • All 73 tests in packages/sqlite-brotli/tests/run-tests.sh pass on x86_64 Linux with SQLite 3.51.2, also under ASan and UBSan with leak checking. They include decoding all 23,094 Brotli rows of a documentation.db and byte-identical output to brotli -q 11 -w 24 -D.
  • Allocation-failure injection while loading: no crash, no leak.
  • NDK r29 cross-builds for aarch64 and arm, against headers and libraries from the published sqlite 3.51.2 and brotli 1.2.0 packages: only the init function is exported, and there are no com.termux strings.
  • build.sh copying, in a scratch copy of the repo: fresh setup, edits picked up, name collisions (an upstream package, a subpackage, an x11-packages name), the rename, and removal of a deleted package.
  • On a Galaxy Note20 Ultra (Android 13), all 73 tests pass, including all 23,094 documentation.db rows:
    • in CoGo's terminal (build 26.45, aarch64), with its installed sqlite 3.51.2 and brotli 1.2.0, loading the extension by name (.load sqlite-brotli, LD_LIBRARY_PATH unset);
    • from adb shell with the published aarch64 and arm (32-bit) sqlite3 and brotli.
      These used the NDK cross-builds above, copied into place by hand.
  • CI (run 42935163808) built both packages (aarch64 12 min 33 s, arm 15 min 11 s) and published them to the stage bucket: only the two sqlite-brotli .deb files and the index were uploaded (681 packages, up from 679). Each package has the library, README and test script, depends on brotli, links only libbrotlienc, libbrotlidec and libc, exports only the init function, and has no com.termux strings.
  • The CI-built aarch64 and arm libraries pass all 73 tests from adb shell on the same phone.
  • In CoGo's terminal, the CI-built aarch64 package installs with dpkg -i, dpkg --verify is clean, and its installed run-tests.sh passes all 73 tests, loading the extension by name.

Second review, commit cb7450a (version 1.0.0-1). An independent Claude review of the final diff raised 10 findings; 8 were confirmed, 1 was half right and 1 did not reproduce.

  • The functions are registered SQLITE_INNOCUOUS. The sqlite3 shell turns trusted_schema off, so before this, views, triggers, CHECK constraints and generated columns could not use them. A new test covers it, and the previous build fails it.
  • The comment claiming the length limit stops a decompression bomb is corrected: the 1 GB default can be more than a phone gives one process. The README says to lower it with .limit length before decoding untrusted data.
  • build.sh writes the .cotg-own-package marker before copying, so an interrupted copy no longer blocks later builds. One update_package_name helper now does the com.termux rename for upstream and our packages; it handles any file name and skips binary files, and for the pinned termux-packages it produces the same tree as before.
  • A change to one of our packages needs a higher version or revision (build-package.sh skips a same-version package, and publishing keeps the published file); build.sh and the README say so, and sqlite-brotli is now 1.0.0-1.
  • run-tests.sh quotes file paths for SQL, dot-commands and URIs, and is installed executable. tests/hello.c, the extension acceptance criterion 1 loads, is added, with build instructions in the README.
  • Tested: all 74 tests pass on x86_64 Linux, also under ASan and UBSan, and with every path under a directory named a b'c%d#e?f. The build.sh checks pass in a scratch copy, the recipe passes Termux's linter, and NDK cross-builds for both architectures export only the init function.
  • CI (run 42960020302) built 1.0.0-1 for both architectures and published it to the stage bucket; only the two .deb files and the index were uploaded. run-tests.sh is installed executable.
  • In CoGo's terminal, dpkg -i upgrades 1.0.0 to the CI-built 1.0.0-1 aarch64 package, dpkg --verify is clean, and the installed run-tests.sh, run directly, passes all 74 tests, including AC1 with hello.so built from tests/hello.c.
  • The CI-built arm 1.0.0-1 library passes all 74 tests from adb shell on the same phone, with the published 32-bit sqlite3 and brotli.

Hal Eisen's review, commit c0d8bd4 (version 1.0.0-2):

  • build.sh asks git which packages are upstream. The untracked marker can outlast a forced checkout or reset --hard of a newer termux-packages; if that commit adds a package with our name, the old check took it for ours and replaced upstream's files, and the cleanup could delete the package. Now a package folder that git tracks stops the build before either rm -rf, and the marker only limits the cleanup to folders build.sh made. Git variables inherited from a caller (GIT_DIR, GIT_INDEX_FILE) are dropped, and the build stops if termux-packages is not a git checkout of its own or git fails.
  • sqlite-brotli depends on brotli (>= 1.1.0), which added the four dictionary functions it calls (v1.0.9's headers have none of them). Revision 2, so the new Depends: line is published.
  • An independent Claude review of the fix found two more issues, both fixed: inherited git variables could point git at another repository or index, and the error for a tree owned by another user gave the wrong advice.
  • Tested: 11 build.sh cases in scratch copies pass, including both of Hal's cases; the previous build.sh overwrites or deletes upstream's files in 4 of them. A normal run gives the same termux-packages tree as before, and the recipe passes Termux's linter.
  • CI (run 43083357904) built 1.0.0-2 (aarch64 11 min 46 s, arm 16 min 12 s) and published it to the stage bucket. Both .deb files say Depends: brotli (>= 1.1.0), and their files are byte-identical to 1.0.0-1's.
  • In CoGo's terminal, dpkg -i upgrades 1.0.0-1 to 1.0.0-2, dpkg --verify is clean, and the installed tests pass 74/74. Every command in the ticket's Steps to QA gives the expected output there, and run-tests.sh -d passes 73/73 on the app's own documentation.db (23,098 Brotli rows) in 78 s.
  • apt from the staging repo (stage.appdevforall.org, signed with the prod key) in CoGo's terminal: after apt-get remove sqlite-brotli, .load sqlite-brotli fails with dlopen failed: library "sqlite-brotli.so" not found. apt-get update fetches the signed index with no warnings, apt-get install sqlite-brotli installs 1.0.0-2 (the installed brotli 1.2.0 meets >= 1.1.0), dpkg --verify is clean, and the tests pass 73/73 on the app's documentation.db. apt used a temporary source list and lists folder, so the phone's apt setup is unchanged.

After review: merge, publish to prod from main (staging=false, which rebuilds), then apt install sqlite-brotli in CoGo's terminal and the ticket's Steps to QA.

🤖 Generated with Claude Code

sqlite-brotli is a SQLite loadable extension that adds
encodeBrotli(data [, dictionary [, quality [, lgwin]]]) and
decodeBrotli(data [, dictionary]), to maintain documentation.db, whose
content is Brotli-compressed with a raw custom dictionary. It installs
$PREFIX/lib/sqlite-brotli.so, loaded with `.load sqlite-brotli`, plus
its README and acceptance tests.

The dictionary is the raw (LZ77 prefix) kind that `brotli -D` and
brotli4j's PreparedDictionary use, so data moves freely between the
extension, the brotli tool and the app. Each connection caches the
prepared encoder dictionary. Invalid arguments, corrupt or truncated
input, trailing data and results over SQLITE_LIMIT_LENGTH are SQL
errors.

A Brotli stream has no checksum or dictionary id, so decoding with a
wrong dictionary of the same length can return wrong bytes with no
error: 21% of documentation.db rows do with another version's
dictionary. ADFA-6635 acceptance criterion 5 was amended to match, and
the README says so.

termux-packages has no such package, so build.sh now copies our own
packages from packages/<name>/ into termux-packages/packages/ on every
run, applies the com.termux rename to them, removes copies whose source
is gone, and stops if a name is also an upstream package or subpackage.
It reads repo.json with jq, which is now required.

Tests: all 73 pass on x86_64 Linux with SQLite 3.51.2, also under ASan
and UBSan, including decoding all 23,094 Brotli rows of a
documentation.db. NDK r29 cross-builds for aarch64 and arm export only
the extension's init function.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Register encodeBrotli and decodeBrotli as SQLITE_INNOCUOUS. The sqlite3
  shell turns trusted_schema off, so views, triggers, CHECK constraints
  and generated columns could not use them ("unsafe use of
  decodeBrotli()"). A new test covers this, and the previous build fails
  it.
- Correct the comment that said SQLite's length limit stops a
  decompression bomb before memory runs out: its 1 GB default can be more
  than a phone allows one process. The README now says to lower it with
  .limit length before decoding untrusted data.
- build.sh writes the .cotg-own-package marker before copying a package,
  so a copy cut short no longer stops every later build with a false
  "upstream already has this package" error.
- build.sh and the README say that a change to one of our packages needs
  a higher version or revision: build-package.sh skips a package already
  built at the same version, and publishing keeps the published file.
  sqlite-brotli goes to 1.0.0-1 for these changes.
- The com.termux rename is one helper, update_package_name, for upstream
  and for our packages. It handles any file name and leaves binary files
  alone. For the pinned termux-packages it produces the same tree as
  before.
- run-tests.sh quotes file paths for SQL, dot-commands and URIs, so paths
  with quotes, spaces, %, # or ? work. The package installs it
  executable.
- Add tests/hello.c, the hello-world extension that acceptance criterion
  1 loads, with build instructions in the README.

Tests: all 74 pass on x86_64 Linux with SQLite 3.51.2, also under ASan
and UBSan, and with every path under a directory named a b'c%d#e?f. In a
scratch copy, build.sh produces the same termux-packages tree as before
and passes the copy checks (edits, a copy cut short, spaces and binary
files, name collisions, removal). Termux's linter passes the recipe.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread build.sh Resolved
Comment thread packages/sqlite-brotli/build.sh Resolved
Answers Hal Eisen's review of PR #5.

build.sh: the untracked .cotg-own-package marker outlasts a forced
checkout or reset --hard of a newer termux-packages. If that commit adds
a package with our name, the old check took it for ours and replaced
upstream's files, and the cleanup could delete the whole package. Now git
decides: a package folder git tracks in termux-packages stops the build
before either rm -rf. The marker still limits the cleanup to folders
build.sh made. Git variables inherited from a caller (GIT_DIR,
GIT_INDEX_FILE) are dropped, and the build stops if termux-packages is not
a git checkout of its own or git cannot answer.

sqlite-brotli: depend on brotli (>= 1.1.0), which added the dictionary
functions the extension calls. Revision 2, so the new Depends line is
published.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@davidschachterADFA
davidschachterADFA merged commit f2320c8 into main Nov 16, 2026
davidschachterADFA added a commit that referenced this pull request Nov 17, 2026
ADFA-7081: Bring sqlite-js to main (#6 merged into #5's branch after #5 merged)

What the above gibberish says is that someone (I wonder who? Stop looking at me!) merged 6 into 5 AFTER 5 had already merged to main. You can drag a dead horse to water but he's not going to drink and likewise, 6 isn't going anywhere because it's been merged to a branch that was already dead, having been merged into main.

7 is 6 with some minor changes. 6 already had Daniel's approval, so I'm merging 7 to get 6's work into main.

If you understand this, give yourself a gold star.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants