diff --git a/Cargo.lock b/Cargo.lock index abc48ffb891..a6cef645374 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1617,6 +1617,17 @@ dependencies = [ "winsafe", ] +[[package]] +name = "craft-psd" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=040b5202f9ca2ecaedcaea9351fc095c0d3dc5e8#040b5202f9ca2ecaedcaea9351fc095c0d3dc5e8" +dependencies = [ + "encoding_rs", + "flate2", + "rayon", + "thiserror 2.0.21", +] + [[package]] name = "craft-psd" version = "0.1.0" @@ -5762,6 +5773,7 @@ dependencies = [ "craft-ora", "craft-pdf", "craft-pdn", + "craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=040b5202f9ca2ecaedcaea9351fc095c0d3dc5e8)", "craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=2284a6bf76b3d629b5e5bb2ca71e1c319d8dae5f)", "craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=589842f0da5c5a11e71f39b70092dcb918b92f39)", "craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=5e2bc54a48c9f03e7ce4eca42775ce5cc051f5d6)", diff --git a/crates/io/Cargo.toml b/crates/io/Cargo.toml index 1cbb8c4ba7b..a87a3b27d56 100644 --- a/crates/io/Cargo.toml +++ b/crates/io/Cargo.toml @@ -29,6 +29,7 @@ photocraft-doc = { workspace = true } photocraft-compose = { workspace = true } photocraft-algo = { workspace = true } craft-psd = { workspace = true } +craft-psd-names = { package = "craft-psd", git = "https://github.com/storytold/craft-libs", rev = "040b5202f9ca2ecaedcaea9351fc095c0d3dc5e8", version = "0.1.0" } craft-psd-channels = { package = "craft-psd", git = "https://github.com/storytold/craft-libs", rev = "5e2bc54a48c9f03e7ce4eca42775ce5cc051f5d6", version = "0.1.0" } craft-psd-duotone = { package = "craft-psd", version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "b9c22554ce0fd2caccdb53b417082a2ff2ef178f" } craft-psd-guides = { package = "craft-psd", version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "589842f0da5c5a11e71f39b70092dcb918b92f39" } diff --git a/crates/io/src/psd_import.rs b/crates/io/src/psd_import.rs index ac44ac6659f..2b93d02133b 100644 --- a/crates/io/src/psd_import.rs +++ b/crates/io/src/psd_import.rs @@ -904,19 +904,12 @@ fn be_to_ne_plane(plane: &[u8], s: SampleType, n: usize) -> craft_psd::Result Vec { - let mut out = Vec::new(); - let mut at = 0; - while at + 4 <= data.len() { - let n = u32::from_be_bytes([data[at], data[at + 1], data[at + 2], data[at + 3]]) as usize; - at += 4; - // Checked: `n * 2` overflows a 32-bit `usize` for a hostile length. - let Some(end) = n.checked_mul(2).and_then(|len| at.checked_add(len)) else { break }; - let Some(b) = data.get(at..end) else { break }; - let units: Vec = b.as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes([c[0], c[1]])).collect(); - out.push(String::from_utf16_lossy(&units).trim_end_matches('\0').to_string()); - at = end; - } - out + craft_psd_names::unicode_names::records(data) + .map(|b| { + let units: Vec = b.as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes(*c)).collect(); + String::from_utf16_lossy(&units).trim_end_matches('\0').to_string() + }) + .collect() } /// Plane `i` of a decoded merged image whose planes are `plane` bytes each, or `None` when it is diff --git a/crates/io/src/psd_import/tests.rs b/crates/io/src/psd_import/tests.rs index f34c81b8f6f..d15ef5fa28e 100644 --- a/crates/io/src/psd_import/tests.rs +++ b/crates/io/src/psd_import/tests.rs @@ -627,3 +627,16 @@ fn legacy_conversion_refuses_before_waiting_for_the_decode_gate() { drop(_guard); handle.join().expect("worker thread"); } + +#[test] +fn unicode_names_preserve_empty_lossy_and_terminator_policy() { + let names: &[&[u16]] = &[&[], &[0xd800, 65, 0, 0], &[0xd83d, 0xde00], &[65, 0, 66]]; + let mut data = Vec::new(); + for name in names { + data.extend((name.len() as u32).to_be_bytes()); + data.extend(name.iter().flat_map(|u| u.to_be_bytes())); + } + assert_eq!(unicode_names(&data), ["", "\u{fffd}A", "😀", "A\0B"]); + data.extend([0, 0, 0, 2, 0, 65]); + assert_eq!(unicode_names(&data), ["", "\u{fffd}A", "😀", "A\0B"]); +}