From 92beeab713101dcc44203d5924922f16ab1a8a3c Mon Sep 17 00:00:00 2001 From: bbareth <179921368+BBareth@users.noreply.github.com> Date: Sat, 21 Nov 2026 21:49:40 +0100 Subject: [PATCH] RPF9: unify header diagnostics across scan modes --- crates/xcl/src/rpf.rs | 18 +--------------- crates/xcl/src/rpf_probe.rs | 17 ++++++++++++--- crates/xcl/tests/rpf_e2e.rs | 41 +++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 crates/xcl/tests/rpf_e2e.rs diff --git a/crates/xcl/src/rpf.rs b/crates/xcl/src/rpf.rs index 1089bcf..d8f2a2e 100644 --- a/crates/xcl/src/rpf.rs +++ b/crates/xcl/src/rpf.rs @@ -135,24 +135,8 @@ pub fn run(path: &Path, args: &[String]) -> i32 { } let recs = pack::scan(&data, 0x40); if recs.is_empty() { - // no inline records: list the (nested) RPF9 headers and say whether the TOC is readable - let heads: Vec = data.windows(4).enumerate().filter(|(i, w)| i % 0x800 == 0 && *w == pack::RPF9_MAGIC).map(|(i, _)| i).collect(); - let toc = |h: usize| data.get(h + 0x40..h + 0x60).map(|b| b.to_vec()).unwrap_or_default(); - let same = heads.len() > 1 && heads.iter().all(|&h| toc(h) == toc(heads[0])); println!("{}: {:.1} MB, no records with plain names", path.display(), data.len() as f64 / 1048576.0); - println!("RPF9 archives (headers every 2 KB): {}", heads.len()); - for &h in heads.iter().take(top) { - let fields = h.checked_add(4).and_then(|start| h.checked_add(16).and_then(|end| data.get(start..end))); - if let Some(fields) = fields { - let w = |o: usize| u32::from_le_bytes(fields[o..o + 4].try_into().unwrap()); - println!(" {h:#010x}: fields {:#x} {:#x} {:#x}", w(0), w(4), w(8)); - } else { - println!(" {h:#010x}: truncated header"); - } - } - if same { - println!("TOC encrypted: every archive starts its table of contents with the same bytes (a fixed key from the game) – unreadable without the exe"); - } + crate::rpf_probe::summarize(&data, top); return 0; } let (csum, usum): (usize, usize) = recs.iter().fold((0, 0), |a, r| (a.0 + r.csize, a.1 + r.usize)); diff --git a/crates/xcl/src/rpf_probe.rs b/crates/xcl/src/rpf_probe.rs index b2e5b77..c634d3e 100644 --- a/crates/xcl/src/rpf_probe.rs +++ b/crates/xcl/src/rpf_probe.rs @@ -1,4 +1,4 @@ -//! Bounded-memory structural probing for large RPF9 archives. +//! Bounded-memory structural probing for RPF9 archives. use std::io::{self, Read}; use std::path::Path; @@ -34,7 +34,7 @@ struct Probe { /// and a capped number of summaries. This intentionally does not interpret, /// decrypt, or extract the table of contents. fn inspect(reader: &mut impl Read, len: u64, top: usize) -> io::Result { - let mut buffer = vec![0u8; CHUNK_SIZE]; + let mut buffer = vec![0u8; len.min(CHUNK_SIZE as u64) as usize]; let mut base = 0u64; let mut headers = 0u64; let mut root_magic = false; @@ -149,6 +149,18 @@ pub(crate) fn run(path: &Path, top: usize) -> i32 { path.display(), len as f64 / 1048576.0 ); + print_summary(&probe, top); + 0 +} + +/// Report headers from archive data already read by the full scan. +pub(crate) fn summarize(mut data: &[u8], top: usize) { + let len = data.len() as u64; + let probe = inspect(&mut data, len, top).expect("reading from a slice cannot fail"); + print_summary(&probe, top); +} + +fn print_summary(probe: &Probe, top: usize) { println!("RPF9 archives (headers every 2 KB): {}", probe.headers); for header in &probe.shown { if let Some(fields) = header.fields { @@ -175,7 +187,6 @@ pub(crate) fn run(path: &Path, top: usize) -> i32 { ), TocEvidence::Unknown => println!("TOC encryption: unknown (insufficient complete header evidence)"), } - 0 } #[cfg(test)] diff --git a/crates/xcl/tests/rpf_e2e.rs b/crates/xcl/tests/rpf_e2e.rs new file mode 100644 index 0000000..b875b53 --- /dev/null +++ b/crates/xcl/tests/rpf_e2e.rs @@ -0,0 +1,41 @@ +use std::process::Command; +use std::time::{SystemTime, UNIX_EPOCH}; + +#[test] +fn full_scan_and_streaming_probe_agree_on_header_evidence() { + let nonce = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos(); + let archive = std::env::temp_dir().join(format!("xcl-rpf-{}-{nonce}.rpf", std::process::id())); + let mut data = vec![0; 0x860]; + data[..4].copy_from_slice(b"9FPR"); + data[0x800..0x804].copy_from_slice(b"9FPR"); + std::fs::write(&archive, data).unwrap(); + + let run = |mode| { + Command::new(env!("CARGO_BIN_EXE_xcl")) + .arg("rpf") + .arg(&archive) + .arg(mode) + .output() + }; + let full = run("--full"); + let probe = run("--probe"); + std::fs::remove_file(&archive).unwrap(); + + let summary = |output: std::process::Output| { + assert!(output.status.success(), "{:?}", output); + String::from_utf8(output.stdout) + .unwrap() + .lines() + .skip(1) // The banner describes the selected mode. + .collect::>() + .join("\n") + }; + let full = summary(full.unwrap()); + let probe = summary(probe.unwrap()); + assert_eq!(full, probe); + assert!(full.contains("RPF9 archives (headers every 2 KB): 2")); + assert!(full.contains("TOC encryption: unknown")); +}