diff --git a/crates/cms/src/lib.rs b/crates/cms/src/lib.rs index abd2b2604..446051762 100644 --- a/crates/cms/src/lib.rs +++ b/crates/cms/src/lib.rs @@ -29,6 +29,7 @@ pub mod lut3d; pub mod lutfile; pub mod math; pub mod named; +mod normalize; pub mod pipeline; pub mod profile; pub mod synth; @@ -43,6 +44,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; diff --git a/crates/cms/src/normalize.rs b/crates/cms/src/normalize.rs index 30f5c1801..2ce575d05 100644 --- a/crates/cms/src/normalize.rs +++ b/crates/cms/src/normalize.rs @@ -1,44 +1,142 @@ -//! Encoded ICC normalization extracted from LightCraft. -fn align_tags(b: &[u8]) -> Vec { - 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> = (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 -} +//! Bounded encoded ICC layout normalization, originating in LightCraft. -fn fixed_date(mut b: Vec) -> Vec { - if let Some(date) = b.get_mut(24..36) { +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) -> Vec { + if let Some(date) = bytes.get_mut(24..36) { date.fill(0); } - b + align(&bytes).unwrap_or(bytes) } +const MAX_BYTES: usize = 64 * 1024 * 1024; +const MAX_TAGS: usize = 4096; + +fn be(bytes: &[u8], at: usize) -> Option { + 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> { + 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().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)); + } +} diff --git a/crates/cms/src/normalize_legacy_tests.rs b/crates/cms/src/normalize_legacy_tests.rs new file mode 100644 index 000000000..dae5c2770 --- /dev/null +++ b/crates/cms/src/normalize_legacy_tests.rs @@ -0,0 +1,43 @@ +//! Encoded ICC normalization extracted from LightCraft. +pub(super) fn align_tags(b: &[u8]) -> Vec { + 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> = (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) -> Vec { + if let Some(date) = b.get_mut(24..36) { + date.fill(0); + } + b +}