Skip to content

feat(objects): dispatch the non-entity objects and read their TV fields from the R2007+ string stream - #36

Merged
DrunkOnJava merged 1 commit into
mainfrom
feat/object-decoders-dispatch
Aug 30, 2026
Merged

DrunkOnJava merged 1 commit into
mainfrom
feat/object-decoders-dispatch

Conversation

@DrunkOnJava

@DrunkOnJava DrunkOnJava commented Aug 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

Two things were wrong with the non-entity objects, and they compounded:

  1. They were unreachable. decode_from_raw short-circuited every object that is neither a drawing entity nor a symbol-table entry straight to Unhandled, so the 65 DICTIONARY records of sample_AC1032.dwg — and every XRECORD, *_CONTROL and ACDB_PLACEHOLDER in the corpus — counted as "skipped" no matter how good the decoder behind them was.
  2. The decoders read their TV fields inline, so on R2007+ they returned whatever bits sat at that offset and never errored.

Fixing (1) without (2) would have manufactured a coverage number out of garbage, so this PR does both, plus the third thing that turned out to be blocking every Custom(N) object: AcDb:Classes was being read from the wrong offset and returned an empty table on every real file.

Every decoder reached from the new dispatch path is self-validating: objects::modern::ObjectStream::finish requires the data fields to end exactly on the record's data-stream boundary, so a wrong field list produces DecodedEntity::Error, never a plausible-looking struct that inflates the ratio.

What landed

  • src/objects/modern.rs (new) — the object analogue of tables/modern.rs. One field list per decoder serves both layouts (read_tv takes from the string stream on R2007+, inline before that), and finish enforces the boundary:

    Release band Boundary
    R2010+, record with strings first bit of the string stream
    R2010+, record without strings handle-stream start minus the one B "strings present" trailer bit
    R2000-R2007 the RL object-data-size-in-bits from the object prologue
    R13/R14 unknown — no check
  • Dispatch — dispatch_object routes DICTIONARY, XRECORD, ACDB_PLACEHOLDER, ACAD_GROUP and the ten *_CONTROL owners; dispatch_object_class routes SCALE, DICTIONARYVAR, ACDBDICTIONARYWDFLT and IMAGEDEF by DXF class name. DispatchSummary semantics are unchanged — a new kind counts as decoded only when its decoder returns Ok, and a class whose item_class_id is 0x1F3 with no object decoder returns Unhandled rather than being fed to the entity preamble reader.

  • New decoders — objects::placeholder (ACDB_PLACEHOLDER), objects::dictionary_var (DICTIONARYVAR).

  • Issue LIGHT, GEODATA, IMAGEDEF read TV fields inline on R2007+; LIGHT's private read_tv ignores the version entirely (always 8-bit) #32 — LIGHT's private read_tv ignored its version argument and always read 8-bit inline; LIGHT exists only from R2007, the release that moved TV into the string stream, so it was doubly wrong. LIGHT, GEODATA (five TVs, one a full WKT/PROJ string) and IMAGEDEF now read through the split stream, each self-validating.

  • examples/probe_object_layout.rs (new) — the instrument behind every "measured" claim below. It prints, per record, the bit budget from the end of the common object data to the data-stream boundary under both candidate prefixes, the raw bits, and the strings the string stream holds.

  • examples/coverage_report.rs now also prints an unhandled histogram by object kind, so the next-largest lever is visible from the report.

Measured coverage

cargo run --release --example coverage_report -- /Users/griffin/Developer/re/dwg-recon-2026-04-19/samples (19 files, read-only archive):

Version Before (dec / skip / err / rate) After (dec / skip / err / rate)
R14 / R2000 / R2007 × 9 n/a (no handle map) n/a (no handle map)
R2004 (AC1018) × 3 75 / 522 / 0 / 12.6 % 489 / 108 / 0 / 81.9 %
R2010 (AC1024) × 3 69 / 474 / 0 / 12.7 % 438 / 105 / 0 / 80.7 %
R2013 (AC1027) × 3 69 / 327 / 0 / 17.4 % 291 / 105 / 0 / 73.5 %
R2018 (AC1032) × 1 384 / 337 / 24 / 51.5 % 488 / 231 / 26 / 65.5 %
Aggregate 597 / 1660 / 24 / 26.2 % 1706 / 549 / 26 / 74.8 %

The two extra errors on the R2018 sample are the second STYLE_CONTROL and the second DIMSTYLE_CONTROL record, whose bytes are not a control object at all (they were previously counted as "skipped" without anything having tried to read them). Every other file still decodes with zero errors.

Verification on sample_AC1032.dwg

cargo run --release --example dump_decoded_entities -- .../sample_AC1032.dwg:

[0] BLOCK_CONTROL  entries=26
[8] DIMSTYLE_CONTROL  entries=6 trailing_rc=4
[9] DICTIONARY  entries=23 cloning=1 hard_owner=false keys=["ACAD_ASSOCPERSSUBENTMANAGER", "ACAD_CIP_PREVIOUS_PRODUCT_INFO", "ACAD_COLOR", "ACAD_DETAILVIEWSTYLE", "ACAD_GROUP", …]
[10] DICTIONARY  entries=2 cloning=1 hard_owner=false keys=["*A1", "*A2"]
[12] ACDB_PLACEHOLDER  reactors=1
[19] DICTIONARY  entries=1 cloning=1 hard_owner=false keys=["Standard"]

and on arc_2013.dwg, where the class map now resolves:

[13] DICTIONARY  entries=3 keys=["ByBlock", "ByLayer", "Global"]
[15] DICTIONARY  entries=6 keys=["BUMPTILE", "DIFFUSETILE", "OPACITYTILE", "REFLECTIONTILE", "REFRACTIONTILE", "SPECULARTILE"]
[64] SCALE  name="1:1" paper=1 drawing=1 unit_scale=true
[65] SCALE  name="1:2" paper=1 drawing=2 unit_scale=false
[66] SCALE  name="1:4" paper=1 drawing=4 unit_scale=false

Real strings, and the SCALE ratios are semantically right (1:2 → paper 1 / drawing 2, and only 1:1 is flagged as the unit scale).

LAYOUT is not in that list on purpose. Its names are real strings in the string stream (audit_string_streams shows "Layout1", "Model", "ANSI_A_(8.50_x_11.00_Inches)"), but LAYOUT embeds the whole PLOTSETTINGS field list ahead of them and this crate has not matched that list against real bytes yet, so it cannot satisfy the boundary check. Rather than guess, it stays Unhandled and the offsets are documented — see the follow-ups below.

Measurements

Every layout change below is read off bytes in the read-only sample archive, not inferred from the spec alone.

The common object data (§19.4.2) keeps its BL num_reactors on R2007+. Three independent confirmations:

  • DICTIONARY's BL numitems decodes to the number of strings its string stream actually holds only with the BL consumed — 23 for the named-object dictionary, 4 for the layout dictionary, 2 for the group dictionary, 0 for the empty ones. Without it, numitems reads 0 on a dictionary that demonstrably has entries.
  • ACDB_PLACEHOLDER, whose body is provably empty, has exactly 15 bits from the end of its object header to its handle stream on arc_2013.dwg — BS 0 (2) + BL 1 (10) + B + B + the one-bit §19.1 trailer. Without the BL, 2 bits are left over.
  • The nine one-field *_CONTROL records close on their BL numentries only with the reactor count consumed first.

DICTIONARY's entry names are a block, not name/handle pairs. arc_2004.dwg handle 16 (the linetype dictionary): its RL object-data-size ends 255 bits past the header, and BL numitems = 3, BS cloning = 1, RC hard-owner = 0 plus three TVs consumes exactly those bits, the first reading length 8 followed by the ASCII bytes of ByBlock and its NUL. Reading a handle after each name desynchronises on the second entry. Dictionary::entries: Vec<DictionaryEntry> therefore became Dictionary::keys: Vec<String>, whose index is the index of the matching item handle.

ACAD_SCALE is BS version, TV name, BD paper, BD drawing, B is_unit_scale — not TV, BD, BD, BS. arc_2004.dwg's 1:1 record spends its 49-bit budget exactly as BS 0 / TV of length 4 whose bytes are 31 3A 31 00 / BD 1.0 / BD 1.0 / B 1. arc_2013.dwg's 1:2 record spends 71 bits as BS 0 / BD 1.0 / a full 66-bit double whose LE bytes are 00 00 00 00 00 00 00 40 (2.0) / B 0.

ACAD_GROUP is TV, BS, BS, BL — not two B flags. Both GROUP records of sample_AC1032.dwg leave exactly 30 bits for three 10-bit fields reading 2, 1 and 6; the boolean reading ran the trailing BL off the end, which is why both failed to decode. The first field reading 2 rather than 0/1 is why it is surfaced as a raw i16: the width is established, that value's semantics are not, and there is no second file with a GROUP record to cross-check.

DIMSTYLE_CONTROL carries one extra RC — 8 bits past num_entries on arc_2004.dwg (value 1), arc_2013.dwg (1) and sample_AC1032.dwg (4), while the other nine controls close with nothing left over. Surfaced verbatim as Control::dimstyle_trailing_rc rather than named after a guess.

AcDb:Classes starts at byte 20 on R2004 and byte 28 on R2010+. The old parser started at byte 24 for every release and read max_class_number as a byte-aligned RL from bytes 20-24; it read class number 0 and desynchronised immediately, so the table was empty on every real file and no Custom(N) object could be dispatched at all.

File Bytes 16.. First bits after the header Decodes to
arc_2004.dwg 86 02 00 00 then 3F 40 40 00 … 00 + 1111110100000001 BS = 509
arc_2010.dwg 42 04 00 00 00 00 00 00 0A 22 00 00 then 3F 40 40 00 … same BS = 509
arc_2013.dwg D9 03 00 00 00 00 00 00 C3 1E 00 00 then 3F 00 40 00 … 00 + 1111110000000001 BS = 508
sample_AC1032.dwg 66 18 00 00 00 00 00 00 2A C3 00 00 then 09 40 80 00 … 00 + 0010010100000010 BS = 549

From R2007 the records themselves split the same way object records do — all the non-string fields first, then 3 × N strings as a block. On arc_2013.dwg that yields nine records numbered 500..=508, item class id 0x1F3 throughout, object counts 1/3/24/17/1/1/1/1/5 matching the object stream exactly, and 27 strings reading "ObjectDBX Classes", "AcDbDictionaryWithDefault", "ACDBDICTIONARYWDFLT", "ObjectDBX Classes", "AcDbMaterial", "MATERIAL", … in order. ClassMap::parse accepts a table only when the class numbers run consecutively from 500, so a mislocated stream reports no classes rather than a desynchronised list. write_class_map was rewritten as the true inverse and round-trips in both layouts.

Spec sections

ODA Open Design Specification for .dwg files v5.4.1: §19.1 (object data / R2007 split-stream layout and its trailer), §19.4.2 (common object data), §19.5.1-§19.5.10 (*_CONTROL), §19.5.19 (DICTIONARY), §19.5.26 (IMAGEDEF), §19.6.5 (XRECORD), §19.6.7 (ACAD_GROUP), §19.6.8 (ACAD_SCALE), §19.4.94 (LIGHT), §19.4.95 (GEODATA), §5.7 / §21.4.2 (AcDb:Classes), §5 Table 4 (object type codes), §2 (bit-code primitives).

Not determined — documented and stopped

Per the project's rule against guessing a layout that the bytes do not settle:

  • LAYOUT / PLOTSETTINGS (31 records). Five TVs per LAYOUT record, budgets of 808 / 400 / 424 bits on arc_2013.dwg; the numeric field list between them is not established. Left Unhandled.
  • ACAD_MLINESTYLE (10 records). The documented field list does not close on the 442-bit budget of arc_2013.dwg handle 96 — the first BD decodes to ~1e-226 rather than an angle. Left Unhandled.
  • MATERIAL / VISUALSTYLE / PROPERTYSET_DATA decode a documented prefix of their fields and stop, so they cannot satisfy the boundary check and are deliberately not dispatched. VISUALSTYLE alone is 216 records on the corpus — the biggest single remaining lever.
  • The R2018 AcDb:Classes record layout. sample_AC1032.dwg declares 50 classes but its ninth record does not decode with the field list that works on R2004/R2010/R2013, so the consecutiveness check rejects the table and all 194 Custom(N) records in that file — including its 33 SCALE records — stay Unhandled.
  • The R2007+ symbol-table common object prefix. tables/modern.rs omits the BL num_reactors this PR measured as present on every non-entity object, and compensates with a different flag order, so both readings land on the string stream and both satisfy the boundary check. Which one assigns the right values to the right fields is not determinable from the 16 bits an APPID record spends there. Names are unaffected (they come from the string stream either way), so this PR does not touch the table path.

Gate

cargo fmt --all --check                                            # clean
cargo clippy --all-targets --all-features -- -D warnings           # clean
cargo test --release                                               # 691 lib + 16 integration suites, 0 failed
cargo deny check all                                               # clean
RUSTDOCFLAGS='-D warnings' cargo doc --no-deps --all-features      # clean
cargo +1.85.0 test --no-run                                        # MSRV clean

tests/canonical_corpus.rs pins are unchanged — none of them touch the object structs.

Test-shape changes, each because the behaviour they pinned is the behaviour this PR fixes:

  • tests/code_table_agreement.rs — non_entity_codes_route_to_unhandled split into non_entity_codes_with_decoders_are_routed_not_unhandled (DICTIONARY, XRECORD, ACDB_PLACEHOLDER, GROUP, four controls) and non_entity_codes_without_decoders_stay_unhandled (MLINESTYLE, LAYOUT, LONG_TRANSACTION).
  • tests/dispatch_roundtrip.rs — the same split, plus a property test that the object decoders are total on random bytes.
  • src/classes.rs — the writer round-trip tests now assert the measured header shape and cover both record layouts.

Source-provenance declaration

Clean-room declaration. I certify that:

  1. All code and tests in this PR were written from scratch by me.
  2. I consulted only sources listed as allowed in CLEANROOM.md — the public ODA Open Design Specification for .dwg files v5.4.1, the public AutoCAD DXF group-code reference, and first-party byte inspection of the publicly-available sample DWGs in the local read-only recon archive (nextgis/dwg_samples lineage). In particular I did not consult Autodesk SDK source, ODA SDK (Drawings SDK / Teigha) source, LibreDWG source, decompilations of Autodesk or ODA binaries, or any copyleft-licensed DWG implementation code.
  3. Every layout claim in this PR is cited in the diff as a module-level comment naming the file, record handle and bit budget it was measured from; examples/probe_object_layout.rs reproduces every one of them.
  4. I have never had access to any of the forbidden sources in a commercial or NDA'd context.

Closes #33
Closes #32


Note

Medium Risk
Large bit-level decoder and dispatcher changes affect how untrusted DWG bytes are interpreted; boundary checks and tests mitigate silent mis-decode but R2018 class-table and undispatched types remain gaps.

Overview
Non-entity objects (DICTIONARY, XRECORD, *_CONTROL, GROUP, placeholders, custom SCALE/DICTIONARYVAR/IMAGEDEF) are now routed through dedicated decoders instead of always returning Unhandled, which is why corpus decode coverage jumps from ~26% to ~75% on the measured samples.

The PR adds objects::modern (parallel to tables::modern): one field list per object type, TV from the R2007+ string stream when applicable, and ObjectStream::finish so data fields must end exactly on the record boundary—wrong layouts become DecodedEntity::Error, not plausible garbage.

Wire-format fixes include DICTIONARY keys as a name block (handles live after the data stream), corrected ACAD_SCALE / ACAD_GROUP field lists, DIMSTYLE_CONTROL’s extra trailing RC, and LIGHT, GEODATA, and IMAGEDEF reading TV from the split stream.

ClassMap::parse was reading the class list from the wrong byte offset and treating max_class_number as an RL; it now matches R2004 vs R2010+ headers, R2007+ split class records, and a consecutiveness check—unlocking Custom(N) dispatch on older files (R2018 still blocked when the ninth class record does not decode).

Tooling/docs: new probe_object_layout, coverage_report unhandled histogram, object::body_cursor / data_end_bit, and updated architecture/changelog/status coverage tables.

Reviewed by Cursor Bugbot for commit 4f07e61. Configure here.

…ds from the R2007+ string stream

Non-entity objects were unreachable from `decode_from_raw` — every
DICTIONARY, XRECORD, `*_CONTROL` and ACDB_PLACEHOLDER in the corpus was
short-circuited to `Unhandled` — and the ones that were reachable
directly read their `TV` fields inline, so they were silently wrong on
R2007+.

- New `src/objects/modern.rs`: one field list per decoder serves both
  layouts, and `ObjectStream::finish` asserts the data fields end
  exactly on the record's data-stream boundary.
- `entities::dispatch::dispatch_object` routes DICTIONARY, XRECORD,
  ACDB_PLACEHOLDER, ACAD_GROUP and the ten `*_CONTROL` owners;
  `dispatch_object_class` routes SCALE, DICTIONARYVAR,
  ACDBDICTIONARYWDFLT and IMAGEDEF by DXF class name.
- Measured corrections: the common object data keeps its `BL
  num_reactors` on R2007+; DICTIONARY's names are a block, not
  name/handle pairs; ACAD_SCALE is `BS, TV, BD, BD, B`; ACAD_GROUP is
  `TV, BS, BS, BL`; DIMSTYLE_CONTROL carries one extra `RC`.
- `ClassMap::parse` read the class list from the wrong offset and
  returned an empty table on every real file; fixed, with the R2007+
  split-record layout, and `write_class_map` rewritten as its inverse.
- LIGHT's version-blind private `read_tv` removed; LIGHT, GEODATA and
  IMAGEDEF now read through the split stream (#32).

Coverage on the 19-file corpus: 597 / 1660 / 24 / 26.2% before,
1706 / 549 / 26 / 74.8% after.

Closes #33
Closes #32
@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_4109857f-751e-4cf2-a53e-84e887a33418)

@DrunkOnJava
DrunkOnJava merged commit 006b165 into main Aug 30, 2026
20 checks passed
@DrunkOnJava
DrunkOnJava deleted the feat/object-decoders-dispatch branch August 30, 2026 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant