Consolidate calc_checksum_bytes_internal routine

Now that the deprecated `get_checksum{,_bytes}` routines are removed,
the checksum is always computed with ignored data after the first '#',
i.e. the boolean parameter `exclude_hash` is not needed anymore and
the functionality can be consolidated into `calc_checksum_bytes`.
This commit is contained in:
Sebastian Falbesoner 2024-01-02 04:09:45 +01:00
parent 179cfeff51
commit ed91a4bdb4
No known key found for this signature in database
GPG Key ID: C2371D91CB716EA7

View File

@ -42,22 +42,16 @@ fn poly_mod(mut c: u64, val: u64) -> u64 {
c c
} }
/// Computes the checksum bytes of a descriptor. /// Compute the checksum bytes of a descriptor, excludes any existing checksum in the descriptor string from the calculation
/// `exclude_hash = true` ignores all data after the first '#' (inclusive). pub fn calc_checksum_bytes(mut desc: &str) -> Result<[u8; 8], DescriptorError> {
pub(crate) fn calc_checksum_bytes_internal(
mut desc: &str,
exclude_hash: bool,
) -> Result<[u8; 8], DescriptorError> {
let mut c = 1; let mut c = 1;
let mut cls = 0; let mut cls = 0;
let mut clscount = 0; let mut clscount = 0;
let mut original_checksum = None; let mut original_checksum = None;
if exclude_hash { if let Some(split) = desc.split_once('#') {
if let Some(split) = desc.split_once('#') { desc = split.0;
desc = split.0; original_checksum = Some(split.1);
original_checksum = Some(split.1);
}
} }
for ch in desc.as_bytes() { for ch in desc.as_bytes() {
@ -95,20 +89,12 @@ pub(crate) fn calc_checksum_bytes_internal(
Ok(checksum) Ok(checksum)
} }
/// Compute the checksum bytes of a descriptor, excludes any existing checksum in the descriptor string from the calculation
pub fn calc_checksum_bytes(desc: &str) -> Result<[u8; 8], DescriptorError> {
calc_checksum_bytes_internal(desc, true)
}
/// Compute the checksum of a descriptor, excludes any existing checksum in the descriptor string from the calculation /// Compute the checksum of a descriptor, excludes any existing checksum in the descriptor string from the calculation
pub fn calc_checksum(desc: &str) -> Result<String, DescriptorError> { pub fn calc_checksum(desc: &str) -> Result<String, DescriptorError> {
// unsafe is okay here as the checksum only uses bytes in `CHECKSUM_CHARSET` // unsafe is okay here as the checksum only uses bytes in `CHECKSUM_CHARSET`
calc_checksum_bytes_internal(desc, true) calc_checksum_bytes(desc).map(|b| unsafe { String::from_utf8_unchecked(b.to_vec()) })
.map(|b| unsafe { String::from_utf8_unchecked(b.to_vec()) })
} }
// TODO in release 0.25.0, consolidate calc_checksum_bytes_internal into calc_checksum_bytes
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;