From 88b68b4144859b586a881f5851e09362eccb92db Mon Sep 17 00:00:00 2001 From: bmjask <11672696+bmjask@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:10:40 -0500 Subject: [PATCH] Olympus: probe large RPF9 archives safely --- crates/xcl/src/lib.rs | 1 + crates/xcl/src/main.rs | 3 + crates/xcl/src/rpf.rs | 22 +++ crates/xcl/src/rpf_probe.rs | 293 ++++++++++++++++++++++++++++++++++++ 4 files changed, 319 insertions(+) create mode 100644 crates/xcl/src/rpf_probe.rs diff --git a/crates/xcl/src/lib.rs b/crates/xcl/src/lib.rs index d9ba072..fbeff57 100644 --- a/crates/xcl/src/lib.rs +++ b/crates/xcl/src/lib.rs @@ -23,6 +23,7 @@ pub mod pe; pub mod report; pub mod resolver; pub mod rpf; +mod rpf_probe; pub mod shaders; pub mod state; pub mod stubs; diff --git a/crates/xcl/src/main.rs b/crates/xcl/src/main.rs index a9cf22e..bd231ba 100644 --- a/crates/xcl/src/main.rs +++ b/crates/xcl/src/main.rs @@ -34,6 +34,9 @@ Usage: for the recompiler). RPF9 packages are scanned for shader records. --recomp: translates every RDNA2 program → HLSL and shows what is missing; --compile N: additionally compiles N complete translations with DXC. + xcl rpf [--top N] [--find NAME] [--extract NAME --out DIR] + Lists or extracts readable inline records. Archives larger than 1 GB use a + bounded-memory header probe; --probe requests it explicitly and --full overrides it. xcl --version "; diff --git a/crates/xcl/src/rpf.rs b/crates/xcl/src/rpf.rs index 64509a1..4932bae 100644 --- a/crates/xcl/src/rpf.rs +++ b/crates/xcl/src/rpf.rs @@ -12,6 +12,8 @@ use windows_sys::Win32::Storage::FileSystem::{MoveFileExW, MOVEFILE_WRITE_THROUG use crate::gpu::pack; +const AUTO_PROBE_SIZE: u64 = 1024 * 1024 * 1024; + /// Printable signature of unpacked data: 4 ASCII bytes or hex. fn magic(b: &[u8]) -> String { let m = &b[..b.len().min(4)]; @@ -100,6 +102,26 @@ pub fn run(path: &Path, args: &[String]) -> i32 { let find = opt("--find"); let extract = opt("--extract"); let out = opt("--out").map(PathBuf::from).unwrap_or_else(|| PathBuf::from("rpf_out")); + let len = match std::fs::metadata(path) { + Ok(metadata) => metadata.len(), + Err(error) => { + eprintln!("xcl rpf: {}: {error}", path.display()); + return 2; + } + }; + let explicit_probe = args.iter().any(|arg| arg == "--probe"); + let force_full = args.iter().any(|arg| arg == "--full"); + if explicit_probe { + return crate::rpf_probe::run(path, top); + } + if len > AUTO_PROBE_SIZE && !force_full { + if find.is_some() || extract.is_some() { + eprintln!("xcl rpf: detailed options on archives larger than 1 GB require --full"); + return 2; + } + println!("archive exceeds 1 GB; using bounded-memory probe (pass --full to request a detailed scan)"); + return crate::rpf_probe::run(path, top); + } let data = match std::fs::read(path) { Ok(d) => d, Err(e) => { diff --git a/crates/xcl/src/rpf_probe.rs b/crates/xcl/src/rpf_probe.rs new file mode 100644 index 0000000..b2e5b77 --- /dev/null +++ b/crates/xcl/src/rpf_probe.rs @@ -0,0 +1,293 @@ +//! Bounded-memory structural probing for large RPF9 archives. + +use std::io::{self, Read}; +use std::path::Path; + +use crate::gpu::pack; + +const CHUNK_SIZE: usize = 4 * 1024 * 1024; +const MAX_SHOWN_HEADERS: usize = 1024; + +#[derive(Debug, PartialEq, Eq)] +enum TocEvidence { + Encrypted, + Different, + Unknown, +} + +#[derive(Debug)] +struct HeaderProbe { + offset: u64, + fields: Option<[u32; 3]>, +} + +#[derive(Debug)] +struct Probe { + root_magic: bool, + headers: u64, + truncated_headers: u64, + shown: Vec, + toc_evidence: TocEvidence, +} + +/// Scan only RPF9 header locations while retaining one fixed-size input chunk +/// 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 base = 0u64; + let mut headers = 0u64; + let mut root_magic = false; + let mut truncated_headers = 0u64; + let shown_limit = top.min(MAX_SHOWN_HEADERS); + let mut shown = Vec::with_capacity(shown_limit); + let mut first_toc: Option<[u8; 32]> = None; + let mut complete_tocs = 0u64; + let mut all_tocs_same = true; + + while base < len { + let want = usize::try_from((len - base).min(CHUNK_SIZE as u64)).unwrap(); + let mut got = 0usize; + while got < want { + let count = reader.read(&mut buffer[got..want])?; + if count == 0 { + break; + } + got += count; + } + if got == 0 { + break; + } + + let chunk = &buffer[..got]; + for local in (0..got).step_by(0x800) { + if chunk.get(local..local + 4) != Some(pack::RPF9_MAGIC) { + continue; + } + headers += 1; + let offset = base + local as u64; + root_magic |= offset == 0; + let fields = chunk.get(local + 4..local + 16).map(|bytes| { + [ + u32::from_le_bytes(bytes[0..4].try_into().unwrap()), + u32::from_le_bytes(bytes[4..8].try_into().unwrap()), + u32::from_le_bytes(bytes[8..12].try_into().unwrap()), + ] + }); + let toc = chunk.get(local + 0x40..local + 0x60).map(|bytes| { + let mut value = [0u8; 32]; + value.copy_from_slice(bytes); + value + }); + if fields.is_none() || toc.is_none() { + truncated_headers += 1; + } + if shown.len() < shown_limit { + shown.push(HeaderProbe { offset, fields }); + } + if let Some(toc) = toc { + complete_tocs += 1; + if let Some(first) = first_toc { + all_tocs_same &= first == toc; + } else { + first_toc = Some(toc); + } + } + } + base += got as u64; + } + + let toc_evidence = if truncated_headers == 0 + && complete_tocs >= 2 + && all_tocs_same + && first_toc.is_some_and(|toc| toc.iter().any(|byte| *byte != 0)) + { + TocEvidence::Encrypted + } else if complete_tocs >= 2 && !all_tocs_same { + TocEvidence::Different + } else { + TocEvidence::Unknown + }; + Ok(Probe { + root_magic, + headers, + truncated_headers, + shown, + toc_evidence, + }) +} + +pub(crate) fn run(path: &Path, top: usize) -> i32 { + let mut file = match std::fs::File::open(path) { + Ok(file) => file, + Err(error) => { + eprintln!("xcl rpf: {}: {error}", path.display()); + return 2; + } + }; + let len = match file.metadata() { + Ok(metadata) => metadata.len(), + Err(error) => { + eprintln!("xcl rpf: {}: {error}", path.display()); + return 2; + } + }; + let probe = match inspect(&mut file, len, top) { + Ok(probe) => probe, + Err(error) => { + eprintln!("xcl rpf: {}: {error}", path.display()); + return 2; + } + }; + if !probe.root_magic { + eprintln!("xcl rpf: {} is not an RPF9 package", path.display()); + return 2; + } + + println!( + "{}: {:.1} MB, streaming header probe", + path.display(), + len as f64 / 1048576.0 + ); + println!("RPF9 archives (headers every 2 KB): {}", probe.headers); + for header in &probe.shown { + if let Some(fields) = header.fields { + println!( + " {:#010x}: fields {:#x} {:#x} {:#x}", + header.offset, fields[0], fields[1], fields[2] + ); + } else { + println!(" {:#010x}: truncated header", header.offset); + } + } + if top > MAX_SHOWN_HEADERS && probe.headers > MAX_SHOWN_HEADERS as u64 { + println!("header display capped at {MAX_SHOWN_HEADERS}"); + } + if probe.truncated_headers != 0 { + println!("truncated RPF9 headers: {}", probe.truncated_headers); + } + match probe.toc_evidence { + TocEvidence::Encrypted => println!( + "TOC encryption: confirmed indicator (multiple complete archives have the same nonzero TOC prefix)" + ), + TocEvidence::Different => println!( + "TOC encryption: not indicated by header prefixes; contents remain unclassified" + ), + TocEvidence::Unknown => println!("TOC encryption: unknown (insufficient complete header evidence)"), + } + 0 +} + +#[cfg(test)] +mod tests { + use super::*; + use std::collections::BTreeMap; + + struct SyntheticArchive { + len: u64, + position: u64, + bytes: BTreeMap, + max_read: usize, + } + + impl SyntheticArchive { + fn new(len: u64) -> Self { + Self { + len, + position: 0, + bytes: BTreeMap::new(), + max_read: 0, + } + } + + fn header(&mut self, offset: u64, toc: [u8; 32]) { + let mut header = [0u8; 0x60]; + header[..4].copy_from_slice(pack::RPF9_MAGIC); + header[4..8].copy_from_slice(&0x120u32.to_le_bytes()); + header[8..12].copy_from_slice(&0x100u32.to_le_bytes()); + header[12..16].copy_from_slice(&0x20u32.to_le_bytes()); + header[0x40..0x60].copy_from_slice(&toc); + for (index, byte) in header.into_iter().enumerate() { + self.bytes.insert(offset + index as u64, byte); + } + } + + fn magic_only(&mut self, offset: u64) { + for (index, byte) in pack::RPF9_MAGIC.iter().copied().enumerate() { + self.bytes.insert(offset + index as u64, byte); + } + } + } + + impl Read for SyntheticArchive { + fn read(&mut self, buffer: &mut [u8]) -> io::Result { + self.max_read = self.max_read.max(buffer.len()); + let count = + usize::try_from((self.len - self.position).min(buffer.len() as u64)).unwrap(); + buffer[..count].fill(0); + let end = self.position + count as u64; + for (&offset, &byte) in self.bytes.range(self.position..end) { + buffer[(offset - self.position) as usize] = byte; + } + self.position = end; + Ok(count) + } + } + + #[test] + fn bounds_reads_and_detects_repeated_nonzero_tocs() { + let logical_len = CHUNK_SIZE as u64 * 3 + 0x1000; + let mut archive = SyntheticArchive::new(logical_len); + archive.header(0, [0x5a; 32]); + archive.header(CHUNK_SIZE as u64 * 2 + 0x800, [0x5a; 32]); + + let probe = inspect(&mut archive, logical_len, 1).unwrap(); + assert!(probe.root_magic); + assert_eq!(probe.headers, 2); + assert_eq!(probe.shown.len(), 1); + assert_eq!(probe.toc_evidence, TocEvidence::Encrypted); + assert!(archive.max_read <= CHUNK_SIZE); + } + + #[test] + fn does_not_guess_from_one_header_or_unbounded_top() { + let mut archive = SyntheticArchive::new(0x1000); + archive.header(0, [0x33; 32]); + + let probe = inspect(&mut archive, 0x1000, usize::MAX).unwrap(); + assert!(probe.root_magic); + assert!(probe.shown.capacity() <= MAX_SHOWN_HEADERS); + assert_eq!(probe.toc_evidence, TocEvidence::Unknown); + } + + #[test] + fn keeps_different_or_zero_tocs_unclassified() { + let mut different = SyntheticArchive::new(0x1800); + different.header(0, [0x11; 32]); + different.header(0x800, [0x22; 32]); + assert_eq!( + inspect(&mut different, 0x1800, 2).unwrap().toc_evidence, + TocEvidence::Different + ); + + let mut zero = SyntheticArchive::new(0x1800); + zero.header(0, [0; 32]); + zero.header(0x800, [0; 32]); + assert_eq!( + inspect(&mut zero, 0x1800, 2).unwrap().toc_evidence, + TocEvidence::Unknown + ); + } + + #[test] + fn treats_truncated_headers_as_unknown() { + let mut archive = SyntheticArchive::new(0x804); + archive.header(0, [0x44; 32]); + archive.magic_only(0x800); + + let probe = inspect(&mut archive, 0x804, 2).unwrap(); + assert_eq!(probe.headers, 2); + assert_eq!(probe.truncated_headers, 1); + assert_eq!(probe.toc_evidence, TocEvidence::Unknown); + } +}