mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-16 15:22:43 +00:00
psd: record stray leading bytes before the layer block check, use craft-psd's long-length keys
This commit is contained in:
1 parent
e9690b2c02
commit
7f9f0bc489
2 files changed
+26
-10
No files matched your search
+9
-10
@@ -494,18 +494,23 @@ fn prefixed_layer_block(data: &[u8]) -> Option<(Vec<u8>, Splice)> {
|
||||
let end = be(data, ls, w)?.checked_add(ls + w)?.min(data.len());
|
||||
let li_at = ls + w;
|
||||
let gm_at = be(data, li_at, w)?.checked_add(li_at + w)?;
|
||||
let mut pos = be(data, gm_at, 4)?.checked_add(gm_at + 4)?;
|
||||
let blocks = be(data, gm_at, 4)?.checked_add(gm_at + 4)?;
|
||||
let mut pos = blocks;
|
||||
let is_sig = |i: usize| matches!(data.get(i..i + 4), Some(b"8BIM" | b"8B64"));
|
||||
// Stray bytes before the first block hide every block from craft-psd.
|
||||
let mut lead = None;
|
||||
while pos < end {
|
||||
let skip = (0..=3).find(|&k| is_sig(pos + k))?;
|
||||
let at = pos + skip;
|
||||
let key = data.get(at + 4..at + 8)?;
|
||||
let width = if w == 8 && PSB_LONG_KEYS.iter().any(|k| k[..] == *key) { 8 } else { 4 };
|
||||
// Leading bytes before the first block are recorded before its key is looked at.
|
||||
if lead.is_none() && pos == blocks && skip > 0 {
|
||||
lead = Some((pos, skip));
|
||||
}
|
||||
let key: [u8; 4] = data.get(at + 4..at + 8)?.try_into().ok()?;
|
||||
let width = if craft_psd::tagged::uses_long_length(head.header.version, &key) { 8 } else { 4 };
|
||||
let len = be(data, at + 8, width)?;
|
||||
let body = at + 8 + width;
|
||||
if matches!(key, b"Lr16" | b"Lr32" | b"Layr") {
|
||||
if matches!(&key, b"Lr16" | b"Lr32" | b"Layr") {
|
||||
if let Some((at, n)) = lead {
|
||||
return Splice::apply(data, at, n, &[(ls, w)]);
|
||||
}
|
||||
@@ -513,17 +518,11 @@ fn prefixed_layer_block(data: &[u8]) -> Option<(Vec<u8>, Splice)> {
|
||||
be(data, body, w).filter(|&n| n > 0 && n.checked_add(w).is_some_and(|e| e <= len))?;
|
||||
return Splice::apply(data, body, w, &[(at + 8, width), (ls, w)]);
|
||||
}
|
||||
if pos == gm_at + 4 + be(data, gm_at, 4)? && skip > 0 {
|
||||
lead = Some((pos, skip));
|
||||
}
|
||||
pos = body.checked_add(len)?;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Keys whose block length is 8 bytes in a PSB.
|
||||
const PSB_LONG_KEYS: [&[u8; 4]; 13] = [b"LMsk", b"Lr16", b"Lr32", b"Layr", b"Mt16", b"Mt32", b"Mtrn", b"Alph", b"FMsk", b"lnk2", b"FEid", b"FXid", b"PxSD"];
|
||||
|
||||
/// A copy of the file with one range taken out, and the way from the copy's offsets back to the
|
||||
/// original's.
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
|
||||
@@ -173,3 +173,20 @@ fn a_stray_byte_before_the_global_blocks_is_skipped() {
|
||||
let p = Psd::parse(insert(&bytes, first, &[0], &[(ls, w)])).expect("parse");
|
||||
same_layers(&p, &want);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stray_bytes_before_a_leading_16_bit_block_are_skipped() {
|
||||
let (bytes, want, ls, w, at) = deep();
|
||||
let li_at = ls + w;
|
||||
let gm_at = li_at + w + bytes[li_at..li_at + w].iter().fold(0usize, |v, &b| v << 8 | usize::from(b));
|
||||
let first = gm_at + 4 + u32::from_be_bytes(bytes[gm_at..gm_at + 4].try_into().unwrap()) as usize;
|
||||
// Drop the blocks before `Lr16` so it comes first, then put 1 to 3 stray bytes before it.
|
||||
let mut lead = [&bytes[..first], &bytes[at..]].concat();
|
||||
let lm = lead[ls..ls + w].iter().fold(0u64, |v, &b| v << 8 | u64::from(b)) - (at - first) as u64;
|
||||
lead[ls..ls + w].copy_from_slice(&lm.to_be_bytes()[8 - w..]);
|
||||
assert_eq!(&lead[first + 4..first + 8], b"Lr16");
|
||||
for n in 1..=3 {
|
||||
let p = Psd::parse(insert(&lead, first, &vec![0; n], &[(ls, w)])).expect("parse");
|
||||
same_layers(&p, &want);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user