Merge remote-tracking branch 'origin/main' into w1627

This commit is contained in:
FredApps
2026-11-16 02:54:17 +01:00
75 changed files with 5530 additions and 73 deletions
+1 -1
View File
@@ -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-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
+1
View File
@@ -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
+31
View File
@@ -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",
]
@@ -1878,6 +1881,15 @@ dependencies = [
"image",
]
[[package]]
name = "craft-gridcraft-format"
version = "0.1.0"
dependencies = [
"serde",
"serde_json",
"thiserror 2.0.21",
]
[[package]]
name = "craft-gzip"
version = "0.1.0"
@@ -2023,6 +2035,7 @@ name = "craft-jsonrpc"
version = "0.1.0"
dependencies = [
"serde_json",
"tokio",
]
[[package]]
@@ -2658,6 +2671,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"
@@ -6141,6 +6158,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519"
dependencies = [
"cfg-if",
"rayon",
]
[[package]]
@@ -7978,6 +7996,7 @@ dependencies = [
"loop9",
"quick-error 2.0.1",
"rav1e",
"rayon",
"rgb",
]
@@ -9921,9 +9940,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"
+2
View File
@@ -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" }
@@ -41,6 +42,7 @@ bytemuck = "1"
kurbo = "0.13"
thiserror = "2"
craft-gzip = { path = "crates/gzip" }
craft-gridcraft-format = { path = "crates/gridcraft-format" }
craft-aaf = { path = "crates/aaf" }
craft-atomic = { path = "crates/atomic" }
craft-captions = { path = "crates/captions" }
+3
View File
@@ -176,6 +176,9 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
every frame's position, delay, disposal and transparent colour, the loop count and the first
frame's colour table, without decompressing pixels. Tells an animation from a still and whether
a file can hold the picture it declares before a decoder runs.
- [`craft-gridcraft-format`](crates/gridcraft-format/README.md): GridCraft's native JSON workbook
(`.json`, `.scjson`): the extensions, the reader, the pretty writer, and newer entries kept
unread and written back into a list. The workbook model and what follows a read stay in GridCraft.
- [`craft-hdr`](crates/hdr/src/lib.rs): Radiance HDR (RGBE) picture reader, from FilmCraft: flat,
old and new run-length scan lines and all eight axis orders, to linear float RGB. Scan lines are
validated before the picture is allocated.
+147
View File
@@ -0,0 +1,147 @@
//! Compatibility WAVE and AIFF wrappers for embedded PCM essence in interchange files.
//! PCM inside the model is interleaved little-endian signed integers (8-bit unsigned for WAVE).
pub use crate::{pcm_aiff_header as aiff_header, pcm_wav_file as wav_file, pcm_wav_header as wav_header};
/// PCM, channels, sample rate and bits of a WAVE file (integer PCM only).
pub fn parse_wav(b: &[u8]) -> Option<(Vec<u8>, u16, u32, u16)> {
if b.len() < 12 || &b[..4] != b"RIFF" || &b[8..12] != b"WAVE" {
return None;
}
let mut at = 12;
let mut fmt = None;
while at + 8 <= b.len() {
let id = &b[at..at + 4];
let len = u32::from_le_bytes(b[at + 4..at + 8].try_into().ok()?) as usize;
let body = &b[at + 8..(at + 8).saturating_add(len).min(b.len())];
if id == b"fmt " && body.len() >= 16 {
let tag = u16::from_le_bytes([body[0], body[1]]);
let ch = u16::from_le_bytes([body[2], body[3]]);
let sr = u32::from_le_bytes(body[4..8].try_into().ok()?);
let bits = u16::from_le_bytes([body[14], body[15]]);
if tag != 1 && tag != 0xFFFE {
return None;
}
if tag == 0xFFFE {
// The first two bytes of the sub-format GUID carry the real format code.
let sub = body.get(24..26)?;
if u16::from_le_bytes([sub[0], sub[1]]) != 1 {
return None;
}
}
fmt = Some((ch, sr, bits));
} else if id == b"data" {
let (ch, sr, bits) = fmt?;
return Some((body.to_vec(), ch, sr, bits));
}
at = at.checked_add(8usize.checked_add(len)?.checked_add(len & 1)?)?;
}
None
}
/// A complete AIFF file (big-endian PCM) from little-endian PCM.
pub fn aiff_file(pcm: &[u8], channels: u16, sample_rate: u32, bits: u16) -> Vec<u8> {
let bps = bits.div_ceil(8) as usize;
let frames = pcm.len() / (bps * channels.max(1) as usize).max(1);
let mut v = aiff_header(frames as u32, channels, sample_rate, bits);
for s in pcm.chunks_exact(bps.max(1)) {
if bps == 1 {
v.push(s[0] ^ 0x80); // WAVE 8-bit is unsigned, AIFF signed
} else {
v.extend(s.iter().rev());
}
}
if (v.len() - 8) & 1 == 1 {
v.push(0);
}
v
}
/// PCM (converted to little-endian), channels, sample rate and bits of an AIFF / AIFF-C (`NONE`
/// / `twos` / `sowt`) file.
pub fn parse_aiff(b: &[u8]) -> Option<(Vec<u8>, u16, u32, u16)> {
if b.len() < 12 || &b[..4] != b"FORM" || !(&b[8..12] == b"AIFF" || &b[8..12] == b"AIFC") {
return None;
}
let mut at = 12;
let mut comm = None;
while at + 8 <= b.len() {
let id = &b[at..at + 4];
let len = u32::from_be_bytes(b[at + 4..at + 8].try_into().ok()?) as usize;
let body = &b[at + 8..(at + 8).saturating_add(len).min(b.len())];
if id == b"COMM" && body.len() >= 18 {
let ch = u16::from_be_bytes([body[0], body[1]]);
let bits = u16::from_be_bytes([body[6], body[7]]);
let sr = from_extended(body[8..18].try_into().ok()?).round() as u32;
let little = body.len() >= 22 && &body[18..22] == b"sowt";
comm = Some((ch, sr, bits, little));
} else if id == b"SSND" && body.len() >= 8 {
let (ch, sr, bits, little) = comm?;
let off = u32::from_be_bytes(body[..4].try_into().ok()?) as usize;
let data = body.get(8usize.checked_add(off)?..)?;
let bps = bits.div_ceil(8) as usize;
let mut pcm = Vec::with_capacity(data.len());
for s in data.chunks_exact(bps.max(1)) {
if bps == 1 {
pcm.push(s[0] ^ 0x80);
} else if little {
pcm.extend_from_slice(s);
} else {
pcm.extend(s.iter().rev());
}
}
return Some((pcm, ch, sr, bits));
}
at = at.checked_add(8usize.checked_add(len)?.checked_add(len & 1)?)?;
}
None
}
fn from_extended(b: [u8; 10]) -> f64 {
let e = (u16::from_be_bytes([b[0], b[1]]) & 0x7FFF) as i32;
let m = u64::from_be_bytes(b[2..10].try_into().unwrap_or([0; 8]));
if e == 0 && m == 0 {
return 0.0;
}
m as f64 / (1u64 << 63) as f64 * 2f64.powi(e - 16383)
}
#[cfg(test)]
mod tests {
/// A mutated EDL whose clip ran for days produced an OMF descriptor for more than 4 GiB of
/// PCM: the RIFF size field overflowed ("attempt to add with overflow").
#[test]
fn huge_wav_header_saturates() {
let h = super::wav_header(u32::MAX, 2, u32::MAX, 24);
assert_eq!(&h[4..8], &u32::MAX.to_le_bytes());
}
use super::*;
#[test]
fn accepts_truncated_essence_summaries() {
let mut wav = wav_file(&[1, 2, 3, 4], 1, 48_000, 16);
wav.truncate(46);
assert_eq!(parse_wav(&wav), Some((vec![1, 2], 1, 48_000, 16)));
let mut aiff = aiff_file(&[1, 2, 3, 4], 1, 48_000, 16);
aiff.truncate(56);
assert_eq!(parse_aiff(&aiff), Some((vec![1, 2], 1, 48_000, 16)));
}
#[test]
fn wav_and_aiff_round_trip() {
let pcm: Vec<u8> = (0..48u8).collect(); // 8 stereo 24-bit frames
let w = wav_file(&pcm, 2, 48_000, 24);
assert_eq!(parse_wav(&w), Some((pcm.clone(), 2, 48_000, 24)));
let a = aiff_file(&pcm, 2, 44_100, 24);
assert_eq!(parse_aiff(&a), Some((pcm.clone(), 2, 44_100, 24)));
let p16: Vec<u8> = (0..32u8).collect();
assert_eq!(parse_aiff(&aiff_file(&p16, 1, 96_000, 16)), Some((p16, 1, 96_000, 16)));
assert!(parse_wav(b"RIFF").is_none());
assert!(parse_aiff(&a[..30]).is_none());
for cut in 0..w.len() {
let _ = parse_wav(&w[..cut]);
let _ = parse_aiff(&a[..cut.min(a.len())]);
}
}
}
+3
View File
@@ -76,6 +76,9 @@ pub mod wav_markers;
))]
mod write;
/// PCM WAVE and AIFF wrappers for interchange essence.
#[cfg(feature = "pcm-encode")]
pub mod essence_pcm;
/// Interleaved PCM packet decoding.
pub mod pcm_interleaved;
/// Range-backed PCM WAVE access and a 16-bit PCM writer.
+3
View File
@@ -29,8 +29,10 @@ pub mod lut3d;
pub mod lutfile;
pub mod math;
pub mod named;
mod normalize;
pub mod pipeline;
pub mod profile;
pub mod sony_lut;
pub mod spi1d;
pub mod synth;
pub mod transform;
@@ -44,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;
+142
View File
@@ -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));
}
}
+43
View File
@@ -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
}
+82
View File
@@ -0,0 +1,82 @@
//! Sony Imageworks SPI3D and SPIMTX text readers, extracted from EffectCraft.
//!
//! These preserve the source reader's format tolerance. SPI3D sample values are not
//! clamped or sanitized; rendering policy belongs to the caller. Duplicate coordinates
//! retain their last value, and the total row count must match the lattice volume.
/// Maximum lattice side accepted by the source reader.
pub const MAX_SIDE: usize = 129;
/// Row-major homogeneous transform with a separate normalized code-value offset.
#[derive(Clone, Debug, PartialEq)]
pub struct Matrix {
pub matrix: [f64; 16],
pub offset: [f64; 4],
}
/// A cubic RGB table, red coordinate varying fastest.
#[derive(Clone, Debug, PartialEq)]
pub struct Lattice {
pub size: usize,
pub values: Vec<[f32; 3]>,
}
/// Read exactly twelve finite SPIMTX numbers, dividing the offset column by 65535.
/// Parsing uses a fixed array, not storage proportional to an oversized input.
pub fn parse_spimtx(text: &str) -> Option<Matrix> {
let mut words = text.split_whitespace();
let mut v = [0.0f64; 12];
for value in &mut v {
*value = words.next()?.parse().ok()?;
if !value.is_finite() {
return None;
}
}
if words.next().is_some() {
return None;
}
let mut matrix = [0.0; 16];
let mut offset = [0.0; 4];
for (r, row) in v.as_chunks::<4>().0.iter().enumerate() {
matrix[r * 4..r * 4 + 3].copy_from_slice(&row[..3]);
offset[r] = row[3] / 65535.0;
}
matrix[15] = 1.0;
Some(Matrix { matrix, offset })
}
/// Read a SPI3D table with a bounded 2..=129 cubic lattice and exactly six fields per row.
/// A `SPILUT` prefix is accepted and the second header line is ignored, as in EffectCraft.
pub fn parse_spi3d(text: &str) -> Option<Lattice> {
let mut lines = text.lines().map(str::trim).filter(|l| !l.is_empty());
if !lines.next()?.starts_with("SPILUT") {
return None;
}
lines.next()?;
let mut sizes = lines.next()?.split_whitespace();
let n: usize = sizes.next()?.parse().ok()?;
let g: usize = sizes.next()?.parse().ok()?;
let b: usize = sizes.next()?.parse().ok()?;
if sizes.next().is_some() || g != n || b != n || !(2..=MAX_SIDE).contains(&n) {
return None;
}
let count = n * n * n;
let mut values = vec![[0.0f32; 3]; count];
let mut seen = 0usize;
for line in lines {
if seen == count {
return None;
}
let mut fields = line.split_whitespace();
let r: usize = fields.next()?.parse().ok()?;
let g: usize = fields.next()?.parse().ok()?;
let b: usize = fields.next()?.parse().ok()?;
let color = [fields.next()?.parse().ok()?, fields.next()?.parse().ok()?, fields.next()?.parse().ok()?];
if fields.next().is_some() || r >= n || g >= n || b >= n {
return None;
}
*values.get_mut(r + g * n + b * n * n)? = color;
seen += 1;
}
(seen == count).then_some(Lattice { size: n, values })
}
+46
View File
@@ -0,0 +1,46 @@
use craft_cms::sony_lut::{MAX_SIDE, parse_spi3d, parse_spimtx};
#[test]
fn spimtx_is_finite_exact_and_scales_offsets() {
let m = parse_spimtx("2 0 0 6553.5\n0 3 0 -65535\n0 0 4 0").unwrap();
assert_eq!(m.matrix, [2.0, 0.0, 0.0, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0]);
assert_eq!(m.offset, [0.1, -1.0, 0.0, 0.0]);
for bad in ["", "1 0 0 0", "NaN 0 0 0 0 1 0 0 0 0 1 0", "inf 0 0 0 0 1 0 0 0 0 1 0", "1 0 0 0 0 1 0 0 0 0 1 0 9"] {
assert!(parse_spimtx(bad).is_none());
}
}
fn table() -> String {
let mut text = String::from("SPILUT 1.0\n3 3\n2 2 2\n");
// Reverse row order to prove coordinates, rather than row order, determine storage.
for i in (0..8).rev() {
let (r, g, b) = (i & 1, (i >> 1) & 1, i >> 2);
text.push_str(&format!("{r} {g} {b} {r} {g} {b}\n"));
}
text
}
#[test]
fn spi3d_red_fast_coordinates_and_limits() {
let text = table();
let lut = parse_spi3d(&text).unwrap();
assert_eq!(lut.size, 2);
for (i, row) in lut.values.iter().enumerate() {
assert_eq!(*row, [(i & 1) as f32, ((i >> 1) & 1) as f32, (i >> 2) as f32]);
}
assert!(parse_spi3d(&format!("{text}0 0 0 0 0 0\n")).is_none());
assert!(parse_spi3d("SPILUT 1.0\n3 3\n2 2 2\n0 0 0 0 0 0\n").is_none());
assert!(parse_spi3d(&format!("SPILUT 1.0\n3 3\n{0} {0} {0}\n", MAX_SIDE + 1)).is_none());
assert!(parse_spi3d(&text.replace("2 2 2", "2 2 3")).is_none());
assert!(parse_spi3d(&text.replace("0 0 0 0 0 0", "2 0 0 0 0 0")).is_none());
assert!(parse_spi3d(&text.replace("0 0 0 0 0 0", "0 0 0 0 0 0 extra")).is_none());
}
#[test]
fn inherited_tolerance_is_explicit() {
let text = "SPILUT alternative\nignored\n2 2 2\n".to_owned() + &"0 0 0 -1 2 NaN\n".repeat(8);
let lut = parse_spi3d(&text).unwrap();
assert_eq!(&lut.values[0][..2], &[-1.0, 2.0]);
assert!(lut.values[0][2].is_nan());
assert_eq!(lut.values[1], [0.0; 3]);
}
+3
View File
@@ -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;
+110
View File
@@ -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));
}
}
+3
View File
@@ -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 }
+62
View File
@@ -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);
}
}
+12
View File
@@ -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;
+33
View File
@@ -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());
}
}
+49
View File
@@ -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));
}
}
+55
View File
@@ -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));
}
}
+181
View File
@@ -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());
}
}
}
+69
View File
@@ -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]);
}
}
+16
View File
@@ -0,0 +1,16 @@
[package]
name = "craft-gridcraft-format"
description = "GridCraft's native JSON workbook file: its extensions, reader and pretty writer"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
repository.workspace = true
[lints]
workspace = true
[dependencies]
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
+17
View File
@@ -0,0 +1,17 @@
# craft-gridcraft-format
GridCraft's native workbook file is one pretty-printed UTF-8 JSON object, with a two-space indent
and no newline at the end. It is saved as `.json`, or as `.scjson` by older builds.
The workbook is a type parameter. GridCraft owns the model, and after a read it rebuilds the
model's indexes, drops cells outside the grid and reads the macros. This crate owns the bytes:
- `EXTENSIONS` and `is_extension` name the format.
- `read(bytes)` reads a workbook. A failure reads as "not a GridCraft JSON workbook: …".
- `write(workbook)` writes one with `serde_json`'s pretty printer.
- `write_with_kept(workbook, field, kept)` appends entries that a newer version wrote, and this
build kept without reading, to the top-level list `field`, as the file held them. With nothing
kept, the output is exactly `write`'s. Otherwise the workbook goes through a `serde_json::Value`
first, so its object keys come out in map order, as GridCraft has always written them.
There is no `unsafe` and no panics, and it builds for `wasm32-unknown-unknown`.
+59
View File
@@ -0,0 +1,59 @@
//! GridCraft's native workbook file: the workbook as one pretty-printed UTF-8 JSON object,
//! saved as `.json` (or `.scjson`, its older name).
//!
//! GridCraft owns the workbook model and what happens after a read: rebuilding its indexes,
//! dropping cells outside the grid, and reading its macros. This crate owns the bytes: which
//! extensions name the format, how the JSON is read and written, and how entries kept from a
//! newer version are written back into a list as the file held them.
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
#![forbid(unsafe_code)]
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::Value;
/// The extensions of the format, lower case. `scjson` is the name older builds saved.
pub const EXTENSIONS: [&str; 2] = ["json", "scjson"];
/// Whether `extension` (without the dot, any case) names the format.
pub fn is_extension(extension: &str) -> bool {
EXTENSIONS.iter().any(|e| e.eq_ignore_ascii_case(extension))
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// The bytes are not a workbook: not JSON, or JSON of another shape.
#[error("not a GridCraft JSON workbook: {0}")]
Read(serde_json::Error),
/// The workbook could not be written as JSON.
#[error("{0}")]
Write(serde_json::Error),
}
/// Read a workbook. Unknown fields are left to the model's own serde rules.
pub fn read<T: DeserializeOwned>(bytes: &[u8]) -> Result<T, Error> {
serde_json::from_slice(bytes).map_err(Error::Read)
}
/// Write a workbook as pretty-printed JSON (two-space indent, no newline at the end).
pub fn write<T: Serialize>(workbook: &T) -> Result<Vec<u8>, Error> {
serde_json::to_vec_pretty(workbook).map_err(Error::Write)
}
/// [`write`], with `kept` appended to the top-level list `field` (made if absent): entries a
/// newer version wrote that this build kept without reading. With nothing kept this is exactly
/// [`write`]. Otherwise the workbook goes through a [`Value`] first, so its object keys are
/// written in `serde_json`'s map order.
pub fn write_with_kept<T: Serialize>(workbook: &T, field: &str, kept: &[Value]) -> Result<Vec<u8>, Error> {
if kept.is_empty() {
return write(workbook);
}
let mut v = serde_json::to_value(workbook).map_err(Error::Write)?;
if let Some(list) = v.as_object_mut().map(|o| o.entry(field).or_insert_with(|| Value::Array(vec![]))).and_then(Value::as_array_mut) {
list.extend(kept.iter().cloned());
}
serde_json::to_vec_pretty(&v).map_err(Error::Write)
}
#[cfg(test)]
mod tests;
+87
View File
@@ -0,0 +1,87 @@
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use serde::Deserialize;
use serde_json::json;
use super::*;
/// A stand-in for an app's workbook: fields in declaration order, one of them a list.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Book {
sheets: Vec<String>,
active: u32,
#[serde(default)]
macros: Vec<Value>,
}
fn book() -> Book {
Book { sheets: vec!["Sheet1".into(), "Été ✓".into()], active: 1, macros: vec![json!({"v": 1, "name": "Fill"})] }
}
#[test]
fn extensions_are_json_and_scjson_in_any_case() {
for yes in ["json", "scjson", "JSON", "ScJson"] {
assert!(is_extension(yes), "{yes}");
}
for no in ["", "xlsx", "jsonl", ".json", "scjs"] {
assert!(!is_extension(no), "{no}");
}
}
#[test]
fn a_workbook_is_written_pretty_in_field_order() {
let bytes = write(&book()).unwrap();
let text = String::from_utf8(bytes).unwrap();
assert_eq!(
text,
"{\n \"sheets\": [\n \"Sheet1\",\n \"Été ✓\"\n ],\n \"active\": 1,\n \"macros\": [\n {\n \"name\": \"Fill\",\n \"v\": 1\n }\n ]\n}"
);
assert_eq!(read::<Book>(text.as_bytes()).unwrap(), book());
}
#[test]
fn kept_entries_are_appended_as_the_file_held_them() {
let kept = [json!({"v": 9, "name": "Future", "extra": [1, 2]})];
let bytes = write_with_kept(&book(), "macros", &kept).unwrap();
let back: Book = read(&bytes).unwrap();
assert_eq!(back.macros, [json!({"v": 1, "name": "Fill"}), kept[0].clone()]);
// Through a JSON value, keys are in map order: `active` before `macros` before `sheets`.
let text = String::from_utf8(bytes).unwrap();
assert!(text.find("\"active\"") < text.find("\"macros\"") && text.find("\"macros\"") < text.find("\"sheets\""), "{text}");
// With nothing kept, the plain writer's bytes.
assert_eq!(write_with_kept(&book(), "macros", &[]).unwrap(), write(&book()).unwrap());
}
#[test]
fn kept_entries_make_the_list_when_it_is_absent() {
#[derive(Serialize)]
struct Bare {
sheets: Vec<String>,
}
let bytes = write_with_kept(&Bare { sheets: vec![] }, "macros", &[json!(1)]).unwrap();
assert_eq!(serde_json::from_slice::<Value>(&bytes).unwrap(), json!({"sheets": [], "macros": [1]}));
// A field that is not a list is left as it is.
let bytes = write_with_kept(&json!({"macros": "text"}), "macros", &[json!(1)]).unwrap();
assert_eq!(serde_json::from_slice::<Value>(&bytes).unwrap(), json!({"macros": "text"}));
}
#[test]
fn what_is_not_a_workbook_says_so() {
for bad in [&b""[..], b"{", b"[]", b"{\"sheets\": 7, \"active\": 0}", b"\xff\xfe"] {
let err = read::<Book>(bad).unwrap_err();
assert!(matches!(err, Error::Read(_)));
assert!(err.to_string().starts_with("not a GridCraft JSON workbook: "), "{err}");
}
assert_eq!(
read::<Book>(b"{\"sheets\": 7}").unwrap_err().to_string(),
"not a GridCraft JSON workbook: invalid type: integer `7`, expected a sequence at line 1 column 12"
);
}
#[test]
fn a_value_json_cannot_hold_is_a_write_error() {
use std::collections::BTreeMap;
let bad: BTreeMap<Vec<u8>, u8> = [(vec![1], 1)].into();
assert!(matches!(write(&bad), Err(Error::Write(_))));
assert!(matches!(write_with_kept(&bad, "macros", &[json!(1)]), Err(Error::Write(_))));
}
+4
View File
@@ -13,6 +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
+2
View File
@@ -57,6 +57,8 @@ 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 |
| HEIF/HEIC | with feature `heif` | **no** | U8, U16 (10/12-bit decodes to U16) | RGB, RGBA | yes (auxiliary alpha) | yes (`colr` prof) | yes | yes | no | no | n/a | `heic-rs` |
+4
View File
@@ -20,6 +20,10 @@ pub(crate) mod jxl;
pub(crate) mod pcx;
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;
+9
View File
@@ -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 {
+20
View File
@@ -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"))
}
+395
View File
@@ -0,0 +1,395 @@
//! SGI image files (`.sgi`, `.rgb`, `.rgba`, `.bw`): 8- and 16-bit gray, gray + alpha, RGB and
//! RGBA, verbatim or run-length encoded.
//!
//! Read and write follow the published SGI image file format description: a 512-byte big-endian
//! header, then one plane per channel with the rows stored bottom to top. Palette-mapped files
//! (colormap ID other than 0) are rejected. The writer uses the run-length encoding.
use crate::error::CodecError;
use crate::fidelity::Plan;
use crate::format::Format;
use crate::image::{ChannelLayout, Image, SampleType};
use crate::options::Limits;
const F: Format = Format::Sgi;
const HEADER: usize = 512;
const MAGIC: u16 = 474;
fn bad(reason: impl std::fmt::Display) -> CodecError {
CodecError::malformed(F, reason)
}
fn be16(b: &[u8], i: usize) -> u16 {
u16::from_be_bytes([b[i], b[i + 1]])
}
fn be32(b: &[u8], i: usize) -> u32 {
u32::from_be_bytes([b[i], b[i + 1], b[i + 2], b[i + 3]])
}
/// The fields of the header that decoding needs, already checked for plausibility.
struct Header {
rle: bool,
wide: bool,
width: u32,
height: u32,
planes: usize,
}
/// Parse the 512-byte header. `None` when the bytes are not a plausible SGI header.
fn header(b: &[u8]) -> Option<Header> {
if b.len() < HEADER || be16(b, 0) != MAGIC {
return None;
}
let (storage, bpc, dimension) = (b[2], b[3], be16(b, 4));
let (width, height, planes) = (u32::from(be16(b, 6)), u32::from(be16(b, 8)), usize::from(be16(b, 10)));
if storage > 1 || !(1..=2).contains(&bpc) || !(1..=3).contains(&dimension) {
return None;
}
// A one-dimensional image has one row, a two-dimensional one a single plane.
let (height, planes) = match dimension {
1 => (1, 1),
2 => (height, 1),
_ => (height, planes),
};
if width == 0 || height == 0 || !(1..=4).contains(&planes) {
return None;
}
Some(Header { rle: storage == 1, wide: bpc == 2, width, height, planes })
}
/// Whether `b` starts with a plausible SGI header (the magic number plus consistent fields).
pub(crate) fn looks_like_sgi(b: &[u8]) -> bool {
header(b).is_some()
}
fn layout_of(planes: usize) -> ChannelLayout {
match planes {
1 => ChannelLayout::Gray,
2 => ChannelLayout::GrayA,
3 => ChannelLayout::Rgb,
_ => ChannelLayout::Rgba,
}
}
/// Expand one run-length encoded row into exactly `out.len()` samples.
fn read_rle_row(src: &[u8], wide: bool, out: &mut [u16]) -> Result<(), CodecError> {
let step = if wide { 2 } else { 1 };
let sample = |pos: usize| -> Result<u16, CodecError> {
let s = src.get(pos..pos + step).ok_or_else(|| bad("truncated RLE row"))?;
Ok(if wide { u16::from_be_bytes([s[0], s[1]]) } else { u16::from(s[0]) })
};
let (mut pos, mut n) = (0usize, 0usize);
while n < out.len() {
let code = sample(pos)?;
pos += step;
let count = usize::from(code & 0x7f);
if count == 0 {
return Err(bad("RLE row ends before the image width"));
}
if count > out.len() - n {
return Err(bad("RLE packet runs past the end of the row"));
}
if code & 0x80 != 0 {
for slot in &mut out[n..n + count] {
*slot = sample(pos)?;
pos += step;
}
} else {
let value = sample(pos)?;
pos += step;
out[n..n + count].fill(value);
}
n += count;
}
Ok(())
}
pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
let h = header(b).ok_or_else(|| bad("invalid SGI header"))?;
if be32(b, 104) != 0 {
return Err(CodecError::unsupported(F, "palette-mapped SGI files are not supported"));
}
let layout = layout_of(h.planes);
let sample = if h.wide { SampleType::U16 } else { SampleType::U8 };
limits.check(h.width, h.height, layout, sample)?;
let (w, ht, planes) = (h.width as usize, h.height as usize, h.planes);
let rows = ht.checked_mul(planes).ok_or_else(|| bad("image size overflow"))?;
let bps = if h.wide { 2 } else { 1 };
let total = w.checked_mul(rows).ok_or_else(|| bad("image size overflow"))?;
// Reject inputs that cannot hold the declared pixels before allocating anything for them.
let table = if h.rle { rows.checked_mul(8).ok_or_else(|| bad("image size overflow"))? } else { 0 };
let min_len = if h.rle {
// Both offset tables must fit; rows may share compressed data, so the data itself is
// checked row by row against the offsets below.
HEADER + table
} else {
HEADER + total.checked_mul(bps).ok_or_else(|| bad("image size overflow"))?
};
if b.len() < min_len {
return Err(bad("the file is too short for the declared image"));
}
let mut out = Vec::new();
out.try_reserve_exact(total).map_err(|_| bad("pixel buffer is too large"))?;
out.resize(total, 0u16);
let mut row = vec![0u16; w];
for z in 0..planes {
for y in 0..ht {
let index = y + z * ht;
if h.rle {
let start = be32(b, HEADER + index * 4) as usize;
let len = be32(b, HEADER + rows * 4 + index * 4) as usize;
let end = start
.checked_add(len)
.filter(|&e| e <= b.len() && start >= HEADER + table)
.ok_or_else(|| bad("RLE row lies outside the file"))?;
read_rle_row(&b[start..end], h.wide, &mut row)?;
} else {
let start = HEADER + index * w * bps;
let s = &b[start..start + w * bps];
for (x, v) in row.iter_mut().enumerate() {
*v = if h.wide { u16::from_be_bytes([s[2 * x], s[2 * x + 1]]) } else { u16::from(s[x]) };
}
}
// Rows are stored bottom to top; planes are separate.
let top_y = ht - 1 - y;
for (x, &v) in row.iter().enumerate() {
out[(top_y * w + x) * planes + z] = v;
}
}
}
if h.wide {
Image::from_u16(h.width, h.height, layout, &out)
} else {
Image::from_u8(h.width, h.height, layout, out.iter().map(|&v| v as u8).collect())
}
}
/// Append one run-length encoded row (one packet list and a terminator).
fn write_rle_row(row: &[u16], wide: bool, out: &mut Vec<u8>) {
let put = |out: &mut Vec<u8>, v: u16| {
if wide {
out.extend_from_slice(&v.to_be_bytes());
} else {
out.push(v as u8);
}
};
let mut i = 0;
while i < row.len() {
let mut run = 1;
while i + run < row.len() && run < 127 && row[i + run] == row[i] {
run += 1;
}
if run >= 2 {
put(out, run as u16);
put(out, row[i]);
i += run;
} else {
// A literal packet extends until the next run of two or more.
let mut end = i + 1;
while end < row.len() && end - i < 127 && !(end + 1 < row.len() && row[end] == row[end + 1]) {
end += 1;
}
put(out, 0x80 | (end - i) as u16);
for &v in &row[i..end] {
put(out, v);
}
i = end;
}
}
put(out, 0);
}
/// Writes a run-length encoded SGI file: 8 or 16 bits per channel, gray, gray + alpha, RGB or RGBA.
pub(crate) fn encode(src: &Image, plan: Plan) -> Result<Vec<u8>, CodecError> {
let enc = |msg: String| CodecError::encode(F, msg);
let img = src.converted(plan.layout, plan.sample);
let wide = match img.sample_type() {
SampleType::U8 => false,
SampleType::U16 => true,
s => return Err(enc(format!("SGI stores 8 or 16 bits per channel, not {s:?}"))),
};
let planes = img.layout().channels();
if !matches!(img.layout(), ChannelLayout::Gray | ChannelLayout::GrayA | ChannelLayout::Rgb | ChannelLayout::Rgba) {
return Err(enc(format!("no SGI layout for {:?}", img.layout())));
}
let (w, ht) = img.dimensions();
let (Ok(w16), Ok(h16)) = (u16::try_from(w), u16::try_from(ht)) else {
return Err(enc(format!("dimensions exceed 65535x65535: {w}x{ht}")));
};
let (w, ht) = (w as usize, ht as usize);
let bps = if wide { 2 } else { 1 };
let data = img.data();
if data.len() != w * ht * planes * bps {
return Err(enc("the image has the wrong number of samples".into()));
}
let sample_at = |i: usize| -> u16 { if wide { u16::from_ne_bytes([data[2 * i], data[2 * i + 1]]) } else { u16::from(data[i]) } };
let rows = ht * planes;
let table = HEADER;
let mut starts = Vec::with_capacity(rows);
let mut lens = Vec::with_capacity(rows);
let mut body = Vec::new();
let mut row = vec![0u16; w];
let base = u32::try_from(HEADER + rows * 8).map_err(|_| enc("image too large".into()))?;
for z in 0..planes {
for y in 0..ht {
let src_y = ht - 1 - y; // stored bottom to top
for (x, v) in row.iter_mut().enumerate() {
*v = sample_at((src_y * w + x) * planes + z);
}
let before = body.len();
write_rle_row(&row, wide, &mut body);
let start = u32::try_from(base as usize + before).map_err(|_| enc("image too large".into()))?;
starts.push(start);
lens.push((body.len() - before) as u32);
}
}
let mut out = Vec::with_capacity(table + rows * 8 + body.len());
out.extend_from_slice(&MAGIC.to_be_bytes());
out.push(1); // RLE
out.push(bps as u8);
out.extend_from_slice(&(if planes > 1 { 3u16 } else { 2u16 }).to_be_bytes());
out.extend_from_slice(&w16.to_be_bytes());
out.extend_from_slice(&h16.to_be_bytes());
out.extend_from_slice(&(planes as u16).to_be_bytes());
out.extend_from_slice(&0u32.to_be_bytes()); // pixmin
out.extend_from_slice(&(if wide { 65535u32 } else { 255 }).to_be_bytes()); // pixmax
out.resize(HEADER, 0); // dummy, image name, colormap 0, padding
for s in &starts {
out.extend_from_slice(&s.to_be_bytes());
}
for l in &lens {
out.extend_from_slice(&l.to_be_bytes());
}
out.extend_from_slice(&body);
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
fn gradient(w: u32, h: u32, layout: ChannelLayout) -> Image {
let ch = layout.channels();
let data = (0..w as usize * h as usize * ch).map(|i| ((i * 7 + i / 5) % 251) as u8).collect();
Image::from_u8(w, h, layout, data).unwrap()
}
#[test]
fn rle_round_trips_every_layout_in_8_bits() {
for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] {
let img = gradient(37, 11, layout);
let plan = Plan { layout, sample: SampleType::U8 };
let bytes = encode(&img, plan).unwrap();
assert!(looks_like_sgi(&bytes));
let back = decode(&bytes, &Limits::default()).unwrap();
assert_eq!(back.layout(), layout);
assert_eq!(back.data(), img.data());
}
}
#[test]
fn rle_round_trips_16_bits_with_long_runs() {
let (w, h) = (300u32, 4u32);
let samples: Vec<u16> = (0..w * h * 3).map(|i| if (i / 3) % 200 < 150 { 4242 } else { (i * 31) as u16 }).collect();
let img = Image::from_u16(w, h, ChannelLayout::Rgb, &samples).unwrap();
let bytes = encode(&img, Plan { layout: ChannelLayout::Rgb, sample: SampleType::U16 }).unwrap();
let back = decode(&bytes, &Limits::default()).unwrap();
assert_eq!(back.sample_type(), SampleType::U16);
assert_eq!(back.data(), img.data());
}
/// A hand-built verbatim file: two rows, one plane, stored bottom to top.
#[test]
fn verbatim_files_read_bottom_to_top() {
let mut f = vec![0u8; HEADER];
f[0..2].copy_from_slice(&MAGIC.to_be_bytes());
f[2] = 0; // verbatim
f[3] = 1;
f[4..6].copy_from_slice(&2u16.to_be_bytes());
f[6..8].copy_from_slice(&3u16.to_be_bytes()); // x
f[8..10].copy_from_slice(&2u16.to_be_bytes()); // y
f[10..12].copy_from_slice(&1u16.to_be_bytes()); // z
f.extend_from_slice(&[10, 11, 12, 20, 21, 22]); // bottom row first
let img = decode(&f, &Limits::default()).unwrap();
assert_eq!(img.layout(), ChannelLayout::Gray);
assert_eq!(img.data(), &[20, 21, 22, 10, 11, 12]);
}
#[test]
fn hostile_input_is_an_error_not_a_panic() {
let img = gradient(16, 8, ChannelLayout::Rgb);
let good = encode(&img, Plan { layout: ChannelLayout::Rgb, sample: SampleType::U8 }).unwrap();
// Every truncation fails cleanly.
for len in 0..good.len() {
let _ = decode(&good[..len], &Limits::default());
}
// A row table pointing outside the file, and a packet that overruns its row.
let mut bad_table = good.clone();
bad_table[HEADER..HEADER + 4].copy_from_slice(&u32::MAX.to_be_bytes());
assert!(decode(&bad_table, &Limits::default()).is_err());
let mut overrun = good.clone();
let start = be32(&overrun, HEADER) as usize;
overrun[start] = 127; // a run longer than the 16-pixel row
assert!(decode(&overrun, &Limits::default()).is_err());
// A huge declared size is stopped by the limits or the length check, before allocating.
let mut huge = good;
huge[6..8].copy_from_slice(&u16::MAX.to_be_bytes());
huge[8..10].copy_from_slice(&u16::MAX.to_be_bytes());
assert!(decode(&huge, &Limits::default()).is_err());
}
/// A 1-plane, 8-bit RLE file whose `rows` all start where `starts[i]` says, each `len` bytes long.
fn rle_file(w: u16, starts: &[u32], len: u32, payload: &[u8]) -> Vec<u8> {
let mut f = vec![0u8; HEADER];
f[0..2].copy_from_slice(&MAGIC.to_be_bytes());
f[2] = 1;
f[3] = 1;
f[4..6].copy_from_slice(&2u16.to_be_bytes());
f[6..8].copy_from_slice(&w.to_be_bytes());
f[8..10].copy_from_slice(&(starts.len() as u16).to_be_bytes());
f[10..12].copy_from_slice(&1u16.to_be_bytes());
for s in starts {
f.extend_from_slice(&s.to_be_bytes());
}
for _ in starts {
f.extend_from_slice(&len.to_be_bytes());
}
f.extend_from_slice(payload);
f
}
/// The format lets table entries share one compressed row: eight rows, one payload.
#[test]
fn rows_may_share_compressed_data() {
let table = HEADER + 8 * 8;
let f = rle_file(1, &[table as u32; 8], 3, &[0x81, 255, 0]);
assert_eq!(f.len(), table + 3);
let img = decode(&f, &Limits::default()).unwrap();
assert_eq!((img.dimensions(), img.data()), ((1, 8), &[255u8; 8][..]));
}
/// Compressed rows start after both tables: an offset into a table is refused.
#[test]
fn rows_may_not_start_inside_the_offset_tables() {
// 3x1: the tables end at 520; 515 reads the table's own bytes as a packet list.
let f = rle_file(3, &[515], 3, &[0, 0, 0, 0, 0]);
assert!(decode(&f, &Limits::default()).is_err());
}
/// FFmpeg's decoder, run as an oracle, also reads a row that ends with its last packet and no
/// zero terminator; so does this one.
#[test]
fn a_row_without_its_terminator_still_reads() {
let f = rle_file(1, &[520], 2, &[0x81, 42]);
assert_eq!(decode(&f, &Limits::default()).unwrap().data(), &[42]);
}
#[test]
fn palette_mapped_files_are_unsupported() {
let img = gradient(4, 4, ChannelLayout::Gray);
let mut f = encode(&img, Plan { layout: ChannelLayout::Gray, sample: SampleType::U8 }).unwrap();
f[104..108].copy_from_slice(&1u32.to_be_bytes());
assert!(matches!(decode(&f, &Limits::default()), Err(CodecError::Unsupported { .. })));
}
}
+281
View File
@@ -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 { .. })));
}
}
+93 -2
View File
@@ -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.
+32 -2
View File
@@ -45,6 +45,10 @@ 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.
JpegLs,
}
@@ -118,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; 25] = [
pub const ALL: [Format; 27] = [
Format::Png,
Format::Jpeg,
Format::Jp2,
@@ -143,6 +147,8 @@ impl Format {
Format::Cineon,
Format::Dicom,
Format::Iff,
Format::SunRaster,
Format::Sgi,
Format::JpegLs,
];
@@ -182,6 +188,8 @@ impl Format {
Format::Dicom => "DICOM",
Format::Iff => "Amiga IFF",
Format::Jp2 => "JPEG 2000",
Format::SunRaster => "Sun Raster",
Format::Sgi => "SGI",
Format::JpegLs => "JPEG-LS",
}
}
@@ -214,6 +222,8 @@ 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"],
}
}
@@ -244,6 +254,8 @@ 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",
}
}
@@ -251,7 +263,7 @@ impl Format {
/// Maximum encodable dimensions, if the format has a hard limit.
pub fn max_dimensions(self) -> Option<(u32, u32)> {
match self {
Format::Jpeg | Format::Gif | Format::Iff | Format::JpegLs => Some((65535, 65535)),
Format::Jpeg | Format::Gif | Format::Iff | Format::Sgi | Format::JpegLs => Some((65535, 65535)),
Format::WebP => Some((16384, 16384)),
Format::Ico => Some((256, 256)),
_ => None,
@@ -321,6 +333,16 @@ 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 }
}
Format::JpegLs => FormatCaps { depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base },
Format::Jp2 => FormatCaps { read: true, write: false, layouts: &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA], icc: true, ..base },
}
@@ -403,6 +425,14 @@ 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);
}
if crate::codecs::pcx::looks_like_pcx(b) {
return Some(Format::Pcx);
}
+63 -5
View File
@@ -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;
@@ -67,6 +68,10 @@ pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_
pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding};
pub use half::f16;
#[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`].
@@ -106,6 +111,26 @@ 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")]
{
sgi::decode(bytes, l)
}
#[cfg(not(feature = "sgi"))]
{
Err(CodecError::unsupported(format, "SGI support requires the sgi feature"))
}
}
Format::JpegLs => jpegls::decode(bytes, l),
Format::OpenExr => exr::decode(bytes, l),
Format::Heif => heif::decode(bytes, l, opts.keep_orientation),
@@ -213,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 {
@@ -225,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")))]
@@ -238,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
@@ -481,6 +519,26 @@ 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")]
{
sgi::encode(image, plan)
}
#[cfg(not(feature = "sgi"))]
{
Err(CodecError::unsupported(format, "SGI support requires the sgi feature"))
}
}
Format::JpegLs => jpegls::encode(image, plan),
Format::OpenExr => exr::encode(image, plan, opts),
Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")),
+1 -1
View File
@@ -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.
+59
View File
@@ -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,10 @@ fn default_build_formats_all_symmetric() {
Format::Dpx,
Format::Cineon,
Format::Iff,
#[cfg(feature = "sunraster")]
Format::SunRaster,
#[cfg(feature = "sgi")]
Format::Sgi,
Format::JpegLs,
];
for f in expected {
@@ -224,6 +228,23 @@ fn detect_magic_iff() {
assert_eq!(detect(b"FORM\0\0\0\x14AIFF"), None);
assert!(decode_as(Format::Iff, b"FORM\0\0\0\x14ILBM").is_err());
}
#[cfg(feature = "sgi")]
#[test]
fn detect_magic_sgi() {
use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode};
let img = Image::from_u8(5, 3, ChannelLayout::Rgba, (0..60).collect()).unwrap();
let bytes = encode(&img, Format::Sgi, &EncodeOptions::default()).unwrap();
assert_eq!(detect(&bytes), Some(Format::Sgi));
assert_eq!(from_extension("scan.RGB"), Some(Format::Sgi));
assert_eq!(from_extension("scan.bw"), Some(Format::Sgi));
assert_eq!(decode(&bytes).unwrap().data(), img.data());
assert!(!ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::Sgi));
assert_eq!(Format::Sgi.max_dimensions(), Some((65535, 65535)));
// The magic number alone, without a full header, is not SGI.
assert_eq!(detect(&[0x01, 0xda, 0, 1, 0, 2]), None);
assert!(decode_as(Format::Sgi, &[0x01, 0xda]).is_err());
}
#[test]
fn detect_magic_jpeg_ls() {
use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode};
@@ -239,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));
@@ -328,3 +363,28 @@ fn extensions_unique_across_formats() {
all.dedup();
assert_eq!(all.len(), n);
}
#[cfg(not(feature = "sgi"))]
#[test]
fn sgi_requires_explicit_feature() {
use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode};
assert!(!caps(Format::Sgi).read && !caps(Format::Sgi).write);
assert!(decode_as(Format::Sgi, &[]).is_err());
let image = Image::from_u8(1, 1, ChannelLayout::Gray, vec![0]).unwrap();
assert!(encode(&image, Format::Sgi, &EncodeOptions::default()).is_err());
let mut header = vec![0; 512];
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());
}
+29
View File
@@ -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));
}
+18 -2
View File
@@ -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));
}
+4
View File
@@ -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"] }
+223
View File
@@ -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();
}
}
+2
View File
@@ -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};
+2 -2
View File
@@ -899,7 +899,7 @@ struct Layer {
impl Layer {
/// The layer and its flat array of `width` numbers per entry.
fn of(geom: &Record, element: &str, data: &str, index: &[&str], width: usize) -> Option<(Layer, Vec<f64>)> {
let e = geom.child(element)?;
let e = geom.children_named(element).min_by_key(|e| e.props.first().and_then(Prop::as_i64) != Some(0))?;
let mapping = match e.child("MappingInformationType").and_then(|m| m.str_at(0)).unwrap_or("ByPolygonVertex") {
"ByPolygonVertex" => Mapping::PolygonVertex,
"ByVertex" | "ByVertice" | "ByControlPoint" => Mapping::Vertex,
@@ -908,7 +908,7 @@ impl Layer {
_ => return None,
};
let reference = e.child("ReferenceInformationType").and_then(|m| m.str_at(0)).unwrap_or("Direct");
let index = (reference != "Direct").then(|| index.iter().find_map(|n| e.child(n)).map(Record::ints)).flatten();
let index = if matches!(reference, "IndexToDirect" | "Index") { Some(index.iter().find_map(|n| e.child(n))?.ints()) } else { None };
let data = e.child(data)?.floats();
Some((Layer { mapping, index, len: data.len() / width }, data))
}
+10
View File
@@ -628,6 +628,16 @@ fn fbx6_model_holds_its_mesh() {
}
}
#[test]
fn layer_zero_takes_precedence_and_an_indexed_layer_needs_indices() {
let layer_one = "LayerElementUV: 1 { MappingInformationType: \"ByPolygonVertex\" ReferenceInformationType: \"Direct\" UV: 0.25,0.25, 0.25,0.25, 0.25,0.25, 0.25,0.25, 0.25,0.25, 0.25,0.25, 0.25,0.25 }\n";
let text = ASCII.replace("LayerElementUV: 0 {", &format!("{layer_one} LayerElementUV: 0 {{"));
let scene = parse(text.as_bytes()).unwrap();
assert_eq!(scene.meshes[0].uv_sets[0][0], [0.0, 1.0]);
let missing = ASCII.replace("UVIndex: *7", "UnusedIndex: *7");
assert!(parse(missing.as_bytes()).unwrap().meshes[0].uv_sets.is_empty());
}
#[test]
fn optional_inactive_pre_rotation_keeps_legacy_placements() {
let text = "FBXHeaderExtension: { FBXVersion: 7400 } Objects: { Model: 1, \"Model::A\", \"Null\" { Properties70: { P: \"PreRotation\", \"\", \"\", \"\", 0,0,90 } } }";
+237
View File
@@ -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(&not_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);
}
}
+1
View File
@@ -20,6 +20,7 @@
//! [`Embedded`] and call [`from_embedded`].
pub mod camera_fields;
pub mod compat_orientation;
mod container;
pub mod cr3;
mod datetime;
+372
View File
@@ -0,0 +1,372 @@
//! DNG Camera Profiles (`.dcp`): the user's own camera colour profiles, read at run time from
//! files they supply (DNG specification, chapter 6 "Camera Profiles"). A `.dcp` is a TIFF-structured
//! file whose header carries the magic `RC` (0x4352) in place of 42, with the profile tags in IFD 0:
//! `UniqueCameraModel`, `ProfileName`, the colour/forward matrices and calibration illuminants, the
//! hue/saturation map, look table and tone curve. Nothing here ships a profile.
use craft_tiff::{Ifd, ParseOptions, Tiff, tags as t};
use crate::ColorData;
/// The magic number of a DNG camera profile file (`IIRC` / `MMCR`).
pub const DCP_MAGIC: u16 = 0x4352;
/// A parsed camera profile.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct DcpProfile {
/// `ProfileName` (empty when the file has none).
pub name: String,
/// `UniqueCameraModel`: the camera the profile was made for (empty when absent).
pub camera_model: String,
/// The colour model: illuminants, matrices and the profile look (hue/sat map, look table, tone curve).
pub color: ColorData,
}
/// Errors reading a `.dcp`.
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum DcpError {
#[error("not a DNG camera profile (.dcp)")]
NotDcp,
#[error("damaged camera profile: {0}")]
Damaged(String),
#[error("the camera profile has no usable colour matrix")]
NoMatrix,
}
/// Is `bytes` a DCP file (by its header)?
pub fn is_dcp(bytes: &[u8]) -> bool {
matches!(bytes.get(0..4), Some([b'I', b'I', b'R', b'C'] | [b'M', b'M', b'C', b'R']))
}
/// The largest `.dcp` file read or parsed (real profiles are kilobytes to a few MB).
pub const MAX_DCP_BYTES: u64 = 64 << 20;
/// Read the `.dcp` file at `path`, reading at most [`MAX_DCP_BYTES`] + 1 bytes: a larger file is refused
/// before it is loaded into memory.
pub fn read_dcp(path: &std::path::Path) -> std::io::Result<Vec<u8>> {
use std::io::Read;
let mut bytes = Vec::new();
std::fs::File::open(path)?.take(MAX_DCP_BYTES.saturating_add(1)).read_to_end(&mut bytes)?;
if bytes.len() as u64 > MAX_DCP_BYTES {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "the camera profile is larger than 64 MiB"));
}
Ok(bytes)
}
/// Parse a `.dcp` file. Files above 64 MiB ([`MAX_DCP_BYTES`]) are refused.
pub fn parse_dcp(bytes: &[u8]) -> Result<DcpProfile, DcpError> {
if !is_dcp(bytes) {
return Err(DcpError::NotDcp);
}
if bytes.len() as u64 > MAX_DCP_BYTES {
return Err(DcpError::Damaged("larger than 64 MiB".into()));
}
// the TIFF reader only knows 42 / BigTIFF / raw magics: give it a header with 42 in place of RC
let mut data = bytes.to_vec();
let forty_two: [u8; 2] = if bytes.first() == Some(&b'I') { [42, 0] } else { [0, 42] };
if let Some(m) = data.get_mut(2..4) {
m.copy_from_slice(&forty_two);
}
let opts = ParseOptions { follow_children: false, max_ifds: 4, value_budget: Some(64 << 20), ..Default::default() };
let tiff = Tiff::parse_with(&data, &opts).map_err(|e| DcpError::Damaged(e.to_string()))?;
let ifd: &Ifd = tiff.ifds.first().ok_or_else(|| DcpError::Damaged("no IFD".into()))?;
let mut color = crate::dng::color_data(ifd, ifd, tiff.order);
// a profile carries no white balance or calibration of a particular shot
color.as_shot_neutral = None;
color.as_shot_white_xy = None;
color.analog_balance = None;
color.camera_calibration = [None, None];
color.baseline_sharpness = None;
color.profile.gain_table_map = None;
// only BaselineExposureOffset belongs to a profile (BaselineExposure is the camera's)
color.baseline_exposure =
ifd.f64s(t::BASELINE_EXPOSURE_OFFSET).and_then(|v| v.first().copied()).filter(|v| v.is_finite()).unwrap_or(0.0).clamp(-4.0, 4.0);
// a single-illuminant profile names one illuminant; its second matrix slot stays empty
if color.color_matrix[0].is_none() {
// the first slot is the one every profile must fill (DNG: ColorMatrix1 is required)
return Err(DcpError::NoMatrix);
}
let text = |tag: u16| ifd.string(tag).map(|s| s.trim_matches(char::from(0)).trim().to_string()).unwrap_or_default();
Ok(DcpProfile { name: text(t::PROFILE_NAME), camera_model: text(t::UNIQUE_CAMERA_MODEL), color })
}
/// Write `p` as a `.dcp` file (little-endian): its name, camera, illuminants, matrices, hue/saturation
/// map, look table, tone curve and baseline exposure offset.
pub fn write_dcp(p: &DcpProfile) -> Result<Vec<u8>, DcpError> {
use craft_tiff::writer::srational;
use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value};
let c = &p.color;
// ColorMatrix1 must remain usable after TIFF rational rounding, or our reader rejects it.
let first = c.color_matrix[0].as_ref().ok_or(DcpError::NoMatrix)?;
let serialized = first
.iter()
.flatten()
.map(|v| {
let (n, d) = srational(*v);
f64::from(n) / f64::from(d)
})
.collect();
if first.iter().flatten().any(|v| !v.is_finite()) || crate::dng::mat3(Some(serialized)).is_none() {
return Err(DcpError::NoMatrix);
}
let mut ifd = IfdBuilder::new();
ifd.set(t::UNIQUE_CAMERA_MODEL, Value::Ascii(p.camera_model.clone()));
ifd.set(t::PROFILE_NAME, Value::Ascii(p.name.clone()));
let mat = |m: &crate::Matrix3| Value::SRational(m.iter().flatten().map(|v| srational(*v)).collect());
for i in 0..2 {
if let Some(m) = &c.color_matrix[i] {
ifd.set([t::CALIBRATION_ILLUMINANT_1, t::CALIBRATION_ILLUMINANT_2][i], Value::Short(vec![c.illuminant[i]]));
ifd.set([t::COLOR_MATRIX_1, t::COLOR_MATRIX_2][i], mat(m));
}
if let Some(m) = &c.forward_matrix[i] {
ifd.set([t::FORWARD_MATRIX_1, t::FORWARD_MATRIX_2][i], mat(m));
}
}
let flat = |table: &crate::profile::HsvTable| Value::Float(table.data.iter().flatten().copied().collect());
let dims =
|table: &crate::profile::HsvTable| Value::Long(vec![table.hue_divisions as u32, table.sat_divisions as u32, table.val_divisions as u32]);
if let Some(a) = c.profile.hue_sat_map[0].as_ref() {
ifd.set(t::PROFILE_HUE_SAT_MAP_DIMS, dims(a));
ifd.set(t::PROFILE_HUE_SAT_MAP_DATA_1, flat(a));
if a.srgb_value {
ifd.set(t::PROFILE_HUE_SAT_MAP_ENCODING, Value::Long(vec![1]));
}
if let Some(b) = c.profile.hue_sat_map[1]
.as_ref()
.filter(|b| (b.hue_divisions, b.sat_divisions, b.val_divisions) == (a.hue_divisions, a.sat_divisions, a.val_divisions))
{
ifd.set(t::PROFILE_HUE_SAT_MAP_DATA_2, flat(b));
}
}
if let Some(l) = &c.profile.look_table {
ifd.set(t::PROFILE_LOOK_TABLE_DIMS, dims(l));
ifd.set(t::PROFILE_LOOK_TABLE_DATA, flat(l));
if l.srgb_value {
ifd.set(t::PROFILE_LOOK_TABLE_ENCODING, Value::Long(vec![1]));
}
}
if let Some(tc) = &c.profile.tone_curve {
ifd.set(t::PROFILE_TONE_CURVE, Value::Float(tc.points.iter().flatten().copied().collect()));
}
if c.baseline_exposure != 0.0 {
ifd.set(t::BASELINE_EXPOSURE_OFFSET, Value::SRational(vec![srational(c.baseline_exposure)]));
}
let mut out = TiffWriter::new(ByteOrder::Little, false).write(&[ifd]).map_err(|e| DcpError::Damaged(e.to_string()))?;
// the DCP magic in place of 42
if let Some(m) = out.get_mut(2..4) {
m.copy_from_slice(&DCP_MAGIC.to_le_bytes());
}
Ok(out)
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
#[test]
fn writing_requires_the_first_color_matrix() {
let mut p = DcpProfile { name: "test".into(), camera_model: "test".into(), color: ColorData::default() };
assert_eq!(write_dcp(&p), Err(DcpError::NoMatrix));
let matrix = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
p.color.color_matrix[1] = Some(matrix);
assert_eq!(write_dcp(&p), Err(DcpError::NoMatrix));
p.color.color_matrix[0] = Some(matrix);
let back = parse_dcp(&write_dcp(&p).unwrap()).unwrap();
assert_eq!(back.color.color_matrix, p.color.color_matrix);
}
#[test]
fn writing_rejects_unusable_or_rounding_singular_first_matrices() {
let identity = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
for matrix in [
[[0.0; 3]; 3],
[[1.0, 1.0, 0.0], [1.0, 1.0 + 1e-8, 0.0], [0.0, 0.0, 1.0]],
[[f64::NAN, 0.0, 0.0], identity[1], identity[2]],
[[f64::INFINITY, 0.0, 0.0], identity[1], identity[2]],
] {
let mut p = DcpProfile { name: "test".into(), camera_model: "test".into(), color: ColorData::default() };
p.color.color_matrix[0] = Some(matrix);
assert_eq!(write_dcp(&p), Err(DcpError::NoMatrix));
}
}
#[test]
fn reading_a_profile_stops_past_the_size_limit() {
let dir = std::env::temp_dir().join(format!("lc-read-dcp-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let (fits, big) = (dir.join("fits.dcp"), dir.join("big.dcp"));
std::fs::File::create(&fits).unwrap().set_len(MAX_DCP_BYTES).unwrap();
std::fs::File::create(&big).unwrap().set_len(MAX_DCP_BYTES + 1).unwrap();
assert_eq!(read_dcp(&fits).unwrap().len() as u64, MAX_DCP_BYTES);
assert_eq!(read_dcp(&big).unwrap_err().kind(), std::io::ErrorKind::InvalidData);
let _ = std::fs::remove_dir_all(&dir);
}
/// A minimal single-illuminant profile (our own numbers) built in memory: no profile file is
/// committed to the repository.
pub(crate) fn synthetic_dcp(little: bool, with_hue_sat: bool) -> Vec<u8> {
// entries: (tag, type, count, payload bytes)
let mut entries: Vec<(u16, u16, u32, Vec<u8>)> = Vec::new();
let u16b = |v: u16| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() };
let u32b = |v: u32| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() };
let srat = |v: f64| {
let (n, d) = ((v * 10000.0).round() as i32, 10000i32);
let mut b = if little { n.to_le_bytes().to_vec() } else { n.to_be_bytes().to_vec() };
b.extend(if little { d.to_le_bytes() } else { d.to_be_bytes() });
b
};
let ascii = |s: &str| {
let mut b = s.as_bytes().to_vec();
b.push(0);
b
};
entries.push((t::UNIQUE_CAMERA_MODEL, 2, 13, ascii("Test Camera1")));
let m = [0.9, -0.3, -0.05, -0.4, 1.2, 0.2, -0.05, 0.15, 0.7];
entries.push((t::COLOR_MATRIX_1, 10, 9, m.iter().flat_map(|v| srat(*v)).collect()));
entries.push((t::CALIBRATION_ILLUMINANT_1, 3, 1, [u16b(21), vec![0, 0]].concat()));
entries.push((t::PROFILE_NAME, 2, 9, ascii("My Look")));
if with_hue_sat {
// 2 hues × 2 sats × 1 value: hue shift, sat scale, value scale per entry (FLOAT)
entries.push((t::PROFILE_HUE_SAT_MAP_DIMS, 4, 3, [u32b(2), u32b(2), u32b(1)].concat()));
let f = |v: f32| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() };
entries.push((
t::PROFILE_HUE_SAT_MAP_DATA_1,
11,
12,
[f(0.0), f(1.0), f(1.0), f(5.0), f(1.1), f(1.0), f(0.0), f(1.0), f(1.0), f(-5.0), f(0.9), f(1.0)].concat(),
));
}
entries.sort_by_key(|e| e.0);
let mut out = if little { b"IIRC".to_vec() } else { b"MMCR".to_vec() };
out.extend(u32b(8));
let n = entries.len();
let mut data_off = 8 + 2 + n * 12 + 4;
let mut ifd = u16b(n as u16);
let mut tail = Vec::new();
for (tag, ty, count, payload) in &entries {
ifd.extend(u16b(*tag));
ifd.extend(u16b(*ty));
ifd.extend(u32b(*count));
if payload.len() <= 4 {
let mut p = payload.clone();
p.resize(4, 0);
ifd.extend(p);
} else {
ifd.extend(u32b(data_off as u32));
tail.extend(payload);
data_off += payload.len();
if data_off % 2 == 1 {
tail.push(0);
data_off += 1;
}
}
}
ifd.extend(u32b(0));
out.extend(ifd);
out.extend(tail);
out
}
#[test]
fn reads_a_single_illuminant_profile_in_both_byte_orders() {
for little in [true, false] {
let p = parse_dcp(&synthetic_dcp(little, true)).unwrap();
assert_eq!(p.name, "My Look");
assert_eq!(p.camera_model, "Test Camera1");
assert_eq!(p.color.illuminant, [21, 0]);
let m = p.color.color_matrix[0].unwrap();
assert!((m[1][1] - 1.2).abs() < 1e-4, "{m:?}");
assert!(p.color.color_matrix[1].is_none());
assert!(p.color.profile.hue_sat_map[0].is_some());
assert!(p.color.as_shot_neutral.is_none());
}
assert!(parse_dcp(&synthetic_dcp(true, false)).unwrap().color.profile.hue_sat_map[0].is_none());
}
#[test]
fn rejects_other_files_and_damage_without_panicking() {
assert_eq!(parse_dcp(b"II*\0\x08\0\0\0"), Err(DcpError::NotDcp));
assert_eq!(parse_dcp(b""), Err(DcpError::NotDcp));
let good = synthetic_dcp(true, true);
for cut in [4, 8, 10, 30, good.len() / 2, good.len() - 1] {
let _ = parse_dcp(&good[..cut]);
}
let mut bad = good.clone();
bad[4..8].copy_from_slice(&u32::MAX.to_le_bytes());
assert!(parse_dcp(&bad).is_err());
}
/// The hue/saturation map's header is the user's file too: dimensions far past the data, an entry count
/// claiming 4 billion values, or a data offset past the end drop the map (the matrix still reads) instead of
/// allocating for the claim or reading past the file.
#[test]
fn a_hostile_hue_sat_map_header_drops_the_map() {
let good = synthetic_dcp(true, true);
let at = |needle: &[u8]| good.windows(needle.len()).position(|w| w == needle).unwrap();
let le = |v: &[u32]| v.iter().flat_map(|x| x.to_le_bytes()).collect::<Vec<u8>>();
let dims = at(&le(&[2, 2, 1]));
// the map data's directory entry: tag, FLOAT, count 12, offset
let entry = at(&[t::PROFILE_HUE_SAT_MAP_DATA_1.to_le_bytes(), 11u16.to_le_bytes()].concat());
let patched = |at: usize, bytes: &[u8]| {
let mut b = good.clone();
b[at..at + bytes.len()].copy_from_slice(bytes);
b
};
for (what, bytes) in [
("4096³ dimensions", patched(dims, &le(&[4096, 4096, 4096]))),
("dimensions past u32 products", patched(dims, &le(&[u32::MAX, u32::MAX, u32::MAX]))),
("a zero dimension", patched(dims, &le(&[0, 2, 1]))),
("4 billion values", patched(entry + 4, &le(&[u32::MAX]))),
("data past the end", patched(entry + 8, &le(&[u32::MAX - 16]))),
] {
let p = parse_dcp(&bytes).unwrap_or_else(|e| panic!("{what}: {e:?}"));
assert!(p.color.profile.hue_sat_map[0].is_none(), "{what}: the map is dropped");
assert!(p.color.color_matrix[0].is_some(), "{what}: the matrix still reads");
}
}
#[test]
fn written_profiles_read_back() {
let p = parse_dcp(&synthetic_dcp(false, true)).unwrap();
let back = parse_dcp(&write_dcp(&p).unwrap()).unwrap();
assert_eq!(back.name, p.name);
assert_eq!(back.camera_model, p.camera_model);
assert_eq!(back.color.illuminant, p.color.illuminant);
assert_eq!(back.color.profile.hue_sat_map, p.color.profile.hue_sat_map);
let (a, b) = (back.color.color_matrix[0].unwrap(), p.color.color_matrix[0].unwrap());
assert!(a.iter().flatten().zip(b.iter().flatten()).all(|(x, y)| (x - y).abs() < 1e-6));
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod boundary_tests {
use super::*;
use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value};
fn profile_bytes(offset: f64, matrix: bool) -> Vec<u8> {
let mut ifd = IfdBuilder::new();
if matrix {
ifd.set(t::COLOR_MATRIX_1, Value::SRational(vec![(1, 1), (0, 1), (0, 1), (0, 1), (1, 1), (0, 1), (0, 1), (0, 1), (1, 1)]));
}
ifd.set(t::BASELINE_EXPOSURE_OFFSET, Value::Double(vec![offset]));
ifd.set(t::BASELINE_EXPOSURE, Value::Double(vec![10.0]));
ifd.set(t::AS_SHOT_NEUTRAL, Value::Double(vec![0.5, 1.0, 0.7]));
let mut bytes = TiffWriter::new(ByteOrder::Little, false).write(&[ifd]).unwrap();
bytes[2..4].copy_from_slice(&DCP_MAGIC.to_le_bytes());
bytes
}
#[test]
fn profile_requires_matrix_and_never_keeps_shot_exposure_or_white_balance() {
assert_eq!(parse_dcp(&profile_bytes(0.0, false)), Err(DcpError::NoMatrix));
for (offset, expected) in [(9.0, 4.0), (-9.0, -4.0), (f64::INFINITY, 0.0), (f64::NAN, 0.0)] {
let profile = parse_dcp(&profile_bytes(offset, true)).unwrap();
assert_eq!(profile.color.baseline_exposure, expected);
assert_eq!(profile.color.as_shot_neutral, None);
assert_eq!(profile.color.camera_calibration, [None, None]);
assert_eq!(profile.color.profile.gain_table_map, None);
}
}
}
+1 -1
View File
@@ -91,7 +91,7 @@ fn permute<T: Copy + Default, F: Fn(usize, usize) -> usize>(v: &mut Vec<T>, w: u
}
/// A 3×3 matrix tag: nine finite values that can be inverted.
fn mat3(v: Option<Vec<f64>>) -> Option<Matrix3> {
pub(crate) fn mat3(v: Option<Vec<f64>>) -> Option<Matrix3> {
let v = v?;
if v.len() != 9 || v.iter().any(|x| !x.is_finite()) {
return None;
+3
View File
@@ -68,6 +68,9 @@ mod crw;
mod crx;
#[cfg(feature = "cr3")]
mod crx_wavelet;
/// DNG camera-profile file parsing and writing.
#[cfg(feature = "dng")]
pub mod dcp;
#[cfg(feature = "dng")]
mod dng;
#[cfg(feature = "dng")]
+11
View File
@@ -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
+8
View File
@@ -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.
+327
View File
@@ -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)
}
+28
View File
@@ -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 {}
+11
View File
@@ -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};
+281
View File
@@ -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)));
}
}
+536
View File
@@ -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());
}
}
+109
View File
@@ -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);
}
}
+73 -1
View File
@@ -181,6 +181,12 @@ pub struct CcittRawOutput {
/// Decodes CCITT Group 3 or Group 4 fax `data` with caller-specified `params` and `max_rows`,
/// returning packed MSB-first rows, rows decoded, input bytes consumed, and any decode error.
///
/// The rows are read as the parameters say, as PostScript's `CCITTFaxDecode` filter reads them:
/// on bytes exactly when `encoded_byte_align` is set, whatever codes the data holds (unlike
/// [`decode_ccitt_within`], which reads Group 3 rows with end-of-line codes by those codes). At
/// most `rows` rows are read when it is above 0; a caller that ends the rows at the end-of-block
/// code alone passes 0.
pub fn decode_ccitt_raw(data: &[u8], p: &CcittParams, max_rows: u32) -> Result<CcittRawOutput, CodecError> {
if p.columns <= 0 || p.columns > i64::from(MAX_CCITT_COLUMNS) {
return Err(CodecError::Corrupt { filter: "CCITTFaxDecode", detail: format!("CCITT columns {} outside 1..={MAX_CCITT_COLUMNS}", p.columns) });
@@ -192,7 +198,7 @@ pub fn decode_ccitt_raw(data: &[u8], p: &CcittParams, max_rows: u32) -> Result<C
rows,
end_of_block: p.end_of_block,
end_of_line: p.end_of_line,
rows_are_byte_aligned: p.encoded_byte_align && (p.k < 0 || !starts_with_end_of_line(data)),
rows_are_byte_aligned: p.encoded_byte_align,
encoding: match p.k {
..0 => hayro_ccitt::EncodingMode::Group4,
0 => hayro_ccitt::EncodingMode::Group3_1D,
@@ -579,6 +585,72 @@ mod tests {
}
}
/// `decode_ccitt_raw` reads rows on bytes exactly when `encoded_byte_align` says, as
/// PostScript's `CCITTFaxDecode` filter (and `hayro-ccitt` itself) reads them, in every layout
/// of Group 3 rows: with or without end-of-line codes, fill before each row or before each
/// code's end, an RTC after the rows, and room for more rows than they hold. The codes in the
/// data don't make rows the parameters put on bytes read unaligned (which read an aligned RTC
/// as one more row, #778).
#[test]
fn raw_group_3_rows_follow_the_parameters() {
let mut cases = 0;
for seed in 0..24u64 {
let (columns, rows) = (8 + (seed as u32 * 37) % 200, 2 + seed as u32 % 20);
let runs = text_like(seed, columns, rows);
for k in [0i64, 1, 2] {
for (codes, fill) in [(false, Fill::None), (false, Fill::RowStart), (true, Fill::None), (true, Fill::RowStart), (true, Fill::CodeEnd)]
{
let mut data = group_3(&runs, codes, k > 0, fill);
if codes {
// RTC: six end-of-line codes (with their 1 tag bits for `k` above 0), each on a byte.
let eol: [u8; 2] = if k > 0 { [0x00, 0x18] } else { [0x00, 0x10] };
(0..6).for_each(|_| data.extend_from_slice(&eol));
}
for (aligned, end_of_line, end_of_block) in [(true, true, true), (true, false, true), (true, true, false), (false, true, true)] {
let p = CcittParams {
k,
columns: i64::from(columns),
end_of_line,
encoded_byte_align: aligned,
end_of_block,
..CcittParams::default()
};
let max_rows = rows + 8;
let raw = decode_ccitt_raw(&data, &p, max_rows).unwrap();
let settings = hayro_ccitt::DecodeSettings {
columns,
rows: max_rows,
end_of_block,
end_of_line,
rows_are_byte_aligned: aligned,
encoding: if k > 0 { hayro_ccitt::EncodingMode::Group3_2D { k: k as u32 } } else { hayro_ccitt::EncodingMode::Group3_1D },
invert_black: false,
};
let mut filter = Rows::new(columns as usize, max_rows as usize, false);
let used = hayro_ccitt::decode(&data, &mut filter, &mut hayro_ccitt::DecoderContext::new(settings));
let case = format!(
"seed {seed}, {columns} × {rows}, k {k}, codes {codes}, fill {fill:?}, aligned {aligned}, eol {end_of_line}, eob {end_of_block}"
);
assert_eq!((&raw.data, raw.rows_decoded as usize), (&filter.out, filter.done), "{case}");
if let Ok(used) = used {
assert_eq!((raw.bytes_consumed, raw.error), (used, None), "{case}");
}
cases += 1;
}
}
}
}
assert_eq!(cases, 24 * 3 * 5 * 4);
// Rows on bytes with their codes, as `fax`'s writers lay them out, read whole: the rows
// and nothing after the RTC.
let runs = text_like(7, 74, 4);
let mut data = group_3(&runs, true, false, Fill::RowStart);
(0..6).for_each(|_| data.extend_from_slice(&[0x00, 0x10]));
let p = CcittParams { k: 0, columns: 74, end_of_line: true, encoded_byte_align: true, ..CcittParams::default() };
let raw = decode_ccitt_raw(&data, &p, 9).unwrap();
assert_eq!((raw.data, raw.rows_decoded), (pack(&runs, 74), 4));
}
proptest::proptest! {
/// Any bitmap, of noise or of runs, decodes to its own bits, through an independent decoder
/// and through `decode_ccitt`.
+137 -11
View File
@@ -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;
+425
View File
@@ -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}");
}
}
}
+66
View File
@@ -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
View File
@@ -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]);
}
+14 -4
View File
@@ -34,26 +34,36 @@ impl Imported {
/// above [`crate::rate::MAX_RATE`] per second and start timecodes beyond [`MAX_START_TIMECODE`]
/// frames. Every importer ends with this pass, so no format can let such a number through.
pub fn bound(&mut self, report: &mut Report) {
self.bound_with_report_limit(report, None);
}
/// Apply numeric bounds with an optional limit on distinct warning messages.
pub fn bound_with_report_limit(&mut self, report: &mut Report, limit: Option<usize>) {
for item in self.project.items.values_mut() {
let name = item.name.clone();
if let Some(seq) = item.as_sequence_mut() {
if !plausible_rate(seq.settings.frame_rate) {
report.warn(format!("sequence \"{name}\" has an implausible frame rate ({}); the default was used", seq.settings.frame_rate));
report.warn_limited(
limit,
format!("sequence \"{name}\" has an implausible frame rate ({}); the default was used", seq.settings.frame_rate),
);
seq.settings.frame_rate = FrameRate::default();
}
if seq.start_timecode.unsigned_abs() > MAX_START_TIMECODE {
report.warn(format!("sequence \"{name}\" has an out-of-range start timecode ({} frames); it starts at 0", seq.start_timecode));
report.warn_limited(
limit,
format!("sequence \"{name}\" has an out-of-range start timecode ({} frames); it starts at 0", seq.start_timecode),
);
seq.start_timecode = 0;
}
} else if let Some(media) = item.as_media_mut() {
if let Some(v) = media.info.video.as_mut()
&& !plausible_rate(v.frame_rate)
{
report.warn(format!("media \"{name}\" has an implausible frame rate ({}); the default was used", v.frame_rate));
report.warn_limited(limit, format!("media \"{name}\" has an implausible frame rate ({}); the default was used", v.frame_rate));
v.frame_rate = FrameRate::default();
}
if media.info.start_timecode.is_some_and(|tc| tc.unsigned_abs() > MAX_START_TIMECODE) {
report.warn(format!("media \"{name}\" has an out-of-range start timecode; it was ignored"));
report.warn_limited(limit, format!("media \"{name}\" has an out-of-range start timecode; it was ignored"));
media.info.start_timecode = None;
}
}
+75 -32
View File
@@ -131,7 +131,7 @@ fn color_label(s: &str) -> Label {
// ---------------------------------------------------------------------------------------------
/// `fields` with the keys of `ext` (a JSON object) added after them; a key of ours wins.
fn with_ext(mut fields: Map<String, Value>, ext: &Ext, report: &mut Report) -> Value {
fn with_ext(mut fields: Map<String, Value>, ext: &Ext, report: &mut Report, limit: Option<usize>) -> Value {
if let Some(text) = ext {
match serde_json::from_str::<Value>(text) {
Ok(Value::Object(extra)) => {
@@ -139,7 +139,7 @@ fn with_ext(mut fields: Map<String, Value>, ext: &Ext, report: &mut Report) -> V
fields.entry(k).or_insert(v);
}
}
_ => report.warn("extension data that is not a JSON object was not written to OTIO"),
_ => report.warn_limited(limit, "extension data that is not a JSON object was not written to OTIO"),
}
}
Value::Object(fields)
@@ -355,7 +355,7 @@ fn value_of(v: &Value) -> ParamValue {
const EFFECT_KEYS: [&str; 3] = ["effect", "enabled", "params"];
fn effects_json(effects: &[EffectInstance], report: &mut Report) -> Value {
fn effects_json(effects: &[EffectInstance], report: &mut Report, limit: Option<usize>) -> Value {
Value::Array(
effects
.iter()
@@ -375,7 +375,7 @@ fn effects_json(effects: &[EffectInstance], report: &mut Report) -> Value {
(k.clone(), json!({"value": value_json(&p.value), "keyframes": keys}))
})
.collect();
with_ext(obj(json!({"effect": e.effect, "enabled": e.enabled, "params": params})), &e.ext, report)
with_ext(obj(json!({"effect": e.effect, "enabled": e.enabled, "params": params})), &e.ext, report, limit)
})
.collect(),
)
@@ -475,6 +475,7 @@ struct Exp<'a, 'r> {
p: &'a Project,
opts: &'a ExportOptions<'a>,
report: &'r mut Report,
report_limit: Option<usize>,
stack: Vec<ItemId>,
/// Clips written inside nested sequences so far, and the limit for it (see [`MAX_NESTED_ITEMS`]).
nested_items: usize,
@@ -483,9 +484,19 @@ struct Exp<'a, 'r> {
/// Write `sequence` (and the media and nested sequences it uses) as an OTIO `Timeline`.
pub fn export(p: &Project, seq_id: ItemId, opts: &ExportOptions, report: &mut Report) -> Result<String> {
export_with_report_limit(p, seq_id, opts, report, None)
}
/// Write OTIO with an optional limit on distinct warnings.
pub fn export_with_report_limit(
p: &Project,
seq_id: ItemId,
opts: &ExportOptions,
report: &mut Report,
report_limit: Option<usize>,
) -> Result<String> {
let seq = p.sequence(seq_id).ok_or(Error::NoSequence(seq_id))?;
let rate = seq.settings.frame_rate;
let mut x = Exp { p, opts, report, stack: vec![seq_id], nested_items: 0, nested_limit: MAX_NESTED_ITEMS };
let mut x = Exp { p, opts, report, report_limit, stack: vec![seq_id], nested_items: 0, nested_limit: MAX_NESTED_ITEMS };
let name = opts.name.map(str::to_string).unwrap_or_else(|| p.item(seq_id).map(|i| i.name.clone()).unwrap_or_default());
let stack = x.stack_json(seq_id, &name, None);
let mut meta = Map::new();
@@ -512,13 +523,13 @@ impl Exp<'_, '_> {
fn seq_meta(&mut self, id: ItemId) -> Value {
let Some((it, s)) = self.p.item(id).and_then(|it| Some((it, it.as_sequence()?))) else { return json!({}) };
let m = obj(json!({"item": id.0, "label": it.label.name(), "settings": settings_json(&s.settings), "start_timecode": s.start_timecode}));
with_ext(m, &it.ext, self.report)
with_ext(m, &it.ext, self.report, self.report_limit)
}
/// The `Stack` for a sequence; `range` = (source in, duration) when nested in a track.
fn stack_json(&mut self, id: ItemId, name: &str, range: Option<(Tick, Tick, FrameRate)>) -> Value {
let Some(seq) = self.p.sequence(id) else {
self.report.warn("missing nested sequence skipped");
self.report.warn_limited(self.report_limit, "missing nested sequence skipped");
return json!({"OTIO_SCHEMA": "Stack.1", "name": name, "children": [], "effects": [], "markers": [], "enabled": true, "metadata": {}});
};
let rate = seq.settings.frame_rate;
@@ -564,7 +575,7 @@ impl Exp<'_, '_> {
}
pos = it.end();
}
let meta = with_ext(obj(json!({"locked": t.locked, "channels": channels_name(t.channels)})), &t.ext, self.report);
let meta = with_ext(obj(json!({"locked": t.locked, "channels": channels_name(t.channels)})), &t.ext, self.report, self.report_limit);
json!({
"OTIO_SCHEMA": "Track.1",
"name": t.name,
@@ -580,8 +591,12 @@ impl Exp<'_, '_> {
fn transition_json(&mut self, x: &Transition, cut: Tick, rate: FrameRate) -> Value {
let dissolve = matches!(x.effect.effect.as_str(), "cross_dissolve" | "constant_power" | "constant_gain" | "film_dissolve");
let effect = effects_json(std::slice::from_ref(&x.effect), self.report).as_array().and_then(|a| a.first().cloned()).unwrap_or(Value::Null);
let meta = with_ext(obj(json!({"effect": effect, "align": align_name(x.align), "reverse": x.reverse})), &x.ext, self.report);
let effect = effects_json(std::slice::from_ref(&x.effect), self.report, self.report_limit)
.as_array()
.and_then(|a| a.first().cloned())
.unwrap_or(Value::Null);
let meta =
with_ext(obj(json!({"effect": effect, "align": align_name(x.align), "reverse": x.reverse})), &x.ext, self.report, self.report_limit);
json!({
"OTIO_SCHEMA": "Transition.1",
"name": transition_name(self.opts.catalog, &x.effect),
@@ -604,7 +619,7 @@ impl Exp<'_, '_> {
fields.insert("generator".into(), generator_json(g));
}
let ext = it.and_then(|i| i.ext.clone());
let meta = with_ext(fields, &ext, self.report);
let meta = with_ext(fields, &ext, self.report, self.report_limit);
match &m.media {
MediaRef::Generator(g) => json!({
"OTIO_SCHEMA": "GeneratorReference.1",
@@ -626,11 +641,11 @@ impl Exp<'_, '_> {
fn clip_meta(&mut self, it: &TrackItem) -> Value {
let fields = obj(json!({
"label": it.label.name(), "link": it.link, "group": it.group, "effects": effects_json(&it.effects, self.report),
"label": it.label.name(), "link": it.link, "group": it.group, "effects": effects_json(&it.effects, self.report, self.report_limit),
"gain_db": it.gain_db, "scale_to_frame": it.scale_to_frame, "source_in_ticks": it.source_in.0,
"duration_ticks": it.duration.0,
}));
with_ext(fields, &it.ext, self.report)
with_ext(fields, &it.ext, self.report, self.report_limit)
}
fn item_json(&mut self, it: &TrackItem, kind: TrackKind, rate: FrameRate) -> Value {
@@ -650,17 +665,20 @@ impl Exp<'_, '_> {
let _ = kind;
if matches!(self.p.item(base).map(|i| &i.kind), Some(ItemKind::Sequence(_))) {
if self.stack.contains(&base) {
self.report.warn("recursive nested sequence skipped");
self.report.warn_limited(self.report_limit, "recursive nested sequence skipped");
return json!({"OTIO_SCHEMA": "Gap.1", "name": "", "source_range": tr(Tick::ZERO, it.duration, rate), "effects": [], "markers": [], "enabled": true, "metadata": {}});
}
if self.nested_items >= self.nested_limit {
// once: later nested sequences hit this too, and the message would repeat
if self.nested_items < usize::MAX {
self.nested_items = usize::MAX;
self.report.warn(format!(
"more than {} clips written inside nested sequences: the rest are gaps (OTIO repeats a nested sequence for every use)",
self.nested_limit
));
self.report.warn_limited(
self.report_limit,
format!(
"more than {} clips written inside nested sequences: the rest are gaps (OTIO repeats a nested sequence for every use)",
self.nested_limit
),
);
}
return json!({"OTIO_SCHEMA": "Gap.1", "name": "", "source_range": tr(Tick::ZERO, it.duration, rate), "effects": [], "markers": [], "enabled": true, "metadata": {}});
}
@@ -678,9 +696,10 @@ impl Exp<'_, '_> {
}
let _ = generator_of(self.p, base);
if let Some(clip) = crate::build::uncarried_clip(self.p, it, rate) {
self.report.warn(format!(
"{clip} has no OTIO equivalent: it is written as a clip with a missing media reference and is read back as offline media"
));
self.report.warn_limited(
self.report_limit,
format!("{clip} has no OTIO equivalent: it is written as a clip with a missing media reference and is read back as offline media"),
);
}
let mref = self.media_ref(base, rate);
// OTIO source times are in the reference's time (which starts at its available_range start).
@@ -703,7 +722,7 @@ impl Exp<'_, '_> {
let fields = obj(
json!({"kind": marker_kind_name(m.kind), "url": m.url, "label": m.color.name(), "author": m.author, "start_ticks": m.start.0, "duration_ticks": m.duration.0}),
);
let meta = with_ext(fields, &m.ext, self.report);
let meta = with_ext(fields, &m.ext, self.report, self.report_limit);
json!({
"OTIO_SCHEMA": "Marker.2",
"name": m.name,
@@ -734,12 +753,17 @@ struct Imp<'a, 'c, 'r> {
b: Builder<'c>,
opts: &'a ImportOptions<'a>,
report: &'r mut Report,
report_limit: Option<usize>,
links: HashMap<u64, u64>,
groups: HashMap<u64, u64>,
}
/// Read an OTIO document: a `Timeline`, a `SerializableCollection` of them, a `Stack` or a bare `Track`.
pub fn import(text: &str, opts: &ImportOptions, report: &mut Report) -> Result<Imported> {
import_with_report_limit(text, opts, report, None)
}
/// Read OTIO with an optional limit on distinct warnings.
pub fn import_with_report_limit(text: &str, opts: &ImportOptions, report: &mut Report, report_limit: Option<usize>) -> Result<Imported> {
let v: Value = serde_json::from_str(text).map_err(|e| Error::parse(Format::Otio, e.to_string()))?;
let mut timelines = Vec::new();
collect_timelines(&v, &mut timelines);
@@ -747,11 +771,11 @@ pub fn import(text: &str, opts: &ImportOptions, report: &mut Report) -> Result<I
return Err(Error::parse(Format::Otio, format!("no Timeline in a {} document", schema(&v))));
}
if has_absurd_rate(&v) {
report.warn(format!(
report.warn_limited(report_limit, format!(
"an OTIO rate above {MAX_RATE} per second is not a frame rate; the default 24 fps was used (this includes audio sample rates above it, such as DSD)"
));
}
let mut imp = Imp { b: Builder::new(&name_of(&v), opts.catalog), opts, report, links: HashMap::new(), groups: HashMap::new() };
let mut imp = Imp { b: Builder::new(&name_of(&v), opts.catalog), opts, report, report_limit, links: HashMap::new(), groups: HashMap::new() };
if imp.b.p.name.is_empty() {
imp.b.p.name = "Imported OTIO".into();
}
@@ -760,7 +784,7 @@ pub fn import(text: &str, opts: &ImportOptions, report: &mut Report) -> Result<I
imp.b.top.push(id);
}
let mut imported = imp.b.finish();
imported.bound(report);
imported.bound_with_report_limit(report, report_limit);
Ok(imported)
}
@@ -843,7 +867,7 @@ impl Imp<'_, '_, '_> {
let kind = if t.get("kind").and_then(Value::as_str) == Some("Audio") { TrackKind::Audio } else { TrackKind::Video };
self.track(&mut seq, t, kind);
}
other => self.report.warn(format!("OTIO {other} directly in a stack is not supported")),
other => self.report.warn_limited(self.report_limit, format!("OTIO {other} directly in a stack is not supported")),
}
}
self.b.ensure_tracks(&mut seq, TrackKind::Video, 1);
@@ -905,7 +929,7 @@ impl Imp<'_, '_, '_> {
}
}
other => {
self.report.warn(format!("OTIO {other} items are not supported and were skipped"));
self.report.warn_limited(self.report_limit, format!("OTIO {other} items are not supported and were skipped"));
}
}
}
@@ -925,7 +949,17 @@ impl Imp<'_, '_, '_> {
None => {
let n = name_of(c);
let ty = c.get("transition_type").and_then(Value::as_str).unwrap_or("SMPTE_Dissolve");
transition_effect(self.b.catalog, if n.is_empty() { ty } else { &n }, kind == TrackKind::Audio, self.report)
{
let mut temporary = Report::default();
let effect =
transition_effect(self.b.catalog, if n.is_empty() { ty } else { &n }, kind == TrackKind::Audio, &mut temporary);
for e in temporary.entries {
for _ in 0..e.count {
self.report.warn_limited(self.report_limit, &e.message);
}
}
effect
}
}
};
let align = align_of(meta.and_then(|m| m.get("align"))).unwrap_or(if inn == Tick::ZERO {
@@ -972,7 +1006,7 @@ impl Imp<'_, '_, '_> {
_ => {
if meta.and_then(|m| m.get("effects")).is_none() {
let n = e.get("effect_name").and_then(Value::as_str).unwrap_or("effect");
self.report.warn(format!("OTIO effect \"{n}\" is not supported"));
self.report.warn_limited(self.report_limit, format!("OTIO effect \"{n}\" is not supported"));
}
}
}
@@ -1107,7 +1141,8 @@ impl Imp<'_, '_, '_> {
other => match self.b.catalog.generator_by_name(other) {
Some(g) => g,
None => {
self.report.warn(format!("OTIO generator \"{other}\" is not supported; imported as Black Video"));
self.report
.warn_limited(self.report_limit, format!("OTIO generator \"{other}\" is not supported; imported as Black Video"));
Generator::BlackVideo
}
},
@@ -1127,7 +1162,7 @@ impl Imp<'_, '_, '_> {
}
_ => {
if schema_name(r) != "MissingReference" && !r.is_null() {
self.report.warn(format!("OTIO media reference {} is not supported; imported offline", schema(r)));
self.report.warn_limited(self.report_limit, format!("OTIO media reference {} is not supported; imported offline", schema(r)));
}
let key = format!("missing:{name}");
let existed = self.b.find_media(&key);
@@ -1279,7 +1314,15 @@ mod tests {
let opts = xopts();
let write = |limit: usize| {
let mut report = Report::default();
let mut x = Exp { p: &imported.project, opts: &opts, report: &mut report, stack: vec![seq], nested_items: 0, nested_limit: limit };
let mut x = Exp {
p: &imported.project,
opts: &opts,
report: &mut report,
report_limit: None,
stack: vec![seq],
nested_items: 0,
nested_limit: limit,
};
let stacks = x.stack_json(seq, "t", None).to_string().matches("\"Stack.1\"").count();
(stacks, report)
};
+14
View File
@@ -35,6 +35,20 @@ impl Report {
self.entries.push(ReportEntry { level, message, count: 1 });
}
}
/// Add a warning while bounding distinct diagnostics. `limit` counts every distinct entry already
/// in the report, informational ones included, exactly like FilmCraft's `MAX_REPORT_ENTRIES`
/// budget this replaces; past it a new message is counted under "further diagnostics omitted".
/// Existing messages continue to be counted.
pub fn warn_limited(&mut self, limit: Option<usize>, message: impl Into<String>) {
let mut message = message.into();
if let Some(limit) = limit
&& self.entries.len() >= limit
&& !self.entries.iter().any(|e| e.level == Level::Warning && e.message == message)
{
message = "further diagnostics omitted".into();
}
self.push(Level::Warning, message);
}
pub fn warn(&mut self, message: impl Into<String>) {
self.push(Level::Warning, message);
}
+14
View File
@@ -570,3 +570,17 @@ fn relative_target_urls_are_percent_decoded() {
let clip = &p.sequence(imp.sequences[0]).unwrap().video_tracks[0].items[0];
assert_eq!(media_key(p, clip.item), "/proj/media/Clip A.mov");
}
#[test]
fn optional_report_limit_bounds_distinct_otio_diagnostics_without_changing_default() {
let children: Vec<_> = (0..10).map(|i| serde_json::json!({"OTIO_SCHEMA": format!("Unsupported{i}.1")})).collect();
let doc = serde_json::json!({"OTIO_SCHEMA":"Timeline.1", "tracks":{"OTIO_SCHEMA":"Stack.1", "children":[{"OTIO_SCHEMA":"Track.1", "kind":"Video", "children":children}]}}).to_string();
let opts = iopts(None);
let mut bounded = craft_timeline::Report::default();
assert!(craft_timeline::otio::import_with_report_limit(&doc, &opts, &mut bounded, Some(2)).is_ok());
assert_eq!(bounded.entries.len(), 3);
assert!(bounded.mentions("further diagnostics omitted"));
let mut unlimited = craft_timeline::Report::default();
assert!(craft_timeline::otio::import(&doc, &opts, &mut unlimited).is_ok());
assert_eq!(unlimited.entries.len(), 10);
}
+1 -1
View File
@@ -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 }
+3 -3
View File
@@ -46,7 +46,7 @@ assert_eq!((rows, sheet.map(|s| s.children.len())), (3, Ok(0)));
| `max_input` | 256 MiB | `TooLarge` |
| `max_depth` | 256 levels (the root is 1) | `TooDeep` |
| `max_nodes` | 2,000,000 nodes kept in the tree: elements, text stretches, kept CDATA, instructions and comments | `TooManyNodes` |
| `max_attrs` | 1024 on one start tag, namespace declarations and malformed ones included; with canonical names, also the declarations in scope at once | `TooManyAttrs` |
| `max_attrs` | 1024 on one start tag, namespace declarations and malformed ones included; with canonical names or strict reads, also the declarations in scope at once | `TooManyAttrs` |
| `max_attrs_total` | 4,000,000 attributes kept in the tree, over all its elements | `TooManyAttrsTotal` |
A limit exceeded is an error, never a panic and never a truncated tree. `parse_streaming_partial`
@@ -73,9 +73,9 @@ A lenient read keeps what a damaged document had:
- reading stops when the root element ends: nothing after it is looked at;
- a malformed attribute is skipped; bytes that are not UTF-8 become U+FFFD.
A strict read makes each of those an error, and also a second root element and text outside the
A strict read rejects duplicate raw or expanded attribute names (while allowing undeclared prefixes in fragments). It makes each of those an error, and also a second root element and text outside the
root, in the pull reader as in the tree. Markup the tokenizer cannot read (`<a b='1`, an unterminated comment) is an error either
way. A repeated attribute is kept each time in both.
way. A lenient read keeps repeated attributes.
### Text
+53 -2
View File
@@ -1,6 +1,7 @@
//! Reading: the pull reader over `quick-xml`, the tree builder on top of it, and streaming.
use std::borrow::Cow;
use std::collections::BTreeSet;
use std::ops::Range;
use quick_xml::Reader;
@@ -12,6 +13,10 @@ use crate::write::is_xml_char;
/// The namespace the `xml` prefix is always bound to.
pub const XML_NS: &str = "http://www.w3.org/XML/1998/namespace";
/// [`Events::scope_ids`] id of the XML namespace, bound to `xml:` without a declaration.
const XML_NS_ID: usize = usize::MAX;
/// Expanded-name id of an attribute in no namespace (or with an undeclared prefix).
const NO_NS_ID: usize = usize::MAX - 1;
/// What the reader refuses to exceed.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -469,6 +474,10 @@ pub struct Events<'o, 'i> {
end_names: bool,
open: Vec<Open>,
scope: Vec<(String, String)>,
/// For each binding in `scope`, an id shared by every binding of an equal URI (the index of
/// the first such binding, or [`XML_NS_ID`]), with the URI's hash. Strict reads compare
/// expanded attribute names by this id instead of copying the URI for each attribute.
scope_ids: Vec<(u64, usize)>,
/// The end of an empty element, due after its start.
pending: bool,
/// Whether a root element has started.
@@ -500,7 +509,7 @@ impl<'o, 'i> Events<'o, 'i> {
cfg.trim_text_start = false;
cfg.trim_text_end = false;
cfg.expand_empty_elements = false;
Events { reader, opts, end_names, open: Vec::new(), scope: Vec::new(), pending: false, rooted: false, span: 0..0 }
Events { reader, opts, end_names, open: Vec::new(), scope: Vec::new(), scope_ids: Vec::new(), pending: false, rooted: false, span: 0..0 }
}
/// The bytes of the input the last event was read from. Both events of an empty element
@@ -614,6 +623,7 @@ impl<'o, 'i> Events<'o, 'i> {
fn end(&mut self) -> Event {
let (scope, name) = self.open.pop().map_or((0, String::new()), |o| (o.scope, o.name));
self.scope.truncate(scope);
self.scope_ids.truncate(scope);
Event::End { name }
}
@@ -669,6 +679,7 @@ impl<'o, 'i> Events<'o, 'i> {
if is_ns_decl(key) {
let prefix = self.decode(key.get(6..).unwrap_or_default())?.into_owned();
let uri = self.attr_value(&a.value)?;
self.scope_ids.push(self.uri_id(&uri));
self.scope.push((prefix, uri));
}
}
@@ -680,6 +691,20 @@ impl<'o, 'i> Events<'o, 'i> {
Ok(())
}
/// The id an expanded name uses for `uri`: one hash and at most one full comparison per
/// declaration, so a long URI costs its length once however many attributes use it.
fn uri_id(&self, uri: &str) -> (u64, usize) {
use std::hash::{Hash, Hasher};
let mut h = std::collections::hash_map::DefaultHasher::new();
uri.hash(&mut h);
let hash = h.finish();
if uri == XML_NS {
return (hash, XML_NS_ID);
}
let first = self.scope_ids.iter().zip(&self.scope).position(|((h, _), (_, u))| *h == hash && u == uri);
(hash, first.map_or(self.scope.len(), |i| self.scope_ids.get(i).map_or(i, |(_, id)| *id)))
}
/// In a strict read: is the reader before or after the root element?
fn outside_root(&self) -> bool {
self.opts.strict && self.open.is_empty()
@@ -699,7 +724,7 @@ impl<'o, 'i> Events<'o, 'i> {
_ => None,
};
let scope = self.scope.len();
if canonical.is_some() {
if canonical.is_some() || self.opts.strict {
self.declare(e)?;
}
let raw = e.name();
@@ -707,6 +732,8 @@ impl<'o, 'i> Events<'o, 'i> {
let mut out = Vec::new();
let mut attrs = e.attributes();
attrs.with_checks(false);
let mut raw_names = BTreeSet::new();
let mut expanded_names = BTreeSet::new();
for (seen, a) in attrs.enumerate() {
if seen >= limits.max_attrs {
return Err(Error::TooManyAttrs(limits.max_attrs));
@@ -718,6 +745,30 @@ impl<'o, 'i> Events<'o, 'i> {
};
let key = a.key.as_ref();
let decl = is_ns_decl(key);
if self.opts.strict {
let raw_key = self.decode(key)?;
if !raw_names.insert(raw_key.to_string()) {
return self.syntax(format!("duplicate attribute `{raw_key}`"));
}
if !decl {
// Default namespaces do not apply to attributes. Keep undeclared prefixes
// distinct so standalone fragments retain the reader's existing behaviour.
let expanded = match raw_key.split_once(':') {
Some(("xml", local)) => (XML_NS_ID, local),
Some((prefix, local)) => self
.scope
.iter()
.zip(&self.scope_ids)
.rev()
.find(|((p, _), _)| p == prefix)
.map_or((NO_NS_ID, raw_key.as_ref()), |(_, (_, id))| (*id, local)),
None => (NO_NS_ID, raw_key.as_ref()),
};
if !expanded_names.insert((expanded.0, expanded.1.to_string())) {
return self.syntax(format!("duplicate expanded attribute `{raw_key}`"));
}
}
}
if decl && !self.opts.ns_decls {
continue;
}
+43 -1
View File
@@ -117,7 +117,11 @@ fn an_attribute_flood_is_refused_at_the_limit() {
let opts = opts.with_limits(limits);
for (k, kind) in kinds.iter().enumerate() {
let _ = opts.parse(tag(16, kind).as_bytes());
assert_eq!(opts.parse(tag(17, kind).as_bytes()).err().map(|e| e.is_limit() || (opts.strict && k == 3)), Some(true), "kind {k}");
assert_eq!(
opts.parse(tag(17, kind).as_bytes()).err().map(|e| e.is_limit() || (opts.strict && (k == 1 || k == 3))),
Some(true),
"kind {k}"
);
assert!(opts.first_start(tag(17, kind).as_bytes(), "a").is_none());
}
assert_eq!(opts.parse(tag(17, kinds[0]).as_bytes()), Err(Error::TooManyAttrs(16)));
@@ -524,3 +528,41 @@ fn a_strict_pull_reader_refuses_what_is_outside_the_root() {
// white space, comments and instructions may stand outside the root
assert!(read(strict, b"<?xml version='1.0'?>\r\n <!-- c -->\t<a>x</a>\n<?pi?> ").is_ok());
}
#[test]
fn strict_attributes_are_unique_before_name_rewriting() {
use craft_xml::{Names, Options};
for names in [Names::AsWritten, Names::Local] {
let strict = Options { names, ..Options::default() }.strict();
for xml in [
"<r a='1' a='2'/>",
"<r xmlns:p='urn:x' xmlns:q='urn:x' p:a='1' q:a='2'/>",
"<r xml:a='1' xmlns:p='http://www.w3.org/XML/1998/namespace' p:a='2'/>",
"<r xmlns:p='urn:x'><c xmlns:q='urn:x' p:a='1' q:a='2'/></r>",
] {
assert!(strict.parse(xml.as_bytes()).is_err(), "{xml}");
let mut events = strict.events(xml.as_bytes());
assert!(
loop {
match events.next() {
Ok(Some(_)) => {}
Ok(None) => break false,
Err(_) => break true,
}
},
"{xml}"
);
}
for xml in [
"<r xmlns='urn:x' xmlns:p='urn:x' a='1' p:a='2'/>",
"<r p:a='1' q:a='2'/>",
"<r xmlns:p='urn:x'><c xmlns:p='urn:y' xmlns:q='urn:x' p:a='1' q:a='2'/></r>",
] {
assert!(strict.parse(xml.as_bytes()).is_ok(), "{xml}");
}
assert!(Options { names, ..Options::default() }.parse(b"<r a='1' a='2'/>").is_ok());
}
let strict = Options { limits: craft_xml::Limits { max_attrs: 2, ..craft_xml::Limits::DEFAULT }, ..Options::default() }.strict();
assert!(strict.parse(b"<r xmlns:a='a' xmlns:b='b'><c xmlns:c='c'/></r>").is_err());
assert!(strict.parse(b"<r a='1' b='2' c='3'/>").is_err());
}