diff --git a/Cargo.lock b/Cargo.lock index d96f0a7..2d9fb41 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -51,6 +51,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "anyhow" +version = "1.0.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" + [[package]] name = "bitflags" version = "1.3.2" @@ -76,6 +82,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "80672091db20273a15cf9fdd4e47ed43b5091ec9841bf4c6145c9dfbbcae09ed" dependencies = [ "clap_builder", + "clap_derive", + "once_cell", ] [[package]] @@ -91,6 +99,18 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_derive" +version = "4.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8cd2b2a819ad6eec39e8f1d6b53001af1e5469f8c177579cdaeb313115b825f" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "clap_lex" version = "0.5.0" @@ -139,6 +159,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.1" @@ -168,6 +194,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "itoa" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" + [[package]] name = "libc" version = "0.2.146" @@ -262,12 +294,29 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "ryu" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" + [[package]] name = "serde" version = "1.0.164" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e8c8cf938e98f769bc164923b06dce91cea1751522f46f8466461af04c9027d" +[[package]] +name = "serde_json" +version = "1.0.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdf3bf93142acad5821c99197022e170842cdbc1c30482b98750c688c640842a" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "strsim" version = "0.10.0" @@ -289,9 +338,10 @@ dependencies = [ name = "umskt" version = "0.1.0" dependencies = [ + "anyhow", "clap", "openssl", - "serde", + "serde_json", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 502a4d2..55e2ded 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -clap = "4.3.4" +anyhow = "1.0.71" +clap = { version = "4.3.4", features = ["derive"] } openssl = "0.10.54" -serde = "1.0.164" +serde_json = "1.0" diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..6c88f43 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,106 @@ +use std::{fs::File, io::BufReader, path::Path}; + +use anyhow::{anyhow, Result}; +use clap::Parser; +use serde_json::from_reader; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Mode { + Bink1998, + Bink2002, + ConfirmationId, +} + +impl Default for Mode { + fn default() -> Self { + Self::Bink1998 + } +} + +#[derive(Parser, Debug)] +#[command(author, about, long_about = None)] +pub struct Options { + /// Enable verbose output + #[arg(short, long)] + verbose: bool, + + /// Number of keys to generate + #[arg(short = 'n', long = "number", default_value = "1")] + num_keys: i32, + + /// Specify which keys file to load + #[arg(short = 'f', long = "file", default_value = "keys.json")] + keys_filename: String, + + /// Installation ID used to generate confirmation ID + #[arg(short, long)] + instid: Option, + + /// Specify which BINK identifier to load + #[arg(short, long, default_value = "2E")] + binkid: String, + + /// Show which products/binks can be loaded + #[arg(short, long)] + list: bool, + + /// Specify which Channel Identifier to use + #[arg(short = 'c', long = "channel", default_value = "640")] + channel_id: i32, + + #[clap(skip)] + application_mode: Mode, +} + +pub fn load_json>(path: P) -> Result { + let file = File::open(path)?; + let reader = BufReader::new(file); + let json = from_reader(reader)?; + Ok(json) +} + +pub fn parse_command_line() -> Result { + let mut args = Options::parse(); + if args.instid.is_some() { + args.application_mode = Mode::ConfirmationId; + } + Ok(args) +} + +pub fn validate_command_line(options: &mut Options) -> Result { + if options.verbose { + println!("Loading keys file {}", options.keys_filename); + } + + let keys = load_json(&options.keys_filename)?; + + if options.verbose { + println!("Loaded keys from {} successfully", options.keys_filename); + } + + if options.list { + let products = keys["Products"].as_object().ok_or(anyhow!( + "`Products` object not found in {}", + options.keys_filename + ))?; + for (key, value) in products.iter() { + println!("{}: {}", key, value["BINK"]); + } + + println!("\n\n** Please note: any BINK ID other than 2E is considered experimental at this time **\n"); + } + + let bink_id = u32::from_str_radix(&options.binkid, 16)?; + + if bink_id >= 0x40 { + options.application_mode = Mode::Bink2002; + } + + if options.channel_id > 999 { + return Err(anyhow!( + "Refusing to create a key with a Channel ID greater than 999" + )); + } + + Ok(keys) +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..d918138 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,10 @@ -fn main() { +use anyhow::Result; + +mod cli; + +fn main() -> Result<()> { + let mut args = cli::parse_command_line()?; + let _keys = cli::validate_command_line(&mut args); println!("Hello, world!"); + Ok(()) }