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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use crate::{
    containers::Bytes,
    derive,
    io::{Endian, Sink, Source},
    protocols::ZString,
    tes3::{self, Error, File, FileHash, Hash, Result},
};
use bstr::BString;
use std::io::Write;

mod constants {
    pub(crate) const FILE_ENTRY_SIZE: usize = 0x8;
    pub(crate) const HASH_SIZE: usize = 0x8;
    pub(crate) const HEADER_MAGIC: u32 = 0x100;
    pub(crate) const HEADER_SIZE: usize = 0xC;
}

struct Offsets {
    name_offsets: usize,
    names: usize,
    hashes: usize,
    file_data: usize,
}

struct Header {
    hash_offset: u32,
    file_count: u32,
}

impl Header {
    #[must_use]
    fn compute_offsets(&self) -> Offsets {
        let file_count = self.file_count as usize;
        let name_offsets = constants::HEADER_SIZE + constants::FILE_ENTRY_SIZE * file_count;
        let names = name_offsets + 0x4 * file_count;
        let hashes = constants::HEADER_SIZE + self.hash_offset as usize;
        let file_data = hashes + constants::HASH_SIZE * file_count;
        Offsets {
            name_offsets,
            names,
            hashes,
            file_data,
        }
    }
}

derive::key!(Key: FileHash);

impl<'bytes> Key<'bytes> {
    #[must_use]
    fn hash_in_place(name: &mut BString) -> FileHash {
        tes3::hash_file_in_place(name)
    }
}

type ReadResult<T> = T;
derive::archive! {
    /// Represents the TES3 revision of the bsa format.
    Archive => ReadResult
    Map: (Key: FileHash) => File
}

impl<'bytes> Archive<'bytes> {
    pub fn write<Out>(&self, stream: &mut Out) -> Result<()>
    where
        Out: ?Sized + Write,
    {
        let mut sink = Sink::new(stream);
        let header = self.make_header()?;
        Self::write_header(&mut sink, &header)?;
        self.write_files(&mut sink)?;
        self.write_name_offsets(&mut sink)?;
        self.write_names(&mut sink)?;
        self.write_hashes(&mut sink)?;
        self.write_file_data(&mut sink)?;

        Ok(())
    }

    fn make_header(&self) -> Result<Header> {
        Ok(Header {
            file_count: self.map.len().try_into()?,
            hash_offset: {
                let names_offset = 0xC * self.map.len();
                let names_len: usize = self.map.keys().map(|x| x.name().len() + 1).sum();
                (names_offset + names_len).try_into()?
            },
        })
    }

    fn write_files<Out>(&self, sink: &mut Sink<Out>) -> Result<()>
    where
        Out: ?Sized + Write,
    {
        let mut offset: u32 = 0;
        for file in self.map.values() {
            let size: u32 = file.bytes.len().try_into()?;
            sink.write(&(size, offset), Endian::Little)?;
            offset += size;
        }

        Ok(())
    }

    fn write_file_data<Out>(&self, sink: &mut Sink<Out>) -> Result<()>
    where
        Out: ?Sized + Write,
    {
        for file in self.map.values() {
            sink.write_bytes(file.as_bytes())?;
        }

        Ok(())
    }

    fn write_hashes<Out>(&self, sink: &mut Sink<Out>) -> Result<()>
    where
        Out: ?Sized + Write,
    {
        for key in self.map.keys() {
            let hash = &key.hash();
            sink.write(&(hash.lo, hash.hi), Endian::Little)?;
        }

        Ok(())
    }

    fn write_header<Out>(sink: &mut Sink<Out>, header: &Header) -> Result<()>
    where
        Out: ?Sized + Write,
    {
        sink.write(
            &(
                constants::HEADER_MAGIC,
                header.hash_offset,
                header.file_count,
            ),
            Endian::Little,
        )?;
        Ok(())
    }

    fn write_name_offsets<Out>(&self, sink: &mut Sink<Out>) -> Result<()>
    where
        Out: ?Sized + Write,
    {
        let mut offset: u32 = 0;
        for key in self.map.keys() {
            sink.write(&offset, Endian::Little)?;
            offset += u32::try_from(key.name().len() + 1)?;
        }

        Ok(())
    }

    fn write_names<Out>(&self, sink: &mut Sink<Out>) -> Result<()>
    where
        Out: ?Sized + Write,
    {
        for key in self.map.keys() {
            sink.write_protocol::<ZString>(key.name(), Endian::Little)?;
        }

        Ok(())
    }

    fn do_read<In>(source: &mut In) -> Result<ReadResult<Self>>
    where
        In: ?Sized + Source<'bytes>,
    {
        let header = Self::read_header(source)?;
        let offsets = header.compute_offsets();
        let mut map = Map::default();

        for i in 0..header.file_count as usize {
            let (key, value) = Self::read_file(source, i, &offsets)?;
            map.insert(key, value);
        }

        Ok(Self { map })
    }

    fn read_file<In>(
        source: &mut In,
        idx: usize,
        offsets: &Offsets,
    ) -> Result<(Key<'bytes>, File<'bytes>)>
    where
        In: ?Sized + Source<'bytes>,
    {
        let hash = source.save_restore_position(|source| -> Result<Hash> {
            source.seek_absolute(offsets.hashes + constants::HASH_SIZE * idx)?;
            Self::read_hash(source)
        })??;

        let name = source.save_restore_position(|source| -> Result<Bytes<'bytes>> {
            source.seek_absolute(offsets.name_offsets + 0x4 * idx)?;
            let offset: u32 = source.read(Endian::Little)?;
            source.seek_absolute(offsets.names + offset as usize)?;
            let name = source.read_protocol::<ZString>(Endian::Little)?;
            Ok(name)
        })??;

        let (size, offset): (u32, u32) = source.read(Endian::Little)?;
        let container = source.save_restore_position(|source| -> Result<Bytes<'bytes>> {
            source.seek_absolute(offsets.file_data + offset as usize)?;
            let result = source.read_bytes(size as usize)?;
            Ok(result)
        })??;

        Ok((
            Key {
                hash: hash.into(),
                name,
            },
            File { bytes: container },
        ))
    }

    fn read_hash<In>(source: &mut In) -> Result<Hash>
    where
        In: ?Sized + Source<'bytes>,
    {
        let (lo, hi) = source.read(Endian::Little)?;
        Ok(Hash { lo, hi })
    }

    fn read_header<In>(source: &mut In) -> Result<Header>
    where
        In: ?Sized + Source<'bytes>,
    {
        let (magic, hash_offset, file_count) = source.read(Endian::Little)?;
        match magic {
            constants::HEADER_MAGIC => Ok(Header {
                hash_offset,
                file_count,
            }),
            _ => Err(Error::InvalidMagic(magic)),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        prelude::*,
        tes3::{Archive, ArchiveKey, Error, File, FileHash, Hash},
        Borrowed,
    };
    use anyhow::Context as _;
    use bstr::BString;
    use memmap2::Mmap;
    use std::{
        ffi::OsStr,
        fs,
        io::{self, Read as _},
        path::Path,
    };
    use walkdir::WalkDir;

    #[test]
    fn default_state() -> anyhow::Result<()> {
        let bsa = Archive::new();
        assert!(bsa.is_empty());
        assert!(bsa.len() == 0);
        Ok(())
    }

    #[test]
    fn invalid_magic() -> anyhow::Result<()> {
        let path = Path::new("data/tes3_invalid_test/invalid_magic.bsa");
        match Archive::read(path) {
            Err(Error::InvalidMagic(0x200)) => Ok(()),
            Err(err) => Err(err.into()),
            Ok(_) => anyhow::bail!("read should have failed"),
        }
    }

    #[test]
    fn invalid_out_of_bounds() -> anyhow::Result<()> {
        let path = Path::new("data/tes3_invalid_test/invalid_exhausted.bsa");
        match Archive::read(path) {
            Err(Error::Io(io)) => {
                assert_eq!(io.kind(), io::ErrorKind::UnexpectedEof);
                Ok(())
            }
            Err(err) => Err(err.into()),
            Ok(_) => anyhow::bail!("read should have failed"),
        }
    }

    #[test]
    fn reading() -> anyhow::Result<()> {
        let root_path = Path::new("data/tes3_read_test/");
        let archive = {
            let archive_path = root_path.join("test.bsa");
            Archive::read(archive_path.as_path())
                .with_context(|| format!("failed to read from archive: {archive_path:?}"))?
        };

        for file_path in WalkDir::new(root_path) {
            if let Ok(file_path) = file_path {
                let metadata = file_path
                    .metadata()
                    .context("failed to get file path metadata")?;
                if metadata.is_file() && file_path.path().extension() != Some(OsStr::new("bsa")) {
                    let key = file_path
                        .path()
                        .strip_prefix(root_path)
                        .with_context(|| {
                            format!(
                                "failed to strip prefix ({root_path:?}) from path ({file_path:?})"
                            )
                        })?
                        .as_os_str();
                    let file = archive
                        .get(&ArchiveKey::from(key.as_encoded_bytes()))
                        .with_context(|| format!("failed to get file with key: {key:?}"))?;
                    assert_eq!(file.len() as u64, metadata.len());

                    let mut original_data = Vec::new();
                    fs::File::open(file_path.path())
                        .with_context(|| format!("failed to open file: {file_path:?}"))?
                        .read_to_end(&mut original_data)
                        .with_context(|| format!("failed to read from file: {file_path:?}"))?;
                    assert_eq!(file.as_bytes(), &original_data[..]);
                }
            }
        }

        Ok(())
    }

    #[test]
    fn writing() -> anyhow::Result<()> {
        struct Info {
            key: ArchiveKey<'static>,
            path: &'static Path,
        }

        impl Info {
            fn new(lo: u32, hi: u32, path: &'static str) -> Self {
                let hash = Hash { lo, hi };
                let key = ArchiveKey::from(path);
                assert_eq!(&hash, key.hash());
                Self {
                    key,
                    path: Path::new(path),
                }
            }
        }

        let infos = [
            Info::new(0x0C18356B, 0xA578DB74, "Tiles/tile_0001.png"),
            Info::new(0x1B0D3416, 0xF5D5F30E, "Share/License.txt"),
            Info::new(0x1B3B140A, 0x07B36E53, "Background/background_middle.png"),
            Info::new(0x29505413, 0x1EB4CED7, "Construct 3/Pixel Platformer.c3p"),
            Info::new(0x4B7D031B, 0xD4701AD4, "Tilemap/characters_packed.png"),
            Info::new(0x74491918, 0x2BEBCD0A, "Characters/character_0001.png"),
        ];

        let mmapped = {
            let mut result = Vec::<Mmap>::new();
            for info in &infos {
                let file_path = Path::new("data/tes3_write_test/data").join(info.path);
                let fd = fs::File::open(file_path.clone())
                    .with_context(|| format!("failed to open file: {file_path:?}"))?;
                let file = unsafe {
                    Mmap::map(&fd)
                        .with_context(|| format!("failed to memory map file: {file_path:?}"))?
                };
                result.push(file);
            }
            result
        };

        let stream = {
            let mut archive = Archive::new();
            for (data, info) in mmapped.iter().zip(&infos) {
                let file: File = data[..].into();
                assert!(archive.insert(info.key.clone(), file).is_none());
            }
            let mut result = Vec::<u8>::new();
            archive
                .write(&mut result)
                .context("failed to write test archive to memory")?;
            result
        };

        let archive =
            Archive::read(Borrowed(&stream)).context("failed to read from archive in memory")?;
        for (data, info) in mmapped.iter().zip(&infos) {
            let file = archive.get(info.key.hash()).with_context(|| {
                format!("failed to get value from archive with key: {:?}", info.path)
            })?;
            assert_eq!(file.as_bytes(), &data[..]);
        }

        Ok(())
    }

    #[test]
    fn assert_generic_interfaces_compile() -> anyhow::Result<()> {
        let mut bsa = Archive::default();
        let key = ArchiveKey::default();
        let hash = FileHash::default();

        _ = bsa.get(&key);
        _ = bsa.get(&hash);

        _ = bsa.remove(&key);
        _ = bsa.remove(&hash);

        _ = bsa.remove_entry(&key);
        _ = bsa.remove_entry(&hash);

        _ = bsa.insert(key, Default::default());
        _ = bsa.insert(BString::default(), Default::default());

        Ok(())
    }
}