From 2e3ff2162bbf0ee24dc225fc54df19eb4e539547 Mon Sep 17 00:00:00 2001 From: el-j Date: Sun, 15 Nov 2026 11:44:48 +0100 Subject: [PATCH 01/70] craft-image-codecs: SGI image reader and writer Adds Format::Sgi: 8- and 16-bit gray, gray+alpha, RGB and RGBA, verbatim and run-length encoded on read, run-length encoded on write. Palette-mapped files are reported as unsupported. Closes #1559 --- crates/image-codecs/README.md | 1 + crates/image-codecs/src/codecs.rs | 1 + crates/image-codecs/src/codecs/sgi.rs | 345 ++++++++++++++++++++ crates/image-codecs/src/format.rs | 14 +- crates/image-codecs/src/lib.rs | 4 +- crates/image-codecs/tests/it/detect_caps.rs | 17 + 6 files changed, 379 insertions(+), 3 deletions(-) create mode 100644 crates/image-codecs/src/codecs/sgi.rs diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index 6e9e11617..135853490 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -56,6 +56,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | Cineon | yes | yes | U16 (10-bit code values read and written exactly through U16) | Gray, RGB | no | no | no | no | no | no | no | built in | | DICOM | yes | **no** | U8, U16 | Gray, RGB | no | no | no | no | no | no | n/a | built in | | Amiga IFF (ILBM/PBM) | yes | yes (24-bit ILBM) | U8 | RGB, RGBA | yes (1-bit mask plane on write) | no | no | no | no | no | no | built in | +| SGI (`.sgi`, `.rgb`, `.bw`) | yes | yes (RLE) | U8, U16 | Gray, Gray+A, RGB, RGBA | yes | no | no | no | no | no | no | built in | | AVIF | **no** | only with feature `avif` | U8 | RGB, RGBA | yes | no | no | no | no | no | yes | `image` + `ravif` | | HEIF/HEIC | with feature `heif` | **no** | U8, U16 (10/12-bit decodes to U16) | RGB, RGBA | yes (auxiliary alpha) | yes (`colr` prof) | yes | yes | no | no | n/a | `heic-rs` | diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs index 0288ae22b..6f070a290 100644 --- a/crates/image-codecs/src/codecs.rs +++ b/crates/image-codecs/src/codecs.rs @@ -17,6 +17,7 @@ pub(crate) mod jxl; pub(crate) mod pcx; pub(crate) mod png; pub(crate) mod pnm; +pub(crate) mod sgi; pub(crate) mod tga; pub(crate) mod tiff; pub(crate) mod tiff_exif; diff --git a/crates/image-codecs/src/codecs/sgi.rs b/crates/image-codecs/src/codecs/sgi.rs new file mode 100644 index 000000000..1e31b5f0a --- /dev/null +++ b/crates/image-codecs/src/codecs/sgi.rs @@ -0,0 +1,345 @@ +//! SGI image files (`.sgi`, `.rgb`, `.rgba`, `.bw`): 8- and 16-bit gray, gray + alpha, RGB and +//! RGBA, verbatim or run-length encoded. +//! +//! Read and write follow the published SGI image file format description: a 512-byte big-endian +//! header, then one plane per channel with the rows stored bottom to top. Palette-mapped files +//! (colormap ID other than 0) are rejected. The writer uses the run-length encoding. + +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::format::Format; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::Limits; + +const F: Format = Format::Sgi; +const HEADER: usize = 512; +const MAGIC: u16 = 474; + +fn bad(reason: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, reason) +} + +fn be16(b: &[u8], i: usize) -> u16 { + u16::from_be_bytes([b[i], b[i + 1]]) +} + +fn be32(b: &[u8], i: usize) -> u32 { + u32::from_be_bytes([b[i], b[i + 1], b[i + 2], b[i + 3]]) +} + +/// The fields of the header that decoding needs, already checked for plausibility. +struct Header { + rle: bool, + wide: bool, + width: u32, + height: u32, + planes: usize, +} + +/// Parse the 512-byte header. `None` when the bytes are not a plausible SGI header. +fn header(b: &[u8]) -> Option
{ + if b.len() < HEADER || be16(b, 0) != MAGIC { + return None; + } + let (storage, bpc, dimension) = (b[2], b[3], be16(b, 4)); + let (width, height, planes) = (u32::from(be16(b, 6)), u32::from(be16(b, 8)), usize::from(be16(b, 10))); + if storage > 1 || !(1..=2).contains(&bpc) || !(1..=3).contains(&dimension) { + return None; + } + // A one-dimensional image has one row, a two-dimensional one a single plane. + let (height, planes) = match dimension { + 1 => (1, 1), + 2 => (height, 1), + _ => (height, planes), + }; + if width == 0 || height == 0 || !(1..=4).contains(&planes) { + return None; + } + Some(Header { rle: storage == 1, wide: bpc == 2, width, height, planes }) +} + +/// Whether `b` starts with a plausible SGI header (the magic number plus consistent fields). +pub(crate) fn looks_like_sgi(b: &[u8]) -> bool { + header(b).is_some() +} + +fn layout_of(planes: usize) -> ChannelLayout { + match planes { + 1 => ChannelLayout::Gray, + 2 => ChannelLayout::GrayA, + 3 => ChannelLayout::Rgb, + _ => ChannelLayout::Rgba, + } +} + +/// Expand one run-length encoded row into exactly `out.len()` samples. +fn read_rle_row(src: &[u8], wide: bool, out: &mut [u16]) -> Result<(), CodecError> { + let step = if wide { 2 } else { 1 }; + let sample = |pos: usize| -> Result { + let s = src.get(pos..pos + step).ok_or_else(|| bad("truncated RLE row"))?; + Ok(if wide { u16::from_be_bytes([s[0], s[1]]) } else { u16::from(s[0]) }) + }; + let (mut pos, mut n) = (0usize, 0usize); + while n < out.len() { + let code = sample(pos)?; + pos += step; + let count = usize::from(code & 0x7f); + if count == 0 { + return Err(bad("RLE row ends before the image width")); + } + if count > out.len() - n { + return Err(bad("RLE packet runs past the end of the row")); + } + if code & 0x80 != 0 { + for slot in &mut out[n..n + count] { + *slot = sample(pos)?; + pos += step; + } + } else { + let value = sample(pos)?; + pos += step; + out[n..n + count].fill(value); + } + n += count; + } + Ok(()) +} + +pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { + let h = header(b).ok_or_else(|| bad("invalid SGI header"))?; + if be32(b, 104) != 0 { + return Err(CodecError::unsupported(F, "palette-mapped SGI files are not supported")); + } + let layout = layout_of(h.planes); + let sample = if h.wide { SampleType::U16 } else { SampleType::U8 }; + limits.check(h.width, h.height, layout, sample)?; + let (w, ht, planes) = (h.width as usize, h.height as usize, h.planes); + let rows = ht.checked_mul(planes).ok_or_else(|| bad("image size overflow"))?; + let bps = if h.wide { 2 } else { 1 }; + let total = w.checked_mul(rows).ok_or_else(|| bad("image size overflow"))?; + // Reject inputs that cannot hold the declared pixels before allocating anything for them. + let table = if h.rle { rows.checked_mul(8).ok_or_else(|| bad("image size overflow"))? } else { 0 }; + let min_len = if h.rle { + // Every row needs at least a packet and a terminator. + HEADER + table + rows * bps + } else { + HEADER + total.checked_mul(bps).ok_or_else(|| bad("image size overflow"))? + }; + if b.len() < min_len { + return Err(bad("the file is too short for the declared image")); + } + let mut out = Vec::new(); + out.try_reserve_exact(total).map_err(|_| bad("pixel buffer is too large"))?; + out.resize(total, 0u16); + let mut row = vec![0u16; w]; + for z in 0..planes { + for y in 0..ht { + let index = y + z * ht; + if h.rle { + let start = be32(b, HEADER + index * 4) as usize; + let len = be32(b, HEADER + rows * 4 + index * 4) as usize; + let end = start.checked_add(len).filter(|&e| e <= b.len() && start >= HEADER).ok_or_else(|| bad("RLE row lies outside the file"))?; + read_rle_row(&b[start..end], h.wide, &mut row)?; + } else { + let start = HEADER + index * w * bps; + let s = &b[start..start + w * bps]; + for (x, v) in row.iter_mut().enumerate() { + *v = if h.wide { u16::from_be_bytes([s[2 * x], s[2 * x + 1]]) } else { u16::from(s[x]) }; + } + } + // Rows are stored bottom to top; planes are separate. + let top_y = ht - 1 - y; + for (x, &v) in row.iter().enumerate() { + out[(top_y * w + x) * planes + z] = v; + } + } + } + if h.wide { + Image::from_u16(h.width, h.height, layout, &out) + } else { + Image::from_u8(h.width, h.height, layout, out.iter().map(|&v| v as u8).collect()) + } +} + +/// Append one run-length encoded row (one packet list and a terminator). +fn write_rle_row(row: &[u16], wide: bool, out: &mut Vec) { + let put = |out: &mut Vec, v: u16| { + if wide { + out.extend_from_slice(&v.to_be_bytes()); + } else { + out.push(v as u8); + } + }; + let mut i = 0; + while i < row.len() { + let mut run = 1; + while i + run < row.len() && run < 127 && row[i + run] == row[i] { + run += 1; + } + if run >= 2 { + put(out, run as u16); + put(out, row[i]); + i += run; + } else { + // A literal packet extends until the next run of two or more. + let mut end = i + 1; + while end < row.len() && end - i < 127 && !(end + 1 < row.len() && row[end] == row[end + 1]) { + end += 1; + } + put(out, 0x80 | (end - i) as u16); + for &v in &row[i..end] { + put(out, v); + } + i = end; + } + } + put(out, 0); +} + +/// Writes a run-length encoded SGI file: 8 or 16 bits per channel, gray, gray + alpha, RGB or RGBA. +pub(crate) fn encode(src: &Image, plan: Plan) -> Result, CodecError> { + let enc = |msg: String| CodecError::encode(F, msg); + let img = src.converted(plan.layout, plan.sample); + let wide = match img.sample_type() { + SampleType::U8 => false, + SampleType::U16 => true, + s => return Err(enc(format!("SGI stores 8 or 16 bits per channel, not {s:?}"))), + }; + let planes = img.layout().channels(); + if !matches!(img.layout(), ChannelLayout::Gray | ChannelLayout::GrayA | ChannelLayout::Rgb | ChannelLayout::Rgba) { + return Err(enc(format!("no SGI layout for {:?}", img.layout()))); + } + let (w, ht) = img.dimensions(); + let (Ok(w16), Ok(h16)) = (u16::try_from(w), u16::try_from(ht)) else { + return Err(enc(format!("dimensions exceed 65535x65535: {w}x{ht}"))); + }; + let (w, ht) = (w as usize, ht as usize); + let bps = if wide { 2 } else { 1 }; + let data = img.data(); + if data.len() != w * ht * planes * bps { + return Err(enc("the image has the wrong number of samples".into())); + } + let sample_at = |i: usize| -> u16 { if wide { u16::from_ne_bytes([data[2 * i], data[2 * i + 1]]) } else { u16::from(data[i]) } }; + let rows = ht * planes; + let table = HEADER; + let mut starts = Vec::with_capacity(rows); + let mut lens = Vec::with_capacity(rows); + let mut body = Vec::new(); + let mut row = vec![0u16; w]; + let base = u32::try_from(HEADER + rows * 8).map_err(|_| enc("image too large".into()))?; + for z in 0..planes { + for y in 0..ht { + let src_y = ht - 1 - y; // stored bottom to top + for (x, v) in row.iter_mut().enumerate() { + *v = sample_at((src_y * w + x) * planes + z); + } + let before = body.len(); + write_rle_row(&row, wide, &mut body); + let start = u32::try_from(base as usize + before).map_err(|_| enc("image too large".into()))?; + starts.push(start); + lens.push((body.len() - before) as u32); + } + } + let mut out = Vec::with_capacity(table + rows * 8 + body.len()); + out.extend_from_slice(&MAGIC.to_be_bytes()); + out.push(1); // RLE + out.push(bps as u8); + out.extend_from_slice(&(if planes > 1 { 3u16 } else { 2u16 }).to_be_bytes()); + out.extend_from_slice(&w16.to_be_bytes()); + out.extend_from_slice(&h16.to_be_bytes()); + out.extend_from_slice(&(planes as u16).to_be_bytes()); + out.extend_from_slice(&0u32.to_be_bytes()); // pixmin + out.extend_from_slice(&(if wide { 65535u32 } else { 255 }).to_be_bytes()); // pixmax + out.resize(HEADER, 0); // dummy, image name, colormap 0, padding + for s in &starts { + out.extend_from_slice(&s.to_be_bytes()); + } + for l in &lens { + out.extend_from_slice(&l.to_be_bytes()); + } + out.extend_from_slice(&body); + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn gradient(w: u32, h: u32, layout: ChannelLayout) -> Image { + let ch = layout.channels(); + let data = (0..w as usize * h as usize * ch).map(|i| ((i * 7 + i / 5) % 251) as u8).collect(); + Image::from_u8(w, h, layout, data).unwrap() + } + + #[test] + fn rle_round_trips_every_layout_in_8_bits() { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + let img = gradient(37, 11, layout); + let plan = Plan { layout, sample: SampleType::U8 }; + let bytes = encode(&img, plan).unwrap(); + assert!(looks_like_sgi(&bytes)); + let back = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(back.layout(), layout); + assert_eq!(back.data(), img.data()); + } + } + + #[test] + fn rle_round_trips_16_bits_with_long_runs() { + let (w, h) = (300u32, 4u32); + let samples: Vec = (0..w * h * 3).map(|i| if (i / 3) % 200 < 150 { 4242 } else { (i * 31) as u16 }).collect(); + let img = Image::from_u16(w, h, ChannelLayout::Rgb, &samples).unwrap(); + let bytes = encode(&img, Plan { layout: ChannelLayout::Rgb, sample: SampleType::U16 }).unwrap(); + let back = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(back.sample_type(), SampleType::U16); + assert_eq!(back.data(), img.data()); + } + + /// A hand-built verbatim file: two rows, one plane, stored bottom to top. + #[test] + fn verbatim_files_read_bottom_to_top() { + let mut f = vec![0u8; HEADER]; + f[0..2].copy_from_slice(&MAGIC.to_be_bytes()); + f[2] = 0; // verbatim + f[3] = 1; + f[4..6].copy_from_slice(&2u16.to_be_bytes()); + f[6..8].copy_from_slice(&3u16.to_be_bytes()); // x + f[8..10].copy_from_slice(&2u16.to_be_bytes()); // y + f[10..12].copy_from_slice(&1u16.to_be_bytes()); // z + f.extend_from_slice(&[10, 11, 12, 20, 21, 22]); // bottom row first + let img = decode(&f, &Limits::default()).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Gray); + assert_eq!(img.data(), &[20, 21, 22, 10, 11, 12]); + } + + #[test] + fn hostile_input_is_an_error_not_a_panic() { + let img = gradient(16, 8, ChannelLayout::Rgb); + let good = encode(&img, Plan { layout: ChannelLayout::Rgb, sample: SampleType::U8 }).unwrap(); + // Every truncation fails cleanly. + for len in 0..good.len() { + let _ = decode(&good[..len], &Limits::default()); + } + // A row table pointing outside the file, and a packet that overruns its row. + let mut bad_table = good.clone(); + bad_table[HEADER..HEADER + 4].copy_from_slice(&u32::MAX.to_be_bytes()); + assert!(decode(&bad_table, &Limits::default()).is_err()); + let mut overrun = good.clone(); + let start = be32(&overrun, HEADER) as usize; + overrun[start] = 127; // a run longer than the 16-pixel row + assert!(decode(&overrun, &Limits::default()).is_err()); + // A huge declared size is stopped by the limits or the length check, before allocating. + let mut huge = good; + huge[6..8].copy_from_slice(&u16::MAX.to_be_bytes()); + huge[8..10].copy_from_slice(&u16::MAX.to_be_bytes()); + assert!(decode(&huge, &Limits::default()).is_err()); + } + + #[test] + fn palette_mapped_files_are_unsupported() { + let img = gradient(4, 4, ChannelLayout::Gray); + let mut f = encode(&img, Plan { layout: ChannelLayout::Gray, sample: SampleType::U8 }).unwrap(); + f[104..108].copy_from_slice(&1u32.to_be_bytes()); + assert!(matches!(decode(&f, &Limits::default()), Err(CodecError::Unsupported { .. }))); + } +} diff --git a/crates/image-codecs/src/format.rs b/crates/image-codecs/src/format.rs index ecaf945cd..d0d938bb7 100644 --- a/crates/image-codecs/src/format.rs +++ b/crates/image-codecs/src/format.rs @@ -44,6 +44,8 @@ pub enum Format { Iff, /// JPEG 2000 (JP2 container or raw J2K codestream): read-only; see [`ASYMMETRIC_EXCEPTIONS`]. Jp2, + /// SGI image files (`.sgi`, `.rgb`, `.bw`): 8- and 16-bit gray, gray + alpha, RGB and RGBA. + Sgi, } /// What a format can hold **and** what this crate reads/writes for it. @@ -113,7 +115,7 @@ const TIFF_LAYOUTS: &[ChannelLayout] = &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L:: impl Format { /// Every format known to the crate (enabled or not). - pub const ALL: [Format; 24] = [ + pub const ALL: [Format; 25] = [ Format::Png, Format::Jpeg, Format::Jp2, @@ -138,6 +140,7 @@ impl Format { Format::Cineon, Format::Dicom, Format::Iff, + Format::Sgi, ]; pub fn caps(self) -> FormatCaps { @@ -176,6 +179,7 @@ impl Format { Format::Dicom => "DICOM", Format::Iff => "Amiga IFF", Format::Jp2 => "JPEG 2000", + Format::Sgi => "SGI", } } @@ -207,6 +211,7 @@ impl Format { Format::Dicom => &["dcm", "dicom", "dc3"], Format::Iff => &["iff", "lbm", "ilbm"], Format::Jp2 => &["jp2", "j2k", "j2c", "jpf", "jpx"], + Format::Sgi => &["sgi", "rgb", "rgba", "bw"], } } @@ -236,13 +241,14 @@ impl Format { Format::Dicom => "application/dicom", Format::Iff => "image/x-iff", Format::Jp2 => "image/jp2", + Format::Sgi => "image/x-sgi", } } /// Maximum encodable dimensions, if the format has a hard limit. pub fn max_dimensions(self) -> Option<(u32, u32)> { match self { - Format::Jpeg | Format::Gif | Format::Iff => Some((65535, 65535)), + Format::Jpeg | Format::Gif | Format::Iff | Format::Sgi => Some((65535, 65535)), Format::WebP => Some((16384, 16384)), Format::Ico => Some((256, 256)), _ => None, @@ -310,6 +316,7 @@ pub fn caps(format: Format) -> FormatCaps { Format::Cineon => FormatCaps { depths: &[S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Dicom => FormatCaps { read: true, write: false, depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Iff => FormatCaps { depths: &[S::U8], layouts: &[L::Rgb, L::Rgba], alpha: true, ..base }, + Format::Sgi => FormatCaps { depths: &[S::U8, S::U16], layouts: RGB_GRAY, alpha: true, ..base }, Format::Jp2 => FormatCaps { read: true, write: false, layouts: &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA], icc: true, ..base }, } } @@ -388,6 +395,9 @@ pub fn detect(bytes: &[u8]) -> Option { if b.starts_with(b"/* XPM */") || b.starts_with(b"! XPM2\n") || b.starts_with(b"! XPM2\r\n") { return Some(Format::Xpm); } + if crate::codecs::sgi::looks_like_sgi(b) { + return Some(Format::Sgi); + } if crate::codecs::pcx::looks_like_pcx(b) { return Some(Format::Pcx); } diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 21130e392..cad63019d 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -62,7 +62,7 @@ pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_ pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding}; pub use half::f16; -use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; +use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jxl, pcx, png, pnm, sgi, tiff, via_image, webp, xpm}; /// Detect the format and decode with default [`Limits`]. pub fn decode(bytes: &[u8]) -> Result { @@ -101,6 +101,7 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res Format::Cineon => dpx::decode_cineon(bytes, l), Format::Dicom => dicom::decode(bytes, l), Format::Iff => iff::decode(bytes, l), + Format::Sgi => sgi::decode(bytes, l), Format::OpenExr => exr::decode(bytes, l), Format::Heif => heif::decode(bytes, l, opts.keep_orientation), Format::Jp2 => jp2::decode(bytes, l), @@ -457,6 +458,7 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result dpx::encode_cineon(image, plan), Format::Dicom => Err(CodecError::unsupported(format, "DICOM is import-only; save as TIFF or PNG instead")), Format::Iff => iff::encode(image, plan), + Format::Sgi => sgi::encode(image, plan), Format::OpenExr => exr::encode(image, plan, opts), Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")), Format::Jp2 => Err(CodecError::unsupported(format, "JPEG 2000 is import-only; save as PNG or TIFF instead")), diff --git a/crates/image-codecs/tests/it/detect_caps.rs b/crates/image-codecs/tests/it/detect_caps.rs index 640772c83..d7d448388 100644 --- a/crates/image-codecs/tests/it/detect_caps.rs +++ b/crates/image-codecs/tests/it/detect_caps.rs @@ -62,6 +62,7 @@ fn default_build_formats_all_symmetric() { Format::Dpx, Format::Cineon, Format::Iff, + Format::Sgi, ]; for f in expected { assert!(caps(f).read && caps(f).write, "{f:?}"); @@ -218,6 +219,22 @@ fn detect_magic_iff() { assert_eq!(detect(b"FORM\0\0\0\x14AIFF"), None); assert!(decode_as(Format::Iff, b"FORM\0\0\0\x14ILBM").is_err()); } +#[test] +fn detect_magic_sgi() { + use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode}; + let img = Image::from_u8(5, 3, ChannelLayout::Rgba, (0..60).collect()).unwrap(); + let bytes = encode(&img, Format::Sgi, &EncodeOptions::default()).unwrap(); + assert_eq!(detect(&bytes), Some(Format::Sgi)); + assert_eq!(from_extension("scan.RGB"), Some(Format::Sgi)); + assert_eq!(from_extension("scan.bw"), Some(Format::Sgi)); + assert_eq!(decode(&bytes).unwrap().data(), img.data()); + assert!(!ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::Sgi)); + assert_eq!(Format::Sgi.max_dimensions(), Some((65535, 65535))); + // The magic number alone, without a full header, is not SGI. + assert_eq!(detect(&[0x01, 0xda, 0, 1, 0, 2]), None); + assert!(decode_as(Format::Sgi, &[0x01, 0xda]).is_err()); +} + #[test] fn detect_magic_avif() { assert_eq!(detect(b"\0\0\0\x1cftypavif\0\0\0\0avifmif1miaf"), Some(Format::Avif)); From 5628ad359d8f5bf9778f53edba9b9989b4b61d09 Mon Sep 17 00:00:00 2001 From: el-j Date: Sun, 15 Nov 2026 11:49:53 +0100 Subject: [PATCH 02/70] craft-image-codecs: Sun Raster image reader and writer Adds Format::SunRaster: 1-, 8- (gray or colour-mapped), 24- and 32-bit files, uncompressed or byte-encoded (RLE) on read, uncompressed on write. Closes #1560 --- crates/image-codecs/README.md | 1 + crates/image-codecs/src/codecs.rs | 1 + crates/image-codecs/src/codecs/sunraster.rs | 237 ++++++++++++++++++++ crates/image-codecs/src/format.rs | 12 +- crates/image-codecs/src/lib.rs | 4 +- crates/image-codecs/tests/it/detect_caps.rs | 14 ++ 6 files changed, 267 insertions(+), 2 deletions(-) create mode 100644 crates/image-codecs/src/codecs/sunraster.rs diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index 6e9e11617..f9f5f53e6 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -56,6 +56,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | Cineon | yes | yes | U16 (10-bit code values read and written exactly through U16) | Gray, RGB | no | no | no | no | no | no | no | built in | | DICOM | yes | **no** | U8, U16 | Gray, RGB | no | no | no | no | no | no | n/a | built in | | Amiga IFF (ILBM/PBM) | yes | yes (24-bit ILBM) | U8 | RGB, RGBA | yes (1-bit mask plane on write) | no | no | no | no | no | no | built in | +| Sun Raster (`.ras`) | yes (incl. RLE and colour maps) | yes (uncompressed) | U8 | Gray, Gray+A, RGB, RGBA | yes | no | no | no | no | no | no | built in | | AVIF | **no** | only with feature `avif` | U8 | RGB, RGBA | yes | no | no | no | no | no | yes | `image` + `ravif` | | HEIF/HEIC | with feature `heif` | **no** | U8, U16 (10/12-bit decodes to U16) | RGB, RGBA | yes (auxiliary alpha) | yes (`colr` prof) | yes | yes | no | no | n/a | `heic-rs` | diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs index 0288ae22b..aecf95bbd 100644 --- a/crates/image-codecs/src/codecs.rs +++ b/crates/image-codecs/src/codecs.rs @@ -17,6 +17,7 @@ pub(crate) mod jxl; pub(crate) mod pcx; pub(crate) mod png; pub(crate) mod pnm; +pub(crate) mod sunraster; pub(crate) mod tga; pub(crate) mod tiff; pub(crate) mod tiff_exif; diff --git a/crates/image-codecs/src/codecs/sunraster.rs b/crates/image-codecs/src/codecs/sunraster.rs new file mode 100644 index 000000000..84d60f145 --- /dev/null +++ b/crates/image-codecs/src/codecs/sunraster.rs @@ -0,0 +1,237 @@ +//! Sun Raster files (`.ras`, `.sun`): 1-bit, 8-bit (gray or colour-mapped), 24-bit RGB and 32-bit +//! RGBA, uncompressed or byte-encoded (RLE) on read; uncompressed on write. +//! +//! Follows the published rasterfile format: a 32-byte big-endian header, an optional colour map, +//! then rows padded to an even number of bytes. In the "standard" type (1) 24- and 32-bit pixels +//! are stored blue-first; in the "RGB" type (3) red-first. Other types (TIFF, IFF, experimental) +//! are reported as unsupported. + +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::format::Format; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::Limits; + +const F: Format = Format::SunRaster; +const HEADER: usize = 32; +const MAGIC: u32 = 0x59a6_6a95; + +fn bad(reason: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, reason) +} + +fn be32(b: &[u8], i: usize) -> u32 { + u32::from_be_bytes([b[i], b[i + 1], b[i + 2], b[i + 3]]) +} + +/// Whether `b` starts with a Sun rasterfile header. +pub(crate) fn looks_like_sunraster(b: &[u8]) -> bool { + b.len() >= HEADER && be32(b, 0) == MAGIC && matches!(be32(b, 12), 1 | 8 | 24 | 32) +} + +/// Expand byte-encoded data (`0x80` escapes a run) until `want` bytes are produced. +fn unrle(src: &[u8], want: usize) -> Result, CodecError> { + let mut out = Vec::new(); + out.try_reserve_exact(want).map_err(|_| bad("pixel buffer is too large"))?; + let mut i = 0; + while out.len() < want { + let b = *src.get(i).ok_or_else(|| bad("truncated RLE data"))?; + i += 1; + if b != 0x80 { + out.push(b); + continue; + } + let n = *src.get(i).ok_or_else(|| bad("truncated RLE data"))?; + i += 1; + if n == 0 { + out.push(0x80); + continue; + } + let v = *src.get(i).ok_or_else(|| bad("truncated RLE data"))?; + i += 1; + let count = usize::from(n) + 1; + if count > want - out.len() { + return Err(bad("RLE run passes the end of the image")); + } + out.resize(out.len() + count, v); + } + Ok(out) +} + +pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { + if !looks_like_sunraster(b) { + return Err(bad("invalid Sun raster header")); + } + let (w, h, depth, kind, map_type, map_len) = (be32(b, 4), be32(b, 8), be32(b, 12) as usize, be32(b, 20), be32(b, 24), be32(b, 28) as usize); + if !matches!(kind, 0..=3) { + return Err(CodecError::unsupported(F, format!("Sun raster type {kind} is not supported"))); + } + if map_type > 2 { + return Err(CodecError::unsupported(F, format!("Sun raster map type {map_type} is not supported"))); + } + let layout = match depth { + 1 | 8 if map_type != 1 => ChannelLayout::Gray, + 1 | 8 | 24 => ChannelLayout::Rgb, + _ => ChannelLayout::Rgba, + }; + limits.check(w, h, layout, SampleType::U8)?; + let (wu, hu) = (w as usize, h as usize); + let stride = wu.checked_mul(depth).and_then(|n| n.checked_add(15)).ok_or_else(|| bad("image size overflow"))? / 16 * 2; + let want = stride.checked_mul(hu).ok_or_else(|| bad("image size overflow"))?; + let data_at = HEADER.checked_add(map_len).ok_or_else(|| bad("colour map size overflow"))?; + let map = b.get(HEADER..data_at).ok_or_else(|| bad("truncated colour map"))?; + let rest = &b[data_at..]; + let raw; + let rows: &[u8] = if kind == 2 { + raw = unrle(rest, want)?; + &raw + } else { + rest.get(..want).ok_or_else(|| bad("the file is too short for the declared image"))? + }; + let channels = layout.channels(); + let mut out = Vec::new(); + out.try_reserve_exact(wu * hu * channels).map_err(|_| bad("pixel buffer is too large"))?; + let entries = map_len / 3; + for y in 0..hu { + let row = &rows[y * stride..(y + 1) * stride]; + for x in 0..wu { + match depth { + 1 => { + // A set bit is black. + let bit = (row[x / 8] >> (7 - x % 8)) & 1; + out.push(if bit == 1 { 0 } else { 255 }); + } + 8 if map_type == 1 => { + let i = usize::from(row[x]); + if i >= entries { + return Err(bad("pixel index is outside the colour map")); + } + out.extend_from_slice(&[map[i], map[entries + i], map[2 * entries + i]]); + } + 8 => out.push(row[x]), + 24 => { + let p = &row[3 * x..3 * x + 3]; + out.extend_from_slice(&if kind == 3 { [p[0], p[1], p[2]] } else { [p[2], p[1], p[0]] }); + } + _ => { + let p = &row[4 * x..4 * x + 4]; + out.extend_from_slice(&if kind == 3 { [p[1], p[2], p[3], p[0]] } else { [p[3], p[2], p[1], p[0]] }); + } + } + } + } + Image::from_u8(w, h, layout, out) +} + +/// Writes an uncompressed rasterfile: 8-bit gray, 24-bit RGB or 32-bit RGBA, in the RGB-ordered type. +pub(crate) fn encode(src: &Image, plan: Plan) -> Result, CodecError> { + let enc = |msg: String| CodecError::encode(F, msg); + let img = src.converted(plan.layout, plan.sample); + if img.sample_type() != SampleType::U8 { + return Err(enc(format!("Sun raster stores 8 bits per channel, not {:?}", img.sample_type()))); + } + let depth: usize = match img.layout() { + ChannelLayout::Gray => 8, + ChannelLayout::Rgb => 24, + ChannelLayout::Rgba => 32, + l => return Err(enc(format!("no Sun raster layout for {l:?}"))), + }; + let (w, h) = img.dimensions(); + let (wu, hu) = (w as usize, h as usize); + let bytes = depth / 8; + let stride = (wu * bytes).next_multiple_of(2); + let len = stride.checked_mul(hu).and_then(|n| u32::try_from(n).ok()).ok_or_else(|| enc("image too large".into()))?; + let data = img.data(); + if data.len() != wu * hu * bytes { + return Err(enc("the image has the wrong number of samples".into())); + } + let mut out = Vec::with_capacity(HEADER + len as usize); + for v in [MAGIC, w, h, depth as u32, len, 3, 0, 0] { + out.extend_from_slice(&v.to_be_bytes()); + } + for y in 0..hu { + let row = &data[y * wu * bytes..(y + 1) * wu * bytes]; + if depth == 32 { + // Stored alpha first. + for p in row.as_chunks::<4>().0 { + out.extend_from_slice(&[p[3], p[0], p[1], p[2]]); + } + } else { + out.extend_from_slice(row); + } + out.resize(HEADER + (y + 1) * stride, 0); + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn pattern(w: u32, h: u32, layout: ChannelLayout) -> Image { + let n = w as usize * h as usize * layout.channels(); + Image::from_u8(w, h, layout, (0..n).map(|i| ((i * 13 + i / 3) % 253) as u8).collect()).unwrap() + } + + #[test] + fn round_trips_gray_rgb_and_rgba_with_odd_widths() { + for layout in [ChannelLayout::Gray, ChannelLayout::Rgb, ChannelLayout::Rgba] { + let img = pattern(13, 7, layout); + let bytes = encode(&img, Plan { layout, sample: SampleType::U8 }).unwrap(); + assert!(looks_like_sunraster(&bytes)); + let back = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!((back.layout(), back.data()), (layout, img.data())); + } + } + + fn file(depth: u32, kind: u32, map: &[u8], map_type: u32, w: u32, h: u32, data: &[u8]) -> Vec { + let mut f = Vec::new(); + for v in [MAGIC, w, h, depth, data.len() as u32, kind, map_type, map.len() as u32] { + f.extend_from_slice(&v.to_be_bytes()); + } + f.extend_from_slice(map); + f.extend_from_slice(data); + f + } + + #[test] + fn standard_type_is_blue_first_and_rle_expands() { + let f = file(24, 1, &[], 0, 1, 1, &[1, 2, 3, 0]); + assert_eq!(decode(&f, &Limits::default()).unwrap().data(), &[3, 2, 1]); + // 0x80 0x00 is a literal 0x80; 0x80 n v is n + 1 copies of v. The 2x2 image has stride 2. + let f = file(8, 2, &[], 0, 2, 2, &[0x80, 0x00, 0x80, 0x00, 0x80, 0x01, 7]); + assert_eq!(decode(&f, &Limits::default()).unwrap().data(), &[0x80, 0x80, 7, 7]); + // A run that passes the end of the image is an error. + assert!(decode(&file(8, 2, &[], 0, 2, 2, &[0x80, 0x09, 7]), &Limits::default()).is_err()); + } + + #[test] + fn colour_mapped_and_one_bit_files() { + // Two entries: red plane, green plane, blue plane. + let map = [10, 20, 30, 40, 50, 60]; + let f = file(8, 1, &map, 1, 2, 1, &[0, 1]); + let img = decode(&f, &Limits::default()).unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgb, &[10, 30, 50, 20, 40, 60][..])); + let f = file(8, 1, &map, 1, 1, 1, &[5, 0]); + assert!(decode(&f, &Limits::default()).is_err()); + let f = file(1, 1, &[], 0, 3, 1, &[0b1010_0000, 0]); + assert_eq!(decode(&f, &Limits::default()).unwrap().data(), &[0, 255, 0]); + } + + #[test] + fn hostile_input_is_an_error_not_a_panic() { + let img = pattern(9, 5, ChannelLayout::Rgb); + let good = encode(&img, Plan { layout: ChannelLayout::Rgb, sample: SampleType::U8 }).unwrap(); + for len in 0..good.len() { + let _ = decode(&good[..len], &Limits::default()); + } + let mut huge = good.clone(); + huge[4..8].copy_from_slice(&u32::MAX.to_be_bytes()); + huge[8..12].copy_from_slice(&u32::MAX.to_be_bytes()); + assert!(decode(&huge, &Limits::default()).is_err()); + let mut bad_map = good; + bad_map[28..32].copy_from_slice(&u32::MAX.to_be_bytes()); + assert!(decode(&bad_map, &Limits::default()).is_err()); + assert!(matches!(decode(&file(24, 4, &[], 0, 1, 1, &[0; 4]), &Limits::default()), Err(CodecError::Unsupported { .. }))); + } +} diff --git a/crates/image-codecs/src/format.rs b/crates/image-codecs/src/format.rs index ecaf945cd..d4075cc8e 100644 --- a/crates/image-codecs/src/format.rs +++ b/crates/image-codecs/src/format.rs @@ -44,6 +44,8 @@ pub enum Format { Iff, /// JPEG 2000 (JP2 container or raw J2K codestream): read-only; see [`ASYMMETRIC_EXCEPTIONS`]. Jp2, + /// Sun Raster (`.ras`): 1-, 8-, 24- and 32-bit, uncompressed or RLE. + SunRaster, } /// What a format can hold **and** what this crate reads/writes for it. @@ -113,7 +115,7 @@ const TIFF_LAYOUTS: &[ChannelLayout] = &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L:: impl Format { /// Every format known to the crate (enabled or not). - pub const ALL: [Format; 24] = [ + pub const ALL: [Format; 25] = [ Format::Png, Format::Jpeg, Format::Jp2, @@ -138,6 +140,7 @@ impl Format { Format::Cineon, Format::Dicom, Format::Iff, + Format::SunRaster, ]; pub fn caps(self) -> FormatCaps { @@ -176,6 +179,7 @@ impl Format { Format::Dicom => "DICOM", Format::Iff => "Amiga IFF", Format::Jp2 => "JPEG 2000", + Format::SunRaster => "Sun Raster", } } @@ -207,6 +211,7 @@ impl Format { Format::Dicom => &["dcm", "dicom", "dc3"], Format::Iff => &["iff", "lbm", "ilbm"], Format::Jp2 => &["jp2", "j2k", "j2c", "jpf", "jpx"], + Format::SunRaster => &["ras", "sun"], } } @@ -236,6 +241,7 @@ impl Format { Format::Dicom => "application/dicom", Format::Iff => "image/x-iff", Format::Jp2 => "image/jp2", + Format::SunRaster => "image/x-sun-raster", } } @@ -310,6 +316,7 @@ pub fn caps(format: Format) -> FormatCaps { Format::Cineon => FormatCaps { depths: &[S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Dicom => FormatCaps { read: true, write: false, depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Iff => FormatCaps { depths: &[S::U8], layouts: &[L::Rgb, L::Rgba], alpha: true, ..base }, + Format::SunRaster => FormatCaps { layouts: &[L::Gray, L::Rgb, L::Rgba], alpha: true, ..base }, Format::Jp2 => FormatCaps { read: true, write: false, layouts: &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA], icc: true, ..base }, } } @@ -388,6 +395,9 @@ pub fn detect(bytes: &[u8]) -> Option { if b.starts_with(b"/* XPM */") || b.starts_with(b"! XPM2\n") || b.starts_with(b"! XPM2\r\n") { return Some(Format::Xpm); } + if crate::codecs::sunraster::looks_like_sunraster(b) { + return Some(Format::SunRaster); + } if crate::codecs::pcx::looks_like_pcx(b) { return Some(Format::Pcx); } diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 21130e392..ab235d2ab 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -62,7 +62,7 @@ pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_ pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding}; pub use half::f16; -use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; +use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jxl, pcx, png, pnm, sunraster, tiff, via_image, webp, xpm}; /// Detect the format and decode with default [`Limits`]. pub fn decode(bytes: &[u8]) -> Result { @@ -101,6 +101,7 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res Format::Cineon => dpx::decode_cineon(bytes, l), Format::Dicom => dicom::decode(bytes, l), Format::Iff => iff::decode(bytes, l), + Format::SunRaster => sunraster::decode(bytes, l), Format::OpenExr => exr::decode(bytes, l), Format::Heif => heif::decode(bytes, l, opts.keep_orientation), Format::Jp2 => jp2::decode(bytes, l), @@ -457,6 +458,7 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result dpx::encode_cineon(image, plan), Format::Dicom => Err(CodecError::unsupported(format, "DICOM is import-only; save as TIFF or PNG instead")), Format::Iff => iff::encode(image, plan), + Format::SunRaster => sunraster::encode(image, plan), Format::OpenExr => exr::encode(image, plan, opts), Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")), Format::Jp2 => Err(CodecError::unsupported(format, "JPEG 2000 is import-only; save as PNG or TIFF instead")), diff --git a/crates/image-codecs/tests/it/detect_caps.rs b/crates/image-codecs/tests/it/detect_caps.rs index 640772c83..17fb20442 100644 --- a/crates/image-codecs/tests/it/detect_caps.rs +++ b/crates/image-codecs/tests/it/detect_caps.rs @@ -62,6 +62,7 @@ fn default_build_formats_all_symmetric() { Format::Dpx, Format::Cineon, Format::Iff, + Format::SunRaster, ]; for f in expected { assert!(caps(f).read && caps(f).write, "{f:?}"); @@ -218,6 +219,19 @@ fn detect_magic_iff() { assert_eq!(detect(b"FORM\0\0\0\x14AIFF"), None); assert!(decode_as(Format::Iff, b"FORM\0\0\0\x14ILBM").is_err()); } +#[test] +fn detect_magic_sun_raster() { + use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode}; + let img = Image::from_u8(5, 3, ChannelLayout::Rgb, (0..45).collect()).unwrap(); + let bytes = encode(&img, Format::SunRaster, &EncodeOptions::default()).unwrap(); + assert_eq!(detect(&bytes), Some(Format::SunRaster)); + assert_eq!(from_extension("scan.RAS"), Some(Format::SunRaster)); + assert_eq!(decode(&bytes).unwrap().data(), img.data()); + assert!(!ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::SunRaster)); + assert_eq!(detect(&[0x59, 0xa6, 0x6a, 0x95]), None); + assert!(decode_as(Format::SunRaster, &[0x59, 0xa6, 0x6a, 0x95]).is_err()); +} + #[test] fn detect_magic_avif() { assert_eq!(detect(b"\0\0\0\x1cftypavif\0\0\0\0avifmif1miaf"), Some(Format::Avif)); From 1985afe943f2088e56697948638d3286260e6db9 Mon Sep 17 00:00:00 2001 From: nuclearspike Date: Sun, 15 Nov 2026 09:40:41 -0500 Subject: [PATCH 03/70] tga: port metadata payload boundaries and regressions from FilmCraft #1932 Source head e7842f76f887005c460c64a8ab51d59d086e6f27; adapt original decoder guard and synthetic regressions to shared Picture/Error API. --- crates/tga/src/lib.rs | 118 ++++++++- crates/tga/src/metadata_tests.rs | 424 +++++++++++++++++++++++++++++++ 2 files changed, 536 insertions(+), 6 deletions(-) create mode 100644 crates/tga/src/metadata_tests.rs diff --git a/crates/tga/src/lib.rs b/crates/tga/src/lib.rs index ac9d21497..88f4efe6c 100644 --- a/crates/tga/src/lib.rs +++ b/crates/tga/src/lib.rs @@ -18,6 +18,8 @@ //! Input is treated as hostile: the picture size is capped by [`Limits`], and a run-length file //! must be long enough to hold the pixels its header declares before anything is allocated. +#[cfg(test)] +mod metadata_tests; #[cfg(test)] mod tests; @@ -180,6 +182,105 @@ impl Stored { } } +fn metadata_offset(body: &[u8], offset: usize, first_pixel: usize) -> Option { + // Optional metadata cannot replace the header, image ID, or colour map. + (offset >= first_pixel && offset < body.len()).then_some(offset) +} + +/// The start of every developer-directory payload with a nonzero length, from the directory at +/// `directory` in `body` (none when that offset holds no readable directory). +fn developer_payload_starts(body: &[u8], directory: usize) -> Vec { + let Some(count) = + body.get(directory..directory.saturating_add(2)).and_then(|v| v.first_chunk::<2>()).map(|v| usize::from(u16::from_le_bytes(*v))) + else { + return Vec::new(); + }; + let Some(fields) = body.get(directory.saturating_add(2)..) else { return Vec::new() }; + fields + .chunks(10) + .take(count) + .filter_map(|field| { + let start = field.get(2..6).and_then(|v| v.first_chunk::<4>()).map(|v| u32::from_le_bytes(*v) as usize)?; + let length = field.get(6..10).and_then(|v| v.first_chunk::<4>()).map(|v| u32::from_le_bytes(*v))?; + (length != 0).then_some(start) + }) + .collect() +} + +fn image_data_end(bytes: &[u8], first_pixel: usize) -> Result { + if !bytes.ends_with(SIGNATURE) { + return Ok(bytes.len()); + } + // A TGA 2.0 footer cannot begin inside the mandatory header. A legacy image + // may end with the same signature as pixel bytes. + let Some(footer) = bytes.len().checked_sub(SIGNATURE.len() + 8).filter(|&at| at >= HEADER) else { + return Ok(bytes.len()); + }; + let offsets = bytes.get(footer..footer + 8).and_then(|b| b.first_chunk::<8>()).ok_or_else(|| Error::Malformed("truncated footer"))?; + let extension = offsets.first_chunk::<4>().map(|v| u32::from_le_bytes(*v) as usize).ok_or_else(|| Error::Malformed("truncated footer"))?; + let directory = offsets + .get(4..) + .and_then(|v| v.first_chunk::<4>()) + .map(|v| u32::from_le_bytes(*v) as usize) + .ok_or_else(|| Error::Malformed("truncated footer"))?; + let body = bytes.get(..footer).ok_or_else(|| Error::Malformed("truncated footer"))?; + let mut end = footer; + + if let Some(offset) = metadata_offset(body, extension, first_pixel) { + // An in-body reference is a conservative image boundary, even if it overlaps + // required pixels. Those bytes cannot be distinguished from a truncated image + // whose missing pixels are supplied by metadata. + end = end.min(offset); + // TGA 2.0 extension fields at byte offsets 482, 486 and 490 point to the colour + // correction, postage-stamp and scan-line tables. The record's first two bytes give + // its size (495 in TGA 2.0); a shorter record has no such fields, and a record never + // reaches into a developer directory that follows it, so those bytes are not read as + // table offsets. Tables may precede the record. + let declared = + body.get(offset..offset.saturating_add(2)).and_then(|v| v.first_chunk::<2>()).map_or(0, |v| usize::from(u16::from_le_bytes(*v))); + let mut record_end = offset.saturating_add(declared.min(495)); + // The developer directory and its payloads may sit anywhere after the pixels, a payload + // before its directory included; whichever starts first after the extension ends it. + // A directory offset of zero means there is none: the header is not read as one. + let payloads = metadata_offset(body, directory, first_pixel).map(|d| developer_payload_starts(body, d)).unwrap_or_default(); + for next in std::iter::once(directory).chain(payloads) { + if next > offset { + record_end = record_end.min(next); + } + } + if let Some(area) = body.get(offset..record_end.min(body.len())) { + for field in [482, 486, 490] { + if let Some(value) = area.get(field..field + 4).and_then(|v| v.first_chunk::<4>()) + && let Some(table) = metadata_offset(body, u32::from_le_bytes(*value) as usize, first_pixel) + { + end = end.min(table); + } + } + } + } + + if let Some(offset) = metadata_offset(body, directory, first_pixel) { + end = end.min(offset); + if let Some(count) = + body.get(offset..offset.saturating_add(2)).and_then(|v| v.first_chunk::<2>()).map(|v| usize::from(u16::from_le_bytes(*v))) + && let Some(fields) = body.get(offset.saturating_add(2)..) + { + // A corrupt count or final entry does not hide earlier payload offsets. Each + // entry has a 2-byte tag followed by a 4-byte offset and 4-byte length. + for field in fields.chunks(10).take(count) { + if let Some(value) = field.get(2..6).and_then(|v| v.first_chunk::<4>()) + && let Some(length) = field.get(6..10).and_then(|v| v.first_chunk::<4>()) + && u32::from_le_bytes(*length) != 0 + && let Some(payload) = metadata_offset(body, u32::from_le_bytes(*value) as usize, first_pixel) + { + end = end.min(payload); + } + } + } + } + Ok(end) +} + /// Decodes a Targa file to straight-alpha RGBA8, top row first. pub fn decode(bytes: &[u8], limits: &Limits) -> Result { let err = Error::Malformed; @@ -208,13 +309,18 @@ pub fn decode(bytes: &[u8], limits: &Limits) -> Result { } else { None }; - let mut at = HEADER + id_len; - if let Some(l) = map_entry { - let end = at + map_len * l.bytes(); - let map = bytes.get(at..end).ok_or(err("truncated colour map"))?; - palette = map.chunks_exact(l.bytes()).map(|e| l.rgba(e)).collect(); - at = end; + let map_start = HEADER + id_len; + let first_pixel = match map_entry { + Some(layout) => map_start.checked_add(map_len * layout.bytes()).ok_or(err("colour map"))?, + None => map_start, + }; + let end = image_data_end(bytes, first_pixel)?; + let bytes = bytes.get(..end).ok_or(err("truncated image data"))?; + if let Some(layout) = map_entry { + let map = bytes.get(map_start..first_pixel).ok_or(err("truncated colour map"))?; + palette = map.chunks_exact(layout.bytes()).map(|entry| layout.rgba(entry)).collect(); } + let at = first_pixel; let (stored_as, mapped) = match kind & 7 { 1 => { if map_type != 1 || !matches!(depth, 8 | 16) { diff --git a/crates/tga/src/metadata_tests.rs b/crates/tga/src/metadata_tests.rs new file mode 100644 index 000000000..a5c6c0b12 --- /dev/null +++ b/crates/tga/src/metadata_tests.rs @@ -0,0 +1,424 @@ +//! Metadata-boundary regressions ported from FilmCraft #1932 (nuclearspike). +use super::*; +fn header(kind: u8, w: u16, h: u16, depth: u8, descriptor: u8) -> Vec { + let mut b = vec![0, 0, kind, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + b.extend_from_slice(&w.to_le_bytes()); + b.extend_from_slice(&h.to_le_bytes()); + b.extend_from_slice(&[depth, descriptor]); + b +} +fn dec(bytes: &[u8]) -> Result { + decode(bytes, &Limits::default()) +} +#[test] +fn legacy_pixels_that_end_in_signature_are_not_a_footer_before_header() { + for prefix in [0usize, 7] { + let width = (prefix + SIGNATURE.len()) as u16; + let mut file = header(3, width, 1, 8, 0x20); + file.extend(vec![0; prefix]); + file.extend_from_slice(SIGNATURE); + let image = dec(&file).unwrap(); + assert_eq!((image.width, image.height), (u32::from(width), 1)); + let pixels: Vec = std::iter::repeat_n(0, prefix).chain(SIGNATURE.iter().copied()).flat_map(|gray| [gray, gray, gray, 255]).collect(); + assert_eq!(image.rgba, pixels); + let source = dec(&file).unwrap(); + assert_eq!(source.rgba, pixels); + } +} + +#[test] +fn short_extension_followed_by_a_developer_payload_keeps_the_pixels() { + // a 482-byte extension whose size field still says 495, then a developer payload whose + // first bytes land where a full extension keeps its colour-correction offset, then the + // directory that points back at that payload + let mut file = header(2, 1, 1, 24, 0x20); + file.extend_from_slice(&[10, 20, 30]); + let extension = file.len() as u32; + let mut area = vec![0; 482]; + area[..2].copy_from_slice(&495u16.to_le_bytes()); + file.extend_from_slice(&area); + let payload = file.len() as u32; + file.extend_from_slice(&18u32.to_le_bytes()); + file.extend_from_slice(&[0; 14]); + let directory = file.len() as u32; + file.extend_from_slice(&1u16.to_le_bytes()); + file.extend_from_slice(&0u16.to_le_bytes()); + file.extend_from_slice(&payload.to_le_bytes()); + file.extend_from_slice(&18u32.to_le_bytes()); + file.extend_from_slice(&extension.to_le_bytes()); + file.extend_from_slice(&directory.to_le_bytes()); + file.extend_from_slice(SIGNATURE); + let source = dec(&file).expect("the image decodes"); + assert_eq!((source.width, source.height), (1, 1)); + assert_eq!(source.rgba, vec![30, 20, 10, 255]); +} + +#[test] +fn a_footer_without_a_directory_does_not_read_the_header_as_one() { + // no developer directory (offset 0). Read as one, the header's three-byte image ID + // length is an entry count, and the third "entry" is the first words of a colour + // correction table that sits right after a short pixel area; the extension points at + // that table, so the image is incomplete and must stay so. + let mut file = header(2, 2, 1, 24, 0x20); + file[0] = 3; + file.extend_from_slice(&[7, 8, 9]); + file.extend_from_slice(&[0, 0, 255]); + let table = file.len() as u32; + let mut words = vec![0; 2048]; + words[..4].copy_from_slice(&2074u32.to_le_bytes()); + words[4..8].copy_from_slice(&1u32.to_le_bytes()); + file.extend_from_slice(&words); + let extension = file.len() as u32; + let mut area = vec![0; 495]; + area[..2].copy_from_slice(&495u16.to_le_bytes()); + area[482..486].copy_from_slice(&table.to_le_bytes()); + file.extend_from_slice(&area); + file.extend_from_slice(&extension.to_le_bytes()); + file.extend_from_slice(&0u32.to_le_bytes()); + file.extend_from_slice(SIGNATURE); + let decoded = dec(&file); + assert!(decoded.is_err(), "the second pixel is missing"); +} + +#[test] +fn short_extension_followed_by_a_developer_directory_keeps_the_pixels() { + // a complete 1x1 image, a 482-byte extension (its size says so) and right after it a + // developer directory whose entry's length lands where a full extension keeps its + // scan-line table offset; reading it as one would move the image end into the header + let mut file = header(2, 1, 1, 24, 0x20); + file.extend_from_slice(&[10, 20, 30]); + let extension = file.len() as u32; + let mut area = vec![0; 482]; + area[..2].copy_from_slice(&482u16.to_le_bytes()); + file.extend_from_slice(&area); + let directory = file.len() as u32; + let payload = directory + 2 + 10; + file.extend_from_slice(&1u16.to_le_bytes()); + file.extend_from_slice(&0u16.to_le_bytes()); + file.extend_from_slice(&payload.to_le_bytes()); + file.extend_from_slice(&18u32.to_le_bytes()); + file.extend_from_slice(&[0; 18]); + file.extend_from_slice(&extension.to_le_bytes()); + file.extend_from_slice(&directory.to_le_bytes()); + file.extend_from_slice(SIGNATURE); + let source = dec(&file).expect("the image decodes"); + assert_eq!((source.width, source.height), (1, 1)); +} + +#[test] +fn importing_truncated_pixels_does_not_consume_tga_footer() { + for (kind, data) in [(2, vec![0, 0, 255]), (10, vec![0x80, 0, 0, 255])] { + for metadata in [None, Some(0usize), Some(1)] { + let mut file = header(kind, 2, 1, 24, 0x20); + file.extend_from_slice(&data); // only the first of two pixels + let mut offsets = [0u8; 8]; + if let Some(field) = metadata { + let offset = file.len() as u32; + offsets[field * 4..field * 4 + 4].copy_from_slice(&offset.to_le_bytes()); + let mut area = vec![0; 495]; + if field == 0 { + area[..2].copy_from_slice(&495u16.to_le_bytes()); + } + file.extend_from_slice(&area); // optional metadata is not pixel data + } + file.extend_from_slice(&offsets); + file.extend_from_slice(SIGNATURE); + let result = dec(&file); + assert!(matches!(result, Err(_)), "kind {kind}, metadata {metadata:?}: metadata is not a second pixel"); + } + } + for kind in [1, 9] { + let mut file = header(kind, 1, 1, 8, 0x20); + file[1] = 1; + file[5] = 1; + file[7] = 24; + file.extend_from_slice(&[0, 0]); // missing third palette byte and the index + file.extend_from_slice(&[0; 8]); + file.extend_from_slice(SIGNATURE); + let result = dec(&file); + assert!(matches!(result, Err(_)), "kind {kind}: the footer is not colour-map data"); + } +} + +#[test] +fn complete_tga_footer_and_metadata_do_not_change_pixels() { + for metadata in [None, Some(0usize), Some(1)] { + let mut file = header(2, 1, 1, 24, 0x20); + file.extend_from_slice(&[0, 0, 255]); + let mut offsets = [0u8; 8]; + if let Some(field) = metadata { + let offset = file.len() as u32; + offsets[field * 4..field * 4 + 4].copy_from_slice(&offset.to_le_bytes()); + let mut area = vec![0; 495]; + if field == 0 { + area[..2].copy_from_slice(&495u16.to_le_bytes()); + } + file.extend_from_slice(&area); + } + file.extend_from_slice(&offsets); + file.extend_from_slice(SIGNATURE); + let source = dec(&file).unwrap(); + assert_eq!(source.rgba, [255, 0, 0, 255]); + } +} + +fn preceding_metadata(complete: bool, metadata: usize) -> Vec { + let mut file = header(2, if complete { 1 } else { 2 }, 1, 24, 0x20); + file.extend_from_slice(&[0, 0, 255]); + let payload_offset = file.len() as u32; + let mut offsets = [0u8; 8]; + if metadata == 0 { + file.extend_from_slice(&[0, 255, 0]); + let directory_offset = file.len() as u32; + file.extend_from_slice(&1u16.to_le_bytes()); + file.extend_from_slice(&1u16.to_le_bytes()); + file.extend_from_slice(&payload_offset.to_le_bytes()); + file.extend_from_slice(&3u32.to_le_bytes()); + offsets[4..].copy_from_slice(&directory_offset.to_le_bytes()); + } else { + match metadata { + 1 => file.extend_from_slice(&[0; 2048]), + 2 => file.extend_from_slice(&[1, 1, 0, 255, 0]), + 3 => file.extend_from_slice(&18u32.to_le_bytes()), + _ => unreachable!(), + } + let extension_offset = file.len() as u32; + let mut extension = [0; 495]; + extension[..2].copy_from_slice(&495u16.to_le_bytes()); + let field = [0, 482, 486, 490][metadata]; + extension[field..field + 4].copy_from_slice(&payload_offset.to_le_bytes()); + file.extend_from_slice(&extension); + offsets[..4].copy_from_slice(&extension_offset.to_le_bytes()); + } + file.extend_from_slice(&offsets); + file.extend_from_slice(SIGNATURE); + file +} + +#[test] +fn preceding_metadata_cannot_supply_missing_pixels() { + let mut accepted = Vec::new(); + for metadata in 0..4 { + let result = dec(&preceding_metadata(false, metadata)); + if !matches!(result, Err(_)) { + accepted.push(metadata); + } + } + assert!(accepted.is_empty(), "metadata payloads supplied missing pixels: {accepted:?}"); +} + +#[test] +fn complete_pixels_before_metadata_keep_their_colour() { + for metadata in 0..4 { + let source = dec(&preceding_metadata(true, metadata)).unwrap(); + assert_eq!(source.rgba, [255, 0, 0, 255], "metadata {metadata}"); + } + let mut file = preceding_metadata(true, 1); + let footer = file.len() - SIGNATURE.len() - 8; + let extension = u32::from_le_bytes(file[footer..footer + 4].try_into().unwrap()) as usize; + file[extension..extension + 2].copy_from_slice(&496u16.to_le_bytes()); + file.insert(footer, 0); + let source = dec(&file).unwrap(); + assert_eq!(source.rgba, [255, 0, 0, 255], "larger complete extensions preserve pixels"); +} + +#[test] +fn malformed_optional_metadata_keeps_complete_pixels_readable() { + let accept = |file: Vec| { + let source = dec(&file).unwrap(); + assert_eq!(source.rgba, [255, 0, 0, 255]); + }; + for field in [0, 4] { + let mut file = preceding_metadata(true, 0); + let footer = file.len() - SIGNATURE.len() - 8; + file[footer + field..footer + field + 4].copy_from_slice(&u32::MAX.to_le_bytes()); + accept(file); + } + for metadata in 1..4 { + let mut file = preceding_metadata(true, metadata); + let footer = file.len() - SIGNATURE.len() - 8; + let extension = u32::from_le_bytes(file[footer..footer + 4].try_into().unwrap()) as usize; + let field = [0, 482, 486, 490][metadata]; + file[extension + field..extension + field + 4].copy_from_slice(&u32::MAX.to_le_bytes()); + accept(file); + } + let mut file = preceding_metadata(true, 0); + let footer = file.len() - SIGNATURE.len() - 8; + let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; + file.drain(directory + 2..footer); + accept(file); + let mut file = preceding_metadata(true, 0); + let footer = file.len() - SIGNATURE.len() - 8; + let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; + file[directory + 8..directory + 12].copy_from_slice(&u32::MAX.to_le_bytes()); + accept(file); + let mut file = preceding_metadata(true, 0); + let footer = file.len() - SIGNATURE.len() - 8; + let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; + file[directory..directory + 2].copy_from_slice(&2u16.to_le_bytes()); + accept(file); + // A zero-length entry cannot mark a payload boundary, even if its offset overlaps pixels. + let mut file = preceding_metadata(true, 0); + let footer = file.len() - SIGNATURE.len() - 8; + let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; + file[directory + 4..directory + 8].copy_from_slice(&19u32.to_le_bytes()); + file[directory + 8..directory + 12].copy_from_slice(&0u32.to_le_bytes()); + accept(file); + let mut file = preceding_metadata(true, 2); + file[21..23].copy_from_slice(&[255, 255]); + accept(file); + let mut file = preceding_metadata(true, 2); + let footer = file.len() - SIGNATURE.len() - 8; + let extension = u32::from_le_bytes(file[footer..footer + 4].try_into().unwrap()) as usize; + file[extension..extension + 2].copy_from_slice(&494u16.to_le_bytes()); + accept(file); + let mut file = preceding_metadata(true, 2); + let footer = file.len() - SIGNATURE.len() - 8; + file.remove(footer - 1); // extension is physically short, but its postage offset remains + accept(file); +} + +#[test] +fn malformed_metadata_offsets_inside_id_or_palette_keep_complete_pixels() { + let mut file = header(1, 1, 1, 8, 0x20); + file[0] = 4; // image ID length + file[1] = 1; // colour map present + file[5..7].copy_from_slice(&1u16.to_le_bytes()); + file[7] = 24; // one BGR palette entry + file.extend_from_slice(&[7, 8, 9, 10]); // image ID + file.extend_from_slice(&[0, 0, 255]); // red palette entry + file.push(0); // palette index + for (field, offset) in [(0, HEADER + 1), (4, HEADER + 4 + 1)] { + let mut candidate = file.clone(); + let mut footer_offsets = [0u8; 8]; + footer_offsets[field..field + 4].copy_from_slice(&(offset as u32).to_le_bytes()); + candidate.extend_from_slice(&footer_offsets); + candidate.extend_from_slice(SIGNATURE); + let image = dec(&candidate).unwrap(); + assert_eq!(image.rgba, [255, 0, 0, 255], "footer field {field}"); + } +} + +#[test] +fn malformed_optional_metadata_cannot_supply_missing_pixels() { + let mut file = preceding_metadata(false, 0); + let footer = file.len() - SIGNATURE.len() - 8; + let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; + file[directory + 8..directory + 12].copy_from_slice(&u32::MAX.to_le_bytes()); + assert!(matches!(dec(&file), Err(_))); + + // The declared entry count exceeds the bytes before the footer, but the first entry's + // payload still precedes the directory and cannot be used as a second pixel. + let mut file = preceding_metadata(false, 0); + let footer = file.len() - SIGNATURE.len() - 8; + let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; + file[directory..directory + 2].copy_from_slice(&2u16.to_le_bytes()); + assert!(matches!(dec(&file), Err(_))); + + let mut file = preceding_metadata(false, 2); + let footer = file.len() - SIGNATURE.len() - 8; + let extension = u32::from_le_bytes(file[footer..footer + 4].try_into().unwrap()) as usize; + file[extension..extension + 2].copy_from_slice(&494u16.to_le_bytes()); + assert!(matches!(dec(&file), Err(_))); + let mut file = preceding_metadata(false, 2); + let footer = file.len() - SIGNATURE.len() - 8; + file.remove(footer - 1); + assert!(matches!(dec(&file), Err(_))); +} + +#[test] +fn valid_developer_directory_with_overlapping_payload_rejects_pixels() { + let mut file = preceding_metadata(true, 0); + let footer = file.len() - SIGNATURE.len() - 8; + let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; + file[directory..directory + 2].copy_from_slice(&2u16.to_le_bytes()); + let mut extra = [0u8; 10]; + extra[..2].copy_from_slice(&2u16.to_le_bytes()); + extra[2..6].copy_from_slice(&(HEADER as u32).to_le_bytes()); + extra[6..10].copy_from_slice(&1u32.to_le_bytes()); + file.splice(footer..footer, extra); + let moved_footer = file.len() - SIGNATURE.len() - 8; + assert_eq!(u32::from_le_bytes(file[moved_footer + 4..moved_footer + 8].try_into().unwrap()), directory as u32); + assert_eq!(u16::from_le_bytes(file[directory..directory + 2].try_into().unwrap()), 2); + assert_eq!(&file[directory + 12..directory + 22], &extra); + assert!(matches!(dec(&file), Err(_))); +} + +#[test] +fn nonempty_developer_reference_inside_pixels_rejects_the_whole_image() { + let mut file = header(2, 2, 1, 24, 0x20); + file.extend_from_slice(&[0, 0, 255, 0, 255, 0]); + let directory = file.len() as u32; + file.extend_from_slice(&1u16.to_le_bytes()); + file.extend_from_slice(&1u16.to_le_bytes()); + file.extend_from_slice(&(HEADER as u32 + 3).to_le_bytes()); + file.extend_from_slice(&1u32.to_le_bytes()); + file.extend_from_slice(&0u32.to_le_bytes()); + file.extend_from_slice(&directory.to_le_bytes()); + file.extend_from_slice(SIGNATURE); + assert!(matches!(dec(&file), Err(_)), "an overlapping reference must not return a partial image"); +} + +#[test] +fn inflated_developer_count_scans_the_next_chunk_as_an_entry_and_rejects_its_pixel_payload() { + // a directory claiming two entries over one real entry: the ten bytes after it, scanned as + // the second entry, name a payload inside the pixels, which is refused like any other + let mut file = header(2, 2, 1, 24, 0x20); + file.extend_from_slice(&[0, 0, 255, 0, 255, 0]); + let directory = file.len() as u32; + file.extend_from_slice(&2u16.to_le_bytes()); + file.extend_from_slice(&1u16.to_le_bytes()); + file.extend_from_slice(&(directory + 22).to_le_bytes()); + file.extend_from_slice(&0u32.to_le_bytes()); + file.extend_from_slice(&2u16.to_le_bytes()); + file.extend_from_slice(&(HEADER as u32 + 3).to_le_bytes()); + file.extend_from_slice(&1u32.to_le_bytes()); + file.extend_from_slice(&0u32.to_le_bytes()); + file.extend_from_slice(&directory.to_le_bytes()); + file.extend_from_slice(SIGNATURE); + assert!(matches!(dec(&file), Err(_)), "a scanned entry's payload inside the pixels is refused"); +} + +#[test] +fn legacy_pixels_that_end_in_signature_at_or_after_the_header_are_read_as_a_footer() { + // a footerless 36×1 grayscale image whose last 26 bytes happen to be eight zeros and the + // signature: the putative trailer begins at byte 28, so it is taken for a footer and the + // image, left with 10 pixel bytes, is rejected as truncated (the ambiguity the body states) + let prefix = 18usize; + let width = (prefix + SIGNATURE.len()) as u16; + let mut file = header(3, width, 1, 8, 0x20); + file.extend(vec![0; prefix]); + file.extend_from_slice(SIGNATURE); + assert!(matches!(dec(&file), Err(_))); +} + +#[test] +fn mutated_tga_footer_files_never_panic() { + let mut file = header(2, 2, 1, 24, 0x20); + file.extend_from_slice(&[0, 0, 255, 0, 255, 0]); + file.extend_from_slice(&[0; 8]); + file.extend_from_slice(SIGNATURE); + for end in 0..file.len() { + assert!(std::panic::catch_unwind(|| dec(&file[..end])).is_ok(), "truncation at {end}"); + } + for at in 0..file.len() { + let mut mutated = file.clone(); + mutated[at] ^= 0xFF; + assert!(std::panic::catch_unwind(|| dec(&mutated)).is_ok(), "mutation at {at}"); + } + for metadata in 0..4 { + let file = preceding_metadata(true, metadata); + assert!(dec(&file).is_ok()); + let footer = file.len() - SIGNATURE.len() - 8; + for end in 0..footer { + let mut truncated = file[..end].to_vec(); + truncated.extend_from_slice(&file[footer..]); + assert!(std::panic::catch_unwind(|| dec(&truncated)).is_ok(), "metadata {metadata}, footer-preserving truncation at {end}"); + } + for at in 0..file.len() { + let mut mutated = file.clone(); + mutated[at] ^= 0xFF; + assert!(std::panic::catch_unwind(|| dec(&mutated)).is_ok(), "metadata {metadata}, mutation at {at}"); + } + } +} From b6b33b6096a247514f5cbd1741633913e7261e91 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 09:40:41 -0500 Subject: [PATCH 04/70] tga: record original metadata-boundary source attribution --- ATTRIBUTION.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index c087ecda1..e6f928645 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -99,3 +99,5 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso ## Binary Apple property lists `crates/plist` is extracted from `storytold/craft-anything` (`crates/formats/src/plist.rs` and checked byte reads), at source main `88d1d9cac852aec1c7fd97886db451e005e49d2a`. MIT OR Apache-2.0; source license texts are retained in the crate. Primary import Git authors preserve Brandon Thomas, BFlat, RRFZ and Afloat. Regression imports preserve BFlat. XML, JSON projection, calendar formatting and app policy were not imported into the public API. + +`crates/tga/src/metadata_tests.rs` and metadata-boundary helpers in `crates/tga/src/lib.rs` are adapted from nuclearspike's FilmCraft #1932 at `e7842f76f887005c460c64a8ab51d59d086e6f27` (MIT OR Apache-2.0). Synthetic fixtures are original contributor test data; primary Git author retained. From e555cc0afe12d14a3fed4e369e78d45cf084109a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 09:48:49 -0500 Subject: [PATCH 05/70] fix(tga): use eager static footer errors --- crates/tga/src/lib.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/crates/tga/src/lib.rs b/crates/tga/src/lib.rs index 88f4efe6c..06a9941a0 100644 --- a/crates/tga/src/lib.rs +++ b/crates/tga/src/lib.rs @@ -216,14 +216,11 @@ fn image_data_end(bytes: &[u8], first_pixel: usize) -> Result { let Some(footer) = bytes.len().checked_sub(SIGNATURE.len() + 8).filter(|&at| at >= HEADER) else { return Ok(bytes.len()); }; - let offsets = bytes.get(footer..footer + 8).and_then(|b| b.first_chunk::<8>()).ok_or_else(|| Error::Malformed("truncated footer"))?; - let extension = offsets.first_chunk::<4>().map(|v| u32::from_le_bytes(*v) as usize).ok_or_else(|| Error::Malformed("truncated footer"))?; - let directory = offsets - .get(4..) - .and_then(|v| v.first_chunk::<4>()) - .map(|v| u32::from_le_bytes(*v) as usize) - .ok_or_else(|| Error::Malformed("truncated footer"))?; - let body = bytes.get(..footer).ok_or_else(|| Error::Malformed("truncated footer"))?; + let offsets = bytes.get(footer..footer + 8).and_then(|b| b.first_chunk::<8>()).ok_or(Error::Malformed("truncated footer"))?; + let extension = offsets.first_chunk::<4>().map(|v| u32::from_le_bytes(*v) as usize).ok_or(Error::Malformed("truncated footer"))?; + let directory = + offsets.get(4..).and_then(|v| v.first_chunk::<4>()).map(|v| u32::from_le_bytes(*v) as usize).ok_or(Error::Malformed("truncated footer"))?; + let body = bytes.get(..footer).ok_or(Error::Malformed("truncated footer"))?; let mut end = footer; if let Some(offset) = metadata_offset(body, extension, first_pixel) { From d2bb64c4ac41c9242479f5e8277580b847151e5a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 10:14:40 -0500 Subject: [PATCH 06/70] fix(tga): bound aggregate decoder storage and reserve fallibly --- crates/tga/src/lib.rs | 36 +++++++++++++++++++++++++------- crates/tga/src/metadata_tests.rs | 22 +++++++++---------- crates/tga/src/tests.rs | 33 +++++++++++++++++++++++++++-- 3 files changed, 70 insertions(+), 21 deletions(-) diff --git a/crates/tga/src/lib.rs b/crates/tga/src/lib.rs index 06a9941a0..5174a8093 100644 --- a/crates/tga/src/lib.rs +++ b/crates/tga/src/lib.rs @@ -59,11 +59,13 @@ pub type Result = std::result::Result; pub struct Limits { /// Most pixels (width × height) a picture may have. Default 2^28 (16384 × 16384). pub max_pixels: u64, + /// Maximum combined palette, stored-pixel and RGBA allocation bytes. Default 256 MiB. + pub max_decoded_bytes: usize, } impl Default for Limits { fn default() -> Self { - Self { max_pixels: 1 << 28 } + Self { max_pixels: 1 << 28, max_decoded_bytes: 256 << 20 } } } @@ -313,10 +315,6 @@ pub fn decode(bytes: &[u8], limits: &Limits) -> Result { }; let end = image_data_end(bytes, first_pixel)?; let bytes = bytes.get(..end).ok_or(err("truncated image data"))?; - if let Some(layout) = map_entry { - let map = bytes.get(map_start..first_pixel).ok_or(err("truncated colour map"))?; - palette = map.chunks_exact(layout.bytes()).map(|entry| layout.rgba(entry)).collect(); - } let at = first_pixel; let (stored_as, mapped) = match kind & 7 { 1 => { @@ -335,14 +333,35 @@ pub fn decode(bytes: &[u8], limits: &Limits) -> Result { }; let data = bytes.get(at..).unwrap_or_default(); let bpp = stored_as.bytes(); + let stored_bytes = pixels.checked_mul(bpp).ok_or(Error::LimitExceeded("image size"))?; + let rgba_bytes = pixels.checked_mul(4).ok_or(Error::LimitExceeded("image size"))?; + let palette_bytes = if map_entry.is_some() { map_len * 4 } else { 0 }; + let aggregate = + stored_bytes.checked_add(rgba_bytes).and_then(|n| n.checked_add(palette_bytes)).ok_or(Error::LimitExceeded("decode allocation bytes"))?; + if aggregate > limits.max_decoded_bytes { + return Err(Error::LimitExceeded("decode allocation bytes")); + } + if let Some(layout) = map_entry { + let map = bytes.get(map_start..first_pixel).ok_or(err("truncated colour map"))?; + palette.try_reserve_exact(map_len).map_err(|_| Error::LimitExceeded("allocation failed"))?; + palette.extend(map.chunks_exact(layout.bytes()).map(|entry| layout.rgba(entry))); + } // The stored values, in file order. let stored: Vec = if rle { unpack_rle(data, pixels, bpp)? } else { let n = pixels.checked_mul(bpp).ok_or(Error::LimitExceeded("image size"))?; - data.get(..n).ok_or(err("truncated pixel data"))?.to_vec() + { + let source = data.get(..n).ok_or(err("truncated pixel data"))?; + let mut values = Vec::new(); + values.try_reserve_exact(n).map_err(|_| Error::LimitExceeded("allocation failed"))?; + values.extend_from_slice(source); + values + } }; - let mut rgba = vec![0u8; pixels.checked_mul(4).ok_or(Error::LimitExceeded("image size"))?]; + let mut rgba = Vec::new(); + rgba.try_reserve_exact(rgba_bytes).map_err(|_| Error::LimitExceeded("allocation failed"))?; + rgba.resize(rgba_bytes, 0u8); for (i, v) in stored.chunks_exact(bpp).enumerate().take(pixels) { let (x, y) = (i % w, i / w); let x = if right_to_left { w - 1 - x } else { x }; @@ -386,7 +405,8 @@ fn unpack_rle(mut data: &[u8], pixels: usize, bpp: usize) -> Result> { return Err(short); } let total = pixels.checked_mul(bpp).ok_or(Error::LimitExceeded("image size"))?; - let mut out = Vec::with_capacity(total); + let mut out = Vec::new(); + out.try_reserve_exact(total).map_err(|_| Error::LimitExceeded("allocation failed"))?; while out.len() < total { let (&head, rest) = data.split_first().ok_or(short.clone())?; let count = usize::from(head & 0x7F) + 1; diff --git a/crates/tga/src/metadata_tests.rs b/crates/tga/src/metadata_tests.rs index a5c6c0b12..583f5d691 100644 --- a/crates/tga/src/metadata_tests.rs +++ b/crates/tga/src/metadata_tests.rs @@ -124,7 +124,7 @@ fn importing_truncated_pixels_does_not_consume_tga_footer() { file.extend_from_slice(&offsets); file.extend_from_slice(SIGNATURE); let result = dec(&file); - assert!(matches!(result, Err(_)), "kind {kind}, metadata {metadata:?}: metadata is not a second pixel"); + assert!(result.is_err(), "kind {kind}, metadata {metadata:?}: metadata is not a second pixel"); } } for kind in [1, 9] { @@ -136,7 +136,7 @@ fn importing_truncated_pixels_does_not_consume_tga_footer() { file.extend_from_slice(&[0; 8]); file.extend_from_slice(SIGNATURE); let result = dec(&file); - assert!(matches!(result, Err(_)), "kind {kind}: the footer is not colour-map data"); + assert!(result.is_err(), "kind {kind}: the footer is not colour-map data"); } } @@ -200,7 +200,7 @@ fn preceding_metadata_cannot_supply_missing_pixels() { let mut accepted = Vec::new(); for metadata in 0..4 { let result = dec(&preceding_metadata(false, metadata)); - if !matches!(result, Err(_)) { + if result.is_ok() { accepted.push(metadata); } } @@ -305,7 +305,7 @@ fn malformed_optional_metadata_cannot_supply_missing_pixels() { let footer = file.len() - SIGNATURE.len() - 8; let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; file[directory + 8..directory + 12].copy_from_slice(&u32::MAX.to_le_bytes()); - assert!(matches!(dec(&file), Err(_))); + assert!(dec(&file).is_err()); // The declared entry count exceeds the bytes before the footer, but the first entry's // payload still precedes the directory and cannot be used as a second pixel. @@ -313,17 +313,17 @@ fn malformed_optional_metadata_cannot_supply_missing_pixels() { let footer = file.len() - SIGNATURE.len() - 8; let directory = u32::from_le_bytes(file[footer + 4..footer + 8].try_into().unwrap()) as usize; file[directory..directory + 2].copy_from_slice(&2u16.to_le_bytes()); - assert!(matches!(dec(&file), Err(_))); + assert!(dec(&file).is_err()); let mut file = preceding_metadata(false, 2); let footer = file.len() - SIGNATURE.len() - 8; let extension = u32::from_le_bytes(file[footer..footer + 4].try_into().unwrap()) as usize; file[extension..extension + 2].copy_from_slice(&494u16.to_le_bytes()); - assert!(matches!(dec(&file), Err(_))); + assert!(dec(&file).is_err()); let mut file = preceding_metadata(false, 2); let footer = file.len() - SIGNATURE.len() - 8; file.remove(footer - 1); - assert!(matches!(dec(&file), Err(_))); + assert!(dec(&file).is_err()); } #[test] @@ -341,7 +341,7 @@ fn valid_developer_directory_with_overlapping_payload_rejects_pixels() { assert_eq!(u32::from_le_bytes(file[moved_footer + 4..moved_footer + 8].try_into().unwrap()), directory as u32); assert_eq!(u16::from_le_bytes(file[directory..directory + 2].try_into().unwrap()), 2); assert_eq!(&file[directory + 12..directory + 22], &extra); - assert!(matches!(dec(&file), Err(_))); + assert!(dec(&file).is_err()); } #[test] @@ -356,7 +356,7 @@ fn nonempty_developer_reference_inside_pixels_rejects_the_whole_image() { file.extend_from_slice(&0u32.to_le_bytes()); file.extend_from_slice(&directory.to_le_bytes()); file.extend_from_slice(SIGNATURE); - assert!(matches!(dec(&file), Err(_)), "an overlapping reference must not return a partial image"); + assert!(dec(&file).is_err(), "an overlapping reference must not return a partial image"); } #[test] @@ -376,7 +376,7 @@ fn inflated_developer_count_scans_the_next_chunk_as_an_entry_and_rejects_its_pix file.extend_from_slice(&0u32.to_le_bytes()); file.extend_from_slice(&directory.to_le_bytes()); file.extend_from_slice(SIGNATURE); - assert!(matches!(dec(&file), Err(_)), "a scanned entry's payload inside the pixels is refused"); + assert!(dec(&file).is_err(), "a scanned entry's payload inside the pixels is refused"); } #[test] @@ -389,7 +389,7 @@ fn legacy_pixels_that_end_in_signature_at_or_after_the_header_are_read_as_a_foot let mut file = header(3, width, 1, 8, 0x20); file.extend(vec![0; prefix]); file.extend_from_slice(SIGNATURE); - assert!(matches!(dec(&file), Err(_))); + assert!(dec(&file).is_err()); } #[test] diff --git a/crates/tga/src/tests.rs b/crates/tga/src/tests.rs index 06109a179..76472075f 100644 --- a/crates/tga/src/tests.rs +++ b/crates/tga/src/tests.rs @@ -116,8 +116,8 @@ fn hostile_headers_fail_without_allocating() { fn the_pixel_limit_is_checked() { let mut f = header(3, 2, 2, 8, 0); f.extend_from_slice(&[0; 4]); - assert!(decode(&f, &Limits { max_pixels: 4 }).is_ok()); - assert_eq!(decode(&f, &Limits { max_pixels: 3 }), Err(Error::LimitExceeded("pixel count"))); + assert!(decode(&f, &Limits { max_pixels: 4, ..Limits::default() }).is_ok()); + assert_eq!(decode(&f, &Limits { max_pixels: 3, ..Limits::default() }), Err(Error::LimitExceeded("pixel count"))); } #[test] @@ -235,3 +235,32 @@ fn written_files_read_back() { assert_eq!(r.rgba, want, "{layout:?}"); } } + +#[test] +fn aggregate_decode_budget_applies_before_rle_expansion() { + let mut file = header(10, 16, 16, 32, 0x28); + file.extend_from_slice(&[255, 0, 0, 255, 255, 255, 0, 0, 255, 255]); + let mut limits = Limits::default(); + limits.max_decoded_bytes = 2047; + assert_eq!(decode(&file, &limits), Err(Error::LimitExceeded("decode allocation bytes"))); + limits.max_decoded_bytes = 2048; + assert_eq!(decode(&file, &limits).unwrap().rgba.len(), 1024); +} +#[test] +fn default_budget_rejects_large_rle_header_without_large_fixture() { + let file = header(10, 16384, 16384, 32, 0x28); + assert_eq!(decode(&file, &Limits::default()), Err(Error::LimitExceeded("decode allocation bytes"))); +} +#[test] +fn aggregate_decode_budget_counts_raw_and_palette_storage() { + let mut file = header(1, 1, 1, 8, 0x20); + file[1] = 1; + file[5] = 1; + file[7] = 24; + file.extend_from_slice(&[0, 0, 255, 0]); + let mut limits = Limits::default(); + limits.max_decoded_bytes = 8; + assert_eq!(decode(&file, &limits), Err(Error::LimitExceeded("decode allocation bytes"))); + limits.max_decoded_bytes = 9; + assert_eq!(decode(&file, &limits).unwrap().rgba, [255, 0, 0, 255]); +} From a33a81b1f19b348deab1ec5bc3942cb26095e692 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 10:15:49 -0500 Subject: [PATCH 07/70] test(tga): initialize custom decode limits directly --- crates/tga/src/tests.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/tga/src/tests.rs b/crates/tga/src/tests.rs index 76472075f..887c2915a 100644 --- a/crates/tga/src/tests.rs +++ b/crates/tga/src/tests.rs @@ -240,8 +240,7 @@ fn written_files_read_back() { fn aggregate_decode_budget_applies_before_rle_expansion() { let mut file = header(10, 16, 16, 32, 0x28); file.extend_from_slice(&[255, 0, 0, 255, 255, 255, 0, 0, 255, 255]); - let mut limits = Limits::default(); - limits.max_decoded_bytes = 2047; + let mut limits = Limits { max_decoded_bytes: 2047, ..Limits::default() }; assert_eq!(decode(&file, &limits), Err(Error::LimitExceeded("decode allocation bytes"))); limits.max_decoded_bytes = 2048; assert_eq!(decode(&file, &limits).unwrap().rgba.len(), 1024); @@ -258,8 +257,7 @@ fn aggregate_decode_budget_counts_raw_and_palette_storage() { file[5] = 1; file[7] = 24; file.extend_from_slice(&[0, 0, 255, 0]); - let mut limits = Limits::default(); - limits.max_decoded_bytes = 8; + let mut limits = Limits { max_decoded_bytes: 8, ..Limits::default() }; assert_eq!(decode(&file, &limits), Err(Error::LimitExceeded("decode allocation bytes"))); limits.max_decoded_bytes = 9; assert_eq!(decode(&file, &limits).unwrap().rgba, [255, 0, 0, 255]); From 7b51909d40deb15397ec90695dc611bde5d13e34 Mon Sep 17 00:00:00 2001 From: Francesco BV Date: Sun, 15 Nov 2026 10:28:08 -0500 Subject: [PATCH 08/70] shx: preserve CADCraft shape compiler and errors Original source by Francesco BV in cadcraft52c1e6de, copied from main7064a685. App font lookup, text layout and glyph interpretation are not moved. --- crates/shx/src/compile.rs | 313 ++++++++++++++++++++++++++++++++++++++ crates/shx/src/error.rs | 29 ++++ 2 files changed, 342 insertions(+) create mode 100644 crates/shx/src/compile.rs create mode 100644 crates/shx/src/error.rs diff --git a/crates/shx/src/compile.rs b/crates/shx/src/compile.rs new file mode 100644 index 000000000..2e8cb3c25 --- /dev/null +++ b/crates/shx/src/compile.rs @@ -0,0 +1,313 @@ +//! Shape source (`.shp`) to a compiled SHX file, as COMPILE does. +//! +//! A source is a list of definitions: a header line `*number,defbytes,NAME` followed by its +//! specification bytes (on as many lines as needed, ending with 0). Numbers are decimal, or +//! hexadecimal after a leading `0` (`012`, `08cfb`); parentheses only group; `;` starts a +//! comment. A big font starts with `*BIGFONT nchars,nranges,first,last,…` and a Unicode font +//! with `*UNIFONT,6,name` followed by `above,below,modes,encoding,type,0`. Shape 0 is a text +//! font's record (`above,below,modes,0`). +//! +//! Operands are encoded by what they are: a subshape number takes two bytes in a Unicode font +//! (and after `7,0` in a big font), displacements and bulges are two's complement, and an arc's +//! direction byte (`-043`) keeps its sign in the high bit. `defbytes` is not checked: the +//! program's own length is written. + +use super::ShxError; +use super::parse::{BIGFONT, SHAPES_10, UNIFONT}; + +/// Largest source compiled. +const MAX_SOURCE: usize = 8 << 20; + +#[derive(Clone, Copy, PartialEq)] +enum Kind { + Shapes, + Big, + Uni, +} + +struct Shape { + number: u16, + name: String, + program: Vec, +} + +/// A number token: value (sign included). +fn number(tok: &str, line: usize) -> Result { + let t: String = tok.chars().filter(|c| !matches!(c, '(' | ')') && !c.is_whitespace()).collect(); + let (neg, digits) = match t.strip_prefix('-') { + Some(d) => (true, d), + None => (false, t.strip_prefix('+').unwrap_or(&t)), + }; + let err = || ShxError::Source(line, format!("not a number: {tok:?}")); + let v = if digits.len() > 1 && digits.starts_with('0') { + i64::from_str_radix(digits.get(1..).unwrap_or_default(), 16).map_err(|_| err())? + } else { + digits.parse::().map_err(|_| err())? + }; + if v > 0xFFFF { + return Err(err()); + } + Ok(if neg { -v } else { v }) +} + +/// The comma-separated numbers of a line (comment removed, blank fields skipped). +fn numbers(text: &str, line: usize) -> Result, ShxError> { + text.split(',').map(str::trim).filter(|t| !t.is_empty() && *t != "(" && *t != ")").map(|t| number(t, line)).collect() +} + +/// The spec-byte encoder: walks the tokens as the reader walks the bytes. +struct Enc<'a> { + toks: &'a [i64], + i: usize, + out: Vec, + kind: Kind, + line: usize, +} + +impl Enc<'_> { + fn next(&mut self) -> Result { + let v = self.toks.get(self.i).copied().ok_or_else(|| ShxError::Source(self.line, "the shape ends inside a command".into()))?; + self.i += 1; + Ok(v) + } + fn byte(&mut self) -> Result<(), ShxError> { + let v = self.next()?; + let b = u8::try_from(v).map_err(|_| ShxError::Source(self.line, format!("{v} is not a byte")))?; + self.out.push(b); + Ok(()) + } + /// Two's complement −128..127. + fn signed(&mut self) -> Result { + let v = self.next()?; + let b = i8::try_from(v).map_err(|_| ShxError::Source(self.line, format!("{v} is not −128..127")))?; + self.out.extend(b.to_le_bytes()); + Ok(v) + } + /// Sign in the high bit (an arc's direction byte). + fn sign_magnitude(&mut self) -> Result<(), ShxError> { + let v = self.next()?; + let m = u8::try_from(v.unsigned_abs()) + .ok() + .filter(|m| *m < 0x80) + .ok_or_else(|| ShxError::Source(self.line, format!("{v} is not an octant byte")))?; + self.out.push(if v < 0 { m | 0x80 } else { m }); + Ok(()) + } + fn word(&mut self) -> Result<(), ShxError> { + let v = self.next()?; + let w = u16::try_from(v).map_err(|_| ShxError::Source(self.line, format!("{v} is not a shape number")))?; + self.out.extend(w.to_be_bytes()); + Ok(()) + } + + fn program(mut self) -> Result, ShxError> { + while self.i < self.toks.len() { + let code = self.next()?; + let c = u8::try_from(code).map_err(|_| ShxError::Source(self.line, format!("{code} is not a code")))?; + self.out.push(c); + match c { + 0 => break, + 3 | 4 => self.byte()?, + 7 => match self.kind { + Kind::Uni => self.word()?, + Kind::Big => { + let first = self.toks.get(self.i).copied().unwrap_or(0); + if first == 0 { + self.byte()?; + self.word()?; + for _ in 0..4 { + self.byte()?; + } + } else { + self.byte()?; + } + } + Kind::Shapes => self.byte()?, + }, + 8 => { + self.signed()?; + self.signed()?; + } + 9 => loop { + let (x, y) = (self.signed()?, self.signed()?); + if x == 0 && y == 0 { + break; + } + }, + 10 => { + self.byte()?; + self.sign_magnitude()?; + } + 11 => { + for _ in 0..4 { + self.byte()?; + } + self.sign_magnitude()?; + } + 12 => { + for _ in 0..3 { + self.signed()?; + } + } + 13 => loop { + let (x, y) = (self.signed()?, self.signed()?); + if x == 0 && y == 0 { + break; + } + self.signed()?; + }, + _ => {} + } + } + if self.out.last() != Some(&0) { + self.out.push(0); + } + Ok(self.out) + } +} + +/// Compile shape source into an SHX file (a shape file or text font, a big font, or a Unicode +/// font, as its first definition says). +pub fn compile(source: &str) -> Result, ShxError> { + if source.len() > MAX_SOURCE { + return Err(ShxError::TooLarge); + } + let mut kind = Kind::Shapes; + let mut ranges: Vec<(u16, u16)> = Vec::new(); + let mut uni_info: Option<(String, Vec)> = None; + // Definitions: header (number, name, line) and the numbers that follow it. + let mut defs: Vec<(i64, String, usize, Vec)> = Vec::new(); + let mut uni_header_pending = false; + for (no, raw) in source.lines().enumerate() { + let line = no + 1; + let text = raw.split(';').next().unwrap_or_default().trim(); + if text.is_empty() { + continue; + } + if let Some(head) = text.strip_prefix('*') { + let upper = head.trim_start().to_ascii_uppercase(); + if let Some(rest) = upper.strip_prefix("BIGFONT") { + kind = Kind::Big; + let v = numbers(rest, line)?; + let n = usize::try_from(v.get(1).copied().unwrap_or(0)).unwrap_or(0); + 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); + let to = |x: i64| u16::try_from(x).map_err(|_| ShxError::Source(line, "bad escape range".into())); + ranges.push((to(lo)?, to(hi)?)); + } + continue; + } + if upper.starts_with("UNIFONT") { + kind = Kind::Uni; + let name = head.splitn(3, ',').nth(2).unwrap_or_default().trim().to_string(); + uni_info = Some((name, Vec::new())); + uni_header_pending = true; + continue; + } + uni_header_pending = false; + let mut parts = head.splitn(3, ','); + let num = number(parts.next().unwrap_or_default().trim(), line)?; + let _defbytes = parts.next(); + let name = parts.next().unwrap_or_default().trim().to_string(); + defs.push((num, name, line, Vec::new())); + continue; + } + let v = numbers(text, line)?; + if uni_header_pending { + if let Some((_, info)) = uni_info.as_mut() { + for x in v { + info.push(u8::try_from(x).map_err(|_| ShxError::Source(line, "bad font record".into()))?); + } + } + continue; + } + match defs.last_mut() { + Some(d) => d.3.extend(v), + None => return Err(ShxError::Source(line, "specification bytes before any *number,defbytes,NAME line".into())), + } + } + let mut shapes: Vec = Vec::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 { + // The font record: plain bytes. + toks.iter().map(|x| u8::try_from(*x).map_err(|_| ShxError::Source(line, "bad font record".into()))).collect::, _>>()? + } else { + Enc { toks: &toks, i: 0, out: Vec::new(), kind, line }.program()? + }; + if shapes.iter().any(|s| s.number == number) { + return Err(ShxError::Source(line, format!("shape {number} is defined twice"))); + } + shapes.push(Shape { number, name, program }); + } + if shapes.is_empty() { + return Err(ShxError::Empty); + } + shapes.sort_by_key(|s| s.number); + let record = |s: &Shape| -> Result, ShxError> { + let mut r: Vec = s.name.bytes().filter(|b| *b != 0).collect(); + r.push(0); + r.extend(&s.program); + if r.len() > usize::from(u16::MAX) { Err(ShxError::TooLarge) } else { Ok(r) } + }; + let count = u16::try_from(shapes.len()).map_err(|_| ShxError::TooLarge)?; + let len16 = |r: &[u8]| u16::try_from(r.len()).unwrap_or(u16::MAX); + let mut out = Vec::new(); + match kind { + Kind::Shapes => { + out.extend(SHAPES_10); + out.extend(shapes.first().map_or(0, |s| s.number).to_le_bytes()); + out.extend(shapes.last().map_or(0, |s| s.number).to_le_bytes()); + out.extend(count.to_le_bytes()); + let recs: Vec> = shapes.iter().map(record).collect::>()?; + for (s, r) in shapes.iter().zip(&recs) { + out.extend(s.number.to_le_bytes()); + out.extend(len16(r).to_le_bytes()); + } + for r in &recs { + out.extend(r); + } + out.extend(b"EOF"); + } + Kind::Big => { + out.extend(BIGFONT); + let nranges = u16::try_from(ranges.len()).map_err(|_| ShxError::TooLarge)?; + out.extend(count.to_le_bytes()); + out.extend(count.to_le_bytes()); + out.extend(nranges.to_le_bytes()); + for (lo, hi) in &ranges { + out.extend(lo.to_le_bytes()); + out.extend(hi.to_le_bytes()); + } + let recs: Vec> = shapes.iter().map(record).collect::>()?; + let mut off = out.len() + 8 * recs.len(); + for (s, r) in shapes.iter().zip(&recs) { + out.extend(s.number.to_le_bytes()); + out.extend(len16(r).to_le_bytes()); + out.extend(u32::try_from(off).map_err(|_| ShxError::TooLarge)?.to_le_bytes()); + off += r.len(); + } + for r in &recs { + out.extend(r); + } + } + Kind::Uni => { + out.extend(UNIFONT); + let (name, info) = uni_info.unwrap_or_default(); + out.extend((u32::from(count) + 1).to_le_bytes()); + let mut rec: Vec = name.bytes().filter(|b| *b != 0).collect(); + rec.push(0); + rec.extend(&info); + out.extend(len16(&rec).to_le_bytes()); + out.extend(&rec); + for s in &shapes { + let r = record(s)?; + out.extend(s.number.to_le_bytes()); + out.extend(len16(&r).to_le_bytes()); + out.extend(r); + } + } + } + Ok(out) +} diff --git a/crates/shx/src/error.rs b/crates/shx/src/error.rs new file mode 100644 index 000000000..0addd1c98 --- /dev/null +++ b/crates/shx/src/error.rs @@ -0,0 +1,29 @@ +/// Why a font file can't be read or a shape source can't be compiled. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ShxError { + /// Not an SHX file (unknown signature). + NotShx, + /// The file ends inside its header or index. + Truncated, + /// The file is larger, or has more of something, than CADCraft reads. + TooLarge, + /// The file is well-formed but holds no shape. + Empty, + /// Shape source error: line and message. + Source(usize, String), +} + +impl std::fmt::Display for ShxError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + ShxError::NotShx => f.write_str("not an SHX shape or font file"), + ShxError::Truncated => f.write_str("the SHX file is truncated"), + ShxError::TooLarge => f.write_str("the SHX file is too large"), + ShxError::Empty => f.write_str("the SHX file has no shapes"), + ShxError::Source(line, msg) => write!(f, "line {line}: {msg}"), + } + } +} + +impl std::error::Error for ShxError {} + From b3307f7814629961841b80ab907116252e41b34d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 10:30:48 -0500 Subject: [PATCH 09/70] shx: expose shared bounded shape compiler with hostile-source tests --- Cargo.lock | 4 ++++ Cargo.toml | 1 + crates/shx/Cargo.toml | 11 +++++++++++ crates/shx/README.md | 8 ++++++++ crates/shx/src/compile.rs | 20 +++++++++++++++++--- crates/shx/src/error.rs | 1 - crates/shx/src/lib.rs | 8 ++++++++ crates/shx/tests/compiler.rs | 30 ++++++++++++++++++++++++++++++ 8 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 crates/shx/Cargo.toml create mode 100644 crates/shx/README.md create mode 100644 crates/shx/src/lib.rs create mode 100644 crates/shx/tests/compiler.rs 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)); +} From a2541707240580153c8ea9ef3043ec0257468d0a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:48:40 +0000 Subject: [PATCH 10/70] Share WebP header inspection --- crates/image-codecs/src/lib.rs | 1 + crates/image-codecs/src/webp_probe.rs | 47 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 crates/image-codecs/src/webp_probe.rs diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 6516b9ff2..961a04327 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -31,6 +31,7 @@ pub mod orientation; pub mod resolution; pub mod source_settings; pub mod web; +pub mod webp_probe; pub use crate::allocation::probe_allocation_size; pub use crate::cicp::Cicp; pub use crate::cicp::ContentLightLevel; diff --git a/crates/image-codecs/src/webp_probe.rs b/crates/image-codecs/src/webp_probe.rs new file mode 100644 index 000000000..8376274df --- /dev/null +++ b/crates/image-codecs/src/webp_probe.rs @@ -0,0 +1,47 @@ +//! Header-only WebP inspection with image-chunk completeness checks. + +use crate::{CodecError, Format}; + +/// Stored width, height and EXIF orientation without decoding pixels. The image payload must +/// fit in the input, but its compressed contents are not validated. Uses the default decode limits. +pub fn header(bytes: &[u8]) -> Result<(u32, u32, u16), CodecError> { + let err = |s: &str| CodecError::malformed(Format::WebP, s); + let mut p = 12usize; + let mut has_image = false; + let mut exif = None; + while let Some(h) = bytes.get(p..p.saturating_add(8)) { + let size = u32::from_le_bytes([h[4], h[5], h[6], h[7]]) as usize; + let end = p.saturating_add(8).saturating_add(size); + if matches!(&h[..4], b"VP8 " | b"VP8L" | b"ANMF") { + if end > bytes.len() { + return Err(err("truncated image data")); + } + has_image = true; + } else if &h[..4] == b"EXIF" && end <= bytes.len() && exif.is_none() { + exif = bytes.get(p + 8..end); + } + p = end.saturating_add(size & 1); + } + if !has_image { + return Err(err("no image data")); + } + let (w, h) = crate::probe_size(bytes)?; + let orientation = exif.map_or(1, crate::exif_orientation); + let (w, h) = if (5..=8).contains(&orientation) { (h, w) } else { (w, h) }; + Ok((w, h, orientation)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn rejects_missing_and_truncated_image_chunks() { + let mut bytes = b"RIFF\x00\x00\x00\x00WEBP".to_vec(); + assert!(header(&bytes).is_err()); + bytes.extend_from_slice(b"VP8 "); + bytes.extend_from_slice(&10u32.to_le_bytes()); + bytes.push(0); + assert!(header(&bytes).is_err()); + } +} From 3c5007339754def577b3c5274f523d96384a35c4 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:48:41 +0000 Subject: [PATCH 11/70] Share bounded SHX container reader --- crates/shx/src/lib.rs | 2 + crates/shx/src/parse.rs | 281 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 283 insertions(+) create mode 100644 crates/shx/src/parse.rs diff --git a/crates/shx/src/lib.rs b/crates/shx/src/lib.rs index e595e90f5..ce330450c 100644 --- a/crates/shx/src/lib.rs +++ b/crates/shx/src/lib.rs @@ -4,5 +4,7 @@ //! Font discovery, glyph execution and text layout remain consumer responsibilities. mod compile; mod error; +pub mod parse; pub use compile::compile; pub use error::ShxError; +pub use parse::{ParsedFont, Record, ShxKind}; diff --git a/crates/shx/src/parse.rs b/crates/shx/src/parse.rs new file mode 100644 index 000000000..9cd4a35ad --- /dev/null +++ b/crates/shx/src/parse.rs @@ -0,0 +1,281 @@ +//! Reading the compiled SHX containers (layouts in the module documentation). Every count and +//! offset comes from the file, so each is checked against its length; records that run past +//! the end are dropped and the rest kept. + +use super::ShxError; +use std::collections::HashMap; + +/// The SHX container kind. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ShxKind { + Shapes, + BigFont, + Unifont, +} +/// Byte ranges of one shape name and pen program in [`ParsedFont::data`]. +#[derive(Clone, Copy, Debug)] +pub struct Record { + pub name: (usize, usize), + pub program: (usize, usize), +} +/// Decoded container records and metrics; glyph execution remains a caller concern. +pub struct ParsedFont { + pub kind: ShxKind, + pub name: String, + pub header: bool, + pub above: f64, + pub below: f64, + pub dual: bool, + pub encoding: u8, + pub char_width: f64, + pub ranges: Vec<(u16, u16)>, + pub data: Vec, + pub records: HashMap, +} + +pub const SHAPES_10: &[u8] = b"AutoCAD-86 shapes 1.0\r\n\x1a"; +pub const SHAPES_11: &[u8] = b"AutoCAD-86 shapes 1.1\r\n\x1a"; +pub const BIGFONT: &[u8] = b"AutoCAD-86 bigfont 1.0\r\n\x1a"; +pub const UNIFONT: &[u8] = b"AutoCAD-86 unifont 1.0\r\n\x1a"; + +/// Largest file read (the largest big fonts are a few megabytes). +pub const MAX_FILE_BYTES: usize = 32 << 20; +/// Most escape ranges in a big font (lead bytes: at most 256). +const MAX_RANGES: usize = 256; + +/// A checked little-endian reader. +struct Rd<'a> { + b: &'a [u8], + pos: usize, +} + +impl<'a> Rd<'a> { + fn take(&mut self, n: usize) -> Result<&'a [u8], ShxError> { + let end = self.pos.checked_add(n).ok_or(ShxError::Truncated)?; + let s = self.b.get(self.pos..end).ok_or(ShxError::Truncated)?; + self.pos = end; + Ok(s) + } + fn u16(&mut self) -> Result { + match self.take(2)? { + [a, b] => Ok(u16::from_le_bytes([*a, *b])), + _ => Err(ShxError::Truncated), + } + } + fn u32(&mut self) -> Result { + match self.take(4)? { + [a, b, c, d] => Ok(u32::from_le_bytes([*a, *b, *c, *d])), + _ => Err(ShxError::Truncated), + } + } +} + +/// A font being read. +struct Font { + kind: ShxKind, + name: String, + header: bool, + above: f64, + below: f64, + dual: bool, + encoding: u8, + char_width: f64, + ranges: Vec<(u16, u16)>, + records: HashMap, +} + +impl Font { + fn new(kind: ShxKind) -> Font { + Font { + kind, + name: String::new(), + header: false, + above: 0.0, + below: 0.0, + dual: false, + encoding: 0, + char_width: 0.0, + ranges: Vec::new(), + records: HashMap::new(), + } + } + + /// Add the record of shape `number` at `start..end` of `data`: a NUL-terminated name, then + /// the program. Shape 0 is the font record. A record without a name terminator is dropped; + /// the first record of a number wins. + fn record(&mut self, data: &[u8], number: u16, start: usize, end: usize) { + let Some(bytes) = data.get(start..end) else { return }; + let Some(nul) = bytes.iter().position(|b| *b == 0) else { return }; + let name = (start, start + nul); + let program = (start + nul + 1, end); + if number == 0 { + if !self.header { + self.font_record(data.get(program.0..program.1).unwrap_or_default(), data.get(name.0..name.1).unwrap_or_default()); + } + return; + } + self.records.entry(number).or_insert(Record { name, program }); + } + + /// The font record: above, below, modes (and for a unifont encoding and embedding); an + /// extended big font's is character height, 0, modes, character width. + fn font_record(&mut self, info: &[u8], name: &[u8]) { + self.header = true; + self.name = name.iter().take(255).map(|c| char::from(*c)).collect(); + let byte = |i: usize| info.get(i).copied().unwrap_or(0); + self.above = f64::from(byte(0)); + self.below = f64::from(byte(1)); + self.dual = byte(2) == 2; + if self.kind == ShxKind::Unifont { + self.encoding = byte(3); + } + if self.kind == ShxKind::BigFont && info.len() >= 5 && byte(1) == 0 { + // Extended: height, 0, modes, width, 0. + self.below = 0.0; + self.char_width = f64::from(byte(3)); + } + } + + fn finish(self, data: Vec) -> Result { + if self.records.is_empty() { + return Err(ShxError::Empty); + } + Ok(ParsedFont { + kind: self.kind, + name: self.name, + header: self.header, + above: self.above, + below: self.below, + dual: self.dual, + encoding: self.encoding, + char_width: if self.char_width > 0.0 { self.char_width } else { self.above }, + ranges: self.ranges, + data, + records: self.records, + }) + } +} + +pub fn parse(bytes: &[u8]) -> Result { + if bytes.len() > MAX_FILE_BYTES { + return Err(ShxError::TooLarge); + } + if bytes.starts_with(SHAPES_10) || bytes.starts_with(SHAPES_11) { + shapes(bytes) + } else if bytes.starts_with(BIGFONT) { + bigfont(bytes) + } else if bytes.starts_with(UNIFONT) { + unifont(bytes) + } else { + Err(ShxError::NotShx) + } +} + +fn shapes(bytes: &[u8]) -> Result { + let mut r = Rd { b: bytes, pos: SHAPES_10.len() }; + let _first = r.u16()?; + let _last = r.u16()?; + let count = usize::from(r.u16()?); + // The whole index must be there before anything is read from it. + let index = r.take(count.checked_mul(4).ok_or(ShxError::TooLarge)?)?; + let mut f = Font::new(ShxKind::Shapes); + let mut pos = r.pos; + for [a, b, c, d] in index.as_chunks::<4>().0 { + let (number, len) = (u16::from_le_bytes([*a, *b]), usize::from(u16::from_le_bytes([*c, *d]))); + let Some(end) = pos.checked_add(len).filter(|e| *e <= bytes.len()) else { break }; + f.record(bytes, number, pos, end); + pos = end; + } + f.finish(bytes.to_vec()) +} + +fn bigfont(bytes: &[u8]) -> Result { + let mut r = Rd { b: bytes, pos: BIGFONT.len() }; + let index_size = usize::from(r.u16()?); + let count = usize::from(r.u16()?); + let nranges = usize::from(r.u16()?); + if nranges > MAX_RANGES { + return Err(ShxError::TooLarge); + } + let mut f = Font::new(ShxKind::BigFont); + for _ in 0..nranges { + let (lo, hi) = (r.u16()?, r.u16()?); + f.ranges.push((lo.min(hi), lo.max(hi))); + } + // The index has `count` entries of 8 bytes; a file whose count doesn't fit but whose index + // size does is read by that. + let fits = |n: usize| n.checked_mul(8).and_then(|l| r.pos.checked_add(l)).is_some_and(|e| e <= bytes.len()); + let n = if fits(count) { + count + } else if fits(index_size) { + index_size + } else { + return Err(ShxError::Truncated); + }; + let index = r.take(n.checked_mul(8).ok_or(ShxError::TooLarge)?)?; + for [a, b, c, d, o0, o1, o2, o3] in index.as_chunks::<8>().0 { + let number = u16::from_le_bytes([*a, *b]); + let len = usize::from(u16::from_le_bytes([*c, *d])); + let Ok(off) = usize::try_from(u32::from_le_bytes([*o0, *o1, *o2, *o3])) else { continue }; + if len == 0 { + continue; + } + let Some(end) = off.checked_add(len).filter(|e| *e <= bytes.len()) else { continue }; + f.record(bytes, number, off, end); + } + f.finish(bytes.to_vec()) +} + +fn unifont(bytes: &[u8]) -> Result { + let mut r = Rd { b: bytes, pos: UNIFONT.len() }; + let count = r.u32()?; + let info_len = usize::from(r.u16()?); + let info = r.take(info_len)?; + let mut f = Font::new(ShxKind::Unifont); + let nul = info.iter().position(|b| *b == 0).unwrap_or(info.len()); + f.font_record(info.get(nul.saturating_add(1)..).unwrap_or_default(), info.get(..nul).unwrap_or_default()); + // `count` includes the font record; never trusted beyond the data that is there. + let mut left = count.saturating_sub(1); + while left > 0 { + left -= 1; + let Ok(number) = r.u16() else { break }; + let Ok(len) = r.u16().map(usize::from) else { break }; + let start = r.pos; + let Some(end) = start.checked_add(len).filter(|e| *e <= bytes.len()) else { break }; + r.pos = end; + f.record(bytes, number, start, end); + } + f.finish(bytes.to_vec()) +} + +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used)] + use super::*; + #[test] + fn reads_shapes_bigfont_and_unifont_records() { + for (source, kind, shape) in [ + ("*0,4,font\n6,2,2,0\n*65,4,A\n1,064,0\n", ShxKind::Shapes, 65), + ("*BIGFONT 2,1,081,09F\n*0,4,big\n8,0,0,0\n*082A0,4,square\n1,080,0\n", ShxKind::BigFont, 0x82a0), + ("*UNIFONT,6,uni\n6,2,0,0,0,0\n*00041,4,a\n1,064,0\n", ShxKind::Unifont, 65), + ] { + let bytes = crate::compile(source).unwrap(); + let font = parse(&bytes).unwrap(); + assert_eq!(font.kind, kind); + assert!(font.header); + let record = font.records.get(&shape).unwrap(); + assert_eq!(font.data.get(record.program.1 - 1), Some(&0)); + } + } + #[test] + fn truncation_limits_and_unknown_signatures_are_errors() { + assert!(matches!(parse(b"not a font"), Err(ShxError::NotShx))); + assert!(matches!(parse(&vec![0; MAX_FILE_BYTES + 1]), Err(ShxError::TooLarge))); + let mut shapes = SHAPES_10.to_vec(); + shapes.extend([1, 0, 255, 255, 255, 255]); + assert!(matches!(parse(&shapes), Err(ShxError::Truncated))); + let mut big = BIGFONT.to_vec(); + big.extend([255, 255, 255, 255, 255, 255]); + assert!(matches!(parse(&big), Err(ShxError::TooLarge))); + } +} From b919ac40f3873fd3893c7ad97e6351940714c366 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 10:48:46 -0500 Subject: [PATCH 12/70] test(tga): validate metadata fixture selector explicitly --- crates/tga/src/metadata_tests.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/tga/src/metadata_tests.rs b/crates/tga/src/metadata_tests.rs index 583f5d691..85c434bf4 100644 --- a/crates/tga/src/metadata_tests.rs +++ b/crates/tga/src/metadata_tests.rs @@ -163,6 +163,7 @@ fn complete_tga_footer_and_metadata_do_not_change_pixels() { } fn preceding_metadata(complete: bool, metadata: usize) -> Vec { + assert!(metadata <= 3, "test metadata selector must be 0..=3"); let mut file = header(2, if complete { 1 } else { 2 }, 1, 24, 0x20); file.extend_from_slice(&[0, 0, 255]); let payload_offset = file.len() as u32; @@ -180,7 +181,7 @@ fn preceding_metadata(complete: bool, metadata: usize) -> Vec { 1 => file.extend_from_slice(&[0; 2048]), 2 => file.extend_from_slice(&[1, 1, 0, 255, 0]), 3 => file.extend_from_slice(&18u32.to_le_bytes()), - _ => unreachable!(), + _ => {} } let extension_offset = file.len() as u32; let mut extension = [0; 495]; From 8a1bad8c2cc47f71c2a75fb19b67b54762fe8389 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:48:56 +0000 Subject: [PATCH 13/70] Cover WebP stored orientation --- crates/image-codecs/src/webp_probe.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/image-codecs/src/webp_probe.rs b/crates/image-codecs/src/webp_probe.rs index 8376274df..d70fac38d 100644 --- a/crates/image-codecs/src/webp_probe.rs +++ b/crates/image-codecs/src/webp_probe.rs @@ -35,6 +35,14 @@ pub fn header(bytes: &[u8]) -> Result<(u32, u32, u16), CodecError> { mod tests { use super::*; + #[test] + fn preserves_stored_dimensions_with_exif_orientation() { + let mut image = crate::Image::from_u8(2, 3, crate::ChannelLayout::Rgb, vec![42; 18]).unwrap(); + image.meta.exif = Some(b"II\x2a\x00\x08\x00\x00\x00\x01\x00\x12\x01\x03\x00\x01\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00".to_vec()); + let bytes = crate::encode(&image, Format::WebP, &crate::EncodeOptions::default()).unwrap(); + assert_eq!(header(&bytes).unwrap(), (2, 3, 6)); + } + #[test] fn rejects_missing_and_truncated_image_chunks() { let mut bytes = b"RIFF\x00\x00\x00\x00WEBP".to_vec(); From 82e258ab6eae4f4d6cd9dfe463a8640fd20c96d9 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:50:14 +0000 Subject: [PATCH 14/70] Exercise camera orientation in WebP fixture --- crates/image-codecs/src/webp_probe.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/image-codecs/src/webp_probe.rs b/crates/image-codecs/src/webp_probe.rs index d70fac38d..8e33a71cf 100644 --- a/crates/image-codecs/src/webp_probe.rs +++ b/crates/image-codecs/src/webp_probe.rs @@ -39,7 +39,11 @@ mod tests { fn preserves_stored_dimensions_with_exif_orientation() { let mut image = crate::Image::from_u8(2, 3, crate::ChannelLayout::Rgb, vec![42; 18]).unwrap(); image.meta.exif = Some(b"II\x2a\x00\x08\x00\x00\x00\x01\x00\x12\x01\x03\x00\x01\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00".to_vec()); - let bytes = crate::encode(&image, Format::WebP, &crate::EncodeOptions::default()).unwrap(); + let mut bytes = crate::encode(&image, Format::WebP, &crate::EncodeOptions::default()).unwrap(); + // The writer correctly makes exported pixels upright; simulate a camera file carrying orientation 6. + let exif = bytes.windows(4).position(|v| v == b"EXIF").unwrap(); + let tiff = exif + 8; + bytes[tiff + 18] = 6; assert_eq!(header(&bytes).unwrap(), (2, 3, 6)); } From 5ae0c89fdfca73c924db0521e2772e67c6cd49d3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:50:50 +0000 Subject: [PATCH 15/70] audio: share compatible PCM essence wrappers --- crates/audio/src/essence_pcm.rs | 147 ++++++++++++++++++++++++++++++++ crates/audio/src/lib.rs | 4 + 2 files changed, 151 insertions(+) create mode 100644 crates/audio/src/essence_pcm.rs diff --git a/crates/audio/src/essence_pcm.rs b/crates/audio/src/essence_pcm.rs new file mode 100644 index 000000000..593ebb1c2 --- /dev/null +++ b/crates/audio/src/essence_pcm.rs @@ -0,0 +1,147 @@ +//! Compatibility WAVE and AIFF wrappers for embedded PCM essence in interchange files. +//! PCM inside the model is interleaved little-endian signed integers (8-bit unsigned for WAVE). + +pub use crate::{pcm_aiff_header as aiff_header, pcm_wav_file as wav_file, pcm_wav_header as wav_header}; + +/// PCM, channels, sample rate and bits of a WAVE file (integer PCM only). +pub fn parse_wav(b: &[u8]) -> Option<(Vec, u16, u32, u16)> { + if b.len() < 12 || &b[..4] != b"RIFF" || &b[8..12] != b"WAVE" { + return None; + } + let mut at = 12; + let mut fmt = None; + while at + 8 <= b.len() { + let id = &b[at..at + 4]; + let len = u32::from_le_bytes(b[at + 4..at + 8].try_into().ok()?) as usize; + let body = &b[at + 8..(at + 8).saturating_add(len).min(b.len())]; + if id == b"fmt " && body.len() >= 16 { + let tag = u16::from_le_bytes([body[0], body[1]]); + let ch = u16::from_le_bytes([body[2], body[3]]); + let sr = u32::from_le_bytes(body[4..8].try_into().ok()?); + let bits = u16::from_le_bytes([body[14], body[15]]); + if tag != 1 && tag != 0xFFFE { + return None; + } + if tag == 0xFFFE { + // The first two bytes of the sub-format GUID carry the real format code. + let sub = body.get(24..26)?; + if u16::from_le_bytes([sub[0], sub[1]]) != 1 { + return None; + } + } + fmt = Some((ch, sr, bits)); + } else if id == b"data" { + let (ch, sr, bits) = fmt?; + return Some((body.to_vec(), ch, sr, bits)); + } + at = at.checked_add(8usize.checked_add(len)?.checked_add(len & 1)?)?; + } + None +} + +/// A complete AIFF file (big-endian PCM) from little-endian PCM. +pub fn aiff_file(pcm: &[u8], channels: u16, sample_rate: u32, bits: u16) -> Vec { + let bps = bits.div_ceil(8) as usize; + let frames = pcm.len() / (bps * channels.max(1) as usize).max(1); + let mut v = aiff_header(frames as u32, channels, sample_rate, bits); + for s in pcm.chunks_exact(bps.max(1)) { + if bps == 1 { + v.push(s[0] ^ 0x80); // WAVE 8-bit is unsigned, AIFF signed + } else { + v.extend(s.iter().rev()); + } + } + if (v.len() - 8) & 1 == 1 { + v.push(0); + } + v +} + +/// PCM (converted to little-endian), channels, sample rate and bits of an AIFF / AIFF-C (`NONE` +/// / `twos` / `sowt`) file. +pub fn parse_aiff(b: &[u8]) -> Option<(Vec, u16, u32, u16)> { + if b.len() < 12 || &b[..4] != b"FORM" || !(&b[8..12] == b"AIFF" || &b[8..12] == b"AIFC") { + return None; + } + let mut at = 12; + let mut comm = None; + while at + 8 <= b.len() { + let id = &b[at..at + 4]; + let len = u32::from_be_bytes(b[at + 4..at + 8].try_into().ok()?) as usize; + let body = &b[at + 8..(at + 8).saturating_add(len).min(b.len())]; + if id == b"COMM" && body.len() >= 18 { + let ch = u16::from_be_bytes([body[0], body[1]]); + let bits = u16::from_be_bytes([body[6], body[7]]); + let sr = from_extended(body[8..18].try_into().ok()?).round() as u32; + let little = body.len() >= 22 && &body[18..22] == b"sowt"; + comm = Some((ch, sr, bits, little)); + } else if id == b"SSND" && body.len() >= 8 { + let (ch, sr, bits, little) = comm?; + let off = u32::from_be_bytes(body[..4].try_into().ok()?) as usize; + let data = body.get(8usize.checked_add(off)?..)?; + let bps = bits.div_ceil(8) as usize; + let mut pcm = Vec::with_capacity(data.len()); + for s in data.chunks_exact(bps.max(1)) { + if bps == 1 { + pcm.push(s[0] ^ 0x80); + } else if little { + pcm.extend_from_slice(s); + } else { + pcm.extend(s.iter().rev()); + } + } + return Some((pcm, ch, sr, bits)); + } + at = at.checked_add(8usize.checked_add(len)?.checked_add(len & 1)?)?; + } + None +} + +fn from_extended(b: [u8; 10]) -> f64 { + let e = (u16::from_be_bytes([b[0], b[1]]) & 0x7FFF) as i32; + let m = u64::from_be_bytes(b[2..10].try_into().unwrap_or([0; 8])); + if e == 0 && m == 0 { + return 0.0; + } + m as f64 / (1u64 << 63) as f64 * 2f64.powi(e - 16383) +} + +#[cfg(test)] +mod tests { + /// A mutated EDL whose clip ran for days produced an OMF descriptor for more than 4 GiB of + /// PCM: the RIFF size field overflowed ("attempt to add with overflow"). + #[test] + fn huge_wav_header_saturates() { + let h = super::wav_header(u32::MAX, 2, u32::MAX, 24); + assert_eq!(&h[4..8], &u32::MAX.to_le_bytes()); + } + + use super::*; + + #[test] + fn accepts_truncated_essence_summaries() { + let mut wav = wav_file(&[1, 2, 3, 4], 1, 48_000, 16); + wav.truncate(46); + assert_eq!(parse_wav(&wav), Some((vec![1, 2], 1, 48_000, 16))); + let mut aiff = aiff_file(&[1, 2, 3, 4], 1, 48_000, 16); + aiff.truncate(56); + assert_eq!(parse_aiff(&aiff), Some((vec![1, 2], 1, 48_000, 16))); + } + + #[test] + fn wav_and_aiff_round_trip() { + let pcm: Vec = (0..48u8).collect(); // 8 stereo 24-bit frames + let w = wav_file(&pcm, 2, 48_000, 24); + assert_eq!(parse_wav(&w), Some((pcm.clone(), 2, 48_000, 24))); + let a = aiff_file(&pcm, 2, 44_100, 24); + assert_eq!(parse_aiff(&a), Some((pcm.clone(), 2, 44_100, 24))); + let p16: Vec = (0..32u8).collect(); + assert_eq!(parse_aiff(&aiff_file(&p16, 1, 96_000, 16)), Some((p16, 1, 96_000, 16))); + assert!(parse_wav(b"RIFF").is_none()); + assert!(parse_aiff(&a[..30]).is_none()); + for cut in 0..w.len() { + let _ = parse_wav(&w[..cut]); + let _ = parse_aiff(&a[..cut.min(a.len())]); + } + } +} diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index 33dcdee3b..7bbfbbcf5 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -66,6 +66,10 @@ mod wav; feature = "vorbis-encode" ))] mod write; + +/// PCM WAVE and AIFF wrappers for interchange essence. +#[cfg(feature = "pcm-encode")] +pub mod essence_pcm; #[cfg(feature = "vorbis-encode")] pub use write::{OGG_BITRATES, encode_ogg}; From 2f2d1d84e3a36b3f93e81507aeff3f0da97a7162 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:51:27 +0000 Subject: [PATCH 16/70] Share bounded SHX glyph program interpreter --- crates/shx/src/lib.rs | 1 + crates/shx/src/program.rs | 443 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 444 insertions(+) create mode 100644 crates/shx/src/program.rs diff --git a/crates/shx/src/lib.rs b/crates/shx/src/lib.rs index ce330450c..e330e59fd 100644 --- a/crates/shx/src/lib.rs +++ b/crates/shx/src/lib.rs @@ -5,6 +5,7 @@ mod compile; mod error; pub mod parse; +pub mod program; pub use compile::compile; pub use error::ShxError; pub use parse::{ParsedFont, Record, ShxKind}; diff --git a/crates/shx/src/program.rs b/crates/shx/src/program.rs new file mode 100644 index 000000000..9115d1c6d --- /dev/null +++ b/crates/shx/src/program.rs @@ -0,0 +1,443 @@ +//! The shape pen program: vector length/direction bytes and special codes 0–14, run into +//! polylines. Bounded everywhere: subshape depth, location stack, steps, points, coordinates. +//! +//! - A byte with a non-zero high nibble is a vector: length (high nibble) in one of 16 +//! directions (low nibble, counter-clockwise from east in 22.5° steps; the off-axis ones +//! are half-unit or unit steps on the minor axis). +//! - 0 end, 1 pen down, 2 pen up, 3/4 divide/multiply vector lengths by the next byte, +//! 5/6 push/pop the location, 7 subshape (one byte; two bytes high first in a Unicode font; +//! `0`, a two-byte number, base point, width and height in an extended big font), +//! 8 one X-Y displacement, 9 displacements up to (0,0), 10 octant arc (radius, +//! ±0SC: sign = clockwise, S start octant, C octants, 0 = 8), 11 fractional arc (start +//! offset, end offset, high and low radius, ±0SC; offsets in 1/256 of 45° from the octant +//! boundaries), 12 bulge arc (dx, dy, bulge: ±127 = semicircle), 13 bulge arcs up to (0,0), +//! 14 the next command is for vertical text only (dual-orientation fonts). +//! - Displacements and bulges are two's complement bytes; the arc direction byte keeps its +//! sign in the high bit. + +use std::f64::consts::{FRAC_PI_4, TAU}; + +use super::ShxKind; +use std::ops::{Add, Mul, Neg, Sub}; + +/// A point in SHX font units. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct Point { + pub x: f64, + pub y: f64, +} +impl Point { + pub const ZERO: Self = Self { x: 0.0, y: 0.0 }; + pub const fn new(x: f64, y: f64) -> Self { + Self { x, y } + } + fn is_finite(self) -> bool { + self.x.is_finite() && self.y.is_finite() + } + fn len(self) -> f64 { + self.x.hypot(self.y) + } + fn len2(self) -> f64 { + self.x * self.x + self.y * self.y + } +} +impl Add for Point { + type Output = Self; + fn add(self, rhs: Self) -> Self { + Self::new(self.x + rhs.x, self.y + rhs.y) + } +} +impl Sub for Point { + type Output = Self; + fn sub(self, rhs: Self) -> Self { + Self::new(self.x - rhs.x, self.y - rhs.y) + } +} +impl Mul for Point { + type Output = Self; + fn mul(self, rhs: f64) -> Self { + Self::new(self.x * rhs, self.y * rhs) + } +} +impl Neg for Point { + type Output = Self; + fn neg(self) -> Self { + Self::new(-self.x, -self.y) + } +} + +/// Font records and metrics the bounded shape interpreter needs. +pub trait Font { + fn kind(&self) -> ShxKind; + fn dual(&self) -> bool; + fn above(&self) -> f64; + fn char_width(&self) -> f64; + fn program(&self, number: u16) -> Option<&[u8]>; +} +/// Pen-down strokes and advance in font units. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Glyph { + pub strokes: Vec>, + pub advance: Point, +} + +/// Subshape nesting (a cycle ends here). +const MAX_DEPTH: usize = 16; +/// Location stack (the format promises four). +const MAX_STACK: usize = 64; +/// Bytes run per glyph, subshapes included. +const MAX_STEPS: usize = 200_000; +/// Points per glyph. +const MAX_POINTS: usize = 50_000; +/// Coordinates in font units. +const MAX_COORD: f64 = 1.0e7; +/// Arc segments per full turn. +const ARC_STEPS: f64 = 64.0; + +/// Why a program stops early: the glyph is then not drawn. +#[derive(Debug)] +pub struct Broken; + +type Res = Result<(), Broken>; + +struct Pen<'a> { + font: &'a dyn Font, + vertical: bool, + pos: Point, + down: bool, + /// Vector length factor (codes 3 and 4). + scale: f64, + /// Per-axis factor of an extended big font subshape. + axes: Point, + stack: Vec, + strokes: Vec>, + active: Vec, + steps: usize, + points: usize, +} + +pub fn outline(font: &dyn Font, number: u16, vertical: bool) -> Result { + let mut pen = Pen { + font, + vertical, + pos: Point::ZERO, + down: true, + scale: 1.0, + axes: Point::new(1.0, 1.0), + stack: Vec::new(), + strokes: Vec::new(), + active: Vec::new(), + steps: 0, + points: 0, + }; + pen.run(number, 0)?; + pen.flush(); + Ok(Glyph { strokes: pen.strokes, advance: pen.pos }) +} + +/// The 16 vector directions (unit length on the major axis). +fn direction(d: u8) -> Point { + let (x, y) = match d & 15 { + 0 => (1.0, 0.0), + 1 => (1.0, 0.5), + 2 => (1.0, 1.0), + 3 => (0.5, 1.0), + 4 => (0.0, 1.0), + 5 => (-0.5, 1.0), + 6 => (-1.0, 1.0), + 7 => (-1.0, 0.5), + 8 => (-1.0, 0.0), + 9 => (-1.0, -0.5), + 10 => (-1.0, -1.0), + 11 => (-0.5, -1.0), + 12 => (0.0, -1.0), + 13 => (0.5, -1.0), + 14 => (1.0, -1.0), + _ => (1.0, -0.5), + }; + Point::new(x, y) +} + +/// A two's complement operand byte. +fn signed(b: u8) -> f64 { + f64::from(i8::from_le_bytes([b])) +} + +/// A program being read: its bytes and position. +struct Cur<'p> { + b: &'p [u8], + pos: usize, +} + +impl Cur<'_> { + fn byte(&mut self) -> Result { + let v = self.b.get(self.pos).copied().ok_or(Broken)?; + self.pos += 1; + Ok(v) + } +} + +impl Pen<'_> { + fn step(&mut self) -> Res { + self.steps += 1; + if self.steps > MAX_STEPS { Err(Broken) } else { Ok(()) } + } + + fn flush(&mut self) { + if self.active.len() >= 2 { + self.strokes.push(std::mem::take(&mut self.active)); + } else { + self.active.clear(); + } + } + + /// Move the pen by `d` font units (already scaled), drawing when the pen is down. + fn to(&mut self, target: Point) -> Res { + if !target.is_finite() || target.x.abs().max(target.y.abs()) > MAX_COORD { + return Err(Broken); + } + if self.down { + if self.active.is_empty() { + self.active.push(self.pos); + self.points += 1; + } + self.active.push(target); + self.points += 1; + if self.points > MAX_POINTS { + return Err(Broken); + } + } else { + self.flush(); + } + self.pos = target; + Ok(()) + } + + /// A displacement in vector units (times the length factor and the subshape axes). + fn by(&mut self, d: Point) -> Res { + let k = d * self.scale; + self.to(self.pos + Point::new(k.x * self.axes.x, k.y * self.axes.y)) + } + + /// An arc of `radius` vector units from the pen, starting at angle `start` on its circle + /// and turning `sweep` radians (positive counter-clockwise). + fn arc(&mut self, radius: f64, start: f64, sweep: f64) -> Res { + let r = radius * self.scale; + if !(r.is_finite() && r > 0.0 && sweep.is_finite()) { + return Ok(()); + } + let n = if self.down { (sweep.abs() / TAU * ARC_STEPS).ceil().clamp(1.0, ARC_STEPS) as usize } else { 1 }; + let from = self.pos; + let s0 = Point::new(start.cos(), start.sin()); + for i in 1..=n { + let a = start + sweep * i as f64 / n as f64; + let d = (Point::new(a.cos(), a.sin()) - s0) * r; + self.to(from + Point::new(d.x * self.axes.x, d.y * self.axes.y))?; + } + Ok(()) + } + + /// An arc to the displacement `d` with `bulge` (−127..127, ±127 a semicircle). + fn bulge(&mut self, d: Point, bulge: f64) -> Res { + let b = bulge.clamp(-127.0, 127.0) / 127.0; + if b == 0.0 || !self.down || d.len2() == 0.0 { + return self.by(d); + } + // Centre from the chord midpoint along its left normal; sweep 4·atan(b). + let centre = d * 0.5 + Point::new(-d.y, d.x) * ((1.0 - b * b) / (4.0 * b)); + let rel = -centre; + self.arc(rel.len(), rel.y.atan2(rel.x), 4.0 * b.atan()) + } + + /// The octant byte `±0SC`: (clockwise, start octant, octants with 0 = 8). + fn octants(spec: u8) -> (f64, f64, f64) { + let sign = if spec & 0x80 != 0 { -1.0 } else { 1.0 }; + let s = f64::from((spec >> 4) & 7); + let c = match spec & 7 { + 0 => 8.0, + c => f64::from(c), + }; + (sign, s, c) + } + + /// The vector unit size of an extended big font's subshape cell. + fn cell(&self) -> Point { + let h = if self.font.above() > 0.0 { self.font.above() } else { 1.0 }; + let w = if self.font.char_width() > 0.0 { self.font.char_width() } else { h }; + Point::new(w, h) + } + + fn run(&mut self, number: u16, depth: usize) -> Res { + if depth >= MAX_DEPTH { + return Err(Broken); + } + let program = self.font.program(number).ok_or(Broken)?; + let mut r = Cur { b: program, pos: 0 }; + let mut skip = false; + while r.pos < r.b.len() { + self.step()?; + let code = r.byte()?; + // Code 14: in a dual-orientation font the next command is for vertical text only. + let exec = !skip; + skip = false; + match code { + 0 => return Ok(()), + 1 if exec => self.down = true, + 2 if exec => { + self.down = false; + self.flush(); + } + 3 | 4 => { + let k = f64::from(r.byte()?); + if exec && k > 0.0 { + let s = if code == 3 { self.scale / k } else { self.scale * k }; + if !(1e-9..=MAX_COORD).contains(&s) { + return Err(Broken); + } + self.scale = s; + } + } + 5 if exec => { + if self.stack.len() >= MAX_STACK { + return Err(Broken); + } + self.stack.push(self.pos); + } + 6 if exec => { + self.flush(); + if let Some(p) = self.stack.pop() { + self.pos = p; + } + } + 7 => self.subshape(&mut r, exec, depth)?, + 8 | 9 | 12 | 13 => loop { + self.step()?; + let d = Point::new(signed(r.byte()?), signed(r.byte()?)); + if matches!(code, 9 | 13) && d == Point::ZERO { + break; + } + let bulge = if matches!(code, 12 | 13) { signed(r.byte()?) } else { 0.0 }; + if exec { + self.bulge(d, bulge)?; + } + if matches!(code, 8 | 12) { + break; + } + }, + 10 => { + let radius = f64::from(r.byte()?); + let (sign, s, c) = Self::octants(r.byte()?); + if exec { + self.arc(radius, s * FRAC_PI_4, sign * c * FRAC_PI_4)?; + } + } + 11 => { + let start = f64::from(r.byte()?) / 256.0; + let end = match r.byte()? { + 0 => 1.0, + e => f64::from(e) / 256.0, + }; + let radius = f64::from(u16::from_be_bytes([r.byte()?, r.byte()?])); + let (sign, s, c) = Self::octants(r.byte()?); + if exec { + // From `start` past the first octant boundary to `end` past the last + // octant's. + self.arc(radius, (s + sign * start) * FRAC_PI_4, sign * (c - 1.0 + end - start) * FRAC_PI_4)?; + } + } + 14 => skip = self.font.dual() && !self.vertical && exec, + 1..=15 => {} + v => { + if exec { + self.by(direction(v) * f64::from(v >> 4))?; + } + } + } + } + Ok(()) + } + + /// Code 7 and its operands. + fn subshape(&mut self, r: &mut Cur, exec: bool, depth: usize) -> Res { + match self.font.kind() { + ShxKind::Unifont => { + let n = u16::from_be_bytes([r.byte()?, r.byte()?]); + if exec { + self.run(n, depth + 1)?; + } + } + ShxKind::BigFont => { + let first = r.byte()?; + if first != 0 { + if exec { + self.run(u16::from(first), depth + 1)?; + } + return Ok(()); + } + // Extended: the primitive drawn in the box (x, y, width, height) of the cell. + let n = u16::from_be_bytes([r.byte()?, r.byte()?]); + let (x, y, w, h) = (f64::from(r.byte()?), f64::from(r.byte()?), f64::from(r.byte()?), f64::from(r.byte()?)); + if !exec { + return Ok(()); + } + let (pos, axes, down) = (self.pos, self.axes, self.down); + let cell = self.cell(); + self.flush(); + self.pos = pos + Point::new(x * axes.x, y * axes.y) * self.scale; + self.axes = Point::new(axes.x * w / cell.x, axes.y * h / cell.y); + let res = self.run(n, depth + 1); + self.flush(); + self.axes = axes; + self.pos = pos; + self.down = down; + res?; + } + ShxKind::Shapes => { + let n = u16::from(r.byte()?); + if exec { + self.run(n, depth + 1)?; + } + } + } + Ok(()) + } +} + +impl Font for crate::ParsedFont { + fn kind(&self) -> ShxKind { + self.kind + } + fn dual(&self) -> bool { + self.dual + } + fn above(&self) -> f64 { + self.above + } + fn char_width(&self) -> f64 { + self.char_width + } + fn program(&self, number: u16) -> Option<&[u8]> { + let r = self.records.get(&number)?; + self.data.get(r.program.0..r.program.1) + } +} + +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used)] + use super::*; + #[test] + fn executes_shape_vectors_without_app_geometry_types() { + let bytes = crate::compile("*1,3,A\n1,010,0\n").unwrap(); + let font = crate::parse::parse(&bytes).unwrap(); + let glyph = outline(&font, 1, false).unwrap(); + assert_eq!(glyph.strokes, vec![vec![Point::new(0.0, 0.0), Point::new(1.0, 0.0)]]); + assert_eq!(glyph.advance, Point::new(1.0, 0.0)); + } + #[test] + fn recursive_shapes_stop_at_the_depth_bound() { + let bytes = crate::compile("*1,3,A\n7,1,0\n").unwrap(); + let font = crate::parse::parse(&bytes).unwrap(); + assert!(outline(&font, 1, false).is_err()); + } +} From 5a36112af6567a60491c47361a9fdd44ad362979 Mon Sep 17 00:00:00 2001 From: el-j Date: Sun, 15 Nov 2026 16:51:49 +0100 Subject: [PATCH 17/70] craft-image-codecs: SGI rows may share compressed data and must start after the offset tables Addresses review findings: the RLE size check no longer demands separate payload space for every row, and a row offset inside the offset tables is refused. A row without its trailing zero count still reads, as in ffmpeg. --- crates/image-codecs/src/codecs/sgi.rs | 56 +++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/crates/image-codecs/src/codecs/sgi.rs b/crates/image-codecs/src/codecs/sgi.rs index 1e31b5f0a..64c80299c 100644 --- a/crates/image-codecs/src/codecs/sgi.rs +++ b/crates/image-codecs/src/codecs/sgi.rs @@ -120,8 +120,9 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { // Reject inputs that cannot hold the declared pixels before allocating anything for them. let table = if h.rle { rows.checked_mul(8).ok_or_else(|| bad("image size overflow"))? } else { 0 }; let min_len = if h.rle { - // Every row needs at least a packet and a terminator. - HEADER + table + rows * bps + // Both offset tables must fit; rows may share compressed data, so the data itself is + // checked row by row against the offsets below. + HEADER + table } else { HEADER + total.checked_mul(bps).ok_or_else(|| bad("image size overflow"))? }; @@ -138,7 +139,10 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { if h.rle { let start = be32(b, HEADER + index * 4) as usize; let len = be32(b, HEADER + rows * 4 + index * 4) as usize; - let end = start.checked_add(len).filter(|&e| e <= b.len() && start >= HEADER).ok_or_else(|| bad("RLE row lies outside the file"))?; + let end = start + .checked_add(len) + .filter(|&e| e <= b.len() && start >= HEADER + table) + .ok_or_else(|| bad("RLE row lies outside the file"))?; read_rle_row(&b[start..end], h.wide, &mut row)?; } else { let start = HEADER + index * w * bps; @@ -335,6 +339,52 @@ mod tests { assert!(decode(&huge, &Limits::default()).is_err()); } + /// A 1-plane, 8-bit RLE file whose `rows` all start where `starts[i]` says, each `len` bytes long. + fn rle_file(w: u16, starts: &[u32], len: u32, payload: &[u8]) -> Vec { + let mut f = vec![0u8; HEADER]; + f[0..2].copy_from_slice(&MAGIC.to_be_bytes()); + f[2] = 1; + f[3] = 1; + f[4..6].copy_from_slice(&2u16.to_be_bytes()); + f[6..8].copy_from_slice(&w.to_be_bytes()); + f[8..10].copy_from_slice(&(starts.len() as u16).to_be_bytes()); + f[10..12].copy_from_slice(&1u16.to_be_bytes()); + for s in starts { + f.extend_from_slice(&s.to_be_bytes()); + } + for _ in starts { + f.extend_from_slice(&len.to_be_bytes()); + } + f.extend_from_slice(payload); + f + } + + /// The format lets table entries share one compressed row: eight rows, one payload. + #[test] + fn rows_may_share_compressed_data() { + let table = HEADER + 8 * 8; + let f = rle_file(1, &[table as u32; 8], 3, &[0x81, 255, 0]); + assert_eq!(f.len(), table + 3); + let img = decode(&f, &Limits::default()).unwrap(); + assert_eq!((img.dimensions(), img.data()), ((1, 8), &[255u8; 8][..])); + } + + /// Compressed rows start after both tables: an offset into a table is refused. + #[test] + fn rows_may_not_start_inside_the_offset_tables() { + // 3x1: the tables end at 520; 515 reads the table's own bytes as a packet list. + let f = rle_file(3, &[515], 3, &[0, 0, 0, 0, 0]); + assert!(decode(&f, &Limits::default()).is_err()); + } + + /// FFmpeg's decoder, run as an oracle, also reads a row that ends with its last packet and no + /// zero terminator; so does this one. + #[test] + fn a_row_without_its_terminator_still_reads() { + let f = rle_file(1, &[520], 2, &[0x81, 42]); + assert_eq!(decode(&f, &Limits::default()).unwrap().data(), &[42]); + } + #[test] fn palette_mapped_files_are_unsupported() { let img = gradient(4, 4, ChannelLayout::Gray); From e9eaaf5641640b4e2735b0af79ef589c5a74a88a Mon Sep 17 00:00:00 2001 From: el-j Date: Sun, 15 Nov 2026 16:52:35 +0100 Subject: [PATCH 18/70] craft-image-codecs: Sun Raster 1-bit colour maps, opaque standard 32-bit pixels, fallible output buffer Addresses review findings: a 1-bit file with an RGB colour map reads as colour; the unused first byte of standard 32-bit pixels is no longer read as alpha; the writer's buffer is reserved with try_reserve_exact. README lists the native layouts correctly. --- crates/image-codecs/README.md | 2 +- crates/image-codecs/src/codecs/sunraster.rs | 52 +++++++++++++++++++-- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index f9f5f53e6..a37760d80 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -56,7 +56,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | Cineon | yes | yes | U16 (10-bit code values read and written exactly through U16) | Gray, RGB | no | no | no | no | no | no | no | built in | | DICOM | yes | **no** | U8, U16 | Gray, RGB | no | no | no | no | no | no | n/a | built in | | Amiga IFF (ILBM/PBM) | yes | yes (24-bit ILBM) | U8 | RGB, RGBA | yes (1-bit mask plane on write) | no | no | no | no | no | no | built in | -| Sun Raster (`.ras`) | yes (incl. RLE and colour maps) | yes (uncompressed) | U8 | Gray, Gray+A, RGB, RGBA | yes | no | no | no | no | no | no | built in | +| Sun Raster (`.ras`) | yes (incl. RLE and colour maps) | yes (uncompressed) | U8 | Gray, RGB, RGBA | yes | no | no | no | no | no | no | built in | | AVIF | **no** | only with feature `avif` | U8 | RGB, RGBA | yes | no | no | no | no | no | yes | `image` + `ravif` | | HEIF/HEIC | with feature `heif` | **no** | U8, U16 (10/12-bit decodes to U16) | RGB, RGBA | yes (auxiliary alpha) | yes (`colr` prof) | yes | yes | no | no | n/a | `heic-rs` | diff --git a/crates/image-codecs/src/codecs/sunraster.rs b/crates/image-codecs/src/codecs/sunraster.rs index 84d60f145..85c24acc5 100644 --- a/crates/image-codecs/src/codecs/sunraster.rs +++ b/crates/image-codecs/src/codecs/sunraster.rs @@ -97,9 +97,18 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { for x in 0..wu { match depth { 1 => { - // A set bit is black. let bit = (row[x / 8] >> (7 - x % 8)) & 1; - out.push(if bit == 1 { 0 } else { 255 }); + if map_type == 1 { + // The bit is an index into the (two-entry or longer) colour map. + let i = usize::from(bit); + if i >= entries { + return Err(bad("pixel index is outside the colour map")); + } + out.extend_from_slice(&[map[i], map[entries + i], map[2 * entries + i]]); + } else { + // A set bit is black. + out.push(if bit == 1 { 0 } else { 255 }); + } } 8 if map_type == 1 => { let i = usize::from(row[x]); @@ -115,7 +124,8 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { } _ => { let p = &row[4 * x..4 * x + 4]; - out.extend_from_slice(&if kind == 3 { [p[1], p[2], p[3], p[0]] } else { [p[3], p[2], p[1], p[0]] }); + // In the standard types the first byte (X in XBGR) is unused, not alpha. + out.extend_from_slice(&if kind == 3 { [p[1], p[2], p[3], p[0]] } else { [p[3], p[2], p[1], 255] }); } } } @@ -123,6 +133,13 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { Image::from_u8(w, h, layout, out) } +/// A buffer of `cap` bytes, or an error instead of an abort when the allocation cannot be made. +fn reserve(cap: usize) -> std::result::Result, std::collections::TryReserveError> { + let mut v = Vec::new(); + v.try_reserve_exact(cap)?; + Ok(v) +} + /// Writes an uncompressed rasterfile: 8-bit gray, 24-bit RGB or 32-bit RGBA, in the RGB-ordered type. pub(crate) fn encode(src: &Image, plan: Plan) -> Result, CodecError> { let enc = |msg: String| CodecError::encode(F, msg); @@ -145,7 +162,8 @@ pub(crate) fn encode(src: &Image, plan: Plan) -> Result, CodecError> { if data.len() != wu * hu * bytes { return Err(enc("the image has the wrong number of samples".into())); } - let mut out = Vec::with_capacity(HEADER + len as usize); + let mut out = reserve(HEADER.checked_add(len as usize).ok_or_else(|| enc("image too large".into()))?) + .map_err(|_| enc("the image is too large to encode".into()))?; for v in [MAGIC, w, h, depth as u32, len, 3, 0, 0] { out.extend_from_slice(&v.to_be_bytes()); } @@ -218,6 +236,32 @@ mod tests { assert_eq!(decode(&f, &Limits::default()).unwrap().data(), &[0, 255, 0]); } + #[test] + fn one_bit_files_with_a_colour_map_read_as_colour() { + // Two entries: red plane, green plane, blue plane; bit 0 is entry 0. + let map = [10, 20, 30, 40, 50, 60]; + let f = file(1, 1, &map, 1, 3, 1, &[0b0100_0000, 0]); + let img = decode(&f, &Limits::default()).unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgb, &[10, 30, 50, 20, 40, 60, 10, 30, 50][..])); + // A one-entry map has no entry for a set bit. + assert!(decode(&file(1, 1, &[1, 2, 3], 1, 1, 1, &[0b1000_0000, 0]), &Limits::default()).is_err()); + } + + #[test] + fn the_unused_first_byte_of_standard_32_bit_pixels_is_not_alpha() { + let img = decode(&file(32, 1, &[], 0, 1, 1, &[0, 30, 20, 10]), &Limits::default()).unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgba, &[10, 20, 30, 255][..])); + // In the RGB-ordered type the first byte is the alpha the writer stores there. + let img = decode(&file(32, 3, &[], 0, 1, 1, &[7, 10, 20, 30]), &Limits::default()).unwrap(); + assert_eq!(img.data(), &[10, 20, 30, 7]); + } + + #[test] + fn an_output_buffer_too_large_to_allocate_is_an_error() { + assert!(reserve(usize::MAX).is_err()); + assert!(reserve(16).is_ok()); + } + #[test] fn hostile_input_is_an_error_not_a_panic() { let img = pattern(9, 5, ChannelLayout::Rgb); From 939146a5b159a268ee5fc67e26b3dc915b4d4094 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:14:11 +0000 Subject: [PATCH 19/70] tga dpx: share uncompressed RGBA8 export --- crates/dpx/src/lib.rs | 3 + crates/dpx/src/rgba8_dpx10.rs | 105 ++++++++++++++++++++++++++++++++++ crates/tga/src/lib.rs | 3 + crates/tga/src/rgba8_raw.rs | 60 +++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 crates/dpx/src/rgba8_dpx10.rs create mode 100644 crates/tga/src/rgba8_raw.rs diff --git a/crates/dpx/src/lib.rs b/crates/dpx/src/lib.rs index a762566f8..26d9e8656 100644 --- a/crates/dpx/src/lib.rs +++ b/crates/dpx/src/lib.rs @@ -34,6 +34,9 @@ //! Input is treated as hostile: every header read is bounds-checked, the picture size is capped //! by [`Limits`] and the image data must be present in full before anything is allocated. +/// RGBA8-to-10-bit DPX encoding. +pub mod rgba8_dpx10; + #[cfg(test)] mod tests; diff --git a/crates/dpx/src/rgba8_dpx10.rs b/crates/dpx/src/rgba8_dpx10.rs new file mode 100644 index 000000000..6ec5e23b4 --- /dev/null +++ b/crates/dpx/src/rgba8_dpx10.rs @@ -0,0 +1,105 @@ +//! Uncompressed RGB(A) output from top-down RGBA8 pixels. +/// Why the supplied frame cannot be encoded. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Rgba8EncodeError { + /// Empty, overflowing, or mismatched dimensions. + FrameSize, + /// A dimension cannot fit the output format. + TooLarge, +} +fn checked_len(rgba: &[u8], w: u32, h: u32) -> std::result::Result { + let n = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(4)).ok_or(Rgba8EncodeError::FrameSize)?; + if w == 0 || h == 0 || rgba.len() != n { + return Err(Rgba8EncodeError::FrameSize); + } + Ok(n) +} +/// Header size of the DPX files we write (file + image + orientation + film + TV headers). +const DPX_HEADER: usize = 2048; + +/// A DPX file: 10-bit RGB (8-bit values widened by bit replication, so 0 → 0 and 255 → 1023). +pub fn encode_rgba8_dpx10(rgba: &[u8], w: u32, h: u32, creator: &str) -> std::result::Result, Rgba8EncodeError> { + let n = checked_len(rgba, w, h)?; + // one 32-bit word per pixel; `n` is already `w * h * 4` + let size = n.checked_add(DPX_HEADER).ok_or_else(|| Rgba8EncodeError::FrameSize)?; + let file_size = u32::try_from(size).map_err(|_| Rgba8EncodeError::TooLarge)?; + // SMPTE ST 268: undefined numeric fields are all ones, undefined text fields are NUL + let mut hd = vec![0xFF_u8; DPX_HEADER]; + for (a, b) in [ + (36, 660), // file name, creation time, creator, project, copyright + (664, 768), // reserved + (1356, 1408), // reserved + (1432, 1620), // source file name, time, input device, serial number + (1644, 1664), // reserved + (1664, 1712), // film manufacturer, type, offset, prefix, count, format + (1732, 1920), // frame id, slate, reserved + (1972, 2048), // reserved + ] { + hd[a..b].fill(0); + } + for e in 0..8 { + let d = 780 + e * 72 + 40; + hd[d..d + 32].fill(0); // element descriptions + } + let put32 = |hd: &mut Vec, at: usize, v: u32| hd[at..at + 4].copy_from_slice(&v.to_be_bytes()); + let put16 = |hd: &mut Vec, at: usize, v: u16| hd[at..at + 2].copy_from_slice(&v.to_be_bytes()); + // file information header + hd[0..4].copy_from_slice(b"SDPX"); + put32(&mut hd, 4, DPX_HEADER as u32); // offset to the image data + hd[8..16].copy_from_slice(b"V2.0\0\0\0\0"); + put32(&mut hd, 16, file_size); + put32(&mut hd, 20, 1); // ditto key: new frame + put32(&mut hd, 24, 1664); // generic header length + put32(&mut hd, 28, 384); // industry header length + put32(&mut hd, 32, 0); // user data length + let creator = creator.as_bytes(); + let n = creator.len().min(100); + hd[160..160 + n].copy_from_slice(&creator[..n]); + // image information header + put16(&mut hd, 768, 0); // orientation: left to right, top to bottom + put16(&mut hd, 770, 1); // one image element + put32(&mut hd, 772, w); + put32(&mut hd, 776, h); + let el = 780; + put32(&mut hd, el, 0); // unsigned + put32(&mut hd, el + 4, 0); // reference low data code + put32(&mut hd, el + 12, 1023); // reference high data code + hd[el + 20] = 50; // descriptor: RGB + hd[el + 21] = 6; // transfer: ITU-R BT.709 + hd[el + 22] = 6; // colorimetric: ITU-R BT.709 + hd[el + 23] = 10; // bit depth + put16(&mut hd, el + 24, 1); // packing: filled to 32-bit words, method A + put16(&mut hd, el + 26, 0); // no encoding (uncompressed) + put32(&mut hd, el + 28, DPX_HEADER as u32); // offset to the element's data + put32(&mut hd, el + 32, 0); // end-of-line padding + put32(&mut hd, el + 36, 0); // end-of-image padding + // image orientation header: no offset, square pixels + put32(&mut hd, 1408, 0); + put32(&mut hd, 1412, 0); + put32(&mut hd, 1424, w); + put32(&mut hd, 1428, h); + put32(&mut hd, 1628, 1); + put32(&mut hd, 1632, 1); + // television header: progressive + hd[1928] = 0; + let mut out = Vec::with_capacity(size); + out.extend_from_slice(&hd); + let ten = |v: u8| (u32::from(v) << 2) | (u32::from(v) >> 6); + for &[r, g, b, _] in rgba.as_chunks::<4>().0 { + out.extend_from_slice(&((ten(r) << 22) | (ten(g) << 12) | (ten(b) << 2)).to_be_bytes()); + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn packed_rgb_and_creator() { + let bytes = encode_rgba8_dpx10(&[255, 0, 128, 255], 1, 1, "FilmCraft").unwrap(); + assert_eq!(&bytes[..4], b"SDPX"); + assert_eq!(&bytes[160..169], b"FilmCraft"); + assert_eq!(&bytes[2048..], &((1023u32 << 22) | (514u32 << 2)).to_be_bytes()); + assert_eq!(encode_rgba8_dpx10(&[], 0, 1, ""), Err(Rgba8EncodeError::FrameSize)); + } +} diff --git a/crates/tga/src/lib.rs b/crates/tga/src/lib.rs index ac9d21497..3a92052ff 100644 --- a/crates/tga/src/lib.rs +++ b/crates/tga/src/lib.rs @@ -18,6 +18,9 @@ //! Input is treated as hostile: the picture size is capped by [`Limits`], and a run-length file //! must be long enough to hold the pixels its header declares before anything is allocated. +/// Uncompressed RGBA8-to-Targa encoding. +pub mod rgba8_raw; + #[cfg(test)] mod tests; diff --git a/crates/tga/src/rgba8_raw.rs b/crates/tga/src/rgba8_raw.rs new file mode 100644 index 000000000..acec4b9bc --- /dev/null +++ b/crates/tga/src/rgba8_raw.rs @@ -0,0 +1,60 @@ +//! Uncompressed RGB(A) output from top-down RGBA8 pixels. +/// Why the supplied frame cannot be encoded. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Rgba8EncodeError { + /// Empty, overflowing, or mismatched dimensions. + FrameSize, + /// A dimension cannot fit the 16-bit Targa header. + Dimensions, +} +fn checked_len(rgba: &[u8], w: u32, h: u32) -> std::result::Result { + let n = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(4)).ok_or(Rgba8EncodeError::FrameSize)?; + if w == 0 || h == 0 || rgba.len() != n { + return Err(Rgba8EncodeError::FrameSize); + } + Ok(n) +} +/// An uncompressed Targa file: 24-bit BGR, or 32-bit BGRA with `alpha` (straight alpha, as the +/// frame carries it). +pub fn encode_rgba8_raw(rgba: &[u8], w: u32, h: u32, alpha: bool) -> std::result::Result, Rgba8EncodeError> { + checked_len(rgba, w, h)?; + let (tw, th) = match (u16::try_from(w), u16::try_from(h)) { + (Ok(tw), Ok(th)) => (tw, th), + _ => return Err(Rgba8EncodeError::Dimensions), + }; + let bpp: usize = if alpha { 4 } else { 3 }; + let row = w as usize * 4; + let mut out = Vec::with_capacity(18 + w as usize * h as usize * bpp + 26); + out.extend_from_slice(&[0, 0, 2]); // no ID, no colour map, uncompressed true-colour + out.extend_from_slice(&[0; 5]); // colour map specification (unused) + out.extend_from_slice(&[0, 0, 0, 0]); // x / y origin + out.extend_from_slice(&tw.to_le_bytes()); + out.extend_from_slice(&th.to_le_bytes()); + out.push(if alpha { 32 } else { 24 }); + out.push(if alpha { 8 } else { 0 }); // attribute bits; bottom-left origin + // bottom-left origin: the last frame row comes first + for line in rgba.chunks_exact(row).rev() { + for &[r, g, b, a] in line.as_chunks::<4>().0 { + out.extend_from_slice(&[b, g, r]); + if alpha { + out.push(a); + } + } + } + // TGA 2.0 footer without extension or developer areas + out.extend_from_slice(&[0; 8]); + out.extend_from_slice(b"TRUEVISION-XFILE.\0"); + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn raw_round_trip() { + let bytes = encode_rgba8_raw(&[1, 2, 3, 4], 1, 1, true).unwrap(); + assert_eq!(&bytes[18..22], &[3, 2, 1, 4]); + assert_eq!(&bytes[bytes.len() - 18..], b"TRUEVISION-XFILE.\0"); + assert_eq!(encode_rgba8_raw(&[], 0, 1, false), Err(Rgba8EncodeError::FrameSize)); + } +} From 2aa7f7f8d211786650d6cc3a4d3f6be165ddb30a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:22:05 +0000 Subject: [PATCH 20/70] dpx: fix RGBA writer lint --- crates/dpx/src/rgba8_dpx10.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/dpx/src/rgba8_dpx10.rs b/crates/dpx/src/rgba8_dpx10.rs index 6ec5e23b4..05ce20b15 100644 --- a/crates/dpx/src/rgba8_dpx10.rs +++ b/crates/dpx/src/rgba8_dpx10.rs @@ -21,7 +21,7 @@ const DPX_HEADER: usize = 2048; pub fn encode_rgba8_dpx10(rgba: &[u8], w: u32, h: u32, creator: &str) -> std::result::Result, Rgba8EncodeError> { let n = checked_len(rgba, w, h)?; // one 32-bit word per pixel; `n` is already `w * h * 4` - let size = n.checked_add(DPX_HEADER).ok_or_else(|| Rgba8EncodeError::FrameSize)?; + let size = n.checked_add(DPX_HEADER).ok_or(Rgba8EncodeError::FrameSize)?; let file_size = u32::try_from(size).map_err(|_| Rgba8EncodeError::TooLarge)?; // SMPTE ST 268: undefined numeric fields are all ones, undefined text fields are NUL let mut hd = vec![0xFF_u8; DPX_HEADER]; From 159b942defe3613c91a4beec5fadfcb1593f2600 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:24:27 +0000 Subject: [PATCH 21/70] Share contextual directional text shaping --- Cargo.lock | 1 + crates/font-catalog/Cargo.toml | 1 + crates/font-catalog/src/lib.rs | 2 + crates/font-catalog/src/shape.rs | 150 +++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 crates/font-catalog/src/shape.rs diff --git a/Cargo.lock b/Cargo.lock index 9424cf3fe..3e1487dca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1761,6 +1761,7 @@ name = "craft-font-catalog" version = "0.1.0" dependencies = [ "fontique", + "harfrust 0.13.3", "kurbo 0.13.1", "skrifa 0.47.0", "thiserror 2.0.21", diff --git a/crates/font-catalog/Cargo.toml b/crates/font-catalog/Cargo.toml index 1f0830a63..c53d871b8 100644 --- a/crates/font-catalog/Cargo.toml +++ b/crates/font-catalog/Cargo.toml @@ -15,6 +15,7 @@ default = ["system"] system = ["dep:fontique"] [dependencies] +harfrust = "0.13" kurbo = { workspace = true } skrifa = { version = "0.47", default-features = false, features = ["std"] } thiserror = { workspace = true } diff --git a/crates/font-catalog/src/lib.rs b/crates/font-catalog/src/lib.rs index 7da4ca310..176a38738 100644 --- a/crates/font-catalog/src/lib.rs +++ b/crates/font-catalog/src/lib.rs @@ -26,6 +26,7 @@ mod fontconfig; mod outline; mod registry; mod scan; +mod shape; mod suitcase; pub mod system; @@ -34,6 +35,7 @@ pub use env::{FontEnv, Os, font_dirs, font_store_dir, office_font_dirs}; pub use fontconfig::fontconfig_dirs; pub use outline::{FontOutline, OutlineError}; pub use scan::{FaceInfo, LocalizedName, Source, is_font_file, style_italic, style_weight, style_width}; +pub use shape::{RunGlyph, RunInput, ShapeRunError, shape_run}; #[cfg(test)] mod tests; diff --git a/crates/font-catalog/src/shape.rs b/crates/font-catalog/src/shape.rs new file mode 100644 index 000000000..7fb0814d3 --- /dev/null +++ b/crates/font-catalog/src/shape.rs @@ -0,0 +1,150 @@ +//! Shape a run with its source clusters and surrounding text context. + +use std::sync::Arc; + +use harfrust::{BufferClusterLevel, Direction, Feature, Language, Script, ShapeOptions, ShapePlan, Shaper, UnicodeBuffer}; + +/// Parameters for a single directional run. Text is in logical order and `source_start` is its +/// byte offset in the complete source. Context influences joining but emits no glyphs. +pub struct RunInput<'a> { + /// Text in this run. + pub text: &'a str, + /// Its byte offset in the complete source. + pub source_start: usize, + /// Text before the run, within the same shaping context. + pub pre_context: &'a str, + /// Text after the run, within the same shaping context. + pub post_context: &'a str, + /// Direction of shaping. + pub direction: Direction, + /// Language used for language-specific forms. + pub language: Option<&'a Language>, + /// OpenType features with ranges in source byte coordinates. + pub features: &'a [Feature], + /// Use character-level instead of monotone grapheme clusters. + pub character_clusters: bool, + /// Uppercase each mapped character without losing its source cluster. + pub uppercase: bool, +} + +/// A glyph in design units, with its source-byte cluster and directional advances and offsets. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct RunGlyph { + pub glyph_id: u32, + pub cluster: u32, + pub x_advance: i32, + pub y_advance: i32, + pub x_offset: i32, + pub y_offset: i32, +} + +/// A run's source offsets cannot fit HarfRust's 32-bit clusters. +#[derive(Debug, thiserror::Error)] +#[error("shaping cluster exceeds the 32-bit source offset range")] +pub struct ShapeRunError; + +/// Shape a run using a caller's face-specific plan cache. `map_character` applies digit or other +/// character mapping before case conversion; expansions retain the original byte cluster. +/// The plan callback receives the guessed script, direction, language and feature set so it can +/// reuse plans for the particular shaper and its variable-font instance. +pub fn shape_run( + shaper: &Shaper<'_>, + input: &RunInput<'_>, + mut map_character: impl FnMut(char) -> char, + plan: impl FnOnce(&Shaper<'_>, Direction, Option