Skip to content

fix(classes): write_class_map reproduces AutoCAD's AcDb:Classes byte for byte (§10.1, §10.2, #45) - #405

Merged
DrunkOnJava merged 7 commits into
mainfrom
fix/class-map-writer-byte-exact
Oct 27, 2026
Merged

DrunkOnJava merged 7 commits into
mainfrom
fix/class-map-writer-byte-exact

Conversation

@DrunkOnJava

@DrunkOnJava DrunkOnJava commented Oct 27, 2026 •

Copy link
Copy Markdown
Owner

Summary

write_class_map now writes AcDb:Classes the way AutoCAD does, byte for byte: on all 61 pinned drawings, R14 to R2018 (the Corpus job's run, reported below), reading the stored section, parsing it and writing it back gives the same bytes. Closes #45.

Spec reference

ODA v5.4.1 §10.1 (R13 to R15) and §10.2 (R18 and later), with the fields the spec leaves open taken from AutoCAD's own pinned drawings. #45 and its 2026-10-25 note named some of the departures (the size in bits lacked the 32 real files add, the string-stream trailer was missing, the CRC began at the sentinel, R13 to R15 opened with a BL and a B, and the R2010 and R2013 CRC rule was unmeasured). The probe settled each against real files and found the rest:

  • the size in bytes on R2007 and later counts the bits RL along with the stream;
  • R13 to R15 have no BL maximum and no B before the first record;
  • the CRC starts at the size word, not the sentinel, and covers the bit-size words on R2007 and later (every release measured, R2010 and R2013 included);
  • the end sentinel after the CRC was never written, and R2004 and later carry eight zero bytes after it (§10.2 says "the ODA writes 0 bytes");
  • pre-R2007 names end in a NUL that the BS length counts (read_tv drops it);
  • the bits that pad the class data to a byte are ones;
  • a string block of 0x8000 bits or more takes two RS words (sample_AC1032.dwg: 45,804 bits).

The layout table and what each rule rests on is the new ARCHITECTURE.md §7ax.

Test plan

CI is the evidence. The first five items were run locally before builds moved to the GitHub runners; the checks on this head replace them.

  • cargo fmt --all -- --check, cargo clippy --all-targets --all-features -- -D warnings, cargo test --release --all-features (767 library tests, every integration file) and RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features passed locally; python3 tools/ci/spec_coverage.py --check and python3 tools/maintainer/train/arch.py check pass
  • The Corpus job runs the new examples/probe_class_roundtrip.rs with --strict on every pinned drawing (step "Class table written back byte for byte on every pinned drawing", log class-roundtrip-probe.log in the corpus-measurement artifact). Its module doc says what it proves. The run on head 39fe95b, https://github.com/DrunkOnJava/dwg-rs/actions/runs/40592472261, reports:
release  match  differ    failed  no section
2000         9       0         0           0
2004        12       0         0           0
2007         8       0         0           0
2010         8       0         0           0
2013         8       0         0           0
2018         4       0         0           0
R14         12       0         0           0
TOTAL       61       0         0           0   of 61 drawings, 0 could not be opened

The earlier figures in this PR (52 of 52 on the drawings pinned then, 61 of 61 over my local corpus cache) were pre-ban local runs; tools/corpus/pins.tsv now pins all 61 (acadsharp 10, bookviewerapp3 4, nextgis 42, oshw-jetson 4, planar-algorithm 1) and the CI run above is the record: 61 of 61 match, none differs, fails or cannot be opened. The run's corpus-measurement artifact holds the whole log, one line per drawing.

Before this change the probe, built against the old writer on the previous main and run locally on the 52 drawings pinned then, matched 0 of 52, the first differing byte 16 (the size word) on every drawing. That is the only figure here not reproduced by CI, since CI runs the branch's writer only.

Where a drawing differs the probe names the first differing byte, the part of the section it falls in and both values. --strict fails on a difference, on a drawing it cannot open, read or parse, and on a run in which nothing matched, so a parser regression that empties every class map fails the job. Pre-R2007 names are read and written under the drawing's own code page.

An independent Python port of the same layout, written from the measured bytes and not from the Rust, also reproduced all eight files it was tried on (local).

Not measured: R13 (AC1012), the corpus holds no AutoCAD-written file; it is written by the R14 rule. The two unknown BLs of an R2004+ record and the B after the maximum class number are written as 0 and true; every pinned section matches with those values.

Existing tests changed

Two unit tests in src/classes.rs pinned the old layout and move with it (no test added): the empty-table test now expects 51 bytes (sentinel, size, stream, CRC, end sentinel, eight zeros), and the R2010 header test expects the size word 11, the bit size 52 + 32 and a length 10 bytes longer than R2004's (8 of header, 2 of trailer).

write_tv also keeps the code-page encoding from #382 (merged into this branch's base): the encoded bytes now carry the terminating NUL on R2004 and earlier.

Source provenance: ODA v5.4.1 §10 and the pinned public drawings (AutoCAD output) read by the probe. No algorithm-description comments from any other project were read, so there is nothing to add to CLEANROOM.md.

Source-provenance declaration

By submitting this PR I certify that my contribution does not incorporate executable code from any source whose license is incompatible with Apache-2.0. Specifically:

  • No Autodesk DWG SDK source (RealDWG, ObjectARX, ObjectDBX) or NDA-protected Autodesk documentation was consulted or imported.
  • No Open Design Alliance Teigha / Drawings SDK source was consulted or imported. The ODA's public Open Design Specification for .dwg files PDF is explicitly allowed (see CLEANROOM.md).
  • No GPL-licensed DWG implementation source (LibreDWG, etc.) was consulted or imported. This matters because dwg-rs is Apache-2.0 and must remain compatible.

My contribution is based on:

  • The ODA's freely-redistributable Open Design Specification for .dwg files (v5.4.1);
  • Public reverse-engineering notes, research papers, or blog posts cited inline;
  • Reading algorithm-description comments (not executable code) in permissively-licensed projects (MIT / Apache / BSD) to resolve spec ambiguities — disclose any such reading in this PR body so it can be recorded in CLEANROOM.md.

See CLEANROOM.md for the full allowed/forbidden source list and the honest scope of what "clean-room" means for this project.

Developer Certificate of Origin

By submitting this PR, I certify that the contribution complies with the
Developer Certificate of Origin 1.1.
Specifically, I affirm that the contribution is my own work (or rightfully
licensed to me) and that it can be distributed under the project's Apache-2.0
license.

I will sign off commits with git commit -s when asked, which adds a
Signed-off-by trailer to the commit message asserting the DCO.

…for byte (§10.1, §10.2, #45)

The writer was the inverse of ClassMap::parse and nothing more: the size
in bits lacked the 32 real files add, the string-stream trailer was
missing, R13 to R15 opened with a BL and a B that §10.1 does not have,
the CRC began at the sentinel, the end sentinel and the eight zero bytes
after it were absent, pre-R2007 names carried no terminating NUL and the
padding bits were clear.

examples/probe_class_roundtrip.rs reads each drawing's section, parses
it, writes it back for the drawing's own release and compares the bytes.
Before: 0 of the 52 pinned drawings match. After: 52 of 52, and 61 of 61
over the whole local corpus cache.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 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-27T22:37:14.748820Z 3f743c3 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.

@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: 60f75853cd

ℹ️ 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 examples/probe_class_roundtrip.rs Resolved
Comment thread examples/probe_class_roundtrip.rs Resolved
…ng's code page and fails strict on what it cannot compare (#45)

The probe parsed and wrote class names under the default code page, so a
pre-R2007 drawing with non-ASCII names measured an encoding mismatch
rather than the writer. Both now run inside with_code_page(file.code_page()).

--strict stayed green when a drawing could not be opened, its section could
not be read, or its classes did not parse, because only a byte difference
set the flag. Those are counted separately from a drawing that has no
AcDb:Classes section, fail strict, and so does a run in which nothing
matched, so a parser regression that empties every class map turns it red.
… Corpus job (#45)

The class-table writer's byte-exactness rested on local runs from before
builds moved to the runners. The Corpus job now runs the probe on the
fetched corpus, fails on a difference, an unreadable drawing or a run in
which nothing matched, and uploads class-roundtrip-probe.log in the
corpus-measurement artifact. ARCHITECTURE.md names that run as the record
and labels the earlier figures as local.
@DrunkOnJava
DrunkOnJava enabled auto-merge (squash) October 27, 2026 22:42
@DrunkOnJava
DrunkOnJava merged commit 07c71b5 into main Oct 27, 2026
25 checks passed
@DrunkOnJava
DrunkOnJava deleted the fix/class-map-writer-byte-exact branch October 27, 2026 22:51
DrunkOnJava added a commit that referenced this pull request Oct 27, 2026
DrunkOnJava added a commit that referenced this pull request Oct 28, 2026
…10-27 (#416)

* docs: bring README, ROADMAP and CHANGELOG up to the landings of 2026-10-27

README's code-page and DXF writer text named work in review that has landed
(#371, #380, #382, #384); jev_review.py prose ranked those sentences 0.72 to
0.96. ROADMAP's "where it stands" table was a 2026-10-21 snapshot whose
numbers, CI row and release row were all out of date; it now points to the
rendered numbers instead of repeating them. CHANGELOG gains #371, #380, #382,
#384, #385 and #404.

* docs(changelog): record the CI-only merge train (#408)

* docs: OBJECTS landed (#372): README, ROADMAP and CHANGELOG

* docs: LAYOUT, GROUP and MLINESTYLE (#373), the class map writer (#405) and the double-byte round trip (#414) landed

* docs: VISUALSTYLE landed (#413)

* docs(readme): name the skipped types still in review without per-type counts; MATERIAL landed (#325)

* docs(readme): point to the open decoder PRs instead of a list that each landing makes stale
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.

write_class_map is not a byte-exact AutoCAD emitter (size_in_bits field lacks +32; string-stream trailer omitted)

1 participant