rpgmv_tool/command/
encrypt_png.rs1use anyhow::Context;
2use clap::Parser;
3use std::fs::File;
4use std::io::BufReader;
5use std::io::BufWriter;
6use std::io::Write;
7use std::path::PathBuf;
8
9#[derive(Debug, Parser)]
10#[command(about = "Encrypt a png")]
11pub struct Options {
12 #[arg(long = "input", short = 'i', help = "A png to encrypt")]
13 pub input: PathBuf,
14
15 #[arg(long = "output", short = 'o', help = "The output file")]
16 pub output: PathBuf,
17
18 #[arg(long = "key", short = 'k', help = "The key, as hex")]
19 pub key: String,
20}
21
22pub fn exec(options: Options) -> anyhow::Result<()> {
23 let input = File::open(&options.input).with_context(|| {
24 format!(
25 "failed to open \"{}\" for reading",
26 &options.input.display()
27 )
28 })?;
29 let mut input = BufReader::new(input);
30
31 let output = File::create(&options.output).with_context(|| {
32 format!(
33 "failed to open \"{}\" for writing",
34 &options.output.display()
35 )
36 })?;
37 let mut output = BufWriter::new(output);
38
39 let mut key = [0; 16];
40 base16ct::mixed::decode(&options.key, &mut key)
41 .with_context(|| format!("failed to decode hex \"{}\"", options.key))?;
42
43 let mut writer = rpgmvp::Writer::new(&mut output, key);
44 writer.write_header()?;
45 std::io::copy(&mut input, &mut writer)?;
46
47 output.flush().context("failed to flush")?;
48 let output = output.into_inner()?;
49 output.sync_all()?;
50
51 Ok(())
52}