mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
Add bounded selective plist array lookup for locale parity
This commit is contained in:
1 parent
1ec7ab92b5
commit
cc4fc29cdb
3 files changed
+78
-4
No files matched your search
@@ -75,6 +75,12 @@ pub fn parse_binary(data: &[u8]) -> Result<Value, FormatError> {
|
||||
|
||||
/// Parse a binary property list, with caller-selected input, object, expansion and nesting limits.
|
||||
pub fn parse_binary_with_limits(data: &[u8], limits: Limits) -> Result<Value, FormatError> {
|
||||
let (mut b, top) = reader(data, limits)?;
|
||||
let mut stack = reserved(limits.max_depth.min(256))?;
|
||||
b.object(top, &mut stack)
|
||||
}
|
||||
|
||||
fn reader(data: &[u8], limits: Limits) -> Result<(Bin<'_>, usize), FormatError> {
|
||||
if data.len() > limits.max_input_bytes {
|
||||
return Err(FormatError::TooLarge("input bytes".into()));
|
||||
}
|
||||
@@ -122,9 +128,8 @@ pub fn parse_binary_with_limits(data: &[u8], limits: Limits) -> Result<Value, Fo
|
||||
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, bytes_left: limits.max_expanded_bytes, limits };
|
||||
let mut stack = reserved(limits.max_depth.min(256))?;
|
||||
b.object(top, &mut stack)
|
||||
let b = Bin { r: Reader::new(objects), offsets, ref_size, nodes: 0, bytes_left: limits.max_expanded_bytes, limits };
|
||||
Ok((b, top))
|
||||
}
|
||||
|
||||
fn read_be(r: &Reader<'_>, off: usize, n: usize) -> Option<u64> {
|
||||
@@ -333,3 +338,54 @@ fn reserved_string(n: usize) -> Result<String, FormatError> {
|
||||
s.try_reserve_exact(n).map_err(|_| FormatError::TooLarge("string allocation".into()))?;
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
/// Select the first string in one top-level dictionary array without expanding unrelated values.
|
||||
/// First matching dictionary key wins. Missing/wrong-typed values return `None`; malformed
|
||||
/// selected objects and resource exhaustion return errors. Unselected value graphs are not
|
||||
/// validated. This permits preference readers to ignore unrelated future object kinds.
|
||||
pub fn first_string_in_array(data: &[u8], key: &str, limits: Limits) -> Result<Option<String>, FormatError> {
|
||||
let (mut b, top) = reader(data, limits)?;
|
||||
let (kind, n, start) = b.header(top)?;
|
||||
if kind != 0xd {
|
||||
return Ok(None);
|
||||
}
|
||||
if n > limits.max_nodes {
|
||||
return Err(FormatError::TooLarge("dictionary lookup entries".into()));
|
||||
}
|
||||
let bytes = n.checked_mul(2).and_then(|n| n.checked_mul(b.ref_size)).ok_or_else(|| FormatError::Malformed("dictionary references".into()))?;
|
||||
b.r.slice(start, bytes).ok_or_else(|| FormatError::Truncated("dictionary references".into()))?;
|
||||
for i in 0..n {
|
||||
let k = b.reference(start, i)?;
|
||||
if b.selected_string(k)?.as_deref() != Some(key) {
|
||||
continue;
|
||||
}
|
||||
let value = b.reference(start, n + i)?;
|
||||
let (kind, count, payload) = b.header(value)?;
|
||||
if kind != 0xa || count == 0 {
|
||||
return Ok(None);
|
||||
}
|
||||
let first = b.reference(payload, 0)?;
|
||||
return b.selected_string(first);
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
impl Bin<'_> {
|
||||
fn header(&self, idx: usize) -> Result<(u8, usize, usize), FormatError> {
|
||||
let at = *self.offsets.get(idx).ok_or_else(|| FormatError::Malformed("missing object".into()))?;
|
||||
let marker = self.r.u8(at).ok_or_else(|| FormatError::Truncated("object marker".into()))?;
|
||||
let kind = marker >> 4;
|
||||
// Only containers and strings use an extended length. Unknown types are not traversed.
|
||||
let (n, start) = if matches!(kind, 0x5 | 0x6 | 0xa | 0xd) { self.count(at, marker & 15)? } else { (usize::from(marker & 15), at + 1) };
|
||||
Ok((kind, n, start))
|
||||
}
|
||||
fn selected_string(&mut self, idx: usize) -> Result<Option<String>, FormatError> {
|
||||
let (kind, _, _) = self.header(idx)?;
|
||||
if !matches!(kind, 0x5 | 0x6) {
|
||||
return Ok(None);
|
||||
}
|
||||
match self.object(idx, &mut Vec::new())? {
|
||||
Value::String(s) => Ok(Some(s)),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
mod binary;
|
||||
mod bytes;
|
||||
pub use binary::{DateValue, Value, parse_binary, parse_binary_with_limits};
|
||||
pub use binary::{DateValue, Value, first_string_in_array, parse_binary, parse_binary_with_limits};
|
||||
|
||||
/// Caller-selected resource limits. Zero values refuse the corresponding resource.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
|
||||
@@ -55,3 +55,21 @@ fn utf16_and_lossy_expansion_spend_budget() {
|
||||
assert_eq!(parse_binary(&bad).unwrap().as_str(), Some("�"));
|
||||
assert!(parse_binary_with_limits(&bad, Limits { max_expanded_bytes: 2, ..Limits::default() }).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selective_lookup_ignores_unrelated_graphs_and_preserves_first_match() {
|
||||
use craft_plist::first_string_in_array;
|
||||
let b = plist(&[&[0xd2, 1, 2, 3, 4], b"\x55Other", b"\x5eAppleLanguages", &[0xaf], &[0xa2, 5, 6], b"\x55ja-JP", &[0xff]]);
|
||||
assert!(parse_binary(&b).is_err());
|
||||
assert_eq!(first_string_in_array(&b, "AppleLanguages", Limits::default()).unwrap().as_deref(), Some("ja-JP"));
|
||||
let duplicate = plist(&[&[0xd2, 1, 1, 2, 3], b"\x5eAppleLanguages", &[0xa1, 4], &[0xa1, 5], b"\x55zh-TW", b"\x55ko-KR"]);
|
||||
assert_eq!(first_string_in_array(&duplicate, "AppleLanguages", Limits::default()).unwrap().as_deref(), Some("zh-TW"));
|
||||
assert!(first_string_in_array(&b, "AppleLanguages", Limits { max_expanded_bytes: 1, ..Limits::default() }).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selective_lookup_requires_an_array_not_a_set() {
|
||||
use craft_plist::first_string_in_array;
|
||||
let b = plist(&[&[0xd1, 1, 2], b"\x5eAppleLanguages", &[0xc1, 3], b"\x55ja-JP"]);
|
||||
assert_eq!(first_string_in_array(&b, "AppleLanguages", Limits::default()).unwrap(), None);
|
||||
}
|
||||
Reference in new issue
Block a user