RPF9: unify header diagnostics across scan modes

This commit is contained in:
bbareth
2026-11-21 21:49:40 +01:00
parent fd746622e9
commit 92beeab713
3 changed files with 56 additions and 20 deletions
+1 -17
View File
@@ -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<usize> = 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));
+14 -3
View File
@@ -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<Probe> {
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)]
+41
View File
@@ -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::<Vec<_>>()
.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"));
}