2023-01-21 18:25:09 -05:00
|
|
|
use super::{base32::args, Cmd};
|
|
|
|
use clap::{ArgMatches, Command};
|
2023-01-06 23:41:02 -05:00
|
|
|
use data_encoding::BASE64;
|
2023-01-03 23:02:43 -05:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
fs,
|
2023-01-04 15:14:16 -05:00
|
|
|
io::{self, Read, Write},
|
2023-01-03 23:02:43 -05:00
|
|
|
};
|
2023-01-04 15:14:16 -05:00
|
|
|
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
|
2023-01-03 23:02:43 -05:00
|
|
|
|
2023-01-13 01:08:32 -05:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct Base64;
|
2023-01-03 23:02:43 -05:00
|
|
|
|
|
|
|
impl Cmd for Base64 {
|
|
|
|
fn cli(&self) -> Command {
|
|
|
|
Command::new("base64")
|
|
|
|
.author("Nathan Fisher")
|
|
|
|
.about("Base64 encode/decode data and print to standard output")
|
2023-01-21 18:25:09 -05:00
|
|
|
.args(args())
|
2023-01-03 23:02:43 -05:00
|
|
|
}
|
|
|
|
|
2023-02-04 08:54:27 -05:00
|
|
|
fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
|
2023-01-04 15:14:16 -05:00
|
|
|
let color = match matches.get_one::<String>("color").map(String::as_str) {
|
|
|
|
Some("always") => ColorChoice::Always,
|
|
|
|
Some("ansi") => ColorChoice::AlwaysAnsi,
|
|
|
|
Some("auto") => {
|
|
|
|
if atty::is(atty::Stream::Stdout) {
|
|
|
|
ColorChoice::Auto
|
|
|
|
} else {
|
|
|
|
ColorChoice::Never
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ColorChoice::Never,
|
|
|
|
};
|
2023-01-27 00:15:04 -05:00
|
|
|
if let Some(files) = matches.get_many::<String>("INPUT") {
|
|
|
|
let (len, _) = files.size_hint();
|
|
|
|
for (index, file) in files.enumerate() {
|
|
|
|
if { len > 1 || matches.get_flag("VERBOSE") } && !matches.get_flag("QUIET") {
|
|
|
|
let mut stdout = StandardStream::stdout(color);
|
|
|
|
stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
|
|
|
|
match index {
|
|
|
|
0 => writeln!(stdout, "===> {file} <==="),
|
|
|
|
_ => writeln!(stdout, "\n===> {file} <==="),
|
|
|
|
}?;
|
|
|
|
stdout.reset()?;
|
|
|
|
} else if index > 0 {
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
let contents = get_contents(&file)?;
|
|
|
|
if matches.get_flag("DECODE") {
|
|
|
|
decode_base64(contents, matches.get_flag("IGNORE"))?;
|
|
|
|
} else {
|
|
|
|
encode_base64(
|
|
|
|
&contents,
|
|
|
|
match matches.get_one("WRAP") {
|
|
|
|
Some(c) => *c,
|
|
|
|
None => 76,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2023-01-03 23:02:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path(&self) -> Option<crate::Path> {
|
2023-01-13 01:08:32 -05:00
|
|
|
Some(crate::Path::UsrBin)
|
2023-01-03 23:02:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decode_base64(mut contents: String, ignore: bool) -> Result<(), Box<dyn Error>> {
|
|
|
|
if ignore {
|
|
|
|
contents.retain(|c| !c.is_whitespace());
|
|
|
|
} else {
|
|
|
|
contents = contents.replace('\n', "");
|
|
|
|
}
|
2023-01-13 01:08:32 -05:00
|
|
|
let decoded = BASE64.decode(contents.as_bytes())?;
|
2023-01-03 23:02:43 -05:00
|
|
|
let output = String::from_utf8(decoded)?;
|
2023-01-06 23:41:02 -05:00
|
|
|
println!("{}\n", output.trim_end());
|
2023-01-03 23:02:43 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encode_base64(contents: &str, wrap: usize) {
|
2023-01-06 23:41:02 -05:00
|
|
|
BASE64
|
|
|
|
.encode(contents.as_bytes())
|
2023-01-03 23:02:43 -05:00
|
|
|
.chars()
|
|
|
|
.collect::<Vec<char>>()
|
|
|
|
.chunks(wrap)
|
|
|
|
.map(|c| c.iter().collect::<String>())
|
2023-01-04 15:14:16 -05:00
|
|
|
.for_each(|line| println!("{line}"));
|
2023-01-03 23:02:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_contents(file: &str) -> Result<String, Box<dyn Error>> {
|
|
|
|
let mut contents = String::new();
|
|
|
|
if file == "-" {
|
|
|
|
io::stdin().read_to_string(&mut contents)?;
|
|
|
|
} else {
|
2023-01-03 23:21:42 -05:00
|
|
|
contents = fs::read_to_string(file)?;
|
2023-01-03 23:02:43 -05:00
|
|
|
}
|
|
|
|
Ok(contents)
|
|
|
|
}
|