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
//! The file header.
use super::Semver;
use crate::{Ecc, Endian, Error, Result, NATIVE_ENDIAN};
use byteorder::{BigEndian, ByteOrder, LittleEndian, ReadBytesExt, WriteBytesExt};
use std::io::{Read, Write};

/// The current version of the format.
pub const FORMAT_VERSION: Semver = Semver::new(0, 1, 0);

/// The file header.
#[repr(C)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Header {
    /// Magic identifier.  Ecc::HFF_MAGIC
    magic: Ecc,
    /// Version of the file format.
    version: Semver,
    /// The overall content type of this file.
    content: Ecc,
    /// Total count of tables in the header.
    table_count: u32,
    /// Total count of chunks in the header.
    chunk_count: u32,
}

impl Header {
    /// Size of the header.
    pub const SIZE: usize = std::mem::size_of::<Self>();

    /// Create a new instance.
    pub fn new(content: Ecc, table_count: u32, chunk_count: u32) -> Self {
        Self {
            magic: Ecc::HFF_MAGIC,
            version: FORMAT_VERSION,
            content,
            table_count,
            chunk_count,
        }
    }

    /// Check that this is a valid file header.
    pub fn is_valid(&self) -> bool {
        match self.magic.endian(Ecc::HFF_MAGIC) {
            Some(endian) => {
                if endian == NATIVE_ENDIAN {
                    self.version == FORMAT_VERSION
                } else {
                    self.version.swap_bytes() == FORMAT_VERSION
                }
            }
            None => false,
        }
    }

    /// Get the container version.
    pub fn version(&self) -> Semver {
        self.version
    }

    /// What's the endian?
    pub fn is_native_endian(&self) -> bool {
        self.magic == Ecc::HFF_MAGIC
    }

    /// Get the table count.
    pub fn table_count(&self) -> u32 {
        self.table_count
    }

    /// Get the chunk count.
    pub fn chunk_count(&self) -> u32 {
        self.chunk_count
    }

    /// Read from a given stream.
    pub fn read(reader: &mut dyn Read) -> Result<Self> {
        let mut magic = [0_u8; 8];
        reader.read_exact(&mut magic)?;

        // Detect the file content endianess.  NOTE: This only describes
        // the file structure itself, the chunk content is "not" considered
        // part of this.  It is up to the user to deal with endianess of
        // the chunks.
        match Ecc::HFF_MAGIC.endian(magic.into()) {
            Some(endian) => match endian {
                Endian::Little => Ok(Self {
                    magic: magic.into(),
                    version: Semver::read::<LittleEndian>(reader)?,
                    content: Ecc::read::<LittleEndian>(reader)?,
                    table_count: reader.read_u32::<LittleEndian>()?,
                    chunk_count: reader.read_u32::<LittleEndian>()?,
                }),
                Endian::Big => Ok(Self {
                    magic: magic.into(),
                    version: Semver::read::<BigEndian>(reader)?,
                    content: Ecc::read::<BigEndian>(reader)?,
                    table_count: reader.read_u32::<BigEndian>()?,
                    chunk_count: reader.read_u32::<BigEndian>()?,
                }),
            },
            None => Err(Error::Invalid("Not an HFF file.".into())),
        }
    }

    /// Write to the given stream.
    pub fn write<E: ByteOrder>(self, writer: &mut dyn Write) -> Result<()> {
        self.magic.write::<E>(writer)?;
        self.version.write::<E>(writer)?;
        self.content.write::<E>(writer)?;
        writer.write_u32::<E>(self.table_count)?;
        writer.write_u32::<E>(self.chunk_count)?;
        Ok(())
    }

    /// A test helper.  Swapping the bytes like this only makes sense for
    /// testing because the read adjusts to endianess after reading only
    /// the magic and not the rest.
    #[cfg(test)]
    pub fn swap_bytes(&self) -> Self {
        Self {
            magic: self.magic.swap_bytes(),
            version: self.version.swap_bytes(),
            content: self.content.swap_bytes(),
            table_count: self.table_count.swap_bytes(),
            chunk_count: self.chunk_count.swap_bytes(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{NE, OP};

    #[test]
    fn test_struct_layout() {
        assert_eq!(std::mem::size_of::<Header>(), 32);
    }

    #[test]
    fn validation() {
        assert!(Header::new(Ecc::new("test"), 0, 0).is_valid());
        assert!(Header::new(Ecc::new("test"), 0, 0).is_native_endian());
        assert!(Header::new(Ecc::new("test"), 0, 0).swap_bytes().is_valid());
        assert!(!Header::new(Ecc::new("test"), 0, 0)
            .swap_bytes()
            .is_native_endian());
    }

    #[test]
    fn serialization() {
        {
            let header = Header::new("Test".into(), 1, 2);
            let mut buffer = vec![];
            assert!(header.write::<NE>(&mut buffer).is_ok());
            let dup = Header::read(&mut buffer.as_slice()).unwrap();
            assert_eq!(dup.magic, Ecc::HFF_MAGIC);
            assert_eq!(dup.version, Semver::new(0, 1, 0));
            assert_eq!(dup.content, Ecc::new("Test"));
            assert_eq!(dup.table_count, 1);
            assert_eq!(dup.chunk_count, 2);
        }

        {
            let header = Header::new("Test".into(), 1, 2);
            let mut buffer = vec![];
            assert!(header.write::<OP>(&mut buffer).is_ok());
            let dup = Header::read(&mut buffer.as_slice()).unwrap();
            assert_eq!(dup.magic, Ecc::HFF_MAGIC.swap_bytes());
            assert_eq!(dup.version, Semver::new(0, 1, 0));
            assert_eq!(dup.content, Ecc::new("Test"));
            assert_eq!(dup.table_count, 1);
            assert_eq!(dup.chunk_count, 2);
        }
    }
}