Skip to content

Commit 42997a2

Browse files
committed
checksum: rename algo len to bitlen to avoid confusion
1 parent 9f3be29 commit 42997a2

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/uucore/src/lib/features/checksum.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ fn print_file_report<W: Write>(
317317
}
318318
}
319319

320-
pub fn detect_algo(algo: &str, length: Option<usize>) -> UResult<HashAlgorithm> {
320+
pub fn detect_algo(algo: &str, bitlen: Option<usize>) -> UResult<HashAlgorithm> {
321321
match algo {
322322
ALGORITHM_OPTIONS_SYSV => Ok(HashAlgorithm {
323323
name: ALGORITHM_OPTIONS_SYSV,
@@ -366,7 +366,7 @@ pub fn detect_algo(algo: &str, length: Option<usize>) -> UResult<HashAlgorithm>
366366
}),
367367
ALGORITHM_OPTIONS_BLAKE2B | "b2sum" => {
368368
// Set default length to 512 if None
369-
let bits = length.unwrap_or(512);
369+
let bits = bitlen.unwrap_or(512);
370370
if bits == 512 {
371371
Ok(HashAlgorithm {
372372
name: ALGORITHM_OPTIONS_BLAKE2B,
@@ -393,7 +393,7 @@ pub fn detect_algo(algo: &str, length: Option<usize>) -> UResult<HashAlgorithm>
393393
}),
394394
ALGORITHM_OPTIONS_SHAKE128 | "shake128sum" => {
395395
let bits =
396-
length.ok_or_else(|| USimpleError::new(1, "--bits required for SHAKE128"))?;
396+
bitlen.ok_or_else(|| USimpleError::new(1, "--bits required for SHAKE128"))?;
397397
Ok(HashAlgorithm {
398398
name: ALGORITHM_OPTIONS_SHAKE128,
399399
create_fn: Box::new(|| Box::new(Shake128::new())),
@@ -402,15 +402,15 @@ pub fn detect_algo(algo: &str, length: Option<usize>) -> UResult<HashAlgorithm>
402402
}
403403
ALGORITHM_OPTIONS_SHAKE256 | "shake256sum" => {
404404
let bits =
405-
length.ok_or_else(|| USimpleError::new(1, "--bits required for SHAKE256"))?;
405+
bitlen.ok_or_else(|| USimpleError::new(1, "--bits required for SHAKE256"))?;
406406
Ok(HashAlgorithm {
407407
name: ALGORITHM_OPTIONS_SHAKE256,
408408
create_fn: Box::new(|| Box::new(Shake256::new())),
409409
bits,
410410
})
411411
}
412412
//ALGORITHM_OPTIONS_SHA3 | "sha3" => (
413-
_ if algo.starts_with("sha3") => create_sha3(length),
413+
_ if algo.starts_with("sha3") => create_sha3(bitlen),
414414

415415
_ => Err(ChecksumError::UnknownAlgorithm.into()),
416416
}
@@ -621,15 +621,17 @@ fn identify_algo_name_and_length(
621621
return None;
622622
}
623623

624-
let bits = line_info.algo_bitlen.map_or(Some(None), |bits| {
625-
if bits % 8 == 0 {
626-
Some(Some(bits / 8))
627-
} else {
628-
None // Return None to signal a divisibility issue
624+
let bitlen = if let Some(b) = line_info.algo_bitlen {
625+
if b % 8 != 0 {
626+
// Improperly formatted line : the given length is incorrect.
627+
return None;
629628
}
630-
})?;
629+
Some(b / 8)
630+
} else {
631+
None
632+
};
631633

632-
Some((algorithm, bits))
634+
Some((algorithm, bitlen))
633635
}
634636

635637
/// Parses a checksum line, detect the algorithm to use, read the file and produce
@@ -677,7 +679,7 @@ fn process_checksum_line(
677679
.ok_or(LineCheckError::ImproperlyFormatted)?;
678680

679681
// If the algo_name is provided, we use it, otherwise we try to detect it
680-
let (algo_name, length) = if line_info.is_algo_based() {
682+
let (algo_name, algo_bitlen) = if line_info.is_algo_based() {
681683
identify_algo_name_and_length(&line_info, cli_algo_name)
682684
.ok_or(LineCheckError::ImproperlyFormatted)?
683685
} else if let Some(a) = cli_algo_name {
@@ -696,7 +698,7 @@ fn process_checksum_line(
696698
return Err(LineCheckError::ImproperlyFormatted);
697699
};
698700

699-
let mut algo = detect_algo(&algo_name, length)?;
701+
let mut algo = detect_algo(&algo_name, algo_bitlen)?;
700702

701703
let (filename_to_check_unescaped, prefix) = unescape_filename(filename_to_check);
702704

0 commit comments

Comments
 (0)