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
use std::io::Cursor;

use byteorder::{LittleEndian, ReadBytesExt};
use failure::{ensure, format_err, Error};
use log::debug;

use crate::model::{
    owned::{ComplexEntry, Entry, EntryHeader, SimpleEntry, TableTypeBuf},
    TableType,
};

pub use self::configuration::{ConfigurationWrapper, Region};

mod configuration;

#[derive(Debug)]
pub struct TableTypeWrapper<'a> {
    raw_data: &'a [u8],
    data_offset: u64,
}

impl<'a> TableTypeWrapper<'a> {
    pub fn new(raw_data: &'a [u8], data_offset: u64) -> Self {
        Self {
            raw_data,
            data_offset,
        }
    }

    pub fn to_buffer(&self) -> Result<TableTypeBuf, Error> {
        let id = self.get_id()?;
        let amount = self.get_amount()?;
        let config = self.get_configuration()?.to_buffer()?;
        let mut owned = TableTypeBuf::new(id & 0xF, config);

        for i in 0..amount {
            let entry = self.get_entry(i)?;
            owned.add_entry(entry);
        }

        Ok(owned)
    }

    pub fn get_entries(&self) -> Result<Vec<Entry>, Error> {
        let mut cursor = Cursor::new(self.raw_data);
        cursor.set_position(self.data_offset);

        self.decode_entries(&mut cursor)
    }

    fn decode_entries(&self, cursor: &mut Cursor<&[u8]>) -> Result<Vec<Entry>, Error> {
        let mut offsets = Vec::new();
        let mut entries = Vec::new();

        for _ in 0..self.get_amount()? {
            offsets.push(cursor.read_u32::<LittleEndian>()?);
        }

        for i in 0..self.get_amount()? {
            let id = i & 0xFFFF;

            if offsets[i as usize] == 0xFFFF_FFFF {
                entries.push(Entry::Empty(id, id));
            } else {
                let maybe_entry = Self::decode_entry(cursor, id)?;

                if let Some(e) = maybe_entry {
                    entries.push(e);
                } else {
                    debug!("Entry with a negative count");
                }
            }
        }

        Ok(entries)
    }

    fn decode_entry(cursor: &mut Cursor<&[u8]>, id: u32) -> Result<Option<Entry>, Error> {
        let header_size = cursor.read_u16::<LittleEndian>()?;
        let flags = cursor.read_u16::<LittleEndian>()?;
        let key_index = cursor.read_u32::<LittleEndian>()?;
        let header_entry = EntryHeader::new(header_size, flags, key_index);

        if header_entry.is_complex() {
            Self::decode_complex_entry(cursor, header_entry, id)
        } else {
            Self::decode_simple_entry(cursor, header_entry, id)
        }
    }

    fn decode_simple_entry(
        cursor: &mut Cursor<&[u8]>,
        header: EntryHeader,
        id: u32,
    ) -> Result<Option<Entry>, Error> {
        cursor.read_u16::<LittleEndian>()?;
        // Padding
        cursor.read_u8()?;
        let val_type = cursor.read_u8()?;
        let data = cursor.read_u32::<LittleEndian>()?;

        let simple = SimpleEntry::new(id, header.get_key_index(), val_type, data);
        let entry = Entry::Simple(simple);

        Ok(Some(entry))
    }

    fn decode_complex_entry(
        cursor: &mut Cursor<&[u8]>,
        header: EntryHeader,
        id: u32,
    ) -> Result<Option<Entry>, Error> {
        let parent_entry = cursor.read_u32::<LittleEndian>()?;
        let value_count = cursor.read_u32::<LittleEndian>()?;
        let mut entries = Vec::with_capacity(value_count as usize);

        if value_count == 0xFFFF_FFFF {
            return Ok(None);
        }

        for j in 0..value_count {
            debug!(
                "Parsing value: {}/{} (@{})",
                j,
                value_count - 1,
                cursor.position()
            );
            let val_id = cursor.read_u32::<LittleEndian>()?;
            cursor.read_u16::<LittleEndian>()?;
            // Padding
            cursor.read_u8()?;
            let val_type = cursor.read_u8()?;
            let data = cursor.read_u32::<LittleEndian>()?;

            let simple_entry = SimpleEntry::new(val_id, header.get_key_index(), val_type, data);

            entries.push(simple_entry);
        }

        let complex = ComplexEntry::new(id, header.get_key_index(), parent_entry, entries);
        let entry = Entry::Complex(complex);

        Ok(Some(entry))
    }
}

impl<'a> TableType for TableTypeWrapper<'a> {
    type Configuration = ConfigurationWrapper<'a>;

    fn get_id(&self) -> Result<u8, Error> {
        let mut cursor = Cursor::new(self.raw_data);
        cursor.set_position(8);
        let out_value = cursor.read_u32::<LittleEndian>()? & 0xF;

        Ok(out_value as u8)
    }

    fn get_amount(&self) -> Result<u32, Error> {
        let mut cursor = Cursor::new(self.raw_data);
        cursor.set_position(12);

        Ok(cursor.read_u32::<LittleEndian>()?)
    }

    fn get_configuration(&self) -> Result<Self::Configuration, Error> {
        let ini = 20;
        let end = self.data_offset as usize;

        ensure!(
            ini <= end
                && (end - ini) > 28
                && self.raw_data.len() >= ini
                && self.raw_data.len() >= end,
            "configuration slice is not valid"
        );

        let slice = &self.raw_data[ini..end];
        let wrapper = ConfigurationWrapper::new(slice);

        Ok(wrapper)
    }

    fn get_entry(&self, index: u32) -> Result<Entry, Error> {
        let entries = self.get_entries()?;
        entries
            .get(index as usize)
            .cloned()
            .ok_or_else(|| format_err!("entry not found"))
    }
}