wc3 0.2.0

Read, edit, and write Warcraft III models, BLP textures, and MPQ archives
Documentation
const fn crypt_table() -> [u32; 1280] {
    let mut table = [0; 1280];
    let mut seed = 0x100001u32;
    let mut i = 0;
    while i < 256 {
        let mut j = 0;
        while j < 5 {
            seed = (seed * 125 + 3) % 0x2aaaab;
            let high = (seed & 0xffff) << 16;
            seed = (seed * 125 + 3) % 0x2aaaab;
            table[i + j * 256] = high | (seed & 0xffff);
            j += 1;
        }
        i += 1;
    }
    table
}

const TABLE: [u32; 1280] = crypt_table();

pub(super) fn hash(name: &[u8], kind: usize) -> u32 {
    let mut a = 0x7fed7fedu32;
    let mut b = 0xeeeeeeeeu32;
    for &byte in name {
        let ch = if byte == b'/' {
            b'\\'
        } else {
            byte.to_ascii_uppercase()
        };
        a = TABLE[kind * 256 + ch as usize] ^ a.wrapping_add(b);
        b = (ch as u32)
            .wrapping_add(a)
            .wrapping_add(b)
            .wrapping_add(b << 5)
            .wrapping_add(3);
    }
    a
}

pub(super) fn file_key(name: &[u8], offset: u32, size: u32, adjusted: bool) -> u32 {
    let base = name
        .rsplit(|&b| b == b'/' || b == b'\\')
        .next()
        .unwrap_or(name);
    let key = hash(base, 3);
    if adjusted {
        key.wrapping_add(offset) ^ size
    } else {
        key
    }
}

/// MPQ encrypts complete little-endian words; trailing bytes remain unchanged.
pub(super) fn crypt(bytes: &mut [u8], mut key: u32, decrypt: bool) {
    let mut seed = 0xeeeeeeeeu32;
    for word in bytes.as_chunks_mut::<4>().0.iter_mut() {
        seed = seed.wrapping_add(TABLE[1024 + (key & 255) as usize]);
        let input = u32::from_le_bytes(*word);
        let output = input ^ key.wrapping_add(seed);
        let plain = if decrypt { output } else { input };
        word.copy_from_slice(&output.to_le_bytes());
        key = ((!key << 21).wrapping_add(0x11111111)) | (key >> 11);
        seed = plain
            .wrapping_add(seed)
            .wrapping_add(seed << 5)
            .wrapping_add(3);
    }
}

#[cfg(test)]
#[path = "crypto_tests.rs"]
mod tests;

/// lookup3 hashlittle2 over at most 264 lowercase, slash-normalized filename bytes.
/// The primary and secondary seeds are 1 and 2.
pub(super) fn jenkins(name: &[u8]) -> u64 {
    let data: Vec<u8> = name
        .iter()
        .take(0x108)
        .map(|&b| {
            if b == b'/' {
                b'\\'
            } else {
                b.to_ascii_lowercase()
            }
        })
        .collect();
    let mut a = 0xdeadbeefu32
        .wrapping_add(data.len() as u32)
        .wrapping_add(2);
    let mut b = a;
    let mut c = a.wrapping_add(1);
    let mut tail = data.as_slice();
    while tail.len() > 12 {
        a = a.wrapping_add(u32::from_le_bytes(tail[..4].try_into().unwrap()));
        b = b.wrapping_add(u32::from_le_bytes(tail[4..8].try_into().unwrap()));
        c = c.wrapping_add(u32::from_le_bytes(tail[8..12].try_into().unwrap()));
        a = a.wrapping_sub(c);
        a ^= c.rotate_left(4);
        c = c.wrapping_add(b);
        b = b.wrapping_sub(a);
        b ^= a.rotate_left(6);
        a = a.wrapping_add(c);
        c = c.wrapping_sub(b);
        c ^= b.rotate_left(8);
        b = b.wrapping_add(a);
        a = a.wrapping_sub(c);
        a ^= c.rotate_left(16);
        c = c.wrapping_add(b);
        b = b.wrapping_sub(a);
        b ^= a.rotate_left(19);
        a = a.wrapping_add(c);
        c = c.wrapping_sub(b);
        c ^= b.rotate_left(4);
        b = b.wrapping_add(a);
        tail = &tail[12..];
    }
    if !tail.is_empty() {
        for (i, &byte) in tail.iter().enumerate() {
            let n = (byte as u32) << ((i % 4) * 8);
            match i / 4 {
                0 => a = a.wrapping_add(n),
                1 => b = b.wrapping_add(n),
                _ => c = c.wrapping_add(n),
            }
        }
        c ^= b;
        c = c.wrapping_sub(b.rotate_left(14));
        a ^= c;
        a = a.wrapping_sub(c.rotate_left(11));
        b ^= a;
        b = b.wrapping_sub(a.rotate_left(25));
        c ^= b;
        c = c.wrapping_sub(b.rotate_left(16));
        a ^= c;
        a = a.wrapping_sub(c.rotate_left(4));
        b ^= a;
        b = b.wrapping_sub(a.rotate_left(14));
        c ^= b;
        c = c.wrapping_sub(b.rotate_left(24));
    }
    ((b as u64) << 32) | c as u64
}

/// Candidate keys for a known first plaintext word, checked against its low byte.
pub(super) fn known_word_keys(bytes: &[u8], plain: u32) -> Vec<u32> {
    if bytes.len() < 4 {
        return Vec::new();
    }
    let encrypted = u32::from_le_bytes(bytes[..4].try_into().unwrap());
    let combined = (encrypted ^ plain).wrapping_sub(0xeeeeeeee);
    (0..256)
        .filter_map(|i| {
            let key = combined.wrapping_sub(TABLE[1024 + i]);
            ((key & 255) as usize == i).then_some(key)
        })
        .collect()
}

pub(super) fn content_key(bytes: &[u8], size: u32) -> Option<u32> {
    if bytes.len() < 8 {
        return None;
    }
    for (first, second) in [
        (0x46464952, size.wrapping_sub(8)),
        (0x00905a4d, 3),
        (0x6d783f3c, 0x6576206c),
    ] {
        for key in known_word_keys(bytes, first) {
            let mut words = bytes[..8].to_vec();
            crypt(&mut words, key, true);
            if u32::from_le_bytes(words[4..8].try_into().unwrap()) == second {
                return Some(key);
            }
        }
    }
    None
}