psd: delegate bounded recovery PackBits rows to craft-psd

This commit is contained in:
Dylan M. Taylor committed 2026-11-16 08:34:11 -05:00
1 parent cb9c1b5e1a
commit 4b658873b7
4 files changed
+22 -23

No files matched your search

Generated
+1 -1
View File
@@ -1359,7 +1359,7 @@ dependencies = [
[[package]]
name = "craft-psd"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87#5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87"
source = "git+https://github.com/storytold/craft-libs?rev=55baef0c8d2180b50bcbd8f99a4e8d5bb2389dd6#55baef0c8d2180b50bcbd8f99a4e8d5bb2389dd6"
dependencies = [
"encoding_rs",
"flate2",
+1 -1
View File
@@ -20,7 +20,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 = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "55baef0c8d2180b50bcbd8f99a4e8d5bb2389dd6" }
craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
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" }
+6 -21
View File
@@ -482,27 +482,12 @@ enum PlaneStream {
/// PackBits decoding of one row (appends exactly `row` bytes, zero-padded).
pub(crate) fn unpack_bits(src: &[u8], row: usize, out: &mut Vec<u8>) -> Result<()> {
let start = out.len();
let mut i = 0;
while i < src.len() && out.len() - start < row {
let n = src[i] as i8;
i += 1;
// A packet is written only up to the row's end, so the reserved plane never grows.
let room = row - (out.len() - start);
if n >= 0 {
let k = n as usize + 1;
let s = src.get(i..i + k).ok_or(Error::Truncated)?;
out.extend_from_slice(&s[..k.min(room)]);
i += k;
} else if n != -128 {
let k = (1 - n as isize) as usize;
let v = *src.get(i).ok_or(Error::Truncated)?;
out.extend(std::iter::repeat_n(v, k.min(room)));
i += 1;
}
}
out.resize(start + row, 0);
Ok(())
// The production caller already charges/reserves the complete plane in Budget.
// Keep the shared recovery API bounded to this row; never relax the app budget.
craft_psd::compression::packbits::decode_padded_into(src, row, row, out).map_err(|error| match error {
craft_psd::PsdError::Decompress(_) => Error::Truncated,
other => Error::Invalid(other.to_string()),
})
}
/// PackBits encoding of one row.
+14
View File
@@ -520,3 +520,17 @@ fn a_failed_inflate_reports_its_status_not_its_output() {
let Err(Error::Invalid(msg)) = crate::limits::inflate(&z, 1 << 21) else { panic!("the checksum fails") };
assert!(msg.len() < 100, "{} bytes", msg.len());
}
#[test]
fn shared_packbits_keeps_padded_rows_and_truncated_packet_errors() {
let mut out = vec![42];
unpack_bits(&[0, 7], 3, &mut out).unwrap();
assert_eq!(out, [42, 7, 0, 0]);
unpack_bits(&[128], 2, &mut out).unwrap();
assert_eq!(out, [42, 7, 0, 0, 0, 0]);
assert_eq!(unpack_bits(&[3, 1], 1, &mut vec![]), Err(Error::Truncated));
assert_eq!(unpack_bits(&[255], 1, &mut vec![]), Err(Error::Truncated));
let mut row = vec![];
unpack_bits(&[0, 9, 255], 1, &mut row).unwrap();
assert_eq!(row, [9]);
}