From 26980ff331efa6ec2cb2b9093dc5a154a8f66bdb Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 11:38:06 +0000 Subject: [PATCH] Bound portable material detection --- crates/mcmat/README.md | 2 +- crates/mcmat/src/lib.rs | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/mcmat/README.md b/crates/mcmat/README.md index c14db09d7..074a0ac44 100644 --- a/crates/mcmat/README.md +++ b/crates/mcmat/README.md @@ -5,7 +5,7 @@ contains `material.json`, named texture maps and a source package. Applications parameter values and cook or render maps themselves; this crate does not depend on an app's model. It supports callers that read either version 1 or version 2 of the manifest, or both. -`Material::open` takes caller-provided version, entry-count, map-count, per-entry and total +`is_material` checks for a manifest with a bounded ZIP index; `Material::open` takes caller-provided version, entry-count, map-count, per-entry and total expanded-byte limits. Each subsequent `read` and `read_source` shares that budget, including repeated references to the same ZIP member. Duplicate member names are refused. The manifest preserves unknown fields at the top level and on maps; parameter, input and preset objects are diff --git a/crates/mcmat/src/lib.rs b/crates/mcmat/src/lib.rs index 2e38924b1..75b8709ef 100644 --- a/crates/mcmat/src/lib.rs +++ b/crates/mcmat/src/lib.rs @@ -69,6 +69,12 @@ pub enum Error { Archive(#[from] craft_zip::Error), } +/// Whether a bounded ZIP index contains a material manifest. This does not validate the manifest. +pub fn is_material(bytes: &[u8], max_entries: u64) -> bool { + Archive::open(bytes, craft_zip::Limits { max_entries, max_total_bytes: 0 }) + .is_ok_and(|archive| !archive.has_duplicate_names() && archive.contains(MANIFEST_ENTRY)) +} + /// Manifest and original archive, retaining one shared expanded-byte budget across reads. pub struct Material<'a> { pub manifest: Manifest, @@ -157,6 +163,8 @@ mod tests { for version in [1, 2] { let manifest = sample(version); let bytes = write(&manifest, &[("maps/base.png".into(), vec![1, 2, 3])], b"source").unwrap(); + assert!(is_material(&bytes, 100)); + assert!(!is_material(&bytes, 1)); let mut material = Material::open(&bytes, limits()).unwrap(); assert_eq!(material.manifest, manifest); assert_eq!(material.read("maps/base.png").unwrap(), [1, 2, 3]);