use crate::error::Result;
#[cfg(feature = "crc")]
#[inline]
pub(crate) fn verify(
verify: bool,
expected: u32,
data: &[u8],
section: &'static str,
) -> Result<()> {
if verify && crc32c::crc32c(data) != expected {
return Err(crate::error::Error::Checksum(section));
}
Ok(())
}
#[cfg(not(feature = "crc"))]
#[inline]
pub(crate) fn verify(
_verify: bool,
_expected: u32,
_data: &[u8],
_section: &'static str,
) -> Result<()> {
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "crc")]
#[test]
fn detects_a_wrong_checksum() {
let data = b"payload";
let good = crc32c::crc32c(data);
assert!(verify(true, good, data, "test").is_ok());
assert!(matches!(
verify(true, good ^ 0xFF, data, "test"),
Err(crate::error::Error::Checksum("test"))
));
assert!(verify(false, good ^ 0xFF, data, "test").is_ok());
}
#[cfg(not(feature = "crc"))]
#[test]
fn is_a_no_op_without_the_feature() {
assert!(verify(true, 0xDEAD_BEEF, b"payload", "test").is_ok());
}
}