57 lines
1.4 KiB
Rust
57 lines
1.4 KiB
Rust
|
use {
|
||
|
crate::Error,
|
||
|
std::io::{Read, Write},
|
||
|
};
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
pub enum Checksum {
|
||
|
Md5([u8; 16]),
|
||
|
Sha1([u8; 20]),
|
||
|
Sha256([u8; 32]),
|
||
|
Skip,
|
||
|
}
|
||
|
|
||
|
impl Checksum {
|
||
|
pub fn read<T: Read>(reader: &mut T) -> Result<Self, Error> {
|
||
|
let mut buf = [0; 1];
|
||
|
reader.read_exact(&mut buf)?;
|
||
|
match buf[0] {
|
||
|
0 => {
|
||
|
let mut sum = [0; 16];
|
||
|
reader.read_exact(&mut sum)?;
|
||
|
Ok(Self::Md5(sum))
|
||
|
}
|
||
|
1 => {
|
||
|
let mut sum = [0; 20];
|
||
|
reader.read_exact(&mut sum)?;
|
||
|
Ok(Self::Sha1(sum))
|
||
|
}
|
||
|
2 => {
|
||
|
let mut sum = [0; 32];
|
||
|
reader.read_exact(&mut sum)?;
|
||
|
Ok(Self::Sha256(sum))
|
||
|
}
|
||
|
_ => Ok(Self::Skip),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn write<T: Write>(&self, writer: &mut T) -> Result<(), Error> {
|
||
|
match self {
|
||
|
Self::Md5(sum) => {
|
||
|
writer.write_all(&[b'0'])?;
|
||
|
writer.write_all(&sum[..])?;
|
||
|
}
|
||
|
Self::Sha1(sum) => {
|
||
|
writer.write_all(&[b'1'])?;
|
||
|
writer.write_all(&sum[..])?;
|
||
|
}
|
||
|
Self::Sha256(sum) => {
|
||
|
writer.write_all(&[b'2'])?;
|
||
|
writer.write_all(&sum[..])?;
|
||
|
}
|
||
|
Self::Skip => writer.write_all(&[b'3'])?,
|
||
|
}
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|