diff --git a/src/cmd_trydecode.rs b/src/cmd_trydecode.rs new file mode 100644 index 0000000..6f30b05 --- /dev/null +++ b/src/cmd_trydecode.rs @@ -0,0 +1,74 @@ +use crate::util::{read_file_or_stdin, trim_vec}; +use crate::CmdExec; +use base58::FromBase58; +use base64::engine::general_purpose::{ + STANDARD, STANDARD_NO_PAD, + URL_SAFE, URL_SAFE_NO_PAD, +}; +use base64::Engine; +use clap::Args; +use rust_util::{iff, simple_error, XResult}; +use std::io::{stdout, Write}; + +#[derive(Debug, Args)] +pub struct CmdTryDecode { + /// Input file + #[arg(long, short = 'i')] + pub r#in: Option, +} + +impl CmdExec for CmdTryDecode { + fn exec(&mut self) -> XResult<()> { + let input = read_file_or_stdin(&self.r#in)?; + let input = trim_vec(&input); + + let mut success_decoded = false; + if let Ok(decoded) = hex::decode(input) { + stdout().write_all(b"HEX : ")?; + stdout().write_all(&decoded)?; + stdout().write_all(b"\n")?; + success_decoded = true; + } + if let Ok(s) = String::from_utf8(input.to_vec()) { + if let Ok(decoded) = s.trim().from_base58() { + stdout().write_all(b"BASE58: ")?; + stdout().write_all(&decoded)?; + stdout().write_all(b"\n")?; + success_decoded = true; + } + } + if let Ok(decoded) = URL_SAFE.decode(&input) { + stdout().write_all(b"BASE64: ")?; + stdout().write_all(&decoded)?; + stdout().write_all(b"\n")?; + success_decoded = true; + } + if !success_decoded { + if let Ok(decoded) = URL_SAFE_NO_PAD.decode(&input) { + stdout().write_all(b"BASE64: ")?; + stdout().write_all(&decoded)?; + stdout().write_all(b"\n")?; + success_decoded = true; + } + if !success_decoded { + if let Ok(decoded) = STANDARD.decode(&input) { + stdout().write_all(b"BASE64: ")?; + stdout().write_all(&decoded)?; + stdout().write_all(b"\n")?; + success_decoded = true; + } + if !success_decoded { + if let Ok(decoded) = STANDARD_NO_PAD.decode(&input) { + stdout().write_all(b"BASE64: ")?; + stdout().write_all(&decoded)?; + stdout().write_all(b"\n")?; + success_decoded = true; + } + } + } + } + + iff!(success_decoded, Ok(()), simple_error!("decode failed")) + } +} + diff --git a/src/main.rs b/src/main.rs index 1649b1a..bd65368 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,12 @@ mod cmd_hex; mod cmd_base64; mod cmd_base58; mod util; +mod cmd_trydecode; use crate::cmd_base58::CmdBase58; use crate::cmd_base64::CmdBase64; use crate::cmd_hex::CmdHex; +use crate::cmd_trydecode::CmdTryDecode; use clap::{Parser, Subcommand}; use rust_util::{failure, XResult}; @@ -32,6 +34,9 @@ enum Commands { /// Base58 encoding #[command(arg_required_else_help = true, short_flag = '5')] Base58(CmdBase58), + /// Try decode + #[command(arg_required_else_help = true, short_flag = 't')] + TryDecode(CmdTryDecode), } fn main() { @@ -47,5 +52,6 @@ fn inner_main() -> XResult<()> { Commands::Hex(mut cmd_hex) => cmd_hex.exec(), Commands::Base64(mut cmd_base64) => cmd_base64.exec(), Commands::Base58(mut cmd_base58) => cmd_base58.exec(), + Commands::TryDecode(mut cmd_try_decode) => cmd_try_decode.exec(), } }