mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Merge pull request #15218 from storytold/cutover/psd-unicode-names
io: use shared PSD Unicode channel-name framing
This commit is contained in:
4 files changed
+32
-13
No files matched your search
Generated
+12
@@ -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)",
|
||||
|
||||
@@ -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" }
|
||||
|
||||
@@ -904,19 +904,12 @@ fn be_to_ne_plane(plane: &[u8], s: SampleType, n: usize) -> craft_psd::Result<Ve
|
||||
}
|
||||
|
||||
pub(crate) fn unicode_names(data: &[u8]) -> Vec<String> {
|
||||
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<u16> = 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<u16> = 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
|
||||
|
||||
@@ -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"]);
|
||||
}
|
||||
Reference in new issue
Block a user