diff --git a/Cargo.lock b/Cargo.lock index ae7f294a7..a9c0fcf11 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index 21f47439c..94aae50b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/crates/psd/src/pixels.rs b/crates/psd/src/pixels.rs index 290a7b6f4..6a2bfc65a 100644 --- a/crates/psd/src/pixels.rs +++ b/crates/psd/src/pixels.rs @@ -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) -> 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. diff --git a/crates/psd/src/tests.rs b/crates/psd/src/tests.rs index 22c575154..3930e9c53 100644 --- a/crates/psd/src/tests.rs +++ b/crates/psd/src/tests.rs @@ -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]); +}