Repository navigation
feat(tables,entities): R2007+ string-stream decoders for the symbol table, TEXT and attributes - #23
Merged
Merged
Conversation
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.
Bugbot couldn't run - usage limit reachedBugbot 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) |
This was referenced Aug 30, 2026
7 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
R2007 and later store every
TV(variable text) field of an object in aseparate string stream between the data stream and the handle stream, even
though the record's field table still lists those
TVslots inline. Anydecoder that reads
TVfrom the data cursor gets random bits — which is whatSTYLE,DIMSTYLE,VPORT,TEXT,ATTRIBandATTDEFwere doing, andwhat
LAYERwas doing silently.This PR adds
src/string_stream.rsto locate that stream,src/tables/modern.rsto 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::finishrejects 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>sample_AC1032.dwg(R2018)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:
src/string_stream.rs).(§20.4.56), UCS (§20.4.6), VIEW (§20.4.7), VPORT (§20.4.8), DIMSTYLE
(§20.4.5), APPID (§20.4.50).
CMCcolour; §2.1 —4BITS.Findings measured from bytes (all recorded in
ARCHITECTURE.md§7a)payload_bits - handle_stream_bitslands 8 or 16 bits short. The rule is
payload_bits - handle_stream_bits + mc_field_bits, i.e. corrected by thewidth of the leading
MChandle-stream-size field. Verified on 59 objectsacross 11 object types in
sample_AC1032.dwg(R2018),line_2013.dwg(R2013) and
arc_2010.dwg(R2010) with zero deviation. Reproduce withthe new
examples/probe_string_stream.rs, which printsdelta_vs_predictedper object.B 64-flag,B xdep,BS xrefindex+1— not
B, BS, B. Only that order keeps theBSon a 2-bit10code acrossevery STYLE record; the alternative reports xref index 83 for
Standard.CMCcolour form —BSindex +BLtrue colour +RCbyte —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
BLtop byte selects the interpretation:
0xC3= ACI index in the low byte,0xC2= literal RGB (VIEW's ambient reads0xC2333333= RGB 51/51/51).RC. The fourDIMSTYLE records in
sample_AC1032.dwgwith the bit clear parse with a4-bit object prefix; the two with it set (
ISO-25,custom_dim_style) needexactly 8 more bits.
heightis a rawRD, not aBD. Every TEXT in the R2018 samplecarries
DataFlags 0xFF, leaving flag byte +2RDinsertion +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.0only asa 64-bit
RDat bit 220, with noBDtype code in front of it.valuesbits:0x01frozen,0x02off,0x04frozen-in-new,0x08locked,0x10plot, bits 5-9 lineweight index. The R2018 sample namesits 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), andDefpoints(non-plotting, as it must be).ISO-25DIMSTYLE decodes to the published ISO-25 defaults across the whole~70-variable body, down to
dimaltmzf/dimmzf100.0 and the trailingdimlwd/dimlweof -2 — an independent check that the field sequence is right,not just self-consistent.
Not done — stopped rather than guessed
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.
record between the TEXT body and the tag —
MULTI_LINE_ATTcarries threestrings where a single-line ATTRIB carries two. Not modelled; the two records
report their misalignment (
delta -722/delta -466) instead of guessing.sample_AC1032.dwgobject #697 (handle0x1588, 515 bytes) reports typecode
0x41(VPORT) but carries no string stream at all. Left erroring.data-stream bits than the matching ATTRIB. Reading that
RCbefore the fieldlength 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)
All three AC1018 sample files error on 20 objects each, mostly
Bit cursor exhausted: wanted 8 bits, 4 bits remainin 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.
without erroring and returns a one-character garbage string (
"ѕ") for everyrecord. It needs the same split-stream port.
Layer::is_lockedtests bit0x04, but the DWGvaluesword puts lockedat
0x08(Layer_Lockreads0x03F8);0x04is 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.
src/object_type.rscode 67(
0x43) is APPID and UCS is0x3F. Both are ported here; the six remaining0x43errors are all in the R2004 files, and there were no0x43errors onthe R2018 sample before this PR either. Flagging in case the issue's premise
needs revisiting.
Closes #14
Closes #15
Test plan
cargo fmt --all -- --checkpassescargo clippy --all-targets --all-features -- -D warningspassescargo test --releasepasses (664 lib tests;tests/canonical_corpus.rsfrom 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-featurespassescargo deny check allpassescargo +1.85.0 test --no-runpasses (MSRV)pub fn)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
tests/r2013_entity_values.rs, verifiedagainst the sample corpus (they skip when it is not checked out)
Source-provenance declaration
Clean-room declaration. I certify that:
CLEANROOM.md. In particular Idid 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.
§20.4.x, §2.11, §2.1) or, where the spec was insufficient, as a measured
observation naming its evidence.
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
TVtext 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_streamlocates the stream via a measured trailer rule (payload_bits - handle_stream_bits + mc_field_bits) andtables/modernenforces 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_streamfor independent verification, expanded dump output for decoded table types, BitWriter unit tests per ported type, and real-DWG regressions onsample_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.