pub fn process_block_integrity<'a>(data_block: &'a mut [u8], integrity: &mut Integrity, block_num: usize) -> Result<&'a [u8], IntegrityError> {
    match integrity {
        Integrity::Crc32 => {
            if data_block.len() < 6 {
                return Err(IntegrityError::Crc32Error { line_num: block_num });
            }
            let crc = crc32::checksum_ieee(&data_block[..data_block.len() - 4]);
            let mut crc_in_file = [0u8; 4];
            crc_in_file.clone_from_slice(&data_block[data_block.len() - 4..]);
            if crc != u32::from_le_bytes(crc_in_file) {
                return Err(IntegrityError::Crc32Error { line_num: block_num });
            }
            Ok(&data_block[..data_block.len() - 4])
        },
        Integrity::Sha1Chain(hash_of_prev) => {
            const HASH_LEN: usize = 20;
            if data_block.len() < HASH_LEN + 1 {
                return Err(IntegrityError::Sha1ChainError { line_num: block_num });
            }
            let data = &data_block[..data_block.len() - HASH_LEN];
            let mut current_hash: [u8; HASH_LEN] = [0; HASH_LEN];
            blockchain_sha1(&hash_of_prev[..], data, &mut current_hash);
            let hash_in_file = &data_block[data_block.len() - HASH_LEN..];
            if current_hash != hash_in_file {
                return Err(IntegrityError::Sha1ChainError { line_num: block_num });
            }
            *hash_of_prev = current_hash;
            Ok(data)
        },
        Integrity::Sha256Chain(hash_of_prev) => {
            const HASH_LEN: usize = 32;
            if data_block.len() < HASH_LEN + 1 {
                return Err(IntegrityError::Sha1ChainError { line_num: block_num });
            }
            let data = &data_block[..data_block.len() - HASH_LEN];
            let mut current_hash: [u8; HASH_LEN] = [0; HASH_LEN];
            blockchain_sha256(&hash_of_prev[..], data, &mut current_hash);
            let hash_in_file = &data_block[data_block.len() - HASH_LEN..];
            if current_hash != hash_in_file {
                return Err(IntegrityError::Sha1ChainError { line_num: block_num });
            }
            *hash_of_prev = current_hash;
            Ok(data)
        },
    }
}
/// Check data integrity after read from file.
Check data integrity after read from file.
Check data integrity after read from file .
pub fn process_block_integrity<'a>(data_block: &'a mut [u8], integrity: &mut Integrity, block_num: usize) -> Result<&'a [u8], IntegrityError> {
    match integrity {
        Integrity::Crc32 => {
            if data_block.len() < 6 {
                return Err(IntegrityError::Crc32Error { line_num: block_num });
            }
            let crc = crc32::checksum_ieee(&data_block[..data_block.len() - 4]);
            let mut crc_in_file = [0u8; 4];
            crc_in_file.clone_from_slice(&data_block[data_block.len() - 4..]);
            if crc != u32::from_le_bytes(crc_in_file) {
                return Err(IntegrityError::Crc32Error { line_num: block_num });
            }
            Ok(&data_block[..data_block.len() - 4])
        },
        Integrity::Sha1Chain(hash_of_prev) => {
            const HASH_LEN: usize = 20;
            if data_block.len() < HASH_LEN + 1 {
                return Err(IntegrityError::Sha1ChainError { line_num: block_num });
            }
            let data = &data_block[..data_block.len() - HASH_LEN];
            let mut current_hash: [u8; HASH_LEN] = [0; HASH_LEN];
            blockchain_sha1(&hash_of_prev[..], data, &mut current_hash);
            let hash_in_file = &data_block[data_block.len() - HASH_LEN..];
            if current_hash != hash_in_file {
                return Err(IntegrityError::Sha1ChainError { line_num: block_num });
            }
            *hash_of_prev = current_hash;
            Ok(data)
        },
        Integrity::Sha256Chain(hash_of_prev) => {
            const HASH_LEN: usize = 32;
            if data_block.len() < HASH_LEN + 1 {
                return Err(IntegrityError::Sha1ChainError { line_num: block_num });
            }
            let data = &data_block[..data_block.len() - HASH_LEN];
            let mut current_hash: [u8; HASH_LEN] = [0; HASH_LEN];
            blockchain_sha256(&hash_of_prev[..], data, &mut current_hash);
            let hash_in_file = &data_block[data_block.len() - HASH_LEN..];
            if current_hash != hash_in_file {
                return Err(IntegrityError::Sha1ChainError { line_num: block_num });
            }
            *hash_of_prev = current_hash;
            Ok(data)
        },
    }
}
pub fn process_block_integrity < ' a > ( data_block : & ' a mut [ u8 ] , integrity : & mut Integrity , block_num : usize ) -> Result < & ' a [ u8 ] , IntegrityError > { match integrity { Integrity :: Crc32 => { if data_block . len ( ) < 6 { return Err ( IntegrityError :: Crc32Error { line_num : block_num } ) ; } let crc = crc32 :: checksum_ieee ( & data_block [ .. data_block . len ( ) - 4 ] ) ; let mut crc_in_file = [ 0u8 ; 4 ] ; crc_in_file . clone_from_slice ( & data_block [ data_block . len ( ) - 4 .. ] ) ; if crc != u32 :: from_le_bytes ( crc_in_file ) { return Err ( IntegrityError :: Crc32Error { line_num : block_num } ) ; } Ok ( & data_block [ .. data_block . len ( ) - 4 ] ) } , Integrity :: Sha1Chain ( hash_of_prev ) => { const HASH_LEN : usize = 20 ; if data_block . len ( ) < HASH_LEN + 1 { return Err ( IntegrityError :: Sha1ChainError { line_num : block_num } ) ; } let data = & data_block [ .. data_block . len ( ) - HASH_LEN ] ; let mut current_hash : [ u8 ; HASH_LEN ] = [ 0 ; HASH_LEN ] ; blockchain_sha1 ( & hash_of_prev [ .. ] , data , & mut current_hash ) ; let hash_in_file = & data_block [ data_block . len ( ) - HASH_LEN .. ] ; if current_hash != hash_in_file { return Err ( IntegrityError :: Sha1ChainError { line_num : block_num } ) ; } * hash_of_prev = current_hash ; Ok ( data ) } , Integrity :: Sha256Chain ( hash_of_prev ) => { const HASH_LEN : usize = 32 ; if data_block . len ( ) < HASH_LEN + 1 { return Err ( IntegrityError :: Sha1ChainError { line_num : block_num } ) ; } let data = & data_block [ .. data_block . len ( ) - HASH_LEN ] ; let mut current_hash : [ u8 ; HASH_LEN ] = [ 0 ; HASH_LEN ] ; blockchain_sha256 ( & hash_of_prev [ .. ] , data , & mut current_hash ) ; let hash_in_file = & data_block [ data_block . len ( ) - HASH_LEN .. ] ; if current_hash != hash_in_file { return Err ( IntegrityError :: Sha1ChainError { line_num : block_num } ) ; } * hash_of_prev = current_hash ; Ok ( data ) } , } }
Check data integrity after read from file.
Check data integrity after read from file .
data_block
&'a mut [u8]
integrity
&mut Integrity
block_num
usize
data_block
&'a mut [u8]
integrity
&mut Integrity
block_num
usize