Skip to content

fix(class-map): the class record's version fields are BL, not BS (closes #37) - #41

Merged
DrunkOnJava merged 1 commit into
mainfrom
fix/r2018-class-map-drift
Aug 30, 2026
Merged

DrunkOnJava merged 1 commit into
mainfrom
fix/r2018-class-map-drift

Conversation

@DrunkOnJava

@DrunkOnJava DrunkOnJava commented Aug 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

sample_AC1032.dwg's AcDb:Classes list desynchronised at record 9, so all 194 Custom(N) objects on the file — its 33 SCALE records included — stayed Unhandled. The cause is a contradiction inside the ODA spec that the bytes settle.

The spec states the class record twice:

Field §10.2 (R18+) §5.8 (R2007)
max_class_number BS + RC 0x00 + RC 0x00 BL
dwg_version BS BL
maintenance_version BS BL

BS and BL are bit-identical on tag 10 (2 bits, zero) and tag 01 (10 bits, one byte), and differ only on tag 00, where BS takes an 18-bit RS and BL a 34-bit RL. BS max_class_number + two RC 0x00 is also 34 bits over the same four little-endian bytes as a tag-00 BL, so the header never discriminates. The §10.2 reading therefore survives on any file whose classes keep both version fields under 256 — which is every class on arc_2004, arc_2010 and arc_2013.

sample_AC1032.dwg is the first file that doesn't:

Class DXF name num_objects dwg_version maintenance_version record width
508 MLEADERSTYLE 2 33 329 113 bits
516 ACDBDETAILVIEWSTYLE 2 33 329 113 bits
520 WIPEOUT 1 33 329 113 bits
526 MULTILEADER 15 33 329 113 bits

Every other record on that file is 57-89 bits. Reading the 329 as a BS consumes 18 bits instead of 34, so record 8 ran 16 bits long, record 9 hit the reserved 11 BL tag, and the consecutiveness guard threw the whole table away.

The R2018 class-record layout, with evidence

[0..16]   sentinel
[16..20]  RL  size of the class data area, in bytes
[20..24]  RL  (R2010+) unknown, observed 0
[24..28]  RL  (R2010+) size of the class data area, in bits
-- bit stream: byte 20 on R2004, byte 28 on R2010+ --
BL max_class_number
B  unknown
-- then max_class_number - 500 + 1 records: --
BS class_number
BS version / proxy flags
TV app_name / cpp_class_name / dxf_class_name   (inline pre-R2007; string block from R2007)
B  was_a_proxy
BS item_class_id
BL num_objects
BL dwg_version           <-- BS in §10.2
BL maintenance_version   <-- BS in §10.2
BL unknown (0 on every file measured)
BL unknown (0 on every file measured)

1. The record count lands exactly on the string block. On R2007+ all 3 × N names live in one block after the last record, so a list that stops one bit early mis-pairs every name. With the corrected field list, reading exactly max_class_number - 500 + 1 records lands on the block's first bit and the names come out in order (ACDBDICTIONARYWDFLT, ACDBPLACEHOLDER, LAYOUT, … ACDBPERSSUBENTMANAGER on AC1032).

2. The §19.4.1 string-stream trailer agrees independently. Its terminating bit is set on all three files and its length word equals the measured span, including the two-word form on AC1032 (0xB2EC has bit 15 set, second word 1 → (0xB2EC & 0x7FFF) | (1 << 15) = 45804):

File records records end strings end trailer at size_in_bits − 32 length word
arc_2010.dwg 10 877 8665 8682 7788 = 8665 − 877
arc_2013.dwg 9 788 7826 7843 7038 = 7826 − 788
sample_AC1032.dwg 50 4093 49897 49930 45804 = 49897 − 4093

(bit offsets relative to the start of the bit stream.) On the pre-R2007 layout the equivalent check is the declared size: arc_2004.dwg's ten records consume 5161 of the 5168 bits its header declares, the remaining 7 being byte padding.

3. num_objects matches the object walk. The field is now carried on ClassDef; summed over the corpus against what all_objects() walks it agrees exactly on the high-count classes — VISUALSTYLE 240/240, SCALE 186/186, ACDBDETAILVIEWSTYLE 11/11, ACDBSECTIONVIEWSTYLE 11/11, MLEADERSTYLE 11/11, TABLESTYLE 10/10, BLOCKGRIPLOCATIONCOMPONENT 6/6. Where the two disagree (DICTIONARYVAR 104/72, MULTILEADER 15/10, LAYOUT 4/0) the shortfall is on the walk side — objects the handle-map walker doesn't reach — not the class table.

examples/probe_class_layout.rs prints all of this:

cargo run --release --example probe_class_layout -- samples/sample_AC1032.dwg

Measured coverage

cargo run --release --example coverage_report -- <19-file samples dir>:

Corpus slice Before After
R2004 (AC1018) × 3 489 / 108 / 0 / 81.9 % 489 / 108 / 0 / 81.9 %
R2010 (AC1024) × 3 438 / 105 / 0 / 80.7 % 438 / 105 / 0 / 80.7 %
R2013 (AC1027) × 3 291 / 105 / 0 / 73.5 % 291 / 105 / 0 / 73.5 %
R2018 (AC1032) × 1 488 / 231 / 26 / 65.5 % 533 / 166 / 46 / 71.5 %
Aggregate 1706 / 549 / 26 / 74.8 % 1751 / 484 / 46 / 76.8 %

65 R2018 records left skipped. 45 of them now decode (the 33 SCALE records among them). The other 20 now reach a decoder and fail: MULTILEADER (10), MESH (3), ACAD_TABLE (2), WIPEOUT, IMAGE, IMAGEDEF, ACDBDICTIONARYWDFLT, DICTIONARYVAR — almost all with Bit cursor exhausted: wanted 8 bits, 3 bits remain inside the common entity preamble, before any per-entity field is read. That is the R2007+ preamble/handle-stream gap already tracked under #103, now visible on five more classes. Those 20 were always broken; the missing class table was hiding them.

Also in this PR

  • ClassDef gained num_objects, dwg_version, maintenance_version; write_class_map round-trips them instead of writing zeros, so the writer is still the exact inverse of the parser — including the oversized-BL case, pinned by a new BitWriter-built test on both the inline (R2004) and split (R2018) layouts.
  • write_class_map's doc now states plainly where it is not a byte-exact AutoCAD emitter (the size-in-bits header and the string-stream trailer), since neither is read back.
  • examples/coverage_report.rs names custom classes in its unhandled histogram — VISUALSTYLE (custom class) rather than CUSTOM(502) (0x01F6). A class number is per-file, so the name is the only meaningful aggregate key; this also merged two entries that were the same class under different numbers on different files.

Spec sections

  • ODA Open Design Specification for .dwg files v5.4.1 §5.8 — AcDb:Classes for R2007+: RL size in bytes, RL total size in bits, BL maximum class number, B, class data, string stream data, B. Record tail BL / BL / BL / BL / BL. This is the description the bytes match.
  • §10.2 — AcDb:Classes for R18+: the BS + RC + RC header and BS version fields. Bit-identical on the observed value range; wrong on sample_AC1032.dwg.
  • §10.1 — R13-R15 layout (no max_class_number, no record tail), unchanged here and still unreachable in practice: those releases do not expose the section by name.
  • §19.4.1 — string-stream location, reused as the corroborating check.
  • §2.1 / §2.3 / §2.4 — B / BS / BL tag alphabets, which is what makes the two spec readings collide.

Source provenance

Clean-room declaration. I certify that:

  1. All code and tests in this PR were written from scratch for this repository.
  2. Sources consulted were limited to those CLEANROOM.md lists as allowed: the freely-redistributable ODA Open Design Specification for .dwg files v5.4.1 PDF (§5.8, §10.1, §10.2, §19.4.1, §2.x), and first-party byte inspection of the publicly-available sample DWGs in the local corpus (nextgis/dwg_samples lineage plus sample_AC1032.dwg). No Autodesk SDK source, no ODA SDK / Teigha source, no LibreDWG, no decompilation of any Autodesk or ODA binary, and no copyleft-licensed DWG implementation code.
  3. The spec sections this PR relies on are cited above and in the diff (src/classes.rs module docs, ARCHITECTURE.md §7e, examples/probe_class_layout.rs).
  4. I have never had access to any of the forbidden sources in a commercial or NDA'd context.

Gate

cargo fmt --all -- --check, cargo clippy --all-targets --all-features -- -D warnings, cargo test --release (693 lib + all integration suites), cargo deny check all, RUSTDOCFLAGS='-D warnings' cargo doc --no-deps --all-features, and cargo +1.85.0 test --no-run (MSRV) all pass. The canonical-corpus fixtures did not move: the class-map writer change only affects the record tail, which those fixtures do not pin.

Closes #37

https://claude.ai/code/session_01BaHKf1Rw5aJBGK5y3xmx7C


Note

Medium Risk
Changes bit-level layout for the class table and custom-type dispatch on R2004+; impact is mitigated by consecutiveness checks, round-trip tests, and a fixture for maintenance_version > 255.

Overview
Fixes R2018 AcDb:Classes parsing so custom object types dispatch by DXF name instead of falling through as anonymous Custom(N).

The ODA spec disagrees on whether max_class_number, dwg_version, and maintenance_version are BS or BL; the old parser followed §10.2. That matches §5.8 while values stay ≤255, but on sample_AC1032.dwg four classes use maintenance_version = 329, which misreads as BS and desynchronizes the table at record 9. The consecutiveness guard then drops the whole map, leaving 194 custom objects undispatchable. The parser and write_class_map now use BL for those fields (§5.8), and ClassDef carries num_objects, dwg_version, and maintenance_version with writer round-trip.

Adds examples/probe_class_layout.rs for bit-offset / string-trailer verification, and coverage_report labels unhandled custom types by DXF class name. Corpus aggregate decode rate moves 74.8% → 76.8% on the R2018 sample; error count rises because objects that were skipped now hit decoders and fail in the common entity preamble (#103), not because of a regression.

Reviewed by Cursor Bugbot for commit 01b6c84. Configure here.

The ODA v5.4.1 spec states the AcDb:Classes record twice and the two
statements disagree. Section 10.2 (R18+) gives `BS max_class_number` +
two `RC 0x00`, `BS dwg_version` and `BS maintenance_version`; section
5.8 (R2007) gives `BL` for all three. The bytes side with 5.8.

The two readings are bit-identical on the `10` (zero) and `01`
(one-byte) tags and differ only on tag `00`, where BS takes an 18-bit
RS and BL a 34-bit RL — so the 10.2 reading survives every file whose
classes keep both version fields under 256. sample_AC1032.dwg is the
first that does not: MLEADERSTYLE (508), ACDBDETAILVIEWSTYLE (516),
WIPEOUT (520) and MULTILEADER (526) record dwg_version 33 and
maintenance_version 329, making those records 113 bits wide where every
other record on the file is 57-89. Reading 329 as a BS lost 16 bits at
record 8, record 9 hit the reserved `11` BL tag, the consecutiveness
check discarded the table, and all 194 Custom(N) objects on the file
fell through to Unhandled.

Corroborated three ways: exactly max_class_number - 500 + 1 records
decode with consecutive class numbers on arc_2010 / arc_2013 /
sample_AC1032; the 3 x N strings that follow end precisely at the
section's string-stream trailer (length words 7788 / 7038 / 45804 bits,
each equal to the measured span); and the num_objects field now carried
on ClassDef matches the object walk exactly on the high-count classes
(VISUALSTYLE 240/240, SCALE 186/186, MLEADERSTYLE 11/11).

ClassDef gained num_objects, dwg_version and maintenance_version, and
write_class_map round-trips them instead of writing zeros, so the
writer stays the exact inverse including the oversized-BL case.
examples/probe_class_layout.rs is the reproducer; coverage_report.rs
now names custom classes in its unhandled histogram.

Measured on the 19-file local corpus:

  R2004 x 3       489 / 108 /  0 / 81.9%  (unchanged)
  R2010 x 3       438 / 105 /  0 / 80.7%  (unchanged)
  R2013 x 3       291 / 105 /  0 / 73.5%  (unchanged)
  R2018 x 1       488 / 231 / 26 / 65.5%  ->  533 / 166 / 46 / 71.5%
  Aggregate      1706 / 549 / 26 / 74.8%  -> 1751 / 484 / 46 / 76.8%

65 R2018 records left skipped: 45 now decode (its 33 SCALE among them)
and 20 now reach a decoder and fail in the R2007+ common entity
preamble tracked under #103. Those 20 were always broken; the missing
class table was hiding them.

Closes #37

Claude-Session: https://claude.ai/code/session_01BaHKf1Rw5aJBGK5y3xmx7C
@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_a3bd1aa9-4829-4b0f-8a28-59d54c1b8a3c)

@DrunkOnJava
DrunkOnJava merged commit 0914187 into main Aug 30, 2026
20 checks passed
@DrunkOnJava
DrunkOnJava deleted the fix/r2018-class-map-drift branch August 30, 2026 16:08
DrunkOnJava added a commit that referenced this pull request Aug 30, 2026
… BLL (#46)

The common entity preamble (§19.4.1) opens with the EED chain and a `B`
"graphics present" flag; when set, a size field and that many bytes of
proxy graphics follow. That size field is an `RL` up to R2007 and a
`BLL` (§2.4) from R2010 on. Reading it as an `RL` on R2018 produced
preview sizes in the millions of bytes for records a few hundred bytes
long and ran the byte-skip loop off the end of the record inside the
preamble — the 20 errors #41 newly surfaced on sample_AC1032.dwg.

The failures looked class-shaped because AutoCAD writes proxy graphics
for entities whose class it does not implement natively, so the flag is
set on exactly MULTILEADER / MESH / ACAD_TABLE / WIPEOUT / IMAGE and
clear on LINE / TEXT / MTEXT. The field list is identical either way.

`3B` is three raw bits, not a stop-at-zero prefix code: the old reading
could only produce {0, 2, 6, 7}, and the file requires a one-byte
length. IMAGE 0x662 and WIPEOUT 0x44D write `001` + 0x8C = 140 bytes,
MULTILEADER 0x66E writes `010` + 0x50 0x03 = 848, MESH 0x343 writes
9512 — and each lands the rest of the preamble on the same field values
every plain entity in the file carries (entmode 2, 0 reactors, colour
0x0100, linetype scale 1.0, invisibility 0, lineweight 0x1D).

Three body fixes fall out of the same two records:

- an IMAGE clip boundary of type 1 carries no vertex count; the two
  corners follow the boundary type directly. The `BL` read there
  consumed the first 34 bits of the -0.5 corner.
- WIPEOUT stores a raster-image record — the IMAGE and WIPEOUT are
  bit-identical for 175 bits and the IMAGE field list closes the
  WIPEOUT exactly on its data boundary. `wipeout::Wipeout` is now an
  alias for `image::Image`.
- a MESH face-list count counts `BL` entries, not faces, and edge
  endpoints are `BL`. Both real MESH records now decode with Euler
  characteristics 2 and 0. Three bits before the data boundary are
  documented rather than guessed.

Measured with examples/coverage_report.rs over the 19-file corpus:
aggregate 1751/484/46 = 76.8% -> 1755/487/39 = 76.9%; R2018
533/166/46 = 71.5% -> 537/169/39 = 72.1%.

Closes #42, closes #16
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.

AcDb:Classes: the R2018 record layout drifts at the ninth record, so all 194 Custom(N) objects on sample_AC1032.dwg stay Unhandled

1 participant