mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 16:53:05 +00:00
Merge pull request #2241 from FredApps/fix/786-psd-section-recovery
psd: add opt-in section-divider blend recovery
This commit is contained in:
@@ -489,7 +489,23 @@ fn expect_kind(r: &mut Reader<'_>, kind: &'static [u8; 4]) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl SectionDivider {
|
||||
/// Read a divider while recovering a missing or invalid optional blend signature.
|
||||
///
|
||||
/// The four-byte kind is required. Fewer than eight bytes after the kind, or a suffix
|
||||
/// whose signature is not `8BIM`, leaves both blend mode and subtype absent.
|
||||
/// Unlike this opt-in recovery, [`TaggedBlock::parsed`] still rejects an invalid
|
||||
/// complete signature. Unknown kinds and blend keys are preserved.
|
||||
pub fn from_bytes_recovering_blend(data: &[u8]) -> Result<Self> {
|
||||
parse_section_with_recovery(data, true)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_section(d: &[u8]) -> Result<SectionDivider> {
|
||||
parse_section_with_recovery(d, false)
|
||||
}
|
||||
|
||||
fn parse_section_with_recovery(d: &[u8], recover_blend: bool) -> Result<SectionDivider> {
|
||||
let mut r = Reader::new(d);
|
||||
let kind = SectionType::from_u32(r.u32()?);
|
||||
let mut blend_mode = None;
|
||||
@@ -497,6 +513,9 @@ fn parse_section(d: &[u8]) -> Result<SectionDivider> {
|
||||
if r.remaining() >= 8 {
|
||||
let sig = r.array::<4>()?;
|
||||
if &sig != b"8BIM" {
|
||||
if recover_blend {
|
||||
return Ok(SectionDivider { kind, blend_mode, sub_type });
|
||||
}
|
||||
return Err(PsdError::InvalidSignature { expected: "8BIM", found: sig });
|
||||
}
|
||||
blend_mode = Some(BlendMode::from_key(r.array()?));
|
||||
@@ -809,4 +828,35 @@ mod tests {
|
||||
assert_eq!(blocks, vec![TaggedBlock::layer_id(1)]);
|
||||
assert_eq!(trailing, out[damaged..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn section_recovering_blend_preserves_strict_parser_and_prefixes() {
|
||||
let full = [0, 0, 0, 99, b'8', b'B', b'I', b'M', b'z', b'z', b'z', b'z', 0, 0, 0, 7, 255];
|
||||
for len in 0..=17 {
|
||||
let data = &full[..len];
|
||||
let recovered = SectionDivider::from_bytes_recovering_blend(data);
|
||||
if len < 4 {
|
||||
assert!(recovered.is_err());
|
||||
assert!(parse_section(data).is_err());
|
||||
continue;
|
||||
}
|
||||
let value = recovered.unwrap();
|
||||
assert_eq!(value.kind, SectionType::Unknown(99));
|
||||
assert_eq!(value.blend_mode.map(BlendMode::key), (len >= 12).then_some(*b"zzzz"));
|
||||
assert_eq!(value.sub_type, (len >= 16).then_some(7));
|
||||
assert_eq!(parse_section(data).unwrap(), value);
|
||||
for key in [*b"lsct", *b"lsdk"] {
|
||||
assert_eq!(TaggedBlock::new(key, data.to_vec()).parsed(), Some(Ok(BlockData::SectionDivider(value))));
|
||||
}
|
||||
}
|
||||
for len in 12..=17 {
|
||||
let mut data = full[..len].to_vec();
|
||||
data[4..8].copy_from_slice(b"nope");
|
||||
let value = SectionDivider::from_bytes_recovering_blend(&data).unwrap();
|
||||
assert_eq!(value, SectionDivider { kind: SectionType::Unknown(99), blend_mode: None, sub_type: None });
|
||||
for key in [*b"lsct", *b"lsdk"] {
|
||||
assert!(matches!(TaggedBlock::new(key, data.clone()).parsed(), Some(Err(PsdError::InvalidSignature { .. }))));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user