wow-mpq 0.6.2

High-performance parser for World of Warcraft MPQ archives with parallel processing support
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
//! Support for the MPQ (attributes) special file.
//!
//! The (attributes) file stores extended metadata about files in the archive,
//! including CRC32 checksums, MD5 hashes, file timestamps, and patch information.

use crate::error::{Error, Result};
use byteorder::{LittleEndian, ReadBytesExt};
use bytes::Bytes;
use std::io::{Cursor, Read};

/// Flags indicating which attributes are present in the file
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AttributeFlags(u32);

impl AttributeFlags {
    /// CRC32 checksums are present
    pub const CRC32: u32 = 0x00000001;
    /// File timestamps are present
    pub const FILETIME: u32 = 0x00000002;
    /// MD5 hashes are present
    pub const MD5: u32 = 0x00000004;
    /// Patch bit indicators are present
    pub const PATCH_BIT: u32 = 0x00000008;
    /// All attributes are present
    pub const ALL: u32 = 0x0000000F;

    /// Create new attribute flags
    pub fn new(value: u32) -> Self {
        Self(value)
    }

    /// Check if CRC32 checksums are present
    pub fn has_crc32(&self) -> bool {
        self.0 & Self::CRC32 != 0
    }

    /// Check if file timestamps are present
    pub fn has_filetime(&self) -> bool {
        self.0 & Self::FILETIME != 0
    }

    /// Check if MD5 hashes are present
    pub fn has_md5(&self) -> bool {
        self.0 & Self::MD5 != 0
    }

    /// Check if patch bits are present
    pub fn has_patch_bit(&self) -> bool {
        self.0 & Self::PATCH_BIT != 0
    }

    /// Get the raw flags value
    pub fn as_u32(&self) -> u32 {
        self.0
    }
}

/// File attributes for a single file in the archive
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileAttributes {
    /// CRC32 checksum of the uncompressed file data
    pub crc32: Option<u32>,
    /// Windows FILETIME timestamp (100-nanosecond intervals since 1601-01-01)
    pub filetime: Option<u64>,
    /// MD5 hash of the uncompressed file data
    pub md5: Option<[u8; 16]>,
    /// Whether this file is a patch file
    pub is_patch: Option<bool>,
}

impl FileAttributes {
    /// Create empty attributes
    pub fn new() -> Self {
        Self {
            crc32: None,
            filetime: None,
            md5: None,
            is_patch: None,
        }
    }
}

impl Default for FileAttributes {
    fn default() -> Self {
        Self::new()
    }
}

/// Parsed (attributes) file data
#[derive(Debug, Clone)]
pub struct Attributes {
    /// Version of the attributes file (should be 100)
    pub version: u32,
    /// Flags indicating which attributes are present
    pub flags: AttributeFlags,
    /// Attributes for each file in the block table
    pub file_attributes: Vec<FileAttributes>,
    /// CRC32 checksum (Phase 1 stub)
    pub crc32: Option<u32>,
    /// MD5 checksum (Phase 1 stub)  
    pub md5: Option<[u8; 16]>,
    /// File time (Phase 1 stub)
    pub filetime: Option<u64>,
}

impl Attributes {
    /// Expected version for the attributes file
    pub const EXPECTED_VERSION: u32 = 100;

    /// Parse attributes from raw data
    pub fn parse(data: &Bytes, block_count: usize) -> Result<Self> {
        if data.len() < 8 {
            return Err(Error::invalid_format(
                "Attributes file too small for header",
            ));
        }

        let mut cursor = Cursor::new(data);

        // Read header
        let version = cursor.read_u32::<LittleEndian>().map_err(Error::Io)?;
        if version != Self::EXPECTED_VERSION {
            return Err(Error::invalid_format(format!(
                "Unsupported attributes version: {} (expected {})",
                version,
                Self::EXPECTED_VERSION
            )));
        }

        let flags = AttributeFlags::new(cursor.read_u32::<LittleEndian>().map_err(Error::Io)?);

        // Calculate expected size
        let mut expected_size = 8; // header
        if flags.has_crc32() {
            expected_size += block_count * 4;
        }
        if flags.has_filetime() {
            expected_size += block_count * 8;
        }
        if flags.has_md5() {
            expected_size += block_count * 16;
        }
        if flags.has_patch_bit() {
            expected_size += block_count.div_ceil(8);
        }

        // Be more lenient with size validation to handle real-world MPQ variations
        // Some MPQ files may have slightly different patch bit calculations
        let min_required_size = 8 + // header
            if flags.has_crc32() { block_count * 4 } else { 0 } +
            if flags.has_filetime() { block_count * 8 } else { 0 } +
            if flags.has_md5() { block_count * 16 } else { 0 } +
            if flags.has_patch_bit() {
                // Allow for off-by-one variations in patch bit calculations
                let ideal_patch_bytes = block_count.div_ceil(8);
                if ideal_patch_bytes > 0 { ideal_patch_bytes - 1 } else { 0 }
            } else { 0 };

        if data.len() < min_required_size {
            return Err(Error::invalid_format(format!(
                "Attributes file too small: {} bytes (expected at least {}, ideally {})",
                data.len(),
                min_required_size,
                expected_size
            )));
        }

        // Log when we encounter size discrepancies for debugging
        if data.len() != expected_size {
            log::warn!(
                "Attributes file size mismatch: actual={}, expected={}, difference={} (tolerating for compatibility)",
                data.len(),
                expected_size,
                expected_size as i32 - data.len() as i32
            );
        }

        // Parse attributes for each file
        let mut file_attributes = Vec::with_capacity(block_count);

        // Parse CRC32 array if present
        let crc32_values = if flags.has_crc32() {
            let mut values = Vec::with_capacity(block_count);
            for _ in 0..block_count {
                values.push(cursor.read_u32::<LittleEndian>().map_err(Error::Io)?);
            }
            Some(values)
        } else {
            None
        };

        // Parse timestamp array if present
        let filetime_values = if flags.has_filetime() {
            let mut values = Vec::with_capacity(block_count);
            for _ in 0..block_count {
                values.push(cursor.read_u64::<LittleEndian>().map_err(Error::Io)?);
            }
            Some(values)
        } else {
            None
        };

        // Parse MD5 array if present
        let md5_values = if flags.has_md5() {
            let mut values = Vec::with_capacity(block_count);
            for _ in 0..block_count {
                let mut hash = [0u8; 16];
                cursor.read_exact(&mut hash).map_err(Error::Io)?;
                values.push(hash);
            }
            Some(values)
        } else {
            None
        };

        // Parse patch bits if present
        let patch_bits = if flags.has_patch_bit() {
            let ideal_byte_count = block_count.div_ceil(8);
            // Calculate how many bytes are actually available for patch bits
            let position = cursor.position() as usize;
            let available_bytes = if data.len() > position {
                data.len() - position
            } else {
                0
            };
            let actual_byte_count = available_bytes.min(ideal_byte_count);

            log::debug!(
                "Patch bits: ideal={ideal_byte_count} bytes, available={available_bytes} bytes, reading={actual_byte_count} bytes"
            );

            let mut bits = vec![0u8; ideal_byte_count]; // Always allocate the ideal size
            if actual_byte_count > 0 {
                let mut actual_bits = vec![0u8; actual_byte_count];
                cursor.read_exact(&mut actual_bits).map_err(Error::Io)?;
                bits[..actual_byte_count].copy_from_slice(&actual_bits);
                // Remaining bytes in bits stay as 0, which is safe for patch bit interpretation
            }
            Some(bits)
        } else {
            None
        };

        // Combine into FileAttributes structs
        for i in 0..block_count {
            let mut attrs = FileAttributes::new();

            if let Some(ref values) = crc32_values {
                attrs.crc32 = Some(values[i]);
            }

            if let Some(ref values) = filetime_values {
                attrs.filetime = Some(values[i]);
            }

            if let Some(ref values) = md5_values {
                attrs.md5 = Some(values[i]);
            }

            if let Some(ref bits) = patch_bits {
                let byte_index = i / 8;
                let bit_index = i % 8;
                attrs.is_patch = Some((bits[byte_index] & (1 << bit_index)) != 0);
            }

            file_attributes.push(attrs);
        }

        Ok(Self {
            version,
            flags,
            file_attributes,
            crc32: None,    // Phase 1 stub
            md5: None,      // Phase 1 stub
            filetime: None, // Phase 1 stub
        })
    }

    /// Get attributes for a specific block index
    pub fn get_file_attributes(&self, block_index: usize) -> Option<&FileAttributes> {
        self.file_attributes.get(block_index)
    }

    /// Create attributes data for writing to an archive
    pub fn to_bytes(&self) -> Result<Vec<u8>> {
        let block_count = self.file_attributes.len();
        let mut data = Vec::new();

        // Write header
        data.extend_from_slice(&self.version.to_le_bytes());
        data.extend_from_slice(&self.flags.as_u32().to_le_bytes());

        // Write CRC32 array if present
        if self.flags.has_crc32() {
            for attrs in &self.file_attributes {
                let crc = attrs.crc32.unwrap_or(0);
                data.extend_from_slice(&crc.to_le_bytes());
            }
        }

        // Write timestamp array if present
        if self.flags.has_filetime() {
            for attrs in &self.file_attributes {
                let time = attrs.filetime.unwrap_or(0);
                data.extend_from_slice(&time.to_le_bytes());
            }
        }

        // Write MD5 array if present
        if self.flags.has_md5() {
            for attrs in &self.file_attributes {
                let hash = attrs.md5.unwrap_or([0u8; 16]);
                data.extend_from_slice(&hash);
            }
        }

        // Write patch bits if present
        if self.flags.has_patch_bit() {
            let byte_count = block_count.div_ceil(8);
            let mut bits = vec![0u8; byte_count];

            for (i, attrs) in self.file_attributes.iter().enumerate() {
                if attrs.is_patch.unwrap_or(false) {
                    let byte_index = i / 8;
                    let bit_index = i % 8;
                    bits[byte_index] |= 1 << bit_index;
                }
            }

            data.extend_from_slice(&bits);
        }

        Ok(data)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_attribute_flags() {
        let flags = AttributeFlags::new(AttributeFlags::ALL);
        assert!(flags.has_crc32());
        assert!(flags.has_filetime());
        assert!(flags.has_md5());
        assert!(flags.has_patch_bit());

        let flags = AttributeFlags::new(AttributeFlags::CRC32 | AttributeFlags::MD5);
        assert!(flags.has_crc32());
        assert!(!flags.has_filetime());
        assert!(flags.has_md5());
        assert!(!flags.has_patch_bit());
    }

    #[test]
    fn test_parse_empty_attributes() {
        // Create minimal attributes file with no attributes
        let mut data = Vec::new();
        data.extend_from_slice(&100u32.to_le_bytes()); // version
        data.extend_from_slice(&0u32.to_le_bytes()); // flags (no attributes)

        let bytes = Bytes::from(data);
        let attrs = Attributes::parse(&bytes, 0).unwrap();

        assert_eq!(attrs.version, 100);
        assert_eq!(attrs.flags.as_u32(), 0);
        assert_eq!(attrs.file_attributes.len(), 0);
    }

    #[test]
    fn test_parse_crc32_only() {
        let mut data = Vec::new();
        data.extend_from_slice(&100u32.to_le_bytes()); // version
        data.extend_from_slice(&AttributeFlags::CRC32.to_le_bytes()); // flags

        // Add CRC32 values for 2 files
        data.extend_from_slice(&0x12345678u32.to_le_bytes());
        data.extend_from_slice(&0x9ABCDEF0u32.to_le_bytes());

        let bytes = Bytes::from(data);
        let attrs = Attributes::parse(&bytes, 2).unwrap();

        assert_eq!(attrs.version, 100);
        assert!(attrs.flags.has_crc32());
        assert!(!attrs.flags.has_filetime());
        assert!(!attrs.flags.has_md5());
        assert!(!attrs.flags.has_patch_bit());

        assert_eq!(attrs.file_attributes.len(), 2);
        assert_eq!(attrs.file_attributes[0].crc32, Some(0x12345678));
        assert_eq!(attrs.file_attributes[1].crc32, Some(0x9ABCDEF0));
    }

    #[test]
    fn test_roundtrip() {
        // Create attributes with all fields
        let mut file_attrs = Vec::new();

        let mut attr1 = FileAttributes::new();
        attr1.crc32 = Some(0x12345678);
        attr1.filetime = Some(0x01234567_89ABCDEF);
        attr1.md5 = Some([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
        attr1.is_patch = Some(false);
        file_attrs.push(attr1);

        let mut attr2 = FileAttributes::new();
        attr2.crc32 = Some(0x9ABCDEF0);
        attr2.filetime = Some(0xFEDCBA98_76543210);
        attr2.md5 = Some([16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
        attr2.is_patch = Some(true);
        file_attrs.push(attr2);

        let original = Attributes {
            version: 100,
            flags: AttributeFlags::new(AttributeFlags::ALL),
            file_attributes: file_attrs,
            crc32: None,    // Phase 1 stub
            md5: None,      // Phase 1 stub
            filetime: None, // Phase 1 stub
        };

        // Convert to bytes and back
        let bytes = original.to_bytes().unwrap();
        let parsed = Attributes::parse(&Bytes::from(bytes), 2).unwrap();

        assert_eq!(parsed.version, original.version);
        assert_eq!(parsed.flags.as_u32(), original.flags.as_u32());
        assert_eq!(parsed.file_attributes.len(), original.file_attributes.len());

        for i in 0..2 {
            assert_eq!(parsed.file_attributes[i], original.file_attributes[i]);
        }
    }
}