dream_archive 1.0.0

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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
use super::{
    ArchiveFlags, ArchiveTypes, ArchiveVersion, Error, HashFields, Result, hash_directory,
    hash_file,
};
use crate::BString;
use crate::{
    CompressionOverride,
    bsa::{FilenameEncoding, encode_filename},
    builder_fs,
};
use flate2::{Compression, write::ZlibEncoder};
use std::{
    borrow::Cow,
    collections::HashSet,
    fs::{self, File},
    io::{self, BufWriter, Cursor, Seek, Write},
    path::{Path, PathBuf},
    sync::Arc,
};

const MAGIC: u32 = u32::from_le_bytes(*b"BSA\0");
const HEADER_SIZE: u32 = 0x24;
const FOLDER_RECORD_SIZE_V104: usize = 16;
const FOLDER_RECORD_SIZE_V105: usize = 24;
const FILE_RECORD_SIZE: usize = 16;

/// Builder for string-backed TES4-family BSA archives.
///
/// Directory strings and file strings are present, and output order is
/// deterministic by TES4 hashes. Embedded names and hash-only output remain
/// separate features, not flags to accidentally trip over.
#[derive(Clone, Debug)]
pub struct Builder {
    version: ArchiveVersion,
    archive_types: ArchiveTypes,
    compressed: bool,
    name_mode: NameMode,
    zlib_level: Compression,
    entries: Vec<BuilderEntry>,
    paths: HashSet<(BString, BString)>,
}

/// How the TES4 writer stores recoverable path names.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum NameMode {
    /// Write folder and file string tables.
    #[default]
    Strings,
    /// Omit string tables and rely on folder/file hashes for lookup.
    HashOnly,
    /// Write full virtual paths next to file payloads, without string tables.
    Embedded,
    /// Write both string tables and embedded full virtual paths.
    StringsAndEmbedded,
}

/// PC game-oriented TES4-family writer preset.
///
/// Profiles intentionally set only the layout generation that is intrinsic to a
/// game family. Compression, archive type bits, and name storage mode remain
/// explicit policy choices because real tools produce different combinations.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GameProfile {
    /// The Elder Scrolls IV: Oblivion PC BSA archives.
    Oblivion,
    /// Fallout 3 PC BSA archives, excluding `XMem` output.
    Fallout3,
    /// Fallout: New Vegas PC BSA archives, excluding `XMem` output.
    FalloutNewVegas,
    /// The Elder Scrolls V: Skyrim Legendary Edition PC BSA archives.
    SkyrimLe,
    /// Skyrim Special/Anniversary Edition PC BSA archives.
    SkyrimSe,
}

impl GameProfile {
    const fn version(self) -> ArchiveVersion {
        match self {
            Self::Oblivion => ArchiveVersion::v103,
            Self::Fallout3 | Self::FalloutNewVegas | Self::SkyrimLe => ArchiveVersion::v104,
            Self::SkyrimSe => ArchiveVersion::v105,
        }
    }
}

impl NameMode {
    const fn directory_strings(self) -> bool {
        matches!(self, Self::Strings | Self::StringsAndEmbedded)
    }

    const fn file_strings(self) -> bool {
        matches!(self, Self::Strings | Self::StringsAndEmbedded)
    }

    const fn embedded_file_names(self) -> bool {
        matches!(self, Self::Embedded | Self::StringsAndEmbedded)
    }
}

#[derive(Clone, Debug)]
struct BuilderEntry {
    folder: BString,
    name: BString,
    folder_hash: HashFields,
    file_hash: HashFields,
    compression: CompressionOverride,
    source: EntrySource,
}

#[derive(Clone, Debug)]
enum EntrySource {
    Bytes(Vec<u8>),
    File {
        path: PathBuf,
        len: u64,
    },
    ArchiveEntry {
        archive: Arc<super::Archive>,
        id: super::EntryId,
        len: u64,
    },
}

struct PreparedEntry<'a> {
    entry: &'a BuilderEntry,
    stored: Cow<'a, [u8]>,
}

#[derive(Clone, Copy)]
struct SortedFolder<'a> {
    name: &'a [u8],
    hash: HashFields,
    files_start: usize,
    files_end: usize,
}

impl Default for Builder {
    fn default() -> Self {
        Self {
            version: ArchiveVersion::v104,
            archive_types: ArchiveTypes::MISC,
            compressed: false,
            name_mode: NameMode::Strings,
            zlib_level: Compression::default(),
            entries: Vec::new(),
            paths: HashSet::new(),
        }
    }
}

impl Builder {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a builder configured for a PC game archive family.
    ///
    /// The preset selects the archive version. It deliberately leaves
    /// compression disabled, archive type as [`ArchiveTypes::MISC`], and name
    /// storage as [`NameMode::Strings`]. Those are archive policy decisions, not
    /// inherent consequences of the game name.
    #[must_use]
    pub fn with_profile(profile: GameProfile) -> Self {
        let mut builder = Self::new();
        builder.set_profile(profile);
        builder
    }

    #[must_use]
    pub fn oblivion() -> Self {
        Self::with_profile(GameProfile::Oblivion)
    }

    #[must_use]
    pub fn fallout3() -> Self {
        Self::with_profile(GameProfile::Fallout3)
    }

    #[must_use]
    pub fn fallout_new_vegas() -> Self {
        Self::with_profile(GameProfile::FalloutNewVegas)
    }

    #[must_use]
    pub fn skyrim_le() -> Self {
        Self::with_profile(GameProfile::SkyrimLe)
    }

    #[must_use]
    pub fn skyrim_se() -> Self {
        Self::with_profile(GameProfile::SkyrimSe)
    }

    pub fn set_profile(&mut self, profile: GameProfile) -> &mut Self {
        self.version = profile.version();
        self
    }

    #[must_use]
    pub fn version(&self) -> ArchiveVersion {
        self.version
    }

    pub fn set_version(&mut self, version: ArchiveVersion) -> &mut Self {
        self.version = version;
        self
    }

    #[must_use]
    pub fn archive_types(&self) -> ArchiveTypes {
        self.archive_types
    }

    pub fn set_archive_types(&mut self, archive_types: ArchiveTypes) -> &mut Self {
        self.archive_types = archive_types;
        self
    }

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

    pub fn set_compressed(&mut self, compressed: bool) -> &mut Self {
        self.compressed = compressed;
        self
    }

    #[must_use]
    pub fn name_mode(&self) -> NameMode {
        self.name_mode
    }

    pub fn set_name_mode(&mut self, name_mode: NameMode) -> &mut Self {
        self.name_mode = name_mode;
        self
    }

    #[must_use]
    pub fn zlib_level(&self) -> Compression {
        self.zlib_level
    }

    /// The level zlib compresses members at in a version 103 or 104 archive (105
    /// uses LZ4), 6 unless set. [`Compression`] is `flate2`'s, re-exported as
    /// `dream_archive::flate2`.
    pub fn set_zlib_level(&mut self, level: Compression) -> &mut Self {
        self.zlib_level = level;
        self
    }

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

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

    /// Add an owned copy of one raw file payload.
    ///
    /// # Errors
    ///
    /// Returns an error if the path can not be represented safely, is a
    /// duplicate after TES4 archive normalization, or allocation fails.
    pub fn add_bytes(&mut self, path: impl AsRef<[u8]>, bytes: impl AsRef<[u8]>) -> Result<()> {
        self.add_bytes_with_compression(path, bytes, CompressionOverride::Inherit)
    }

    /// Add bytes with an explicit per-file compression policy.
    ///
    /// TES4 stores overrides as the archive compression flag plus, when needed,
    /// the per-file compression toggle bit.
    ///
    /// # Errors
    ///
    /// Returns an error if the path can not be represented safely, is a
    /// duplicate after TES4 archive normalization, or allocation fails.
    pub fn add_bytes_with_compression(
        &mut self,
        path: impl AsRef<[u8]>,
        bytes: impl AsRef<[u8]>,
        compression: CompressionOverride,
    ) -> Result<()> {
        let (folder, name) = normalize_stored_path(path.as_ref())?;
        let path_key = (folder.clone(), name.clone());
        if self.paths.contains(&path_key) {
            return Err(Error::DuplicatePath);
        }
        let mut owned = Vec::new();
        owned.try_reserve_exact(bytes.as_ref().len())?;
        owned.extend_from_slice(bytes.as_ref());
        self.add_source(
            folder,
            name,
            path_key,
            compression,
            EntrySource::Bytes(owned),
        )
    }

    /// Record a filesystem file and store it at `archive_path` when written.
    ///
    /// Symlinked files are followed for payload bytes; the archive path is the
    /// path supplied by the caller.
    ///
    /// # Errors
    ///
    /// Returns an error if source metadata lookup fails or adding the archive entry fails. Payload read errors are reported when writing.
    pub fn add_file(
        &mut self,
        archive_path: impl AsRef<[u8]>,
        source: impl AsRef<Path>,
    ) -> Result<()> {
        self.add_file_with_compression(archive_path, source, CompressionOverride::Inherit)
    }

    /// Record a filesystem file with a per-file compression policy.
    ///
    /// # Errors
    ///
    /// Returns an error if source metadata lookup fails or adding the archive entry fails. Payload read errors are reported when writing.
    pub fn add_file_with_compression(
        &mut self,
        archive_path: impl AsRef<[u8]>,
        source: impl AsRef<Path>,
        compression: CompressionOverride,
    ) -> Result<()> {
        let source = source.as_ref();
        let len = fs::metadata(source)?.len();
        let (folder, name) = normalize_stored_path(archive_path.as_ref())?;
        let path_key = (folder.clone(), name.clone());
        self.add_source(
            folder,
            name,
            path_key,
            compression,
            EntrySource::File {
                path: source.to_path_buf(),
                len,
            },
        )
    }

    /// Preserve an entry from another TES4-family archive without materializing
    /// it in the builder. The entry is decoded and stored according to this
    /// builder's compression policy when written.
    ///
    /// # Errors
    ///
    /// Returns an error if the archive path is invalid, duplicated, or the source id is invalid.
    pub fn add_archive_entry(
        &mut self,
        archive_path: impl AsRef<[u8]>,
        archive: Arc<super::Archive>,
        id: super::EntryId,
    ) -> Result<()> {
        self.add_archive_entry_with_compression(
            archive_path,
            archive,
            id,
            CompressionOverride::Inherit,
        )
    }

    /// Preserve an entry from another TES4-family archive with an explicit
    /// compression policy for the new archive.
    ///
    /// # Errors
    ///
    /// Returns an error if the archive path is invalid, duplicated, or the source id is invalid.
    pub fn add_archive_entry_with_compression(
        &mut self,
        archive_path: impl AsRef<[u8]>,
        archive: Arc<super::Archive>,
        id: super::EntryId,
        compression: CompressionOverride,
    ) -> Result<()> {
        let len = archive.extracted_len_by_id(id)?;
        let (folder, name) = normalize_stored_path(archive_path.as_ref())?;
        let path_key = (folder.clone(), name.clone());
        self.add_source(
            folder,
            name,
            path_key,
            compression,
            EntrySource::ArchiveEntry { archive, id, len },
        )
    }

    /// Recursively add all files below `root` using paths relative to `root`.
    ///
    /// File symlinks are followed for payload bytes, but stored at the relative
    /// path where the symlink was found. Directory symlinks are ignored. Paths
    /// are taken from the platform filesystem bytes where available; use
    /// [`Self::add_encoded_path`] when a legacy BSA filename encoding is required.
    ///
    /// # Errors
    ///
    /// Returns an error if directory traversal, source metadata lookup, or adding an entry fails. Payload read errors are reported when writing.
    pub fn add_dir(&mut self, root: impl AsRef<Path>) -> Result<()> {
        self.add_dir_with_compression(root, CompressionOverride::Inherit)
    }

    /// Recursively add a directory with a per-file compression policy.
    ///
    /// File symlinks are followed for payload bytes. Directory symlinks are
    /// ignored.
    ///
    /// # Errors
    ///
    /// Returns an error if directory traversal, source metadata lookup, or adding an entry fails. Payload read errors are reported when writing.
    pub fn add_dir_with_compression(
        &mut self,
        root: impl AsRef<Path>,
        compression: CompressionOverride,
    ) -> Result<()> {
        let root = root.as_ref();
        for path in builder_fs::collect_files(root)? {
            let relative = path
                .strip_prefix(root)
                .map_err(|_| Error::InvalidArchivePath)?;
            let archive_path =
                builder_fs::path_to_archive_bytes(relative).ok_or(Error::InvalidArchivePath)?;
            self.add_file_with_compression(archive_path, &path, compression)?;
        }
        Ok(())
    }

    /// Encode a Unicode archive path with an explicit legacy filename encoding,
    /// then add the payload.
    ///
    /// # Errors
    ///
    /// Returns an error if `path` can not be encoded losslessly, the encoded
    /// path is invalid for a TES4 archive, is a duplicate, or allocation fails.
    pub fn add_encoded_path(
        &mut self,
        path: &str,
        encoding: FilenameEncoding,
        bytes: impl AsRef<[u8]>,
    ) -> Result<()> {
        let encoded = encode_filename(path, encoding)?;
        self.add_bytes(encoded.as_ref(), bytes)
    }

    /// Write the archive to a filesystem path.
    ///
    /// # Errors
    ///
    /// Returns an error if creating/writing the file fails or archive integer
    /// fields overflow their TES4 on-disk sizes.
    pub fn write_path(&self, path: impl AsRef<Path>) -> Result<()> {
        let file = File::create(path)?;
        self.write_seek(BufWriter::new(file))
    }

    /// Write the archive to a byte vector.
    ///
    /// # Errors
    ///
    /// Returns an error if archive integer fields overflow their TES4 on-disk
    /// sizes, output allocation fails, a deferred source can not be read, or
    /// embedded file names are asked of a version 103 archive.
    pub fn to_vec(&self) -> Result<Vec<u8>> {
        let mut out = Cursor::new(Vec::new());
        self.write_seek(&mut out)?;
        Ok(out.into_inner())
    }

    /// Write the archive to `out`.
    ///
    /// # Errors
    ///
    /// Returns an error if writing fails, archive integer fields overflow their
    /// TES4 on-disk sizes, a deferred source can not be read, or embedded file
    /// names are asked of a version 103 archive.
    pub fn write_seek<W: Write + Seek>(&self, mut out: W) -> Result<()> {
        if self.name_mode.embedded_file_names() && self.version == ArchiveVersion::v103 {
            return Err(Error::NotImplemented(
                "TES4 embedded file names require version 104 or 105",
            ));
        }
        let entries = self.sorted_entries();
        let prepared = self.prepare_entries(&entries)?;
        let folders = sorted_folders(&entries);
        let folder_names_len = if self.name_mode.directory_strings() {
            folder_names_len(&folders)?
        } else {
            0
        };
        let file_names_len = if self.name_mode.file_strings() {
            file_names_len(&entries)?
        } else {
            0
        };
        let data_offset = data_offset(
            entries.len(),
            folders.len(),
            folder_names_len,
            file_names_len,
            self.version,
            self.name_mode,
        )?;

        write_u32(&mut out, MAGIC)?;
        write_u32(&mut out, self.version as u32)?;
        write_u32(&mut out, HEADER_SIZE)?;
        write_u32(
            &mut out,
            if self.name_mode.directory_strings() {
                ArchiveFlags::DIRECTORY_STRINGS.bits()
            } else {
                0
            } | if self.name_mode.file_strings() {
                ArchiveFlags::FILE_STRINGS.bits()
            } else {
                0
            } | if self.name_mode.embedded_file_names() {
                ArchiveFlags::EMBEDDED_FILE_NAMES.bits()
            } else {
                0
            } | if self.compressed {
                ArchiveFlags::COMPRESSED.bits()
            } else {
                0
            },
        )?;
        write_u32(&mut out, folders.len().try_into()?)?;
        write_u32(&mut out, entries.len().try_into()?)?;
        write_u32(&mut out, folder_names_len.try_into()?)?;
        write_u32(&mut out, file_names_len.try_into()?)?;
        write_u16(&mut out, self.archive_types.bits())?;
        write_u16(&mut out, 0)?;

        let mut folder_block_offset = usize::try_from(HEADER_SIZE)?
            .checked_add(folder_record_size(self.version) * folders.len())
            .ok_or(Error::OutOfBounds)?;
        for folder in &folders {
            write_folder_record(&mut out, self.version, folder, folder_block_offset)?;
            folder_block_offset = folder_block_offset
                .checked_add(if self.name_mode.directory_strings() {
                    1 + folder.name.len() + 1
                } else {
                    0
                })
                .and_then(|offset| {
                    offset.checked_add(FILE_RECORD_SIZE * (folder.files_end - folder.files_start))
                })
                .ok_or(Error::OutOfBounds)?;
        }

        let mut payload_offset: u32 = data_offset.try_into()?;
        for folder in &folders {
            if self.name_mode.directory_strings() {
                write_bzstring(&mut out, folder.name)?;
            }
            for entry in &prepared[folder.files_start..folder.files_end] {
                write_hash(&mut out, entry.entry.file_hash)?;
                let file_size = entry.file_size(self.compressed, self.name_mode)?;
                write_u32(&mut out, file_size)?;
                write_u32(&mut out, payload_offset)?;
                payload_offset = payload_offset
                    .checked_add(file_size & !(1 << 30 | 1 << 31))
                    .ok_or(Error::OutOfBounds)?;
            }
        }

        if self.name_mode.file_strings() {
            for entry in &entries {
                out.write_all(&entry.name)?;
                out.write_all(&[0])?;
            }
        }
        for entry in &prepared {
            if self.name_mode.embedded_file_names() {
                write_bstring(&mut out, &entry.entry.embedded_name()?)?;
            }
            out.write_all(&entry.stored)?;
        }
        Ok(())
    }

    fn add_source(
        &mut self,
        folder: BString,
        name: BString,
        path_key: (BString, BString),
        compression: CompressionOverride,
        source: EntrySource,
    ) -> Result<()> {
        if self.paths.contains(&path_key) {
            return Err(Error::DuplicatePath);
        }
        let folder_hash = hash_directory(&folder).0;
        let file_hash = hash_file(&name).0;
        self.entries.try_reserve(1)?;
        self.paths.try_reserve(1)?;
        self.paths.insert(path_key);
        self.entries.push(BuilderEntry {
            folder,
            name,
            folder_hash,
            file_hash,
            compression,
            source,
        });
        Ok(())
    }

    fn prepare_entries<'a>(&self, entries: &[&'a BuilderEntry]) -> Result<Vec<PreparedEntry<'a>>> {
        let mut prepared = Vec::new();
        prepared.try_reserve_exact(entries.len())?;
        for entry in entries {
            let stored = if entry.is_compressed(self.compressed) {
                Cow::Owned(compressed_payload(
                    self.version,
                    &entry.source.to_vec()?,
                    self.zlib_level,
                )?)
            } else {
                Cow::Owned(entry.source.to_vec()?)
            };
            prepared.push(PreparedEntry { entry, stored });
        }
        Ok(prepared)
    }

    fn sorted_entries(&self) -> Vec<&BuilderEntry> {
        let mut entries: Vec<_> = self.entries.iter().collect();
        entries.sort_by(|left, right| {
            left.folder_hash
                .numeric()
                .cmp(&right.folder_hash.numeric())
                .then_with(|| left.folder.cmp(&right.folder))
                .then_with(|| left.file_hash.numeric().cmp(&right.file_hash.numeric()))
                .then_with(|| left.name.cmp(&right.name))
        });
        entries
    }
}

impl PreparedEntry<'_> {
    fn file_size(&self, default_compressed: bool, name_mode: NameMode) -> Result<u32> {
        const RESERVED_FILE_SIZE_BITS: usize = (1 << 30) | (1 << 31);
        let embedded_len = if name_mode.embedded_file_names() {
            self.entry.embedded_name()?.len() + 1
        } else {
            0
        };
        let stored_len = self
            .stored
            .len()
            .checked_add(embedded_len)
            .ok_or(Error::OutOfBounds)?;
        if stored_len & RESERVED_FILE_SIZE_BITS != 0 {
            return Err(Error::OutOfBounds);
        }
        let mut size: u32 = stored_len.try_into()?;
        if self.entry.is_compressed(default_compressed) != default_compressed {
            size |= 1 << 30;
        }
        Ok(size)
    }
}

impl EntrySource {
    fn len(&self) -> u64 {
        match self {
            Self::Bytes(bytes) => bytes.len() as u64,
            Self::File { len, .. } | Self::ArchiveEntry { len, .. } => *len,
        }
    }

    fn copy_to(&self, out: &mut impl Write) -> Result<u64> {
        match self {
            Self::Bytes(bytes) => {
                out.write_all(bytes)?;
                Ok(bytes.len().try_into()?)
            }
            Self::File { path, len } => {
                let mut file = File::open(path)?;
                let copied = io::copy(&mut file, out)?;
                if copied == *len {
                    Ok(copied)
                } else {
                    Err(Error::Io(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "deferred source file size changed before archive write",
                    )))
                }
            }
            Self::ArchiveEntry { archive, id, len } => {
                let copied = archive.extract_entry_by_id(*id, out)?;
                if copied == *len {
                    Ok(copied)
                } else {
                    Err(Error::OutOfBounds)
                }
            }
        }
    }

    fn to_vec(&self) -> Result<Vec<u8>> {
        let mut out = Vec::new();
        out.try_reserve_exact(self.len().try_into()?)?;
        self.copy_to(&mut out)?;
        Ok(out)
    }
}

impl BuilderEntry {
    fn is_compressed(&self, default_compressed: bool) -> bool {
        match self.compression {
            CompressionOverride::Inherit => default_compressed,
            CompressionOverride::Store => false,
            CompressionOverride::Compress => true,
        }
    }

    fn embedded_name(&self) -> Result<BString> {
        if self.folder.is_empty() {
            return Ok(self.name.clone());
        }
        let mut out = Vec::new();
        out.try_reserve_exact(self.folder.len() + 1 + self.name.len())?;
        out.extend_from_slice(&self.folder);
        out.push(b'\\');
        out.extend_from_slice(&self.name);
        Ok(BString::from(out))
    }
}

fn sorted_folders<'a>(entries: &[&'a BuilderEntry]) -> Vec<SortedFolder<'a>> {
    let mut folders = Vec::new();
    let mut start = 0;
    while start < entries.len() {
        let folder = entries[start].folder.as_slice();
        let hash = entries[start].folder_hash;
        let mut end = start + 1;
        while end < entries.len() && entries[end].folder.as_slice() == folder {
            end += 1;
        }
        folders.push(SortedFolder {
            name: folder,
            hash,
            files_start: start,
            files_end: end,
        });
        start = end;
    }
    folders
}

fn folder_names_len(folders: &[SortedFolder<'_>]) -> Result<usize> {
    folders.iter().try_fold(0usize, |sum, folder| {
        sum.checked_add(folder.name.len() + 1)
            .ok_or(Error::OutOfBounds)
    })
}

fn file_names_len(entries: &[&BuilderEntry]) -> Result<usize> {
    entries.iter().try_fold(0usize, |sum, entry| {
        sum.checked_add(entry.name.len() + 1)
            .ok_or(Error::OutOfBounds)
    })
}

fn data_offset(
    file_count: usize,
    folder_count: usize,
    folder_names_len: usize,
    file_names_len: usize,
    version: ArchiveVersion,
    name_mode: NameMode,
) -> Result<usize> {
    usize::try_from(HEADER_SIZE)?
        .checked_add(folder_record_size(version) * folder_count)
        .and_then(|offset| {
            offset.checked_add(if name_mode.directory_strings() {
                folder_count + folder_names_len
            } else {
                0
            })
        })
        .and_then(|offset| offset.checked_add(FILE_RECORD_SIZE * file_count))
        .and_then(|offset| {
            offset.checked_add(if name_mode.file_strings() {
                file_names_len
            } else {
                0
            })
        })
        .ok_or(Error::OutOfBounds)
}

fn folder_record_size(version: ArchiveVersion) -> usize {
    match version {
        ArchiveVersion::v103 | ArchiveVersion::v104 => FOLDER_RECORD_SIZE_V104,
        ArchiveVersion::v105 => FOLDER_RECORD_SIZE_V105,
    }
}

fn write_folder_record(
    out: &mut impl Write,
    version: ArchiveVersion,
    folder: &SortedFolder<'_>,
    file_records_offset: usize,
) -> Result<()> {
    write_hash(out, folder.hash)?;
    write_u32(out, (folder.files_end - folder.files_start).try_into()?)?;
    match version {
        ArchiveVersion::v103 | ArchiveVersion::v104 => {
            write_u32(out, file_records_offset.try_into()?)?;
        }
        ArchiveVersion::v105 => {
            write_u32(out, 0)?;
            write_u64(out, file_records_offset.try_into()?)?;
        }
    }
    Ok(())
}

fn compressed_payload(
    version: ArchiveVersion,
    bytes: &[u8],
    zlib_level: Compression,
) -> Result<Vec<u8>> {
    let mut out = Vec::new();
    out.try_reserve_exact(bytes.len().saturating_add(4))?;
    out.extend_from_slice(&u32::try_from(bytes.len())?.to_le_bytes());
    match version {
        ArchiveVersion::v103 | ArchiveVersion::v104 => {
            let mut encoder = ZlibEncoder::new(Vec::new(), zlib_level);
            encoder.write_all(bytes)?;
            out.extend_from_slice(&encoder.finish()?);
        }
        ArchiveVersion::v105 => {
            let mut encoder = lz4_flex::frame::FrameEncoder::new(Vec::new());
            encoder.write_all(bytes)?;
            out.extend_from_slice(
                &encoder
                    .finish()
                    .map_err(|error| Error::Lz4Frame(error.to_string()))?,
            );
        }
    }
    Ok(out)
}

fn normalize_stored_path(path: &[u8]) -> Result<(BString, BString)> {
    if path.is_empty() {
        return Err(Error::InvalidArchivePath);
    }
    let mut components = Vec::new();
    for component in path.split(|byte| matches!(*byte, b'/' | b'\\')) {
        if component.is_empty() || component == b"." {
            continue;
        }
        if component == b".." || component.contains(&0) || component.contains(&b':') {
            return Err(Error::InvalidArchivePath);
        }
        components.push(component);
    }
    let Some(name) = components.pop() else {
        return Err(Error::InvalidArchivePath);
    };
    if name.is_empty() {
        return Err(Error::InvalidArchivePath);
    }
    let mut folder = Vec::new();
    folder.try_reserve_exact(path.len())?;
    for (index, component) in components.iter().enumerate() {
        if index != 0 {
            folder.push(b'\\');
        }
        extend_lowercase(&mut folder, component);
    }
    let mut name_out = Vec::new();
    name_out.try_reserve_exact(name.len())?;
    extend_lowercase(&mut name_out, name);
    Ok((BString::from(folder), BString::from(name_out)))
}

fn extend_lowercase(out: &mut Vec<u8>, bytes: &[u8]) {
    out.extend(bytes.iter().copied().map(|byte| match byte {
        b'A'..=b'Z' => byte + 32,
        _ => byte,
    }));
}

fn write_bzstring(out: &mut impl Write, bytes: &[u8]) -> Result<()> {
    let len = bytes.len().checked_add(1).ok_or(Error::OutOfBounds)?;
    out.write_all(&[len.try_into()?])?;
    out.write_all(bytes)?;
    out.write_all(&[0])?;
    Ok(())
}

fn write_bstring(out: &mut impl Write, bytes: &[u8]) -> Result<()> {
    out.write_all(&[bytes.len().try_into()?])?;
    out.write_all(bytes)?;
    Ok(())
}

fn write_hash(out: &mut impl Write, hash: HashFields) -> Result<()> {
    out.write_all(&hash.numeric().to_le_bytes())?;
    Ok(())
}

fn write_u16(out: &mut impl Write, value: u16) -> Result<()> {
    out.write_all(&value.to_le_bytes())?;
    Ok(())
}

fn write_u32(out: &mut impl Write, value: u32) -> Result<()> {
    out.write_all(&value.to_le_bytes())?;
    Ok(())
}

fn write_u64(out: &mut impl Write, value: u64) -> Result<()> {
    out.write_all(&value.to_le_bytes())?;
    Ok(())
}