assembly_pack/
crc.rs

1//! # CRC digest of resource paths
2
3use crc::{Crc, CRC_32_MPEG_2};
4
5fn normalize_char(b: u8) -> u8 {
6    match b {
7        b'/' => b'\\',
8        b'A'..=b'Z' => b + 0x20,
9        _ => b,
10    }
11}
12
13const ALG: Crc<u32> = Crc::<u32>::new(&CRC_32_MPEG_2);
14
15/// Calculate the Cyclic-Redundancy-Check for a file path
16///
17/// The game uses [CRC-32/MPEG-2], transforms all letters to lowercase,
18/// replaces slashes with backslashes and appends 4 NULL bytes.
19///
20/// [CRC-32/MPEG-2]: https://reveng.sourceforge.io/crc-catalogue/17plus.htm#crc.cat.crc-32-mpeg-2
21pub fn calculate_crc(path: &[u8]) -> u32 {
22    let mut crc = ALG.digest();
23
24    let mut s = 0;
25    for (i, b) in path.iter().copied().enumerate() {
26        let n = normalize_char(b);
27        if n != b {
28            if i > s {
29                crc.update(&path[s..i]);
30            }
31            crc.update(&[n]);
32            s = i + 1;
33        }
34    }
35    crc.update(&path[s..]);
36
37    // I have no clue why this was added
38    crc.update(&[0, 0, 0, 0]);
39
40    crc.finalize()
41}