whiteoutlib 0.2.0

Read and write Blizzard game assets from Rust: models (MDX, M2, M3), textures (BLP, DDS, PNG, JPEG, BMP, TGA, TIFF, GIF) 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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026 Fernando Sahmkow

/// WDC1 … WDC5 — the compressed containers. WDC1 introduced field storage
/// info (bitpacking, pallet data, common data); WDC2 split the payload into
/// sections and made string references relative; WDC3 slimmed the offset map;
/// WDC4 added encrypted-record lists and reordered two blocks; WDC5 prefixed
/// the header with a version number and a build string.

#include "db_internal.h"

#include <algorithm>

namespace whiteout::database {

namespace {

constexpr u32 FIELD_STORAGE_INFO_SIZE = 24;

/// The three trailing words of field_storage_info, whose meaning depends on
/// the storage type.
struct StorageExtra {
    u32 first = 0;
    u32 second = 0;
    u32 third = 0;
};

bool readFieldStorageInfo(TableData& table, ByteCursor& cursor, u32 blockSize, IssueSink& sink) {
    const u32 count = blockSize / FIELD_STORAGE_INFO_SIZE;
    if (count == 0) {
        return true;
    }
    if (count != table.fields.size()) {
        // The field structure block and the storage info block should agree;
        // trust the storage info, which is the one the client reads.
        table.fields.resize(count);
    }

    u32 palletOffset = 0;
    u32 commonOffset = 0;
    for (u32 i = 0; i < count; ++i) {
        const u16 offsetBits = cursor.read<u16>();
        const u16 sizeBits = cursor.read<u16>();
        const u32 additionalSize = cursor.read<u32>();
        const u32 storage = cursor.read<u32>();
        StorageExtra extra;
        extra.first = cursor.read<u32>();
        extra.second = cursor.read<u32>();
        extra.third = cursor.read<u32>();
        if (!cursor.ok()) {
            return sink.fail("truncated field storage info block");
        }
        if (storage > static_cast<u32>(FieldStorage::BitpackedSigned)) {
            return sink.fail("unknown field storage type " + std::to_string(storage));
        }

        FieldInfo& field = table.fields[i];
        const u32 structureElementSize = field.elementSize;
        field.storage = static_cast<FieldStorage>(storage);
        field.offsetBits = offsetBits;
        field.sizeBits = sizeBits;
        field.additionalDataSize = additionalSize;

        switch (field.storage) {
        case FieldStorage::None:
            // The field structure block reports the width of one element and
            // the storage info the width of the whole field, so their ratio is
            // the array length. A zero element size means the structure block
            // disagreed; fall back to a scalar.
            field.elementSize = structureElementSize != 0 ? structureElementSize : sizeBits / 8;
            field.arrayCount =
                field.elementSize != 0 ? std::max(1u, sizeBits / (field.elementSize * 8)) : 1;
            break;
        case FieldStorage::Bitpacked:
        case FieldStorage::BitpackedSigned:
            field.arrayCount = 1;
            field.elementSize = 0;
            field.signExtend =
                field.storage == FieldStorage::BitpackedSigned || (extra.third & 0x1) != 0;
            break;
        case FieldStorage::CommonData:
            field.arrayCount = 1;
            field.elementSize = 4;
            field.defaultValue = extra.first;
            field.additionalDataOffset = commonOffset;
            commonOffset += additionalSize;
            break;
        case FieldStorage::BitpackedIndexed:
            field.arrayCount = 1;
            field.elementSize = 4;
            field.additionalDataOffset = palletOffset;
            palletOffset += additionalSize;
            break;
        case FieldStorage::BitpackedIndexedArray:
            field.arrayCount = std::max(1u, extra.third);
            field.elementSize = 4;
            field.additionalDataOffset = palletOffset;
            palletOffset += additionalSize;
            break;
        }
    }
    return true;
}

/// Slice the common-data block into per-field exception lists.
void sliceCommonData(TableData& table, u32 blockOffset, u32 blockSize) {
    table.commonMaps.assign(table.fields.size(), {});
    for (size_t i = 0; i < table.fields.size(); ++i) {
        const FieldInfo& field = table.fields[i];
        if (field.storage != FieldStorage::CommonData || field.additionalDataSize == 0) {
            continue;
        }
        const u64 start = static_cast<u64>(blockOffset) + field.additionalDataOffset;
        const u64 end = start + field.additionalDataSize;
        if (end > static_cast<u64>(blockOffset) + blockSize || end > table.buffer.size()) {
            continue;
        }
        auto& entries = table.commonMaps[i];
        const u32 pairs = field.additionalDataSize / 8;
        entries.reserve(pairs);
        for (u32 p = 0; p < pairs; ++p) {
            u32 id = 0;
            u32 value = 0;
            std::memcpy(&id, table.buffer.data() + start + p * 8, 4);
            std::memcpy(&value, table.buffer.data() + start + p * 8 + 4, 4);
            entries.emplace_back(id, value);
        }
        std::sort(entries.begin(), entries.end());
    }
}

} // namespace

bool readWdc(TableData& table, ByteCursor& cursor, IssueSink& sink) {
    TableInfo& info = table.info;
    const Version version = info.version;
    const bool sectioned = version >= Version::WDC2;

    if (version >= Version::WDC5) {
        info.versionNumber = cursor.read<u32>();
        info.schemaString = cursor.readFixedString(128);
    }

    info.recordCount = cursor.read<u32>();
    info.fieldCount = cursor.read<u32>();
    info.recordSize = cursor.read<u32>();
    info.stringTableSize = cursor.read<u32>();
    info.tableHash = cursor.read<u32>();
    info.layoutHash = cursor.read<u32>();
    info.minId = cursor.read<u32>();
    info.maxId = cursor.read<u32>();
    info.locale = cursor.read<u32>();

    u32 wdc1CopyTableSize = 0;
    if (!sectioned) {
        wdc1CopyTableSize = cursor.read<u32>();
    }
    info.flags = cursor.read<u16>();
    info.idIndex = cursor.read<u16>();
    info.totalFieldCount = cursor.read<u32>();
    info.bitpackedDataOffset = cursor.read<u32>();
    info.lookupColumnCount = cursor.read<u32>();

    u32 wdc1OffsetMapOffset = 0;
    u32 wdc1IdListSize = 0;
    u32 wdc1RelationshipSize = 0;
    if (!sectioned) {
        wdc1OffsetMapOffset = cursor.read<u32>();
        wdc1IdListSize = cursor.read<u32>();
    }
    const u32 fieldStorageInfoSize = cursor.read<u32>();
    const u32 commonDataSize = cursor.read<u32>();
    const u32 palletDataSize = cursor.read<u32>();
    if (sectioned) {
        info.sectionCount = cursor.read<u32>();
    } else {
        wdc1RelationshipSize = cursor.read<u32>();
        info.sectionCount = 1;
    }
    if (!cursor.ok()) {
        return sink.fail(std::string("truncated ") + versionName(version) + " header");
    }

    info.hasOffsetMap = (info.flags & TableFlags::OffsetMap) != 0;
    info.hasRelationshipData = (info.flags & TableFlags::RelationshipData) != 0;
    info.hasNonInlineIds = (info.flags & TableFlags::NonInlineIds) != 0;
    info.isBitpacked = (info.flags & TableFlags::Bitpacked) != 0;

    // ---- Section headers -----------------------------------------------

    struct SectionHeader {
        u64 tactKeyHash = 0;
        u32 fileOffset = 0;
        u32 recordCount = 0;
        u32 stringTableSize = 0;
        u32 offsetRecordsEnd = 0;
        u32 idListSize = 0;
        u32 relationshipDataSize = 0;
        u32 offsetMapIdCount = 0;
        u32 copyTableCount = 0;
    };

    std::vector<SectionHeader> headers;
    if (sectioned) {
        if (info.sectionCount > cursor.remaining() / 8) {
            return sink.fail("section count is larger than the file can hold");
        }
        headers.reserve(info.sectionCount);
        for (u32 i = 0; i < info.sectionCount; ++i) {
            SectionHeader header;
            header.tactKeyHash = cursor.read<u64>();
            header.fileOffset = cursor.read<u32>();
            header.recordCount = cursor.read<u32>();
            header.stringTableSize = cursor.read<u32>();
            if (version == Version::WDC2) {
                const u32 copyTableSize = cursor.read<u32>();
                header.copyTableCount = copyTableSize / 8;
                header.offsetRecordsEnd = cursor.read<u32>(); // 'offset_map_offset' in WDC2
                header.idListSize = cursor.read<u32>();
                header.relationshipDataSize = cursor.read<u32>();
                header.offsetMapIdCount = info.hasOffsetMap ? (info.maxId - info.minId + 1) : 0;
            } else {
                header.offsetRecordsEnd = cursor.read<u32>();
                header.idListSize = cursor.read<u32>();
                header.relationshipDataSize = cursor.read<u32>();
                header.offsetMapIdCount = cursor.read<u32>();
                header.copyTableCount = cursor.read<u32>();
            }
            headers.push_back(header);
        }
    } else {
        SectionHeader header;
        header.fileOffset = 0; // filled in below, once the fixed blocks are read
        header.recordCount = info.recordCount;
        header.stringTableSize = info.stringTableSize;
        header.offsetRecordsEnd = wdc1OffsetMapOffset;
        header.idListSize = wdc1IdListSize;
        header.relationshipDataSize = wdc1RelationshipSize;
        header.offsetMapIdCount = info.hasOffsetMap ? (info.maxId - info.minId + 1) : 0;
        header.copyTableCount = wdc1CopyTableSize / 8;
        headers.push_back(header);
    }
    if (!cursor.ok()) {
        return sink.fail("truncated section headers");
    }

    // ---- Field structure -------------------------------------------------

    {
        std::vector<u16> positions;
        std::vector<u32> byteSizes;
        positions.reserve(info.totalFieldCount);
        byteSizes.reserve(info.totalFieldCount);
        for (u32 i = 0; i < info.totalFieldCount; ++i) {
            const i16 size = cursor.read<i16>();
            const u16 position = cursor.read<u16>();
            positions.push_back(position);
            byteSizes.push_back(static_cast<u32>(std::max(0, (32 - size) / 8)));
        }
        if (!cursor.ok()) {
            return sink.fail("truncated field structure block");
        }
        deriveFieldLayout(table, positions, byteSizes);
    }

    // WDC1 keeps the record payload right after the field structure and the
    // fixed blocks at the end; WDC2 and later hoist the fixed blocks up front.
    if (!sectioned) {
        headers[0].fileOffset = static_cast<u32>(cursor.position());
        const u64 recordBytes =
            info.hasOffsetMap ? 0 : static_cast<u64>(info.recordCount) * info.recordSize;
        u64 skip = recordBytes + info.stringTableSize;
        if (info.hasOffsetMap) {
            if (wdc1OffsetMapOffset < cursor.position()) {
                return sink.fail("WDC1 offset map offset precedes the record data");
            }
            skip = static_cast<u64>(wdc1OffsetMapOffset) - cursor.position() +
                   static_cast<u64>(headers[0].offsetMapIdCount) * 6;
        }
        cursor.skip(static_cast<size_t>(skip));
        cursor.skip(headers[0].idListSize);
        cursor.skip(static_cast<size_t>(headers[0].copyTableCount) * 8);
        if (!cursor.ok()) {
            return sink.fail("WDC1 record, string or id data runs past the end of the file");
        }
    }

    if (!readFieldStorageInfo(table, cursor, fieldStorageInfoSize, sink)) {
        return false;
    }

    table.palletDataOffset = static_cast<u32>(cursor.position());
    table.palletDataSize = palletDataSize;
    cursor.skip(palletDataSize);
    const u32 commonDataOffset = static_cast<u32>(cursor.position());
    cursor.skip(commonDataSize);
    if (!cursor.ok()) {
        return sink.fail("pallet or common data runs past the end of the file");
    }
    sliceCommonData(table, commonDataOffset, commonDataSize);

    // WDC1 keeps its relationship map at the very end of the file, past the
    // pallet and common data blocks the cursor has just stepped over.
    const u32 wdc1RelationshipOffset = static_cast<u32>(cursor.position());

    // ---- Encrypted record lists (WDC4+) ---------------------------------

    std::vector<std::vector<u32>> encryptedIds(headers.size());
    if (version >= Version::WDC4) {
        for (size_t i = 0; i < headers.size(); ++i) {
            if (headers[i].tactKeyHash == 0) {
                continue;
            }
            const u32 count = cursor.read<u32>();
            if (!cursor.ok() || count > cursor.remaining() / 4) {
                return sink.fail("encrypted record list runs past the end of the file");
            }
            encryptedIds[i] = cursor.readVector<u32>(count);
        }
        if (!cursor.ok()) {
            return sink.fail("truncated encrypted record list");
        }
    }

    // ---- Sections --------------------------------------------------------

    table.sections.reserve(headers.size());
    for (size_t i = 0; i < headers.size(); ++i) {
        const SectionHeader& header = headers[i];
        cursor.seek(header.fileOffset);
        if (!cursor.ok()) {
            return sink.fail("section " + std::to_string(i) + " starts outside the file");
        }

        SectionData section;
        section.tactKeyHash = header.tactKeyHash;
        section.fileOffset = header.fileOffset;
        section.recordCount = header.recordCount;
        section.stringTableSize = header.stringTableSize;
        section.encryptedIds = std::move(encryptedIds[i]);

        auto readOffsetMap = [&](u32 count) {
            section.offsetMap.reserve(count);
            for (u32 e = 0; e < count; ++e) {
                OffsetMapEntry entry;
                entry.offset = cursor.read<u32>();
                entry.size = cursor.read<u16>();
                section.offsetMap.push_back(entry);
            }
        };

        if (info.hasOffsetMap) {
            section.recordDataOffset = header.fileOffset;
            section.recordDataSize = header.offsetRecordsEnd > header.fileOffset
                                         ? header.offsetRecordsEnd - header.fileOffset
                                         : 0;
            section.stringTableSize = 0;
            cursor.seek(static_cast<size_t>(header.fileOffset) + section.recordDataSize);
            // WDC1 and WDC2 park the offset map immediately after the records;
            // WDC3 moved it past the id list and copy table.
            if (version <= Version::WDC2) {
                readOffsetMap(header.offsetMapIdCount);
            }
        } else {
            section.recordDataOffset = header.fileOffset;
            section.recordDataSize = header.recordCount * info.recordSize;
            cursor.skip(section.recordDataSize);
            section.stringDataOffset = static_cast<u32>(cursor.position());
            cursor.skip(header.stringTableSize);
        }
        if (!cursor.ok()) {
            return sink.fail("section " + std::to_string(i) +
                             " record data runs past the end of the file");
        }

        section.idList = cursor.readVector<u32>(header.idListSize / 4);
        section.copyTable.reserve(header.copyTableCount);
        for (u32 c = 0; c < header.copyTableCount; ++c) {
            CopyEntry entry;
            entry.newId = cursor.read<u32>();
            entry.sourceId = cursor.read<u32>();
            section.copyTable.push_back(entry);
        }
        if (!cursor.ok()) {
            return sink.fail("section " + std::to_string(i) +
                             " id list or copy table runs past the end of the file");
        }

        auto readRelationships = [&](u32 blockSize) {
            if (blockSize == 0) {
                return;
            }
            const size_t start = cursor.position();
            const u32 count = cursor.read<u32>();
            cursor.skip(8); // min_id, max_id
            // Encrypted sections carry an unreadable count; the header's block
            // size is authoritative for stepping over it either way.
            const u32 storable = std::min<u32>(count, blockSize >= 12 ? (blockSize - 12) / 8 : 0);
            if (cursor.ok()) {
                const bool keyedById =
                    version >= Version::WDC4 && (info.flags & TableFlags::RelationshipData) != 0;
                section.relationKeyedById = keyedById;
                if (!keyedById) {
                    section.relationByRecord.assign(header.recordCount, 0);
                }
                for (u32 e = 0; e < storable; ++e) {
                    const u32 foreignId = cursor.read<u32>();
                    const u32 target = cursor.read<u32>();
                    if (!cursor.ok()) {
                        break;
                    }
                    if (keyedById) {
                        section.relationById[target] = foreignId;
                    } else if (target < section.relationByRecord.size()) {
                        section.relationByRecord[target] = foreignId;
                    }
                }
            }
            cursor.seek(start + blockSize);
        };

        if (version <= Version::WDC2) {
            if (!sectioned) {
                cursor.seek(wdc1RelationshipOffset);
            }
            readRelationships(header.relationshipDataSize);
        } else {
            readOffsetMap(header.offsetMapIdCount);
            if (version >= Version::WDC4 && info.hasRelationshipData) {
                section.offsetMapIds = cursor.readVector<u32>(header.offsetMapIdCount);
                readRelationships(header.relationshipDataSize);
            } else {
                readRelationships(header.relationshipDataSize);
                section.offsetMapIds = cursor.readVector<u32>(header.offsetMapIdCount);
            }
            if (!cursor.ok()) {
                return sink.fail("section " + std::to_string(i) +
                                 " offset map or relationship data is truncated");
            }
        }

        // A section with a TACT key whose records we cannot decrypt reads as
        // zeroes. Flag it so callers can tell "no data" from "zero data".
        if (section.tactKeyHash != 0 && section.recordDataOffset < table.buffer.size()) {
            const u8* start = table.buffer.data() + section.recordDataOffset;
            const size_t length = std::min<size_t>(section.recordDataSize,
                                                   table.buffer.size() - section.recordDataOffset);
            section.encrypted = std::all_of(start, start + length, [](u8 b) { return b == 0; });
            if (section.encrypted) {
                sink.warn("section " + std::to_string(i) + " is encrypted (TACT key " +
                          std::to_string(section.tactKeyHash) + ")");
            }
        }

        table.sections.push_back(std::move(section));
    }

    return true;
}

} // namespace whiteout::database