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
use crc::{Crc, CRC_32_MPEG_2};
fn normalize_char(b: u8) -> u8 {
match b {
b'/' => b'\\',
b'A'..=b'Z' => b + 0x20,
_ => b,
}
}
const ALG: Crc<u32> = Crc::<u32>::new(&CRC_32_MPEG_2);
pub fn calculate_crc(path: &[u8]) -> u32 {
let mut crc = ALG.digest();
let mut s = 0;
for (i, b) in path.iter().copied().enumerate() {
let n = normalize_char(b);
if n != b {
if i > s {
crc.update(&path[s..i]);
}
crc.update(&[n]);
s = i + 1;
}
}
crc.update(&path[s..]);
crc.update(&[0, 0, 0, 0]);
crc.finalize()
}