urna_format/layout/
footer.rs1use crate::error::UrnaError;
6use sha2::{Digest, Sha256};
7
8use super::URNA_FOOTER_SIZE;
9
10#[repr(C)]
15#[derive(Clone, Copy, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
16pub struct UrnaFooter {
17 pub footer_size: u64,
18 pub file_hash: [u8; 32],
19}
20
21impl UrnaFooter {
22 pub fn new(file_hash: [u8; 32]) -> Self {
23 Self {
24 footer_size: URNA_FOOTER_SIZE as u64,
25 file_hash,
26 }
27 }
28
29 pub fn as_bytes(&self) -> &[u8] {
32 bytemuck::bytes_of(self)
33 }
34
35 pub fn as_bytes_mut(&mut self) -> &mut [u8] {
39 bytemuck::bytes_of_mut(self)
40 }
41
42 pub fn compute_file_hash(data_without_footer: &[u8]) -> [u8; 32] {
43 let hash = Sha256::digest(data_without_footer);
44 let mut out = [0u8; 32];
45 out.copy_from_slice(&hash[..]);
46 out
47 }
48
49 pub fn from_bytes(bytes: &[u8]) -> crate::Result<Self> {
50 if bytes.len() < std::mem::size_of::<Self>() {
51 return Err(UrnaError::UnexpectedEof);
52 }
53 let mut footer = UrnaFooter::new([0; 32]);
54 footer.as_bytes_mut().copy_from_slice(bytes);
55 Ok(footer)
56 }
57}