Skip to content

fix(entities): close the last 39 R2018 errors — MULTILEADER, HATCH and seven more field lists - #58

Merged
DrunkOnJava merged 4 commits into
mainfrom
fix/r2018-residual-entities
Aug 30, 2026
Merged

DrunkOnJava merged 4 commits into
mainfrom
fix/r2018-residual-entities

Conversation

@DrunkOnJava

@DrunkOnJava DrunkOnJava commented Aug 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

sample_AC1032.dwg now decodes with zero errors, and so does every other file in the 19-file corpus. The 39 records that still failed after #52 and #53 — MULTILEADER 15, HATCH 8, INSERT 4, LTYPE 3, SPLINE 2, and one each of 3DFACE, LWPOLYLINE, DIMENSION_ANG_2LN, ATTDEF, ATTRIB, IMAGEDEF and PDFUNDERLAY — are all decoded, each by a decoder that checks itself against the record's data-stream boundary.

That check is the point. Every R2007+ decoder here must land its data fields exactly on the first bit of the record's string stream, or — for a record that carries no strings — on the bit before the strings present trailer flag. A field list that is wrong anywhere lands somewhere else and errors. Zero errors therefore means zero known mis-reads, not zero unchecked reads. DIMENSION, MTEXT and HATCH are moved off the weaker <= string_start assertion onto exact equality as part of this.

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

Both columns measured on this machine: the "before" column on a detached worktree at fa7fcc8 (origin/main), the "after" column on this branch after merging it.

Corpus slice Before (fa7fcc8) After
R2004 (AC1018) × 3 498 / 99 / 0 / 83.4 % 498 / 99 / 0 / 83.4 %
R2010 (AC1024) × 3 519 / 24 / 0 / 95.6 % 519 / 24 / 0 / 95.6 %
R2013 (AC1027) × 3 372 / 24 / 0 / 93.9 % 372 / 24 / 0 / 93.9 %
R2018 (AC1032) × 1 716 / 87 / 39 / 85.0 % 755 / 87 / 0 / 89.7 %
Aggregate 2105 / 234 / 39 / 88.5 % 2144 / 234 / 0 / 90.2 %

The three older bands are untouched — no record of theirs changes bucket. What remains corpus-wide is the skipped column: 234 records of types with no field list matched against real bytes yet (VISUALSTYLE on R2004/R2007, MATERIAL, MLEADERSTYLE, MLINESTYLE, TABLESTYLE, the detail/section view styles).

The finding that unblocked four of the nine entity types

The strings present trailer bit is not an entity field. A record with no string stream still writes the one-bit flag that says so; it is the last bit before the handle stream, and it belongs to the trailer, not to the record's field list. So such a record's data fields end one bit before string_stream::data_section_end.

All 20 LWPOLYLINEs, the 3DFACE, the four INSERTs and the two SPLINEs of sample_AC1032.dwg carry no strings, and every single one ended its field list exactly one bit short of the old boundary — a uniform delta -1 across four unrelated field lists. Modelling that as a trailing B per entity would have put a fabricated field at the end of each. New string_stream::data_field_end returns the string-stream start bit when there is one and data_section_end - 1 when there is not; object::data_end_bit and tables::modern::open_entity both route through it, and two tests in string_stream.rs pin both branches.

Per-entity bit evidence

MULTILEADER — 15 records (§20.4.48 + §20.4.86)

§20.4.48 says it outright: "A significant portion (content block/text and leaders) of the multileader entity is stored in the MLeaderAnnotContext object (see paragraph 20.4.86), which is embedded into this object (stream)." So the record reads BS 270 version, then the whole context field list inline, then the style block.

Three things the spec's own tables settle, each confirmed by the boundary:

  • The inherited prefix is not written when embedded. §20.4.86 inherits AcDbAnnotScaleObjectContextData (§20.4.71) → AcDbObjectContextData (§20.4.89) — a BS version, two B flags and an H. None of it is present: on handle 0x7B8 the version reads 2 at bit 6885 and the next 10 bits are a BL of 1 (one leader root), after which the two B flags, a full-double connection point and a (1, 0, 0) direction all land. Reading the inherited prefix first puts the connection point 12 bits late and it decodes as a reserved BD pattern.
  • The -R2007 arrowhead / block-label block is genuinely absent on R2018. Nothing is lost: in R2010+ the arrowhead moved into the leader line (H 341 arrow symbol per line).
  • Every H consumes zero data-stream bits. MLEADER previously read three of them inline, which is what MLEADER reads three H handles inline on R2007+, misaligning before its TV fields (10 records of real text unread) #31 diagnosed.

Value corroboration, all read from the string stream at the slot the field list predicts: "MLeader text, hello!" (0x7B8), "MULTILEADER TEST" (0xB1A, 0xB27), "MULTILEADER\PTEST\P123" (0xB1B). Geometry corroborates too — 0xB1A reads landing distance 8, arrowhead size 4, content type 2 (MTEXT), block scale (1, 1, 1), override flags 279552 = 1<<10 | 1<<14 | 1<<18 (content type, text alignment, use-default-MTEXT), and top/bottom attachment 9/9.

Not established, and marked as such in the code: 17 bits sit between B 293 is annotative and the R2010+ BS 271 that §20.4.48 does not list — an MC (two bytes on all 15 records) then a B. The MC holds only 274, 530 and 786 across the whole file (18 + 128·{2,4,6}) and the B is true on every record. B + RS, or B + two RCs, consume the same bits on every record here, because each byte pair happens to have the continuation bit set on the first byte and clear on the second; MC is taken because it is the only one of the three that stays correct if a record ever carries a larger value. Which of the three Bs in that run is is annotative is also undecidable from this corpus — the decoder keeps §20.4.48's order and surfaces the other two as MLeader::undocumented_mc / undocumented_flag rather than inventing names.

HATCH — 8 records (§20.4.75)

The gradient block is unconditional on R2004+: §20.4.75 lists Is Gradient Fill as a BL (not the BS this crate read) and does not guard the rest of the record behind it. All eight HATCH records of the sample carry two strings in their string stream — "LINEAR" then the pattern name ("ANSI31", "AR-PARQ1", "SOLID,_O", "HSBDR3E0", "HVEGE100") — even though six of them have the flag clear. Returning early on a clear flag consumed one TV too few and shifted the entire boundary-path tree; that is the num_boundary_handles count 3254780159 in #30.

Four more corrections from the same section, each needed for a record to close:

Was Is
boundary handles read inline per path §20.4.75 puts them in "Common Entity Handle Data" — the data stream carries only the BL count (#30)
path points / seed points as BD pairs 2RD — raw doubles
BS style then straight to the pattern angle a BS pattern type sits between them
pattern angle / scale / double / definition lines always read written only if (!solidfill)
BS pixel_size always read a BD, written only when some path flag has bit 4 set

The pattern-line cap moves 100 → 4096: "HVEGE100" (handle 0xBC4) legitimately declares 232 definition lines, and closes exactly.

INSERT — 4 records (§20.4.9)

The BB data flags select one of four scale encodings, none of them the BD triple this crate read: 11 = (1,1,1) with nothing stored, 01 = x is 1.0 plus two DDs, 10 = x as an RD with y = z = x, 00 = x as an RD plus two DDs defaulting to x. The extrusion is a 3BD, not a BE. The BL owned-object count is written only when has_attribs is set.

handle data flags x scale rotation has_attribs count ends boundary
0x660 10 2.5 3.9269908 false — 405 405
0x661 10 2.5 3.9269908 false — 405 405
0xC9E 10 2.5 3.9269908 false — 405 405
0x79C 10 4.1911332 0 true 3 309 309

LTYPE — 3 records (§20.4.58)

The dash record is BD length, BS complex shapecode, RD x-offset, RD y-offset, BD scale, BD rotation, BS shapeflag — two raw doubles in the middle, and the shape flag after the geometry. The X 9 text area is 512 bytes on R2007+ and present only when some dash sets shapeflag & 0x02; all three failing records have it clear on every dash. The modern path also drops its heuristic string-scanning fallback and goes through tables::modern::open_table_entry like every other table entry.

SPLINE — 2 records (§20.4.40)

Degree is a BL, not a BD. And on R2013+ the fit/control branch is derived, not read: "the scenario flag becomes 1 if the knot parameter is Custom or has no fit data, otherwise 2".

handle stored scenario flags1 knot param branch ends boundary
0x433 1 0 15 (Custom) control — 8 knots 0,0,0,0,1,1,1,1, 4 control points 822 822
0x434 1 9 (method fit points + use knot param) 2 (Uniform) fit — tolerance 1e-10, 2 fit points 734 734

Both store 1; one is a fit spline.

LWPOLYLINE — 1 record (§20.4.85)

The flag bits are 0x001 normal, 0x002 thickness, 0x004 constant width, 0x008 elevation, 0x010 bulges, 0x020 widths, 0x400 vertex ids — and the constant width is read first, right after the flag, not after the counts. 19 of the sample's 20 records carry flag 0 or 0x200 and survive either reading; handle 0x4A6 carries flag 4, and under the old reading that BD was an extrusion three fields later, which put the vertex count on a reserved 11 pattern. 0x200 is not one of §20.4.85's presence bits and twelve records carry exactly it with no optional field at all; it is read as the closed flag, which is the conventional meaning — nothing in this corpus contradicts it and nothing proves it.

3DFACE — 1 record (§20.4.32)

Corners 2-4 are 3DD triples defaulting componentwise to the previous corner, not BD offsets added to it, and all four corners are always present — only the invisible-edge BS is conditional on has_no_flag_ind. Handle 0x322 closes at bit 503.

IMAGEDEF — 1 record (§20.4.81)

Imgsize and Pixelsize are 2RD. Read as BDs, handle 0x8C4 lands on a reserved 11 pattern.

DIMENSION (angular, 2-line) — 1 record (§20.4.27)

The 2RD 16-point comes first, before the four 3BD def points. Handle 0x516 closes at bit 1129 with that order and hits a reserved BD pattern without it.

PDFUNDERLAY — 1 record (no spec section)

The v5.4 spec has no UNDERLAY chapter, so this field list is measured from bytes alone. Handle 0xD07 closes exactly at bit 381 with 3BD normal, 3BD insertion point, BD rotation, 3BD scale, RC flags, RC contrast, RC fade, BS clip-vertex count — and every value it yields is self-consistent: unit normal (0, 0, 1), insertion (16.2869…, 15, 0), zero rotation, unit scale (1, 1, 1), flags 3 (clip on + underlay on), contrast 100, fade 0. The previous order put the scale on (0, 0, 1) and the normal on (1, 1, 1), then read the definition handle inline. Not established: the clip-vertex encoding — the record carries none, and the sibling IMAGE uses 2RD for its clip boundary.

Two issues closed on the way

#29 — MTEXT's 567 undecoded bits. §20.4.46 has an R2018+ block after the background flags: a B "is NOT annotative" and, when set, a version, a default flag, a handle slot, and a redundant copy of the record's own attachment point, x-axis, insertion point, rectangle and extents, then the column block. That is exactly why #29's probe found the tail repeating the record's own values. Handle 0x513 closes at bit 773 with is_not_annotative = true, version 4, attachment point 7, x-axis (1,0,0), insertion (741.02…, 14.70…, 0), extents 16.374 × 1.845, column type 0. MTEXT now asserts exact equality instead of <= string_start, and surfaces is_annotative, column_type, column_width, column_gutter, column_heights. The background block is also entered on the R2018 text-frame bit 0x10, per the same section.

#30 — HATCH boundary paths. Above.

#31 is the MLEADER handle-stream diagnosis; it is subsumed by the MULTILEADER work here but left for the maintainer to close alongside #57.

ATTRIB / ATTDEF (§20.4.4, §20.4.5)

An R2010+ RC version and an R2018+ RC attribute type sit between the shared TEXT body and the attribute's own fields. A multi-line attribute (type != 1) then embeds a whole MTEXT record "starting from the Entmode (entity mode)" — no length, no type code, no handle, no EED chain, no graphics block.

handle type body ends boundary strings
0x705 1 311 311 value, tag
0x79F 1 389 389 value, tag
0x7A0 1 311 311 value, tag
0x79D 2 (multi-line) 1111 1111 (empty) value, "my multi line text for the attrrib", "MULTI_LINE_ATT"
0x6F8 / 0x797 / 0x798 / 0x799 (ATTDEF) 1 319 / 397 / 319 / 319 same value, tag, prompt
0x796 (ATTDEF) 4 (multi-line) 871 871 value, MTEXT text, tag, prompt

ATTDEF is "Common ATTRIB Entity Data" plus its own RC version and a TV prompt. The lone unexplained RC the old ATTDEF field list carried after the lock bit was that version byte, and the two bytes §20.4.4 puts before the tag were missing entirely — three bytes cancelling to one, which is why the old list closed on the four single-line ATTDEFs and failed on the multi-line one.

#54 — measured, not settled

Exactly three entity records in the corpus set the R2013+ has AcDs binary data bit: 3DSOLID 0xD65 and 0xD6A, and REGION 0xD69. All three are ACIS pass-through decoders whose field lists this crate does not close, so they cannot arbitrate the marker width either — I probed 0xD69 under both the 0-bit and 16-bit readings and neither is decidable from a record whose tail is undecoded. None of the 39 records fixed in this PR sets the bit, so the reading did not matter for any of them, and common_entity.rs is unchanged in that respect. Settling #54 still needs a file with a flag-setting record of a type whose field list closes.

Probes added

  • examples/probe_decode_errors.rs — every erroring record with its handle, type label, body-start bit, data boundary and the bit budget between them. This is the granularity a field-list investigation needs and coverage_report deliberately does not print.
  • examples/probe_entity_field_list.rs — the entity-side twin of probe_field_list: walks a candidate token list against one entity record past its common preamble and prints the delta from the boundary. H and TV slots consume no data bits on R2007+; TEXTFIELDS, MTEXTFIELDS and ENTMODE composites walk the shared bodies; --at=<bit> starts the walk at an absolute offset so a tail hypothesis can be tested without re-describing everything in front of it.

Spec sections

§19.1 (split streams), §20.4.1 (common entity data), §20.4.4 (ATTRIB), §20.4.5 (ATTDEF), §20.4.9 (INSERT), §20.4.27 (DIMENSION angular 2-line), §20.4.32 (3DFACE), §20.4.40 (SPLINE), §20.4.46 (MTEXT), §20.4.48 (MLEADER), §20.4.58 (LTYPE), §20.4.71 / §20.4.86 / §20.4.89 (MLeaderAnnotContext and its bases), §20.4.75 (HATCH), §20.4.81 (IMAGEDEF), §20.4.85 (LWPLINE), §2.11 (CmColor).

Source provenance

Clean-room declaration. Every layout in this PR was derived from the Open Design Alliance's freely-redistributable Open Design Specification for .dwg files v5.4 plus a bit-level walk of sample_AC1032.dwg. No LibreDWG, ODA SDK / Teigha, or Autodesk SDK source was read; no decompilation was consulted; no permissively-licensed DWG implementation source or comments were consulted for this change. Where the spec is silent — the UNDERLAY field list, the 17 undocumented MULTILEADER bits, the LWPOLYLINE 0x200 bit, the UNDERLAY clip-vertex encoding — the code says so in place rather than guessing, and the PR text above repeats each one. CLEANROOM.md is unchanged because there is nothing new to disclose.

Gates

cargo fmt --all -- --check, cargo clippy --all-targets --all-features -- -D warnings, cargo test --release (722 lib tests + 16 integration suites, all green), cargo deny check all, RUSTDOCFLAGS='-D warnings' cargo doc --no-deps --all-features, cargo +1.85.0 test --no-run — all pass. Every new pub fn carries a /// line.

New BitWriter-built R2018 tests, one or more per fixed decoder: MULTILEADER round-trip + misalignment rejection, HATCH split-stream two-name test + clear-flag regression guard, INSERT for all four data-flag forms plus a boundary-checked ATTRIB-carrying record, SPLINE control/fit/derived-scenario/misalignment, 3DFACE triangle + quad, LTYPE name-and-description + dash record, MTEXT R2018 annotative block + dynamic columns + truncation rejection, ATTRIB multi-line embedded MTEXT, ATTDEF single-line, and two tests pinning both branches of string_stream::data_field_end.

Closes #57
Closes #30
Closes #29
Refs #31
Refs #54


Note

Medium Risk
Large, behavior-changing binary layout fixes across many public entity types and structs; risk is mitigated by exact boundary self-checks and expanded tests, but consumers depending on old field shapes or partial decode success may see different parsed values.

Overview
Eliminates all 39 remaining decode errors on sample_AC1032.dwg and brings the 19-file corpus to 0 errored records (2,144 decoded / 90.2%), by aligning R2007+ entity decoders with the real data-stream boundary.

Shared plumbing: string_stream::data_field_end treats the one-bit “strings present” trailer as outside the field list (string-less records end one bit early). Dispatch routes more types through exact boundary checks; H and TV slots consume no data-stream bits. read_entity_mode_onwards supports embedded MTEXT inside multi-line ATTRIB/ATTDEF.

Decoder fixes: MULTILEADER (embedded annot context, split-stream path, DecodedEntity::MLeader boxed). HATCH (unconditional gradient + BL flag, 2RD geometry, conditional pattern block, boundary handle count only). INSERT (BB scale encodings, 3BD extrusion, conditional owned count). Plus corrected field lists for SPLINE, LWPOLYLINE, 3DFACE, IMAGEDEF, DIMENSION (2-line angular), LTYPE, UNDERLAY, MTEXT R2018 tail, and ATTRIB/ATTDEF with optional embedded MTEXT.

Adds probe_decode_errors and probe_entity_field_list; updates ARCHITECTURE, CHANGELOG, README, and STATUS with measured coverage.

Reviewed by Cursor Bugbot for commit 606bb82. Configure here.

…tors restart per handle section

The AcDb:Handles delta run was decoded with two wrong rules, and their
combination is what #44 measured as a 96,739-byte address-space
shortfall against AcDb:AcDbObjects.

1. The handle delta is an UNSIGNED modular char; only the offset delta
   is signed. Reading it as signed steals bit 0x40 of the terminating
   byte as a negation flag, so every single-byte delta in 0x40..=0x7F
   decoded small and negative. On sample_AC1032.dwg the first
   divergence is entry 272, after which every handle ran 108 low.

2. Both accumulators restart at zero on every handle section. The
   first pair of a section is absolute. Carrying the totals across the
   boundary threw entries 656..841 (186 of 842) into a fabricated
   address space and produced the phantom offset 1,289,590.

The section assembly was never wrong: AcDb:AcDbObjects is 41 pages of
0x7400 decompressed bytes truncated to its declared 1,192,851, and the
trailing 24,685 bytes are the last page's zero padding. After the fix
the map's highest offset is 1,192,823.

Every record repeats its own handle, so the object stream verifies the
map against itself. Agreement goes from 272/842 to 842/842, walker
skips from 97 to 0, and the walked records cover 1,191,935 of 1,192,851
section bytes (916 unclaimed, longest run 4).

BitCursor::read_handle now rejects a counter above 8 with the new
Error::HandleCounterTooLarge, surfaced as a walker skip rather than a
decoder error so coverage ratios stay comparable.

Coverage over the local 19-file corpus: R2018 561/145/39/75.3% ->
712/91/39/84.6%; aggregate 1923/319/39/84.3% -> 2074/265/39/87.2%.

Closes #43
Closes #44
Closes #51

Claude-Session: https://claude.ai/code/session_01BaHKf1Rw5aJBGK5y3xmx7C
…tities

# Conflicts:
#	CHANGELOG.md
#	README.md
#	STATUS.md
…d seven more field lists

sample_AC1032.dwg now decodes with zero errors, and so does every other
file in the 19-file corpus: 2144 decoded / 234 skipped / 0 errored /
90.2% aggregate, up from 2105 / 234 / 39 / 88.5%.

- the `strings present` trailer bit is not an entity field, so a record
  with no string stream ends one bit before data_section_end
- object references consume no data-stream bits (MULTILEADER, HATCH,
  UNDERLAY read them inline)
- MULTILEADER embeds a whole MLeaderAnnotContext (SS20.4.48 + SS20.4.86)
- the HATCH gradient block is unconditional and its flag is a BL
- INSERT data flags, SPLINE derived scenario + BL degree, LWPOLYLINE
  flag bits, 3DFACE 3DD corners, IMAGEDEF 2RD, DIMENSION ang-2-line
  16-point first, LTYPE dash record, UNDERLAY field order
- MTEXT's R2018 redundant/column block and ATTRIB's embedded MTEXT

Closes #57, closes #30, closes #29
@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_cba9c309-41c2-42f9-bae2-b1e1c25b7f32)

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