Skip to content

Read archives that aren't in memory, a byte range at a time - #10

Merged
xbmc4lyfe merged 2 commits into
mainfrom
claude/ranged
Nov 7, 2026
Merged

xbmc4lyfe merged 2 commits into
mainfrom
claude/ranged

Conversation

@xbmc4lyfe

@xbmc4lyfe xbmc4lyfe commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

What

ZipReader::new(&[u8]) takes the whole archive as one slice, so reading a 60 GB archive on a server, or one not on local disk, meant 60 GB in memory. Someone trying to use zipview for that hit exactly this.

ZipIndex opens an archive of a known length through reads of byte ranges, and holds only what it read:

  • Opening. One read of the end of the archive, up to 65 KB: the end record, its longest comment and the ZIP64 records. When the central directory is small it's in that read too. Otherwise a second read fetches it, in pieces of at most 16 MiB. A 50 GiB test archive opens with a single 65 KB read.
  • Entries. fetch_entry reads the 30-byte local header, then that entry's header and data. fetch_password_check reads the header, then the first 18 bytes of data.
  • Bounds. No read is over 16 MiB, so a directory size field that lies can't make a caller allocate more than that at once. An entry over the 256 MiB inflate limit is refused before its bytes are read, and so is one whose compressed size couldn't fit that limit. Buffers for entry reads are reserved fallibly, so a failed allocation returns an error rather than aborting.

Two ways to drive it:

  • Blocking. ZipIndex::open(len, read), fetch_entry(entry, password, read) and fetch_password_check(entry, read) take read(offset, buf). Errors come back as the caller's error type via From<ZipReadError>. std::io::Error now converts, so |o, b| file.read_exact_at(b, o) just works.
  • One read at a time, for asynchronous fetching (HTTP ranges, pieces from elsewhere): while let Some(range) = opener.next_read()? { opener.supply(range.start, &fetch(range).await?)?; } let index = opener.finish()?;. For entries, header_range / entry_range / password_check_range name the bytes, and read_entry / password_check finish from them.

How it's built:

  • Opener (src/index.rs). It follows ZipReader::new step by step: end-record search, ZIP64 locator and record, the declared-then-computed directory location for stub-prefixed archives, and the entry walk. Central headers go through the same parse_central_entry.
  • Walk state. The walk over the directory (cursor, headers left, entries so far) is kept between reads, so each header is parsed once. Directory bytes already parsed are dropped, so a big directory isn't held twice. A partly supplied read is asked for again only from where the supplied bytes end.
  • Entry reads. ZipReader::read_entry_with_password and password_check were split into a shared read_local_entry / local_password_check over "bytes from the local header on". The in-memory reader passes the whole archive and an offset; ZipIndex passes what it read. This is a pure move.
  • Inlining. parse_central_entry is now #[inline(always)]. With a second caller, the compiler had stopped inlining it into ZipReader::new, and open-20000 went 460 → 515 µs. With the attribute it's back to 448–462 µs, against main's 454–470.

Measured (fastest round, M4 Air, examples/bench.rs)

case main this
open-20000 (ZipReader) 454–470 µs 448–462 µs
index-open-20000 (new: ZipIndex over the same bytes from memory) — 597 µs
inflate, stored, by_name, aes256-entry unchanged unchanged

index-open-20000 pays for copying the tail and directory out of the reads, which is small next to any real fetch.

Correctness

  • Differential tests (new, in tests/writer.rs) open every input both ways and require the same entries, comment and open error. They also require the same bytes or error from every entry, with no password, the right one and a wrong one, and the same password checks. The inputs:
    • the fixtures: empty, a 65,000-byte comment, plain, ZIP64, stub-prefixed, ZIP64 plus prefix, ZipCrypto, ZipCrypto with ZIP64, AES-256, and AES with a prefix;
    • every truncation and every single-byte corruption (two flips per byte) of the plain, ZIP64, prefixed and ZipCrypto archives.
  • The test reader panics on any read past the end of the archive.
  • A 21 MB directory supplied 64 KiB at a time (over 300 reads) opens in 0.3 s and matches ZipReader. Between reads the opener holds at most the tail plus one header. This is a unit test; before the fix from review, each read re-parsed the directory from the start.
  • Read counts are pinned: a small directory opens in 1 read, a ~200 KB directory in 2, an entry in 2. A ~17 MB directory takes several reads, none over 16 MiB. A 50 GiB sparse archive opens with one 65 KB read, reads readme.txt, and refuses the 50 GiB entry without reading it. A 60 GB archive whose end record claims a 4 GiB directory takes at most 3 reads of at most 16 MiB.
  • The writer gained ZipCrypto (written from the PKWARE note), WinZip AES-256 (using the crate's key derivation and CTR, since these fixtures test ranged reading, not the cipher) and ZIP64 end records. A new test checks the reader decrypts what it writes.
  • Breaking the opener on purpose is caught. Each of these fails 1–3 tests: dropping the prefix shift, ignoring the ZIP64 record, and an 18 → 12-byte password-check read.
  • Fuzzing. fuzz_zip now also opens every input with ZipIndex and asserts it agrees with ZipReader on the entries, every entry's bytes with and without a password, and every password check. It ran 180,806 inputs locally (3 minutes, on the final code) with no findings.
  • cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo test --all-targets (debug, as CI) and cargo test --doc pass on aarch64-apple-darwin. The same checks pass in release on x86_64-linux.

🤖 Generated with Claude Code

ZipReader takes the whole archive as one slice, so a 60 GB archive on a
server meant 60 GB in memory. ZipIndex opens an archive of a given length
through reads of byte ranges: the end of the archive (one read of up to
65 KB, which also holds the directory when it is small), then the central
directory in reads of at most 16 MiB. An entry is read on its own, its
local header and then the entry, and a password check takes two small
reads. ZipIndex::open and fetch_entry take a blocking read(offset, buf);
ZipIndexOpener, header_range, entry_range and password_check_range do the
same one step at a time for asynchronous callers. The opener follows
ZipReader::new step by step and entry reads share its code, so a new
differential test holds the two readers to the same entries, comment,
errors, entry bytes and password checks on every fixture (plain, ZIP64,
stub-prefixed, ZipCrypto, AES), every truncation and every single-byte
corruption, and the fuzz target asserts the same. A 50 GiB test archive
opens with one 65 KB read. The test writer gains ZipCrypto, AES-256 and
ZIP64 end records.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-07T14:04:58.907834Z aa68e1f Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1777d299fa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/index.rs Outdated
Comment on lines +155 to +156
pub fn step(&self) -> Result<OpenStep> {
match self.open() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Resume directory parsing instead of restarting on every step

For central directories larger than the 16 MiB read limit, every call to step() reruns open() from the beginning, creating a fresh entries vector and parsing all previously supplied headers again before requesting the next chunk. Both the blocking and asynchronous APIs therefore perform quadratic parsing and string-allocation work as the directory grows: a 1 GiB directory requires roughly 64 passes over progressively larger prefixes, processing about 32 GiB of directory data. This particularly penalizes the large archives this API is intended to support. Preserve the parsing cursor and accumulated entries across reads so each header is parsed once.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in aa68e1f. The opener now keeps its walk over the directory (cursor, headers left, entries parsed so far) between reads, so each header is parsed once. It drops directory bytes it has already parsed, and asks only for the part of a read not yet supplied. The API is now next_read / supply / finish. New unit test a_big_directory_is_parsed_once_and_not_held_twice: a 21 MB directory supplied 64 KiB at a time (300+ reads) opens in 0.3 s, matches ZipReader, and never holds more than the tail plus one header between reads.

Every step re-ran the open from the start, so a directory needing several
reads was parsed again on each, quadratic in its size (Codex review). The
opener now keeps its walk (cursor, count left, entries so far) between
reads, drops directory bytes it has parsed, and asks only for the part of a
read not yet supplied. Its API becomes next_read / supply / finish. A
21 MB directory supplied in over 300 partial reads now opens in 0.3 s,
holding at most the tail and one header between reads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@xbmc4lyfe

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: aa68e1f036

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@xbmc4lyfe
xbmc4lyfe merged commit 2c3d73a into main Nov 7, 2026
3 checks passed
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.

1 participant