Function spectrusty_formats::mdr::checksum
source · pub fn checksum<I: IntoIterator<Item = B>, B: Borrow<u8>>(iter: I) -> u8
Expand description
Checksum calculating routine used by Spectrum for Microdrive data.
Examples found in repository?
src/mdr.rs (line 545)
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
fn update_block_checksums(&mut self) {
self.data[DESCHK] = checksum(&self.data[0..DESCHK]);
self.data[DCHK] = checksum(&self.data[DESCHK + 1..DCHK])
}
fn erase(&mut self) {
self.data[RECFLG] = 0;
self.data[RECLEN..RECLEN + 2].iter_mut().for_each(|p| *p=0);
self.data[DESCHK] = checksum(&self.data[0..DESCHK]);
}
fn format(&mut self, seq: u8, catalog_name: &[u8]) {
if !(1..=245).contains(&seq) {
panic!("HDNUMB must be between 1 and 254");
}
self.head[HDFLAG] = 1;
self.head[HDNUMB] = seq;
let head_name = &mut self.head[HDNAME..HDNAME + 10];
copy_name(head_name.try_into().unwrap(), catalog_name);
self.head[HDCHK] = checksum(&self.head[0..HDCHK]);
self.erase();
}
fn validate(&self) -> Result<(), &'static str> {
if self.head[HDFLAG] & 1 != 1 {
return Err("bad sector header: HDFLAG bit 0 is reset");
}
if self.data[RECFLG] & 1 != 0 {
return Err("bad data block: RECFLG bit 0 is set");
}
if !(1..=254).contains(&self.head[HDNUMB]) {
return Err("bad sector header: HDNUMB is outside the allowed range: [1-254]");
}
if self.file_block_len() > BLOCK_DATA_MAX {
return Err("bad data block: RECLEN is larger than the sector size: 512");
}
if self.head[HDCHK] != checksum(&self.head[0..HDCHK]) {
return Err("bad sector header: HDCHK invalid checksum");
}
if self.data[DESCHK] != checksum(&self.data[0..DESCHK]) {
return Err("bad data block: DESCHK invalid header checksum");
}
if !self.is_free() &&
self.data[DCHK] != checksum(&self.data[DESCHK+1..DCHK])
{
return Err("bad data block: DESCHK invalid data checksum");
}
Ok(())
}