1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #5803 from D9nni/cksum

cksum: Add --raw argument
This commit is contained in:
Sylvestre Ledru 2024-01-10 18:36:08 +01:00 committed by GitHub
commit 19a9380089
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 90 additions and 2 deletions

View file

@ -5,14 +5,17 @@
// spell-checker:ignore (ToDO) fname, algo
use clap::{crate_version, value_parser, Arg, ArgAction, Command};
use hex::decode;
use hex::encode;
use std::error::Error;
use std::ffi::OsStr;
use std::fmt::Display;
use std::fs::File;
use std::io::{self, stdin, BufReader, Read};
use std::io::{self, stdin, stdout, BufReader, Read, Write};
use std::iter;
use std::path::Path;
use uucore::{
error::{FromIo, UResult},
error::{FromIo, UError, UResult},
format_usage, help_about, help_section, help_usage,
sum::{
div_ceil, Blake2b, Digest, DigestWriter, Md5, Sha1, Sha224, Sha256, Sha384, Sha512, Sm3,
@ -36,6 +39,31 @@ const ALGORITHM_OPTIONS_SHA512: &str = "sha512";
const ALGORITHM_OPTIONS_BLAKE2B: &str = "blake2b";
const ALGORITHM_OPTIONS_SM3: &str = "sm3";
#[derive(Debug)]
enum CkSumError {
RawMultipleFiles,
}
impl UError for CkSumError {
fn code(&self) -> i32 {
match self {
Self::RawMultipleFiles => 1,
}
}
}
impl Error for CkSumError {}
impl Display for CkSumError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::RawMultipleFiles => {
write!(f, "the --raw option is not supported with multiple files")
}
}
}
}
fn detect_algo(
program: &str,
length: Option<usize>,
@ -110,6 +138,7 @@ struct Options {
output_bits: usize,
untagged: bool,
length: Option<usize>,
raw: bool,
}
/// Calculate checksum
@ -123,6 +152,11 @@ fn cksum<'a, I>(mut options: Options, files: I) -> UResult<()>
where
I: Iterator<Item = &'a OsStr>,
{
let files: Vec<_> = files.collect();
if options.raw && files.len() > 1 {
return Err(Box::new(CkSumError::RawMultipleFiles));
}
for filename in files {
let filename = Path::new(filename);
let stdin_buf;
@ -141,6 +175,17 @@ where
let (sum, sz) = digest_read(&mut options.digest, &mut file, options.output_bits)
.map_err_context(|| "failed to read input".to_string())?;
if options.raw {
let bytes = match options.algo_name {
ALGORITHM_OPTIONS_CRC => sum.parse::<u32>().unwrap().to_be_bytes().to_vec(),
ALGORITHM_OPTIONS_SYSV | ALGORITHM_OPTIONS_BSD => {
sum.parse::<u16>().unwrap().to_be_bytes().to_vec()
}
_ => decode(sum).unwrap(),
};
stdout().write_all(&bytes)?;
return Ok(());
}
// The BSD checksum output is 5 digit integer
let bsd_width = 5;
match (options.algo_name, not_file) {
@ -238,6 +283,7 @@ mod options {
pub const FILE: &str = "file";
pub const UNTAGGED: &str = "untagged";
pub const LENGTH: &str = "length";
pub const RAW: &str = "raw";
}
#[uucore::main]
@ -298,6 +344,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
output_bits: bits,
length,
untagged: matches.get_flag(options::UNTAGGED),
raw: matches.get_flag(options::RAW),
};
match matches.get_many::<String>(options::FILE) {
@ -354,5 +401,11 @@ pub fn uu_app() -> Command {
.help("digest length in bits; must not exceed the max for the blake2 algorithm and must be a multiple of 8")
.action(ArgAction::Set),
)
.arg(
Arg::new(options::RAW)
.long(options::RAW)
.help("emit a raw binary digest, not hexadecimal")
.action(ArgAction::SetTrue),
)
.after_help(AFTER_HELP)
}

View file

@ -287,6 +287,30 @@ fn test_length_is_zero() {
.stdout_is_fixture("length_is_zero.expected");
}
#[test]
fn test_raw_single_file() {
for algo in ALGOS {
new_ucmd!()
.arg("--raw")
.arg("lorem_ipsum.txt")
.arg(format!("--algorithm={algo}"))
.succeeds()
.no_stderr()
.stdout_is_fixture_bytes(format!("raw/{algo}_single_file.expected"));
}
}
#[test]
fn test_raw_multiple_files() {
new_ucmd!()
.arg("--raw")
.arg("lorem_ipsum.txt")
.arg("alice_in_wonderland.txt")
.fails()
.no_stdout()
.stderr_contains("cksum: the --raw option is not supported with multiple files")
.code_is(1);
}
#[test]
fn test_blake2b_fail_on_directory() {
let (at, mut ucmd) = at_and_ucmd!();

View file

@ -0,0 +1 @@
— ‘‰å`Ãxœ ÿf†øWÑûþEtÞBãÀl«¹W^Jö0šaX´ÓÀ8Á´<C381>5<15>BÍÀ9m•Ã

View file

@ -0,0 +1 @@
<1F>

View file

@ -0,0 +1 @@
訓h

Binary file not shown.

View file

@ -0,0 +1 @@
<EFBFBD>к<>؈:=<18>m毽(%,<2C><>

Binary file not shown.

View file

@ -0,0 +1 @@
÷Ä P 0P gê^ <09>SkEþœC[Ù+?

View file

@ -0,0 +1,3 @@
K荵
2吠鋳<19><EFBFBD>マクアト/g胤€コ囹LZ{WZ3Sゥ
H

View file

@ -0,0 +1 @@
Td«%VªÕ޼sØšÒ!åYyu)ì¯ÀôfÁ•ÏöÖâÆ |T,ýBn^Oऊ¡VgºD k!=Í<03>ú

View file

@ -0,0 +1 @@
m)k€] þÒ(ß0<C39F>»CyMÔíg@¡iÂ

View file

@ -0,0 +1 @@
I