Initial commit.
BinComp ported to Rust.
This commit is contained in:
commit
c78df56306
11 changed files with 267 additions and 0 deletions
12
bin_comp/Cargo.toml
Normal file
12
bin_comp/Cargo.toml
Normal file
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "bin_comp"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
tempfile = "3"
|
105
bin_comp/src/main.rs
Normal file
105
bin_comp/src/main.rs
Normal file
|
@ -0,0 +1,105 @@
|
|||
use std::env::args;
|
||||
use std::io::{self, Read, Write};
|
||||
use std::iter::zip;
|
||||
use std::process::Command;
|
||||
|
||||
use serde::Serialize;
|
||||
use tempfile::NamedTempfile;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Patch {
|
||||
pub address: usize,
|
||||
pub patch_data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Patch {
|
||||
fn new() -> Patch {
|
||||
Patch {
|
||||
address: 0,
|
||||
patch_data: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), anyhow::Error> {
|
||||
println!("BinComp ---- ");
|
||||
|
||||
let mut args = args();
|
||||
|
||||
let symbols_fn = args.next_back().expect("No symbols filename");
|
||||
let base_patch = args.next_back().expect("No basePatch filename");
|
||||
let asm_fn = args.next_back().expect("No input ASM filename");
|
||||
|
||||
let mut zeroes_patch = vec![];
|
||||
|
||||
let (mut zeroes_file, zeroes_path) = NamedTempfile::new()?;
|
||||
let (mut ones_file, ones_path) NamedTempfile::new()?;
|
||||
|
||||
println!("temp file (zeros): {}", zeroes_file.path());
|
||||
println!("temp file (0xFF): {}", ones_file.path());
|
||||
|
||||
run_asar(&asm_fn, zeroes_file.path(), &symbols);
|
||||
|
||||
println!("Reading all 0x00 file.");
|
||||
zeroes_file.read(&mut zeroes_patch)?;
|
||||
println!("Read all 0x00 file.");
|
||||
|
||||
println!("Generating all 0xFF file.");
|
||||
let mut all_ones = vec![0xff; zeroes_patch.len()];
|
||||
ones_file.write_all(&all_ones)?;
|
||||
ones_file.flush()?;
|
||||
println!("Generated all 0xFF file.");
|
||||
|
||||
run_asar(&asm_fn, ones_file.path(), &symbols);
|
||||
|
||||
println!("Reading all 0xFF patched file.");
|
||||
let mut ones_patch = vec![];
|
||||
ones_file.rewind()?.read_to_end(&mut ones_patch)?;
|
||||
println!("Read all 0xFF patched file.");
|
||||
|
||||
assert!(zeroes_patch.len() == ones_patch.len(), "File lengths don't match! Aborting.");
|
||||
|
||||
println!("");
|
||||
|
||||
let list = zip(zeroes_patch, ones_patch).enumerate().fold((true, Vec::new()), |(start, list), &(i, (zero, ff))| {
|
||||
if zero == ff {
|
||||
if start {
|
||||
println!("New patch data found at address {:X}", i);
|
||||
let patch = Patch { address: i, patch_data: vec![] };
|
||||
list.push(patch);
|
||||
}
|
||||
list.last_mut().and_then(|p| p.patch_data.push(zero));
|
||||
|
||||
(false, list)
|
||||
} else {
|
||||
assert!(zero == 0x00 && ff == 0xff, "Something went wrong. Zero file has non-zero or FF file has non-FF where files do not match!");
|
||||
|
||||
(true, list)
|
||||
}
|
||||
});
|
||||
|
||||
println!("Writing patch data file.");
|
||||
let mut bp_file = File::create(&base_patch)?;
|
||||
serde_json::to_writer(bp_file, &list)?;
|
||||
|
||||
println!("Cleaning up temp files.");
|
||||
println!("BinComp finished ----");
|
||||
}
|
||||
|
||||
fn run_asar(asm: &AsRef<PathBuf>, rom: &AsRef<PathBuf>, symbols: &AsRef<PathBuf>) {
|
||||
println!("---- Running asar --no-title-check --fix-checksum=off --symbols=wla --symbols-path={} {} {} ----", symbols, asm, rom);
|
||||
|
||||
let status = Command::new("asar")
|
||||
.arg("--no-title-check")
|
||||
.arg("--fix-checksum=off")
|
||||
.arg("--symbols=wla")
|
||||
.arg(format!("--symbols-path={}", symbols))
|
||||
.arg(asm)
|
||||
.arg(rom)
|
||||
.status()
|
||||
.expect("Failed to run asar");
|
||||
|
||||
assert!(status.success(), "asar threw errors");
|
||||
|
||||
println!("---- asar finished ----");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue