Import binary plist parser changes from Craft Anything 0a8cc088

This commit is contained in:
RRFZ authored and Dylan M. Taylor committed 2026-11-15 07:55:30 -05:00
1 parent feea4e316d
commit d307449ee4
1 file changed
+52 -4
+52 -4
View File
@@ -68,10 +68,17 @@ struct Bin<'a> {
offsets: Vec<usize>,
ref_size: usize,
nodes: usize,
/// Data and text bytes still allowed to be copied out.
bytes_left: usize,
}
/// Parse a `bplist00` binary property list.
pub fn parse_binary(data: &[u8]) -> Result<Value, FormatError> {
parse_binary_within(data, MAX_EXPANDED_BYTES.max(data.len().saturating_mul(3)))
}
/// Parse a binary property list, producing at most `byte_budget` bytes of data and decoded text.
fn parse_binary_within(data: &[u8], byte_budget: usize) -> Result<Value, FormatError> {
if !data.starts_with(b"bplist") {
return Err(FormatError::Malformed("not a binary plist".into()));
}
@@ -110,7 +117,7 @@ pub fn parse_binary(data: &[u8]) -> Result<Value, FormatError> {
let top = usize::try_from(top).map_err(|_| FormatError::Malformed("top object".into()))?;
// Object payloads and references must not borrow bytes from the offset table or trailer.
let objects = data.get(..table).ok_or_else(|| FormatError::Truncated("object area".into()))?;
let mut b = Bin { r: Reader::new(objects), offsets, ref_size, nodes: 0 };
let mut b = Bin { r: Reader::new(objects), offsets, ref_size, nodes: 0, bytes_left: byte_budget };
let mut stack = Vec::new();
b.object(top, &mut stack)
}
@@ -120,6 +127,21 @@ fn read_be(r: &Reader<'_>, off: usize, n: usize) -> Option<u64> {
Some(s.iter().fold(0u64, |acc, &b| (acc << 8) | u64::from(b)))
}
/// UTF-8 size of `String::from_utf8_lossy(bytes)`: each damaged sequence becomes a U+FFFD.
fn lossy_utf8_len(bytes: &[u8]) -> usize {
bytes.utf8_chunks().fold(0usize, |n, c| {
let replacement = if c.invalid().is_empty() { 0 } else { char::REPLACEMENT_CHARACTER.len_utf8() };
n.saturating_add(c.valid().len()).saturating_add(replacement)
})
}
/// UTF-8 size of `String::from_utf16_lossy` over big-endian code units: a lone surrogate becomes
/// a U+FFFD.
fn lossy_utf16_len(bytes: &[u8]) -> usize {
let units = bytes.as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes(*c));
char::decode_utf16(units).fold(0usize, |n, c| n.saturating_add(c.map_or(char::REPLACEMENT_CHARACTER.len_utf8(), char::len_utf8)))
}
impl Bin<'_> {
fn count(&self, at: usize, low: u8) -> Result<(usize, usize), FormatError> {
if low != 0xf {
@@ -144,6 +166,22 @@ impl Bin<'_> {
usize::try_from(v).map_err(|_| FormatError::Malformed("reference".into()))
}
/// Refuse a container whose `children` would pass MAX_NODES, before space is reserved for them.
fn room_for_nodes(&self, children: usize) -> Result<(), FormatError> {
if children > MAX_NODES.saturating_sub(self.nodes) {
return Err(FormatError::TooLarge(format!("more than {MAX_NODES} plist nodes")));
}
Ok(())
}
/// Spend `n` bytes of the data and text budget before copying them out.
fn spend(&mut self, n: usize) -> Result<(), FormatError> {
self.bytes_left =
self.bytes_left.checked_sub(n).ok_or_else(|| FormatError::TooLarge("repeated data and text expand past the plist byte budget".into()))?;
Ok(())
}
/// Read object `idx`. `stack` holds the containers on the current path, so a cycle is refused.
fn object(&mut self, idx: usize, stack: &mut Vec<usize>) -> Result<Value, FormatError> {
self.nodes += 1;
if self.nodes > MAX_NODES {
@@ -189,11 +227,15 @@ impl Bin<'_> {
0x3 => Value::Date(DateValue::Seconds(self.r.u64_be(at + 1).map(f64::from_bits).ok_or_else(|| FormatError::Truncated("date".into()))?)),
0x4 => {
let (n, s) = self.count(at, lo)?;
Value::Data(self.r.slice(s, n).ok_or_else(|| FormatError::Truncated("data".into()))?.to_vec())
let bytes = self.r.slice(s, n).ok_or_else(|| FormatError::Truncated("data".into()))?;
self.spend(bytes.len())?;
Value::Data(bytes.to_vec())
}
0x5 => {
let (n, s) = self.count(at, lo)?;
Value::String(String::from_utf8_lossy(self.r.slice(s, n).ok_or_else(|| FormatError::Truncated("string".into()))?).into_owned())
let bytes = self.r.slice(s, n).ok_or_else(|| FormatError::Truncated("string".into()))?;
self.spend(lossy_utf8_len(bytes))?;
Value::String(String::from_utf8_lossy(bytes).into_owned())
}
0x6 => {
let (n, s) = self.count(at, lo)?;
@@ -201,6 +243,7 @@ impl Bin<'_> {
.r
.slice(s, n.checked_mul(2).ok_or_else(|| FormatError::Malformed("string".into()))?)
.ok_or_else(|| FormatError::Truncated("string".into()))?;
self.spend(lossy_utf16_len(bytes))?;
let units: Vec<u16> = bytes.as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes(*c)).collect();
Value::String(String::from_utf16_lossy(&units))
}
@@ -213,9 +256,13 @@ impl Bin<'_> {
}
0xa | 0xc => {
let (n, s) = self.count(at, lo)?;
if n > self.offsets.len().saturating_mul(4).max(16) || s.saturating_add(n.saturating_mul(self.ref_size)) > self.r.len() {
// Writers store equal values once, so an array may name one object many times:
// only its references have to fit in the file. MAX_NODES and the byte budget bound
// the expansion.
if s.saturating_add(n.saturating_mul(self.ref_size)) > self.r.len() {
return Err(FormatError::Malformed("array larger than the file".into()));
}
self.room_for_nodes(n)?;
stack.push(idx);
let mut out = Vec::with_capacity(n);
for i in 0..n {
@@ -230,6 +277,7 @@ impl Bin<'_> {
if s.saturating_add(n.saturating_mul(self.ref_size).saturating_mul(2)) > self.r.len() {
return Err(FormatError::Malformed("dictionary larger than the file".into()));
}
self.room_for_nodes(n.saturating_mul(2))?;
stack.push(idx);
let mut out = Vec::with_capacity(n);
for i in 0..n {