whiteoutlib 0.1.1

Read and write Blizzard game assets: models (MDX, M2, M3), textures (BLP, DDS, PNG, ...) and archives (CASC, MPQ).
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026 Fernando Sahmkow

#include <whiteout/storages/mpq/storage.h>

#include "../../common/unicode_path.h"
#include "../../storages/common/jenkins.h"
#include "../../storages/common/mapped_file.h"
#include "../../storages/mpq/crypto.h"
#include "../../storages/mpq/file_data.h"
#include "../../storages/mpq/special_files.h"
#include "../../storages/mpq/tables/block_table.h"
#include "../../storages/mpq/tables/hash_table.h"
#include "../../storages/mpq/tables/header.h"
#include "../../storages/mpq/writer.h"

#include <whiteout/interfaces.h>

#include <filesystem>
#include <fstream>
#include <mutex>
#include <shared_mutex>
#include <unordered_map>
#include <unordered_set>

namespace whiteout::storages::mpq {

using storages::common::normalizePath;

// ============================================================================
// Shared archive-parsing helper
// ============================================================================

namespace {

/// Result of parsing an MPQ archive from a memory-mapped file.
struct ParsedArchive {
    storages::common::MappedFile mapping;
    MpqHeader header;
    size_t archiveOffset = 0;
    HashTable hashTable;
    BlockTable blockTable;
    std::vector<std::string> listfileNames;
};

/// Map, parse header, decrypt tables, and read the listfile from an archive on disk.
/// Returns nullopt on any parse failure.  When @p error is non-null, stores a
/// diagnostic message explaining the first failure.
std::optional<ParsedArchive> parseMappedArchive(const std::string& path,
                                                std::string* error = nullptr) {
    auto setError = [&](const std::string& msg) {
        if (error)
            *error = msg;
    };

    auto mappedFile = storages::common::MappedFile::open(path);
    if (!mappedFile) {
        setError("failed to memory-map the file (file not found, empty, or permission denied)");
        return std::nullopt;
    }

    auto parseResult = findAndParseHeader(mappedFile->data());
    if (!parseResult) {
        setError("no valid MPQ header found (missing MPQ\\x1A signature or header too small)");
        return std::nullopt;
    }

    ParsedArchive pa;
    pa.header = parseResult->header;
    pa.archiveOffset = parseResult->archiveOffset;
    auto archiveSpan = mappedFile->data();

    // Parse hash table.
    u64 const htOffset = pa.archiveOffset + pa.header.hashTableByteOffset();
    u64 const htSize = static_cast<u64>(pa.header.hashTableEntries) * 16;
    if (htOffset + htSize > archiveSpan.size()) {
        setError("hash table extends past end of file (offset 0x" + std::to_string(htOffset) +
                 ", size " + std::to_string(htSize) + ", file size " +
                 std::to_string(archiveSpan.size()) + ")");
        return std::nullopt;
    }
    if (!pa.hashTable.parse(archiveSpan.subspan(htOffset, htSize), pa.header.hashTableEntries)) {
        setError("hash table decryption or validation failed");
        return std::nullopt;
    }

    // Parse block table.
    u64 const btOffset = pa.archiveOffset + pa.header.blockTableByteOffset();
    u64 const btSize = static_cast<u64>(pa.header.blockTableEntries) * 16;
    if (btOffset + btSize > archiveSpan.size()) {
        setError("block table extends past end of file (offset 0x" + std::to_string(btOffset) +
                 ", size " + std::to_string(btSize) + ", file size " +
                 std::to_string(archiveSpan.size()) + ")");
        return std::nullopt;
    }
    if (!pa.blockTable.parse(archiveSpan.subspan(btOffset, btSize), pa.header.blockTableEntries)) {
        setError("block table decryption or validation failed");
        return std::nullopt;
    }

    // Parse hi-block table (V2+).
    if (pa.header.formatVersion >= 1 && pa.header.hiBlockTableOffset != 0) {
        u64 const hiOffset = pa.archiveOffset + pa.header.hiBlockTableOffset;
        u64 const hiSize = static_cast<u64>(pa.header.blockTableEntries) * 2;
        if (hiOffset + hiSize <= archiveSpan.size()) {
            pa.blockTable.parseHiBlockTable(archiveSpan.subspan(hiOffset, hiSize),
                                            pa.header.blockTableEntries);
        }
    }

    // Read (listfile).
    auto lfIdx = pa.hashTable.lookup("(listfile)");
    if (lfIdx) {
        const auto& he = pa.hashTable.entry(*lfIdx);
        if (he.blockIndex < pa.blockTable.count()) {
            const auto& be = pa.blockTable.entry(he.blockIndex);
            u32 const fileKey = be.isEncrypted() ? deriveFileKey("(listfile)", be) : 0;
            auto lfData = extractFileData(mappedFile->data(), pa.archiveOffset, be,
                                          pa.header.sectorSize(), fileKey);
            if (!lfData.empty())
                pa.listfileNames = parseListfile(std::span<const u8>(lfData));
        }
    }

    pa.mapping = std::move(*mappedFile);
    return pa;
}

} // anonymous namespace

// ============================================================================
// Overlay key
// ============================================================================

struct OverlayKey {
    std::string normalizedName;
    u16 locale = 0;

    bool operator==(const OverlayKey& o) const {
        return normalizedName == o.normalizedName && locale == o.locale;
    }
};

struct OverlayKeyHash {
    size_t operator()(const OverlayKey& k) const {
        size_t const h1 = std::hash<std::string>{}(k.normalizedName);
        size_t const h2 = std::hash<u16>{}(k.locale);
        return h1 ^ (h2 * 0x9E3779B97F4A7C15ULL + 0x9E3779B9 + (h1 << 6) + (h1 >> 2));
    }
};

// ============================================================================
// Impl
// ============================================================================

struct Storage::Impl {
    // Source archive (read-only, may be empty for create()-d archives).
    std::optional<storages::common::MappedFile> sourceArchive;
    std::string sourcePath;

    // Parsed tables (read-only snapshot of source archive).
    MpqHeader header{};
    size_t archiveOffset = 0;
    HashTable hashTable;
    BlockTable blockTable;

    // Overlay — pending modifications.
    struct OverlayEntry {
        std::string originalName; // Preserves user-provided casing for the listfile.
        std::vector<u8> data;
        WriteOptions opts;
    };
    std::unordered_map<OverlayKey, OverlayEntry, OverlayKeyHash> pendingWrites;
    std::unordered_set<OverlayKey, OverlayKeyHash> pendingDeletes;

    // Parsed (listfile) from source archive.
    std::vector<std::string> sourceListfileNames;

    // Thread safety.
    mutable std::shared_mutex mutex;

    bool isValid = false;

    // Worker pool for parallel compression/decompression (non-owning, may be null).
    interfaces::WorkerPool* pool = nullptr;

    // -- Helpers --

    /// Extract a file from the source archive by name, with optional locale filter.
    std::optional<std::vector<u8>> extractFromSource(const std::string& name,
                                                     std::optional<u16> locale = std::nullopt,
                                                     std::string* error = nullptr) const {
        if (!sourceArchive) {
            if (error)
                *error = "no source archive";
            return std::nullopt;
        }

        auto idx = locale ? hashTable.lookup(name, *locale) : hashTable.lookup(name);
        if (!idx) {
            if (error)
                *error = "not found in hash table";
            return std::nullopt;
        }

        const auto& he = hashTable.entry(*idx);
        if (he.blockIndex >= blockTable.count()) {
            if (error)
                *error = "block index out of range";
            return std::nullopt;
        }
        const auto& be = blockTable.entry(he.blockIndex);

        u32 fileKey = 0;
        if (be.isEncrypted()) {
            fileKey = deriveFileKey(name, be);
        }

        return extractFileData(sourceArchive->data(), archiveOffset, be, header.sectorSize(),
                               fileKey, error, pool);
    }

    /// Shared readFile logic. Caller must hold at least a shared lock on mutex.
    std::optional<std::vector<u8>> readFileCore(const std::string& name, std::optional<u16> locale,
                                                std::string* error) const {
        std::string const norm = normalizePath(name);
        OverlayKey const key{norm, locale.value_or(Locale::Neutral)};

        if (pendingDeletes.contains(key)) {
            if (error)
                *error = "file deleted in overlay";
            return std::nullopt;
        }

        auto it = pendingWrites.find(key);
        if (it != pendingWrites.end()) {
            return it->second.data;
        }

        return extractFromSource(name, locale, error);
    }

    /// Build WriteEntry list for the writer — merges source files + overlay.
    std::vector<WriteEntry> buildWriteEntries() const {
        std::vector<WriteEntry> entries;

        // Normalized pending-delete set for fast lookup.
        std::unordered_set<std::string> deleteSet;
        for (const auto& dk : pendingDeletes) {
            deleteSet.insert(dk.normalizedName);
        }

        // Normalized pending-write keys for checking "already in overlay".
        std::unordered_set<std::string> writeNameSet;
        for (const auto& [key, val] : pendingWrites) {
            writeNameSet.insert(key.normalizedName);
        }

        // 1. Source files (from listfile).
        for (const auto& name : sourceListfileNames) {
            std::string const norm = normalizePath(name);
            if (deleteSet.contains(norm))
                continue; // Deleted in overlay.

            if (writeNameSet.contains(norm)) {
                // Overwritten in overlay — will be added in step 2.
                continue;
            }

            // Raw copy from source.
            auto idx = hashTable.lookup(name);
            if (!idx)
                continue;
            const auto& he = hashTable.entry(*idx);
            if (he.blockIndex >= blockTable.count())
                continue;
            const auto& be = blockTable.entry(he.blockIndex);

            if (!be.exists())
                continue;

            // Get raw sector data span from source archive.
            u64 const dataStart = archiveOffset + be.fileOffset;
            if (dataStart + be.compressedSize <= sourceArchive->data().size()) {
                WriteEntry we;
                we.filename = name;
                we.locale = he.locale;
                we.rawSectors = sourceArchive->data().subspan(dataStart, be.compressedSize);
                we.sourceBlock = be;
                entries.push_back(std::move(we));
            }
        }

        // 2. Overlay writes (new + modified files).
        for (const auto& [key, val] : pendingWrites) {
            WriteEntry we;
            we.filename = val.originalName;
            we.locale = key.locale;
            we.rawData = val.data;
            we.compression = static_cast<CompressionFlag>(val.opts.compression);
            we.encrypt = val.opts.encrypt;
            we.singleUnit = val.opts.singleUnit;
            entries.push_back(std::move(we));
        }

        return entries;
    }

    /// Reset state to invalid (used when save fails after overwriting).
    void invalidate() {
        sourceArchive.reset();
        hashTable = HashTable{};
        blockTable = BlockTable{};
        sourceListfileNames.clear();
        pendingWrites.clear();
        pendingDeletes.clear();
        isValid = false;
    }

    /// Apply the results of parseMappedArchive() into this Impl, setting it as
    /// the new source archive.  Clears any pending overlay.
    void applyParsedArchive(ParsedArchive& pa, const std::string& path) {
        header = pa.header;
        archiveOffset = pa.archiveOffset;
        sourcePath = path;
        hashTable = std::move(pa.hashTable);
        blockTable = std::move(pa.blockTable);
        sourceArchive = std::move(pa.mapping);
        sourceListfileNames = std::move(pa.listfileNames);
        isValid = true;
        pendingWrites.clear();
        pendingDeletes.clear();
    }

    /// Re-map and re-parse an archive from disk, replacing current state.
    /// Clears overlay on success. Returns false on any parse failure.
    bool reloadFromDisk(const std::string& path) {
        auto pa = parseMappedArchive(path);
        if (!pa)
            return false;
        applyParsedArchive(*pa, path);
        return true;
    }
};

// ============================================================================
// Storage — Construction / Destruction
// ============================================================================

Storage::Storage() : m_impl(std::make_unique<Impl>()) {}
Storage::~Storage() = default;
Storage::Storage(Storage&& other) noexcept = default;
Storage& Storage::operator=(Storage&& other) noexcept = default;

// ============================================================================
// Static Factories
// ============================================================================

std::optional<Storage> Storage::open(const std::string& path, interfaces::WorkerPool* pool) {
    return open(path, nullptr, pool);
}

std::optional<Storage> Storage::open(const std::string& path, std::string* error,
                                     interfaces::WorkerPool* pool) {
    auto pa = parseMappedArchive(path, error);
    if (!pa)
        return std::nullopt;

    Storage storage;
    storage.m_impl->applyParsedArchive(*pa, path);
    storage.m_impl->pool = pool;
    return storage;
}

Storage Storage::create(CreateOptions opts, interfaces::WorkerPool* pool) {
    Storage storage;
    auto& impl = *storage.m_impl;

    impl.header =
        buildHeader(static_cast<u16>(opts.version), opts.hashTableSize, opts.sectorSizeShift);
    impl.isValid = true;
    impl.pool = pool;
    return storage;
}

// ============================================================================
// Lifetime
// ============================================================================

void Storage::close() {
    if (m_impl) {
        std::unique_lock const lock(m_impl->mutex);
        m_impl->invalidate();
    }
}

Storage::operator bool() const noexcept {
    return m_impl && m_impl->isValid;
}

// ============================================================================
// Read Operations
// ============================================================================

std::optional<std::vector<u8>> Storage::readFile(const std::string& name) const {
    if (!m_impl || !m_impl->isValid)
        return std::nullopt;
    std::shared_lock const lock(m_impl->mutex);
    return m_impl->readFileCore(name, std::nullopt, nullptr);
}

std::optional<std::vector<u8>> Storage::readFile(const std::string& name,
                                                 std::string* error) const {
    if (!m_impl || !m_impl->isValid) {
        if (error)
            *error = "storage not open";
        return std::nullopt;
    }
    std::shared_lock const lock(m_impl->mutex);
    return m_impl->readFileCore(name, std::nullopt, error);
}

std::optional<std::vector<u8>> Storage::readFile(const std::string& name, u16 locale) const {
    if (!m_impl || !m_impl->isValid)
        return std::nullopt;
    std::shared_lock const lock(m_impl->mutex);
    return m_impl->readFileCore(name, std::optional<u16>(locale), nullptr);
}

bool Storage::fileExists(const std::string& name) const {
    if (!m_impl || !m_impl->isValid)
        return false;
    std::shared_lock const lock(m_impl->mutex);

    std::string const norm = normalizePath(name);
    OverlayKey const key{norm, Locale::Neutral};

    if (m_impl->pendingDeletes.contains(key))
        return false;
    if (m_impl->pendingWrites.contains(key))
        return true;

    return m_impl->hashTable.lookup(name).has_value();
}

std::optional<FileInfo> Storage::fileInfo(const std::string& name) const {
    if (!m_impl || !m_impl->isValid)
        return std::nullopt;
    std::shared_lock const lock(m_impl->mutex);

    std::string const norm = normalizePath(name);
    OverlayKey const key{norm, Locale::Neutral};

    if (m_impl->pendingDeletes.contains(key))
        return std::nullopt;

    // Check overlay.
    auto it = m_impl->pendingWrites.find(key);
    if (it != m_impl->pendingWrites.end()) {
        FileInfo info;
        info.name = name;
        info.uncompressedSize = static_cast<u32>(it->second.data.size());
        info.compressedSize = info.uncompressedSize; // Not compressed yet.
        info.locale = it->first.locale;
        info.flags = FileFlags::Exists;
        return info;
    }

    // Source archive.
    auto idx = m_impl->hashTable.lookup(name);
    if (!idx)
        return std::nullopt;

    const auto& he = m_impl->hashTable.entry(*idx);
    if (he.blockIndex >= m_impl->blockTable.count())
        return std::nullopt;
    const auto& be = m_impl->blockTable.entry(he.blockIndex);

    FileInfo info;
    info.name = name;
    info.compressedSize = be.compressedSize;
    info.uncompressedSize = be.uncompressedSize;
    info.flags = static_cast<FileFlags>(static_cast<u32>(be.flags));
    info.locale = he.locale;
    return info;
}

ArchiveInfo Storage::archiveInfo() const {
    if (!m_impl || !m_impl->isValid)
        return {};
    std::shared_lock const lock(m_impl->mutex);

    ArchiveInfo info;
    info.formatVersion = m_impl->header.formatVersion;
    info.hashTableEntries = m_impl->header.hashTableEntries;
    info.blockTableEntries = m_impl->header.blockTableEntries;
    info.sectorSize = m_impl->header.sectorSize();
    info.archiveSize = (m_impl->header.formatVersion >= 2) ? m_impl->header.archiveSize64
                                                           : m_impl->header.archiveSize;
    return info;
}

std::vector<std::string> Storage::listFiles() const {
    if (!m_impl || !m_impl->isValid)
        return {};
    std::shared_lock const lock(m_impl->mutex);

    // Build normalized delete set.
    std::unordered_set<std::string> deleteSet;
    for (const auto& dk : m_impl->pendingDeletes) {
        deleteSet.insert(dk.normalizedName);
    }

    std::unordered_set<std::string> seen;
    std::vector<std::string> result;

    // Source listfile names.
    for (const auto& name : m_impl->sourceListfileNames) {
        std::string const norm = normalizePath(name);
        if (deleteSet.contains(norm))
            continue;
        if (seen.insert(norm).second) {
            result.push_back(name);
        }
    }

    // Overlay additions.
    for (const auto& [key, val] : m_impl->pendingWrites) {
        if (seen.insert(key.normalizedName).second) {
            result.push_back(val.originalName);
        }
    }

    return result;
}

void Storage::enumerate(std::function<bool(const std::string&)> callback) const {
    auto files = listFiles();
    for (const auto& name : files) {
        if (!callback(name))
            break;
    }
}

// ============================================================================
// Write Operations
// ============================================================================

bool Storage::writeFile(const std::string& name, std::span<const u8> data, WriteOptions opts) {
    if (!m_impl || !m_impl->isValid)
        return false;
    std::unique_lock const lock(m_impl->mutex);

    std::string const norm = normalizePath(name);
    OverlayKey const key{norm, opts.locale};

    // Remove from deletes if present.
    m_impl->pendingDeletes.erase(key);

    // Store in overlay.
    m_impl->pendingWrites[key] = {name, std::vector<u8>(data.begin(), data.end()), opts};

    return true;
}

bool Storage::deleteFile(const std::string& name) {
    if (!m_impl || !m_impl->isValid)
        return false;
    std::unique_lock const lock(m_impl->mutex);

    std::string const norm = normalizePath(name);
    OverlayKey const key{norm, Locale::Neutral};

    // Check if it exists in overlay or source.
    bool const exists =
        m_impl->pendingWrites.contains(key) || m_impl->hashTable.lookup(name).has_value();
    if (!exists)
        return false;

    // Remove from writes if present.
    m_impl->pendingWrites.erase(key);

    // Add to deletes.
    m_impl->pendingDeletes.insert(key);
    return true;
}

// ============================================================================
// Save
// ============================================================================

bool Storage::save() {
    if (!m_impl || !m_impl->isValid)
        return false;
    if (m_impl->sourcePath.empty())
        return false; // Created via create(), no path.
    return save(m_impl->sourcePath);
}

bool Storage::save(const std::string& path) {
    if (!m_impl || !m_impl->isValid)
        return false;
    std::unique_lock const lock(m_impl->mutex);

    bool const isSamePath = (path == m_impl->sourcePath);

    // Build write entries from source + overlay.
    auto entries = m_impl->buildWriteEntries();

    // Write the archive.
    auto archiveData =
        writeArchive(m_impl->header, entries, m_impl->header.hashTableEntries, m_impl->pool);

    if (archiveData.empty())
        return false;

    // Determine output path.  When overwriting the mapped source, write to a
    // temp file first, then atomically rename after unmapping.
    std::string outputPath = path;
    std::string tempPath;
    bool const useTempFile = isSamePath && m_impl->sourceArchive;

    if (useTempFile) {
        tempPath = path + ".tmp";
        outputPath = tempPath;
    }

    // Write archive data to disk.
    {
        auto out = whiteout::common::open_ofstream(outputPath, std::ios::binary | std::ios::trunc);
        if (!out)
            return false;
        out.write(reinterpret_cast<const char*>(archiveData.data()),
                  static_cast<std::streamsize>(archiveData.size()));
        if (!out) {
            if (useTempFile) {
                std::error_code ec;
                std::filesystem::remove(tempPath, ec);
            }
            return false;
        }
    }

    if (useTempFile) {
        m_impl->sourceArchive.reset();

        std::error_code ec;
        std::filesystem::rename(tempPath, path, ec);
        if (ec) {
            m_impl->sourceArchive = storages::common::MappedFile::open(m_impl->sourcePath);
            std::filesystem::remove(tempPath, ec);
            return false;
        }
    }

    // Re-open the saved archive as the new source.
    if (!m_impl->reloadFromDisk(path)) {
        if (useTempFile)
            m_impl->invalidate();
        return false;
    }

    return true;
}

} // namespace whiteout::storages::mpq