io: use shared PSD Unicode channel-name framing

Keep lossy UTF-16, trailing NUL and partial-list policy unchanged. Shared parser preserves Brandon Thomas original framing and Valentin Klein checked-length safeguards.

Co-authored-by: Brandon Thomas <[email protected]>

Co-authored-by: Valentin Klein <[email protected]>
This commit is contained in:
committed 2026-11-15 15:10:15 -05:00
1 parent 463baf8d33
commit e276dbcd26
4 files changed
+39 -20

No files matched your search

Generated
+19 -7
View File
@@ -1628,6 +1628,17 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-psd"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=9d56699be8f8c04fb61f4710186b7454317afaa5#9d56699be8f8c04fb61f4710186b7454317afaa5"
dependencies = [
"encoding_rs",
"flate2",
"rayon",
"thiserror 2.0.21",
]
[[package]]
name = "craft-raw"
version = "0.1.0"
@@ -5527,7 +5538,7 @@ dependencies = [
"craft-ai-agents",
"craft-flags",
"craft-image-codecs",
"craft-psd",
"craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=09188c3bbfa443b089a3e022fad6abd7b464efda)",
"getrandom 0.3.4",
"hmac",
"photocraft-color",
@@ -5561,7 +5572,7 @@ dependencies = [
"clap_mangen",
"craft-cms",
"craft-image-codecs",
"craft-psd",
"craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=09188c3bbfa443b089a3e022fad6abd7b464efda)",
"photocraft-automation",
"photocraft-doc",
"photocraft-engine",
@@ -5626,7 +5637,7 @@ dependencies = [
"craft-image-codecs",
"craft-pcraft-format 0.1.0 (git+https://github.com/storytold/craft-libs?rev=3e970577b9bb446967c2003ec8ecaf4f28b0768c)",
"craft-print",
"craft-psd",
"craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=09188c3bbfa443b089a3e022fad6abd7b464efda)",
"craft-xml",
"exr",
"flate2",
@@ -5729,7 +5740,8 @@ dependencies = [
"craft-ora",
"craft-pdf",
"craft-pdn",
"craft-psd",
"craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=09188c3bbfa443b089a3e022fad6abd7b464efda)",
"craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=9d56699be8f8c04fb61f4710186b7454317afaa5)",
"craft-svg",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ad8faa1c136ce289e9f132c9137d0cd913bd4ef6)",
"craft-xcf",
@@ -5861,7 +5873,7 @@ version = "0.23.1"
dependencies = [
"base64 0.23.1",
"craft-plist",
"craft-psd",
"craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=09188c3bbfa443b089a3e022fad6abd7b464efda)",
"flate2",
"parley",
"photocraft-color",
@@ -5883,7 +5895,7 @@ dependencies = [
"craft-i18n",
"craft-image-codecs",
"craft-print",
"craft-psd",
"craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=09188c3bbfa443b089a3e022fad6abd7b464efda)",
"craft-ui",
"craft-update",
"eframe",
@@ -5938,7 +5950,7 @@ name = "photocraft-web"
version = "0.23.1"
dependencies = [
"craft-image-codecs",
"craft-psd",
"craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=09188c3bbfa443b089a3e022fad6abd7b464efda)",
"eframe",
"egui",
"js-sys",
+1
View File
@@ -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 = "9d56699be8f8c04fb61f4710186b7454317afaa5", version = "0.1.0" }
photocraft-codecs = { workspace = true }
photocraft-formats = { workspace = true }
photocraft-raw = { workspace = true }
+6 -13
View File
@@ -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
+13
View File
@@ -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"]);
}