wc3 0.2.0

Read, edit, and write Warcraft III models, BLP textures, and MPQ archives
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
//! Bounded recovery of malformed classic MPQs. Valid extended formats stay strict.
//!
//! Recovery preserves logical hash slots and resolves classic wrapping before
//! validating physical ranges. It never changes codecs or bypasses resource limits.
use std::io::{ErrorKind, Read, Seek, SeekFrom};

use super::codec::decode;
use super::crypto::{content_key, crypt, hash, known_word_keys};
use super::format::{offset_table_size, sector_count, u32_at, DELETED};
use super::reader::load_offsets;
use super::{BlockEntry, Error, FileFlags, HashEntry, Header, Index, ReadOptions};

/// Interpretation policy for malformed archives.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ReadMode {
    #[default]
    /// Reject malformed archive layouts without recovery.
    Strict,
    /// Recover known classic MPQ quirks, retaining all resource limits.
    Permissive,
}

/// A recovery applied while indexing or opening an entry.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RecoveryDiagnostic {
    /// Description of the recovery applied to the archive or entry.
    pub reason: &'static str,
    /// Table slot or block ID, when the recovery concerns a record.
    pub entry: Option<u32>,
}

pub(super) fn report(out: &mut Vec<RecoveryDiagnostic>, reason: &'static str, entry: Option<u32>) {
    let diagnostic = RecoveryDiagnostic { reason, entry };
    // Bound both memory and work for malicious tables with millions of bad records.
    if out.len() < 1024 {
        out.push(diagnostic);
    } else if out.len() == 1024 {
        out.push(RecoveryDiagnostic {
            reason: "further recovery diagnostics omitted",
            entry: None,
        });
    }
}

/// Classic Storm arithmetic wraps the absolute address at 32 bits.
/// Range checking always follows address resolution, never precedes it.
pub(super) fn address(
    base: u64,
    relative: u64,
    size: u64,
    len: u64,
    wrapping: bool,
) -> Result<u64, Error> {
    let at = if wrapping {
        (base as u32).wrapping_add(relative as u32) as u64
    } else {
        base.checked_add(relative)
            .ok_or(Error::InvalidArchive("offset overflow"))?
    };
    if at.checked_add(size).is_none_or(|end| end > len) {
        return Err(Error::InvalidArchive("read outside source"));
    }
    Ok(at)
}

pub(super) fn read_index(
    source: &mut (impl Read + Seek),
    base: u64,
    len: u64,
    bytes: &[u8],
    options: &ReadOptions,
    diagnostics: &mut Vec<RecoveryDiagnostic>,
) -> Result<Index, Error> {
    let mut normalized = bytes[..32].to_vec();
    if u32_at(bytes, 4) != 32 || bytes[12..14] != [0, 0] {
        report(diagnostics, "classic header size/version overridden", None);
    }
    normalized[4..8].copy_from_slice(&32u32.to_le_bytes());
    normalized[12..14].fill(0);
    if normalized[15] != 0 {
        report(diagnostics, "sector shift high byte ignored", None);
        normalized[15] = 0;
    }
    let mut header = Header::decode(&normalized)?;
    if header
        .sector_size()
        .is_none_or(|size| size > options.max_sector_size)
    {
        return Err(Error::LimitExceeded("sector size"));
    }
    if header.archive_size != len - base {
        report(
            diagnostics,
            "archive extent replaced by source extent",
            None,
        );
    }
    header.archive_size = len - base;
    let mut tables = Vec::new();
    for (offset, count, name, is_hash) in [
        (
            header.hash_table_offset,
            header.hash_table_entries,
            b"(hash table)".as_slice(),
            true,
        ),
        (
            header.block_table_offset,
            header.block_table_entries,
            b"(block table)".as_slice(),
            false,
        ),
    ] {
        let masked = count & 0x0fff_ffff;
        if masked != count {
            report(diagnostics, "table count high bits ignored", None);
        }
        let at = if offset == u32::MAX as u64 {
            report(
                diagnostics,
                "sentinel table offset uses current cursor",
                None,
            );
            source.stream_position()?
        } else {
            address(base, offset, 0, len, true)?
        };
        if at > len {
            return Err(Error::InvalidArchive("table outside source"));
        }
        if at < base {
            report(diagnostics, "table offset wrapped before header", None);
        }
        let available = ((len - at) / 16).min(masked as u64) as u32;
        let logical = if is_hash { masked } else { available };
        if logical > options.max_table_entries || logical as u64 * 16 > options.max_table_bytes {
            return Err(Error::LimitExceeded("table allocation"));
        }
        if is_hash && (logical == 0 || !logical.is_power_of_two()) {
            return Err(Error::InvalidArchive(
                "hash table size is not a power of two",
            ));
        }
        if available < masked {
            report(diagnostics, "classic table truncated", None);
        }
        let mut table = vec![0; available as usize * 16];
        source.seek(SeekFrom::Start(at))?;
        source.read_exact(&mut table)?;
        crypt(&mut table, hash(name, 3), true);
        tables.push((table, logical));
    }
    let (hash_bytes, hash_count) = &tables[0];
    let (block_bytes, block_count) = &tables[1];
    let blocks: Vec<_> = block_bytes
        .as_chunks::<16>()
        .0
        .iter()
        .map(|b| BlockEntry {
            offset: u32_at(b, 0) as u64,
            stored_size: u32_at(b, 4),
            file_size: u32_at(b, 8),
            flags: FileFlags(u32_at(b, 12)),
        })
        .collect();
    let mut hashes = Vec::with_capacity(*hash_count as usize);
    for (slot, b) in hash_bytes.as_chunks::<16>().0.iter().enumerate() {
        let mut id = u32_at(b, 12);
        if id < DELETED {
            let masked = id & 0x0fff_ffff;
            if masked != id {
                report(
                    diagnostics,
                    "block index high bits ignored",
                    Some(slot as u32),
                );
            }
            id = masked;
            if blocks
                .get(id as usize)
                .is_none_or(|b| !b.flags.contains(FileFlags::EXISTS))
            {
                report(
                    diagnostics,
                    "invalid hash reference ignored",
                    Some(slot as u32),
                );
                id = DELETED;
            }
        }
        if b[11] != 0 && id < DELETED {
            report(diagnostics, "hash reserved byte ignored", Some(slot as u32));
        }
        hashes.push(HashEntry {
            name_hash_a: u32_at(b, 0),
            name_hash_b: u32_at(b, 4),
            locale: u16::from_le_bytes(b[8..10].try_into().unwrap()),
            platform: b[10] as u16,
            block_index: id,
        });
    }
    hashes.resize(
        *hash_count as usize,
        HashEntry {
            block_index: DELETED,
            ..HashEntry::empty()
        },
    );
    for (id, block) in blocks.iter().enumerate() {
        if block.flags.contains(FileFlags::EXISTS)
            && address(base, block.offset, 0, len, true)
                .is_ok_and(|at| at < base + 32 && block.file_size != 0)
        {
            report(
                diagnostics,
                "block offset wrapped before header",
                Some(id as u32),
            );
        }
        if block.flags.contains(FileFlags::EXISTS)
            && address(base, block.offset, block.stored_size as u64, len, true).is_err()
        {
            report(
                diagnostics,
                "invalid block extent deferred",
                Some(id as u32),
            );
        }
    }
    header.hash_table_entries = *hash_count;
    header.block_table_entries = *block_count;
    header.table_sizes[0] = hash_bytes.len() as u64;
    header.table_sizes[1] = block_bytes.len() as u64;
    Ok(Index {
        header,
        hashes,
        blocks,
        extended: None,
    })
}

pub(super) struct LayoutRequest<'a> {
    pub base: u64,
    pub len: u64,
    pub block: BlockEntry,
    pub id: u32,
    pub sector_size: u32,
    pub key: Option<u32>,
    pub wrapping: bool,
    pub options: &'a ReadOptions,
}

pub(super) struct EntryLayout {
    pub key: Option<u32>,
    pub offsets: Vec<u32>,
    pub checksums: Vec<u32>,
}

pub(super) fn load_layout(
    source: &mut (impl Read + Seek),
    request: LayoutRequest<'_>,
    diagnostics: &mut Vec<RecoveryDiagnostic>,
) -> Result<EntryLayout, Error> {
    let LayoutRequest {
        base,
        len,
        block,
        id,
        sector_size,
        mut key,
        wrapping,
        options,
    } = request;
    let mut payload_options = options.clone();
    if !wrapping {
        payload_options.mode = ReadMode::Strict;
    }
    let offsets = match load_offsets(
        &mut *source,
        base,
        block,
        sector_size,
        key,
        &payload_options,
    ) {
        Ok(offsets) => offsets,
        Err(original)
            if wrapping
                && block.flags.contains(FileFlags::ENCRYPTED)
                && block.flags.compressed()
                && !block.flags.contains(FileFlags::SINGLE_UNIT) =>
        {
            let size = offset_table_size(
                block.file_size,
                sector_size,
                block.flags.contains(FileFlags::SECTOR_CRC),
            )?;
            if size > options.max_sector_table_bytes {
                return Err(Error::LimitExceeded("sector offset table"));
            }
            let at = address(base, block.offset, size as u64, len, true)?;
            let mut encrypted = vec![0; size as usize];
            source.seek(SeekFrom::Start(at))?;
            source.read_exact(&mut encrypted)?;
            let mut recovered = None;
            'candidates: for first in size..size.saturating_add(4) {
                for candidate in known_word_keys(&encrypted, first) {
                    let candidate = candidate.wrapping_add(1);
                    if let Ok(offsets) = load_offsets(
                        &mut *source,
                        base,
                        block,
                        sector_size,
                        Some(candidate),
                        &payload_options,
                    ) {
                        recovered = Some((candidate, offsets));
                        break 'candidates;
                    }
                }
            }
            let (candidate, offsets) = recovered.ok_or(original)?;
            key = Some(candidate);
            report(
                diagnostics,
                "encryption key recovered from sector table",
                Some(id),
            );
            offsets
        }
        Err(error) => return Err(error),
    };
    if wrapping
        && block.flags.contains(FileFlags::ENCRYPTED)
        && offsets.is_empty()
        && (!block.flags.compressed()
            || (block.flags.contains(FileFlags::SINGLE_UNIT)
                && block.stored_size == block.file_size))
        && block.file_size >= 8
    {
        let size = block.file_size.min(12);
        let at = address(base, block.offset, size as u64, len, true)?;
        let mut encrypted = vec![0; size as usize];
        source.seek(SeekFrom::Start(at))?;
        source.read_exact(&mut encrypted)?;
        if let Some(candidate) = content_key(&encrypted, block.file_size) {
            if key != Some(candidate) {
                key = Some(candidate);
                report(
                    diagnostics,
                    "encryption key recovered from known content",
                    Some(id),
                );
            }
        }
    }
    if block.flags.contains(FileFlags::ENCRYPTED) && key.is_none() && block.file_size != 0 {
        return Err(Error::InvalidArchive("unknown encryption key"));
    }
    if wrapping && offsets.first().is_some_and(|offset| *offset > 0x8000_0000) {
        report(diagnostics, "sector offsets wrapped before table", Some(id));
    }
    if wrapping
        && offsets
            .first()
            .is_some_and(|offset| *offset < 0x8000_0000 && *offset as usize > offsets.len() * 4)
    {
        report(diagnostics, "extra sector table bytes skipped", Some(id));
    }
    let checksum_result = (|| -> Result<Vec<u32>, Error> {
        Ok(
            if options.verify_sector_checksums
                && block.flags.contains(FileFlags::SECTOR_CRC)
                && !offsets.is_empty()
            {
                let count = sector_count(block.file_size, sector_size) as usize;
                let start = offsets[count];
                let stored = offsets[count + 1]
                    .checked_sub(start)
                    .ok_or(Error::InvalidArchive("checksum offsets"))?;
                if stored == 0 {
                    Vec::new()
                } else {
                    let expected = count * 4;
                    if stored as usize > expected {
                        return Err(Error::InvalidArchive("checksum table length"));
                    }
                    let mut bytes = vec![0; stored as usize];
                    source.seek(SeekFrom::Start(address(
                        base,
                        block.offset + start as u64,
                        stored as u64,
                        len,
                        wrapping,
                    )?))?;
                    source.read_exact(&mut bytes)?;
                    if bytes.len() < expected {
                        bytes = decode(&bytes, expected, false)?;
                    }
                    bytes
                        .as_chunks::<4>()
                        .0
                        .iter()
                        .map(|b| u32_at(b, 0))
                        .collect()
                }
            } else {
                Vec::new()
            },
        )
    })();
    let checksums = match checksum_result {
        Ok(checksums) => checksums,
        Err(error)
            if wrapping
                && (matches!(
                    &error,
                    Error::InvalidArchive(_) | Error::UnsupportedCompression(_)
                ) || matches!(&error, Error::Io(io) if io.kind() == ErrorKind::UnexpectedEof)) =>
        {
            report(
                diagnostics,
                "unusable sector checksum metadata ignored",
                Some(id),
            );
            Vec::new()
        }
        Err(error) => return Err(error),
    };
    Ok(EntryLayout {
        key,
        offsets,
        checksums,
    })
}