mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +00:00
psd: add bounded PackBits recovery decoding for app cutovers (#1885)
* psd: expose bounded PackBits recovery row decoding * Clarify recovery row budget and partial-error contract
This commit is contained in:
1 parent
bea91b1861
commit
b774e4d168
2 files changed
+50
-4
No files matched your search
@@ -206,10 +206,34 @@ pub mod packbits {
|
||||
/// `-128` no-op bytes (which are always permitted). Errors, rather than
|
||||
/// overflowing, when `out.len() + expected` does not fit in `usize`.
|
||||
pub(crate) fn decode_into(src: &[u8], expected: usize, out: &mut Vec<u8>) -> Result<()> {
|
||||
decode_row(src, expected, out, false)
|
||||
}
|
||||
|
||||
/// Appends a recovery row: complete oversized packets are clipped and a short
|
||||
/// row is zero-padded. Truncated packet payloads still fail; trailing packets
|
||||
/// after the row is full are ignored. Strict decoding remains the default.
|
||||
///
|
||||
/// Appended row bytes are bounded by `max_row_bytes` and the PSD decoded-size
|
||||
/// limit; callers enforce their aggregate image budget. Allocation is fallible.
|
||||
/// Existing output is retained; an error may leave a partially appended row.
|
||||
pub fn decode_padded_into(src: &[u8], expected: usize, max_row_bytes: usize, out: &mut Vec<u8>) -> Result<()> {
|
||||
if expected > max_row_bytes || expected as u64 > super::MAX_DECODED_BYTES {
|
||||
return Err(PsdError::LimitExceeded("PackBits recovery row exceeds limit"));
|
||||
}
|
||||
out.len().checked_add(expected).ok_or(PsdError::LimitExceeded("PackBits output length overflows usize"))?;
|
||||
out.try_reserve_exact(expected).map_err(|_| PsdError::OutOfMemory("PackBits recovery row allocation failed"))?;
|
||||
decode_row(src, expected, out, true)
|
||||
}
|
||||
|
||||
fn decode_row(src: &[u8], expected: usize, out: &mut Vec<u8>, padded: bool) -> Result<()> {
|
||||
let target = out.len().checked_add(expected).ok_or(PsdError::LimitExceeded("PackBits output length overflows usize"))?;
|
||||
let mut i = 0;
|
||||
while out.len() < target {
|
||||
let Some(&h) = src.get(i) else {
|
||||
if padded {
|
||||
out.resize(target, 0);
|
||||
break;
|
||||
}
|
||||
return Err(PsdError::Decompress("PackBits row ended early".into()));
|
||||
};
|
||||
i += 1;
|
||||
@@ -219,10 +243,10 @@ pub mod packbits {
|
||||
let Some(lit) = src.get(i..i + len) else {
|
||||
return Err(PsdError::Decompress("PackBits literal truncated".into()));
|
||||
};
|
||||
if len > target - out.len() {
|
||||
if !padded && len > target - out.len() {
|
||||
return Err(PsdError::Decompress("PackBits literal overflows row".into()));
|
||||
}
|
||||
out.extend_from_slice(lit);
|
||||
out.extend_from_slice(&lit[..len.min(target - out.len())]);
|
||||
i += len;
|
||||
} else if h != -128 {
|
||||
let len = 1 - h as isize;
|
||||
@@ -231,10 +255,10 @@ pub mod packbits {
|
||||
return Err(PsdError::Decompress("PackBits run truncated".into()));
|
||||
};
|
||||
i += 1;
|
||||
if len > target - out.len() {
|
||||
if !padded && len > target - out.len() {
|
||||
return Err(PsdError::Decompress("PackBits run overflows row".into()));
|
||||
}
|
||||
out.extend(std::iter::repeat_n(b, len));
|
||||
out.extend(std::iter::repeat_n(b, len.min(target - out.len())));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -771,3 +771,25 @@ fn rle_chunks_use_the_callers_pool_and_report_panics() {
|
||||
assert!(map_chunks(2, vec![0, 1], |_| panic!("worker")).is_err());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn padded_packbits_preserves_recovery_without_weakening_strict_decode() {
|
||||
for (input, expected) in
|
||||
[(&[0, 7][..], vec![7, 0, 0]), (&[3, 1, 2, 3, 4][..], vec![1, 2, 3]), (&[253, 9][..], vec![9, 9, 9]), (&[128][..], vec![0, 0, 0])]
|
||||
{
|
||||
let mut out = vec![42];
|
||||
super::packbits::decode_padded_into(input, 3, 3, &mut out).unwrap();
|
||||
assert_eq!(&out[1..], expected);
|
||||
assert_eq!(out[0], 42);
|
||||
assert!(super::packbits::decode(input, 3).is_err());
|
||||
}
|
||||
for input in [&[3, 1][..], &[255][..]] {
|
||||
assert!(super::packbits::decode_padded_into(input, 1, 1, &mut vec![]).is_err());
|
||||
}
|
||||
let mut out = vec![42];
|
||||
assert!(super::packbits::decode_padded_into(&[], 4, 3, &mut out).is_err());
|
||||
assert_eq!(out, [42]);
|
||||
super::packbits::decode_padded_into(&[255], 0, 0, &mut out).unwrap();
|
||||
super::packbits::decode_padded_into(&[0, 7, 255], 1, 1, &mut out).unwrap();
|
||||
assert_eq!(out, [42, 7]);
|
||||
}
|
||||
Reference in new issue
Block a user