Skip to content

Commit

Permalink
Parameterize csc
Browse files Browse the repository at this point in the history
  • Loading branch information
widberg committed Nov 18, 2023
1 parent 9243416 commit cec488c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
10 changes: 5 additions & 5 deletions bff-cli/src/csc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ use bff::BufReader;
use crate::error::BffCliResult;
use crate::stdio_or_path::StdioOrPath;

pub fn csc(input: &StdioOrPath, output: &StdioOrPath) -> BffCliResult<()> {
pub fn csc(input: &StdioOrPath, output: &StdioOrPath, key: &u8) -> BffCliResult<()> {
match (input, output) {
(StdioOrPath::Stdio, StdioOrPath::Stdio) => {
let stdin = BufReader::new(io::stdin().lock());
let mut stdout = BufWriter::new(io::stdout().lock());

csc_copy(stdin, &mut stdout)?;
csc_copy(stdin, &mut stdout, *key)?;
}
(StdioOrPath::Stdio, StdioOrPath::Path(path)) => {
let stdin = BufReader::new(io::stdin().lock());
let mut stdout = BufWriter::new(File::create(path)?);

csc_copy(stdin, &mut stdout)?;
csc_copy(stdin, &mut stdout, *key)?;
}
(StdioOrPath::Path(path), StdioOrPath::Stdio) => {
let stdin = BufReader::new(File::open(path)?);
let mut stdout = BufWriter::new(io::stdout().lock());

csc_copy(stdin, &mut stdout)?;
csc_copy(stdin, &mut stdout, *key)?;
}
(StdioOrPath::Path(path), StdioOrPath::Path(path2)) => {
let stdin = BufReader::new(File::open(path)?);
let mut stdout = BufWriter::new(File::create(path2)?);

csc_copy(stdin, &mut stdout)?;
csc_copy(stdin, &mut stdout, *key)?;
}
}

Expand Down
4 changes: 3 additions & 1 deletion bff-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ enum Commands {
Csc {
input: StdioOrPath,
output: StdioOrPath,
#[arg(short, long, default_value_t = 255)]
key: u8,
},
#[clap(alias = "xpsc")]
ExtractPsc { psc: PathBuf, directory: PathBuf },
Expand Down Expand Up @@ -193,7 +195,7 @@ fn main() -> BffCliResult<()> {
character_set,
),
Commands::RoundTrip { bigfile } => round_trip::round_trip(bigfile),
Commands::Csc { input, output } => csc::csc(input, output),
Commands::Csc { input, output, key } => csc::csc(input, output, key),
Commands::ExtractPsc { psc, directory } => psc::extract_psc(psc, directory),
Commands::CreatePsc { directory, psc } => psc::create_psc(directory, psc),
Commands::ExtractFatLin {
Expand Down
8 changes: 4 additions & 4 deletions bff/src/tsc/csc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ use std::io::{Read, Write};

use crate::BffResult;

pub fn csc_copy<R: Read, W: Write>(reader: R, writer: &mut W) -> BffResult<()> {
pub fn csc_copy<R: Read, W: Write>(reader: R, writer: &mut W, key: u8) -> BffResult<()> {
for byte in reader.bytes() {
let byte = byte?;
writer.write_all(&[!byte])?;
writer.write_all(&[byte ^ key])?;
}

Ok(())
}

pub fn csc_buffer(data: &mut [u8]) {
pub fn csc_buffer(data: &mut [u8], key: u8) {
for byte in data {
*byte = !*byte;
*byte ^= key;
}
}

0 comments on commit cec488c

Please sign in to comment.