From 0a51fe00113cedd029d80c3c558f50bfafe32b81 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 3 Sep 2019 13:11:45 +0200 Subject: [PATCH] src/backup/fixed_index.rs: new helper to compute checksum and file size --- src/backup/fixed_index.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/backup/fixed_index.rs b/src/backup/fixed_index.rs index 61e0a058..4c1d18e3 100644 --- a/src/backup/fixed_index.rs +++ b/src/backup/fixed_index.rs @@ -1,5 +1,6 @@ use failure::*; use std::io::{Seek, SeekFrom}; +use std::convert::TryInto; use crate::tools; use super::IndexFile; @@ -166,6 +167,15 @@ impl FixedIndexReader { Ok((start, end, unsafe { digest.assume_init() })) } + #[inline] + fn chunk_digest(&self, pos: usize) -> &[u8; 32] { + if pos >= self.index_length { + panic!("chunk index out of range"); + } + let slice = unsafe { std::slice::from_raw_parts(self.index.add(pos*32), 32) }; + slice.try_into().unwrap() + } + #[inline] fn chunk_end(&self, pos: usize) -> u64 { if pos >= self.index_length { @@ -180,6 +190,22 @@ impl FixedIndexReader { } } + /// Compute checksum and data size + pub fn compute_csum(&self) -> ([u8; 32], u64) { + + let mut csum = openssl::sha::Sha256::new(); + let mut chunk_end = 0; + for pos in 0..self.index_length { + chunk_end = ((pos+1) * self.chunk_size) as u64; + let digest = self.chunk_digest(pos); + csum.update(&chunk_end.to_le_bytes()); + csum.update(digest); + } + let csum = csum.finish(); + + (csum, chunk_end) + } + pub fn print_info(&self) { println!("Size: {}", self.size); println!("ChunkSize: {}", self.chunk_size);