1
Fork 0
mirror of https://github.com/redstrate/Physis.git synced 2025-04-24 21:37:46 +00:00

Replace crc crate with simpler implementation

The package is kept as a dev-dependency to prevent regressions though!
This commit is contained in:
Joshua Goins 2022-10-24 16:49:06 -04:00
parent ff4ae2fbcc
commit 7aa306da99
4 changed files with 71 additions and 7 deletions

View file

@ -25,15 +25,14 @@ libunshield = "1.4"
walkdir = "2"
hmac-sha512 = "1"
criterion = "0.4"
# used for testing our jamcrc implementation
crc = "3"
[features]
retail_game_testing = []
patch_testing = []
[dependencies]
# used for jamcrc implementation, should eventually move away from it
crc = "3"
# amazing binary parsing/writing library
binrw = "0.10"

63
src/crc.rs Normal file
View file

@ -0,0 +1,63 @@
pub struct JAMCRC {
table: [u32; 256]
}
impl JAMCRC {
pub(crate) const fn new() -> Self {
let mut table: [u32; 256] = [0u32; 256];
let polynomial: u32 = 0xEDB88320;
let mut i = 0;
while i < table.len() {
let mut c: u32 = i as u32;
let mut j = 0;
while j < 8 {
if (c & 1u32) == 1u32 {
c = polynomial ^ (c >> 1);
} else {
c >>= 1;
}
j += 1;
}
table[i] = c;
i += 1;
}
Self {
table
}
}
pub(crate) fn checksum(&self, bytes: &[u8]) -> u32 {
let mut c : u32 = 0xFFFFFFFF;
for byte in bytes {
c = self.table[((c ^ *byte as u32) & 0xFF) as usize] ^ (c >> 8);
}
!(c ^ 0xFFFFFFFF)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::fiin::FileInfo;
use std::fs::read;
use std::path::PathBuf;
#[test]
fn basic_parsing() {
use crc::{Crc, CRC_32_JAMCRC};
use std::io::{Read, Seek, SeekFrom};
const JAMCR: Crc<u32> = Crc::<u32>::new(&CRC_32_JAMCRC);
let bytes : [u8; 9] = [1, 1, 2, 4, 5, 6, 12, 12, 12];
const crc : JAMCRC = JAMCRC::new();
assert_eq!(JAMCR.checksum(&bytes),
crc.checksum(&bytes))
}
}

View file

@ -67,3 +67,5 @@ pub mod tex;
/// Reading material files (MTRL)
pub mod mtrl;
mod crc;

View file

@ -1,10 +1,10 @@
use crate::compression::no_header_decompress;
use crate::dat::{BlockHeader, CompressionMode};
use binrw::BinRead;
use crc::{Crc, CRC_32_JAMCRC};
use std::io::{Read, Seek, SeekFrom};
use crate::crc::JAMCRC;
const JAMCRC: Crc<u32> = Crc::<u32>::new(&CRC_32_JAMCRC);
const CRC : JAMCRC = JAMCRC::new();
/// Calculates a hash for `index` files from a game path.
pub fn calculate_hash(path: &str) -> u64 {
@ -14,8 +14,8 @@ pub fn calculate_hash(path: &str) -> u64 {
let (directory, filename) = lowercase.split_at(pos);
let directory_crc = JAMCRC.checksum(directory.as_bytes());
let filename_crc = JAMCRC.checksum(filename[1..filename.len()].as_bytes());
let directory_crc = CRC.checksum(directory.as_bytes());
let filename_crc = CRC.checksum(filename[1..filename.len()].as_bytes());
(directory_crc as u64) << 32 | (filename_crc as u64)
}