Import binary plist parser changes from Craft Anything 15de3661

This commit is contained in:
BFlat
2026-11-15 07:55:30 -05:00
committed by Dylan M. Taylor
parent 72ee990e03
commit feea4e316d
+15 -4
View File
@@ -102,13 +102,15 @@ pub fn parse_binary(data: &[u8]) -> Result<Value, FormatError> {
for i in 0..num {
let o = read_be(&r, table + i * off_size, off_size).ok_or_else(|| FormatError::Truncated("offset table".into()))?;
let o = usize::try_from(o).map_err(|_| FormatError::Malformed("object offset".into()))?;
if o >= table {
if o < 8 || o >= table {
return Err(FormatError::Malformed(format!("object {i} lies outside the object area")));
}
offsets.push(o);
}
let top = usize::try_from(top).map_err(|_| FormatError::Malformed("top object".into()))?;
let mut b = Bin { r, offsets, ref_size, nodes: 0 };
// 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 stack = Vec::new();
b.object(top, &mut stack)
}
@@ -127,7 +129,10 @@ impl Bin<'_> {
if m & 0xf0 != 0x10 {
return Err(FormatError::Malformed("bad extended length marker".into()));
}
let n = 1usize << (m & 0x0f).min(3);
if m & 0x0f > 3 {
return Err(FormatError::Malformed("extended length width exceeds 64 bits".into()));
}
let n = 1usize << (m & 0x0f);
let v = read_be(&self.r, at + 2, n).ok_or_else(|| FormatError::Truncated("length".into()))?;
let v = usize::try_from(v).map_err(|_| FormatError::Malformed("length".into()))?;
Ok((v, at + 2 + n))
@@ -161,7 +166,10 @@ impl Bin<'_> {
_ => Value::Null,
},
0x1 => {
let n = 1usize << lo.min(4);
if lo > 4 {
return Err(FormatError::Malformed("integer width exceeds 128 bits".into()));
}
let n = 1usize << lo;
let bytes = self.r.slice(at + 1, n).ok_or_else(|| FormatError::Truncated("integer".into()))?;
let v = match n {
8 => i128::from(i64::from_be_bytes(bytes.try_into().map_err(|_| FormatError::Truncated("integer".into()))?)),
@@ -197,6 +205,9 @@ impl Bin<'_> {
Value::String(String::from_utf16_lossy(&units))
}
0x8 => {
if lo > 7 {
return Err(FormatError::Malformed("uid width exceeds 64 bits".into()));
}
let n = usize::from(lo) + 1;
Value::Uid(read_be(&self.r, at + 1, n).ok_or_else(|| FormatError::Truncated("uid".into()))?)
}