dream_archive 0.2.3

Pure-Rust Bethesda BA2 and BSA archive reader, extractor, and writer
Documentation
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use super::{Error, Result, parser};
use crate::bsa::{FilenameEncoding, NormalizedPath, decode_filename_lossy, normalize_lookup_path};
use crate::{BStr, BString};
use crate::{
    Copied,
    extract::{
        ensure_parent_dir, output_path_decoded_into, output_path_into, write_file_atomically,
    },
    storage::Storage,
    stream::{self, BoxReader},
};
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

/// Metadata read from a TES3 BSA archive header.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ArchiveInfo {
    pub hash_offset: u32,
    pub file_count: u32,
}

/// Stable identifier for an entry within one parsed TES3 BSA archive.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct EntryId(usize);

impl EntryId {
    #[must_use]
    pub const fn from_index(index: usize) -> Self {
        Self(index)
    }

    #[must_use]
    pub const fn index(self) -> usize {
        self.0
    }
}

/// One file entry in a TES3 BSA archive index.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Entry {
    path: BString,
    lookup_path: BString,
    record: FileRecord,
    hash: u64,
}

impl Entry {
    #[must_use]
    pub fn path(&self) -> &BStr {
        self.path.as_ref()
    }

    #[must_use]
    pub fn file(&self) -> FileRecord {
        self.record
    }

    /// The stored hash as [`FileHash::numeric`](super::FileHash::numeric), the
    /// same value [`hash_file`](super::hash_file) returns for this entry's path.
    #[must_use]
    pub fn hash(&self) -> u64 {
        self.hash
    }
}

/// Raw file location metadata from a TES3 BSA file record.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct FileRecord {
    pub size: u32,
    pub offset: u32,
}

/// Parsed TES3 BSA archive.
#[derive(Clone, Debug)]
pub struct Archive {
    storage: Storage,
    info: ArchiveInfo,
    entries: Vec<Entry>,
    data_offset: usize,
    lookup: HashMap<BString, usize>,
}

impl Archive {
    /// Read an archive from an owned byte buffer.
    ///
    /// # Errors
    ///
    /// Returns an error when the buffer is not a supported TES3 BSA archive.
    pub fn from_vec(bytes: Vec<u8>) -> Result<Self> {
        parser::parse(Storage::from_vec(bytes))
    }

    /// Read an archive from a byte slice, copying the archive data.
    ///
    /// # Errors
    ///
    /// Returns an error when the slice is not a supported TES3 BSA archive.
    pub fn from_slice(bytes: &[u8]) -> Result<Self> {
        Self::from_vec(bytes.to_vec())
    }

    /// Read an archive from a filesystem path using mmap-backed storage.
    ///
    /// # Errors
    ///
    /// Returns an I/O error if the path can not be mapped, or a parse error when
    /// the file is not a supported TES3 BSA archive.
    pub fn open_path(path: impl AsRef<Path>) -> Result<Self> {
        parser::parse(Storage::open_path(path)?)
    }

    #[must_use]
    pub fn info(&self) -> ArchiveInfo {
        self.info
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    #[must_use]
    pub fn entries(&self) -> &[Entry] {
        &self.entries
    }

    pub fn entries_with_ids(&self) -> impl Iterator<Item = (EntryId, &Entry)> {
        self.entries
            .iter()
            .enumerate()
            .map(|(index, entry)| (EntryId(index), entry))
    }

    #[must_use]
    pub fn entry_by_id(&self, id: EntryId) -> Option<&Entry> {
        self.entries.get(id.0)
    }

    /// Get an entry by stable id, returning an error when it is absent.
    ///
    /// # Errors
    ///
    /// Returns [`Error::OutOfBounds`] when `id` does not identify an entry in this archive.
    pub fn entry_by_id_required(&self, id: EntryId) -> Result<&Entry> {
        self.entry_by_id(id).ok_or(Error::OutOfBounds)
    }

    /// Extract an entry selected by stable id into a writer.
    ///
    /// # Errors
    ///
    /// Returns [`Error::OutOfBounds`] for an invalid id, or the same errors as entry extraction.
    pub fn extract_entry_by_id(&self, id: EntryId, out: impl std::io::Write) -> Result<u64> {
        self.extract_entry(self.entry_by_id_required(id)?, out)
    }

    #[must_use]
    pub fn get(&self, path: impl AsRef<[u8]>) -> Option<&Entry> {
        let normalized = normalize_lookup_path(path.as_ref());
        self.lookup
            .get(normalized.as_slice())
            .map(|&index| &self.entries[index])
    }

    /// Get an entry by a path that was normalized once for repeated lookup.
    #[must_use]
    pub fn get_normalized(&self, path: &NormalizedPath) -> Option<&Entry> {
        self.lookup
            .get(path.as_bytes())
            .map(|&index| &self.entries[index])
    }

    /// Get an entry by path, returning an error when it is absent.
    ///
    /// # Errors
    ///
    /// Returns [`Error::FileNotFound`] when the normalized path is not present.
    pub fn get_required(&self, path: impl AsRef<[u8]>) -> Result<&Entry> {
        let path = path.as_ref();
        self.get(path)
            .ok_or_else(|| Error::FileNotFound(BString::from(path)))
    }

    #[must_use]
    pub fn contains(&self, path: impl AsRef<[u8]>) -> bool {
        self.get(path).is_some()
    }

    #[must_use]
    pub fn contains_normalized(&self, path: &NormalizedPath) -> bool {
        self.get_normalized(path).is_some()
    }

    #[must_use]
    pub fn archive_size(&self) -> usize {
        self.storage.as_bytes().len()
    }

    /// Extract an entry into `out`.
    ///
    /// # Errors
    ///
    /// Returns an error if the entry points outside the archive or output
    /// allocation fails.
    pub fn read_entry_into(&self, entry: &Entry, out: &mut Vec<u8>) -> Result<()> {
        let payload = self.entry_payload(entry)?;
        out.try_reserve_exact(payload.len())?;
        out.extend_from_slice(payload);
        Ok(())
    }

    /// Extract an entry into a writer without allocating a payload buffer.
    ///
    /// # Errors
    ///
    /// Returns an error if the entry points outside the archive or writing fails.
    pub fn extract_entry(&self, entry: &Entry, mut out: impl std::io::Write) -> Result<u64> {
        let payload = self.entry_payload(entry)?;
        out.write_all(payload)?;
        Ok(payload.len().try_into()?)
    }

    /// Extract an entry to a filesystem path atomically.
    ///
    /// The payload is written to a temporary file in the destination directory
    /// and renamed into place only after extraction succeeds.
    ///
    /// # Errors
    ///
    /// Returns the same errors as [`Self::extract_entry`], plus filesystem
    /// errors for creating, writing, or renaming the output file.
    pub fn extract_entry_to_path(&self, entry: &Entry, path: impl AsRef<Path>) -> Result<u64> {
        write_file_atomically(path.as_ref(), |file| self.extract_entry(entry, file))
    }

    /// Extract an entry into a new vector.
    ///
    /// # Errors
    ///
    /// Returns the same errors as [`Self::read_entry_into`].
    pub fn read_entry(&self, entry: &Entry) -> Result<Vec<u8>> {
        let mut out = Vec::new();
        self.read_entry_into(entry, &mut out)?;
        Ok(out)
    }

    /// Extract a path into a new vector.
    ///
    /// # Errors
    ///
    /// Returns the same errors as [`Self::read_entry_into`] if the path exists.
    pub fn read_file(&self, path: impl AsRef<[u8]>) -> Result<Option<Vec<u8>>> {
        self.get(path)
            .map(|entry| self.read_entry(entry))
            .transpose()
    }

    /// Extract a required path into a new vector.
    ///
    /// # Errors
    ///
    /// Returns [`Error::FileNotFound`] if the path does not exist, or the same
    /// errors as [`Self::read_entry`] when it does.
    pub fn read_file_required(&self, path: impl AsRef<[u8]>) -> Result<Vec<u8>> {
        self.read_entry(self.get_required(path)?)
    }

    /// Extract a path into a writer without allocating a payload buffer.
    ///
    /// # Errors
    ///
    /// Returns the same errors as [`Self::extract_entry`] if the path exists.
    pub fn extract_file(
        &self,
        path: impl AsRef<[u8]>,
        out: impl std::io::Write,
    ) -> Result<Option<u64>> {
        self.get(path)
            .map(|entry| self.extract_entry(entry, out))
            .transpose()
    }

    /// Extract a required path into a writer without allocating a payload buffer.
    ///
    /// # Errors
    ///
    /// Returns [`Error::FileNotFound`] if the path does not exist, or the same
    /// errors as [`Self::extract_entry`] when it does.
    pub fn extract_file_required(
        &self,
        path: impl AsRef<[u8]>,
        out: impl std::io::Write,
    ) -> Result<u64> {
        self.extract_entry(self.get_required(path)?, out)
    }

    /// Open an entry as a reader over the archive payload.
    ///
    /// # Errors
    ///
    /// Returns an error if the entry points outside the archive.
    pub fn open_entry<'a>(&'a self, entry: &'a Entry) -> Result<BoxReader<'a>> {
        Ok(stream::borrowed_reader(self.entry_payload(entry)?))
    }

    /// Open an optional path as a reader over the archive payload.
    ///
    /// # Errors
    ///
    /// Returns an error if the member exists but points outside the archive.
    pub fn open_file(&self, path: impl AsRef<[u8]>) -> Result<Option<BoxReader<'_>>> {
        self.get(path)
            .map(|entry| self.open_entry(entry))
            .transpose()
    }

    /// Open a required path as a reader over the archive payload.
    ///
    /// # Errors
    ///
    /// Returns [`Error::FileNotFound`] if the path does not exist, or the same
    /// errors as [`Self::open_entry`] when it does.
    pub fn open_file_required(&self, path: impl AsRef<[u8]>) -> Result<BoxReader<'_>> {
        self.open_entry(self.get_required(path)?)
    }

    /// Extract every entry to `target_dir`, preserving archive paths.
    ///
    /// Returns the number of payload bytes written.
    ///
    /// # Errors
    ///
    /// Returns an error if an archive entry has no safe output path, directory
    /// or file creation fails, or entry extraction fails.
    pub fn extract_to(&self, target_dir: impl AsRef<Path>) -> Result<u64> {
        let target_dir = target_dir.as_ref();
        #[cfg(feature = "parallel")]
        {
            self.extract_to_parallel(target_dir)
        }
        #[cfg(not(feature = "parallel"))]
        {
            self.extract_to_sequential(target_dir)
        }
    }

    /// Extract every entry to `target_dir`, decoding archive path bytes with an
    /// explicit filename encoding before creating filesystem paths.
    ///
    /// This is intended for localized archives whose filenames are stored in a
    /// legacy code page rather than UTF-8.
    ///
    /// # Errors
    ///
    /// Returns an error if an archive entry has no safe output path, directory
    /// or file creation fails, or entry extraction fails.
    pub fn extract_to_with_encoding(
        &self,
        target_dir: impl AsRef<Path>,
        encoding: FilenameEncoding,
    ) -> Result<u64> {
        let target_dir = target_dir.as_ref();
        let mut written = 0u64;
        let mut path = PathBuf::new();
        let mut last_parent = PathBuf::new();
        for entry in &self.entries {
            output_path_decoded_into(&mut path, target_dir, entry.path(), |component| {
                decode_filename_lossy(component, encoding)
            })?;
            ensure_parent_dir(&path, &mut last_parent)?;
            written += self.extract_entry_to_path(entry, &path)?;
        }
        Ok(written)
    }

    fn extract_to_sequential(&self, target_dir: &Path) -> Result<u64> {
        let mut written = 0u64;
        let mut path = PathBuf::new();
        let mut last_parent = PathBuf::new();
        for entry in &self.entries {
            output_path_into(&mut path, target_dir, entry.path())?;
            ensure_parent_dir(&path, &mut last_parent)?;
            written += self.extract_entry_to_path(entry, &path)?;
        }
        Ok(written)
    }

    #[cfg(feature = "parallel")]
    fn extract_to_parallel(&self, target_dir: &Path) -> Result<u64> {
        let paths = self.extract_output_paths(target_dir)?;
        if crate::extract::has_duplicate_paths(&paths) {
            return self.extract_to_sequential(target_dir);
        }
        crate::extract::ensure_parent_dirs(&paths)?;
        self.entries
            .par_iter()
            .zip(paths.par_iter())
            .map(|(entry, path)| self.extract_entry_to_path(entry, path))
            .try_reduce(|| 0, |left, right| Ok(left + right))
    }

    #[cfg(feature = "parallel")]
    fn extract_output_paths(&self, target_dir: &Path) -> Result<Vec<PathBuf>> {
        let mut paths = Vec::new();
        paths.try_reserve_exact(self.entries.len())?;
        for entry in &self.entries {
            let mut path = PathBuf::new();
            output_path_into(&mut path, target_dir, entry.path())?;
            paths.push(path);
        }
        Ok(paths)
    }

    pub(super) fn from_parts(
        storage: Storage,
        info: ArchiveInfo,
        entries: Vec<Entry>,
        data_offset: usize,
    ) -> Self {
        let mut lookup = HashMap::with_capacity(entries.len());
        for (index, entry) in entries.iter().enumerate() {
            lookup.entry(entry.lookup_path.clone()).or_insert(index);
        }
        Self {
            storage,
            info,
            entries,
            data_offset,
            lookup,
        }
    }

    fn entry_payload<'a>(&'a self, entry: &Entry) -> Result<&'a [u8]> {
        let relative: usize = entry.record.offset.try_into()?;
        let start = self
            .data_offset
            .checked_add(relative)
            .ok_or(Error::OutOfBounds)?;
        let len: usize = entry.record.size.try_into()?;
        self.storage
            .as_bytes()
            .get(start..start.checked_add(len).ok_or(Error::OutOfBounds)?)
            .ok_or(Error::OutOfBounds)
    }
}

impl Entry {
    pub(super) fn new(path: BString, record: FileRecord, hash: u64) -> Self {
        let lookup_path = BString::from(normalize_lookup_path(&path));
        Self {
            path,
            lookup_path,
            record,
            hash,
        }
    }
}

impl TryFrom<Copied<'_>> for Archive {
    type Error = Error;
    fn try_from(value: Copied<'_>) -> Result<Self> {
        Self::from_slice(value.0)
    }
}