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
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
// SPDX-License-Identifier: GPL-3.0-or-later

use std::io::{Cursor, Seek, SeekFrom};

use binrw::binrw;
use binrw::{BinRead, Endian};

use crate::common::Language;
use crate::exh::{ColumnDataType, ExcelColumnDefinition, ExcelDataPagination, EXH};
use crate::ByteSpan;

#[binrw]
#[brw(magic = b"EXDF")]
#[brw(big)]
#[allow(dead_code)]
struct EXDHeader {
    version: u16,

    #[br(pad_before = 2)]
    #[br(pad_after = 20)]
    index_size: u32,
}

#[binrw]
#[brw(big)]
struct ExcelDataOffset {
    row_id: u32,
    pub offset: u32,
}

#[binrw]
#[brw(big)]
#[allow(dead_code)]
struct ExcelDataRowHeader {
    data_size: u32,
    row_count: u16,
}

#[binrw]
#[brw(big)]
#[allow(dead_code)]
pub struct EXD {
    header: EXDHeader,

    #[br(count = header.index_size / core::mem::size_of::<ExcelDataOffset>() as u32)]
    data_offsets: Vec<ExcelDataOffset>,

    #[brw(ignore)]
    pub rows: Vec<ExcelRow>,
}

#[derive(Debug)]
pub enum ColumnData {
    String(String),
    Bool(bool),
    Int8(i8),
    UInt8(u8),
    Int16(i16),
    UInt16(u16),
    Int32(i32),
    UInt32(u32),
    Float32(f32),
    Int64(i64),
    UInt64(u64),
}

pub struct ExcelRow {
    pub data: Vec<ColumnData>,
}

impl EXD {
    pub fn from_existing(exh: &EXH, buffer: ByteSpan) -> Option<EXD> {
        let mut cursor = Cursor::new(buffer);
        let mut exd = EXD::read(&mut cursor).ok()?;

        for i in 0..exh.header.row_count {
            for offset in &exd.data_offsets {
                if offset.row_id == i {
                    cursor.seek(SeekFrom::Start(offset.offset.into())).ok()?;

                    let row_header = ExcelDataRowHeader::read(&mut cursor).ok()?;

                    let header_offset = offset.offset + 6; // std::mem::size_of::<ExcelDataRowHeader>() as u32;

                    let mut read_row = |row_offset: u32| -> Option<ExcelRow> {
                        let mut subrow = ExcelRow {
                            data: Vec::with_capacity(exh.column_definitions.len()),
                        };

                        for column in &exh.column_definitions {
                            cursor
                                .seek(SeekFrom::Start((row_offset + column.offset as u32).into()))
                                .ok()?;

                            subrow.data.push(
                                Self::read_column(&mut cursor, exh, row_offset, column).unwrap(),
                            );
                        }

                        Some(subrow)
                    };

                    if row_header.row_count > 1 {
                        for i in 0..row_header.row_count {
                            let subrow_offset =
                                header_offset + (i * exh.header.data_offset + 2 * (i + 1)) as u32;

                            exd.rows.push(read_row(subrow_offset).unwrap());
                        }
                    } else {
                        exd.rows.push(read_row(header_offset).unwrap());
                    }
                }
            }
        }

        Some(exd)
    }

    fn read_data_raw<Z: BinRead<Args<'static> = ()>>(cursor: &mut Cursor<ByteSpan>) -> Option<Z> {
        Z::read_options(cursor, Endian::Big, ()).ok()
    }

    fn read_column(
        cursor: &mut Cursor<ByteSpan>,
        exh: &EXH,
        row_offset: u32,
        column: &ExcelColumnDefinition,
    ) -> Option<ColumnData> {
        let mut read_packed_bool = |shift: i32| -> bool {
            let bit = 1 << shift;
            let bool_data: i32 = Self::read_data_raw(cursor).unwrap_or(0);

            (bool_data & bit) == bit
        };

        match column.data_type {
            ColumnDataType::String => {
                let string_offset: u32 = Self::read_data_raw(cursor).unwrap();

                cursor
                    .seek(SeekFrom::Start(
                        (row_offset + exh.header.data_offset as u32 + string_offset).into(),
                    ))
                    .ok()?;

                let mut string = String::new();

                let mut byte: u8 = Self::read_data_raw(cursor).unwrap();
                while byte != 0 {
                    string.push(byte as char);
                    byte = Self::read_data_raw(cursor).unwrap();
                }

                Some(ColumnData::String(string))
            }
            ColumnDataType::Bool => {
                // FIXME: i believe Bool is int8?
                let bool_data: i32 = Self::read_data_raw(cursor).unwrap();

                Some(ColumnData::Bool(bool_data == 1))
            }
            ColumnDataType::Int8 => Some(ColumnData::Int8(Self::read_data_raw(cursor).unwrap())),
            ColumnDataType::UInt8 => Some(ColumnData::UInt8(Self::read_data_raw(cursor).unwrap())),
            ColumnDataType::Int16 => Some(ColumnData::Int16(Self::read_data_raw(cursor).unwrap())),
            ColumnDataType::UInt16 => {
                Some(ColumnData::UInt16(Self::read_data_raw(cursor).unwrap()))
            }
            ColumnDataType::Int32 => Some(ColumnData::Int32(Self::read_data_raw(cursor).unwrap())),
            ColumnDataType::UInt32 => {
                Some(ColumnData::UInt32(Self::read_data_raw(cursor).unwrap()))
            }
            ColumnDataType::Float32 => {
                Some(ColumnData::Float32(Self::read_data_raw(cursor).unwrap()))
            }
            ColumnDataType::Int64 => Some(ColumnData::Int64(Self::read_data_raw(cursor).unwrap())),
            ColumnDataType::UInt64 => {
                Some(ColumnData::UInt64(Self::read_data_raw(cursor).unwrap()))
            }
            ColumnDataType::PackedBool0 => Some(ColumnData::Bool(read_packed_bool(0))),
            ColumnDataType::PackedBool1 => Some(ColumnData::Bool(read_packed_bool(1))),
            ColumnDataType::PackedBool2 => Some(ColumnData::Bool(read_packed_bool(2))),
            ColumnDataType::PackedBool3 => Some(ColumnData::Bool(read_packed_bool(3))),
            ColumnDataType::PackedBool4 => Some(ColumnData::Bool(read_packed_bool(4))),
            ColumnDataType::PackedBool5 => Some(ColumnData::Bool(read_packed_bool(5))),
            ColumnDataType::PackedBool6 => Some(ColumnData::Bool(read_packed_bool(6))),
            ColumnDataType::PackedBool7 => Some(ColumnData::Bool(read_packed_bool(7))),
        }
    }

    pub fn calculate_filename(
        name: &str,
        language: Language,
        page: &ExcelDataPagination,
    ) -> String {
        use crate::common::get_language_code;

        match language {
            Language::None => {
                format!("{name}_{}.exd", page.start_id)
            }
            lang => {
                format!("{name}_{}_{}.exd", page.start_id, get_language_code(&lang))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::exh::EXHHeader;
    use std::fs::read;
    use std::path::PathBuf;

    use super::*;

    #[test]
    fn test_invalid() {
        let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        d.push("resources/tests");
        d.push("random");

        let exh = EXH {
            header: EXHHeader {
                version: 0,
                data_offset: 0,
                column_count: 0,
                page_count: 0,
                language_count: 0,
                row_count: 0,
            },
            column_definitions: vec![],
            pages: vec![],
            languages: vec![],
        };

        // Feeding it invalid data should not panic
        EXD::from_existing(&exh, &read(d).unwrap());
    }
}