Initial commit.

BinComp ported to Rust.
This commit is contained in:
Lyle Mantooth 2022-05-22 12:31:39 -04:00
commit c78df56306
Signed by: IslandUsurper
GPG key ID: 6DB52EAE123A5789
11 changed files with 267 additions and 0 deletions

13
.github/workflows/build_nix.yml vendored Normal file
View file

@ -0,0 +1,13 @@
name: "Build legacy Nix package on Ubuntu"
on:
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v12
- name: Building package
run: nix-build . -A defaultPackage.x86_64-linux

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

6
Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[workspace]
members = [
"enemize",
"bin_comp",
]

12
bin_comp/Cargo.toml Normal file
View 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
View 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 ----");
}

7
default.nix Normal file
View file

@ -0,0 +1,7 @@
(import (
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/99f1c2157fba4bfe6211a321fd0ee43199025dbf.tar.gz";
sha256 = "0x2jn3vrawwv9xp15674wjz9pixwjyj3j771izayl962zziivbx2"; }
) {
src = ./.;
}).defaultNix

9
enemize/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "enemize-rs"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
md5 = "0.7.0"

3
enemize/src/main.rs Normal file
View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

77
flake.lock Normal file
View file

@ -0,0 +1,77 @@
{
"nodes": {
"naersk": {
"inputs": {
"nixpkgs": "nixpkgs"
},
"locked": {
"lastModified": 1651574473,
"narHash": "sha256-wQhFORvRjo8LB2hTmETmv6cbyKGDPbfWqvZ/0chnDE4=",
"owner": "nix-community",
"repo": "naersk",
"rev": "f21309b38e1da0d61b881b6b6d41b81c1aed4e1d",
"type": "github"
},
"original": {
"owner": "nix-community",
"ref": "master",
"repo": "naersk",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1652574577,
"narHash": "sha256-MoSWPtue4Wi9+kRDxUbLWEBCL8Bswaa8kVMh2JYpSJg=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "118ec238bfb788a34f1d53c4d95931fadfa70367",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1652574577,
"narHash": "sha256-MoSWPtue4Wi9+kRDxUbLWEBCL8Bswaa8kVMh2JYpSJg=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "118ec238bfb788a34f1d53c4d95931fadfa70367",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"naersk": "naersk",
"nixpkgs": "nixpkgs_2",
"utils": "utils"
}
},
"utils": {
"locked": {
"lastModified": 1652557277,
"narHash": "sha256-jSes9DaIVMdmwBB78KkFUVrlDzawmD62vrUg0GS2500=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "12806d31a381e7cd169a6bac35590e7b36dc5fe5",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

27
flake.nix Normal file
View file

@ -0,0 +1,27 @@
{
inputs = {
asar.url = "file:/home/lyle/projects/asar";
naersk.url = "github:nix-community/naersk/master";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, utils, asar, naersk }:
utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
naersk-lib = pkgs.callPackage naersk { };
in
{
defaultPackage = naersk-lib.buildPackage ./.;
defaultApp = utils.lib.mkApp {
drv = self.defaultPackage."${system}";
};
devShell = with pkgs; mkShell {
buildInputs = [ asar cargo rustc rustfmt pre-commit rustPackages.clippy ];
RUST_SRC_PATH = rustPlatform.rustLibSrc;
};
});
}

7
shell.nix Normal file
View file

@ -0,0 +1,7 @@
(import (
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/99f1c2157fba4bfe6211a321fd0ee43199025dbf.tar.gz";
sha256 = "0x2jn3vrawwv9xp15674wjz9pixwjyj3j771izayl962zziivbx2"; }
) {
src = ./.;
}).shellNix