1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use hff_core::{ByteOrder, Chunk, Ecc, Header, Result, Table, NE, OP};
use std::{
    fmt::Debug,
    io::{Cursor, Read, Seek, SeekFrom},
    mem::size_of,
};

/// Trait for types that can be used as a source of chunks.
pub trait ReadSeek: Read + Seek {}

// Implement over anything which can read and seek.
impl<T: Read + Seek> ReadSeek for T {}

/// Act as a read/seek IO object for purposes of having
/// an entire HFF in memory at one time.
#[derive(Debug, Clone)]
pub struct ChunkCache {
    offset: u64,
    buffer: Cursor<Vec<u8>>,
}

impl ChunkCache {
    /// Create a new chunk cache.
    pub fn new(offset: usize, buffer: Vec<u8>) -> Self {
        Self {
            offset: offset as u64,
            buffer: Cursor::new(buffer),
        }
    }
}

impl Read for ChunkCache {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        self.buffer.read(buf)
    }
}

impl Seek for ChunkCache {
    fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
        // Adjust the position if it is from the start because we want
        // to act as if we are in the file 'after' the header+tables.
        let pos = match pos {
            SeekFrom::Current(p) => SeekFrom::Current(p),
            SeekFrom::Start(p) => SeekFrom::Start(p - self.offset),
            SeekFrom::End(p) => SeekFrom::End(p),
        };
        self.buffer.seek(pos)
    }
}

///
#[derive(Debug, Clone, PartialEq)]
pub struct Hff {
    tables: Vec<Table>,
    chunks: Vec<Chunk>,
}

impl Hff {
    /// Create a new Hff wrapper.
    pub fn new(tables: impl Into<Vec<Table>>, chunks: impl Into<Vec<Chunk>>) -> Self {
        Self {
            tables: tables.into(),
            chunks: chunks.into(),
        }
    }

    /// Get the offset from the start of the file to the start of the chunk data.
    pub fn offset_to_data(&self) -> usize {
        size_of::<Header>()
            + (size_of::<Table>() * self.tables.len())
            + (size_of::<Chunk>() * self.chunks.len())
    }

    /// Get an iterator over the root level of the content.
    pub fn iter(&self) -> TableIter {
        TableIter::new(self, 0)
    }
}

///
pub struct TableView<'a> {
    hff: &'a Hff,
    index: usize,
}

impl<'a> PartialEq for TableView<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.hff == other.hff && self.index == other.index
    }
}

impl<'a> Debug for TableView<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.hff.tables[self.index])
    }
}

impl<'a> TableView<'a> {
    /// Create a new TableView.
    pub(super) fn new(hff: &'a Hff, index: usize) -> Self {
        Self { hff, index }
    }

    /// Get the primary identifier.
    pub fn primary(&self) -> Ecc {
        self.hff.tables[self.index].primary()
    }

    /// Get the secondary identifier.
    pub fn secondary(&self) -> Ecc {
        self.hff.tables[self.index].secondary()
    }

    /// Get the count of child tables.
    pub fn child_count(&self) -> usize {
        self.hff.tables[self.index].child_count() as usize
    }

    /// Get an iterator to the child tables.
    pub fn iter(&self) -> TableIter<'a> {
        TableIter::new(self.hff, self.index + 1)
    }

    /// Get an iterator of the chunks.
    pub fn chunks(&self) -> ChunkIter<'a> {
        let table = &self.hff.tables[self.index];
        ChunkIter::new(
            self.hff,
            table.chunk_index() as usize,
            table.chunk_count() as usize,
        )
    }

    /// Get the count of chunks in the table.
    pub fn chunk_count(&self) -> usize {
        self.hff.tables[self.index].chunk_count() as usize
    }

    /// Read the metadata from the given source.
    pub fn metadata(&self, source: &mut dyn ReadSeek) -> Result<Vec<u8>> {
        let table = &self.hff.tables[self.index];
        if table.metadata_length() > 0 {
            source.seek(SeekFrom::Start(table.metadata_offset()))?;
            let mut buffer = vec![0; table.metadata_length() as usize];
            source.read_exact(buffer.as_mut_slice())?;
            Ok(buffer)
        } else {
            Ok(vec![])
        }
    }
}

pub struct ChunkView<'a> {
    hff: &'a Hff,
    index: usize,
}

impl<'a> ChunkView<'a> {
    /// Create a new view.
    pub fn new(hff: &'a Hff, index: usize) -> Self {
        Self { hff, index }
    }

    /// Get the primary identifier.
    pub fn primary(&self) -> Ecc {
        self.hff.chunks[self.index].primary()
    }

    /// Get the secondary identifier.
    pub fn secondary(&self) -> Ecc {
        self.hff.chunks[self.index].secondary()
    }

    /// Get the size of the data in the chunk.
    pub fn size(&self) -> usize {
        self.hff.chunks[self.index].length() as usize
    }

    /// Get the data for the chunk from a read/seek source.
    pub fn data(&self, source: &mut dyn ReadSeek) -> Result<Vec<u8>> {
        let chunk = &self.hff.chunks[self.index];
        if chunk.length() > 0 {
            source.seek(SeekFrom::Start(chunk.offset()))?;
            let mut buffer = vec![0; chunk.length() as usize];
            source.read_exact(buffer.as_mut_slice())?;
            Ok(buffer)
        } else {
            Ok(vec![])
        }
    }
}

///
pub struct ChunkIter<'a> {
    hff: &'a Hff,
    current: isize,
    count: usize,
}

impl<'a> ChunkIter<'a> {
    /// Create a new chunk iterator.
    pub fn new(hff: &'a Hff, index: usize, count: usize) -> Self {
        Self {
            hff,
            current: index as isize - 1,
            count,
        }
    }
}

impl<'a> Iterator for ChunkIter<'a> {
    type Item = ChunkView<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.count > 0 {
            self.count -= 1;
            self.current += 1;
            Some(ChunkView::new(self.hff, self.current as usize))
        } else {
            None
        }
    }
}

///
pub struct TableIter<'a> {
    hff: &'a Hff,
    start: Option<usize>,
    index: usize,
}

impl<'a> TableIter<'a> {
    /// Create a new table iterator.
    pub fn new(hff: &'a Hff, start: usize) -> Self {
        Self {
            hff,
            start: Some(start),
            index: 0,
        }
    }
}

impl<'a> Iterator for TableIter<'a> {
    type Item = TableView<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(start) = self.start.take() {
            self.index = start;
            Some(TableView::new(self.hff, self.index))
        } else {
            let sibling = self.hff.tables[self.index].sibling() as usize;
            if sibling == 0 {
                None
            } else {
                let index = self.index + sibling;
                if index >= self.hff.tables.len() {
                    None
                } else {
                    self.index = index;
                    Some(TableView::new(self.hff, self.index))
                }
            }
        }
    }
}

/// Read a HFF from the given stream.
pub fn read_stream(reader: &mut dyn Read) -> Result<Hff> {
    // The header determines the structure endianess.
    let header = Header::read(reader)?;
    if header.is_native_endian() {
        println!("Native endian.");
        let tables = read_tables::<NE>(reader, header.table_count())?;
        let chunks = read_chunks::<NE>(reader, header.chunk_count())?;
        Ok(Hff::new(tables, chunks))
    } else {
        println!("Opposing endian.");
        let tables = read_tables::<OP>(reader, header.table_count())?;
        let chunks = read_chunks::<OP>(reader, header.chunk_count())?;
        Ok(Hff::new(tables, chunks))
    }
}

/// Read a HFF from the given stream along with all the data for the chunks.
pub fn read_stream_full(reader: &mut dyn Read) -> Result<(Hff, ChunkCache)> {
    let hff = read_stream(reader)?;
    let mut buffer = vec![];
    reader.read_to_end(&mut buffer)?;

    let offset = hff.offset_to_data();
    Ok((hff, ChunkCache::new(offset, buffer)))
}

fn read_tables<E: ByteOrder>(reader: &mut dyn Read, count: u32) -> Result<Vec<Table>> {
    if count > 0 {
        // Create a buffer with appropriate size.
        let mut buffer = vec![0; count as usize * size_of::<Table>()];
        reader.read_exact(&mut buffer.as_mut_slice())?;

        // Read all the tables out of the buffer.
        let mut tables = vec![];
        let reader: &mut dyn Read = &mut buffer.as_slice();
        for _ in 0..count {
            let table = Table::read::<E>(reader)?;
            tables.push(table);
        }

        Ok(tables)
    } else {
        // TODO: Does an empty file make sense?  It's not an error but ....
        Ok(vec![])
    }
}

fn read_chunks<E: ByteOrder>(reader: &mut dyn Read, count: u32) -> Result<Vec<Chunk>> {
    if count > 0 {
        // Create a buffer with the appropriate size.
        let mut buffer = vec![0; count as usize * size_of::<Chunk>()];
        reader.read_exact(&mut buffer.as_mut_slice())?;

        // Read the chunks out of the buffer.
        let mut chunks = vec![];
        let reader: &mut dyn Read = &mut buffer.as_slice();
        for _ in 0..count {
            let chunk = Chunk::read::<E>(reader)?;
            chunks.push(chunk);
        }
        Ok(chunks)
    } else {
        // No chunks, perhaps they put all the real data into the metadata so this is
        // still a viable file.
        Ok(vec![])
    }
}