CFDP extracted to library #201

Closed
muellerr wants to merge 18 commits from continue-cfsp-source-handler into main
Showing only changes of commit 97285baaf4 - Show all commits

View File

@ -133,6 +133,20 @@ pub trait VirtualFilestore {
fn file_size(&self, path: &str) -> Result<u64, FilestoreError>;
/// This special function is the CFDP specific abstraction to calculate the checksum of a file.
/// This allows to keep OS specific details like reading the whole file in the most efficient
/// manner inside the file system abstraction.
///
/// The passed verification buffer argument will be used by the specific implementation as
/// a buffer to read the file into. It is recommended to use common buffer sizes like
/// 4096 or 8192 bytes.
fn calculate_checksum(
&self,
file_path: &str,
checksum_type: ChecksumType,
verification_buf: &mut [u8],
) -> Result<u32, FilestoreError>;
/// This special function is the CFDP specific abstraction to verify the checksum of a file.
/// This allows to keep OS specific details like reading the whole file in the most efficient
/// manner inside the file system abstraction.
@ -146,11 +160,17 @@ pub trait VirtualFilestore {
checksum_type: ChecksumType,
expected_checksum: u32,
verification_buf: &mut [u8],
) -> Result<bool, FilestoreError>;
) -> Result<bool, FilestoreError> {
Ok(
self.calculate_checksum(file_path, checksum_type, verification_buf)?
== expected_checksum,
)
}
}
#[cfg(feature = "std")]
pub mod std_mod {
use super::*;
use std::{
fs::{self, File, OpenOptions},
@ -280,20 +300,14 @@ pub mod std_mod {
Ok(path.metadata()?.len())
}
fn checksum_verify(
fn calculate_checksum(
&self,
file_path: &str,
checksum_type: ChecksumType,
expected_checksum: u32,
verification_buf: &mut [u8],
) -> Result<bool, FilestoreError> {
) -> Result<u32, FilestoreError> {
match checksum_type {
ChecksumType::Modular => {
if self.calc_modular_checksum(file_path)? == expected_checksum {
return Ok(true);
}
Ok(false)
}
ChecksumType::Modular => self.calc_modular_checksum(file_path),
ChecksumType::Crc32 => {
let mut digest = CRC_32.digest();
let file_to_check = File::open(file_path)?;
@ -305,12 +319,9 @@ pub mod std_mod {
}
digest.update(&verification_buf[0..bytes_read]);
}
if digest.finalize() == expected_checksum {
return Ok(true);
}
Ok(false)
Ok(digest.finalize())
}
ChecksumType::NullChecksum => Ok(true),
ChecksumType::NullChecksum => Ok(0),
_ => Err(FilestoreError::ChecksumTypeNotImplemented(checksum_type)),
}
}