mirror of
https://github.com/redstrate/Physis.git
synced 2025-04-25 05:47:45 +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:
parent
ff4ae2fbcc
commit
7aa306da99
4 changed files with 71 additions and 7 deletions
|
@ -25,15 +25,14 @@ libunshield = "1.4"
|
||||||
walkdir = "2"
|
walkdir = "2"
|
||||||
hmac-sha512 = "1"
|
hmac-sha512 = "1"
|
||||||
criterion = "0.4"
|
criterion = "0.4"
|
||||||
|
# used for testing our jamcrc implementation
|
||||||
|
crc = "3"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
retail_game_testing = []
|
retail_game_testing = []
|
||||||
patch_testing = []
|
patch_testing = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# used for jamcrc implementation, should eventually move away from it
|
|
||||||
crc = "3"
|
|
||||||
|
|
||||||
# amazing binary parsing/writing library
|
# amazing binary parsing/writing library
|
||||||
binrw = "0.10"
|
binrw = "0.10"
|
||||||
|
|
||||||
|
|
63
src/crc.rs
Normal file
63
src/crc.rs
Normal 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))
|
||||||
|
}
|
||||||
|
}
|
|
@ -67,3 +67,5 @@ pub mod tex;
|
||||||
|
|
||||||
/// Reading material files (MTRL)
|
/// Reading material files (MTRL)
|
||||||
pub mod mtrl;
|
pub mod mtrl;
|
||||||
|
|
||||||
|
mod crc;
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
use crate::compression::no_header_decompress;
|
use crate::compression::no_header_decompress;
|
||||||
use crate::dat::{BlockHeader, CompressionMode};
|
use crate::dat::{BlockHeader, CompressionMode};
|
||||||
use binrw::BinRead;
|
use binrw::BinRead;
|
||||||
use crc::{Crc, CRC_32_JAMCRC};
|
|
||||||
use std::io::{Read, Seek, SeekFrom};
|
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.
|
/// Calculates a hash for `index` files from a game path.
|
||||||
pub fn calculate_hash(path: &str) -> u64 {
|
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, filename) = lowercase.split_at(pos);
|
||||||
|
|
||||||
let directory_crc = JAMCRC.checksum(directory.as_bytes());
|
let directory_crc = CRC.checksum(directory.as_bytes());
|
||||||
let filename_crc = JAMCRC.checksum(filename[1..filename.len()].as_bytes());
|
let filename_crc = CRC.checksum(filename[1..filename.len()].as_bytes());
|
||||||
|
|
||||||
(directory_crc as u64) << 32 | (filename_crc as u64)
|
(directory_crc as u64) << 32 | (filename_crc as u64)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue