Skip to content

fix(object-types): R2004 object prologue — read the R2000-R2007 RL obj_size and the non-entity common object data - #27

Merged
DrunkOnJava merged 1 commit into
mainfrom
fix/r2004-header-and-mtext
Aug 30, 2026
Merged

DrunkOnJava merged 1 commit into
mainfrom
fix/r2004-header-and-mtext

Conversation

@DrunkOnJava

@DrunkOnJava DrunkOnJava commented Aug 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

The R2004 (AC1018) object prologue was missing a field. ODA v5.4.1 §19.1 puts an RL object data size in bits between the object type and the object handle for the whole R2000..R2007 band — R2010+ replaces it with the leading MC handle-stream size. The reader consumed it for Version::R2000 only, so every AC1018 record was 32 bits out of phase from the handle onwards. That is the whole of Bit cursor exhausted: wanted 8 bits, N bits remain on the three R2004 samples.

Fixing the prologue exposed three further pre-R2007 bugs in the same code path, all fixed here.

Spec reference

§19.1 — object record prologue.

 MS   size in bytes                       all versions (stripped by the walker)
 MC   handle-stream size in bits          R2010+ only
 OT   object type                         all versions
 RL   object DATA size in bits            R2000-R2007 only   <-- the missing field
 H    object handle                       all versions

Read through the new Version::has_object_size_field() predicate in both object::ObjectWalker::read_one_at_pos and entities::dispatch::position_cursor_at_entity_body, and surfaced as RawObject::obj_size_bits — the bit at which an object's data stream ends and its handle stream begins, i.e. the pre-R2010 analogue of the R2007+ string-stream start bit, and therefore the invariant a decoder can validate itself against.

Evidence from examples/dump_line_payload.rs (extended to be version-aware) on line_2004.dwg:

Reading handle preamble ends LINE body vs obj_size
without RL 0x18395D087600000 (nonsense) bit 276 cursor exhausted —
with RL = 347 0x83 bit 84 (50,50,0) -> (100,100,0) bit 347, delta 0

The recovered geometry matches what line_2013.dwg already asserts, and the record lands on its own obj_size with zero deviation.

§19.4.2 — common object data. Non-entity objects (symbol-table entries, dictionaries) carry a shorter prefix than the §19.4.1 entity preamble — no graphics block, no entity mode, but the same EED chain, BL reactor count and xdictionary-missing flag. The pre-R2007 table decoders were starting at the entry name with the cursor still parked on the EED chain. common_entity::read_common_object_data now consumes it.

examples/probe_r2004_object_prefix.rs (new) prints every candidate prefix side by side for a given type code. On line_2004.dwg only EED + BL + B yields readable names:

prefix LAYER STYLE APPID LTYPE
nothing "" "" "" ""
EED "" "" "" ""
EED + B <truncated> <TV len 66> <TV len 65> <TV len 66>
EED + BL "" "" "" ""
EED + BL + B 0 Standard ACAD ByBlock

(plus Annotative, AcadAnnotative, ACAD_NAV_VCDISPLAY, ACAD_EXEMPT_FROM_CAD_STANDARDS, ByLayer, Continuous, *Model_Space, *Paper_Space on the rest of the records.)

§20.4.56 — STYLE. The legacy inline decoder read an RC where the field table has two B bits (shape file, vertical), overrunning each record by exactly the 6 extra bits. The R2007+ split-stream decoder already had this right.

§20.4.8 — VPORT. The legacy inline decoder followed an abbreviated field list (2 × BD view centre instead of 2RD, BS view mode instead of 4BITS, and no UCS / grid tail at all). It now reads the same field table as the already-verified R2007+ split-stream decoder, minus the R2007+ lighting/ambient-colour block and the two trailing grid BS words. That is the only list which consumes the *Active record's 1127 body bits exactly (obj_size = 1240, common object data ends at bit 113), and it yields sane values: height 288.07, width 585.69 with centre x = 292.84 = 585.69/2 — the same "second BD is the view width, not an aspect ratio" relation the R2013 record was measured against.

Measured coverage — before / after

cargo run --release --example coverage_report -- <samples> over the 19-file corpus:

Version Files Before (dec / err / rate) After (dec / err / rate)
R14 / R2000 / R2007 9 n/a (no handle map) n/a (no handle map)
R2004 (AC1018) 3 15 / 60 / 2.5 % 75 / 0 / 12.6 %
R2010 (AC1024) 3 69 / 0 / 12.7 % 69 / 0 / 12.7 %
R2013 (AC1027) 3 69 / 0 / 17.4 % 69 / 0 / 17.4 %
R2018 (AC1032) 1 374 / 34 / 50.2 % 374 / 34 / 50.2 %
Aggregate 19 527 / 94 / 23.1 % 587 / 34 / 25.7 %

Per-file, each R2004 sample goes from 5 / 174 / 20 / 2.5 % to 25 / 174 / 0 / 12.6 %. All 34 remaining errors in the corpus are now in the single R2018 file; every R2004, R2010 and R2013 sample decodes with zero errors.

Table entries on line_2004.dwg also stop returning silent garbage:

before: LAYER name="" flags=0x0160 lineweight=60 color=256
        DIMSTYLE name="R\u{1}\u{fffd}F\u{fffd}\u{1}(\u{18}-DT\u{fffd}"
        STYLE  -> error
        VPORT  -> error
after:  LAYER name="0" flags=0x03F0 plot=true
        DIMSTYLE name="ISO-25" / name="Annotative"
        STYLE name="Standard" font="txt" width=1.0 last_h=2.5
        VPORT name="*Active" h=288.07 width=585.69 centre=(292.84,148.5) lens=50
        LTYPE name="ByBlock" / "ByLayer" / "Continuous"
        BLOCK_RECORD name="*Model_Space" / "*Paper_Space"
        LINE start=(50,50,0) end=(100,100,0)

Canonical-corpus fixture change

tests/canonical_corpus.rs pins decoded field values on the synthetic fixtures. examples/build_fixtures.rs was writing the AC1018 record without the RL, i.e. encoding the same bug the reader had, so the fixture and the reader agreed on a wrong layout. The generator now emits the field for the R2000..R2007 band (two-pass: the field is fixed width but its value depends on the body length, so the payload is built once with a placeholder to measure and once with the measured value).

Consequence: tests/fixtures/canonical/synthetic_2004.dwg changed. synthetic_2010/2013/2018.dwg are byte-identical, and none of the value pins in tests/canonical_corpus.rs moved — the fixture still decodes to the same five entities with the same coordinates.

Test plan

  • cargo fmt --all -- --check
  • cargo clippy --all-targets --all-features -- -D warnings
  • cargo test --release — 666 lib tests + all integration suites pass (was 664; +2 new)
  • cargo deny check all
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features
  • cargo +1.85.0 test --no-run (MSRV, no let-chains)
  • Coverage-report numbers attached above

New tests:

  • entities::dispatch::tests::r2004_line_record_decodes_and_ends_on_obj_size — builds a complete R2004 object record with BitWriter (OT, RL, H, §19.4.1 preamble, §19.4.20 LINE body), walks it through the public decode_from_raw, asserts the geometry, and asserts the negative case: skipping the RL must not produce a valid LINE.
  • entities::dispatch::tests::r2004_common_object_data_consumes_reactor_count — BitWriter-built §19.4.2 prefix followed by a TV, asserting the reactor count and that the name lands correctly.
  • The existing tables::style::tests and tables::vport::tests legacy round-trips were updated to the corrected field tables.

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.
  • No GPL-licensed DWG implementation source (LibreDWG, etc.) was consulted or imported.

This contribution is based on the ODA's freely-redistributable Open Design Specification for .dwg files (v5.4.1) and on first-party byte inspection of publicly-available sample DWG files, reproduced by the two probes under examples/. No permissively-licensed third-party comments were consulted, so no CLEANROOM.md disclosure entry is needed.

Closes #24


Note

Medium Risk
Changes bit alignment for every R2000–R2007 object record and alters table-entry decode entry points; mistakes would corrupt handles and geometry, but the change is spec-scoped and backed by corpus and unit tests.

Overview
Fixes R2004 (AC1018) object-record alignment by reading the §19.1 RL object data size in bits for the whole R2000..R2007 band (not only R2000), gated by Version::has_object_size_field() in the object walker and entity cursor positioning. The value is exposed as RawObject::obj_size_bits so decoders can verify where the data stream ends.

Pre-R2007 symbol-table dispatch now consumes §19.4.2 common object data (EED + reactor count + xdictionary flag) before inline table decoders; legacy STYLE and VPORT field layouts are corrected to match measured R2004 bytes.

Corpus impact: all three R2004 samples go from 60 errors to zero; aggregate decode success rises from 23.1% to 25.7%. Synthetic synthetic_2004.dwg is regenerated with a two-pass RL emit in build_fixtures, plus new probe examples and dispatch unit tests.

Reviewed by Cursor Bugbot for commit b4a3d1e. Configure here.

…y common object data

ODA v5.4.1 §19.1 puts an `RL` "object data size in bits" between the
object type and the object handle for the whole R2000..R2007 band;
R2010+ replaces it with the leading `MC` handle-stream size. The reader
consumed it for R2000 alone, leaving every AC1018 record 32 bits out of
phase from the handle onwards — ~20 `Bit cursor exhausted` errors per
R2004 sample.

Also read the §19.4.2 common object data (EED chain, `BL` reactor count,
xdictionary flag) before the pre-R2007 symbol-table decoders, which
started at the entry name with the cursor still on the EED chain; fix
the legacy STYLE decoder's `RC`-for-two-`B`-bits overread; and give the
legacy VPORT decoder the same field table as the already-verified
R2007+ split-stream decoder.

samples/ corpus: R2004 15 decoded / 60 errored / 2.5% -> 75 / 0 / 12.6%;
aggregate 527 / 94 / 23.1% -> 587 / 34 / 25.7%. All remaining errors are
in the single R2018 sample.

Closes #24
@cursor

cursor Bot commented Aug 30, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_6db688a4-8b88-44a5-87bf-4363328d8e55)

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.

R2004 (AC1018) common object header mis-parses: ~20 'Bit cursor exhausted' errors per R2004 sample, independent of the R2007+ string-stream work

1 participant