Skip to content

Commit 85da335

Browse files
committed
dxf: read R12 and earlier binary DXF (1-byte group codes)
Binary DXF written before R13 stores each group code in one byte, with 255 escaping a 2-byte code. The reader only knew the R13+ layout, where codes are two bytes, and also treated 255 as an escape there. The layout is now told from the first tag; 255 is an escape only in the 1-byte layout and an ordinary code in R13+. Mirrors storytold/cadcraft#1499 as ported in storytold/craft-libs#847 (ebb40fa0c). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0178NDPxWtqKSqJ23Wwu3frF
1 parent 7e06c96 commit 85da335

4 files changed

Lines changed: 208 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ How to write it (see AGENTS.md):
1717
- Arabic, Hebrew, Persian, Urdu, Pashto and Dhivehi lay the interface out right to left: menus open to the left, window close buttons and scroll bars move to the left, and dialogs line up on the right. The drawing, numbers and what you type at the command line stay left to right.
1818

1919
### Improved
20+
- Binary DXF files from AutoCAD R12 and earlier now open.
2021
- The web version downloads the font for your language when you choose it, so Arabic, Hebrew, Persian, Urdu, Pashto, Hindi, Marathi, Nepali, Bengali, Telugu, Tamil, Thai, Lao, Georgian, Japanese and Korean now work in the browser; other languages download nothing, and each font downloads once.
2122
- The search box's hint ("Type a keyword or phrase") is now shown in every interface language instead of English.
2223
- The ribbon is now fully translated in Persian, Urdu, Pashto and Dhivehi, along with the other labels those languages still showed in English.

‎crates/dxf/src/lib.rs‎

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,20 +379,40 @@ impl<'a> Cur<'a> {
379379
}
380380
}
381381

382-
/// Binary DXF (R13+: 2-byte group codes).
382+
/// Binary DXF, the body after the sentinel: R13+ (2-byte group codes) or R12 and earlier (1-byte
383+
/// group codes, where 255 is followed by a 2-byte code).
383384
pub fn parse_binary(b: &[u8]) -> Result<Vec<Tag>> {
384385
parse_binary_with_limit(b, max_tags())
385386
}
386387

388+
/// Whether a binary DXF body uses the pre-R13 layout with 1-byte group codes, told from its first
389+
/// tag: R13+ starts with a 2-byte code (`00 00` for the usual `0 SECTION`, `E7 03` for a 999
390+
/// comment), the old layout with a 1-byte `00` followed by the string's first character, or with
391+
/// the 255 escape and a code that needs it (255 to 1071; an R13+ file whose first group is code 255
392+
/// has the string's first character there instead).
393+
fn legacy_binary_layout(b: &[u8]) -> bool {
394+
match b {
395+
[0, next, ..] => *next != 0,
396+
[255, lo, hi, ..] => (255..=1071).contains(&u16::from_le_bytes([*lo, *hi])),
397+
_ => false,
398+
}
399+
}
400+
387401
fn parse_binary_with_limit(b: &[u8], max_tags: usize) -> Result<Vec<Tag>> {
388402
let mut c = Cur { b, p: 0 };
389403
let mut text = codepage::BinaryText::default();
390404
let mut tags = Vec::new();
405+
let legacy = legacy_binary_layout(b);
391406
while c.p < b.len() {
392-
let mut code = i32::from(c.i16()?);
393-
if code == 255 {
394-
code = i32::from(c.i16()?);
395-
}
407+
// 255 escapes a 2-byte code only in the 1-byte layout; in R13+ it is an ordinary code.
408+
let code = if legacy {
409+
match c.u8()? {
410+
255 => i32::from(c.i16()?),
411+
x => i32::from(x),
412+
}
413+
} else {
414+
i32::from(c.i16()?)
415+
};
396416
let v = match kind_of(code) {
397417
Kind::Str => Value::Str(unescape_unicode(&text.string(code, c.cstr()?))),
398418
Kind::Real => Value::Real(c.f64()?),
@@ -747,6 +767,83 @@ mod tests {
747767
assert_eq!(tags[2], Tag::i(62, 5));
748768
}
749769

770+
/// A pre-R13 binary body: 1-byte group codes, 255 escaping a 2-byte code (1001 here).
771+
fn r12_binary() -> Vec<u8> {
772+
let mut b = BINARY_SENTINEL.to_vec();
773+
let push_str = |b: &mut Vec<u8>, code: u8, s: &str| {
774+
b.push(code);
775+
b.extend_from_slice(s.as_bytes());
776+
b.push(0);
777+
};
778+
push_str(&mut b, 0, "SECTION");
779+
push_str(&mut b, 2, "ENTITIES");
780+
push_str(&mut b, 0, "LINE");
781+
push_str(&mut b, 8, "RAILROAD");
782+
b.push(10);
783+
b.extend_from_slice(&3.25f64.to_le_bytes());
784+
b.push(62);
785+
b.extend_from_slice(&5i16.to_le_bytes());
786+
b.push(255);
787+
b.extend_from_slice(&1001i16.to_le_bytes());
788+
b.extend_from_slice(b"APP\0");
789+
push_str(&mut b, 0, "ENDSEC");
790+
push_str(&mut b, 0, "EOF");
791+
b
792+
}
793+
794+
#[test]
795+
fn r12_binary_parse() {
796+
let tags = parse(&r12_binary()).unwrap();
797+
assert_eq!(tags[0], Tag::s(0, "SECTION"));
798+
assert_eq!(tags[3], Tag::s(8, "RAILROAD"));
799+
assert_eq!(tags[4], Tag::f(10, 3.25));
800+
assert_eq!(tags[5], Tag::i(62, 5));
801+
assert_eq!(tags[6], Tag::s(1001, "APP"));
802+
assert_eq!(tags.last(), Some(&Tag::s(0, "EOF")));
803+
}
804+
805+
#[test]
806+
fn r13_binary_code_255_is_ordinary() {
807+
let mut b = BINARY_SENTINEL.to_vec();
808+
for (code, s) in [(0i16, "LINE"), (255, "x"), (0, "EOF")] {
809+
b.extend_from_slice(&code.to_le_bytes());
810+
b.extend_from_slice(s.as_bytes());
811+
b.push(0);
812+
}
813+
let tags = parse(&b).unwrap();
814+
assert_eq!(tags.get(1).map(|t| t.code), Some(255));
815+
assert_eq!(tags.len(), 3);
816+
}
817+
818+
#[test]
819+
fn binary_layout_detection() {
820+
assert!(legacy_binary_layout(b"\0SECTION\0"));
821+
assert!(legacy_binary_layout(&[255, 0xE7, 0x03]));
822+
assert!(!legacy_binary_layout(b"\0\0SECTION\0"));
823+
assert!(!legacy_binary_layout(&[0xE7, 0x03, b'x', 0]));
824+
assert!(!legacy_binary_layout(b""));
825+
assert!(!legacy_binary_layout(&[0]));
826+
// R13+ code 255 with the string "x": not the escape
827+
assert!(!legacy_binary_layout(&[255, 0, b'x', 0]));
828+
assert!(!legacy_binary_layout(&[255, 0xE7]));
829+
}
830+
831+
#[test]
832+
fn truncated_r12_binary_never_panics() {
833+
let b = r12_binary();
834+
for n in 0..b.len() {
835+
let _ = parse(b.get(..n).unwrap_or(&[]));
836+
}
837+
// Empty body, and a 255 escape cut off at the end of the file.
838+
assert!(parse(BINARY_SENTINEL).is_err());
839+
let mut cut = BINARY_SENTINEL.to_vec();
840+
cut.extend_from_slice(&[0, b'S', 0, 255, 0x10]);
841+
assert!(parse(&cut).is_err());
842+
let mut cut = BINARY_SENTINEL.to_vec();
843+
cut.extend_from_slice(&[0, b'S', 0, 255]);
844+
assert!(parse(&cut).is_err());
845+
}
846+
750847
#[test]
751848
fn malformed_inputs() {
752849
assert!(parse(b"").is_err());

‎crates/dxf/tests/r12_binary.rs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//! Binary DXF written by R12 and earlier (1-byte group codes, 255 escaping a 2-byte code) reads,
2+
//! and the R13+ layout (2-byte codes, 255 an ordinary code) reads as before. Ported from
3+
//! storytold/craft-libs#847 (storytold/cadcraft#1499).
4+
5+
use draftcraft_dxf::{Tag, parse, write_binary};
6+
7+
const SENTINEL: &[u8] = b"AutoCAD Binary DXF\r\n\x1a\0";
8+
9+
#[test]
10+
fn group_code_255_is_an_ordinary_code_in_binary_dxf() {
11+
// Code 255 used to be read as an R12 escape (`FF 00 <code>`), so a file with that code could
12+
// not be read back.
13+
let tags = vec![Tag::s(255, "x"), Tag::s(0, "EOF")];
14+
let back = parse(&write_binary(&tags, true)).unwrap();
15+
assert_eq!(back.len(), 2);
16+
assert_eq!((back[0].code, back[0].str()), (255, "x".to_string()));
17+
}
18+
19+
#[test]
20+
fn r13_binary_reads_unchanged() {
21+
let tags = vec![
22+
Tag::s(0, "SECTION"),
23+
Tag::s(2, "ENTITIES"),
24+
Tag::s(0, "LINE"),
25+
Tag::s(8, "0"),
26+
Tag::f(10, 1.5),
27+
Tag::i(62, 3),
28+
Tag::s(1001, "APP"),
29+
Tag::s(0, "ENDSEC"),
30+
Tag::s(0, "EOF"),
31+
];
32+
assert_eq!(parse(&write_binary(&tags, false)).unwrap(), tags);
33+
}
34+
35+
#[test]
36+
fn hostile_r12_binary_is_an_error_not_a_panic() {
37+
for body in [&[][..], &[0], &[255], &[255, 0xE9], &[0, b'L', 0, 255, 0xE9, 0x03], &[0, b'L', 0, 10, 1, 2]] {
38+
let mut b = SENTINEL.to_vec();
39+
b.extend_from_slice(body);
40+
assert!(parse(&b).is_err(), "{body:?}");
41+
}
42+
}

‎crates/io/src/tests.rs‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,69 @@ fn hostile_dxf_does_not_panic() {
410410
let _ = d.extents(&Space::Model);
411411
}
412412
}
413+
// R12 binary DXF (1-byte group codes): empty, cut inside a tag, a 255 escape at the end.
414+
let r12 = r12_binary_dxf();
415+
let mut hostile: Vec<Vec<u8>> = (R12_SENTINEL.len()..r12.len()).filter_map(|n| r12.get(..n).map(<[u8]>::to_vec)).collect();
416+
hostile.push([R12_SENTINEL, &[0, b'S', 0, 255]].concat());
417+
hostile.push([R12_SENTINEL, &[0, b'S', 0, 255, 0x10]].concat());
418+
for b in hostile {
419+
if let Ok(d) = read(&b, "x.dxf") {
420+
let _ = draftcraft_render::build(&d, &Space::Model, &draftcraft_render::Options::default());
421+
let _ = d.extents(&Space::Model);
422+
}
423+
}
424+
}
425+
426+
const R12_SENTINEL: &[u8] = b"AutoCAD Binary DXF\r\n\x1a\0";
427+
428+
/// An R12 binary DXF (1-byte group codes) with a line, a circle and a text.
429+
fn r12_binary_dxf() -> Vec<u8> {
430+
let mut b = R12_SENTINEL.to_vec();
431+
let s = |b: &mut Vec<u8>, code: u8, v: &str| {
432+
b.push(code);
433+
b.extend_from_slice(v.as_bytes());
434+
b.push(0);
435+
};
436+
let f = |b: &mut Vec<u8>, code: u8, v: f64| {
437+
b.push(code);
438+
b.extend_from_slice(&v.to_le_bytes());
439+
};
440+
s(&mut b, 0, "SECTION");
441+
s(&mut b, 2, "HEADER");
442+
s(&mut b, 9, "$ACADVER");
443+
s(&mut b, 1, "AC1009");
444+
s(&mut b, 0, "ENDSEC");
445+
s(&mut b, 0, "SECTION");
446+
s(&mut b, 2, "ENTITIES");
447+
s(&mut b, 0, "LINE");
448+
s(&mut b, 8, "0");
449+
for (c, v) in [(10, 1.0), (20, 2.0), (30, 0.0), (11, 4.0), (21, 6.0), (31, 0.0)] {
450+
f(&mut b, c, v);
451+
}
452+
s(&mut b, 0, "CIRCLE");
453+
s(&mut b, 8, "0");
454+
for (c, v) in [(10, 5.0), (20, 5.0), (30, 0.0), (40, 2.5)] {
455+
f(&mut b, c, v);
456+
}
457+
s(&mut b, 0, "TEXT");
458+
s(&mut b, 8, "0");
459+
for (c, v) in [(10, 0.0), (20, 0.0), (30, 0.0), (40, 1.5)] {
460+
f(&mut b, c, v);
461+
}
462+
s(&mut b, 1, "R12");
463+
s(&mut b, 0, "ENDSEC");
464+
s(&mut b, 0, "EOF");
465+
b
466+
}
467+
468+
#[test]
469+
fn r12_binary_dxf_reads_a_line_a_circle_and_text() {
470+
let d = read(&r12_binary_dxf(), "r12.dxf").unwrap();
471+
let ents: Vec<&EntityKind> = d.model.iter().map(|e| &e.kind).collect();
472+
assert_eq!(ents.len(), 3, "{ents:?}");
473+
assert!(ents.iter().any(|k| matches!(k, EntityKind::Line(l) if l.a == Vec3::new(1.0, 2.0, 0.0) && l.b == Vec3::new(4.0, 6.0, 0.0))));
474+
assert!(ents.iter().any(|k| matches!(k, EntityKind::Circle(c) if c.center == Vec3::new(5.0, 5.0, 0.0) && c.radius == 2.5)));
475+
assert!(ents.iter().any(|k| matches!(k, EntityKind::Text(t) if t.value == "R12" && t.height == 1.5)));
413476
}
414477

415478
#[test]

0 commit comments

Comments
 (0)