Invalid edc now returns an error.

This commit is contained in:
Filipe Rodrigues 2021-04-24 05:57:02 +01:00
parent b6973b8228
commit 082066af3e
2 changed files with 16 additions and 15 deletions

View File

@ -80,21 +80,12 @@ impl Bytes for Sector {
};
// Validate edc
{
let bytes = match data {
Data::Form1(_) => &byte_array[0x10..0x818],
Data::Form2(_) => &byte_array[0x10..0x92c],
};
//if !header.subheader.submode.contains(SubMode::FORM) {
if let Err(expected_edc) = edc.is_valid(bytes) {
log::warn!(
"Sector crc {:#010x} doesn't match calculated crc {:#010x} match in {:?}",
edc.crc,
expected_edc,
header
);
}
let edc_bytes = match data {
Data::Form1(_) => &byte_array[0x10..0x818],
Data::Form2(_) => &byte_array[0x10..0x92c],
};
if let Err(calculated) = edc.is_valid(edc_bytes) {
return Err(FromBytesError::WrongEdc { found: edc.crc, calculated });
}
Ok(Self { header, data })

View File

@ -17,6 +17,16 @@ pub enum FromBytesError {
/// Unable to read header
#[error("Unable to parse header")]
Header(#[source] header::FromBytesError),
/// Edc was wrong
#[error("Found crc {found}, calculated {calculated}")]
WrongEdc {
/// Found
found: u32,
/// Calculated
calculated: u32,
},
}
/// Error type for [`Bytes::to_bytes`](dcb_bytes::Bytes::to_bytes)