Skip to content

feat(streaming): bounded, resumable LZ77 decoder for R2004-family pages - #246

Merged
DrunkOnJava merged 3 commits into
mainfrom
feat/streaming-lz77
Oct 26, 2026
Merged

DrunkOnJava merged 3 commits into
mainfrom
feat/streaming-lz77

Conversation

@DrunkOnJava

Copy link
Copy Markdown
Owner

Summary

streaming::Lz77Decoder is a bounded, resumable decoder for the R2004-family LZ77 dialect: it decodes one compressed page from any prefix of its bytes into an output slice of any size, pauses between any two calls, and can be checkpointed mid-page and resumed in a fresh process. Advances #205 (R2004+ decompression path) and #207 (decompression checkpoints); neither closes.

Spec reference

ODA Open Design Specification for .dwg files v5.4.1, §4.7 (compression), with the page framing of §4.3 (system pages) and §4.5/§4.6 (data pages) used only to locate the streams under test.

  • Opcode semantics are the crate's own lz77::decompress, measured against AutoCAD-written files: distances are one more than §4.7's text (+0x4000 for 0x10–0x1F, +1 otherwise), and bit 3 of an 0x12–0x1F opcode is bit 14 of the distance. The largest distance is therefore 0xBFFF, so a fixed 0xC000-byte window decodes any page, including system pages whose declared size has no cap. State is that window plus 80 bytes (49,232 bytes measured on aarch64), compile-time bounded at WINDOW + 128 in the no_std, no-allocator kernel, native and wasm32-unknown-unknown.
  • The declared decompressed size is a hard ceiling (Lz77Overrun, checked before anything is written). A reserved opcode is Lz77Opcode, a distance before the first output byte Lz77Offset (each with its input position), and a stream cut before 0x11 is Lz77Truncated from end_of_input(). Errors are terminal; output is provisional until the caller's commit point.
  • Lz77Checkpoint: opcode cursor, pending copy or literal run, totals, limit and the last min(produced, 0xC000) output bytes under a CRC-32 (54 + kept + 4 bytes, at most 49,210; 29,754 for a full 0x7400 page). Restore checks length, version and CRC before copying anything, then requires a state decode can reach.

Test plan

Measured on the rebased commit:

  • python3 tools/streaming/check_invariants.py and both kernel builds (native and wasm32-unknown-unknown).
  • python3 tools/streaming/verify_lz77.py target/debug/examples/stream_lz77 target/debug/examples/streaming_schedules: the four canonical R2004-family fixtures and every R2004, R2010 and R2013 file at nextgis/dwg_samples@ff05d5a (21 files, fetched by sha256 with bounded retry from tools/streaming/lz77-corpus.json, never committed): 25 files, 266 compressed pages (page map, section map, every LZ77 data page), 6,097,392 decompressed bytes. For every page the output length and CRC-32 equal lz77::decompress, and output, CRC and bytes consumed through 0x11 equal an independent Python §4.7 parse. Four fixed input/output schedules (down to one byte in and one byte out, with Pending polls) reproduce every file; 128 proptest input schedules on each of three files. Fresh-process resume inside the largest data page at every call of the canonical page (41 stops) and 45–46 sampled calls of line_2004, line_2010 and line_2013: source reads and output of the two processes concatenate to the uninterrupted run's. Checkpoints taken in seven of the ten decoder modes; 63 bit flips, 63 truncations, an oversized input and nine resealed unreachable states are rejected. A reserved opcode, a distance before the first byte, an output past the declared size and a stream cut before the terminator each end in their named error.
  • The same run passed on GitHub runners via workflow_dispatch on stacked heads containing this commit (run 40296858014, run 40299178744). This head's tree is byte-identical to the locally verified one (7078b85).
  • By hand, outside CI: the local R2018 sample_AC1032.dwg (Establish the provenance of sample_AC1032.dwg, the one R2018 file #96, not fetchable): 53 pages equal to lz77::decompress.
  • fmt, clippy (-D warnings, all targets), cargo doc -D warnings, build_fixtures byte-identical.
  • No new unit tests.

Measured on the corpus and recorded in the docs: every AutoCAD-written page decompresses to exactly its declared size, and every stream ends 11 00 00. The crate's writer does neither; that is recorded on #163 with the numbers.

Not measured: R2018 in CI, pages larger than 0x7400, throughput. R2007 (§5.10) is not covered.

tools/streaming/lz77-corpus.json pins the same nextgis commit the corpus-CI lane (#95) is pinning; that lane converges the manifests after this lands.

Source-provenance declaration

Clean-room declaration. All code in this PR was written from scratch. The only sources consulted were this repository and the ODA's public Open Design Specification for .dwg files v5.4.1. The distance rules come from this repository's own src/lz77.rs (whose provenance is recorded in CLEANROOM.md). No Autodesk SDK, ODA SDK (Drawings / Teigha), LibreDWG or other copyleft DWG implementation source, and no decompilation, was consulted. Spec citations are in the diff.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T10:14:17.221195Z b05686f New commits
ℹ️ 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.

@DrunkOnJava
DrunkOnJava enabled auto-merge (squash) October 26, 2026 09:49

@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: 4f302cbfd3

ℹ️ 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/streaming/lz77.rs Resolved
Lz77Decoder decodes one §4.7 stream from any prefix of its compressed
bytes into an output slice of any size, and can pause between any two
calls. Its only page state is a fixed 0xC000-byte window: with the
crate's measured distance rules (+0x4000 for 0x10-0x1F, +1 otherwise,
bit 3 of 0x12-0x1F as distance bit 14) no back-reference reaches
further, so no page, including a system page with no declared cap,
needs more. The declared decompressed size is a hard ceiling; reserved
opcodes, distances before the first byte, overruns and streams cut
before 0x11 are typed terminal errors. It compiles in the no_std,
no-allocator kernel natively and for wasm32, bounded at WINDOW + 128.

Lz77Checkpoint carries the opcode cursor, pending run, totals and the
last min(produced, 0xC000) output bytes under a CRC-32; restore checks
length, version and CRC before copying and then requires a reachable
state.

stream_lz77 decodes every compressed page of an R2004-family file (page
map, section map, every LZ77 data page). verify_lz77.py runs it on the
four canonical R2004-family fixtures and every R2004, R2010 and R2013
file in the pinned nextgis/dwg_samples commit: 25 files, 266 pages,
6,097,392 bytes, equal to lz77::decompress and to an independent Python
parse (length, CRC-32, bytes consumed through 0x11). Fixed and proptest
schedules down to one byte in and out reproduce the output; fresh-process
resume inside the largest data page leaves no source byte read twice and
no output duplicated; corrupt, truncated and resealed checkpoints and
four stream corruptions end in their named errors. It runs in the
required Streaming E2E job.

Every AutoCAD-written page decompresses to its declared size and every
stream ends 11 00 00; the crate's writer does neither (recorded on #163).

Refs #205, #207
A CRC-valid decoder checkpoint may carry any consumed count; restore
only required it to be non-zero. The next literal or opcode byte then
added to it unchecked: a panic in debug builds, a wrapped input position
in release. consumed is now accumulated with checked_add before any
state moves, and the overflow is StreamError::CounterOverflow, terminal
like every other decoder error.

Audit of every restored counter: produced cannot pass its limit, since
restore and each transition keep every pending run within limit -
produced; acc is saturating and bounded by the limit; count and distance
are bounded by consistent(). The walk cursors are bounded by
consistent() against the verified u32 locators, and both header parsers
are rebuilt by replay.

stream_lz77 gains --decode-checkpoint <path> --input <hex>, which feeds a
restored decoder directly, and its own resume check no longer trusts the
caller file's counters. verify_lz77.py now forges consumed = 2^64 - 1 in
a literal run and at an opcode (both end in CounterOverflow, with no
panic) and decodes a literal run that ends exactly at a limit of
2^64 - 1.

Reported by the review on #246.
@DrunkOnJava
DrunkOnJava merged commit 2cb5a70 into main Oct 26, 2026
25 checks passed
@DrunkOnJava
DrunkOnJava deleted the feat/streaming-lz77 branch October 26, 2026 10:19
DrunkOnJava added a commit that referenced this pull request Oct 28, 2026
… to what landed (#205, #206, #207) (#400)

The ledger stopped at the header and the R13-R2000 walk, and the README's
checkpoint contract described the header checkpoint alone and said full object
and decompression checkpoints remain open. Both now cover the ten checkpoint
kinds on main and the walkers of #240, #246, #260, #276, #299, #312, #333, #340
and #363, the perf series #360 to #366, and cite the evidence files of Streaming
E2E run 40532346151. What was not measured is marked NOT MEASURED.
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