Introducing a new way to review your PRs.

Read the announcement

keiyoushi / extensions-source

Source code of extensions in https://github.com/keiyoushi/extensions

View on GitHub

13 documents · Updated Nov 19, 2026

docs / shared-library-modules

Shared library modules under lib/

Overview

The lib/ tree contains reusable Android/Kotlin extension libraries rather than source-specific code. Most modules expose either a small helper API that an extension calls while building Page objects, an OkHttp Interceptor that rewrites synthetic image URLs into displayable responses, or both. Reader libraries such as lib/speedbinb, lib/publus, lib/clipstudioreader, and lib/e4p follow a common pattern: fetch a vendor manifest/configuration, encode per-page decode state into the image URL fragment, then require the extension's OkHttpClient to include the matching interceptor so the image response is descrambled or decrypted at load time.

Networking support is centered on lib/cronet and lib/cronetruntime. lib/…/Cronet.kt adds OkHttpClient.Builder.cronet(), which swaps OkHttp's network call for a Cronet-backed one only when running on Android and only when the client has not customized TLS, proxy, DNS-affecting settings, or authentication that Cronet cannot honor. lib/…/RuntimeLoader.kt loads a generated assets/cronet-runtime.apk into a process-wide DexClassLoader; that asset is produced by lib/…/build.gradle.kts from the separate application module lib/cronetruntime. The runtime entry point lib/…/CronetRuntime.java prefers platform HttpEngine, falls back to Play Services Cronet, and exposes an OkHttp Interceptor implemented by lib/…/CronetServer.java.

Utility modules provide self-contained primitives used by extensions when a site exposes custom encodings. lib/aessiv implements AES-SIV and AES-GCM-SIV decryption. lib/ece implements RFC 8188 aes128gcm payload decryption, HKDF, and raw P-256 public-key conversion. lib/secretstream ports libsodium-style XChaCha20-Poly1305 secretstream pull support plus a BigInteger X25519 helper. lib/lzstring reimplements lz-string Base64 compression/decompression, lib/unpacker unpacks Dean Edwards packer-compressed JavaScript, and lib/synchrony runs the bundled Synchrony JavaScript deobfuscator through QuickJS.

Image and text adapters bridge nonstandard page payloads into images the host app can render. lib/…/DataImageInterceptor.kt turns data:image attributes into loopback fake URLs and serves decoded bytes. lib/…/TextInterceptor.kt renders title/body text URLs generated by TextInterceptorHelper.createUrl() into PNG pages, with optional APK-bundled fallback fonts. lib/…/ImageDecoder.kt chooses between Mihon, Tachiyomi, and Android decoders at runtime, dropping unavailable host decoders after linkage failures.

Library catalog

  • lib/…/AesSiv.kt decrypts RFC 5297 AES-SIV ciphertexts in tag || ciphertext format with one or more AAD byte arrays. lib/…/AesGcmSiv.kt decrypts RFC 8452 AES-GCM-SIV ciphertext || tag payloads using AES-ECB blocks because Android only exposes AES/GCM-SIV/NoPadding on newer APIs.
  • lib/…/ClipStudioReader.kt extracts pages from Clip Studio Reader comic and EPUB viewers. For comics it obtains a time key, face XML, page XML URLs, grid dimensions, spread halves, and fragments beginning with csr-page; for EPUBs it fetches viewer API config, a bearer token, OPF image entries, optional XOR image key, and fragments beginning with csr-epub. lib/…/ClipStudioReaderInterceptor.kt consumes those fragments, sets required referer/bearer headers, unscrambles comic grids, crops spread halves, and XOR-deobfuscates EPUB image headers when needed. DTOs are in lib/…/ClipStudioReaderDto.kt.
  • lib/cronet provides the extension-facing OkHttp integration. Cronet.kt rebuilds the remaining interceptor chain around CronetChain.kt, injects a TransportProtocol(Protocol.HTTP_2) tag, handles redirects/retries through FollowUpInterceptor.kt, and exposes Response.isCronet. RuntimeLoader.kt is responsible for process-wide dynamic loading of the runtime APK asset.
  • lib/cronetruntime is the embedded runtime APK shipped by lib/cronet. CronetRuntime.java selects and initializes the actual Cronet engine; CronetServer.java adapts Cronet's async UrlRequest API to OkHttp responses, upload bodies, timeouts, cancellation, response headers, decoded-content handling, and streaming ResponseBody reads. The org/chromium/net/* files are the API surface used by Play Services/HttpEngine Cronet.
  • lib/…/DataImageInterceptor.kt exposes Jsoup Element.dataImageAsUrl() and dataImageAsUrlOrNull() helpers and an interceptor for fake https://127.0.0.1/?image... URLs. It decodes Base64 or raw data URI payloads and returns an image response.
  • lib/e4p supports E4P/QSC manifests and XEBP image decryption. E4PManifestReader.kt reads encrypted or plain protobuf manifests, picks the highest-height image variant, preserves auth query parameters, and embeds XEBP decryption data in URL fragments. E4PInterceptor.kt reads the QSC directory via an HTTP Range request, fetches the selected file range, strips plain XEBP containers to WebP, or calls XebpDecoder.kt when fragment decryption context is present. E4PDecoder.kt unwraps TDRM/CDRM manifest wrappers with RC4, custom Blowfish, Blake2-derived keys, and ChaCha8; TiffDecoder.kt decodes the TIFF patch image used by XebpDecoder.kt; protobuf DTOs live in E4PManifestDto.kt.
  • lib/…/Ece.kt decrypts RFC 8188 aes128gcm encrypted content, exposes HKDF, and converts P-256 public keys between Java ECPublicKey and raw uncompressed 0x04 || X || Y form while checking curve membership.
  • lib/…/Intl.kt loads UTF-8 .properties bundles from assets/i18n/messages_<language>.properties, chooses a requested language only if present in the supplied set, falls back to the base language per key, exposes locale-aware formatting, and provides a locale collator/display-name helper.
  • lib/…/ImageDecoder.kt decodes image bytes through host-provided Mihon or Tachiyomi decoders before falling back to BitmapFactory. It catches LinkageError and removes decoders that the current host app does not ship.
  • lib/…/LZString.kt implements compressToBase64() and decompressFromBase64() compatible with the JavaScript lz-string library.
  • lib/publus parses Publus configurations and performs image filename generation/auth/unscrambling. PublusPages.kt fetches configuration_pack.json, decrypts packed config data through Decoder in Publus.kt, builds image URLs through PublusImage.generateFilename(), and stores page decode state in a JSON fragment. PublusInterceptor.kt parses that fragment and uses PublusImage.unscramble() or unscrambleNoKeys() to rebuild JPEG pages. PublusAuth.kt models CloudFront/book auth query parameters and caches per-chapter auth with refresh support. DTOs and fragment parsing are in PublusDto.kt.
  • lib/secretstream contains Java ports of ChaCha20, Poly1305, Core HChaCha20, State, and SecretStream pull-mode decryption derived from libsodium, plus X25519.kt for one-shot shared-secret derivation. SecretStream.pull() verifies Poly1305 tags, decrypts chunks, updates/rekeys stream state, and returns a message/tag pair.
  • lib/speedbinb supports SpeedBinb/PTBinB readers. SpeedBinbReader.kt extracts page lists from reader HTML or the bibGetCntntInfo API, decodes scramble tables via Crypto.kt, constructs image URLs for Direct/REST/SBC servers, and stores ptbinb key pairs in URL fragments. SpeedBinbInterceptor.kt routes synthetic text pages through TextInterceptor, fetches .ptimg.json metadata images, selects PtImgDescrambler, PtBinbDescramblerA, or PtBinbDescramblerF, and recompresses descrambled bitmaps as JPEG. DTOs are in Models.kt, and the shared bitmap move logic is in descrambler/SpeedBinbDescrambler.kt.
  • lib/…/Deobfuscator.kt loads assets/synchrony-v2.4.5.1.js, rewrites its ESM export into globals QuickJS can evaluate, JSON-quotes the source script, and returns Synchrony's deobfuscated JavaScript source.
  • lib/…/TextInterceptor.kt serves generated http://tachiyomi-lib-textinterceptor/<title>/<body> URLs as rendered PNG pages. It uses Android text layout/canvas APIs, HTML entity cleanup, and optional per-character fallback font spans.
  • lib/…/Unpacker.kt expands Dean Edwards packer output by extracting the packed data/dictionary and replacing radix-62 tokens. SubstringExtractor.kt is its cursor-style delimiter helper.

Notable APIs and flows

  • Cronet request flow: an extension adds OkHttpClient.Builder.cronet() from lib/…/Cronet.kt. The interceptor loads CronetRuntime.server(Context) from the runtime APK, checks for unsupported client settings, reconstructs OkHttp's bridge/cache/network interceptors, and ends the chain with CronetServer. CronetServer.java turns a single OkHttp request into a Cronet UrlRequest, copies allowed headers, buffers upload bodies, maps RFC 9218 Priority to Cronet priorities, waits for response headers with OkHttp timeout/cancellation semantics, and exposes body reads through an Okio Source.
  • Fragment-driven image post-processing: ClipStudioReader.kt, SpeedBinbReader.kt, PublusPages.kt, and E4PManifestReader.kt all embed decode parameters in URL fragments rather than external state. Their interceptors (ClipStudioReaderInterceptor.kt, SpeedBinbInterceptor.kt, PublusInterceptor.kt, E4PInterceptor.kt) parse those fragments after the network response and return a normal image response to the app. If an extension calls the page-fetch helper but forgets to install the corresponding interceptor, the app will request the raw vendor URL and see scrambled, encrypted, or XML/JSON payloads.
  • Synthetic page URLs: DataImageInterceptor.kt and TextInterceptor.kt intentionally create non-site URLs that OkHttp can route through an interceptor. dataImageAsUrl() uses https://127.0.0.1/?... for data URIs, while TextInterceptorHelper.createUrl() uses the fixed host tachiyomi-lib-textinterceptor. These URLs are not intended to reach the network.
  • E4P/XEBP page flow: E4PManifestReader.kt fetches an E4P ticket/protobuf manifest, E4PDecoder.kt decrypts/decompresses it, and the reader appends a fragment containing the QSC entry name plus optional XEBP IV/content/consumer/PBEX seed. E4PInterceptor.kt uses the fragment entry name to range-read the file out of the QSC archive and calls XebpDecoder.kt to decrypt a TIFF watermark patch and composite it over the VP8 WebP base image.
  • Publus auth and fragment flow: PublusAuthInfo.toAuth() converts API auth blocks into query parameters; PublusAuthHandler can refresh stale signed auth during a reading session. fetchPages() stores page attributes, keys, and optional session extra in PublusFragment, while PublusInterceptor reads that fragment to decide whether and how to unscramble.

Coupling

Risks

  • lib/…/Cronet.kt reflectively constructs OkHttp internal BridgeInterceptor and CacheInterceptor classes. If OkHttp changes those class names or constructor shapes, Cronet requests fall back to normal OkHttp, and only the once-per-message log in Cronet.kt shows why.
  • lib/…/CronetServer.java buffers the entire request body into memory before installing a Cronet UploadDataProvider. Large uploads through a Cronet-enabled client can increase memory pressure even though response bodies are streamed.
  • lib/…/E4PDecoder.kt, lib/…/XebpDecoder.kt, and lib/…/TiffDecoder.kt encode reverse-engineered DRM/container constants and binary layout assumptions. A site-side update to E4P ticket types, XEBP chunks, PBEX seeds, or TIFF patch format will fail as manifest decode errors, QSC lookup errors, or corrupted image output.
  • lib/…/SpeedBinbReader.kt explicitly works around Jsoup behavior by parsing only the first <t-case> because malformed TTX can nest fallbacks after 128 pages. Changes to that parsing can drop pages or include fallback markup as pages.
  • lib/…/X25519.kt states that its BigInteger scalar multiplication is not constant-time. It is intended for fresh one-shot private scalars only; reusing long-lived private keys through this helper would expose timing-risky key material.