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
#![no_std]
#![deny(
warnings,
future_incompatible,
unused,
unsafe_code,
clippy::all,
clippy::cargo,
clippy::pedantic,
rustdoc::all
)]
#![allow(clippy::cast_lossless, clippy::must_use_candidate)]
#[inline]
pub fn calculate(input: &[u8]) -> (u32, u32) {
const MASK: u32 = 0b1_1111;
let (left, right) = input.split_at(input.len() >> 1);
let (mut a, mut b, mut shift) = (0, 0, 0);
for &n in left {
a ^= u32::from(n) << shift;
shift = (shift + 8) & MASK;
}
shift = 0;
for &n in right {
let temp = u32::from(n) << shift;
b = (b ^ temp).rotate_right(temp & MASK);
shift = (shift + 8) & MASK;
}
(a, b)
}
#[cfg(test)]
mod tests {
include!("../bsa_lists/morrowind.rs");
include!("../bsa_lists/tribunal.rs");
include!("../bsa_lists/bloodmoon.rs");
#[inline]
fn test_hashes(list: &[(&str, u32, u32)]) {
for &(filename, left_hash, right_hash) in list {
assert_eq!(
crate::calculate(filename.as_bytes()),
(left_hash, right_hash)
);
}
}
#[test]
fn morrowind_bsa() {
test_hashes(MORROWIND_BSA);
}
#[test]
fn tribunal_bsa() {
test_hashes(TRIBUNAL_BSA);
}
#[test]
fn bloodmoon_bsa() {
test_hashes(BLOODMOON_BSA);
}
}