mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 20:25:02 +00:00
Merge remote-tracking branch 'origin/main' into w1692
This commit is contained in:
@@ -23,5 +23,5 @@ jobs:
|
||||
# The codec and format code behind features that are off by default. Without these the
|
||||
# coverage build never compiles it, and new code there reads as untested.
|
||||
cargo-features: >-
|
||||
craft-video/dnx,craft-video/vp8,craft-video/prores,craft-video/h264,craft-video/hevc,craft-video/vp9,craft-video/av1,craft-video/threads,craft-audio/decode,craft-audio/encode,craft-audio/peaks,craft-audio/rayon,craft-audio/serde,craft-media/matroska,craft-media/ogg,craft-media/bitstream,craft-media/serde,craft-mesh/stl,craft-mesh/obj,craft-mesh/ply,craft-mesh/gltf,craft-metafile/write,craft-image-codecs/sgi
|
||||
craft-video/dnx,craft-video/vp8,craft-video/prores,craft-video/h264,craft-video/hevc,craft-video/vp9,craft-video/av1,craft-video/threads,craft-audio/decode,craft-audio/encode,craft-audio/peaks,craft-audio/rayon,craft-audio/serde,craft-media/matroska,craft-media/ogg,craft-media/bitstream,craft-media/serde,craft-mesh/stl,craft-mesh/obj,craft-mesh/ply,craft-mesh/gltf,craft-metafile/write,craft-image-codecs/sgi,craft-image-codecs/sunraster
|
||||
secrets: inherit
|
||||
|
||||
@@ -101,4 +101,5 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso
|
||||
|
||||
`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.
|
||||
The XML reader in `crates/plist/src/xml.rs` and its base64 compatibility helper are ported from Craft Anything `8f3b775157d331dd31cfa4da911e26d8856b8c77` (same MIT OR Apache-2.0 source licenses). Brandon Thomas is the primary import author; retained hardening credits include Afloat, BFlat, Rahul, RRFZ and NetterSilver. The finite-real fix preserves gmrmad1 as author from Craft Anything #1905 (`d85e104f`); quoted-attribute/line-ending regressions preserve RRFZ. Bounds/adaptation and bounded entity scanning are subsequent Dylan M. Taylor changes. App-specific JSON projections and keyed-archive interpretation remain outside this crate.
|
||||
|
||||
Generated
+22
@@ -1785,9 +1785,12 @@ 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",
|
||||
"unicode-bidi",
|
||||
"unicode-segmentation",
|
||||
"windows-registry",
|
||||
"write-fonts 0.54.1",
|
||||
]
|
||||
@@ -2022,6 +2025,7 @@ name = "craft-jsonrpc"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"serde_json",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2657,6 +2661,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"
|
||||
@@ -6140,6 +6148,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -7977,6 +7986,7 @@ dependencies = [
|
||||
"loop9",
|
||||
"quick-error 2.0.1",
|
||||
"rav1e",
|
||||
"rayon",
|
||||
"rgb",
|
||||
]
|
||||
|
||||
@@ -9920,9 +9930,21 @@ dependencies = [
|
||||
"mio",
|
||||
"pin-project-lite",
|
||||
"socket2",
|
||||
"tokio-macros",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-macros"
|
||||
version = "2.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 3.0.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-rustls"
|
||||
version = "0.26.6"
|
||||
|
||||
@@ -11,6 +11,7 @@ rust-version = "1.95"
|
||||
repository = "https://github.com/storytold/craft-libs"
|
||||
|
||||
[workspace.dependencies]
|
||||
craft-shx = { path = "crates/shx" }
|
||||
craft-http-url = { path = "crates/http-url" }
|
||||
craft-usd = { path = "crates/usd" }
|
||||
craft-smime = { path = "crates/smime" }
|
||||
|
||||
@@ -29,6 +29,7 @@ pub mod lut3d;
|
||||
pub mod lutfile;
|
||||
pub mod math;
|
||||
pub mod named;
|
||||
mod normalize;
|
||||
pub mod pipeline;
|
||||
pub mod profile;
|
||||
pub mod sony_lut;
|
||||
@@ -45,6 +46,7 @@ pub use gamut::GamutCheck;
|
||||
pub use gpu_matrix_shaper::GpuMatrixShaper;
|
||||
pub use lut3d::Lut3d;
|
||||
pub use named::{NamedColor, NamedColors};
|
||||
pub use normalize::normalize_for_embedding;
|
||||
pub use profile::{ColorSpace, Pcs, Profile, ProfileClass};
|
||||
pub use transform::{SampleKind, Transform, TransformOptions, black_point, cached};
|
||||
pub use write::profile_id;
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
//! Bounded encoded ICC layout normalization, originating in LightCraft.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
/// Zero the creation date, align tag payloads to four bytes, preserve shared payloads,
|
||||
/// update the profile length and clear its stale profile ID. No colour tags are interpreted.
|
||||
///
|
||||
/// This is a layout operation, not profile validation. Malformed tables or layouts exceeding
|
||||
/// 4096 tags or 64 MiB output are returned unchanged except for the creation date. The owned
|
||||
/// input is reused on failure, so rejected input does not require an additional full copy.
|
||||
/// Consumers needing rejection of invalid profiles must separately use `Profile::parse`.
|
||||
pub fn normalize_for_embedding(mut bytes: Vec<u8>) -> Vec<u8> {
|
||||
if let Some(date) = bytes.get_mut(24..36) {
|
||||
date.fill(0);
|
||||
}
|
||||
align(&bytes).unwrap_or(bytes)
|
||||
}
|
||||
|
||||
const MAX_BYTES: usize = 64 * 1024 * 1024;
|
||||
const MAX_TAGS: usize = 4096;
|
||||
|
||||
fn be(bytes: &[u8], at: usize) -> Option<usize> {
|
||||
let s = bytes.get(at..at.checked_add(4)?)?;
|
||||
usize::try_from(u32::from_be_bytes([s[0], s[1], s[2], s[3]])).ok()
|
||||
}
|
||||
|
||||
fn align(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
if bytes.len() > MAX_BYTES {
|
||||
return None;
|
||||
}
|
||||
let count = be(bytes, 128)?;
|
||||
if count > MAX_TAGS {
|
||||
return None;
|
||||
}
|
||||
let end = 132usize.checked_add(count.checked_mul(12)?)?;
|
||||
bytes.get(..end)?;
|
||||
let mut placements = BTreeMap::new();
|
||||
let mut offsets = Vec::new();
|
||||
offsets.try_reserve_exact(count).ok()?;
|
||||
let mut size = end;
|
||||
for index in 0..count {
|
||||
let entry = 132 + index * 12;
|
||||
let off = be(bytes, entry + 4)?;
|
||||
let len = be(bytes, entry + 8)?;
|
||||
if off < end {
|
||||
return None;
|
||||
}
|
||||
bytes.get(off..off.checked_add(len)?)?;
|
||||
let new_off = if let Some(&placed) = placements.get(&(off, len)) {
|
||||
placed
|
||||
} else {
|
||||
let aligned = size.checked_add(3)? & !3;
|
||||
size = aligned.checked_add(len)?;
|
||||
if size > MAX_BYTES {
|
||||
return None;
|
||||
}
|
||||
placements.insert((off, len), aligned);
|
||||
aligned
|
||||
};
|
||||
offsets.push(new_off);
|
||||
}
|
||||
size = size.checked_add(3)? & !3;
|
||||
if size > MAX_BYTES {
|
||||
return None;
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
out.try_reserve_exact(size).ok()?;
|
||||
out.resize(size, 0);
|
||||
out[..end].copy_from_slice(&bytes[..end]);
|
||||
for ((off, len), target) in placements {
|
||||
out[target..target + len].copy_from_slice(&bytes[off..off + len]);
|
||||
}
|
||||
for (index, off) in offsets.into_iter().enumerate() {
|
||||
let field = 132 + index * 12 + 4;
|
||||
out[field..field + 4].copy_from_slice(&u32::try_from(off).ok()?.to_be_bytes());
|
||||
}
|
||||
out[..4].copy_from_slice(&u32::try_from(size).ok()?.to_be_bytes());
|
||||
out[84..100].fill(0);
|
||||
Some(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
#[path = "normalize_legacy_tests.rs"]
|
||||
mod legacy;
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn matches_frozen_writer_and_preserves_aliases() {
|
||||
for count in 0..20usize {
|
||||
let end = 132 + count * 12;
|
||||
let mut bytes = vec![17; end + 31];
|
||||
bytes[128..132].copy_from_slice(&(count as u32).to_be_bytes());
|
||||
for index in 0..count {
|
||||
let field = 132 + index * 12 + 4;
|
||||
let off = end + index % 3;
|
||||
bytes[field..field + 4].copy_from_slice(&(off as u32).to_be_bytes());
|
||||
bytes[field + 4..field + 8].copy_from_slice(&7u32.to_be_bytes());
|
||||
}
|
||||
let expected = legacy::align_tags(&legacy::fixed_date(bytes.clone()));
|
||||
let result = normalize_for_embedding(bytes);
|
||||
assert_eq!(result, expected);
|
||||
assert_eq!(normalize_for_embedding(result.clone()), result);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn real_profile_preserves_color_tags_and_legacy_bytes() {
|
||||
let encoded = crate::Builtin::Srgb.profile().clone().with_encoded_bytes();
|
||||
let input = encoded.to_bytes().to_vec();
|
||||
let normalized = normalize_for_embedding(input.clone());
|
||||
assert_eq!(normalized, legacy::align_tags(&legacy::fixed_date(input)));
|
||||
assert_eq!(&normalized[24..36], &[0; 12]);
|
||||
assert_eq!(&normalized[84..100], &[0; 16]);
|
||||
assert!(crate::Profile::parse(&normalized).is_ok());
|
||||
for index in 0..be(&normalized, 128).unwrap() {
|
||||
assert_eq!(be(&normalized, 136 + index * 12).unwrap() % 4, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_tables_return_owned_input_without_expansion() {
|
||||
for len in 0..132 {
|
||||
let bytes = vec![1; len];
|
||||
assert_eq!(normalize_for_embedding(bytes.clone()), legacy::fixed_date(bytes));
|
||||
}
|
||||
for count in [4097u32, u32::MAX] {
|
||||
let mut bytes = vec![1; 144];
|
||||
bytes[128..132].copy_from_slice(&count.to_be_bytes());
|
||||
assert_eq!(normalize_for_embedding(bytes.clone()), legacy::fixed_date(bytes));
|
||||
}
|
||||
let mut bytes = vec![1; 144];
|
||||
bytes[128..132].copy_from_slice(&1u32.to_be_bytes());
|
||||
bytes[136..140].copy_from_slice(&u32::MAX.to_be_bytes());
|
||||
bytes[140..144].copy_from_slice(&u32::MAX.to_be_bytes());
|
||||
assert_eq!(normalize_for_embedding(bytes.clone()), legacy::fixed_date(bytes));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
//! Encoded ICC normalization extracted from LightCraft.
|
||||
pub(super) fn align_tags(b: &[u8]) -> Vec<u8> {
|
||||
let be = |i: usize| b.get(i..i + 4).map(|s| u32::from_be_bytes([s[0], s[1], s[2], s[3]]) as usize);
|
||||
let Some(n) = be(128) else { return b.to_vec() };
|
||||
let table_end = 132 + 12 * n;
|
||||
let tags: Option<Vec<(usize, usize)>> = (0..n).map(|t| Some((be(132 + 12 * t + 4)?, be(132 + 12 * t + 8)?))).collect();
|
||||
let Some(tags) = tags.filter(|t| table_end <= b.len() && t.iter().all(|&(o, l)| o >= table_end && o + l <= b.len())) else {
|
||||
return b.to_vec();
|
||||
};
|
||||
let mut out = b[..table_end].to_vec();
|
||||
let mut placed: Vec<((usize, usize), usize)> = Vec::new();
|
||||
for (t, &(off, len)) in tags.iter().enumerate() {
|
||||
let new_off = match placed.iter().find(|(k, _)| *k == (off, len)) {
|
||||
// tags sharing data keep sharing it
|
||||
Some(&(_, o)) => o,
|
||||
None => {
|
||||
while !out.len().is_multiple_of(4) {
|
||||
out.push(0);
|
||||
}
|
||||
let o = out.len();
|
||||
out.extend_from_slice(&b[off..off + len]);
|
||||
placed.push(((off, len), o));
|
||||
o
|
||||
}
|
||||
};
|
||||
out[132 + 12 * t + 4..132 + 12 * t + 8].copy_from_slice(&(new_off as u32).to_be_bytes());
|
||||
}
|
||||
while !out.len().is_multiple_of(4) {
|
||||
out.push(0);
|
||||
}
|
||||
let size = out.len() as u32;
|
||||
out[0..4].copy_from_slice(&size.to_be_bytes());
|
||||
// the profile ID (MD5) would no longer match: clear it (= "not computed")
|
||||
out[84..100].fill(0);
|
||||
out
|
||||
}
|
||||
|
||||
pub(super) fn fixed_date(mut b: Vec<u8>) -> Vec<u8> {
|
||||
if let Some(date) = b.get_mut(24..36) {
|
||||
date.fill(0);
|
||||
}
|
||||
b
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
//! 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,
|
||||
/// The output buffer could not be allocated.
|
||||
OutOfMemory,
|
||||
}
|
||||
fn checked_len(rgba: &[u8], w: u32, h: u32) -> std::result::Result<usize, Rgba8EncodeError> {
|
||||
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<Vec<u8>, 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(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<u8>, at: usize, v: u32| hd[at..at + 4].copy_from_slice(&v.to_be_bytes());
|
||||
let put16 = |hd: &mut Vec<u8>, 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;
|
||||
// The caller already holds the frame; reserve the output fallibly so a large export reports
|
||||
// an error instead of aborting the process.
|
||||
let mut out = Vec::new();
|
||||
out.try_reserve_exact(size).map_err(|_| Rgba8EncodeError::OutOfMemory)?;
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,12 @@ default = ["system"]
|
||||
system = ["dep:fontique"]
|
||||
|
||||
[dependencies]
|
||||
harfrust = "0.13"
|
||||
kurbo = { workspace = true }
|
||||
skrifa = { version = "0.47", default-features = false, features = ["std"] }
|
||||
thiserror = { workspace = true }
|
||||
unicode-bidi = "0.3.18"
|
||||
unicode-segmentation = "1.13"
|
||||
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||
fontique = { version = "0.11.1", default-features = false, features = ["system", "fontconfig-dlopen"], optional = true }
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
//! Face choice for a grapheme cluster.
|
||||
|
||||
/// Characters requiring their own glyph, after numeral or other caller-supplied mapping.
|
||||
/// Controls, joiners and variation selectors affect shaping but do not need a separate glyph.
|
||||
pub fn drawable_characters<'a>(cluster: &'a str, map: impl Fn(char) -> char + Copy + 'a) -> impl Iterator<Item = char> + 'a {
|
||||
cluster
|
||||
.chars()
|
||||
.map(map)
|
||||
.filter(|&c| !(c.is_control() || matches!(c, '\u{200C}' | '\u{200D}' | '\u{2028}' | '\u{FE00}'..='\u{FE0F}' | '\u{E0100}'..='\u{E01EF}')))
|
||||
}
|
||||
|
||||
/// Select one face for a cluster: prefer the primary if it covers every drawable character;
|
||||
/// otherwise try each character's fallback until a face covers the whole cluster. If none does,
|
||||
/// use the first character's fallback when the primary lacks it, or keep the primary.
|
||||
/// The caller controls the font catalog, ordering and style matching.
|
||||
pub fn cluster_face<F: Clone>(
|
||||
cluster: &str,
|
||||
primary: &F,
|
||||
map: impl Fn(char) -> char + Copy,
|
||||
primary_covers: impl Fn(char) -> bool,
|
||||
mut fallback: impl FnMut(char) -> Option<F>,
|
||||
covers: impl Fn(&F, char) -> bool,
|
||||
) -> F {
|
||||
let characters = || drawable_characters(cluster, map);
|
||||
if characters().all(&primary_covers) {
|
||||
return primary.clone();
|
||||
}
|
||||
for c in characters() {
|
||||
if let Some(face) = fallback(c)
|
||||
&& characters().all(|ch| covers(&face, ch))
|
||||
{
|
||||
return face;
|
||||
}
|
||||
}
|
||||
if let Some(c) = characters().next()
|
||||
&& !primary_covers(c)
|
||||
&& let Some(face) = fallback(c)
|
||||
{
|
||||
return face;
|
||||
}
|
||||
primary.clone()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn chooses_one_face_for_a_base_and_mark() {
|
||||
let fallback = |c| match c {
|
||||
'a' => Some(1),
|
||||
'\u{301}' => Some(2),
|
||||
_ => None,
|
||||
};
|
||||
let covers = |face: &u8, c| *face == 2 || (*face == 1 && c == 'a');
|
||||
assert_eq!(cluster_face("a\u{301}\u{200d}", &0, |c| c, |_| false, fallback, covers), 2);
|
||||
assert_eq!(drawable_characters("a\u{200d}\u{fe0f}", |c| c).collect::<String>(), "a");
|
||||
}
|
||||
#[test]
|
||||
fn uses_first_character_fallback_when_no_face_covers_cluster() {
|
||||
assert_eq!(cluster_face("ab", &0, |c| c, |_| false, |c| if c == 'a' { Some(1) } else { Some(2) }, |face, c| (*face, c) == (1, 'a')), 1);
|
||||
}
|
||||
}
|
||||
@@ -23,21 +23,33 @@
|
||||
mod catalog;
|
||||
mod embedding;
|
||||
mod env;
|
||||
mod fallback;
|
||||
mod fontconfig;
|
||||
mod ligature;
|
||||
mod line_break;
|
||||
mod outline;
|
||||
mod registry;
|
||||
mod scan;
|
||||
mod segments;
|
||||
/// Bounded SFNT face metadata discovery over caller-provided random-access readers.
|
||||
pub mod sfnt;
|
||||
mod shape;
|
||||
mod suitcase;
|
||||
pub mod system;
|
||||
mod visual_order;
|
||||
|
||||
pub use catalog::{Catalog, FontData, FontFileError, Limits, Lister, LoadedFace, Match, Watch};
|
||||
pub use embedding::{SubsetEmbedding, subset_embedding};
|
||||
pub use env::{FontEnv, Os, font_dirs, font_store_dir, office_font_dirs};
|
||||
pub use fallback::{cluster_face, drawable_characters};
|
||||
pub use fontconfig::fontconfig_dirs;
|
||||
pub use ligature::ligature_caret_stops;
|
||||
pub use line_break::settle_line_break;
|
||||
pub use outline::{FontOutline, GlyphPen, OutlineError};
|
||||
pub use scan::{FaceInfo, LocalizedName, Source, is_font_file, style_italic, style_weight, style_width};
|
||||
pub use segments::SegmentBoundary;
|
||||
pub use shape::{RunGlyph, RunInput, ShapeRunError, shape_run};
|
||||
pub use visual_order::{VisualGlyph, visual_order};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
//! Map ligature caret fractions to grapheme boundaries.
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
/// Return logical source-byte caret stops inside a shaped cluster. `font_carets` receives the
|
||||
/// number of stops and may return their normalized positions along the glyph's logical width.
|
||||
/// Missing or incorrectly sized font data falls back to equal divisions of the width.
|
||||
pub fn ligature_caret_stops(text: &str, byte: usize, len: usize, font_carets: impl FnOnce(usize) -> Option<Vec<f64>>) -> Vec<(usize, f64)> {
|
||||
let Some(s) = byte.checked_add(len).and_then(|end| text.get(byte..end)).filter(|_| len > 1) else { return Vec::new() };
|
||||
let starts: Vec<usize> = s.grapheme_indices(true).skip(1).map(|(i, _)| byte + i).collect();
|
||||
if starts.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
let parts = starts.len() + 1;
|
||||
let mut at =
|
||||
font_carets(starts.len()).filter(|c| c.len() == starts.len()).unwrap_or_else(|| (1..parts).map(|j| j as f64 / parts as f64).collect());
|
||||
at.sort_by(f64::total_cmp);
|
||||
starts.into_iter().zip(at).collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn maps_graphemes_and_sorts_font_positions() {
|
||||
assert_eq!(ligature_caret_stops("ábc", 0, 5, |_| Some(vec![0.8, 0.3])), vec![(3, 0.3), (4, 0.8)]);
|
||||
}
|
||||
#[test]
|
||||
fn falls_back_and_rejects_invalid_ranges() {
|
||||
assert_eq!(ligature_caret_stops("abc", 0, 3, |_| Some(vec![0.5])), vec![(1, 1.0 / 3.0), (2, 2.0 / 3.0)]);
|
||||
assert!(ligature_caret_stops("abc", usize::MAX, 2, |_| None).is_empty());
|
||||
assert!(ligature_caret_stops("é", 1, 1, |_| None).is_empty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
//! Choose a line break after measuring candidate lines as drawn.
|
||||
|
||||
/// Choose the latest candidate that fits when a tab changes the line's drawn width.
|
||||
///
|
||||
/// `breaks` must be sorted by glyph index and the boolean marks hyphenated breaks.
|
||||
/// `checks` limits the candidates measured individually from the end; older non-hyphenated
|
||||
/// candidates are bisected, assuming their drawn widths are nondecreasing. If none fits,
|
||||
/// the earliest eligible break is returned. Without a tab or a measurement callback the
|
||||
/// original `chosen` break is returned.
|
||||
pub fn settle_line_break(
|
||||
tabbed: bool,
|
||||
checks: usize,
|
||||
start: usize,
|
||||
chosen: (usize, bool),
|
||||
breaks: &[(usize, bool)],
|
||||
fits: Option<&dyn Fn(usize, bool) -> bool>,
|
||||
) -> (usize, bool) {
|
||||
let Some(fits) = fits.filter(|_| tabbed) else { return chosen };
|
||||
if fits(chosen.0, chosen.1) {
|
||||
return chosen;
|
||||
}
|
||||
let earlier = breaks.get(breaks.partition_point(|b| b.0 <= start)..breaks.partition_point(|b| b.0 < chosen.0)).unwrap_or_default();
|
||||
if let Some(&b) = earlier.iter().rev().take(checks).find(|b| fits(b.0, b.1)) {
|
||||
return b;
|
||||
}
|
||||
let rest = earlier.get(..earlier.len().saturating_sub(checks)).unwrap_or_default();
|
||||
let plain: Vec<&(usize, bool)> = rest.iter().filter(|b| !b.1).collect();
|
||||
let fitting = plain.partition_point(|b| fits(b.0, b.1));
|
||||
fitting.checked_sub(1).and_then(|k| plain.get(k).copied()).or(earlier.first()).copied().unwrap_or(chosen)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn measures_latest_candidates_and_preserves_hyphenation() {
|
||||
let breaks = [(1, false), (2, false), (3, true), (4, false)];
|
||||
let fits = |i, hyphen| i <= 2 || (i == 3 && hyphen);
|
||||
assert_eq!(settle_line_break(true, 2, 0, (4, false), &breaks, Some(&fits)), (3, true));
|
||||
assert_eq!(settle_line_break(false, 2, 0, (4, false), &breaks, Some(&fits)), (4, false));
|
||||
}
|
||||
#[test]
|
||||
fn bisects_older_plain_breaks_and_falls_back_to_first_eligible() {
|
||||
let breaks = [(1, false), (2, true), (3, false), (4, false), (5, false)];
|
||||
assert_eq!(settle_line_break(true, 1, 0, (5, false), &breaks, Some(&|i, _| i <= 3)), (3, false));
|
||||
assert_eq!(settle_line_break(true, 1, 1, (5, false), &breaks, Some(&|_, _| false)), (2, true));
|
||||
assert_eq!(settle_line_break(true, 1, 5, (5, false), &breaks, Some(&|_, _| false)), (5, false));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
//! Boundaries between runs that need separate shaping.
|
||||
|
||||
/// Tracks the properties of the current shaping segment while walking grapheme clusters.
|
||||
/// The caller resolves font faces, bidi levels and script classification; marks stay with the
|
||||
/// preceding segment even when their face, level or case differs.
|
||||
pub struct SegmentBoundary<F, L, S> {
|
||||
face: F,
|
||||
level: L,
|
||||
small_caps: bool,
|
||||
strong_script: Option<S>,
|
||||
}
|
||||
|
||||
impl<F: Copy + Eq, L: Copy + Eq, S: Copy + Eq> SegmentBoundary<F, L, S> {
|
||||
/// Begin at the first segment's face and bidi level.
|
||||
pub fn new(face: F, level: L) -> Self {
|
||||
Self { face, level, small_caps: false, strong_script: None }
|
||||
}
|
||||
|
||||
/// Observe a cluster and return whether it begins a new shaping segment. Pass `None` for
|
||||
/// common or inherited script and `true` for marks, joiners and variation selectors.
|
||||
pub fn advance(&mut self, face: F, level: L, small_caps: bool, strong_script: Option<S>, mark: bool) -> bool {
|
||||
let script_change = matches!((self.strong_script, strong_script), (Some(a), Some(b)) if a != b);
|
||||
let split = (self.face != face || self.level != level || self.small_caps != small_caps || script_change) && !mark;
|
||||
if split {
|
||||
self.face = face;
|
||||
self.level = level;
|
||||
self.small_caps = small_caps;
|
||||
}
|
||||
if let Some(script) = strong_script {
|
||||
self.strong_script = Some(script);
|
||||
}
|
||||
split
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn preserves_marks_and_splits_on_face_direction_case_and_script() {
|
||||
let mut state = SegmentBoundary::<u8, u8, u8>::new(1, 0);
|
||||
assert!(!state.advance(1, 0, false, Some(1), false));
|
||||
assert!(!state.advance(2, 1, true, None, true));
|
||||
assert!(state.advance(2, 1, true, Some(2), false));
|
||||
assert!(!state.advance(2, 1, true, None, false));
|
||||
assert!(state.advance(2, 1, true, Some(1), false));
|
||||
}
|
||||
#[test]
|
||||
fn strong_script_on_a_mark_updates_the_following_boundary() {
|
||||
let mut state = SegmentBoundary::<u8, u8, u8>::new(1, 0);
|
||||
assert!(!state.advance(1, 0, false, Some(1), false));
|
||||
assert!(!state.advance(1, 0, false, Some(2), true));
|
||||
assert!(!state.advance(1, 0, false, Some(2), false));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
//! 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<Script>, Option<&Language>, &[Feature]) -> Arc<ShapePlan>,
|
||||
) -> Result<Vec<RunGlyph>, ShapeRunError> {
|
||||
let mut buffer = UnicodeBuffer::new();
|
||||
for (offset, character) in input.text.char_indices() {
|
||||
let cluster = input.source_start.checked_add(offset).and_then(|n| u32::try_from(n).ok()).ok_or(ShapeRunError)?;
|
||||
let character = map_character(character);
|
||||
if input.uppercase {
|
||||
for upper in character.to_uppercase() {
|
||||
buffer.add(upper, cluster);
|
||||
}
|
||||
} else {
|
||||
buffer.add(character, cluster);
|
||||
}
|
||||
}
|
||||
buffer.set_direction(input.direction);
|
||||
buffer.guess_segment_properties();
|
||||
if input.character_clusters {
|
||||
buffer.set_cluster_level(BufferClusterLevel::Characters);
|
||||
}
|
||||
if let Some(language) = input.language {
|
||||
buffer.set_language(language.clone());
|
||||
}
|
||||
buffer.set_pre_context(input.pre_context);
|
||||
buffer.set_post_context(input.post_context);
|
||||
let script = Some(buffer.script()).filter(|s| *s != harfrust::script::UNKNOWN);
|
||||
let plan = plan(shaper, buffer.direction(), script, input.language, input.features);
|
||||
let glyphs = shaper.shape(buffer, ShapeOptions::new().features(input.features).plan(Some(&plan)));
|
||||
Ok(glyphs
|
||||
.glyph_infos()
|
||||
.iter()
|
||||
.zip(glyphs.glyph_positions())
|
||||
.map(|(info, pos)| RunGlyph {
|
||||
glyph_id: info.glyph_id,
|
||||
cluster: info.cluster,
|
||||
x_advance: pos.x_advance,
|
||||
y_advance: pos.y_advance,
|
||||
x_offset: pos.x_offset,
|
||||
y_offset: pos.y_offset,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use harfrust::{FontRef, ShaperData};
|
||||
|
||||
#[test]
|
||||
fn synthetic_face_runs_without_installed_fonts() {
|
||||
// A tiny face with a single .notdef glyph. The shaper still processes source
|
||||
// clusters, direction, mapping, features and context without external font files.
|
||||
let mut builder = write_fonts::FontBuilder::new();
|
||||
let mut head = vec![0; 54];
|
||||
head[18..20].copy_from_slice(&1000u16.to_be_bytes());
|
||||
let mut hhea = vec![0; 36];
|
||||
hhea[34..36].copy_from_slice(&1u16.to_be_bytes());
|
||||
for (tag, bytes) in [(b"head", head), (b"hhea", hhea), (b"hmtx", vec![0, 100, 0, 0]), (b"maxp", vec![0, 0, 0x50, 0, 0, 1])] {
|
||||
builder.add_raw(write_fonts::types::Tag::new(tag), bytes);
|
||||
}
|
||||
check_run(&builder.build());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_matches_direct_shaping_with_source_clusters_and_context() {
|
||||
let Ok(dir) = std::env::var("CRAFT_FONTS_DIR") else { return };
|
||||
let bytes = std::fs::read(std::path::Path::new(&dir).join("fonts/noto-sans-arabic/NotoSansArabic.ttf")).unwrap();
|
||||
check_run(&bytes);
|
||||
}
|
||||
|
||||
fn check_run(bytes: &[u8]) {
|
||||
let font = FontRef::from_index(bytes, 0).unwrap();
|
||||
let data = ShaperData::new(&font);
|
||||
let shaper = data.shaper(&font).build();
|
||||
let overflow = RunInput {
|
||||
text: "ab",
|
||||
source_start: usize::MAX,
|
||||
pre_context: "",
|
||||
post_context: "",
|
||||
direction: Direction::LeftToRight,
|
||||
language: None,
|
||||
features: &[],
|
||||
character_clusters: false,
|
||||
uppercase: false,
|
||||
};
|
||||
assert!(shape_run(&shaper, &overflow, |c| c, |s, d, script, lang, f| Arc::new(ShapePlan::new(s, d, script, lang, f))).is_err());
|
||||
let language: Language = "ar".parse().unwrap();
|
||||
let features = [Feature::new(harfrust::Tag::new(b"kern"), 0, 19..25)];
|
||||
for direction in [Direction::LeftToRight, Direction::RightToLeft, Direction::TopToBottom] {
|
||||
let input = RunInput {
|
||||
text: "بaß1",
|
||||
source_start: 19,
|
||||
pre_context: "س",
|
||||
post_context: "م",
|
||||
direction,
|
||||
language: Some(&language),
|
||||
features: &features,
|
||||
character_clusters: true,
|
||||
uppercase: true,
|
||||
};
|
||||
let map = |c| if c == '1' { '٢' } else { c };
|
||||
let actual =
|
||||
shape_run(&shaper, &input, map, |s, d, script, lang, features| Arc::new(ShapePlan::new(s, d, script, lang, features))).unwrap();
|
||||
let mut buffer = UnicodeBuffer::new();
|
||||
for (i, c) in input.text.char_indices() {
|
||||
for upper in map(c).to_uppercase() {
|
||||
buffer.add(upper, 19 + i as u32);
|
||||
}
|
||||
}
|
||||
buffer.set_direction(direction);
|
||||
buffer.guess_segment_properties();
|
||||
buffer.set_cluster_level(BufferClusterLevel::Characters);
|
||||
buffer.set_language(language.clone());
|
||||
buffer.set_pre_context(input.pre_context);
|
||||
buffer.set_post_context(input.post_context);
|
||||
let script = Some(buffer.script()).filter(|s| *s != harfrust::script::UNKNOWN);
|
||||
let plan = ShapePlan::new(&shaper, direction, script, Some(&language), &features);
|
||||
let expected = shaper.shape(buffer, ShapeOptions::new().features(&features).plan(Some(&plan)));
|
||||
let tuples: Vec<_> = expected
|
||||
.glyph_infos()
|
||||
.iter()
|
||||
.zip(expected.glyph_positions())
|
||||
.map(|(i, p)| (i.glyph_id, i.cluster, p.x_advance, p.y_advance, p.x_offset, p.y_offset))
|
||||
.collect();
|
||||
assert_eq!(actual.iter().map(|g| (g.glyph_id, g.cluster, g.x_advance, g.y_advance, g.x_offset, g.y_offset)).collect::<Vec<_>>(), tuples);
|
||||
assert!(!actual.is_empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
//! Visual ordering of shaped clusters after line bidi levels are resolved.
|
||||
use std::ops::Range;
|
||||
use unicode_bidi::{BidiInfo, Level};
|
||||
|
||||
/// A glyph's source byte, source length and original resolved level.
|
||||
/// Zero-length glyphs represent list markers.
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct VisualGlyph {
|
||||
/// Byte offset in the original text.
|
||||
pub byte: usize,
|
||||
/// Source length in bytes, or zero for a list marker.
|
||||
pub len: usize,
|
||||
/// Original level, also used when the line levels lack this byte.
|
||||
pub level: Level,
|
||||
}
|
||||
|
||||
/// Order glyph indices by cluster using caller-resolved line levels (UAX #9 L1–L2).
|
||||
/// `levels` is indexed relative to `paragraph_start`. Adjacent marker glyphs at the
|
||||
/// same byte form their own cluster and reverse internally at a right-to-left level.
|
||||
pub fn visual_order(glyphs: impl IntoIterator<Item = VisualGlyph>, paragraph_start: usize, levels: &[Level]) -> Vec<usize> {
|
||||
let mut clusters: Vec<(Range<usize>, VisualGlyph)> = Vec::new();
|
||||
for (i, glyph) in glyphs.into_iter().enumerate() {
|
||||
if let Some((range, _)) = clusters.last_mut().filter(|(_, prev)| prev.byte == glyph.byte && (prev.len == 0) == (glyph.len == 0)) {
|
||||
range.end = i + 1;
|
||||
} else {
|
||||
clusters.push((i..i + 1, glyph));
|
||||
}
|
||||
}
|
||||
let cluster_levels: Vec<_> = clusters
|
||||
.iter()
|
||||
.map(|(_, g)| if g.len == 0 { g.level } else { levels.get(g.byte.saturating_sub(paragraph_start)).copied().unwrap_or(g.level) })
|
||||
.collect();
|
||||
BidiInfo::reorder_visual(&cluster_levels)
|
||||
.into_iter()
|
||||
.filter_map(|i| clusters.get(i))
|
||||
.flat_map(|(range, glyph)| {
|
||||
let mut indices: Vec<_> = range.clone().collect();
|
||||
if glyph.len == 0 && glyph.level.is_rtl() {
|
||||
indices.reverse();
|
||||
}
|
||||
indices
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn clusters_and_markers_keep_their_distinct_order() {
|
||||
let l = Level::ltr();
|
||||
let r = Level::rtl();
|
||||
let glyphs = [
|
||||
VisualGlyph { byte: 5, len: 0, level: r },
|
||||
VisualGlyph { byte: 5, len: 0, level: r },
|
||||
VisualGlyph { byte: 5, len: 2, level: r },
|
||||
VisualGlyph { byte: 5, len: 2, level: r },
|
||||
VisualGlyph { byte: 7, len: 1, level: l },
|
||||
];
|
||||
assert_eq!(visual_order(glyphs, 5, &[r, r, l]), vec![2, 3, 1, 0, 4]);
|
||||
assert!(visual_order([], 0, &[]).is_empty());
|
||||
}
|
||||
#[test]
|
||||
fn missing_levels_fall_back_to_glyph_level() {
|
||||
let r = Level::rtl();
|
||||
let glyphs = [VisualGlyph { byte: 2, len: 1, level: r }, VisualGlyph { byte: 3, len: 1, level: r }];
|
||||
assert_eq!(visual_order(glyphs, 0, &[]), vec![1, 0]);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,10 @@ workspace = true
|
||||
[features]
|
||||
corpus = []
|
||||
default = []
|
||||
# Opt in to threaded AVIF encoding without enabling AVIF (or assembly) by itself.
|
||||
threads = ["ravif?/threading", "rav1e?/threading"]
|
||||
sgi = []
|
||||
sunraster = []
|
||||
avif = ["image/avif", "dep:ravif", "dep:rav1e", "dep:avif-serialize"]
|
||||
heif = ["dep:craft-heif"]
|
||||
# AVIF decode: craft-video's clean-room pure-Rust AV1 decoder. Native targets only (on wasm32 a
|
||||
|
||||
@@ -57,6 +57,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`) | with feature `sunraster` (incl. RLE and colour maps) | with feature `sunraster` (uncompressed) | U8 | Gray, RGB, RGBA | yes | no | no | no | no | no | no | `sunraster` (off by default) |
|
||||
| SGI (`.sgi`, `.rgb`, `.bw`) | with feature `sgi` | with feature `sgi` (RLE) | U8, U16 | Gray, Gray+A, RGB, RGBA | yes | no | no | no | no | no | no | `sgi` (off by default) |
|
||||
| JPEG-LS (`.jls`) | yes (any NEAR; one scan per component) | yes (lossless) | U8, U16 | Gray, RGB | no | no | no | no | no | no | no | built in |
|
||||
| AVIF | with feature `avif-decode` (native only) | only with feature `avif` | U8, U16 (10/12-bit decodes to U16; PQ/HLG to linear F32) | RGB, RGBA | yes (auxiliary alpha) | read (`colr` prof/rICC) | read | read | no | no | yes | `craft-video` AV1 decoder; `image` + `ravif` to write |
|
||||
|
||||
@@ -22,6 +22,8 @@ pub(crate) mod png;
|
||||
pub(crate) mod pnm;
|
||||
#[cfg(feature = "sgi")]
|
||||
pub(crate) mod sgi;
|
||||
#[cfg(feature = "sunraster")]
|
||||
pub(crate) mod sunraster;
|
||||
pub(crate) mod tga;
|
||||
pub(crate) mod tiff;
|
||||
pub(crate) mod tiff_exif;
|
||||
|
||||
@@ -389,6 +389,15 @@ fn decode_craft_jpeg(bytes: &[u8], limits: &Limits, adobe_transform: Option<u8>,
|
||||
/// truncated or not, are far above this.
|
||||
const MAX_PIXELS_PER_BYTE: u64 = 2048;
|
||||
|
||||
/// The stored size and EXIF block [`decode`] would find, from the marker segments before the
|
||||
/// first scan: the first frame header's size, checked against `limits`, and the APP1 EXIF block.
|
||||
pub(crate) fn probe(bytes: &[u8], limits: &Limits) -> Result<(u32, u32, Option<Vec<u8>>), CodecError> {
|
||||
let meta = scan_metadata(bytes);
|
||||
let (w, h, _) = meta.frame.ok_or_else(|| err("no frame header before the image data"))?;
|
||||
limits.check_size(w, h)?;
|
||||
Ok((w, h, meta.exif))
|
||||
}
|
||||
|
||||
pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
let (end, scan_bytes) = data_end(bytes);
|
||||
if end == DataEnd::Empty {
|
||||
|
||||
@@ -27,6 +27,26 @@ pub(crate) fn dpi_of_ppm(ppm: u32) -> f32 {
|
||||
}
|
||||
|
||||
/// Decodes a whole PNG, by driving a [`PngDecodeRows`] to its end.
|
||||
/// The stored size and EXIF block [`decode`] would find, from the chunks before the image data:
|
||||
/// the same header checks (limits, a file too small for its image) and the same frame (an fcTL
|
||||
/// before the IDAT overrides the header's size), without inflating any pixels or text.
|
||||
pub(crate) fn probe(bytes: &[u8], limits: &Limits) -> Result<(u32, u32, Option<Vec<u8>>), CodecError> {
|
||||
let mut decoder = png::Decoder::new_with_limits(Cursor::new(bytes), png::Limits { bytes: limits.alloc_usize() });
|
||||
decoder.set_ignore_text_chunk(true);
|
||||
{
|
||||
let info = decoder.read_header_info().map_err(map_png_err)?;
|
||||
let (w, h) = info.size();
|
||||
limits.check_size(w, h)?;
|
||||
let stored = u64::from(w).saturating_mul(u64::from(h)).saturating_mul(info.bits_per_pixel() as u64).div_ceil(8);
|
||||
Limits::check_plausible(stored, bytes.len(), 1100)?;
|
||||
}
|
||||
let reader = decoder.read_info().map_err(map_png_err)?;
|
||||
let info = reader.info();
|
||||
let (w, h) = info.frame_control.map_or_else(|| info.size(), |f| (f.width, f.height));
|
||||
limits.check_size(w, h)?;
|
||||
Ok((w, h, info.exif_metadata.as_ref().map(|c| c.to_vec())))
|
||||
}
|
||||
|
||||
pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
PngDecodeRows::new(bytes, limits)?.step(&mut || true)?.ok_or_else(|| err("the decoder stopped before the image ended"))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,281 @@
|
||||
//! 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<Vec<u8>, 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<Image, CodecError> {
|
||||
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 => {
|
||||
let bit = (row[x / 8] >> (7 - x % 8)) & 1;
|
||||
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]);
|
||||
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];
|
||||
// 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] });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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<Vec<u8>, 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<Vec<u8>, 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 = 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());
|
||||
}
|
||||
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<u8> {
|
||||
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 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);
|
||||
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 { .. })));
|
||||
}
|
||||
}
|
||||
@@ -71,6 +71,18 @@ const TAG_DNG_VERSION: u16 = 50706;
|
||||
/// first IFD is often a reduced preview, so this runs before decoding.
|
||||
/// Reads only bounds-checked header bytes; anything malformed is "not raw".
|
||||
fn camera_raw(b: &[u8]) -> bool {
|
||||
camera_raw_mode(b, false)
|
||||
}
|
||||
|
||||
/// The older LightCraft RAW/TIFF classification policy, sharing this bounded directory walk.
|
||||
pub(crate) fn legacy_camera_raw(b: &[u8]) -> bool {
|
||||
camera_raw_mode(b, true)
|
||||
}
|
||||
|
||||
fn camera_raw_mode(b: &[u8], legacy: bool) -> bool {
|
||||
if legacy && b.get(8..10) == Some(b"CR") {
|
||||
return true;
|
||||
}
|
||||
let le = match b.get(0..4) {
|
||||
Some(b"II*\0") => true,
|
||||
Some(b"MM\0*") => false,
|
||||
@@ -92,8 +104,48 @@ fn camera_raw(b: &[u8]) -> bool {
|
||||
let value = |e: usize| match u16_at(e.saturating_add(2))? {
|
||||
3 => u16_at(e.saturating_add(8)).map(u32::from),
|
||||
4 | 13 => u32_at(e.saturating_add(8)),
|
||||
1 | 7 if legacy => b.get(e.saturating_add(8)).copied().map(u32::from),
|
||||
_ => None,
|
||||
};
|
||||
// The compatibility policy recognizes Sony lossless YCbCr only when IFD0 names Sony.
|
||||
let is_sony = if legacy {
|
||||
u32_at(4)
|
||||
.and_then(|v| usize::try_from(v).ok())
|
||||
.and_then(|ifd| {
|
||||
let n = usize::from(u16_at(ifd)?);
|
||||
if n > 4096 {
|
||||
return None;
|
||||
}
|
||||
for i in 0..n {
|
||||
let e = ifd.checked_add(2)?.checked_add(i.checked_mul(12)?)?;
|
||||
let tag = u16_at(e)?;
|
||||
let ty = u16_at(e + 2)?;
|
||||
let count = u32_at(e + 4)? as usize;
|
||||
if tag == 0x010f {
|
||||
let matches = (|| {
|
||||
let size = match ty {
|
||||
1 | 2 | 6 | 7 => 1usize,
|
||||
3 | 8 => 2,
|
||||
4 | 9 | 11 | 13 => 4,
|
||||
5 | 10 | 12 => 8,
|
||||
_ => return None,
|
||||
};
|
||||
let len = size.checked_mul(count)?;
|
||||
let start = if len <= 4 { e + 8 } else { u32_at(e + 8)? as usize };
|
||||
Some(b.get(start..start.checked_add(len)?)?.get(..4)?.eq_ignore_ascii_case(b"SONY"))
|
||||
})()
|
||||
.unwrap_or(false);
|
||||
if matches {
|
||||
return Some(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(false)
|
||||
})
|
||||
.unwrap_or(false)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let mut queue: Vec<u32> = u32_at(4).into_iter().collect();
|
||||
let mut visited = 0;
|
||||
while let Some(ifd) = queue.pop() {
|
||||
@@ -104,6 +156,44 @@ fn camera_raw(b: &[u8]) -> bool {
|
||||
}
|
||||
let ifd = ifd as usize;
|
||||
let Some(n) = u16_at(ifd) else { continue };
|
||||
if legacy
|
||||
&& (n > 4096
|
||||
|| (n > 0
|
||||
&& ifd
|
||||
.checked_add(2)
|
||||
.and_then(|p| usize::from(n - 1).checked_mul(12).and_then(|v| p.checked_add(v)))
|
||||
.and_then(|p| p.checked_add(8))
|
||||
.is_none_or(|end| end > b.len())))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if is_sony {
|
||||
let mut cfa = false;
|
||||
let (mut compression, mut photo, mut samples) = (None, None, None);
|
||||
let (mut saw_compression, mut saw_photo, mut saw_samples) = (false, false, false);
|
||||
for i in 0..usize::from(n) {
|
||||
let e = ifd.saturating_add(2 + 12 * i);
|
||||
match u16_at(e) {
|
||||
Some(0x828e) => cfa = true,
|
||||
Some(0x0103) if !saw_compression => {
|
||||
saw_compression = true;
|
||||
compression = value(e);
|
||||
}
|
||||
Some(0x0106) if !saw_photo => {
|
||||
saw_photo = true;
|
||||
photo = value(e);
|
||||
}
|
||||
Some(0x0115) if !saw_samples => {
|
||||
saw_samples = true;
|
||||
samples = value(e);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if cfa && compression == Some(7) && photo == Some(6) && samples == Some(3) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for i in 0..usize::from(n) {
|
||||
let e = ifd.saturating_add(2 + 12 * i);
|
||||
let (Some(tag), Some(count)) = (u16_at(e), u32_at(e.saturating_add(4))) else {
|
||||
@@ -111,6 +201,7 @@ fn camera_raw(b: &[u8]) -> bool {
|
||||
};
|
||||
match tag {
|
||||
TAG_DNG_VERSION => return true,
|
||||
0x0103 if legacy && matches!(value(e), Some(32767 | 32769 | 32770 | 34316 | 34713 | 65000 | 65535)) => return true,
|
||||
TAG_PHOTOMETRIC if matches!(value(e), Some(PHOTOMETRIC_CFA | PHOTOMETRIC_LINEAR_RAW)) => {
|
||||
return true;
|
||||
}
|
||||
@@ -125,7 +216,7 @@ fn camera_raw(b: &[u8]) -> bool {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if let Some(next) = u32_at(ifd.saturating_add(2 + 12 * usize::from(n))).filter(|&o| o != 0) {
|
||||
if !legacy && let Some(next) = u32_at(ifd.saturating_add(2 + 12 * usize::from(n))).filter(|&o| o != 0) {
|
||||
queue.push(next);
|
||||
}
|
||||
}
|
||||
@@ -171,7 +262,7 @@ mod tags;
|
||||
mod ycbcr;
|
||||
|
||||
pub use decode::TiffBands;
|
||||
pub(crate) use decode::{decode, decode_page, has_unretained_samples, orientation};
|
||||
pub(crate) use decode::{decode, decode_page, has_unretained_samples, orientation, probe};
|
||||
pub(crate) use encode::encode;
|
||||
#[cfg(test)]
|
||||
use encode::{Extras, try_with_capacity};
|
||||
|
||||
@@ -13,6 +13,17 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError>
|
||||
decode_page(bytes, None, limits)
|
||||
}
|
||||
|
||||
/// The stored size and Orientation (1–8) of the page [`decode`] opens, from its directory alone:
|
||||
/// the same page choice and refusals (camera raw, no directory), checked against `limits`,
|
||||
/// without reading any strips or tiles.
|
||||
pub(crate) fn probe(bytes: &[u8], limits: &Limits) -> Result<(u32, u32, u16), CodecError> {
|
||||
let (file, dir, _) = open_page(bytes, None)?;
|
||||
let side = |tag: u16| file.tag_uint(&dir, tag).and_then(|v| u32::try_from(v).ok()).unwrap_or(0);
|
||||
let (w, h) = (side(ifd::TAG_WIDTH), side(ifd::TAG_HEIGHT));
|
||||
limits.check_size(w, h)?;
|
||||
Ok((w, h, ifd::orientation_of(&file, &dir)))
|
||||
}
|
||||
|
||||
/// Decodes page `page` of [`ifd::tiff_info`]'s list (`None`: the default page). The image
|
||||
/// carries the page's metadata, a [`DecodeWarning::MorePages`] when the file holds more
|
||||
/// full-resolution pages, and a [`DecodeWarning::Truncated`] when image data is cut off.
|
||||
|
||||
@@ -45,6 +45,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,
|
||||
/// SGI image files (`.sgi`, `.rgb`, `.bw`): 8- and 16-bit gray, gray + alpha, RGB and RGBA.
|
||||
Sgi,
|
||||
/// JPEG-LS (`.jls`): lossless and near-lossless gray and RGB.
|
||||
@@ -120,7 +122,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; 26] = [
|
||||
pub const ALL: [Format; 27] = [
|
||||
Format::Png,
|
||||
Format::Jpeg,
|
||||
Format::Jp2,
|
||||
@@ -145,6 +147,7 @@ impl Format {
|
||||
Format::Cineon,
|
||||
Format::Dicom,
|
||||
Format::Iff,
|
||||
Format::SunRaster,
|
||||
Format::Sgi,
|
||||
Format::JpegLs,
|
||||
];
|
||||
@@ -185,6 +188,7 @@ impl Format {
|
||||
Format::Dicom => "DICOM",
|
||||
Format::Iff => "Amiga IFF",
|
||||
Format::Jp2 => "JPEG 2000",
|
||||
Format::SunRaster => "Sun Raster",
|
||||
Format::Sgi => "SGI",
|
||||
Format::JpegLs => "JPEG-LS",
|
||||
}
|
||||
@@ -218,6 +222,7 @@ impl Format {
|
||||
Format::Dicom => &["dcm", "dicom", "dc3"],
|
||||
Format::Iff => &["iff", "lbm", "ilbm"],
|
||||
Format::Jp2 => &["jp2", "j2k", "j2c", "jpf", "jpx"],
|
||||
Format::SunRaster => &["ras", "sun"],
|
||||
Format::Sgi => &["sgi", "rgb", "rgba", "bw"],
|
||||
Format::JpegLs => &["jls"],
|
||||
}
|
||||
@@ -249,6 +254,7 @@ impl Format {
|
||||
Format::Dicom => "application/dicom",
|
||||
Format::Iff => "image/x-iff",
|
||||
Format::Jp2 => "image/jp2",
|
||||
Format::SunRaster => "image/x-sun-raster",
|
||||
Format::Sgi => "image/x-sgi",
|
||||
Format::JpegLs => "image/jls",
|
||||
}
|
||||
@@ -327,6 +333,13 @@ 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 {
|
||||
read: cfg!(feature = "sunraster"),
|
||||
write: cfg!(feature = "sunraster"),
|
||||
layouts: &[L::Gray, L::Rgb, L::Rgba],
|
||||
alpha: true,
|
||||
..base
|
||||
},
|
||||
Format::Sgi => {
|
||||
FormatCaps { read: cfg!(feature = "sgi"), write: cfg!(feature = "sgi"), depths: &[S::U8, S::U16], layouts: RGB_GRAY, alpha: true, ..base }
|
||||
}
|
||||
@@ -412,6 +425,10 @@ pub fn detect(bytes: &[u8]) -> Option<Format> {
|
||||
if b.starts_with(b"/* XPM */") || b.starts_with(b"! XPM2\n") || b.starts_with(b"! XPM2\r\n") {
|
||||
return Some(Format::Xpm);
|
||||
}
|
||||
#[cfg(feature = "sunraster")]
|
||||
if crate::codecs::sunraster::looks_like_sunraster(b) {
|
||||
return Some(Format::SunRaster);
|
||||
}
|
||||
#[cfg(feature = "sgi")]
|
||||
if crate::codecs::sgi::looks_like_sgi(b) {
|
||||
return Some(Format::Sgi);
|
||||
|
||||
@@ -35,6 +35,7 @@ pub mod resolution;
|
||||
pub mod source_settings;
|
||||
pub mod tiff_probe;
|
||||
pub mod web;
|
||||
pub mod webp_probe;
|
||||
pub use crate::allocation::{probe_allocation_size, probe_stored_size};
|
||||
pub use crate::cicp::Cicp;
|
||||
pub use crate::cicp::ContentLightLevel;
|
||||
@@ -69,6 +70,8 @@ pub use half::f16;
|
||||
|
||||
#[cfg(feature = "sgi")]
|
||||
use crate::codecs::sgi;
|
||||
#[cfg(feature = "sunraster")]
|
||||
use crate::codecs::sunraster;
|
||||
use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jpegls, jxl, pcx, png, pnm, tiff, via_image, webp, xpm};
|
||||
|
||||
/// Detect the format and decode with default [`Limits`].
|
||||
@@ -108,6 +111,16 @@ 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 => {
|
||||
#[cfg(feature = "sunraster")]
|
||||
{
|
||||
sunraster::decode(bytes, l)
|
||||
}
|
||||
#[cfg(not(feature = "sunraster"))]
|
||||
{
|
||||
Err(CodecError::unsupported(format, "Sun Raster support requires the sunraster feature"))
|
||||
}
|
||||
}
|
||||
Format::Sgi => {
|
||||
#[cfg(feature = "sgi")]
|
||||
{
|
||||
@@ -225,11 +238,12 @@ pub fn decode_png_rows<E: From<CodecError>>(
|
||||
}
|
||||
|
||||
/// The upright pixel size [`decode`] would return, read from the file's header without decoding
|
||||
/// pixels, for WebP, OpenEXR (flat), AVIF (with `avif-decode`) and the GIF/BMP/TGA/ICO/QOI/HDR family. It runs the same
|
||||
/// header reads and size checks as the decoder (default [`Limits`]) and swaps the size for an
|
||||
/// EXIF orientation of 5–8 (AVIF: its container's `clap` and `irot`), so wherever [`decode`]
|
||||
/// succeeds the two agree. Other formats (and deep EXR) return [`CodecError::Unsupported`]: decode
|
||||
/// those to learn their size.
|
||||
/// pixels, for PNG, JPEG, TIFF (the page [`decode`] opens), WebP, OpenEXR (flat), AVIF (with
|
||||
/// `avif-decode`) and the GIF/BMP/TGA/ICO/QOI/HDR family. It runs the same header reads and size
|
||||
/// checks as the decoder (default [`Limits`]) and swaps the size for an orientation of 5–8 (EXIF,
|
||||
/// the TIFF page's own tag, or AVIF's container `clap` and `irot`), so wherever [`decode`]
|
||||
/// succeeds the two agree. Other formats (and deep EXR) return [`CodecError::Unsupported`]:
|
||||
/// decode those to learn their size.
|
||||
pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> {
|
||||
let format = detect(bytes).ok_or(CodecError::UnknownFormat)?;
|
||||
if !caps(format).read {
|
||||
@@ -237,6 +251,12 @@ pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> {
|
||||
}
|
||||
let l = &Limits::default();
|
||||
let (w, h, exif) = match format {
|
||||
Format::Png => png::probe(bytes, l)?,
|
||||
Format::Jpeg => jpeg::probe(bytes, l)?,
|
||||
Format::Tiff => {
|
||||
let (w, h, o) = tiff::probe(bytes, l)?;
|
||||
return Ok(if (5..=8).contains(&o) { (h, w) } else { (w, h) });
|
||||
}
|
||||
Format::WebP => webp::probe(bytes, l)?,
|
||||
Format::OpenExr => exr::probe(bytes, l).map(|(w, h)| (w, h, None))?,
|
||||
#[cfg(all(feature = "avif-decode", not(target_arch = "wasm32")))]
|
||||
@@ -250,6 +270,12 @@ pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> {
|
||||
Ok(if (5..=8).contains(&o) { (h, w) } else { (w, h) })
|
||||
}
|
||||
|
||||
/// Classify classic TIFF camera RAWs using LightCraft's historical bounded heuristic.
|
||||
/// This compatibility policy differs from the decoder's own RAW rejection policy.
|
||||
pub fn legacy_tiff_raw_hint(bytes: &[u8]) -> bool {
|
||||
tiff::legacy_camera_raw(bytes)
|
||||
}
|
||||
|
||||
/// A JPEG's frame as its header declares it, read without decoding pixels: the stored size, the
|
||||
/// layout [`decode`] returns, whether [`decode`] hands the file to its whole-coefficient
|
||||
/// decoder (arithmetic-coded, lossless or 12-bit frames), whose working set is several times the
|
||||
@@ -493,6 +519,16 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result<Vec
|
||||
Format::Cineon => 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 => {
|
||||
#[cfg(feature = "sunraster")]
|
||||
{
|
||||
sunraster::encode(image, plan)
|
||||
}
|
||||
#[cfg(not(feature = "sunraster"))]
|
||||
{
|
||||
Err(CodecError::unsupported(format, "Sun Raster support requires the sunraster feature"))
|
||||
}
|
||||
}
|
||||
Format::Sgi => {
|
||||
#[cfg(feature = "sgi")]
|
||||
{
|
||||
|
||||
@@ -88,7 +88,7 @@ pub fn header_with_max_pixels(b: &[u8], max_pixels: u64) -> Result<(u32, u32, u1
|
||||
if !after_idat {
|
||||
return Err(err("truncated image data"));
|
||||
}
|
||||
Ok((w, h, exif.map_or(1, crate::exif_orientation)))
|
||||
Ok((w, h, exif.map_or(1, craft_metadata::compat_orientation::orientation)))
|
||||
}
|
||||
|
||||
/// Container color declarations preceding image data. Raw values leave color policy to callers.
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
//! 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, craft_metadata::compat_orientation::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 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 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));
|
||||
}
|
||||
|
||||
#[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());
|
||||
}
|
||||
}
|
||||
@@ -62,6 +62,8 @@ fn default_build_formats_all_symmetric() {
|
||||
Format::Dpx,
|
||||
Format::Cineon,
|
||||
Format::Iff,
|
||||
#[cfg(feature = "sunraster")]
|
||||
Format::SunRaster,
|
||||
#[cfg(feature = "sgi")]
|
||||
Format::Sgi,
|
||||
Format::JpegLs,
|
||||
@@ -258,6 +260,20 @@ fn detect_magic_jpeg_ls() {
|
||||
assert!(decode_as(Format::JpegLs, &[0xFF, 0xD8, 0xFF, 0xF7]).is_err());
|
||||
}
|
||||
|
||||
#[cfg(feature = "sunraster")]
|
||||
#[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));
|
||||
@@ -360,3 +376,15 @@ fn sgi_requires_explicit_feature() {
|
||||
header[..12].copy_from_slice(&[1, 218, 0, 1, 0, 2, 0, 1, 0, 1, 0, 1]);
|
||||
assert_eq!(detect(&header), None);
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "sunraster"))]
|
||||
#[test]
|
||||
fn sun_raster_disabled_by_default() {
|
||||
use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode};
|
||||
let bytes = [0x59, 0xa6, 0x6a, 0x95, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 8, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 42, 0];
|
||||
assert_ne!(detect(&bytes), Some(Format::SunRaster));
|
||||
assert!(!caps(Format::SunRaster).read && !caps(Format::SunRaster).write);
|
||||
assert!(decode_as(Format::SunRaster, &bytes).is_err());
|
||||
let image = Image::from_u8(1, 1, ChannelLayout::Gray, vec![42]).unwrap();
|
||||
assert!(encode(&image, Format::SunRaster, &EncodeOptions::default()).is_err());
|
||||
}
|
||||
|
||||
@@ -671,3 +671,32 @@ fn highly_compressible_jpeg_and_gif_still_decode() {
|
||||
assert_eq!(decode(&b).unwrap().width(), 3000, "{f:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// `probe_size` refuses the headers `decode` refuses, from the header alone: a size past the
|
||||
/// limits, and one at the limit that a tiny file cannot hold.
|
||||
#[test]
|
||||
fn probe_size_refuses_png_bomb_headers() {
|
||||
for side in [100_000u32, 32768] {
|
||||
let mut ihdr = Vec::new();
|
||||
ihdr.extend_from_slice(&side.to_be_bytes());
|
||||
ihdr.extend_from_slice(&side.to_be_bytes());
|
||||
ihdr.extend_from_slice(&[16, 6, 0, 0, 0]);
|
||||
let mut b = b"\x89PNG\r\n\x1a\n".to_vec();
|
||||
b.extend(png_chunk(b"IHDR", &ihdr));
|
||||
b.extend(png_chunk(b"IDAT", &[0x78, 0x9C, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01]));
|
||||
b.extend(png_chunk(b"IEND", &[]));
|
||||
assert!(matches!(probe_size(&b), Err(CodecError::LimitExceeded(_))), "{side}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A JPEG header declaring a size past the default limits is refused by `probe_size` as by
|
||||
/// `decode`, without reading the scan.
|
||||
#[test]
|
||||
fn probe_size_refuses_an_oversized_jpeg_header() {
|
||||
let mut jpeg = encoded(Format::Jpeg);
|
||||
assert_eq!(probe_size(&jpeg).unwrap(), (64, 48));
|
||||
let sof = jpeg.windows(2).position(|w| matches!(w, [0xFF, 0xC0..=0xC2])).unwrap();
|
||||
jpeg[sof + 5..sof + 9].copy_from_slice(&[0xFF, 0xFF, 0xFF, 0xFF]);
|
||||
assert!(decode(&jpeg).is_err());
|
||||
assert!(matches!(probe_size(&jpeg), Err(CodecError::LimitExceeded(_))), "{:?}", probe_size(&jpeg));
|
||||
}
|
||||
|
||||
@@ -202,6 +202,7 @@ fn png_opens_upright_for_every_orientation() {
|
||||
let file = png_with_exif(&png, &exif(u32::from(o), o % 2 == 0, false));
|
||||
let img = decode(&file).unwrap();
|
||||
assert_eq!(img.dimensions(), (w as u32, h as u32), "o={o}");
|
||||
assert_eq!(probe_size(&file).unwrap(), img.dimensions(), "the header probe turns upright too, o={o}");
|
||||
assert_eq!(img.data(), &up[..], "o={o}");
|
||||
assert_eq!(exif_orientation(img.meta.exif.as_deref().unwrap()), 1, "o={o}: metadata says upright");
|
||||
// Decoded in steps and finished by `finish_png_decode`, it is the same image.
|
||||
@@ -309,8 +310,10 @@ fn tiff_opens_upright_for_every_orientation() {
|
||||
let up = exact_scene(w, h);
|
||||
for o in 1..=8u16 {
|
||||
let (s, sw, sh) = stored(&up, w, h, o);
|
||||
let img = decode(&tiff_with_orientation(&s, sw as u32, sh as u32, o)).unwrap();
|
||||
let file = tiff_with_orientation(&s, sw as u32, sh as u32, o);
|
||||
let img = decode(&file).unwrap();
|
||||
assert_eq!(img.dimensions(), (w as u32, h as u32), "o={o}");
|
||||
assert_eq!(probe_size(&file).unwrap(), img.dimensions(), "the header probe turns upright too, o={o}");
|
||||
assert_eq!(img.data(), &up[..], "o={o}");
|
||||
}
|
||||
}
|
||||
@@ -358,6 +361,7 @@ fn jpeg_opens_upright_for_every_orientation() {
|
||||
let file = jpeg_with_exif(&jpeg, &exif(u32::from(o), o > 4, o == 7));
|
||||
let img = decode(&file).unwrap();
|
||||
assert_blocks(&img, &up, w, h, &format!("o={o}"));
|
||||
assert_eq!(probe_size(&file).unwrap(), img.dimensions(), "the header probe turns upright too, o={o}");
|
||||
assert_eq!(exif_orientation(img.meta.exif.as_deref().unwrap()), 1);
|
||||
}
|
||||
}
|
||||
@@ -659,7 +663,19 @@ fn long_orientation_reads_and_rewrites_in_both_byte_orders() {
|
||||
#[test]
|
||||
fn probe_size_matches_decode() {
|
||||
let img = synth(7, 5, ChannelLayout::Rgb, SampleType::U8, 3, 0.0);
|
||||
let probed = [Format::WebP, Format::Gif, Format::Bmp, Format::Tga, Format::Qoi, Format::Hdr, Format::Ico, Format::OpenExr];
|
||||
let probed = [
|
||||
Format::Png,
|
||||
Format::Jpeg,
|
||||
Format::Tiff,
|
||||
Format::WebP,
|
||||
Format::Gif,
|
||||
Format::Bmp,
|
||||
Format::Tga,
|
||||
Format::Qoi,
|
||||
Format::Hdr,
|
||||
Format::Ico,
|
||||
Format::OpenExr,
|
||||
];
|
||||
assert!(probed.iter().all(|f| rw_formats().contains(f)));
|
||||
for format in rw_formats() {
|
||||
let bytes = encode(&img, format, &EncodeOptions::default()).unwrap();
|
||||
|
||||
@@ -539,6 +539,7 @@ fn every_ifd_and_subifd_is_listed_and_decodable() {
|
||||
// Like Photoshop: the first full-resolution page opens; the thumbnail is skipped.
|
||||
let img = decode_ok(&b.bytes);
|
||||
assert_eq!((img.dimensions(), img.data()[0]), ((9, 7), 2));
|
||||
assert_eq!(probe_size(&b.bytes).unwrap(), (9, 7), "the header probe sizes the page decode opens");
|
||||
assert_eq!(img.warnings, [DecodeWarning::MorePages { total: Some(2) }]);
|
||||
// Every directory decodes on request.
|
||||
for (i, (w, level)) in [(4, 1u8), (9, 2), (3, 5), (2, 6), (9, 3), (5, 4)].into_iter().enumerate() {
|
||||
@@ -554,6 +555,7 @@ fn a_file_of_only_thumbnails_still_opens() {
|
||||
let b = build(true, false, &[gray_page(4, 3, 7, Some(1))], &[0]);
|
||||
assert_eq!(tiff_info(&b.bytes).unwrap().default_page(), Some(0));
|
||||
assert_eq!(decode_ok(&b.bytes).data()[0], 7);
|
||||
assert_eq!(probe_size(&b.bytes).unwrap(), (4, 3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -698,6 +700,9 @@ fn hostile_sizes_and_counts_are_refused_without_allocating() {
|
||||
patch_value(&mut b, little, big, 0, 256, 0, wv);
|
||||
patch_value(&mut b, little, big, 0, 257, 0, hv);
|
||||
assert!(decode(&b.bytes).is_err(), "{wv}x{hv}");
|
||||
if wv > 300_000 || hv > 300_000 {
|
||||
assert!(matches!(probe_size(&b.bytes), Err(CodecError::LimitExceeded(_))), "{wv}x{hv}: probe");
|
||||
}
|
||||
assert!(decode_with(&b.bytes, &DecodeOptions { limits: Limits::none(), ..Default::default() }).is_err(), "{wv}x{hv} without limits");
|
||||
}
|
||||
// A strip-offset count far past the end of the file.
|
||||
@@ -805,6 +810,7 @@ fn poke(bytes: &[u8]) {
|
||||
let _ = decode_with(bytes, &DecodeOptions { limits: Limits { max_alloc: 16 << 20, ..Limits::default() }, ..Default::default() });
|
||||
let _ = exif_orientation(bytes);
|
||||
let _ = tiff_orientation(bytes);
|
||||
let _ = probe_size(bytes);
|
||||
if let Ok(info) = tiff_info(bytes) {
|
||||
for i in 0..info.pages.len().min(8) {
|
||||
let _ = decode_tiff_page(bytes, i, &DecodeOptions::default());
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
use craft_image_codecs::legacy_tiff_raw_hint;
|
||||
|
||||
fn tiff(tag: u16, ty: u16, value: u32) -> Vec<u8> {
|
||||
let mut b = b"II\x2a\0\x08\0\0\0\x01\0".to_vec();
|
||||
b.extend_from_slice(&tag.to_le_bytes());
|
||||
b.extend_from_slice(&ty.to_le_bytes());
|
||||
b.extend_from_slice(&1u32.to_le_bytes());
|
||||
b.extend_from_slice(&value.to_le_bytes());
|
||||
b.extend_from_slice(&0u32.to_le_bytes());
|
||||
b
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recognizes_legacy_raw_compression_and_broad_cr_marker() {
|
||||
assert!(legacy_tiff_raw_hint(&tiff(0x0103, 3, 34713)));
|
||||
assert!(!legacy_tiff_raw_hint(&tiff(0x0103, 3, 1)));
|
||||
assert!(legacy_tiff_raw_hint(b"II*\0\x08\0\0\0CR"));
|
||||
let mut cut = tiff(0xc612, 1, 1);
|
||||
cut.truncate(17);
|
||||
assert!(!legacy_tiff_raw_hint(&cut));
|
||||
}
|
||||
|
||||
/// A little-endian TIFF with one IFD of (tag, type, count, value) entries and `tail` after it.
|
||||
fn ifd(entries: &[(u16, u16, u32, [u8; 4])], tail: &[u8]) -> Vec<u8> {
|
||||
let mut b = b"II\x2a\0\x08\0\0\0".to_vec();
|
||||
b.extend_from_slice(&(entries.len() as u16).to_le_bytes());
|
||||
for (tag, ty, count, value) in entries {
|
||||
b.extend_from_slice(&tag.to_le_bytes());
|
||||
b.extend_from_slice(&ty.to_le_bytes());
|
||||
b.extend_from_slice(&count.to_le_bytes());
|
||||
b.extend_from_slice(value);
|
||||
}
|
||||
b.extend_from_slice(&0u32.to_le_bytes());
|
||||
b.extend_from_slice(tail);
|
||||
b
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sony_lossless_ycbcr_needs_a_sony_make_and_every_layout_tag() {
|
||||
let short = |v: u16| [v.to_le_bytes()[0], v.to_le_bytes()[1], 0, 0];
|
||||
let layout = [(0x828e, 1, 4, [0, 1, 1, 2]), (0x0103, 3, 1, short(7)), (0x0106, 3, 1, short(6)), (0x0115, 3, 1, short(3))];
|
||||
// Make stored after the IFD (ASCII, 5 bytes): 8 + 2 + 5 * 12 + 4 = 74.
|
||||
let with_make = |make: (u16, u16, u32, [u8; 4]), tail: &[u8]| ifd(&[&[make], &layout[..]].concat(), tail);
|
||||
assert!(legacy_tiff_raw_hint(&with_make((0x010f, 2, 5, 74u32.to_le_bytes()), b"SONY\0")));
|
||||
assert!(legacy_tiff_raw_hint(&with_make((0x010f, 7, 4, *b"sony"), b"")));
|
||||
assert!(!legacy_tiff_raw_hint(&with_make((0x010f, 2, 6, 74u32.to_le_bytes()), b"Nikon\0")));
|
||||
assert!(!legacy_tiff_raw_hint(&with_make((0x010f, 99, 4, *b"SONY"), b"")));
|
||||
assert!(!legacy_tiff_raw_hint(&with_make((0x010f, 2, 5, 9999u32.to_le_bytes()), b"")));
|
||||
// A Sony file without the full lossless layout is not a RAW by this rule.
|
||||
let partial = ifd(&[(0x010f, 7, 4, *b"SONY"), layout[0], layout[1], layout[2]], b"");
|
||||
assert!(!legacy_tiff_raw_hint(&partial));
|
||||
// Repeated tags: the first value wins.
|
||||
let repeated = ifd(&[&[(0x010f, 7, 4, *b"SONY")], &layout[..], &[(0x0103, 3, 1, short(1))]].concat(), b"");
|
||||
assert!(legacy_tiff_raw_hint(&repeated));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_or_cut_directories_are_skipped() {
|
||||
let mut huge = b"II\x2a\0\x08\0\0\0".to_vec();
|
||||
huge.extend_from_slice(&5000u16.to_le_bytes());
|
||||
assert!(!legacy_tiff_raw_hint(&huge));
|
||||
let mut cut = ifd(&[(0x010f, 7, 4, *b"SONY"), (0x0103, 3, 1, [7, 0, 0, 0])], b"");
|
||||
cut.truncate(20);
|
||||
assert!(!legacy_tiff_raw_hint(&cut));
|
||||
}
|
||||
@@ -10,5 +10,9 @@ repository.workspace = true
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[features]
|
||||
async-lines = ["dep:tokio"]
|
||||
|
||||
[dependencies]
|
||||
serde_json = { workspace = true }
|
||||
tokio = { version = "1", optional = true, features = ["rt", "sync", "io-util", "macros"] }
|
||||
|
||||
@@ -0,0 +1,223 @@
|
||||
//! JSON-lines adapter that forwards valid JSON to a service and emits JSON-RPC parse errors.
|
||||
use serde_json::{Value, json};
|
||||
use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader, DuplexStream};
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
/// The service-facing streams and the tasks forwarding the client streams.
|
||||
pub struct Adapter {
|
||||
/// Input for the JSON-RPC service.
|
||||
pub service_in: DuplexStream,
|
||||
/// Output from the JSON-RPC service.
|
||||
pub service_out: DuplexStream,
|
||||
/// Forwarding tasks, to finish after the service exits.
|
||||
pub tasks: Tasks,
|
||||
}
|
||||
/// Background forwarding tasks.
|
||||
pub struct Tasks {
|
||||
reader: JoinHandle<()>,
|
||||
writer: JoinHandle<()>,
|
||||
}
|
||||
impl Tasks {
|
||||
/// Stop the reader and allow queued output to finish, as after a service exits.
|
||||
/// This waits for a blocked output sink; callers needing cancellation can call `abort` instead.
|
||||
pub async fn finish(self) {
|
||||
self.reader.abort();
|
||||
let _ = self.writer.await;
|
||||
}
|
||||
/// Cancel both forwarding tasks, including a writer blocked on output.
|
||||
pub fn abort(self) {
|
||||
self.reader.abort();
|
||||
self.writer.abort();
|
||||
}
|
||||
}
|
||||
|
||||
async fn bounded_line(reader: &mut BufReader<impl AsyncRead + Unpin>, bytes: &mut Vec<u8>, limit: usize) -> std::io::Result<Option<String>> {
|
||||
loop {
|
||||
let chunk = reader.fill_buf().await?;
|
||||
if chunk.is_empty() {
|
||||
if bytes.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
if bytes.len() > limit {
|
||||
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "JSON-RPC line exceeds limit"));
|
||||
}
|
||||
break;
|
||||
}
|
||||
let newline = chunk.iter().position(|b| *b == b'\n');
|
||||
let count = newline.unwrap_or(chunk.len());
|
||||
let size = bytes.len().saturating_add(count);
|
||||
let last = if count == 0 { bytes.last().copied() } else { Some(chunk[count - 1]) };
|
||||
if size > limit && !(size == limit.saturating_add(1) && last == Some(b'\r')) {
|
||||
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "JSON-RPC line exceeds limit"));
|
||||
}
|
||||
bytes.try_reserve(count).map_err(|_| std::io::Error::new(std::io::ErrorKind::OutOfMemory, "JSON-RPC line allocation failed"))?;
|
||||
bytes.extend_from_slice(&chunk[..count]);
|
||||
reader.consume(count + usize::from(newline.is_some()));
|
||||
if newline.is_some() {
|
||||
if bytes.last() == Some(&b'\r') {
|
||||
bytes.pop();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
String::from_utf8(std::mem::take(bytes)).map(Some).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
|
||||
}
|
||||
/// Spawn a line adapter. Invalid JSON gets a JSON-RPC parse error; valid values pass through.
|
||||
/// The bounded error queue applies backpressure if the client stops reading. Input and output lines
|
||||
/// above their respective limits close their forwarding direction, including unterminated lines.
|
||||
/// Buffers grow incrementally; forwarding and JSON parsing can temporarily hold additional copies.
|
||||
pub fn spawn(
|
||||
input: impl AsyncRead + Unpin + Send + 'static,
|
||||
output: impl AsyncWrite + Unpin + Send + 'static,
|
||||
input_limit: usize,
|
||||
output_limit: usize,
|
||||
) -> Adapter {
|
||||
const PIPE_BYTES: usize = 1 << 20;
|
||||
const MAX_QUEUED_PARSE_ERRORS: usize = 64;
|
||||
let (mut to_service, service_in) = tokio::io::duplex(PIPE_BYTES);
|
||||
let (service_out, from_service) = tokio::io::duplex(PIPE_BYTES);
|
||||
let (err_tx, mut err_rx) = tokio::sync::mpsc::channel::<String>(MAX_QUEUED_PARSE_ERRORS);
|
||||
let reader = tokio::spawn(async move {
|
||||
let mut lines = BufReader::new(input);
|
||||
let mut partial = Vec::new();
|
||||
loop {
|
||||
let line = tokio::select! {
|
||||
l = bounded_line(&mut lines, &mut partial, input_limit) => match l { Ok(Some(l)) => l, _ => break },
|
||||
() = err_tx.closed() => break,
|
||||
};
|
||||
if line.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
match serde_json::from_str::<Value>(&line) {
|
||||
Ok(_) => {
|
||||
if to_service.write_all(format!("{line}\n").as_bytes()).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
let reply = json!({"jsonrpc": "2.0", "id": null, "error": {"code": -32700, "message": format!("parse error: {e}")}});
|
||||
if err_tx.send(reply.to_string()).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = to_service.shutdown().await;
|
||||
});
|
||||
let writer = tokio::spawn(async move {
|
||||
let mut output = output;
|
||||
let mut replies = BufReader::new(from_service);
|
||||
let mut partial = Vec::new();
|
||||
loop {
|
||||
let line = tokio::select! {
|
||||
l = bounded_line(&mut replies, &mut partial, output_limit) => match l { Ok(Some(l)) => l, _ => break },
|
||||
Some(e) = err_rx.recv() => e,
|
||||
};
|
||||
if output.write_all(format!("{line}\n").as_bytes()).await.is_err() || output.flush().await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
Adapter { service_in, service_out, tasks: Tasks { reader, writer } }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn forwards_valid_lines_and_returns_parse_errors() {
|
||||
let (mut client_in, input) = tokio::io::duplex(1024);
|
||||
let (output, client_out) = tokio::io::duplex(1024);
|
||||
let Adapter { mut service_in, mut service_out, tasks } = spawn(input, output, 1024, 1024);
|
||||
client_in.write_all(b"\n{bad}\n{\"id\":1}\n").await.unwrap();
|
||||
client_in.shutdown().await.unwrap();
|
||||
let mut forwarded = String::new();
|
||||
service_in.read_to_string(&mut forwarded).await.unwrap();
|
||||
assert_eq!(forwarded, "{\"id\":1}\n");
|
||||
let mut lines = BufReader::new(client_out).lines();
|
||||
let error: Value = serde_json::from_str(&lines.next_line().await.unwrap().unwrap()).unwrap();
|
||||
assert_eq!(error["error"]["code"], -32700);
|
||||
assert!(error["id"].is_null());
|
||||
service_out.write_all(b"{\"id\":1,\"result\":true}\n").await.unwrap();
|
||||
let result: Value = serde_json::from_str(&lines.next_line().await.unwrap().unwrap()).unwrap();
|
||||
assert_eq!(result["result"], true);
|
||||
assert_eq!(result["id"], 1);
|
||||
service_out.shutdown().await.unwrap();
|
||||
tasks.finish().await;
|
||||
}
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn cancelled_line_read_retains_consumed_prefix() {
|
||||
use std::future::{Future, poll_fn};
|
||||
use std::task::Poll;
|
||||
let (mut writer, stream) = tokio::io::duplex(32);
|
||||
let mut reader = BufReader::new(stream);
|
||||
let mut partial = Vec::new();
|
||||
writer.write_all(b"prefix").await.unwrap();
|
||||
{
|
||||
let mut read = Box::pin(bounded_line(&mut reader, &mut partial, 32));
|
||||
poll_fn(|cx| {
|
||||
assert!(matches!(read.as_mut().poll(cx), Poll::Pending));
|
||||
Poll::Ready(())
|
||||
})
|
||||
.await;
|
||||
}
|
||||
assert_eq!(partial, b"prefix");
|
||||
writer.write_all(b" suffix\n").await.unwrap();
|
||||
assert_eq!(bounded_line(&mut reader, &mut partial, 32).await.unwrap().as_deref(), Some("prefix suffix"));
|
||||
}
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn parse_error_does_not_discard_partial_service_reply() {
|
||||
let (mut client, input) = tokio::io::duplex(128);
|
||||
let (output, client_out) = tokio::io::duplex(128);
|
||||
let Adapter { service_in: _, mut service_out, tasks } = spawn(input, output, 128, 128);
|
||||
service_out.write_all(b"{\"id\":1,").await.unwrap();
|
||||
tokio::task::yield_now().await;
|
||||
client.write_all(b"{bad}\n").await.unwrap();
|
||||
let mut lines = BufReader::new(client_out).lines();
|
||||
let error: Value = serde_json::from_str(&lines.next_line().await.unwrap().unwrap()).unwrap();
|
||||
assert_eq!(error["error"]["code"], -32700);
|
||||
service_out.write_all(b"\"result\":true}\n").await.unwrap();
|
||||
let reply: Value = serde_json::from_str(&lines.next_line().await.unwrap().unwrap()).unwrap();
|
||||
assert_eq!(reply, json!({"id": 1, "result": true}));
|
||||
tasks.abort();
|
||||
}
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn crlf_at_exact_limit_does_not_count_the_terminator() {
|
||||
let mut reader = BufReader::new(&b"1234\r\n"[..]);
|
||||
assert_eq!(bounded_line(&mut reader, &mut Vec::new(), 4).await.unwrap().as_deref(), Some("1234"));
|
||||
let mut reader = BufReader::new(&b"1234\r"[..]);
|
||||
assert!(bounded_line(&mut reader, &mut Vec::new(), 4).await.is_err());
|
||||
}
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn oversized_unterminated_lines_stop_both_directions() {
|
||||
let (mut client, input) = tokio::io::duplex(128);
|
||||
let (output, _client_out) = tokio::io::duplex(128);
|
||||
let Adapter { mut service_in, service_out, tasks } = spawn(input, output, 8, 8);
|
||||
client.write_all(b"123456789").await.unwrap();
|
||||
let mut forwarded = Vec::new();
|
||||
service_in.read_to_end(&mut forwarded).await.unwrap();
|
||||
assert!(forwarded.is_empty());
|
||||
drop(service_out);
|
||||
tasks.finish().await;
|
||||
|
||||
let (_client, input) = tokio::io::duplex(128);
|
||||
let (output, mut client_out) = tokio::io::duplex(128);
|
||||
let Adapter { service_in: _, mut service_out, tasks } = spawn(input, output, 8, 8);
|
||||
service_out.write_all(b"123456789").await.unwrap();
|
||||
let mut received = Vec::new();
|
||||
client_out.read_to_end(&mut received).await.unwrap();
|
||||
assert!(received.is_empty());
|
||||
tasks.abort();
|
||||
}
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn blocked_output_tasks_can_be_cancelled() {
|
||||
let (_client, input) = tokio::io::duplex(16);
|
||||
let (output, _blocked) = tokio::io::duplex(1);
|
||||
let Adapter { service_in: _, mut service_out, tasks } = spawn(input, output, 64, 64);
|
||||
service_out.write_all(b"123456789\n").await.unwrap();
|
||||
tokio::task::yield_now().await;
|
||||
tasks.abort();
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,8 @@
|
||||
#![forbid(unsafe_code)]
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
|
||||
#[cfg(feature = "async-lines")]
|
||||
pub mod async_lines;
|
||||
pub mod control;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
@@ -0,0 +1,237 @@
|
||||
//! Compatibility EXIF summary and helpers for legacy clients that accepted loosely formed classic TIFF.
|
||||
//! The ordinary strict EXIF orientation API remains unchanged.
|
||||
|
||||
/// Read a legacy orientation, with an optional `Exif\0\0` prefix.
|
||||
pub fn orientation(bytes: &[u8]) -> u16 {
|
||||
summarize(bytes.strip_prefix(b"Exif\0\0").unwrap_or(bytes)).orientation.unwrap_or(1)
|
||||
}
|
||||
|
||||
/// Byte-order aware reader over a TIFF-structured blob.
|
||||
#[derive(Clone, Copy)]
|
||||
struct Tiff<'a> {
|
||||
pub data: &'a [u8],
|
||||
le: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct Entry {
|
||||
pub tag: u16,
|
||||
pub typ: u16,
|
||||
pub count: u32,
|
||||
/// Offset of the 4-byte value/offset field within `data`.
|
||||
pub value_pos: usize,
|
||||
}
|
||||
|
||||
impl<'a> Tiff<'a> {
|
||||
pub fn new(data: &'a [u8]) -> Option<Tiff<'a>> {
|
||||
let le = match data.get(0..2)? {
|
||||
b"II" => true,
|
||||
b"MM" => false,
|
||||
_ => return None,
|
||||
};
|
||||
Some(Tiff { data, le })
|
||||
}
|
||||
pub fn u16_at(&self, pos: usize) -> Option<u16> {
|
||||
let b = self.data.get(pos..pos.checked_add(2)?)?;
|
||||
Some(if self.le { u16::from_le_bytes([b[0], b[1]]) } else { u16::from_be_bytes([b[0], b[1]]) })
|
||||
}
|
||||
pub fn u32_at(&self, pos: usize) -> Option<u32> {
|
||||
let b = self.data.get(pos..pos.checked_add(4)?)?;
|
||||
let a = [b[0], b[1], b[2], b[3]];
|
||||
Some(if self.le { u32::from_le_bytes(a) } else { u32::from_be_bytes(a) })
|
||||
}
|
||||
pub fn first_ifd(&self) -> Option<usize> {
|
||||
let magic = self.u16_at(2)?;
|
||||
if magic != 42 {
|
||||
return None;
|
||||
}
|
||||
Some(self.u32_at(4)? as usize)
|
||||
}
|
||||
/// Entries of the IFD at `pos` and the offset of the next IFD (0 = none).
|
||||
pub fn ifd(&self, pos: usize) -> Option<(Vec<Entry>, usize)> {
|
||||
let n = self.u16_at(pos)? as usize;
|
||||
if n > 4096 {
|
||||
return None;
|
||||
}
|
||||
let mut v = Vec::with_capacity(n);
|
||||
for i in 0..n {
|
||||
let e = pos.checked_add(2)?.checked_add(i.checked_mul(12)?)?;
|
||||
v.push(Entry { tag: self.u16_at(e)?, typ: self.u16_at(e + 2)?, count: self.u32_at(e + 4)?, value_pos: e + 8 });
|
||||
}
|
||||
let next =
|
||||
pos.checked_add(2).and_then(|p| n.checked_mul(12).and_then(|v| p.checked_add(v))).and_then(|p| self.u32_at(p)).unwrap_or(0) as usize;
|
||||
Some((v, next))
|
||||
}
|
||||
/// First value of a SHORT/LONG entry.
|
||||
pub fn uint(&self, e: &Entry) -> Option<u32> {
|
||||
match e.typ {
|
||||
3 => self.u16_at(e.value_pos).map(u32::from),
|
||||
4 | 13 => self.u32_at(e.value_pos),
|
||||
1 | 7 => self.data.get(e.value_pos).map(|b| *b as u32),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
/// Raw bytes of an entry's value (for ASCII/UNDEFINED/BYTE types).
|
||||
pub fn bytes(&self, e: &Entry) -> Option<&'a [u8]> {
|
||||
let size = match e.typ {
|
||||
1 | 2 | 6 | 7 => 1usize,
|
||||
3 | 8 => 2,
|
||||
4 | 9 | 11 | 13 => 4,
|
||||
5 | 10 | 12 => 8,
|
||||
_ => return None,
|
||||
};
|
||||
let len = size.checked_mul(e.count as usize)?;
|
||||
let start = if len <= 4 { e.value_pos } else { self.u32_at(e.value_pos)? as usize };
|
||||
self.data.get(start..start.checked_add(len)?)
|
||||
}
|
||||
}
|
||||
|
||||
/// What the decoder cares about from an EXIF (TIFF-structured) blob.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct ExifSummary {
|
||||
/// Valid orientation (1–8), if present.
|
||||
pub orientation: Option<u16>,
|
||||
/// (offset, length) of the IFD1 JPEG thumbnail within the blob.
|
||||
pub thumbnail: Option<(usize, usize)>,
|
||||
/// EXIF ColorSpace = 0xFFFF with InteropIndex "R03" (Adobe RGB (1998) "option" files).
|
||||
pub adobe_rgb_hint: bool,
|
||||
}
|
||||
|
||||
pub fn summarize(blob: &[u8]) -> ExifSummary {
|
||||
let mut s = ExifSummary::default();
|
||||
let Some(t) = Tiff::new(blob) else { return s };
|
||||
let Some(ifd0) = t.first_ifd() else { return s };
|
||||
let Some((entries, next)) = t.ifd(ifd0) else { return s };
|
||||
let mut exif_ifd = None;
|
||||
for e in &entries {
|
||||
match e.tag {
|
||||
0x0112 => s.orientation = t.uint(e).map(|v| v as u16).filter(|v| (1..=8).contains(v)),
|
||||
0x8769 => exif_ifd = t.uint(e).map(|v| v as usize),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if next != 0
|
||||
&& next != ifd0
|
||||
&& let Some((e1, _)) = t.ifd(next)
|
||||
{
|
||||
let off = e1.iter().find(|e| e.tag == 0x0201).and_then(|e| t.uint(e));
|
||||
let len = e1.iter().find(|e| e.tag == 0x0202).and_then(|e| t.uint(e));
|
||||
if let (Some(o), Some(l)) = (off, len) {
|
||||
let (o, l) = (o as usize, l as usize);
|
||||
if l > 4 && o.checked_add(l).is_some_and(|end| end <= blob.len()) && blob[o..o + 2] == [0xFF, 0xD8] {
|
||||
s.thumbnail = Some((o, l));
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(p) = exif_ifd
|
||||
&& let Some((ee, _)) = t.ifd(p)
|
||||
{
|
||||
let cs = ee.iter().find(|e| e.tag == 0xA001).and_then(|e| t.uint(e));
|
||||
let interop = ee.iter().find(|e| e.tag == 0xA005).and_then(|e| t.uint(e));
|
||||
if cs == Some(0xFFFF)
|
||||
&& let Some((ie, _)) = interop.and_then(|p| t.ifd(p as usize))
|
||||
{
|
||||
s.adobe_rgb_hint = ie.iter().any(|e| e.tag == 1 && t.bytes(e).is_some_and(|b| b.starts_with(b"R03")));
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
/// Build a tiny EXIF blob (little-endian TIFF) with just an orientation tag. Used by tests and by
|
||||
/// callers that need to stamp orientation on export when no source EXIF exists.
|
||||
pub fn minimal_exif(orientation: u16) -> Vec<u8> {
|
||||
let mut v = Vec::with_capacity(26);
|
||||
v.extend_from_slice(b"II");
|
||||
v.extend_from_slice(&42u16.to_le_bytes());
|
||||
v.extend_from_slice(&8u32.to_le_bytes());
|
||||
v.extend_from_slice(&1u16.to_le_bytes());
|
||||
v.extend_from_slice(&0x0112u16.to_le_bytes());
|
||||
v.extend_from_slice(&3u16.to_le_bytes());
|
||||
v.extend_from_slice(&1u32.to_le_bytes());
|
||||
v.extend_from_slice(&orientation.to_le_bytes());
|
||||
v.extend_from_slice(&[0, 0]);
|
||||
v.extend_from_slice(&0u32.to_le_bytes());
|
||||
v
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn accepts_legacy_count_zero_but_bounds_the_table() {
|
||||
let mut bytes = minimal_exif(6);
|
||||
bytes[14..18].fill(0);
|
||||
assert_eq!(orientation(&bytes), 6);
|
||||
bytes.truncate(19);
|
||||
assert_eq!(orientation(&bytes), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn orientation_roundtrip() {
|
||||
for o in 1..=8 {
|
||||
assert_eq!(summarize(&minimal_exif(o)).orientation, Some(o));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn garbage_ok() {
|
||||
assert_eq!(summarize(b"II*\0\xff\xff\xff\xff"), ExifSummary::default());
|
||||
assert_eq!(summarize(&[]), ExifSummary::default());
|
||||
}
|
||||
|
||||
/// Big-endian IFD: `entries` are (tag, type, count, value-or-offset), value left-justified.
|
||||
fn be_ifd(out: &mut Vec<u8>, entries: &[(u16, u16, u32, [u8; 4])], next: u32) {
|
||||
out.extend_from_slice(&(entries.len() as u16).to_be_bytes());
|
||||
for (tag, typ, count, value) in entries {
|
||||
out.extend_from_slice(&tag.to_be_bytes());
|
||||
out.extend_from_slice(&typ.to_be_bytes());
|
||||
out.extend_from_slice(&count.to_be_bytes());
|
||||
out.extend_from_slice(value);
|
||||
}
|
||||
out.extend_from_slice(&next.to_be_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn big_endian_thumbnail_and_adobe_rgb_interop_hint() {
|
||||
let (ifd1, exif, interop, rational, thumb) = (38u32, 68u32, 98u32, 164u32, 172u32);
|
||||
let mut b = b"MM\0\x2a\0\0\0\x08".to_vec();
|
||||
be_ifd(&mut b, &[(0x0112, 3, 1, [0, 6, 0, 0]), (0x8769, 4, 1, exif.to_be_bytes())], ifd1);
|
||||
be_ifd(&mut b, &[(0x0201, 4, 1, thumb.to_be_bytes()), (0x0202, 1, 1, [6, 0, 0, 0])], 0);
|
||||
be_ifd(&mut b, &[(0xA001, 3, 1, [0xFF, 0xFF, 0, 0]), (0xA005, 13, 1, interop.to_be_bytes())], 0);
|
||||
// Every tag-1 candidate is read; only the ASCII "R03" one matches.
|
||||
be_ifd(
|
||||
&mut b,
|
||||
&[(1, 99, 1, [0; 4]), (1, 5, 1, rational.to_be_bytes()), (1, 4, 1, *b"R98\0"), (1, 3, 1, *b"R9\0\0"), (1, 2, 4, *b"R03\0")],
|
||||
0,
|
||||
);
|
||||
assert_eq!(b.len(), rational as usize);
|
||||
b.extend_from_slice(&[0, 0, 0, 1, 0, 0, 0, 2]);
|
||||
b.extend_from_slice(&[0xFF, 0xD8, 0xFF, 0xD9, 0, 0]);
|
||||
let s = summarize(&b);
|
||||
assert_eq!(s, ExifSummary { orientation: Some(6), thumbnail: Some((thumb as usize, 6)), adobe_rgb_hint: true });
|
||||
// A thumbnail that is not a JPEG, or runs past the blob, is ignored.
|
||||
let mut not_jpeg = b.clone();
|
||||
not_jpeg[thumb as usize] = 0;
|
||||
assert_eq!(summarize(¬_jpeg).thumbnail, None);
|
||||
assert_eq!(summarize(&b[..b.len() - 1]).thumbnail, None);
|
||||
// Without ColorSpace 0xFFFF the interop index is not consulted.
|
||||
let mut srgb = b.clone();
|
||||
srgb[(exif + 10) as usize] = 0;
|
||||
assert!(!summarize(&srgb).adobe_rgb_hint);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_bad_headers_types_and_oversized_tables() {
|
||||
assert_eq!(summarize(b"XX\0\x2a\0\0\0\x08"), ExifSummary::default());
|
||||
assert_eq!(summarize(b"MM\0\x2b\0\0\0\x08"), ExifSummary::default());
|
||||
let mut huge = b"MM\0\x2a\0\0\0\x08".to_vec();
|
||||
huge.extend_from_slice(&5000u16.to_be_bytes());
|
||||
assert_eq!(summarize(&huge), ExifSummary::default());
|
||||
// Orientation stored with an unsupported type (SRATIONAL) is ignored.
|
||||
let mut bytes = minimal_exif(6);
|
||||
bytes[12..14].copy_from_slice(&10u16.to_le_bytes());
|
||||
assert_eq!(orientation(&bytes), 1);
|
||||
assert_eq!(orientation(&[b"Exif\0\0".as_slice(), &minimal_exif(3)].concat()), 3);
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@
|
||||
//! [`Embedded`] and call [`from_embedded`].
|
||||
|
||||
pub mod camera_fields;
|
||||
pub mod compat_orientation;
|
||||
mod container;
|
||||
pub mod cr3;
|
||||
mod datetime;
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "craft-shx"
|
||||
description = "Bounded SHP shape-source compiler for SHX shape, big-font and Unicode containers"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -0,0 +1,8 @@
|
||||
# craft-shx
|
||||
|
||||
Compiles SHP shape source into SHX shapes, big-font and Unicode containers.
|
||||
Extracted from CADCraft by Francesco BV (cadcraft52c1e6de); no font assets are bundled.
|
||||
App font discovery, text layout and glyph interpretation are not part of this crate.
|
||||
The original 8 MiB source and u16 record bounds remain; malformed double signs,
|
||||
excess escape ranges and oversized Unicode font records are refused. Duplicate
|
||||
shape detection is linear rather than a repeated whole-list scan.
|
||||
@@ -0,0 +1,327 @@
|
||||
//! 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;
|
||||
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;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
enum Kind {
|
||||
Shapes,
|
||||
Big,
|
||||
Uni,
|
||||
}
|
||||
|
||||
struct Shape {
|
||||
number: u16,
|
||||
name: String,
|
||||
program: Vec<u8>,
|
||||
}
|
||||
|
||||
/// A number token: value (sign included).
|
||||
fn number(tok: &str, line: usize) -> Result<i64, ShxError> {
|
||||
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::<i64>().map_err(|_| err())?
|
||||
};
|
||||
if !(0..=0xFFFF).contains(&v) {
|
||||
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<Vec<i64>, 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<u8>,
|
||||
kind: Kind,
|
||||
line: usize,
|
||||
}
|
||||
|
||||
impl Enc<'_> {
|
||||
fn next(&mut self) -> Result<i64, ShxError> {
|
||||
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<i64, ShxError> {
|
||||
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<Vec<u8>, 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<Vec<u8>, 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<u8>)> = None;
|
||||
// Definitions: header (number, name, line) and the numbers that follow it.
|
||||
let mut defs: Vec<(i64, String, usize, Vec<i64>)> = 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);
|
||||
// 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);
|
||||
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();
|
||||
if defs.len() >= usize::from(u16::MAX) {
|
||||
return Err(ShxError::TooLarge);
|
||||
}
|
||||
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<Shape> = Vec::new();
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for (num, name, line, toks) in defs {
|
||||
let number = u16::try_from(num).map_err(|_| ShxError::Source(line, format!("shape number {num} out of range")))?;
|
||||
let program = if number == 0 {
|
||||
// The font record: plain bytes.
|
||||
toks.iter().map(|x| u8::try_from(*x).map_err(|_| ShxError::Source(line, "bad font record".into()))).collect::<Result<Vec<u8>, _>>()?
|
||||
} else {
|
||||
Enc { toks: &toks, i: 0, out: Vec::new(), kind, line }.program()?
|
||||
};
|
||||
if !seen.insert(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<Vec<u8>, ShxError> {
|
||||
let mut r: Vec<u8> = 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<Vec<u8>> = shapes.iter().map(record).collect::<Result<_, _>>()?;
|
||||
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<Vec<u8>> = shapes.iter().map(record).collect::<Result<_, _>>()?;
|
||||
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());
|
||||
if name.len().saturating_add(1).saturating_add(info.len()) > usize::from(u16::MAX) {
|
||||
return Err(ShxError::TooLarge);
|
||||
}
|
||||
let mut rec: Vec<u8> = name.bytes().filter(|b| *b != 0).collect();
|
||||
rec.push(0);
|
||||
rec.extend(&info);
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/// 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 {}
|
||||
@@ -0,0 +1,11 @@
|
||||
#![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 mod parse;
|
||||
pub mod program;
|
||||
pub use compile::compile;
|
||||
pub use error::ShxError;
|
||||
pub use parse::{ParsedFont, Record, ShxKind};
|
||||
@@ -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<u8>,
|
||||
pub records: HashMap<u16, Record>,
|
||||
}
|
||||
|
||||
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<u16, ShxError> {
|
||||
match self.take(2)? {
|
||||
[a, b] => Ok(u16::from_le_bytes([*a, *b])),
|
||||
_ => Err(ShxError::Truncated),
|
||||
}
|
||||
}
|
||||
fn u32(&mut self) -> Result<u32, ShxError> {
|
||||
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<u16, Record>,
|
||||
}
|
||||
|
||||
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<u8>) -> Result<ParsedFont, ShxError> {
|
||||
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<ParsedFont, ShxError> {
|
||||
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<ParsedFont, ShxError> {
|
||||
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<ParsedFont, ShxError> {
|
||||
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<ParsedFont, ShxError> {
|
||||
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)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,536 @@
|
||||
//! 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<f64> 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<Vec<Point>>,
|
||||
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<Point>,
|
||||
strokes: Vec<Vec<Point>>,
|
||||
active: Vec<Point>,
|
||||
steps: usize,
|
||||
points: usize,
|
||||
}
|
||||
|
||||
pub fn outline(font: &dyn Font, number: u16, vertical: bool) -> Result<Glyph, Broken> {
|
||||
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<u8, Broken> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod instruction_tests {
|
||||
#![allow(clippy::unwrap_used)]
|
||||
use super::*;
|
||||
|
||||
struct Fixture {
|
||||
kind: ShxKind,
|
||||
programs: Vec<Vec<u8>>,
|
||||
}
|
||||
impl Font for Fixture {
|
||||
fn kind(&self) -> ShxKind {
|
||||
self.kind
|
||||
}
|
||||
fn dual(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn above(&self) -> f64 {
|
||||
10.0
|
||||
}
|
||||
fn char_width(&self) -> f64 {
|
||||
10.0
|
||||
}
|
||||
fn program(&self, number: u16) -> Option<&[u8]> {
|
||||
self.programs.get(usize::from(number)).map(Vec::as_slice)
|
||||
}
|
||||
}
|
||||
fn fixture(program: Vec<u8>) -> Fixture {
|
||||
Fixture { kind: ShxKind::Shapes, programs: vec![program] }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pen_state_scaling_and_location_stack_keep_separate_strokes() {
|
||||
let font = fixture(vec![4, 4, 0x10, 5, 2, 8, 2, 3, 6, 1, 3, 2, 0x14, 0]);
|
||||
let g = outline(&font, 0, false).unwrap();
|
||||
assert_eq!(g.strokes, vec![vec![Point::ZERO, Point::new(4.0, 0.0)], vec![Point::new(4.0, 0.0), Point::new(4.0, 2.0)]]);
|
||||
assert_eq!(g.advance, Point::new(4.0, 2.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vertical_only_commands_consume_operands_without_affecting_horizontal_text() {
|
||||
let font = fixture(vec![14, 8, 10, 20, 0x10, 0]);
|
||||
assert_eq!(outline(&font, 0, false).unwrap().advance, Point::new(1.0, 0.0));
|
||||
assert_eq!(outline(&font, 0, true).unwrap().advance, Point::new(11.0, 20.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn displacement_and_bulge_sequences_end_at_their_chord_endpoint() {
|
||||
for (program, end_x) in
|
||||
[(vec![9, 3, 4, 253, 252, 0, 0, 0], 0.0), (vec![12, 10, 0, 127, 0], 10.0), (vec![13, 10, 0, 129, 246, 0, 0, 0, 0, 0], 0.0)]
|
||||
{
|
||||
let g = outline(&fixture(program), 0, false).unwrap();
|
||||
assert!(g.advance.y.abs() < 1e-10);
|
||||
assert!((g.advance.x - end_x).abs() < 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_octant_arcs_close_and_fractional_arcs_keep_radius() {
|
||||
for program in [vec![10, 10, 0, 0], vec![10, 10, 128, 0], vec![11, 0, 0, 0, 10, 0, 0]] {
|
||||
let g = outline(&fixture(program), 0, false).unwrap();
|
||||
assert!(g.advance.len() < 1e-10);
|
||||
assert_eq!(g.strokes[0].len(), 65);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subshape_operands_follow_font_kind_and_bigfont_restores_parent_position() {
|
||||
for kind in [ShxKind::Shapes, ShxKind::Unifont, ShxKind::BigFont] {
|
||||
let call = match kind {
|
||||
ShxKind::Unifont => vec![7, 0, 1, 0],
|
||||
ShxKind::BigFont => vec![7, 0, 0, 1, 2, 3, 20, 30, 0],
|
||||
_ => vec![7, 1, 0],
|
||||
};
|
||||
let font = Fixture { kind, programs: vec![call, vec![0x10, 0]] };
|
||||
let g = outline(&font, 0, false).unwrap();
|
||||
if kind == ShxKind::BigFont {
|
||||
assert_eq!(g.advance, Point::ZERO);
|
||||
assert_eq!(g.strokes[0], vec![Point::new(2.0, 3.0), Point::new(4.0, 3.0)]);
|
||||
} else {
|
||||
assert_eq!(g.advance, Point::new(1.0, 0.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_operands_and_resource_exhaustion_return_errors() {
|
||||
for program in [vec![8, 1], vec![7, 99], vec![5; MAX_STACK + 1], vec![0x10; MAX_POINTS + 1], vec![4, 255, 4, 255, 4, 255]] {
|
||||
assert!(outline(&fixture(program), 0, false).is_err());
|
||||
}
|
||||
assert!(outline(&fixture(vec![0]), 1, false).is_err());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
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));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn displacement_arc_and_subshape_operands_have_exact_bytes() {
|
||||
let source = "; comment\n*+65,99,A\n3,2,4,3,7,66,8,-128,127,9,(1,-2),(0,0),\n10,12,-043,11,1,2,3,4,043,12,-1,2,-3,13,1,2,-3,0,0,0\n";
|
||||
let bytes = compile(source).unwrap();
|
||||
let program = [3, 2, 4, 3, 7, 66, 8, 128, 127, 9, 1, 254, 0, 0, 10, 12, 0xc3, 11, 1, 2, 3, 4, 0x43, 12, 255, 2, 253, 13, 1, 2, 253, 0, 0, 0];
|
||||
let mut record = vec![b'A', 0];
|
||||
record.extend(program);
|
||||
record.extend(b"EOF");
|
||||
assert!(bytes.ends_with(&record));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bigfont_ranges_index_offsets_and_extended_calls_are_encoded() {
|
||||
let bytes = compile("*BIGFONT 1,2,128,129,140,141\n*65,1,A\n7,66,7,0,01234,1,2,3,4,0\n").unwrap();
|
||||
let signature = b"AutoCAD-86 bigfont 1.0\r\n\x1a";
|
||||
let mut expected = signature.to_vec();
|
||||
expected.extend([1, 0, 1, 0, 2, 0, 128, 0, 129, 0, 140, 0, 141, 0]);
|
||||
let record = [b'A', 0, 7, 66, 7, 0, 0x12, 0x34, 1, 2, 3, 4, 0];
|
||||
let offset = (expected.len() + 8) as u32;
|
||||
expected.extend([65, 0, record.len() as u8, 0]);
|
||||
expected.extend(offset.to_le_bytes());
|
||||
expected.extend(record);
|
||||
assert_eq!(bytes, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unicode_font_records_and_subshape_numbers_are_encoded() {
|
||||
let bytes = compile("*UNIFONT,6,Example\n10,2,0\n0,0,0\n*65,9,A\n7,01234\n").unwrap();
|
||||
let mut expected = b"AutoCAD-86 unifont 1.0\r\n\x1a".to_vec();
|
||||
expected.extend(2u32.to_le_bytes());
|
||||
expected.extend(14u16.to_le_bytes());
|
||||
expected.extend(b"Example\0");
|
||||
expected.extend([10, 2, 0, 0, 0, 0]);
|
||||
expected.extend([65, 0, 6, 0, b'A', 0, 7, 0x12, 0x34, 0]);
|
||||
assert_eq!(bytes, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn font_records_sorting_names_and_implicit_end_are_preserved() {
|
||||
let bytes = compile("*66,1,B\0name\n1\n*0,4,Font\n10,2,0,0\n*65,1,A\n2,0\n").unwrap();
|
||||
let mut expected = b"AutoCAD-86 shapes 1.0\r\n\x1a".to_vec();
|
||||
expected.extend([0, 0, 66, 0, 3, 0]);
|
||||
let records: [(u16, &[u8]); 3] = [(0, b"Font\0\x0a\x02\0\0"), (65, b"A\0\x02\0"), (66, b"Bname\0\x01\0")];
|
||||
for (number, record) in records {
|
||||
expected.extend(number.to_le_bytes());
|
||||
expected.extend((record.len() as u16).to_le_bytes());
|
||||
}
|
||||
for (_, record) in records {
|
||||
expected.extend(record);
|
||||
}
|
||||
expected.extend(b"EOF");
|
||||
assert_eq!(bytes, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_typed_operands_report_source_lines() {
|
||||
for body in ["8,128,0", "10,1,128", "11,1,2,3,4,-128", "7,-1", "3,-1", "256", "7,256"] {
|
||||
assert!(matches!(compile(&format!("*65,1,A\n{body}")), Err(ShxError::Source(1, _))), "{body}");
|
||||
}
|
||||
for source in ["*UNIFONT,6,X\n256\n*65,1,A\n0", "*BIGFONT 1,1,-1,2\n*65,1,A\n0", "*0,1,F\n256", "*-1,1,A\n0"] {
|
||||
assert!(matches!(compile(source), Err(ShxError::Source(_, _))), "{source}");
|
||||
}
|
||||
let large_record = format!("*65,1,{}\n0", "x".repeat(65535));
|
||||
assert_eq!(compile(&large_record), Err(ShxError::TooLarge));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_have_useful_display_messages() {
|
||||
for (error, expected) in [
|
||||
(ShxError::NotShx, "not an SHX shape or font file"),
|
||||
(ShxError::Truncated, "the SHX file is truncated"),
|
||||
(ShxError::TooLarge, "the SHX file is too large"),
|
||||
(ShxError::Empty, "the SHX file has no shapes"),
|
||||
(ShxError::Source(7, "bad operand".into()), "line 7: bad operand"),
|
||||
] {
|
||||
assert_eq!(error.to_string(), expected);
|
||||
}
|
||||
}
|
||||
+137
-11
@@ -18,6 +18,11 @@
|
||||
//! 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 metadata_tests;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
@@ -57,11 +62,13 @@ pub type Result<T> = std::result::Result<T, Error>;
|
||||
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 }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,6 +187,102 @@ impl Stored {
|
||||
}
|
||||
}
|
||||
|
||||
fn metadata_offset(body: &[u8], offset: usize, first_pixel: usize) -> Option<usize> {
|
||||
// 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<usize> {
|
||||
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<usize> {
|
||||
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(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) {
|
||||
// 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<Picture> {
|
||||
let err = Error::Malformed;
|
||||
@@ -208,13 +311,14 @@ pub fn decode(bytes: &[u8], limits: &Limits) -> Result<Picture> {
|
||||
} 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"))?;
|
||||
let at = first_pixel;
|
||||
let (stored_as, mapped) = match kind & 7 {
|
||||
1 => {
|
||||
if map_type != 1 || !matches!(depth, 8 | 16) {
|
||||
@@ -232,14 +336,35 @@ pub fn decode(bytes: &[u8], limits: &Limits) -> Result<Picture> {
|
||||
};
|
||||
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<u8> = 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 };
|
||||
@@ -283,7 +408,8 @@ fn unpack_rle(mut data: &[u8], pixels: usize, bpp: usize) -> Result<Vec<u8>> {
|
||||
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;
|
||||
|
||||
@@ -0,0 +1,425 @@
|
||||
//! Metadata-boundary regressions ported from FilmCraft #1932 (nuclearspike).
|
||||
use super::*;
|
||||
fn header(kind: u8, w: u16, h: u16, depth: u8, descriptor: u8) -> Vec<u8> {
|
||||
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<Picture> {
|
||||
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<u8> = 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!(result.is_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!(result.is_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<u8> {
|
||||
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;
|
||||
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()),
|
||||
_ => {}
|
||||
}
|
||||
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 result.is_ok() {
|
||||
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<u8>| {
|
||||
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!(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.
|
||||
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!(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!(dec(&file).is_err());
|
||||
let mut file = preceding_metadata(false, 2);
|
||||
let footer = file.len() - SIGNATURE.len() - 8;
|
||||
file.remove(footer - 1);
|
||||
assert!(dec(&file).is_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!(dec(&file).is_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!(dec(&file).is_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!(dec(&file).is_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!(dec(&file).is_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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
//! 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,
|
||||
/// The output buffer could not be allocated.
|
||||
OutOfMemory,
|
||||
}
|
||||
fn checked_len(rgba: &[u8], w: u32, h: u32) -> std::result::Result<usize, Rgba8EncodeError> {
|
||||
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<Vec<u8>, 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;
|
||||
// The caller already holds the frame; reserve the second, output-sized buffer fallibly so a
|
||||
// large export reports an error instead of aborting the process.
|
||||
let len = (w as usize * h as usize).checked_mul(bpp).and_then(|n| n.checked_add(18 + 26)).ok_or(Rgba8EncodeError::FrameSize)?;
|
||||
let mut out = Vec::new();
|
||||
out.try_reserve_exact(len).map_err(|_| Rgba8EncodeError::OutOfMemory)?;
|
||||
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));
|
||||
}
|
||||
}
|
||||
+34
-2
@@ -92,6 +92,11 @@ fn hostile_headers_fail_without_allocating() {
|
||||
assert_eq!(dec(&f), Err(Error::LimitExceeded("pixel count")));
|
||||
let mut f = header(10, 16384, 16384, 32, 0);
|
||||
f.extend_from_slice(&[0xFF, 1, 2, 3, 4]);
|
||||
// The aggregate decoded-storage budget rejects this before any allocation.
|
||||
assert_eq!(dec(&f), Err(Error::LimitExceeded("decode allocation bytes")));
|
||||
// Under budget, an incomplete RLE stream must still fail as truncated.
|
||||
let mut f = header(10, 16, 16, 32, 0);
|
||||
f.extend_from_slice(&[0xFF, 1, 2, 3, 4]); // only 128 of 256 pixels
|
||||
assert_eq!(dec(&f), Err(Error::Malformed("truncated run-length data")));
|
||||
let mut f = header(2, 4, 4, 24, 0);
|
||||
f.extend_from_slice(&[0; 10]);
|
||||
@@ -116,8 +121,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 +240,30 @@ 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 { 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);
|
||||
}
|
||||
#[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 { 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]);
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ av1-encode = ["craft-media/isobmff"]
|
||||
# Tiled AV1 fallback for frames that exceed the clean-room encoder's single-tile limits.
|
||||
av1-large-encode = ["av1-encode", "dep:rav1e"]
|
||||
# Decode and encode across worker threads (rayon) in the codecs that support it.
|
||||
threads = ["dep:rayon"]
|
||||
threads = ["dep:rayon", "rav1e?/threading"]
|
||||
|
||||
[dependencies]
|
||||
craft-media = { workspace = true }
|
||||
|
||||
Reference in New Issue
Block a user