Skip to content

feat(tables,entities): R2007+ string-stream decoders for the symbol table, TEXT and attributes - #23

Merged
DrunkOnJava merged 7 commits into
mainfrom
feat/r2007-string-stream-tables
Aug 30, 2026
Merged

DrunkOnJava merged 7 commits into
mainfrom
feat/r2007-string-stream-tables

Conversation

@DrunkOnJava

@DrunkOnJava DrunkOnJava commented Aug 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

R2007 and later store every TV (variable text) field of an object in a
separate string stream between the data stream and the handle stream, even
though the record's field table still lists those TV slots inline. Any
decoder that reads TV from the data cursor gets random bits — which is what
STYLE, DIMSTYLE, VPORT, TEXT, ATTRIB and ATTDEF were doing, and
what LAYER was doing silently.

This PR adds src/string_stream.rs to locate that stream, src/tables/modern.rs
to share the plumbing, and ports LAYER, STYLE, UCS, VIEW, VPORT, APPID and
DIMSTYLE plus the TEXT / ATTRIB / ATTDEF entities onto it. The pre-R2007 inline
decoders are untouched.

The design point worth reviewing: every modern decoder validates itself.
Because the data-stream fields end exactly where the string stream begins,
SplitStream::finish rejects a record whose body does not land on that bit.
A mis-read layout therefore errors with the bit offset and deviation instead of
returning plausible-looking garbage. Every layout finding below was found that
way.

Measured coverage

cargo run --release --example coverage_report -- <19-file sample corpus>

Scope Before After
Aggregate decoded / skipped / errored 458 / 1660 / 163 527 / 1660 / 94
Aggregate ratio 20.1 % 23.1 %
sample_AC1032.dwg (R2018) 329 / 337 / 79 — 44.2 % 374 / 337 / 34 — 50.2 %
R2013 files (each of 3) 20 / 109 / 3 — 15.2 % 23 / 109 / 0 — 17.4 %
R2010 files (each of 3) 18 / 158 / 5 — 9.9 % 23 / 158 / 0 — 12.7 %
R2004 files (each of 3) 5 / 174 / 20 — 2.5 % 5 / 174 / 20 — 2.5 % (unchanged)

Per-type errors: TEXT 25 → 0, STYLE 21 → 6, DIMSTYLE 20 → 6, VPORT 11 → 4,
ATTDEF 5 → 1, ATTRIB 4 → 1. The R2010 and R2013 sample files now decode with
zero errors.
60 of the 94 remaining errors are the three R2004 (AC1018)
files, whose object-header alignment is a separate bug (see below).

Spec reference

ODA Open Design Specification for .dwg files v5.4.1:

  • §19.1 — object data / string / handle stream split (src/string_stream.rs).
  • §19.4.2, §19.4.3, §19.4.46 — ATTRIB, ATTDEF, TEXT.
  • §20.4.x table field tables — LAYER (§20.4.53), LTYPE (§19.5.3), STYLE
    (§20.4.56), UCS (§20.4.6), VIEW (§20.4.7), VPORT (§20.4.8), DIMSTYLE
    (§20.4.5), APPID (§20.4.50).
  • §2.11 — CMC colour; §2.1 — 4BITS.

Findings measured from bytes (all recorded in ARCHITECTURE.md §7a)

  1. The string-stream trailer end. The naive payload_bits - handle_stream_bits
    lands 8 or 16 bits short. The rule is
    payload_bits - handle_stream_bits + mc_field_bits, i.e. corrected by the
    width of the leading MC handle-stream-size field. Verified on 59 objects
    across 11 object types
    in sample_AC1032.dwg (R2018), line_2013.dwg
    (R2013) and arc_2010.dwg (R2010) with zero deviation. Reproduce with
    the new examples/probe_string_stream.rs, which prints
    delta_vs_predicted per object.
  2. Shared table-entry field order is B 64-flag, B xdep, BS xrefindex+1
    — not B, BS, B. Only that order keeps the BS on a 2-bit 10 code across
    every STYLE record; the alternative reports xref index 83 for Standard.
  3. A full CMC colour form — BS index + BL true colour + RC byte —
    is written unconditionally by VIEW's ambient colour, VPORT's, LAYER's and
    DIMSTYLE's four colours, even when the index carries no flag bits. The BL
    top byte selects the interpretation: 0xC3 = ACI index in the low byte,
    0xC2 = literal RGB (VIEW's ambient reads 0xC2333333 = RGB 51/51/51).
  4. The R2013+ "has AcDs binary data" bit is followed by an RC. The four
    DIMSTYLE records in sample_AC1032.dwg with the bit clear parse with a
    4-bit object prefix; the two with it set (ISO-25, custom_dim_style) need
    exactly 8 more bits.
  5. TEXT's height is a raw RD, not a BD. Every TEXT in the R2018 sample
    carries DataFlags 0xFF, leaving flag byte + 2RD insertion + BE + BT +
    height in bits 82..284 of object fix(objects): LAYOUT has no data-store block; those 16 bits are plot_layout_flags #236; the height reads a clean 1.0 only as
    a 64-bit RD at bit 220, with no BD type code in front of it.
  6. LAYER values bits: 0x01 frozen, 0x02 off, 0x04 frozen-in-new,
    0x08 locked, 0x10 plot, bits 5-9 lineweight index. The R2018 sample names
    its layers after their state and every one matches — Layer_Freeze,
    Layer_Off, Layer_vp_freeze, Layer_Lock, Layer_NoPlot,
    Layer_color_80 (ACI 80), Layer_lw_035 (index 9 = 0.35 mm),
    Layer_lw_050 (index 11 = 0.50 mm), Layer_true_color (no ACI), and
    Defpoints (non-plotting, as it must be).

ISO-25 DIMSTYLE decodes to the published ISO-25 defaults across the whole
~70-variable body, down to dimaltmzf/dimmzf 100.0 and the trailing
dimlwd/dimlwe of -2 — an independent check that the field sequence is right,
not just self-consistent.

Not done — stopped rather than guessed

  • UCS is unverified on real bytes. No UCS record exists anywhere in the
    sample corpus, so the ported layout has never met a real file. It is safe
    regardless: the string-stream invariant makes a wrong layout error rather
    than invent values, and the module says so.
  • Multi-line attributes. R2010 multi-line ATTRIB/ATTDEF embed a whole MTEXT
    record between the TEXT body and the tag — MULTI_LINE_ATT carries three
    strings where a single-line ATTRIB carries two. Not modelled; the two records
    report their misalignment (delta -722 / delta -466) instead of guessing.
  • sample_AC1032.dwg object #697 (handle 0x1588, 515 bytes) reports type
    code 0x41 (VPORT) but carries no string stream at all. Left erroring.
  • ATTDEF's extra byte. Every single-line ATTDEF needs exactly eight more
    data-stream bits than the matching ATTRIB. Reading that RC before the field
    length or after the lock-position bit consumes the same eight bits and yields
    identical surfaced fields, so the samples cannot distinguish the two; it is
    read last, and the doc comment says the position is not pinned.

Follow-ups this surfaced (not fixed here)

  • The R2004 (AC1018) object header is broken independently of this work.
    All three AC1018 sample files error on 20 objects each, mostly
    Bit cursor exhausted: wanted 8 bits, 4 bits remain in the common preamble,
    across LAYER / STYLE / APPID / VPORT / DIMSTYLE / BLOCK / ENDBLK / LINE. That
    is 60 of the 94 remaining errors and the single biggest remaining lever.
  • MTEXT is silently wrong on R2007+ in the same way LAYER was: it decodes
    without erroring and returns a one-character garbage string ("ѕ") for every
    record. It needs the same split-stream port.
  • Layer::is_locked tests bit 0x04, but the DWG values word puts locked
    at 0x08 (Layer_Lock reads 0x03F8); 0x04 is frozen-in-new-viewports.
    Fixing it changes pre-R2007 behaviour and an existing test, so it is left for
    a separate change and documented at the call site.
  • Issue R2007+ UCS table entries exhaust the bit cursor (6 errors on sample_AC1032.dwg) #15 names type code 67 as UCS. In src/object_type.rs code 67
    (0x43) is APPID and UCS is 0x3F. Both are ported here; the six remaining
    0x43 errors are all in the R2004 files, and there were no 0x43 errors on
    the R2018 sample before this PR either. Flagging in case the issue's premise
    needs revisiting.

Closes #14
Closes #15

Test plan

  • cargo fmt --all -- --check passes
  • cargo clippy --all-targets --all-features -- -D warnings passes
  • cargo test --release passes (664 lib tests; tests/canonical_corpus.rs
    from ci: non-vacuous coverage-smoke corpus + a criterion noise floor (task #94, refs #17) #19 stays green after merging origin/main)
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features passes
  • cargo deny check all passes
  • cargo +1.85.0 test --no-run passes (MSRV)
  • Docstring coverage still 100 % (496/496 pub fn)
  • A BitWriter-built unit test of the R2007+ layout for each ported record
    (STYLE, APPID, UCS, VIEW, VPORT, LAYER, DIMSTYLE, TEXT, ATTRIB, ATTDEF),
    plus a negative test that a one-bit-off STYLE body is rejected
  • Real-DWG regressions added to tests/r2013_entity_values.rs, verified
    against the sample corpus (they skip when it is not checked out)
  • Coverage-report numbers attached above — measured, never estimated

Source-provenance declaration

Clean-room declaration. I certify that:

  1. All code and tests in this PR were written from scratch.
  2. I consulted only sources listed as allowed in CLEANROOM.md. 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. No algorithm-description
    comments from any third-party project were read for this PR
    — every layout
    here came from the public ODA specification plus byte inspection of the
    publicly-available sample DWGs, and each finding cites the file, object and
    bit offsets that produced it.
  3. Every reference is cited in the diff as a doc comment (ODA §19.1, §19.4.x,
    §20.4.x, §2.11, §2.1) or, where the spec was insufficient, as a measured
    observation naming its evidence.
  4. I have never had access to any of the forbidden sources in a commercial or
    NDA'd context.

Note

Medium Risk
Large decoder surface area on binary DWG layouts; wrong field order would still error via alignment checks, but UCS is unverified on corpus files and edge cases (multi-line ATTRIB, string-less VPORT) remain errors.

Overview
Adds R2007+ split-stream decoding so TV text fields are read from a dedicated string stream (ODA §19.1) instead of the data stream, which was producing garbage or silent wrong values (notably LAYER names).

New string_stream locates the stream via a measured trailer rule (payload_bits - handle_stream_bits + mc_field_bits) and tables/modern enforces that data fields end exactly on the string-stream start bit—misaligned layouts fail with bit-offset errors rather than plausible junk.

Wired for R2007+: LAYER, STYLE, APPID, UCS, VIEW, VPORT, DIMSTYLE (plus existing LTYPE/BLOCK_HEADER paths), and entities TEXT, ATTRIB, ATTDEF. Dispatch routes modern table entries and split-stream entities; pre-R2007 inline decoders are unchanged.

Includes probe_string_stream for independent verification, expanded dump output for decoded table types, BitWriter unit tests per ported type, and real-DWG regressions on sample_AC1032.dwg. Docs and coverage metrics move aggregate decode success from 20.1% → 23.1%; R2010/R2013 samples hit zero decode errors. Multi-line attributes, one anomalous VPORT, and UCS on real bytes remain explicitly blocked or unverified.

Reviewed by Cursor Bugbot for commit 394a38d. Configure here.

Adds src/string_stream.rs: a clean-room implementation of the ODA
v5.4.1 §19.1 split-stream layout, where every TV field of an R2007+
object lives in a separate string stream between the data stream and
the handle stream. The trailer locator (present-flag, 16-bit size,
optional high word) is applied at

    trailer_end = payload_bits - handle_stream_bits + mc_field_bits

a correction measured over 59 objects spanning 12 object types in
sample_AC1032.dwg (R2018), line_2013.dwg (R2013) and arc_2010.dwg
(R2010) with zero deviation. examples/probe_string_stream.rs is the
reproducible probe.

src/tables/modern.rs adds the shared split-stream plumbing for symbol
table entries, including the invariant that the data-stream fields must
end exactly on the string-stream start bit -- a decoder that lands
anywhere else errors instead of returning plausible garbage.

STYLE (0x35) is ported onto it. Measured field order for R2007+ is
B 64-flag, B xdep, BS xrefindex+1, B shape-file, B vertical, then
BD/BD/BD/RC/BD, with name/font/bigfont in the string stream.

Measured coverage on the 19-file sample corpus:
  before  458 decoded / 163 errored / 20.1%   (sample_AC1032 44.2%)
  after   473 decoded / 148 errored / 20.7%   (sample_AC1032 45.0%)
STYLE errors: 21 -> 6 (the remaining 6 are R2004 files on the legacy
inline path).
Ports four more symbol-table entries onto the src/tables/modern.rs
split-stream plumbing, each validated by the invariant that the data
fields must land exactly on the string-stream start bit.

- APPID (0x43): B 64-flag, B xdep, BS xrefindex+1, RC unknown.
- UCS (0x3F): origin / x / y BD3 triples, elevation, ortho view type.
  No UCS record exists in the sample corpus, so this layout is
  unconfirmed on real bytes; the finish() invariant makes a wrong
  layout error rather than invent values. Documented in the module.
- VIEW (0x3D): full R2007+ field body including the lighting block.
  The ambient-colour CMC uses the full BS/BL/RC form even when the
  index carries no flag bits -- measured on the view_custom record of
  sample_AC1032.dwg (RGB 0x333333).
- VPORT (0x41): full R2007+ field body. The 23 bits after the screen
  rectangle are B UCSFOLLOW / BS circle-zoom / B fast-zoom / BB
  UCSICON / B grid-on, and the trailing BS words read 0, 3, 5. All
  three of sample_AC1032.dwg, line_2013.dwg and arc_2010.dwg agree
  bit-for-bit on those flags and now decode with plausible values
  (lower-left (0,0), upper-right (1,1), lens 50.0, grid/snap spacing
  10.0 or 0.5).

Also adds read_cmc_full and read_4bits to tables::modern, and prints
STYLE / APPID / UCS / VIEW / VPORT / DIMSTYLE / LAYER field values in
examples/dump_decoded_entities.

Measured coverage on the 19-file sample corpus:
  before  473 decoded / 148 errored / 20.7%   (sample_AC1032 45.0%)
  after   481 decoded / 140 errored / 21.1%   (sample_AC1032 45.2%)
LAYER was not in the error histogram because it never errored -- it
silently produced garbage: every R2007+ layer name decoded as
"\u{252}v\0\0..." and the flags/lineweight/colour with it. It now
reads its name from the string stream and its state from the `values`
word, and the result checks out against a corpus that names layers
after their state: Layer_Freeze sets 0x01, Layer_Off 0x02,
Layer_vp_freeze 0x04, Layer_Lock 0x08, Layer_NoPlot and Defpoints
clear the plot bit 0x10, Layer_lw_035 reads lineweight index 9 and
Layer_lw_050 index 11, Layer_color_80 reads ACI 80, and
Layer_true_color / Layer_color_book read no ACI at all.

DIMSTYLE now reads its full ~70-variable body -- necessary because the
data fields have to be consumed to the last bit for the string-stream
invariant to verify the record. The ISO-25 entry of line_2013.dwg
decodes to the published ISO-25 defaults end to end, down to
dimaltmzf/dimmzf 100.0 and the trailing dimlwd/dimlwe of -2.

Two further measured findings, both applied to tables::modern:

- The dimension-line/extension/text/text-fill colours use the same
  full BS/BL/RC CMC form already found in VIEW, and its BL top byte
  selects the interpretation: 0xC3 is an ACI index in the low byte,
  0xC2 a literal RGB.
- When the R2013+ "has AcDs binary data" bit is set, one RC follows
  before the record's own fields. The four DIMSTYLE records in
  sample_AC1032.dwg with the bit clear parse with a 4-bit prefix; the
  two with it set (ISO-25, custom_dim_style) need exactly 8 more bits.

Measured coverage on the 19-file sample corpus:
  before  481 decoded / 140 errored / 21.1%   (sample_AC1032 45.2%)
  after   495 decoded / 126 errored / 21.7%   (sample_AC1032 45.9%)
The R2010 and R2013 sample files now decode with zero errors.
Extends the split-stream path from symbol tables to entities. The
dispatcher now routes TEXT (0x01), ATTRIB (0x02) and ATTDEF (0x03)
through decoders that consume the common entity preamble, read the
field body from the data stream, verify it lands exactly on the string
stream, and take the strings from there.

Two measured findings:

- TEXT's height is a raw RD, not a BD. Every TEXT in
  sample_AC1032.dwg carries DataFlags 0xFF, leaving flag byte +
  2RD insertion + BE + BT + height in bits 82..284 of object #236;
  the height reads a clean 1.0 only as a 64-bit RD at bit 220, with
  no BD type code in front of it. All 25 TEXT records now decode with
  their real string ("Hello this is a single line text") and
  insertion points that step in y as the drawing shows.
- ATTDEF needs exactly eight more data-stream bits than ATTRIB. The
  extra RC is read last; the samples cannot distinguish that from
  reading it first, and the doc comment says so.

Single-line attributes decode their value, tag and prompt
(ATTINFO / PRESET_ATT / VERIFY_ATT / CONSTANT_ATT all check out
against the sample's names). Multi-line attributes embed a whole
MTEXT record between the TEXT body and the tag; that is not modelled,
so the two multi-line records report the misalignment instead of
guessing.

Measured coverage on the 19-file sample corpus:
  before  495 decoded / 126 errored / 21.7%   (sample_AC1032 45.9%)
  after   527 decoded /  94 errored / 23.1%   (sample_AC1032 50.2%)
- ARCHITECTURE.md gains section 7a with the split-stream layout, the
  measured trailer-end rule and its evidence table, the self-validating
  decode invariant, and the six field-layout findings that fell out of
  it (table-entry field order, the full CMC form and its 0xC2/0xC3
  discriminator, the AcDs binary-data RC, TEXT's RD height, and the
  LAYER values bits).
- README.md and STATUS.md coverage numbers re-measured, never
  estimated: 458/1660/163 (20.1%) -> 527/1660/94 (23.1%);
  sample_AC1032.dwg 329/337/79 (44.2%) -> 374/337/34 (50.2%).
- CHANGELOG.md Unreleased entry with the before/after table and the
  blocked/unverified items.
…ders

Pins the values recovered from sample_AC1032.dwg: STYLE names + font
files (including the unnamed ltypeshp.shx shape-file style), LAYER
state read back from layers the sample names after their state,
ISO-25 DIMSTYLE variables and the AcDs-binary-data custom_dim_style,
the *Active VPORT screen rectangle and grid/snap block, the TEXT
population's authored string and positive RD heights, and single-line
ATTRIB / ATTDEF value-tag-prompt ordering.

Follows the existing convention in this file: each test skips when the
sample corpus is not checked out beside the repo.
@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_8600fe7b-0c20-455d-a27e-774ee8b18c30)

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