hff_core/write/
hff_desc.rs

1use super::{ChunkArray, DataArray, TableArray};
2use crate::{Chunk, Header, Table};
3
4/// Description of hff and content.
5#[derive(Debug)]
6pub struct HffDesc<'a> {
7    /// The tables.
8    tables: TableArray,
9    /// The chunks.
10    chunks: ChunkArray,
11    /// The data blob.
12    data: Option<DataArray<'a>>,
13}
14
15impl<'a> HffDesc<'a> {
16    /// Create a new content instance.
17    pub fn new(tables: TableArray, chunks: ChunkArray, data: DataArray<'a>) -> Self {
18        Self {
19            tables,
20            chunks,
21            data: Some(data),
22        }
23    }
24
25    /// Finish the descriptor and return the component parts.
26    pub fn finish(self) -> (TableArray, ChunkArray, DataArray<'a>) {
27        (self.tables, self.chunks, self.data.unwrap())
28    }
29
30    /// Update tables and chunks for the given offset and length data.
31    pub fn update_data(
32        tables: &mut TableArray,
33        chunks: &mut ChunkArray,
34        offset: u64,
35        offset_len: &[(u64, u64)],
36    ) {
37        let mut table_index = 0;
38        let mut chunk_index = 0;
39        let mut chunk_count = 0;
40        let mut entry = 0;
41
42        loop {
43            if chunk_count > 0 {
44                // We are filling in chunks.
45                *chunks[chunk_index].offset_mut() = offset_len[entry].0 + offset;
46                *chunks[chunk_index].length_mut() = offset_len[entry].1;
47
48                chunk_count -= 1;
49                entry += 1;
50                chunk_index += 1;
51                continue;
52            }
53
54            if table_index == tables.len() {
55                break;
56            }
57
58            if tables[table_index].0 {
59                // We have metadata in this table.
60                *tables[table_index].1.metadata_offset_mut() = offset_len[entry].0 + offset;
61                *tables[table_index].1.metadata_length_mut() = offset_len[entry].1;
62                entry += 1;
63            }
64
65            chunk_count = tables[table_index].1.chunk_count();
66            table_index += 1;
67        }
68    }
69    /// Size of the content arrays.
70    pub fn arrays_size(&self) -> usize {
71        self.tables.len() * Table::SIZE + self.chunks.len() * Chunk::SIZE
72    }
73
74    /// Calculate the offset from the start of the file to the start of
75    /// the data blob.
76    pub fn offset_to_blob(&self) -> usize {
77        Header::SIZE + self.arrays_size()
78    }
79}