bazaar_chk_map/
lib.rs

1use crc32fast::Hasher;
2use std::fmt::Write;
3
4fn _crc32(bit: &[u8]) -> u32 {
5    let mut hasher = Hasher::new();
6    hasher.update(bit);
7    hasher.finalize()
8}
9
10pub fn search_key_16(key: &[&[u8]]) -> Vec<u8> {
11    let mut result = String::new();
12    for bit in key {
13        write!(&mut result, "{:08X}\x00", _crc32(bit)).unwrap();
14    }
15    result.pop();
16    result.as_bytes().to_vec()
17}
18
19pub fn search_key_255(key: &[&[u8]]) -> Vec<u8> {
20    let mut result = vec![];
21    for bit in key {
22        let crc = _crc32(bit);
23        let crc_bytes = crc.to_be_bytes();
24        result.extend(&crc_bytes);
25        result.push(0x00);
26    }
27    result.pop();
28    result.iter().map(|b| if *b == 0x0A { b'_'} else { *b }).collect()
29}
30
31pub fn bytes_to_text_key(data: &[u8]) -> Result<(&[u8], &[u8]), String> {
32    let sections: Vec<&[u8]> = data.split(|&byte| byte == b'\n').collect();
33
34    let delimiter_position = sections[0]
35        .windows(2)
36        .position(|window| window == b": ");
37
38    if delimiter_position.is_none() {
39        return Err("Invalid key file".to_string());
40    }
41
42    let (_kind, file_id) = sections[0].split_at(delimiter_position.unwrap() + 2);
43
44    Ok((file_id, sections[3]))
45}