-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
152 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
use std::io::{self, BufRead, Read}; | ||
|
||
use bff::crc32::{Asobo32, AsoboAlternate32, Kalisto32}; | ||
use bff::crc64::Asobo64; | ||
use bff::traits::NameHashFunction; | ||
use clap::ValueEnum; | ||
|
||
use crate::error::BffCliResult; | ||
|
||
#[derive(ValueEnum, Clone)] | ||
pub enum CrcAlgorithm { | ||
#[value(alias("a"))] | ||
Asobo, | ||
#[value(alias("alt"))] | ||
AsoboAlternate, | ||
#[value(alias("k"))] | ||
Kalisto, | ||
#[value(alias("a64"))] | ||
Asobo64, | ||
} | ||
|
||
#[derive(ValueEnum, Clone)] | ||
pub enum CrcMode { | ||
Bytes, | ||
Lines, | ||
} | ||
|
||
#[derive(ValueEnum, Clone)] | ||
pub enum CrcFormat { | ||
Signed, | ||
Unsigned, | ||
#[value(alias("hex"))] | ||
Hexadecimal, | ||
} | ||
|
||
fn format_hash(hash: i32, format: &CrcFormat) -> String { | ||
match format { | ||
CrcFormat::Signed => { | ||
format!("{}", hash) | ||
} | ||
CrcFormat::Unsigned => { | ||
format!("{}", hash as u32) | ||
} | ||
CrcFormat::Hexadecimal => { | ||
format!("{:#08x}", hash) | ||
} | ||
} | ||
} | ||
|
||
fn format_hash64(hash: i64, format: &CrcFormat) -> String { | ||
match format { | ||
CrcFormat::Signed => { | ||
format!("{}", hash) | ||
} | ||
CrcFormat::Unsigned => { | ||
format!("{}", hash as u64) | ||
} | ||
CrcFormat::Hexadecimal => { | ||
format!("{:#016x}", hash) | ||
} | ||
} | ||
} | ||
|
||
fn hash( | ||
bytes: &[u8], | ||
starting: &Option<i64>, | ||
algorithm: &CrcAlgorithm, | ||
format: &CrcFormat, | ||
) -> String { | ||
let starting = *starting; | ||
match starting { | ||
None => match algorithm { | ||
CrcAlgorithm::Asobo => format_hash(Asobo32::hash(bytes), format), | ||
CrcAlgorithm::AsoboAlternate => format_hash(AsoboAlternate32::hash(bytes), format), | ||
CrcAlgorithm::Kalisto => format_hash(Kalisto32::hash(bytes), format), | ||
CrcAlgorithm::Asobo64 => format_hash64(Asobo64::hash(bytes), format), | ||
}, | ||
Some(starting) => match algorithm { | ||
CrcAlgorithm::Asobo => { | ||
format_hash(Asobo32::hash_options(bytes, starting as i32), format) | ||
} | ||
CrcAlgorithm::AsoboAlternate => format_hash( | ||
AsoboAlternate32::hash_options(bytes, starting as i32), | ||
format, | ||
), | ||
CrcAlgorithm::Kalisto => { | ||
format_hash(Kalisto32::hash_options(bytes, starting as i32), format) | ||
} | ||
CrcAlgorithm::Asobo64 => format_hash64(Asobo64::hash_options(bytes, starting), format), | ||
}, | ||
} | ||
} | ||
|
||
pub fn crc( | ||
string: &Option<String>, | ||
starting: &Option<i64>, | ||
algorithm: &CrcAlgorithm, | ||
mode: &CrcMode, | ||
format: &CrcFormat, | ||
) -> BffCliResult<()> { | ||
match (string, mode) { | ||
(Some(string), CrcMode::Bytes) => { | ||
println!( | ||
"{} {:?}", | ||
hash(string.as_bytes(), starting, algorithm, format), | ||
string.as_bytes() | ||
); | ||
} | ||
(Some(string), CrcMode::Lines) => { | ||
for line in string.lines() { | ||
println!( | ||
"{} \"{}\"", | ||
hash(line.as_bytes(), starting, algorithm, format), | ||
line | ||
); | ||
} | ||
} | ||
(None, CrcMode::Bytes) => { | ||
let stdin = io::stdin(); | ||
let mut buf: Vec<u8> = Vec::new(); | ||
stdin.lock().read_to_end(&mut buf)?; | ||
println!("{} {:?}", hash(&buf, starting, algorithm, format), buf); | ||
} | ||
(None, CrcMode::Lines) => { | ||
let stdin = io::stdin(); | ||
for line in stdin.lock().lines() { | ||
let line = line?; | ||
println!( | ||
"{} \"{}\"", | ||
hash(line.as_bytes(), starting, algorithm, format), | ||
line | ||
); | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.