PSD: a version 6 slice record whose ID is 16 is read as a slice

A version 6 slices resource may follow a record with a descriptor of
version 16, and the reader took any record that starts with the value
16 for one. A record whose ID is 16 then had its first bytes read as a
descriptor, and the records after it were misread or left out. When the
records read one after another, followed by nothing or by one
descriptor (the layout ag-psd writes), only the last record is checked
for a descriptor. Files that follow each record with a descriptor are
read as before.
This commit is contained in:
dotCooCoo
2026-11-15 08:45:10 -08:00
parent bd67a010f6
commit 303d0c0a05
2 changed files with 70 additions and 6 deletions
+44 -6
View File
@@ -87,8 +87,9 @@ fn version_6(r: &mut Reader<'_>, budget: &mut usize) -> Result<Vec<PsdSlice>, St
if count > MAX_SLICES {
return Err(format!("the file has {count} slices, more than the {MAX_SLICES} read"));
}
let one_after_another = records_one_after_another(Reader::new(r.rest()), count, *budget);
let mut out = vec![];
for _ in 0..count {
for i in 0..count {
// The ID and group ID.
r.take(8)?;
let origin = r.u32()?;
@@ -116,9 +117,10 @@ fn version_6(r: &mut Reader<'_>, budget: &mut usize) -> Result<Vec<PsdSlice>, St
format!("#{red:02x}{green:02x}{blue:02x}")
};
// Photoshop 7 and later may follow a record with a descriptor (version 16); a record that
// doesn't start one is the next slice. A descriptor read that uses up the budget is an
// error.
if r.rest().starts_with(&16u32.to_be_bytes()) {
// doesn't start one is the next slice. When the records follow one another, only the
// last one may be followed by a descriptor. A descriptor read that uses up the budget is
// an error.
if (!one_after_another || i + 1 == count) && r.rest().starts_with(&16u32.to_be_bytes()) {
let mut ahead = Reader::new(r.rest());
ahead.u32()?;
match descriptor(&mut ahead, 0, budget) {
@@ -139,6 +141,37 @@ fn version_6(r: &mut Reader<'_>, budget: &mut usize) -> Result<Vec<PsdSlice>, St
Ok(out)
}
/// Whether `r` holds `count` version 6 records one after another, followed by nothing or by one
/// descriptor (version 16) that reads within `budget`, as ag-psd writes them. A record whose ID is
/// 16 then isn't taken for a descriptor. Otherwise each record may be followed by a descriptor.
fn records_one_after_another(mut r: Reader<'_>, count: usize, mut budget: usize) -> bool {
if !(0..count).all(|_| skip_record(&mut r).is_ok()) {
return false;
}
r.rest().is_empty() || (r.rest().starts_with(&16u32.to_be_bytes()) && r.u32().is_ok() && descriptor(&mut r, 0, &mut budget).is_ok())
}
/// Reads past the version 6 record `r` starts with, in the layout [`version_6`] reads. Its strings
/// are checked and read past without being decoded.
fn skip_record(r: &mut Reader<'_>) -> Result<(), String> {
// The ID and group ID.
r.take(8)?;
if r.u32()? == 1 {
r.take(4)?;
}
units(r)?;
// The type and the rectangle.
r.take(20)?;
for _ in 0..4 {
units(r)?;
}
r.take(1)?;
units(r)?;
// The alignments and the background color.
r.take(12)?;
Ok(())
}
/// Versions 7 and 8: the descriptor's `slices` list, each slice an object.
fn from_descriptor(d: &[(String, Value)]) -> Result<Vec<PsdSlice>, String> {
let Some(Value::List(list)) = field(d, "slices") else { return Err("the slices' descriptor has no list of slices".into()) };
@@ -221,12 +254,17 @@ fn text_len(n: usize) -> Result<usize, String> {
/// A Unicode string: a count of UTF-16 units, then the units; trailing NULs dropped.
fn unicode(r: &mut Reader<'_>) -> Result<String, String> {
let n = text_len(r.u32()? as usize)?;
let bytes = r.take(n.checked_mul(2).ok_or(TRUNCATED)?)?;
let bytes = units(r)?;
let units: Vec<u16> = bytes.as_chunks::<2>().0.iter().map(|u| u16::from_be_bytes(*u)).collect();
Ok(String::from_utf16_lossy(&units).trim_end_matches('\0').to_string())
}
/// A Unicode string's units, as bytes: a count of UTF-16 units, then the units.
fn units<'a>(r: &mut Reader<'a>) -> Result<&'a [u8], String> {
let n = text_len(r.u32()? as usize)?;
r.take(n.checked_mul(2).ok_or(TRUNCATED)?)
}
/// A descriptor's value, as far as the slices need it.
#[derive(Clone, Debug, PartialEq)]
enum Value {
@@ -262,6 +262,32 @@ fn photoshop_7_descriptors_after_version_6_records_are_read_past() {
assert_eq!(names, ["a", "b"]);
}
#[test]
fn a_version_6_record_whose_id_is_16_is_a_slice_not_a_descriptor() {
// The 16th record has ID 16, group ID 0, origin 2, no name and the Image type: its first 18
// bytes also read as a descriptor of version 16 with no items.
let records: Vec<S> = (1..=17).map(|id| if id == 16 { user("", [1, 1, 4, 4]) } else { user("a", [0, 0, 5, 5]) }).collect();
let descriptor_after = [16u32.to_be_bytes().to_vec(), descriptor(&[("topOutset", D::Long(0))])].concat();
for trailing in [vec![], descriptor_after] {
let (s, r) = open(&with_resource(&psd(false), 1050, &v6(&records, &trailing)), json!({}));
assert!(warnings(&r).is_empty(), "{r}");
let got = slices(&s);
assert_eq!(got.len(), 17, "{got:?}");
assert_eq!((got[15].0, got[15].1.name.as_str()), (Rect::new(1.0, 1.0, 4.0, 4.0), ""));
assert_eq!(got[16].1.name, "a");
}
// A descriptor after each record whose class ID is four zero bytes, with no items, followed by
// records with no strings: the records don't also read one after another.
let empty = [ustr(""), 0u32.to_be_bytes().to_vec(), vec![0; 4], 0u32.to_be_bytes().to_vec()].concat();
let trailing = [16u32.to_be_bytes().to_vec(), empty].concat();
for rect in [[0, 0, 0, 0], [0, 0, 5, 5]] {
let (s, r) = open(&with_resource(&psd(false), 1050, &v6(&[user("", rect), user("", rect), user("", rect)], &trailing)), json!({}));
let w = warnings(&r);
assert!(!w.iter().any(|w| w.starts_with("the slices can't be read")), "{w:?}");
assert_eq!(slices(&s).len(), if rect[2] > 0 { 3 } else { 0 }, "{w:?}");
}
}
#[test]
fn version_6_descriptors_share_one_budget_of_values() {
// Each record's descriptor holds 2,501 values, well under the 200,000 one descriptor may hold;