From d0693d9dcb800e4dbc18439759a5b915847fcd11 Mon Sep 17 00:00:00 2001 From: FredApps Date: Wed, 18 Nov 2026 16:39:11 +0100 Subject: [PATCH 1/2] psd: share recovering section-divider framing --- Cargo.lock | 2 +- Cargo.toml | 2 +- crates/psd/src/parse.rs | 14 ++++--- crates/psd/src/tests.rs | 1 + crates/psd/src/tests/section_divider.rs | 53 +++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 crates/psd/src/tests/section_divider.rs diff --git a/Cargo.lock b/Cargo.lock index d341691d6..b212dca03 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1463,7 +1463,7 @@ dependencies = [ [[package]] name = "craft-psd" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=e1fc4b1376148197c2cfd2381eeb400e2731269a#e1fc4b1376148197c2cfd2381eeb400e2731269a" +source = "git+https://github.com/storytold/craft-libs?rev=8df7c0268ac112fd285d33688eb6819365391a80#8df7c0268ac112fd285d33688eb6819365391a80" dependencies = [ "encoding_rs", "flate2", diff --git a/Cargo.toml b/Cargo.toml index b5d7cedc0..05384d547 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs" craft-ps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", default-features = false, features = ["read"] } craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } -craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e1fc4b1376148197c2cfd2381eeb400e2731269a" } +craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "8df7c0268ac112fd285d33688eb6819365391a80" } craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "77686f24fe8665c21369f1b593f4d65a973e2a45" } craft-webp = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } diff --git a/crates/psd/src/parse.rs b/crates/psd/src/parse.rs index 3ba26e9fc..473d849c9 100644 --- a/crates/psd/src/parse.rs +++ b/crates/psd/src/parse.rs @@ -158,14 +158,16 @@ impl Psd { b"lyid" => l.id = Some(r.u32()?), b"iOpa" => l.fill_opacity = r.u8()?, b"lsct" | b"lsdk" => { - l.section = match r.u32()? { - 1 => Section::OpenFolder, - 2 => Section::ClosedFolder, - 3 => Section::Divider, + use craft_psd::tagged::{SectionDivider, SectionType}; + let divider = SectionDivider::from_bytes_recovering_blend(b).map_err(|e| error(&e))?; + l.section = match divider.kind { + SectionType::OpenFolder => Section::OpenFolder, + SectionType::ClosedFolder => Section::ClosedFolder, + SectionType::BoundingDivider => Section::Divider, _ => Section::Layer, }; - if b.len() >= 12 && &b[4..8] == b"8BIM" { - l.blend.copy_from_slice(&b[8..12]); + if let Some(blend) = divider.blend_mode { + l.blend = blend.key(); } } b"TySh" => { diff --git a/crates/psd/src/tests.rs b/crates/psd/src/tests.rs index 2935841dd..628a30bff 100644 --- a/crates/psd/src/tests.rs +++ b/crates/psd/src/tests.rs @@ -5,6 +5,7 @@ use crate::pixels::srgb_encode; mod fixed_adjustments; mod integration; mod opt; +mod section_divider; mod shared_corpus; mod unicode_name; diff --git a/crates/psd/src/tests/section_divider.rs b/crates/psd/src/tests/section_divider.rs new file mode 100644 index 000000000..ec208383b --- /dev/null +++ b/crates/psd/src/tests/section_divider.rs @@ -0,0 +1,53 @@ +use super::*; + +fn parse_section_blocks(blocks: Vec<([u8; 4], Vec)>) -> (Section, [u8; 4]) { + let mut doc = WDoc::new(1, 1); + doc.layers = vec![WLayer::empty("Section", blocks).with_blend(b"mul ")]; + let layer = Psd::parse(write(&doc)).unwrap().layers.remove(0); + (layer.section, layer.blend) +} + +#[test] +fn section_prefixes_and_invalid_optional_signatures_preserve_prior_state() { + let full = [0, 0, 0, 2, b'8', b'B', b'I', b'M', b'z', b'z', b'z', b'z', 0, 0, 0, 7, 255]; + for key in [*b"lsct", *b"lsdk"] { + for len in 0..=17 { + let data = full[..len].to_vec(); + assert_eq!( + parse_section_blocks(vec![(key, data.clone())]), + (if len < 4 { Section::Layer } else { Section::ClosedFolder }, if len < 12 { *b"mul " } else { *b"zzzz" }), + "{key:?} length {len}" + ); + let previous = vec![0, 0, 0, 1, b'8', b'B', b'I', b'M', b's', b'c', b'r', b'n']; + let other = if key == *b"lsct" { *b"lsdk" } else { *b"lsct" }; + assert_eq!( + parse_section_blocks(vec![(other, previous), (key, data.clone())]), + (if len < 4 { Section::OpenFolder } else { Section::ClosedFolder }, if len < 12 { *b"scrn" } else { *b"zzzz" }), + ); + if len >= 12 { + let mut bad = data; + bad[4..8].copy_from_slice(b"nope"); + assert_eq!(parse_section_blocks(vec![(key, bad)]), (Section::ClosedFolder, *b"mul ")); + } + } + } +} + +#[test] +fn section_unknown_kinds_and_sequential_keys_keep_blend_until_replaced() { + for kind in [0u32, 1, 2, 3, 99, u32::MAX] { + let expected = match kind { + 1 => Section::OpenFolder, + 2 => Section::ClosedFolder, + 3 => Section::Divider, + _ => Section::Layer, + }; + for keys in [[*b"lsct", *b"lsdk"], [*b"lsdk", *b"lsct"]] { + let previous = vec![0, 0, 0, 1, b'8', b'B', b'I', b'M', b'z', b'z', b'z', b'z']; + let mut bad = kind.to_be_bytes().to_vec(); + bad.extend_from_slice(b"nopepass"); + bad.extend_from_slice(&7u32.to_be_bytes()); + assert_eq!(parse_section_blocks(vec![(keys[0], previous), (keys[1], bad)]), (expected, *b"zzzz")); + } + } +} From e436d421cd9f7c5f25a2f8b0cc420cb1d738838b Mon Sep 17 00:00:00 2001 From: FredApps Date: Wed, 18 Nov 2026 17:42:52 +0100 Subject: [PATCH 2/2] psd: pin section reader with hosted validation repair --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b212dca03..e10293d96 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1463,7 +1463,7 @@ dependencies = [ [[package]] name = "craft-psd" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=8df7c0268ac112fd285d33688eb6819365391a80#8df7c0268ac112fd285d33688eb6819365391a80" +source = "git+https://github.com/storytold/craft-libs?rev=d04bd2c4fbf49e399c7e0cd7a16feebebadd739c#d04bd2c4fbf49e399c7e0cd7a16feebebadd739c" dependencies = [ "encoding_rs", "flate2", diff --git a/Cargo.toml b/Cargo.toml index 05384d547..c007d1628 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs" craft-ps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", default-features = false, features = ["read"] } craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } -craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "8df7c0268ac112fd285d33688eb6819365391a80" } +craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "d04bd2c4fbf49e399c7e0cd7a16feebebadd739c" } craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "77686f24fe8665c21369f1b593f4d65a973e2a45" } craft-webp = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }