src/backup/data_blob.rs: cleanup - improve code reuse
This commit is contained in:
parent
92c3fd2e22
commit
1090fd4424
@ -167,7 +167,7 @@ impl DataBlob {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Decode blob data
|
/// Decode blob data
|
||||||
pub fn decode(self, config: Option<&CryptConfig>) -> Result<Vec<u8>, Error> {
|
pub fn decode(&self, config: Option<&CryptConfig>) -> Result<Vec<u8>, Error> {
|
||||||
|
|
||||||
let magic = self.magic();
|
let magic = self.magic();
|
||||||
|
|
||||||
@ -311,7 +311,9 @@ impl DataBlob {
|
|||||||
/// Verify digest and data length for unencrypted chunks.
|
/// Verify digest and data length for unencrypted chunks.
|
||||||
///
|
///
|
||||||
/// To do that, we need to decompress data first. Please note that
|
/// To do that, we need to decompress data first. Please note that
|
||||||
/// this is not possible for encrypted chunks.
|
/// this is not possible for encrypted chunks. This function simply return Ok
|
||||||
|
/// for encrypted chunks.
|
||||||
|
/// Note: This does not call verify_crc
|
||||||
pub fn verify_unencrypted(
|
pub fn verify_unencrypted(
|
||||||
&self,
|
&self,
|
||||||
expected_chunk_size: usize,
|
expected_chunk_size: usize,
|
||||||
@ -320,22 +322,18 @@ impl DataBlob {
|
|||||||
|
|
||||||
let magic = self.magic();
|
let magic = self.magic();
|
||||||
|
|
||||||
let verify_raw_data = |data: &[u8]| {
|
if magic == &ENCR_COMPR_BLOB_MAGIC_1_0 || magic == &ENCRYPTED_BLOB_MAGIC_1_0 {
|
||||||
if expected_chunk_size != data.len() {
|
return Ok(());
|
||||||
bail!("detected chunk with wrong length ({} != {})", expected_chunk_size, data.len());
|
}
|
||||||
}
|
|
||||||
let digest = openssl::sha::sha256(data);
|
|
||||||
if &digest != expected_digest {
|
|
||||||
bail!("detected chunk with wrong digest.");
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
};
|
|
||||||
|
|
||||||
if magic == &COMPRESSED_BLOB_MAGIC_1_0 {
|
let data = self.decode(None)?;
|
||||||
let data = zstd::block::decompress(&self.raw_data[12..], 16*1024*1024)?;
|
|
||||||
verify_raw_data(&data)?;
|
if expected_chunk_size != data.len() {
|
||||||
} else if magic == &UNCOMPRESSED_BLOB_MAGIC_1_0 {
|
bail!("detected chunk with wrong length ({} != {})", expected_chunk_size, data.len());
|
||||||
verify_raw_data(&self.raw_data[12..])?;
|
}
|
||||||
|
let digest = openssl::sha::sha256(&data);
|
||||||
|
if &digest != expected_digest {
|
||||||
|
bail!("detected chunk with wrong digest.");
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
Reference in New Issue
Block a user