Skip to content

Restart a ZIP Deflate member near a target: block search, seeded window, overlap proof (RustyGet) - #371

Merged
xbmc4lyfe merged 3 commits into
mainfrom
zip-deflate-restart
Nov 30, 2026
Merged

xbmc4lyfe merged 3 commits into
mainfrom
zip-deflate-restart

Conversation

@xbmc4lyfe

Copy link
Copy Markdown
Collaborator

Closes #363 (RustyGet#814, prybar#226).

An unencrypted ZIP Deflate member (method 8) can now restart near a target offset instead of only at its start.

Method

  • Search: estimate the packed position of the target, then search a bounded packed region before it.
    • Every bit offset is tested for a stored header (LEN/NLEN must match) and a dynamic header (complete code-length code, valid tables with an end-of-block code).
    • Fixed blocks are probed in a second pass at symbol boundaries.
  • Position: each candidate is decoded for 8 KiB without a window. The result is matched against the known plaintext to find its output offset; more than 16 fits makes the candidate ambiguous and it is skipped.
  • Overlap proof: an inflater seeded with the 32 KiB before that offset must reproduce the known plaintext byte for byte up to the target (default 64 KiB, at least 32 KiB + 1). Exactly one offset may pass.
  • Final check: decoding continues to the entry size, then CRC32 over the known prefix plus the decoded rest is checked. A size or CRC mismatch means no restart is accepted.
  • No candidate: the result is a typed NotFound, with a widened retry plan when one is possible. It never guesses.
  • Bounds: work per candidate and in total is budgeted, and packed reads stay within the search region plus the rest of the entry (proved with a counting reader).

API

  • zipfast::inflate64::restart: plan, restart, DeflateEntry, RestartOptions, DeflatePlan, DeflateRestart, RestartError, BlockKind, CrcProof.
  • Inflater: adds seeded, skip_bits and ignore_final. A match reaching past the seeded window is an error, never a panic.
  • map: ZIP method 8 is now Layer::ZipDeflate / RestartGrain::DeflateBlocks. open_at returns Start::Deflate, complete_deflate runs the restart, and failures come back as OpenAtError::Deflate.

Limits (docs/open-at.md)

  • Deflate64 (method 9) and encrypted members still restart only at the start.
  • A crafted second stream that matches the plaintext up to the target is caught only by the final size and CRC check, after output has been written.
  • Callers should pass a packed-position estimate (for example from an earlier copy). The ratio-based default can overshoot, and then the result is NotFound with a retry plan.

Bench (M4, 32 MiB, 1 MiB search region)

Each restart's search took about as long as inflating the whole entry from the start (2–84 ms). The packed bytes fetched were exactly the search region plus the rest of the entry.

Tests

tests/zip_deflate_restart.rs, 12 tests:

  • byte-exact restarts at many offsets for flate2 levels 1, 6 and 9, stored blocks, sync flushes, window-edge matches and fixed-block streams;
  • planted false candidates are rejected, and no false restart is ever accepted;
  • no-candidate reporting with the widened retry;
  • the counting reader;
  • hostile streams;
  • a seeded inflater refusing distances past its window;
  • the open_at handover.

Clean-room: written from RFC 1951 and observed behaviour only.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LG44gYJFfVCupfunL2idYi

…ow, overlap proof (#363)

zipfast::inflate64::restart finds a Deflate restart before a target from the
packed bytes of a search region and the known plaintext, written from RFC 1951
and observed behaviour:

- stored and dynamic headers at every bit (LEN/NLEN, Kraft-complete
  code-length code, valid literal/length and distance codes), then fixed-code
  blocks by probing (inside a fixed block any symbol boundary is a restart;
  a late result jumps the probe back by its own bits per byte);
- a windowless decode of 8 KiB per candidate (known bytes and references
  before the start) aligned against the plaintext; late and ambiguous
  candidates are skipped;
- an overlap proof: Inflater::seeded (new: window seeded from plaintext, a
  match past it is Error::Distance) with Inflater::skip_bits must reproduce
  the plaintext byte for byte from the restart to the target (> one window),
  for exactly one offset;
- the rest decoded to the entry's size and proven by CRC32 of the known
  prefix plus the rest; NotFound carries a widened plan, never a guess.

Wired into open_at: ZIP method 8 (unencrypted) is Layer::ZipDeflate /
RestartGrain::DeflateBlocks with Start::Deflate and complete_deflate.
Inflater::ignore_final lets the restart decoder disregard a borrowed final
bit. Tests in tests/zip_deflate_restart.rs, bench in
examples/deflate_restart_bench.rs, method and limits in docs/open-at.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LG44gYJFfVCupfunL2idYi
@xbmc4lyfe

Copy link
Copy Markdown
Collaborator Author

@codex review

xbmc4lyfe and others added 2 commits November 30, 2026 12:57
…on (PR #371 review)

- A stream must end exactly at the entry's size: keep stepping at the size and require
  end-of-stream with no more output (open only inside the restart's own first block);
  output past the size is Size.
- A fit in the known plaintext below the offsets tried makes a winner ambiguous.
- CRC32 required by default: RestartError::Unproven unless RestartOptions::allow_unproven.
- prove compares only output inside the known plaintext (no underflow at skip == produced).
- Work is charged for empty blocks, consumed input, block headers and dynamic table
  builds, and failed comparisons up to the first mismatch; more than 8 empty blocks in a
  row reject a candidate.
- The held search region is capped at work/8 (4 KiB..MAX_SEARCH) and reported as searched.
- DeflatePlan::widened saturates; DeflateRestart::rejected counts failed proofs.
- Tests: repeated text, known_base > 0 with and without prefix_crc, empty-block and
  failed-fit charging, region caps; docs updated, bench rerun.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LG44gYJFfVCupfunL2idYi
…t asserts (PR #371 review 2)

- NotFound's retry is cut to what one search holds and is None once the searched region
  cannot grow, so following retries never fetches bytes no search scans.
- Only a fixed-block probe's first header has its "final" bit ignored; stored and dynamic
  candidates trust theirs (windowless decode and overlap proof). A probe's first block is
  stopped at its end, and ends the stream at the size only with no whole packed byte left
  after it; anything left must decode to a real end. Steps stopped at a block end may make
  no progress once without counting as truncated.
- Tests: retries_never_fetch_bytes_no_search_scans, the_stream_ends_at_the_size (unit);
  the entry-start fallback must prove, and the capped-region case must be NotFound.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LG44gYJFfVCupfunL2idYi
@xbmc4lyfe

Copy link
Copy Markdown
Collaborator Author

Review: Codex did not respond (out of usage), so two clean-room subagent reviews took its place, one for hostile input and one for correctness against #363.

  • Round 1 found a false restart on repeated plaintext, plus an underflow, work not charged to the budget, and an uncapped region.
  • Fixes: d420a28 and 8a41846.
  • Both reviewers re-ran their probes against 8a41846 and gave a clean verdict, with no wrong-bytes Ok result in any case.

Local CI (full, all gates: fmt, clippy ×2, tests ×2, doctests ×2) passed at 8a41846, the exact head, up to date with main fa031ee, on linux x86_64 (rustyget-x86-q), linux arm64 (rustyget-arm-g) and macOS arm64.

@xbmc4lyfe
xbmc4lyfe merged commit d04984d into main Nov 30, 2026
0 of 7 checks passed
@xbmc4lyfe
xbmc4lyfe deleted the zip-deflate-restart branch November 30, 2026 19:54
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.

Restart a ZIP Deflate member near a target: block search, 32 KB window from known output, overlap proof

1 participant