Skip to content

fix(entities): the R2010+ entity graphics-preview block is sized by a BLL, not an RL - #46

Merged
DrunkOnJava merged 1 commit into
mainfrom
fix/custom-class-entity-preamble
Aug 30, 2026
Merged

DrunkOnJava merged 1 commit into
mainfrom
fix/custom-class-entity-preamble

Conversation

@DrunkOnJava

@DrunkOnJava DrunkOnJava commented Aug 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

The R2007+ common entity preamble did not diverge for custom-class entities. It diverged for entities that carry a graphics-preview block, and on R2018 that block's size field is a BLL (§2.4), not the RL used up to R2007.

AutoCAD writes proxy graphics for entities whose class it does not implement natively, so the graphics present flag (§19.4.1) is set on exactly MULTILEADER / MESH / ACAD_TABLE / WIPEOUT / IMAGE and clear on LINE / TEXT / MTEXT. That is the whole correlation with Custom(N). Nothing in the class record — was_a_proxy, is_an_entity, proxy flags, class-specific headers — changes the preamble field list.

Read as an RL, the size came back as 6,946,890 / 8,425,777 / 8,388,672 bytes for records a few hundred bytes long, and the byte-skip loop ran off the end of the record before any entity-specific field: Bit cursor exhausted: wanted 8 bits, 3 bits remain.

Bit evidence

3B is three raw bits, not a stop-at-zero prefix code. The old reading could produce only {0, 2, 6, 7} — a set with no encoding for "one byte", which the file requires.

record prefix bits size (bytes) preamble ends at bit data boundary
IMAGE 0x662 001 + 8C 140 1213 1920
WIPEOUT 0x44D 001 + 8C 140 1213 2186
MULTILEADER 0x66E 010 + 50 03 848 6885 8687
MULTILEADER 0x818 010 + 98 02 664 5413 7151
MESH 0x343 010 + 28 25 9512 76197 93985
MESH 0x380 010 + 08 24 9224 73893 92597

The check is what follows the block. Every one of those records then reads entmode = 2, num_reactors = 0, no_xdictionary = true, colour 0x0100, linetype scale 1.0, all flag fields 0, invisibility = 0, lineweight = 0x1D — bit for bit the values every plain LINE / TEXT / MTEXT record in the same file carries. With the old RL reading not one of them reaches a plausible tail.

New probe examples/probe_entity_preamble.rs traces any record's preamble under both readings and prints the bits left to its data boundary.

Three body fixes that fall out of the same two records

  • An IMAGE clip boundary of type 1 (rectangle) carries no vertex count. The two corners follow the boundary type directly. The BL the decoder read there consumed the first 34 bits of the -0.5 corner of the IMAGE at 0x662, silently returning a wrong rectangle and running 33 bits past the record. Measured: corners (-0.5, -0.5) and (993.5, 964.5) on a 994 × 965 raster, bits 1663..1919 of a record ending at 1920. The polygon form (type 2) does carry the count — the WIPEOUT reads 4 there, then four RD2 vertices spanning 1673..2185 of a record ending at 2186.

  • WIPEOUT stores a raster-image record. The IMAGE and the WIPEOUT are bit-identical for their first 175 bits, and the IMAGE field list closes the WIPEOUT exactly on its boundary (insertion (271.921…, 3.988…, 0), u/v length 31.7077…, brightness/contrast/fade 50/50/0, polygon clip with 4 vertices). The previous WIPEOUT field list (BL clip_state, BS count, BD2 vertices, B show_clipped, three RCs) matched no part of those bytes — it was decoding the graphics-preview payload as geometry. entities::wipeout::Wipeout is now an alias for entities::image::Image; wipeout::decode takes a Version.

  • A MESH face-list count counts BL entries, not faces, and edge endpoints are BL, not BS.

    handle verts face-list entries faces edges creases V − E + F
    0x343 62 336 72 132 132 2
    0x380 64 320 64 128 128 0

    The Euler characteristics (2 = closed surface, 0 = one-handle torus) are the independent check. decode now rejects any face index or edge endpoint outside the vertex cage, and the crease array reads its own BL count. Three bits before the data boundary are left unread and documented, not guessed: both records hold 100 there, consistent with a trailing BL of 0 plus one padding bit, but two records that both encode zero cannot distinguish a BL from a BS from two spare bits.

MULTILEADER and ACAD_TABLE bodies are deliberately left alone: MULTILEADER's ten failures are now inside the MLEADER body (#31), and ACAD_TABLE has no decoder and reports as Unhandled with its class name.

Measured coverage (examples/coverage_report.rs, 19-file corpus)

Version Files Decoded Skipped Errored Rate
R2004 (AC1018) 3 489 → 489 108 → 108 0 → 0 81.9 % → 81.9 %
R2010 (AC1024) 3 438 → 438 105 → 105 0 → 0 80.7 % → 80.7 %
R2013 (AC1027) 3 291 → 291 105 → 105 0 → 0 73.5 % → 73.5 %
R2018 (AC1032) 1 533 → 537 166 → 169 46 → 39 71.5 % → 72.1 %
Aggregate 19 1751 → 1755 484 → 487 46 → 39 76.8 % → 76.9 %

All 20 preamble errors are gone. The R2018 error histogram is now MULTILEADER 10, HATCH 5, INSERT 3, LTYPE 3, DIMENSION_DIAMETER 2, SPLINE 2, long tail 14 — no Bit cursor exhausted inside a preamble except the one bogus MESH record described below.

#44 CUSTOM(727) — investigated, and it is not a type-code bug

The three records read tag = 01, RC = 0xE7, + 0x1F0 = 727. That is exactly what the bytes say and the documented R2010+ dispatch. They are not objects at all:

  • handle 0x1607 @ 433069 decodes a handle field with counter = 11 — an 11-byte handle value, which cannot exist;
  • handle 0x15E6 @ 1063953 decodes code 9, counter 0, value 0, and claims MS = 29,927 bytes with MC = 14,220 bits of handle stream;
  • two more handle-map entries show the same signature: 0x13E2 → CUSTOM(517) and 0x13E7 → CUSTOM(564), the latter claiming MC = 280,720 bits.

The root cause is upstream of the type read: the handle map addresses offsets up to 1,289,590 while AcDb:AcDbObjects assembles to 1,192,851 bytes — its own declared decompressed size — and six handle-map offsets already fall past the end. The remaining garbage records are offsets that land inside the buffer but on the wrong bytes. That belongs with #43, and read_object_type is left untouched.

Also in this PR

examples/coverage_report.rs names the type in its error histogram, resolving custom classes through the file's AcDb:Classes table the way the unhandled histogram already did. Closes #16.

Spec sections

§2.1 (3B), §2.4 (BLL), §19.1 (object prologue / string stream), §19.4.1 (common entity data), §19.4.35 (AcDbRasterImage), §19.4.66 (AcDbSubDMesh).

Source provenance

Clean-room. Every layout in this PR was derived from the ODA Open Design Specification v5.4.1 plus a bit-level walk of sample_AC1032.dwg; no LibreDWG, ODA SDK, or Autodesk source was read, and no permissively-licensed source or comments were consulted for this change. CLEANROOM.md is unchanged because there is nothing new to disclose.

Gates

cargo fmt --all, cargo clippy --all-targets --all-features -- -D warnings, cargo test --release (16 suites green), cargo deny check all, RUSTDOCFLAGS='-D warnings' cargo doc --no-deps --all-features, cargo +1.85.0 test --no-run — all pass.

Test pins moved with the measurement, each with the reason in place: the 3B value set ({0, 2, 6, 7} → 0..=7) in bitcursor, bitwriter, tests/proptest_roundtrip.rs and tests/mutation_failure_modes.rs; the IMAGE rectangle fixture (dropped its vertex count); the MESH fixture (face-list sizing, BL edges, explicit crease count). tests/canonical_corpus.rs is untouched and green — the synthetic fixtures carry no graphics-preview block.

Closes #42, closes #16


Note

Medium Risk
Changes core bit primitives (3B/BLL) and the shared entity preamble used by every decoder on R2010+; wrong semantics would misalign all entities with graphics preview, though tests and real-DWG probes target the measured layouts.

Overview
Fixes #42 by reading the common entity preamble’s graphics-preview byte count as BLL on R2010+ (still RL through R2007) instead of always using RL, which blew up preview sizes and exhausted the bit cursor on proxy-graphic entities (IMAGE, WIPEOUT, MESH, MULTILEADER, ACAD_TABLE).

3B is now three raw bits (0..=7), not a stop-at-zero prefix, so read_bll / write_bll can encode one-byte lengths; writers emit the exact byte count.

Follow-on body fixes on R2018: IMAGE rectangle clips skip the vertex count; WIPEOUT decodes as Image; MESH treats the face list as a BL entry count, uses BL edge endpoints, and reads creases from their own BL count (with index bounds checks).

Adds probe_entity_preamble and updates coverage_report error histograms to resolve custom classes by DXF name. Corpus coverage moves from 46 → 39 errors on sample_AC1032.dwg (72.1%); remaining MULTILEADER failures are in the MLEADER body, not the preamble.

Reviewed by Cursor Bugbot for commit f1569a4. Configure here.

… BLL

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
@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_f961467a-458d-4dd4-83de-8e6077019ae7)

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