From 05c75fe855c58718a49b2debd38c29f807443bbe Mon Sep 17 00:00:00 2001 From: berce <75228263+bercedev@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:31:58 +0300 Subject: [PATCH] fix(cms): read UTF-16 LUT files by their byte-order mark (#9129) A UTF-16 .look that Photoshop loads was read as UTF-8 and failed with "no in .look", in Color Lookup and in PSD import. Decode FF FE / FE FF as UTF-16 before picking the format. Fixes #9118 Co-authored-by: Brandon Thomas --- crates/cms/src/lutfile.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/cms/src/lutfile.rs b/crates/cms/src/lutfile.rs index 349bd792be..6a85f53908 100644 --- a/crates/cms/src/lutfile.rs +++ b/crates/cms/src/lutfile.rs @@ -90,7 +90,7 @@ impl LutFile { /// Parses a LUT, picking the format from `file_name`'s extension (falls back to sniffing). pub fn parse(file_name: &str, bytes: &[u8]) -> Result { let lower = file_name.to_ascii_lowercase(); - let text = String::from_utf8_lossy(bytes); + let text = decode_text(bytes); if lower.ends_with(".3dl") { parse_3dl(&text) } else if lower.ends_with(".look") || text.trim_start().starts_with('<') { @@ -100,6 +100,19 @@ pub fn parse(file_name: &str, bytes: &[u8]) -> Result { } } +/// UTF-16 with a byte-order mark (Photoshop loads such `.look` files, #9118), else UTF-8. +fn decode_text(bytes: &[u8]) -> std::borrow::Cow<'_, str> { + let utf16 = |rest: &[u8], from: fn([u8; 2]) -> u16| { + let units: Vec = rest.as_chunks::<2>().0.iter().map(|&c| from(c)).collect(); + String::from_utf16_lossy(&units).into() + }; + match bytes { + [0xFF, 0xFE, rest @ ..] => utf16(rest, u16::from_le_bytes), + [0xFE, 0xFF, rest @ ..] => utf16(rest, u16::from_be_bytes), + _ => String::from_utf8_lossy(bytes), + } +} + /// Adobe / Resolve `.cube`. 1D LUTs (`LUT_1D_SIZE`) are expanded to a 33³ table. pub fn parse_cube(text: &str) -> Result { let mut title = String::new(); @@ -496,6 +509,17 @@ DOMAIN_MAX 0.5 1 1 assert_eq!(parse("a.look", xml.as_bytes()).unwrap().data, id.data); } + #[test] + fn look_reads_utf16_with_either_byte_order() { + let id = LutFile::identity(2); + let hex: String = id.data.iter().flat_map(|v| v.to_le_bytes()).map(|b| format!("{b:02X}")).collect(); + let xml = format!("\"2\"\"{hex}\""); + let le: Vec = [0xFF, 0xFE].into_iter().chain(xml.encode_utf16().flat_map(u16::to_le_bytes)).collect(); + let be: Vec = [0xFE, 0xFF].into_iter().chain(xml.encode_utf16().flat_map(u16::to_be_bytes)).collect(); + assert_eq!(parse("a.look", &le).unwrap().data, id.data); + assert_eq!(parse("a.look", &be).unwrap().data, id.data); + } + #[test] fn look_ignores_tags_inside_xml_comments() { let hex = |l: &LutFile| -> String { l.data.iter().flat_map(|v| v.to_le_bytes()).map(|b| format!("{b:02X}")).collect() };