diff --git a/Cargo.lock b/Cargo.lock index add90f8c2..3e9aeba59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2610,6 +2610,10 @@ dependencies = [ name = "craft-sheet" version = "0.1.0" +[[package]] +name = "craft-shx" +version = "0.1.0" + [[package]] name = "craft-silhouette-studio" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index eae758e17..b68880781 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ rust-version = "1.95" repository = "https://github.com/storytold/craft-libs" [workspace.dependencies] +craft-shx = { path = "crates/shx" } craft-smime = { path = "crates/smime" } craft-ber = { path = "crates/ber" } craft-proto = { path = "crates/proto" } diff --git a/crates/shx/Cargo.toml b/crates/shx/Cargo.toml new file mode 100644 index 000000000..012f9d2f9 --- /dev/null +++ b/crates/shx/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "craft-shx" +description = "Bounded SHP shape-source compiler for SHX shape, big-font and Unicode containers" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true diff --git a/crates/shx/README.md b/crates/shx/README.md new file mode 100644 index 000000000..691890447 --- /dev/null +++ b/crates/shx/README.md @@ -0,0 +1,8 @@ +# craft-shx + +Compiles SHP shape source into SHX shapes, big-font and Unicode containers. +Extracted from CADCraft by Francesco BV (cadcraft52c1e6de); no font assets are bundled. +App font discovery, text layout and glyph interpretation are not part of this crate. +The original 8 MiB source and u16 record bounds remain; malformed double signs, +excess escape ranges and oversized Unicode font records are refused. Duplicate +shape detection is linear rather than a repeated whole-list scan. diff --git a/crates/shx/src/compile.rs b/crates/shx/src/compile.rs index 2e8cb3c25..0d91a9545 100644 --- a/crates/shx/src/compile.rs +++ b/crates/shx/src/compile.rs @@ -13,7 +13,9 @@ //! program's own length is written. use super::ShxError; -use super::parse::{BIGFONT, SHAPES_10, UNIFONT}; +const SHAPES_10: &[u8] = b"AutoCAD-86 shapes 1.0\r\n\x1a"; +const BIGFONT: &[u8] = b"AutoCAD-86 bigfont 1.0\r\n\x1a"; +const UNIFONT: &[u8] = b"AutoCAD-86 unifont 1.0\r\n\x1a"; /// Largest source compiled. const MAX_SOURCE: usize = 8 << 20; @@ -44,7 +46,7 @@ fn number(tok: &str, line: usize) -> Result { } else { digits.parse::().map_err(|_| err())? }; - if v > 0xFFFF { + if !(0..=0xFFFF).contains(&v) { return Err(err()); } Ok(if neg { -v } else { v }) @@ -190,6 +192,11 @@ pub fn compile(source: &str) -> Result, ShxError> { kind = Kind::Big; let v = numbers(rest, line)?; let n = usize::try_from(v.get(1).copied().unwrap_or(0)).unwrap_or(0); + // A big font has byte-valued lead ranges. Bound repeated headers too, + // so a tiny declaration cannot amplify into millions of ranges. + if n > 256 || ranges.len().saturating_add(n) > 256 { + return Err(ShxError::TooLarge); + } for k in 0..n { let lo = v.get(2 + 2 * k).copied().unwrap_or(0); let hi = v.get(3 + 2 * k).copied().unwrap_or(lo); @@ -210,6 +217,9 @@ pub fn compile(source: &str) -> Result, ShxError> { let num = number(parts.next().unwrap_or_default().trim(), line)?; let _defbytes = parts.next(); let name = parts.next().unwrap_or_default().trim().to_string(); + if defs.len() >= usize::from(u16::MAX) { + return Err(ShxError::TooLarge); + } defs.push((num, name, line, Vec::new())); continue; } @@ -228,6 +238,7 @@ pub fn compile(source: &str) -> Result, ShxError> { } } let mut shapes: Vec = Vec::new(); + let mut seen = std::collections::HashSet::new(); for (num, name, line, toks) in defs { let number = u16::try_from(num).map_err(|_| ShxError::Source(line, format!("shape number {num} out of range")))?; let program = if number == 0 { @@ -236,7 +247,7 @@ pub fn compile(source: &str) -> Result, ShxError> { } else { Enc { toks: &toks, i: 0, out: Vec::new(), kind, line }.program()? }; - if shapes.iter().any(|s| s.number == number) { + if !seen.insert(number) { return Err(ShxError::Source(line, format!("shape {number} is defined twice"))); } shapes.push(Shape { number, name, program }); @@ -296,6 +307,9 @@ pub fn compile(source: &str) -> Result, ShxError> { out.extend(UNIFONT); let (name, info) = uni_info.unwrap_or_default(); out.extend((u32::from(count) + 1).to_le_bytes()); + if name.len().saturating_add(1).saturating_add(info.len()) > usize::from(u16::MAX) { + return Err(ShxError::TooLarge); + } let mut rec: Vec = name.bytes().filter(|b| *b != 0).collect(); rec.push(0); rec.extend(&info); diff --git a/crates/shx/src/error.rs b/crates/shx/src/error.rs index 0addd1c98..135214f54 100644 --- a/crates/shx/src/error.rs +++ b/crates/shx/src/error.rs @@ -26,4 +26,3 @@ impl std::fmt::Display for ShxError { } impl std::error::Error for ShxError {} - diff --git a/crates/shx/src/lib.rs b/crates/shx/src/lib.rs new file mode 100644 index 000000000..e595e90f5 --- /dev/null +++ b/crates/shx/src/lib.rs @@ -0,0 +1,8 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] +//! SHP source compilation to SHX containers, extracted from CADCraft. +//! Font discovery, glyph execution and text layout remain consumer responsibilities. +mod compile; +mod error; +pub use compile::compile; +pub use error::ShxError; diff --git a/crates/shx/tests/compiler.rs b/crates/shx/tests/compiler.rs new file mode 100644 index 000000000..f558e185a --- /dev/null +++ b/crates/shx/tests/compiler.rs @@ -0,0 +1,30 @@ +use craft_shx::{ShxError, compile}; + +#[test] +fn fixed_shape_bytes() { + let b = compile("*65,2,A\n010,0\n").unwrap(); + let mut expected = b"AutoCAD-86 shapes 1.0\r\n\x1a".to_vec(); + expected.extend([65, 0, 65, 0, 1, 0, 65, 0, 4, 0, b'A', 0, 0x10, 0]); + expected.extend(b"EOF"); + assert_eq!(b, expected); +} +#[test] +fn hostile_sources_are_refused() { + for src in [ + "*1,2,X\n7\n", + "*1,2,X\n9,(1,1)\n", + "*99999,1,X\n0\n", + "*1,1,X\n300,0\n", + "010,0\n", + "*1,1,X\n1,0zz,0\n", + "*1,1,X\n0\n*1,1,Y\n0\n", + "*--9223372036854775808,1,X\n0", + "*BIGFONT 1,65535\n*1,1,X\n0", + ] { + assert!(compile(src).is_err(), "{src:?}"); + } + assert_eq!(compile(""), Err(ShxError::Empty)); + assert_eq!(compile(&" ".repeat((8 << 20) + 1)), Err(ShxError::TooLarge)); + let oversized_header = format!("*UNIFONT,6,{}\n10,2,0,0,0,0\n*65,1,A\n0", "n".repeat(65536)); + assert_eq!(compile(&oversized_header), Err(ShxError::TooLarge)); +}