Created
November 17, 2022 18:56
-
-
Save phako/8e5c274da4865562b33326fffdf4bb00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::VecDeque; | |
use std::env; | |
use std::fs; | |
use std::fs::File; | |
use std::io::prelude::*; | |
use std::io::{Error, ErrorKind}; | |
use std::path::Path; | |
fn help() { | |
println!("Usage: decrypt <in> <key> <out>"); | |
} | |
struct RingData(VecDeque<u8>); | |
// Implements an endless iterator over a set of data | |
impl Iterator for RingData { | |
type Item = u8; | |
fn next(&mut self) -> Option<Self::Item> { | |
match self.0.pop_front() { | |
Some(current) => { | |
self.0.push_back(current); | |
Some(current) | |
} | |
_ => None, | |
} | |
} | |
} | |
fn main() -> Result<(), Box<dyn std::error::Error + 'static>> { | |
let args: Vec<String> = env::args().collect(); | |
if args.len() != 4 { | |
help(); | |
return Ok(()); | |
} | |
let input_data = fs::read(&Path::new(&args[1]))?; | |
let header = &input_data[0..=10]; | |
if !(header.is_ascii() && std::str::from_utf8(&header)? == "AMIROMTYPE1") { | |
return Err(Box::new(Error::new( | |
ErrorKind::InvalidData, | |
"Unsupported ROM file", | |
))); | |
} | |
let mut out_data = Vec::new(); | |
let key = RingData(VecDeque::from(fs::read(&Path::new(&args[2]))?)); | |
for it in input_data[11..].iter().zip(key) { | |
let (a, b) = it; | |
out_data.push(a ^ b); | |
} | |
let mut out = File::create(&Path::new(&args[3]))?; | |
out.write_all(&out_data)?; | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment