feat: try deocde

This commit is contained in:
2024-11-19 01:14:27 +08:00
parent cd4f0646e4
commit 3687410ef3
2 changed files with 80 additions and 0 deletions

74
src/cmd_trydecode.rs Normal file
View File

@@ -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<String>,
}
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"))
}
}

View File

@@ -2,10 +2,12 @@ mod cmd_hex;
mod cmd_base64; mod cmd_base64;
mod cmd_base58; mod cmd_base58;
mod util; mod util;
mod cmd_trydecode;
use crate::cmd_base58::CmdBase58; use crate::cmd_base58::CmdBase58;
use crate::cmd_base64::CmdBase64; use crate::cmd_base64::CmdBase64;
use crate::cmd_hex::CmdHex; use crate::cmd_hex::CmdHex;
use crate::cmd_trydecode::CmdTryDecode;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use rust_util::{failure, XResult}; use rust_util::{failure, XResult};
@@ -32,6 +34,9 @@ enum Commands {
/// Base58 encoding /// Base58 encoding
#[command(arg_required_else_help = true, short_flag = '5')] #[command(arg_required_else_help = true, short_flag = '5')]
Base58(CmdBase58), Base58(CmdBase58),
/// Try decode
#[command(arg_required_else_help = true, short_flag = 't')]
TryDecode(CmdTryDecode),
} }
fn main() { fn main() {
@@ -47,5 +52,6 @@ fn inner_main() -> XResult<()> {
Commands::Hex(mut cmd_hex) => cmd_hex.exec(), Commands::Hex(mut cmd_hex) => cmd_hex.exec(),
Commands::Base64(mut cmd_base64) => cmd_base64.exec(), Commands::Base64(mut cmd_base64) => cmd_base64.exec(),
Commands::Base58(mut cmd_base58) => cmd_base58.exec(), Commands::Base58(mut cmd_base58) => cmd_base58.exec(),
Commands::TryDecode(mut cmd_try_decode) => cmd_try_decode.exec(),
} }
} }