Merge pull request #1585 from storytold/dot/plist-binary-v2

Extract bounded binary Apple property-list reader into craft-plist
This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-15 09:32:06 -05:00
commit e2e3b4213a
13 files changed
+849

No files matched your search

+4
View File
@@ -95,3 +95,7 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso
| `crates/raw/docs/raf-corpus.sha256` (hashes only; the files are not bundled) | The photographers who contributed each file to raw.pixls.us | [raw.pixls.us](https://raw.pixls.us/) | CC0-1.0 |
| `crates/drawingml/src/preset_text.txt` | DeckCraft contributors (Nikita Rakitin); generated by `crates/drawingml/tests/fixtures/make_preset_guides.py` from the guide formulas of ECMA-376 Part 1 `presetShapeDefinitions.xml` | [storytold/deckcraft#650](https://github.com/storytold/deckcraft/pull/650) | MIT OR Apache-2.0 (the formulas are the format's definition in ECMA-376) |
| `crates/drawingml/tests/fixtures/preset_table.txt` | DeckCraft contributors (Nikita Rakitin); generated by `crates/drawingml/tests/fixtures/make_preset_table.py` from ECMA-376 Part 1 `presetShapeDefinitions.xml` | [storytold/deckcraft#650](https://github.com/storytold/deckcraft/pull/650) | MIT OR Apache-2.0 (the formulas are the format's definition in ECMA-376) |
## Binary Apple property lists
`crates/plist` is extracted from `storytold/craft-anything` (`crates/formats/src/plist.rs` and checked byte reads), at source main `88d1d9cac852aec1c7fd97886db451e005e49d2a`. MIT OR Apache-2.0; source license texts are retained in the crate. Primary import Git authors preserve Brandon Thomas, BFlat, RRFZ and Afloat. Regression imports preserve BFlat. XML, JSON projection, calendar formatting and app policy were not imported into the public API.
Generated
+7
View File
@@ -2375,6 +2375,13 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-plist"
version = "0.1.0"
dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-platform"
version = "0.1.0"
+1
View File
@@ -17,6 +17,7 @@ craft-proto = { path = "crates/proto" }
acadrust = "=0.6.6"
log = "0.4"
# internal: add each crate here as `craft-<name> = { path = "crates/<name>" }`
craft-plist = { path = "crates/plist" }
craft-platform = { path = "crates/platform" }
craft-aep = { path = "crates/aep" }
craft-affinity = { path = "crates/affinity" }
+14
View File
@@ -0,0 +1,14 @@
[package]
name = "craft-plist"
description = "Bounded cross-platform binary Apple property-list reader"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
repository.workspace = true
[lints]
workspace = true
[dependencies]
thiserror.workspace = true
+177
View File
@@ -0,0 +1,177 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 ArtCraft Team and the Craft Anything contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+11
View File
@@ -0,0 +1,11 @@
# craft-plist
A cross-platform bounded binary (`bplist00`) reader, extracted from Craft Anything's existing implementation and hardening history. Typed dictionaries retain order and duplicate keys; `get` returns the first match. Strings retain the source reader's lossy decoding policy. Shared references expand under node and decoded-byte budgets, and cycles are rejected.
`parse_binary_with_limits` accepts explicit input, object, expanded-node, nesting and decoded-byte limits. Recursion always has a hard ceiling of 256 even if a caller supplies a larger depth. Counts are checked before container reservation; allocations can return a resource error. Limits are per parse, not a promise of a precise process RSS cap.
This first PR contains reading only. XML parsing/writing and keyed-archive interpretation are not implemented. JSON projection, date formatting, OS file access and preference/locale selection are not part of this crate. Existing updater/Photo streaming `plist` users remain unchanged.
Initial consumers: PhotoCraft's AppleLanguages binary-reader path; later Craft Anything binary parsing and LightCraft XML presets (after XML support/parity). Photo must retain its 4 MiB input policy and first-language selection.
Source: `storytold/craft-anything` at `88d1d9cac852aec1c7fd97886db451e005e49d2a`, `crates/formats/src/plist.rs` and checked byte reader. Source license files retained. Imported changes retain Brandon Thomas, BFlat, RRFZ and Afloat as primary Git authors; imported regression tests are BFlat's. No external specification implementation copied.
+391
View File
@@ -0,0 +1,391 @@
use crate::{FormatError, Limits, bytes::Reader};
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
Dict(Vec<(String, Value)>),
Array(Vec<Value>),
String(String),
Integer(i128),
Real(f64),
Bool(bool),
/// Seconds since 2001-01-01T00:00:00Z (binary), or the ISO text (XML).
Date(DateValue),
Data(Vec<u8>),
/// A keyed-archive object reference.
Uid(u64),
Null,
}
#[derive(Debug, Clone, PartialEq)]
pub enum DateValue {
Seconds(f64),
Text(String),
}
impl Value {
pub fn get(&self, key: &str) -> Option<&Value> {
match self {
Value::Dict(d) => d.iter().find(|(k, _)| k == key).map(|(_, v)| v),
_ => None,
}
}
pub fn as_str(&self) -> Option<&str> {
match self {
Value::String(s) => Some(s),
_ => None,
}
}
pub fn as_array(&self) -> Option<&[Value]> {
match self {
Value::Array(a) => Some(a),
_ => None,
}
}
pub fn as_uid(&self) -> Option<u64> {
match self {
Value::Uid(u) => Some(*u),
_ => None,
}
}
pub fn as_int(&self) -> Option<i128> {
match self {
Value::Integer(i) => Some(*i),
_ => None,
}
}
}
struct Bin<'a> {
r: Reader<'a>,
offsets: Vec<usize>,
ref_size: usize,
nodes: usize,
/// Data and text bytes still allowed to be copied out.
bytes_left: usize,
limits: Limits,
}
/// Parse a `bplist00` binary property list.
pub fn parse_binary(data: &[u8]) -> Result<Value, FormatError> {
parse_binary_with_limits(data, Limits::default())
}
/// 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()));
}
if !data.starts_with(b"bplist") {
return Err(FormatError::Malformed("not a binary plist".into()));
}
if !data.starts_with(b"bplist00") {
return Err(FormatError::Unsupported("unknown binary plist version".into()));
}
let len = data.len();
if len < 8 + 32 {
return Err(FormatError::Truncated("binary plist trailer".into()));
}
let r = Reader::new(data);
let t = len - 32;
let off_size = usize::from(r.u8(t + 6).unwrap_or(0));
let ref_size = usize::from(r.u8(t + 7).unwrap_or(0));
let num = r.u64_be(t + 8).unwrap_or(0);
let top = r.u64_be(t + 16).unwrap_or(0);
let table = r.u64_be(t + 24).unwrap_or(0);
if !(1..=8).contains(&off_size) || !(1..=8).contains(&ref_size) {
return Err(FormatError::Malformed("bad offset or reference size".into()));
}
let num = usize::try_from(num).map_err(|_| FormatError::Malformed("object count".into()))?;
let table = usize::try_from(table).map_err(|_| FormatError::Malformed("offset table".into()))?;
if num == 0 || num > len {
return Err(FormatError::Malformed(format!("{num} objects in a {len}-byte file")));
}
if num > limits.max_objects {
return Err(FormatError::TooLarge("object count".into()));
}
let need = num.checked_mul(off_size).and_then(|n| n.checked_add(table)).ok_or_else(|| FormatError::Malformed("offset table".into()))?;
if need > t || table < 8 {
return Err(FormatError::Truncated("offset table runs past the trailer".into()));
}
let mut offsets = reserved(num)?;
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 < 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()))?;
// 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 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> {
let s = r.slice(off, n)?;
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 {
return Ok((usize::from(low), at + 1));
}
let m = self.r.u8(at + 1).ok_or_else(|| FormatError::Truncated("length".into()))?;
if m & 0xf0 != 0x10 {
return Err(FormatError::Malformed("bad extended length marker".into()));
}
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))
}
fn reference(&self, at: usize, i: usize) -> Result<usize, FormatError> {
let off = i.checked_mul(self.ref_size).and_then(|x| x.checked_add(at)).ok_or_else(|| FormatError::Malformed("reference".into()))?;
let v = read_be(&self.r, off, self.ref_size).ok_or_else(|| FormatError::Truncated("object reference".into()))?;
usize::try_from(v).map_err(|_| FormatError::Malformed("reference".into()))
}
/// Refuse a container whose `children` would pass the node limit, before space is reserved for them.
fn room_for_nodes(&self, children: usize) -> Result<(), FormatError> {
if children > self.limits.max_nodes.saturating_sub(self.nodes) {
return Err(FormatError::TooLarge("expanded node count".into()));
}
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 > self.limits.max_nodes {
return Err(FormatError::TooLarge("expanded node count".into()));
}
if stack.len() >= self.limits.max_depth.min(256) {
return Err(FormatError::Malformed("nesting too deep".into()));
}
if stack.contains(&idx) {
return Err(FormatError::Malformed(format!("object {idx} contains itself")));
}
let at = *self.offsets.get(idx).ok_or_else(|| FormatError::Malformed(format!("reference to missing object {idx}")))?;
let m = self.r.u8(at).ok_or_else(|| FormatError::Truncated("object marker".into()))?;
let (hi, lo) = (m >> 4, m & 0x0f);
let v = match hi {
0x0 => match lo {
0x0 => Value::Null,
0x8 => Value::Bool(false),
0x9 => Value::Bool(true),
_ => return Err(FormatError::Malformed(format!("unknown simple plist object {m:#04x}"))),
},
0x1 => {
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()))?)),
16 => i128::from_be_bytes(bytes.try_into().map_err(|_| FormatError::Truncated("integer".into()))?),
_ => i128::from(bytes.iter().fold(0u64, |acc, &b| (acc << 8) | u64::from(b))),
};
Value::Integer(v)
}
0x2 => {
let v = match lo {
2 => self.r.u32_be(at + 1).map(|b| f64::from(f32::from_bits(b))),
3 => self.r.u64_be(at + 1).map(f64::from_bits),
_ => None,
};
Value::Real(v.ok_or_else(|| FormatError::Truncated("real".into()))?)
}
0x3 if lo == 3 => {
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)?;
let bytes = self.r.slice(s, n).ok_or_else(|| FormatError::Truncated("data".into()))?;
self.spend(bytes.len())?;
{
let mut out = reserved(bytes.len())?;
out.extend_from_slice(bytes);
Value::Data(out)
}
}
0x5 => {
let (n, s) = self.count(at, lo)?;
let bytes = self.r.slice(s, n).ok_or_else(|| FormatError::Truncated("string".into()))?;
let size = lossy_utf8_len(bytes);
self.spend(size)?;
let mut out = reserved_string(size)?;
for chunk in bytes.utf8_chunks() {
out.push_str(chunk.valid());
if !chunk.invalid().is_empty() {
out.push(char::REPLACEMENT_CHARACTER);
}
}
Value::String(out)
}
0x6 => {
let (n, s) = self.count(at, lo)?;
let bytes = self
.r
.slice(s, n.checked_mul(2).ok_or_else(|| FormatError::Malformed("string".into()))?)
.ok_or_else(|| FormatError::Truncated("string".into()))?;
let size = lossy_utf16_len(bytes);
self.spend(size)?;
let mut out = reserved_string(size)?;
let units = bytes.as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes(*c));
for ch in char::decode_utf16(units) {
out.push(ch.unwrap_or(char::REPLACEMENT_CHARACTER));
}
Value::String(out)
}
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()))?)
}
0xa | 0xc => {
let (n, s) = self.count(at, lo)?;
// Writers store equal values once, so an array may name one object many times:
// only its references have to fit in the file. the node limit 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 = reserved(n)?;
for i in 0..n {
let child = self.reference(s, i)?;
out.push(self.object(child, stack)?);
}
stack.pop();
Value::Array(out)
}
0xd => {
let (n, s) = self.count(at, lo)?;
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 = reserved(n)?;
for i in 0..n {
let k = self.reference(s, i)?;
let v = self.reference(s, i + n)?;
let key = match self.object(k, stack)? {
Value::String(s) => s,
_ => return Err(FormatError::Unsupported("non-string dictionary keys cannot be represented in this plist view".into())),
};
out.push((key, self.object(v, stack)?));
}
stack.pop();
Value::Dict(out)
}
_ => return Err(FormatError::Malformed(format!("unknown object marker {m:#04x}"))),
};
Ok(v)
}
}
fn reserved<T>(n: usize) -> Result<Vec<T>, FormatError> {
let mut v = Vec::new();
v.try_reserve_exact(n).map_err(|_| FormatError::TooLarge("allocation".into()))?;
Ok(v)
}
fn reserved_string(n: usize) -> Result<String, FormatError> {
let mut s = String::new();
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),
}
}
}
+37
View File
@@ -0,0 +1,37 @@
//! Checked little/big-endian reads over untrusted bytes. Every read returns `None` when out of range.
#[derive(Debug, Clone, Copy)]
pub struct Reader<'a> {
data: &'a [u8],
}
impl<'a> Reader<'a> {
pub fn new(data: &'a [u8]) -> Self {
Reader { data }
}
pub fn len(&self) -> usize {
self.data.len()
}
pub fn slice(&self, off: usize, len: usize) -> Option<&'a [u8]> {
let end = off.checked_add(len)?;
self.data.get(off..end)
}
pub fn u8(&self, off: usize) -> Option<u8> {
self.data.get(off).copied()
}
fn arr<const N: usize>(&self, off: usize) -> Option<[u8; N]> {
self.slice(off, N)?.try_into().ok()
}
pub fn u32_be(&self, off: usize) -> Option<u32> {
self.arr(off).map(u32::from_be_bytes)
}
pub fn u64_be(&self, off: usize) -> Option<u64> {
self.arr(off).map(u64::from_be_bytes)
}
}
+33
View File
@@ -0,0 +1,33 @@
//! Bounded binary Apple property lists, independent of operating-system preferences.
//! XML and writing are intentionally not implemented in this first extraction.
//! File discovery, locale selection, bundle validation and application policy stay in callers.
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
mod binary;
mod bytes;
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)]
pub struct Limits {
pub max_input_bytes: usize,
pub max_objects: usize,
pub max_nodes: usize,
pub max_depth: usize,
pub max_expanded_bytes: usize,
}
impl Default for Limits {
fn default() -> Self {
Self { max_input_bytes: 16 << 20, max_objects: 100_000, max_nodes: 100_000, max_depth: 64, max_expanded_bytes: 64 << 20 }
}
}
#[derive(Debug, thiserror::Error)]
pub enum FormatError {
#[error("malformed property list: {0}")]
Malformed(String),
#[error("truncated property list: {0}")]
Truncated(String),
#[error("unsupported property list: {0}")]
Unsupported(String),
#[error("property list resource limit: {0}")]
TooLarge(String),
}
+75
View File
@@ -0,0 +1,75 @@
use craft_plist::{Limits, Value, parse_binary, parse_binary_with_limits};
fn plist(objects: &[&[u8]]) -> Vec<u8> {
let mut b = b"bplist00".to_vec();
let mut offsets = Vec::new();
for obj in objects {
offsets.push(b.len() as u8);
b.extend_from_slice(obj);
}
let table = b.len() as u64;
b.extend_from_slice(&offsets);
b.extend_from_slice(&[0, 0, 0, 0, 0, 0, 1, 1]);
b.extend_from_slice(&(objects.len() as u64).to_be_bytes());
b.extend_from_slice(&0u64.to_be_bytes());
b.extend_from_slice(&table.to_be_bytes());
b
}
#[test]
fn languages_and_every_truncation() {
let b = plist(&[&[0xd1, 1, 2], b"\x5eAppleLanguages", &[0xa1, 3], b"\x55ja-JP"]);
let p = parse_binary(&b).unwrap();
assert_eq!(p.get("AppleLanguages").unwrap().as_array().unwrap()[0].as_str(), Some("ja-JP"));
for n in 0..b.len() {
assert!(parse_binary(&b[..n]).is_err(), "prefix {n}");
}
}
#[test]
fn cycles_and_repeated_references() {
assert!(parse_binary(&plist(&[&[0xa1, 0]])).is_err());
let b = plist(&[&[0xa3, 1, 1, 1], b"\x51x"]);
assert_eq!(parse_binary(&b).unwrap(), Value::Array(vec![Value::String("x".into()); 3]));
assert!(parse_binary_with_limits(&b, Limits { max_nodes: 3, ..Limits::default() }).is_err());
assert!(parse_binary_with_limits(&b, Limits { max_expanded_bytes: 2, ..Limits::default() }).is_err());
}
#[test]
fn explicit_limits_and_version() {
let b = plist(&[&[0xa1, 1], b"\x51x"]);
for l in [
Limits { max_input_bytes: b.len() - 1, ..Limits::default() },
Limits { max_objects: 1, ..Limits::default() },
Limits { max_depth: 1, ..Limits::default() },
Limits { max_nodes: 0, ..Limits::default() },
] {
assert!(parse_binary_with_limits(&b, l).is_err());
}
let mut future = b;
future[7] = b'1';
assert!(parse_binary(&future).is_err());
}
#[test]
fn utf16_and_lossy_expansion_spend_budget() {
let b = plist(&[&[0x62, 0xd8, 0x3d, 0xde, 0x00]]);
assert_eq!(parse_binary(&b).unwrap().as_str(), Some("😀"));
assert!(parse_binary_with_limits(&b, Limits { max_expanded_bytes: 3, ..Limits::default() }).is_err());
let bad = plist(&[&[0x51, 0xff]]);
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);
}
+47
View File
@@ -0,0 +1,47 @@
use craft_plist::{Value, parse_binary};
fn single_object(object: &[u8]) -> Vec<u8> {
let mut data = b"bplist00".to_vec();
data.extend_from_slice(object);
let table = data.len() as u64;
data.push(8);
data.extend_from_slice(&[0, 0, 0, 0, 0, 0, 1, 1]);
data.extend_from_slice(&1u64.to_be_bytes());
data.extend_from_slice(&0u64.to_be_bytes());
data.extend_from_slice(&table.to_be_bytes());
data
}
#[test]
fn unsupported_integer_widths_are_not_clamped() {
for marker in 0x15..=0x1f {
let mut object = vec![marker];
object.extend_from_slice(&[0; 16]);
assert!(parse_binary(&single_object(&object)).is_err(), "accepted {marker:#x}");
}
}
#[test]
fn extended_lengths_and_uids_do_not_lose_high_bytes() {
let mut length = vec![0x4f, 0x14];
length.extend_from_slice(&[0; 16]);
assert!(parse_binary(&single_object(&length)).is_err());
for marker in 0x88..=0x8f {
let mut object = vec![marker, 1];
object.resize(usize::from(marker & 15) + 2, 0);
assert!(parse_binary(&single_object(&object)).is_err(), "accepted {marker:#x}");
}
}
#[test]
fn supported_maximum_widths_keep_their_values() {
let mut integer = vec![0x14];
integer.extend_from_slice(&i128::MAX.to_be_bytes());
assert!(matches!(parse_binary(&single_object(&integer)), Ok(Value::Integer(i)) if i == i128::MAX));
let mut uid = vec![0x87];
uid.extend_from_slice(&u64::MAX.to_be_bytes());
assert!(matches!(parse_binary(&single_object(&uid)), Ok(Value::Uid(i)) if i == u64::MAX));
let mut empty = vec![0x4f, 0x13];
empty.extend_from_slice(&0u64.to_be_bytes());
assert!(matches!(parse_binary(&single_object(&empty)), Ok(Value::Data(d)) if d.is_empty()));
}
+31
View File
@@ -0,0 +1,31 @@
use craft_plist::{Value, parse_binary};
fn single_object(object: &[u8], offset: u8) -> Vec<u8> {
let mut data = b"bplist00".to_vec();
data.extend_from_slice(object);
let table = data.len() as u64;
data.push(offset);
data.extend_from_slice(&[0, 0, 0, 0, 0, 0, 1, 1]);
data.extend_from_slice(&1u64.to_be_bytes());
data.extend_from_slice(&0u64.to_be_bytes());
data.extend_from_slice(&table.to_be_bytes());
data
}
#[test]
fn binary_values_cannot_consume_the_offset_table_or_trailer() {
for object in [&[0x54, b'x'][..], &[0x43, 0x42], &[0x13], &[0x87], &[0x62, 0, b'x']] {
assert!(parse_binary(&single_object(object, 8)).is_err(), "accepted object {object:02x?}");
}
}
#[test]
fn binary_offsets_cannot_point_inside_the_file_header() {
assert!(parse_binary(&single_object(&[0x00], 0)).is_err());
assert!(matches!(parse_binary(&single_object(&[0x00], 8)), Ok(Value::Null)));
}
#[test]
fn objects_ending_at_the_table_boundary_are_valid() {
assert!(matches!(parse_binary(&single_object(&[0x51, b'x'], 8)), Ok(Value::String(s)) if s == "x"));
}