mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
shx: expose shared bounded shape compiler with hostile-source tests
This commit is contained in:
1 parent
7b51909d40
commit
b3307f7814
8 files changed
+79
-4
No files matched your search
Generated
+4
@@ -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"
|
||||
|
||||
@@ -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" }
|
||||
|
||||
@@ -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
|
||||
@@ -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.
|
||||
@@ -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<i64, ShxError> {
|
||||
} else {
|
||||
digits.parse::<i64>().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<Vec<u8>, 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<Vec<u8>, 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<Vec<u8>, ShxError> {
|
||||
}
|
||||
}
|
||||
let mut shapes: Vec<Shape> = 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<Vec<u8>, 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<Vec<u8>, 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<u8> = name.bytes().filter(|b| *b != 0).collect();
|
||||
rec.push(0);
|
||||
rec.extend(&info);
|
||||
|
||||
@@ -26,4 +26,3 @@ impl std::fmt::Display for ShxError {
|
||||
}
|
||||
|
||||
impl std::error::Error for ShxError {}
|
||||
|
||||
@@ -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;
|
||||
@@ -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));
|
||||
}
|
||||
Reference in new issue
Block a user