whiteoutlib 0.1.4

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
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026 Fernando Sahmkow

#include <whiteout/sno/core_toc.h>

#include <algorithm>
#include <cstring>
#include <istream>

#include "../common/binary_reader.h"
#include "../common/streams.h"

namespace whiteout {
namespace sno {

bool CoreToc::parse(std::span<const u8> data) {
    m_all.clear();
    m_groupIndex.clear();
    m_idIndex.clear();
    m_formatHashes.clear();
    m_format = CoreTocFormat::Unknown;

    if (data.size() < 8) {
        return false;
    }

    common::span_streambuf sbuf(data);
    std::istream stream(&sbuf);
    common::BinaryReader reader(stream);

    const u32 firstWord = reader.read<u32>();

    if (firstWord == 0xBCDE6611u) {
        // D4 new format.
        m_format = CoreTocFormat::D4New;
        return parseD4(data, true);
    }

    // If the first word is 0 and the file is large enough for D3's 70-group
    // header (1120 bytes), try the D3 legacy format.  In D3, group-0 always
    // has 0 entries so the first u32 is 0.
    constexpr u32 kD3NumGroups = 70;
    constexpr size_t kD3HeaderSize = kD3NumGroups * 4 * 4; // 1120 bytes
    if (firstWord == 0 && data.size() > kD3HeaderSize + 64) {
        m_format = CoreTocFormat::D3Legacy;
        return parseD3Legacy(data);
    }

    // D4 old format: firstWord is snoGroupsCount.
    m_format = CoreTocFormat::D4Old;
    return parseD4(data, false);
}

// ---- D3 legacy format (no magic, 70 fixed groups, 4 header arrays) ---------

bool CoreToc::parseD3Legacy(std::span<const u8> data) {
    constexpr u32 kNumGroups = 70;
    constexpr size_t kHeaderSize = kNumGroups * 4 * 4; // 1120 bytes

    if (data.size() < kHeaderSize) {
        return false;
    }

    common::span_streambuf sbuf(data);
    std::istream stream(&sbuf);
    common::BinaryReader reader(stream);

    // Header layout:
    //   entryCounts[70]    @ 0
    //   sectionOffsets[70] @ 280  (byte offset per group into data section)
    //   hashCounts[70]     @ 560  (unused)
    //   hashData[70]       @ 840  (unused)

    // Read counts sequentially from offset 0.
    std::vector<u32> entryCounts(kNumGroups);
    for (u32 g = 0; g < kNumGroups; ++g) {
        entryCounts[g] = reader.read<u32>();
    }

    // Read offsets sequentially (reader is at offset 280).
    std::vector<u32> sectionOffsets(kNumGroups);
    for (u32 g = 0; g < kNumGroups; ++g) {
        sectionOffsets[g] = reader.read<u32>();
    }

    // Data section starts right after the header.
    const size_t dataStart = kHeaderSize;

    // Build a sorted list of group offsets so we can compute section bounds.
    struct GroupInfo {
        u32 groupId;
        u32 headerCount;
        size_t sectionStart;
    };
    std::vector<GroupInfo> groups;
    groups.reserve(kNumGroups);

    for (u32 g = 0; g < kNumGroups; ++g) {
        if (entryCounts[g] > 0) {
            groups.push_back({g, entryCounts[g], dataStart + sectionOffsets[g]});
        }
    }

    // Sort by section offset.
    std::sort(groups.begin(), groups.end(), [](const GroupInfo& a, const GroupInfo& b) {
        return a.sectionStart < b.sectionStart;
    });

    // Estimate total entries for reservation.
    size_t totalEstimate = 0;
    for (auto& gi : groups)
        totalEstimate += gi.headerCount;
    m_all.reserve(totalEstimate);

    for (size_t gi = 0; gi < groups.size(); ++gi) {
        const auto& info = groups[gi];
        const i32 expectedGroup = static_cast<i32>(info.groupId);

        // Section bounds: from sectionStart to the next group's start (or EOF).
        const size_t sectionEnd =
            (gi + 1 < groups.size()) ? groups[gi + 1].sectionStart : data.size();

        if (info.sectionStart >= data.size()) {
            continue;
        }

        // Scan entries: each is 12 bytes (snoGroup, snoId, nameOff).
        // Stop when the first field no longer matches the expected group ID.
        size_t entryCount = 0;
        {
            reader.setPosition(static_cast<u32>(info.sectionStart));
            size_t pos = info.sectionStart;
            while (pos + 12 <= sectionEnd) {
                const i32 g = reader.read<i32>();
                if (g != expectedGroup)
                    break;
                reader.skip(8);
                ++entryCount;
                pos += 12;
            }
        }

        // Name pool starts right after the entry records.
        const size_t nameBase = info.sectionStart + entryCount * 12;

        const size_t startIdx = m_all.size();

        for (size_t i = 0; i < entryCount; ++i) {
            reader.setPosition(static_cast<u32>(info.sectionStart + i * 12 + 4));

            const i32 snoId = reader.read<i32>();
            const i32 nameRelOffset = reader.read<i32>();

            const size_t namePos = nameBase + static_cast<size_t>(nameRelOffset);

            std::string name;
            if (namePos < data.size()) {
                reader.setPosition(static_cast<u32>(namePos));
                name = reader.readZString();
            }

            TocEntry entry;
            entry.group = static_cast<SnoGroup>(expectedGroup);
            entry.snoId = snoId;
            entry.name = std::move(name);

            m_idIndex[snoId] = m_all.size();
            m_all.push_back(std::move(entry));
        }

        m_groupIndex[expectedGroup] = {startIdx, m_all.size() - startIdx};
    }

    return true;
}

// ---- D4 format (old and new) ------------------------------------------------

bool CoreToc::parseD4(std::span<const u8> data, bool newFormat) {
    const size_t tocOffset = newFormat ? 8 : 4;

    common::span_streambuf sbuf(data);
    std::istream stream(&sbuf);
    common::BinaryReader reader(stream);

    // For newFormat: magic at 0-3, snoGroupsCount at 4-7.
    // For old format: snoGroupsCount at 0-3.
    reader.setPosition(newFormat ? 4 : 0);
    const u32 snoGroupsCount = reader.read<u32>();
    // Reader is now at tocOffset where the header arrays begin.

    if (snoGroupsCount > 1024) {
        return false; // sanity check
    }

    // Number of header arrays: old format has 3 (counts, offsets, unk),
    // new format has 4 (counts, offsets, unk, formatHashes).
    const u32 headerArrays = newFormat ? 4u : 3u;
    const size_t headerSize = tocOffset + static_cast<size_t>(headerArrays) * snoGroupsCount * 4;
    if (data.size() < headerSize + 4) {
        return false;
    }

    // Read per-group counts.
    std::vector<u32> counts(snoGroupsCount);
    for (u32 c = 0; c < snoGroupsCount; ++c) {
        counts[c] = reader.read<u32>();
    }

    // Read per-group offsets.
    std::vector<u32> offsets(snoGroupsCount);
    for (u32 c = 0; c < snoGroupsCount; ++c) {
        offsets[c] = reader.read<u32>();
    }

    // Skip unk array.
    reader.skip(snoGroupsCount * 4);

    // Read format hashes (new format only).
    if (newFormat) {
        for (u32 c = 0; c < snoGroupsCount; ++c) {
            u32 const fh = reader.read<u32>();
            if (c > 0 && fh != 0) {
                m_formatHashes[static_cast<i32>(c)] = fh;
            }
        }
    }

    // Data entries start right after the header arrays + an extra 4-byte field.
    // (Matches parse.js: dataStart = (newFormat ? 12 : 8) + (newFormat ? 16 : 12) * snoGroupsCount)
    const size_t dataStart = headerSize + 4;

    // First pass: count total entries.
    size_t totalEntries = 0;
    for (u32 c = 0; c < snoGroupsCount; ++c) {
        totalEntries += counts[c];
    }
    m_all.reserve(totalEntries);

    // Second pass: read entries.
    for (u32 c = 0; c < snoGroupsCount; ++c) {
        const u32 entryCount = counts[c];
        const u32 entryOffset = offsets[c];

        if (entryCount == 0) {
            continue;
        }

        const size_t groupDataStart = dataStart + entryOffset;

        // Entry table: entryCount entries of 12 bytes each.
        const size_t entryTableSize = static_cast<size_t>(entryCount) * 12;

        // Names start after the entry table.
        const size_t nameTableStart = groupDataStart + entryTableSize;

        // Bounds check.
        if (groupDataStart + entryTableSize > data.size()) {
            return false;
        }

        const size_t startIdx = m_all.size();

        for (u32 i = 0; i < entryCount; ++i) {
            reader.setPosition(static_cast<u32>(groupDataStart + static_cast<size_t>(i) * 12));

            const i32 snoGroup = reader.read<i32>();
            const i32 snoId = reader.read<i32>();
            const i32 nameRelOffset = reader.read<i32>();

            // Name offset is relative: nameTableStart + nameRelOffset.
            const size_t namePos = nameTableStart + static_cast<size_t>(nameRelOffset);

            // Read null-terminated name.
            std::string name;
            if (namePos < data.size()) {
                reader.setPosition(static_cast<u32>(namePos));
                name = reader.readZString();
            }

            TocEntry entry;
            entry.group = static_cast<SnoGroup>(snoGroup);
            entry.snoId = snoId;
            entry.name = std::move(name);

            m_idIndex[snoId] = m_all.size();
            m_all.push_back(std::move(entry));
        }

        m_groupIndex[static_cast<i32>(c)] = {startIdx, m_all.size() - startIdx};
    }

    return true;
}

std::span<const TocEntry> CoreToc::entriesForGroup(SnoGroup group) const {
    const auto groupId = static_cast<i32>(group);
    auto it = m_groupIndex.find(groupId);
    if (it == m_groupIndex.end()) {
        return {};
    }
    return std::span<const TocEntry>(m_all.data() + it->second.first, it->second.second);
}

const TocEntry* CoreToc::findById(i32 snoId) const {
    auto it = m_idIndex.find(snoId);
    if (it == m_idIndex.end()) {
        return nullptr;
    }
    return &m_all[it->second];
}

// ============================================================================
// Mutation
// ============================================================================

bool CoreToc::addEntry(const TocEntry& entry) {
    if (m_idIndex.count(entry.snoId))
        return false;

    const i32 groupId = static_cast<i32>(entry.group);
    const size_t idx = m_all.size();
    m_all.push_back(entry);
    m_idIndex[entry.snoId] = idx;

    // Update group index — new entry appends to the group's range.
    auto git = m_groupIndex.find(groupId);
    if (git == m_groupIndex.end()) {
        m_groupIndex[groupId] = {idx, 1};
    } else {
        // If the group's entries are contiguous and this is at the end, extend.
        auto& [start, count] = git->second;
        if (start + count == idx) {
            ++count;
        } else {
            // Non-contiguous — relocate the group's entries to end for contiguity.
            // This happens when entries from other groups were added in between.
            const size_t oldCount = count;
            std::vector<TocEntry> const groupEntries(m_all.begin() + static_cast<ptrdiff_t>(start),
                                                     m_all.begin() +
                                                         static_cast<ptrdiff_t>(start + oldCount));
            // The new entry is already at m_all.back() at idx.
            // We don't shuffle here — just track it as a separate range.
            // For serialization, we'll gather entries per group anyway.
            // Mark the group as needing re-gathering by setting start = 0, count = 0.
            // We'll use a gather approach in serialize instead.
            ++count; // Just increment; serialization gathers by group.
        }
    }

    return true;
}

const TocEntry* CoreToc::findByName(SnoGroup group, const std::string& name) const {
    const i32 groupId = static_cast<i32>(group);
    // Linear scan over entries — could be optimized with an index if needed.
    for (auto& e : m_all) {
        if (static_cast<i32>(e.group) == groupId && e.name == name)
            return &e;
    }
    return nullptr;
}

i32 CoreToc::maxSnoId() const {
    i32 maxId = 0;
    for (auto& e : m_all) {
        if (e.snoId > maxId)
            maxId = e.snoId;
    }
    return maxId;
}

// ============================================================================
// Serialization
// ============================================================================

/// Helper: write a little-endian u32 to a buffer.
static void writeU32(std::vector<u8>& buf, u32 val) {
    const auto off = buf.size();
    buf.resize(off + 4);
    std::memcpy(buf.data() + off, &val, 4);
}

/// Helper: write a little-endian i32 to a buffer.
static void writeI32(std::vector<u8>& buf, i32 val) {
    const auto off = buf.size();
    buf.resize(off + 4);
    std::memcpy(buf.data() + off, &val, 4);
}

std::vector<u8> CoreToc::serializeD3Legacy() const {
    constexpr u32 kNumGroups = 70;

    // Gather entries per group.
    std::vector<std::vector<const TocEntry*>> perGroup(kNumGroups);
    for (auto& e : m_all) {
        const auto gid = static_cast<u32>(e.group);
        if (gid < kNumGroups)
            perGroup[gid].push_back(&e);
    }

    // Build per-group section blobs (entry table + name pool).
    struct GroupSection {
        std::vector<u8> data;
        u32 entryCount = 0;
    };
    std::vector<GroupSection> sections(kNumGroups);

    for (u32 g = 0; g < kNumGroups; ++g) {
        auto& entries = perGroup[g];
        if (entries.empty())
            continue;

        auto& sec = sections[g];
        sec.entryCount = static_cast<u32>(entries.size());

        // Build name pool and record relative offsets.
        std::vector<u8> namePool;
        std::vector<i32> nameRelOffsets;
        nameRelOffsets.reserve(entries.size());

        for (auto* e : entries) {
            nameRelOffsets.push_back(static_cast<i32>(namePool.size()));
            namePool.insert(namePool.end(), e->name.begin(), e->name.end());
            namePool.push_back(0); // null terminator
        }

        // Write entry records: 12 bytes each (snoGroup, snoId, nameRelOffset).
        sec.data.reserve(entries.size() * 12 + namePool.size());
        for (size_t i = 0; i < entries.size(); ++i) {
            writeI32(sec.data, static_cast<i32>(g));
            writeI32(sec.data, entries[i]->snoId);
            writeI32(sec.data, nameRelOffsets[i]);
        }

        // Append name pool.
        sec.data.insert(sec.data.end(), namePool.begin(), namePool.end());
    }

    // Compute section offsets (relative to data section start).
    std::vector<u32> sectionOffsets(kNumGroups, 0);
    u32 offset = 0;
    for (u32 g = 0; g < kNumGroups; ++g) {
        sectionOffsets[g] = offset;
        offset += static_cast<u32>(sections[g].data.size());
    }

    // Build output.
    std::vector<u8> result;
    constexpr size_t kHeaderSize = kNumGroups * 4 * 4; // 1120
    result.reserve(kHeaderSize + offset);

    // Header: entryCounts[70]
    for (u32 g = 0; g < kNumGroups; ++g)
        writeU32(result, sections[g].entryCount);

    // Header: sectionOffsets[70]
    for (u32 g = 0; g < kNumGroups; ++g)
        writeU32(result, sectionOffsets[g]);

    // Header: hashCounts[70] (zeros)
    for (u32 g = 0; g < kNumGroups; ++g)
        writeU32(result, 0);

    // Header: hashData[70] (zeros)
    for (u32 g = 0; g < kNumGroups; ++g)
        writeU32(result, 0);

    // Data sections.
    for (u32 g = 0; g < kNumGroups; ++g) {
        result.insert(result.end(), sections[g].data.begin(), sections[g].data.end());
    }

    return result;
}

std::vector<u8> CoreToc::serializeD4New() const {
    // Collect all unique group IDs.
    std::unordered_map<i32, std::vector<const TocEntry*>> perGroup;
    i32 maxGroupId = 0;
    for (auto& e : m_all) {
        const auto gid = static_cast<i32>(e.group);
        if (gid > maxGroupId)
            maxGroupId = gid;
        perGroup[gid].push_back(&e);
    }

    const u32 snoGroupsCount = static_cast<u32>(maxGroupId + 1);

    // Build per-group section blobs.
    struct GroupBlob {
        std::vector<u8> data;
        u32 entryCount = 0;
    };
    std::vector<GroupBlob> groupBlobs(snoGroupsCount);

    for (auto& [gid, entries] : perGroup) {
        if (gid < 0 || static_cast<u32>(gid) >= snoGroupsCount)
            continue;

        auto& blob = groupBlobs[static_cast<u32>(gid)];
        blob.entryCount = static_cast<u32>(entries.size());

        // Build name pool.
        std::vector<u8> namePool;
        std::vector<i32> nameRelOffsets;
        nameRelOffsets.reserve(entries.size());

        for (auto* e : entries) {
            nameRelOffsets.push_back(static_cast<i32>(namePool.size()));
            namePool.insert(namePool.end(), e->name.begin(), e->name.end());
            namePool.push_back(0);
        }

        // Write entry records: 12 bytes each (snoGroup, snoId, nameRelOffset).
        blob.data.reserve(entries.size() * 12 + namePool.size());
        for (size_t i = 0; i < entries.size(); ++i) {
            writeI32(blob.data, gid);
            writeI32(blob.data, entries[i]->snoId);
            writeI32(blob.data, nameRelOffsets[i]);
        }

        blob.data.insert(blob.data.end(), namePool.begin(), namePool.end());
    }

    // Compute offsets.
    std::vector<u32> offsets(snoGroupsCount, 0);
    u32 dataOffset = 0;
    for (u32 g = 0; g < snoGroupsCount; ++g) {
        offsets[g] = dataOffset;
        dataOffset += static_cast<u32>(groupBlobs[g].data.size());
    }

    // Build output.
    // Header: magic(4) + snoGroupsCount(4) + 4 arrays(counts, offsets, unk, formatHashes) + pad(4)
    const size_t headerSize = 8 + static_cast<size_t>(snoGroupsCount) * 4 * 4 + 4;
    std::vector<u8> result;
    result.reserve(headerSize + dataOffset);

    // Magic.
    writeU32(result, 0xBCDE6611u);
    // snoGroupsCount.
    writeU32(result, snoGroupsCount);

    // Counts array.
    for (u32 g = 0; g < snoGroupsCount; ++g)
        writeU32(result, groupBlobs[g].entryCount);

    // Offsets array.
    for (u32 g = 0; g < snoGroupsCount; ++g)
        writeU32(result, offsets[g]);

    // Unk array (zeros).
    for (u32 g = 0; g < snoGroupsCount; ++g)
        writeU32(result, 0);

    // Format hashes array.
    for (u32 g = 0; g < snoGroupsCount; ++g) {
        auto it = m_formatHashes.find(static_cast<i32>(g));
        writeU32(result, (it != m_formatHashes.end()) ? it->second : 0);
    }

    // Extra 4-byte field.
    writeU32(result, 0);

    // Data sections.
    for (u32 g = 0; g < snoGroupsCount; ++g) {
        result.insert(result.end(), groupBlobs[g].data.begin(), groupBlobs[g].data.end());
    }

    return result;
}

} // namespace sno
} // namespace whiteout