2024-05-13 13:51:17 +02:00
|
|
|
#![allow(dead_code)] // this is exported to lib.rs
|
2024-05-13 13:19:49 +02:00
|
|
|
use anyhow::anyhow;
|
2024-05-12 19:53:38 +02:00
|
|
|
use clap::{ArgGroup, Parser};
|
2024-05-13 15:45:38 +02:00
|
|
|
use libpt::bintols::{join, split};
|
2024-05-12 18:36:36 +02:00
|
|
|
|
2024-05-13 13:19:49 +02:00
|
|
|
pub type NumberType = u128;
|
2024-05-12 01:02:55 +02:00
|
|
|
|
2024-05-12 19:33:48 +02:00
|
|
|
/// formats supported by numf
|
2024-05-12 20:29:12 +02:00
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
2024-05-12 01:02:55 +02:00
|
|
|
pub enum Format {
|
|
|
|
Dec,
|
|
|
|
Hex,
|
|
|
|
Bin,
|
|
|
|
Octal,
|
2024-05-12 01:49:11 +02:00
|
|
|
Base64,
|
2024-05-12 01:55:56 +02:00
|
|
|
Base32,
|
2024-05-12 01:02:55 +02:00
|
|
|
}
|
|
|
|
|
2024-05-12 19:53:38 +02:00
|
|
|
/// Describes what the formatter should do
|
2024-05-12 19:33:48 +02:00
|
|
|
///
|
2024-05-12 19:53:38 +02:00
|
|
|
/// Use [Self::default] to get a basic variant or create a object yourself.
|
|
|
|
///
|
|
|
|
/// This struct can be parsed with [clap] derive.
|
2024-05-12 19:54:45 +02:00
|
|
|
#[derive(Parser, Debug, Clone, PartialEq, Eq, Hash)]
|
2024-05-12 19:53:38 +02:00
|
|
|
#[clap(author, version, about, long_about = None)]
|
|
|
|
#[command(
|
|
|
|
author,
|
|
|
|
version,
|
|
|
|
about,
|
|
|
|
long_about,
|
|
|
|
help_template = r#"{about-section}
|
|
|
|
{usage-heading} {usage}
|
|
|
|
{all-args}{tab}
|
|
|
|
|
|
|
|
{name}: {version}
|
|
|
|
Author: {author-with-newline}
|
|
|
|
"#
|
|
|
|
)]
|
|
|
|
#[clap(group(
|
|
|
|
ArgGroup::new("format")
|
|
|
|
.args(&["hex", "bin", "oct", "dec", "base64", "base32"]),
|
|
|
|
))]
|
2024-05-12 19:33:48 +02:00
|
|
|
pub struct FormatOptions {
|
2024-05-12 19:53:38 +02:00
|
|
|
#[arg(short, long)]
|
|
|
|
/// add a prefix (like "0x" for hex)
|
2024-05-12 19:33:48 +02:00
|
|
|
prefix: bool,
|
2024-05-12 19:53:38 +02:00
|
|
|
#[arg(short = 'P', long)]
|
|
|
|
/// add a padding to make the number at least one byte long
|
|
|
|
///
|
|
|
|
/// For example, `0b1100` will be `0b00001100` with this.
|
|
|
|
/// This does not apply to all formats, only hexadecimal and binary.
|
2024-05-12 19:33:48 +02:00
|
|
|
padding: bool,
|
2024-05-12 19:53:38 +02:00
|
|
|
#[arg(short = 'x', long, default_value_t = true)]
|
|
|
|
/// format to hexadecimal
|
|
|
|
hex: bool,
|
|
|
|
#[arg(short, long)]
|
|
|
|
/// format to binary
|
|
|
|
bin: bool,
|
|
|
|
#[arg(short, long)]
|
|
|
|
/// format to decimal
|
|
|
|
dec: bool,
|
|
|
|
#[arg(short, long)]
|
|
|
|
/// format to octal
|
|
|
|
oct: bool,
|
|
|
|
#[arg(short = 's', long)]
|
|
|
|
/// format to base64
|
|
|
|
base64: bool,
|
|
|
|
#[arg(short = 'z', long)]
|
|
|
|
/// format to base32
|
|
|
|
base32: bool,
|
2024-05-13 16:55:39 +02:00
|
|
|
#[clap(value_parser=numf_parser::<NumberType>, required=false)]
|
2024-05-16 17:32:45 +02:00
|
|
|
/// numbers that should be formatted
|
2024-05-12 19:53:38 +02:00
|
|
|
///
|
2024-05-13 16:55:39 +02:00
|
|
|
/// Any of the [Formats](Format::format) are supported, but the prefixes are needed for formats
|
|
|
|
/// other than decimal.
|
|
|
|
///
|
|
|
|
/// Formats:
|
|
|
|
///
|
2024-05-13 17:13:33 +02:00
|
|
|
/// * '0x' - Hexadecimal
|
2024-05-13 16:55:39 +02:00
|
|
|
///
|
2024-05-13 17:13:33 +02:00
|
|
|
/// * '0b' - Binary
|
|
|
|
///
|
|
|
|
/// * '0o' - Octal
|
|
|
|
///
|
|
|
|
/// * '0s' - Base64
|
|
|
|
///
|
|
|
|
/// * '032s' - Base32
|
|
|
|
///
|
|
|
|
/// The numbers may be left empty at first, if numbers are provided from the stdin.
|
2024-05-13 13:19:49 +02:00
|
|
|
numbers: Vec<NumberType>,
|
2024-05-12 19:33:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FormatOptions {
|
2024-05-12 19:53:38 +02:00
|
|
|
/// get the format that the user has configured
|
|
|
|
pub fn format(&self) -> Format {
|
|
|
|
if self.oct {
|
|
|
|
Format::Octal
|
|
|
|
} else if self.bin {
|
|
|
|
Format::Bin
|
|
|
|
} else if self.dec {
|
|
|
|
Format::Dec
|
|
|
|
} else if self.base64 {
|
|
|
|
Format::Base64
|
|
|
|
} else if self.base32 {
|
|
|
|
Format::Base32
|
|
|
|
} else if self.hex {
|
|
|
|
Format::Hex
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
2024-05-12 19:33:48 +02:00
|
|
|
}
|
2024-05-12 19:53:38 +02:00
|
|
|
|
|
|
|
/// set the format manually
|
|
|
|
pub fn set_format(&mut self, format: Format) {
|
|
|
|
self.bin = false;
|
|
|
|
self.oct = false;
|
|
|
|
self.dec = false;
|
|
|
|
self.hex = false;
|
|
|
|
self.base64 = false;
|
|
|
|
self.base32 = false;
|
|
|
|
match format {
|
|
|
|
Format::Bin => self.bin = true,
|
|
|
|
Format::Hex => self.hex = true,
|
|
|
|
Format::Octal => self.oct = true,
|
|
|
|
Format::Base64 => self.base64 = true,
|
|
|
|
Format::Base32 => self.base32 = true,
|
|
|
|
Format::Dec => self.dec = true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get numbers
|
|
|
|
pub fn numbers(&self) -> &[u128] {
|
|
|
|
self.numbers.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// set numbers manually
|
2024-05-13 13:19:49 +02:00
|
|
|
pub fn set_numbers(&mut self, numbers: Vec<NumberType>) {
|
2024-05-12 19:53:38 +02:00
|
|
|
self.numbers = numbers;
|
|
|
|
}
|
|
|
|
|
2024-05-12 20:19:57 +02:00
|
|
|
/// set padding manually
|
|
|
|
pub fn set_padding(&mut self, value: bool) {
|
|
|
|
self.padding = value
|
|
|
|
}
|
|
|
|
|
2024-05-12 19:53:38 +02:00
|
|
|
/// get padding
|
|
|
|
pub fn padding(&self) -> bool {
|
|
|
|
self.padding
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get prefix
|
|
|
|
pub fn prefix(&self) -> bool {
|
|
|
|
self.prefix
|
|
|
|
}
|
2024-05-12 20:19:57 +02:00
|
|
|
|
|
|
|
/// set prefix manually
|
|
|
|
pub fn set_prefix(&mut self, value: bool) {
|
|
|
|
self.prefix = value;
|
|
|
|
}
|
2024-05-13 16:55:39 +02:00
|
|
|
|
|
|
|
/// manually add a number
|
|
|
|
pub fn push_number(&mut self, value: NumberType) {
|
|
|
|
self.numbers.push(value)
|
|
|
|
}
|
2024-05-12 19:53:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FormatOptions {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
padding: false,
|
|
|
|
prefix: false,
|
|
|
|
oct: false,
|
|
|
|
hex: true,
|
|
|
|
bin: false,
|
|
|
|
base32: false,
|
|
|
|
base64: false,
|
|
|
|
dec: false,
|
|
|
|
numbers: vec![],
|
|
|
|
}
|
2024-05-12 19:33:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-12 01:02:55 +02:00
|
|
|
impl Format {
|
2024-05-13 13:19:49 +02:00
|
|
|
/// Get the perfix for that [Format]
|
2024-05-12 01:02:55 +02:00
|
|
|
pub fn prefix(&self) -> String {
|
|
|
|
match self {
|
2024-05-12 01:49:11 +02:00
|
|
|
// apperently used nowhere, sometimes 0 is used as a prefix but I
|
|
|
|
// think this makes it more clear that this is decimal
|
2024-05-12 01:02:55 +02:00
|
|
|
Format::Dec => "0d",
|
2024-05-12 01:49:11 +02:00
|
|
|
// very common
|
2024-05-12 01:02:55 +02:00
|
|
|
Format::Hex => "0x",
|
2024-05-12 01:49:11 +02:00
|
|
|
// very common
|
2024-05-12 01:02:55 +02:00
|
|
|
Format::Bin => "0b",
|
2024-05-12 01:49:11 +02:00
|
|
|
// somewhat common
|
2024-05-12 01:02:55 +02:00
|
|
|
Format::Octal => "0o",
|
2024-05-12 01:49:11 +02:00
|
|
|
// perl and a few other programs seem to use this too
|
|
|
|
Format::Base64 => "0s",
|
2024-05-12 01:55:56 +02:00
|
|
|
// no idea, I made this up
|
|
|
|
Format::Base32 => "032s",
|
2024-05-12 01:02:55 +02:00
|
|
|
}
|
|
|
|
.to_string()
|
|
|
|
}
|
2024-05-13 13:19:49 +02:00
|
|
|
/// format a number with a [Format] and [FormatOptions]
|
|
|
|
pub fn format(&self, num: NumberType, options: &FormatOptions) -> String {
|
2024-05-12 01:02:55 +02:00
|
|
|
let mut buf = String::new();
|
2024-05-13 13:19:49 +02:00
|
|
|
if options.prefix() {
|
2024-05-12 01:02:55 +02:00
|
|
|
buf += &self.prefix();
|
|
|
|
}
|
|
|
|
match self {
|
|
|
|
Format::Hex => {
|
2024-05-13 13:19:49 +02:00
|
|
|
if options.padding() {
|
2024-05-12 19:33:48 +02:00
|
|
|
let tmp = &format!("{num:X}");
|
|
|
|
buf += &("0".repeat((2 - tmp.len() % 2) % 2) + tmp);
|
|
|
|
} else {
|
|
|
|
buf += &format!("{num:X}");
|
|
|
|
}
|
2024-05-12 01:02:55 +02:00
|
|
|
}
|
|
|
|
Format::Bin => {
|
2024-05-13 13:19:49 +02:00
|
|
|
if options.padding() {
|
2024-05-12 19:33:48 +02:00
|
|
|
let tmp = &format!("{num:b}");
|
|
|
|
buf += &("0".repeat((8 - tmp.len() % 8) % 8) + tmp);
|
|
|
|
} else {
|
|
|
|
buf += &format!("{num:b}");
|
|
|
|
}
|
2024-05-12 01:02:55 +02:00
|
|
|
}
|
2024-05-13 13:19:49 +02:00
|
|
|
Format::Octal => buf += &format!("{num:o}"),
|
|
|
|
Format::Dec => buf += &format!("{num}"),
|
2024-05-12 18:36:36 +02:00
|
|
|
Format::Base64 => buf += &fast32::base64::RFC4648.encode(&split::unsigned_to_vec(num)),
|
|
|
|
Format::Base32 => buf += &fast32::base32::RFC4648.encode(&split::unsigned_to_vec(num)),
|
2024-05-12 01:02:55 +02:00
|
|
|
}
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
}
|
2024-05-13 13:19:49 +02:00
|
|
|
|
|
|
|
/// Validates an unsigned integer value that can be one of [Format](format::Format).
|
|
|
|
///
|
|
|
|
/// The number is assumed to be base-10 by default, it is parsed as a different
|
|
|
|
/// [Format](format::Format) if the number is prefixed with the [prefix](format::FormatOptions::prefix),
|
2024-05-13 13:39:56 +02:00
|
|
|
/// case sensitive. So if the user inputs `0b1100` then this is parsed as
|
2024-05-13 13:19:49 +02:00
|
|
|
/// [Binary](format::Format::Bin) and so on.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// This allows base-10 addresses to be passed normally, or values formatted with any of the
|
|
|
|
/// [Formats](format::Format) defined by this crate to be passed when prefixed with the respective
|
|
|
|
/// prefix.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use clap::Parser;
|
2024-05-13 13:31:33 +02:00
|
|
|
/// use numf::format::numf_parser;
|
2024-05-13 13:19:49 +02:00
|
|
|
///
|
|
|
|
/// #[derive(Parser)]
|
|
|
|
/// struct Args {
|
2024-05-13 14:25:04 +02:00
|
|
|
/// #[clap(short, long, value_parser=numf_parser::<u128>)]
|
2024-05-13 13:31:33 +02:00
|
|
|
/// address: u128,
|
2024-05-13 13:19:49 +02:00
|
|
|
/// }
|
|
|
|
/// let args = Args::parse_from(&["", "-a", "0x10"]);
|
|
|
|
/// assert_eq!(args.address, 16);
|
|
|
|
/// ```
|
2024-05-13 13:51:17 +02:00
|
|
|
pub fn numf_parser<T>(s: &str) -> anyhow::Result<T>
|
|
|
|
where
|
2024-05-13 15:45:38 +02:00
|
|
|
T: std::str::FromStr + std::convert::TryFrom<u128>,
|
2024-05-13 13:51:17 +02:00
|
|
|
<T as std::str::FromStr>::Err: std::fmt::Display,
|
|
|
|
T: num::Num,
|
|
|
|
<T as num::Num>::FromStrRadixErr: std::fmt::Display,
|
2024-05-13 14:25:04 +02:00
|
|
|
<T as std::str::FromStr>::Err: std::fmt::Debug,
|
2024-05-13 15:45:38 +02:00
|
|
|
u128: std::convert::From<T>,
|
|
|
|
<T as std::str::FromStr>::Err: std::error::Error,
|
|
|
|
<T as std::convert::TryFrom<u128>>::Error: std::error::Error,
|
|
|
|
<T as std::convert::TryFrom<u128>>::Error: std::marker::Send,
|
|
|
|
<T as std::convert::TryFrom<u128>>::Error: std::marker::Sync,
|
|
|
|
<T as std::convert::TryFrom<u128>>::Error: 'static,
|
2024-05-13 13:51:17 +02:00
|
|
|
{
|
|
|
|
if s.starts_with(&Format::Dec.prefix()) || s.parse::<T>().is_ok() {
|
2024-05-13 13:19:49 +02:00
|
|
|
let s = match s.strip_prefix(&Format::Dec.prefix()) {
|
|
|
|
Some(sr) => sr,
|
|
|
|
None => s,
|
|
|
|
};
|
|
|
|
match s.parse() {
|
|
|
|
Ok(r) => Ok(r),
|
|
|
|
Err(e) => {
|
|
|
|
let e = format!("{e}");
|
|
|
|
Err(anyhow!(e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if s.starts_with(&Format::Hex.prefix()) {
|
|
|
|
let s = match s.strip_prefix(&Format::Hex.prefix()) {
|
|
|
|
Some(sr) => sr,
|
|
|
|
None => s,
|
|
|
|
};
|
2024-05-13 13:51:17 +02:00
|
|
|
match T::from_str_radix(s, 16) {
|
2024-05-13 13:19:49 +02:00
|
|
|
Ok(r) => Ok(r),
|
|
|
|
Err(e) => {
|
|
|
|
let e = format!("{e}");
|
|
|
|
Err(anyhow!(e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if s.starts_with(&Format::Octal.prefix()) {
|
|
|
|
let s = match s.strip_prefix(&Format::Octal.prefix()) {
|
|
|
|
Some(sr) => sr,
|
|
|
|
None => s,
|
|
|
|
};
|
2024-05-13 13:51:17 +02:00
|
|
|
match T::from_str_radix(s, 8) {
|
2024-05-13 13:19:49 +02:00
|
|
|
Ok(r) => Ok(r),
|
|
|
|
Err(e) => {
|
|
|
|
let e = format!("{e}");
|
|
|
|
Err(anyhow!(e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if s.starts_with(&Format::Bin.prefix()) {
|
|
|
|
let s = match s.strip_prefix(&Format::Bin.prefix()) {
|
|
|
|
Some(sr) => sr,
|
|
|
|
None => s,
|
|
|
|
};
|
2024-05-13 13:51:17 +02:00
|
|
|
match T::from_str_radix(s, 2) {
|
2024-05-13 13:19:49 +02:00
|
|
|
Ok(r) => Ok(r),
|
|
|
|
Err(e) => {
|
|
|
|
let e = format!("{e}");
|
|
|
|
Err(anyhow!(e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if s.starts_with(&Format::Base64.prefix()) {
|
2024-05-13 14:25:04 +02:00
|
|
|
let s = match s.strip_prefix(&Format::Base64.prefix()) {
|
|
|
|
Some(sr) => sr,
|
|
|
|
None => s,
|
|
|
|
};
|
|
|
|
match fast32::base64::RFC4648.decode_str(s) {
|
2024-05-13 15:52:18 +02:00
|
|
|
Ok(r) => Ok(join::array_to_unsigned::<T>(&r)?),
|
2024-05-13 14:25:04 +02:00
|
|
|
Err(e) => {
|
|
|
|
let e = format!("{e}");
|
|
|
|
Err(anyhow!(e))
|
|
|
|
}
|
|
|
|
}
|
2024-05-13 13:19:49 +02:00
|
|
|
} else if s.starts_with(&Format::Base32.prefix()) {
|
2024-05-13 14:25:04 +02:00
|
|
|
let s = match s.strip_prefix(&Format::Base32.prefix()) {
|
|
|
|
Some(sr) => sr,
|
|
|
|
None => s,
|
|
|
|
};
|
|
|
|
match fast32::base32::RFC4648.decode_str(s) {
|
2024-05-13 15:45:38 +02:00
|
|
|
Ok(r) => Ok(join::array_to_unsigned::<T>(&r)?),
|
2024-05-13 14:25:04 +02:00
|
|
|
Err(e) => {
|
|
|
|
let e = format!("{e}");
|
|
|
|
Err(anyhow!(e))
|
|
|
|
}
|
|
|
|
}
|
2024-05-13 13:19:49 +02:00
|
|
|
} else {
|
|
|
|
let e = "could not determine the format of the value".to_string();
|
|
|
|
Err(anyhow!(e))
|
|
|
|
}
|
|
|
|
}
|