Skip to content

Commit

Permalink
Add FuelNet CRC32
Browse files Browse the repository at this point in the history
  • Loading branch information
widberg committed Feb 28, 2024
1 parent bc9b735 commit 432ffa9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
7 changes: 6 additions & 1 deletion bff-cli/src/crc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io::{self, BufRead, Read};

use bff::crc::{Asobo32, Asobo64, AsoboAlternate32, BlackSheep32, Kalisto32, Ubisoft64};
use bff::crc::{Asobo32, Asobo64, AsoboAlternate32, BlackSheep32, Kalisto32, Ubisoft64, FuelNet32};
use bff::traits::NameHashFunction;
use clap::ValueEnum;

Expand All @@ -20,6 +20,8 @@ pub enum CrcAlgorithm {
Asobo64,
#[value(alias("u64"))]
Ubisoft64,
#[value(alias("net"))]
FuelNet32,
}

#[derive(ValueEnum, Clone)]
Expand Down Expand Up @@ -82,6 +84,9 @@ fn hash(bytes: &[u8], starting: &i64, algorithm: &CrcAlgorithm, format: &CrcForm
}
CrcAlgorithm::Asobo64 => format_hash64(Asobo64::hash_options(bytes, starting), format),
CrcAlgorithm::Ubisoft64 => format_hash64(Ubisoft64::hash_options(bytes, starting), format),
CrcAlgorithm::FuelNet32 => {
format_hash(FuelNet32::hash_options(bytes, starting as i32), format)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion bff/src/crc/blacksheep32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::traits::NameHashFunction;

// Appears to be the same as FreeBSD
// https://github.com/lattera/freebsd/blob/401a161083850a9a4ce916f37520c084cff1543b/sys/libkern/crc32.c#L51-L95
const CRC32_TABLE: [u32; 256] = [
pub const CRC32_TABLE: [u32; 256] = [
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
Expand Down
33 changes: 33 additions & 0 deletions bff/src/crc/fuelnet32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::traits::NameHashFunction;
use super::blacksheep32::CRC32_TABLE;

pub const fn fuelnet32(bytes: &[u8]) -> i32 {
fuelnet32_options(bytes, 0)
}

// https://github.com/lattera/freebsd/blob/401a161083850a9a4ce916f37520c084cff1543b/sys/libkern/crc32.c#L103-L113
pub const fn fuelnet32_options(bytes: &[u8], starting: i32) -> i32 {
let mut hash = !starting as u32;
let mut i: usize = 0;
while i < bytes.len() {
let c = bytes[i];
hash = (hash >> 8) ^ CRC32_TABLE[((c as u32 ^ hash) & 0xff) as usize];
i += 1;
}

!hash as i32
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FuelNet32;
impl NameHashFunction for FuelNet32 {
type Target = i32;

fn hash(bytes: &[u8]) -> Self::Target {
fuelnet32(bytes)
}

fn hash_options(bytes: &[u8], starting: Self::Target) -> Self::Target {
fuelnet32_options(bytes, starting)
}
}
2 changes: 2 additions & 0 deletions bff/src/crc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ mod asobo_alternate32;
mod blacksheep32;
mod kalisto32;
mod ubisoft64;
mod fuelnet32;

pub use asobo32::*;
pub use asobo64::*;
pub use asobo_alternate32::*;
pub use blacksheep32::*;
pub use kalisto32::*;
pub use ubisoft64::*;
pub use fuelnet32::*;

0 comments on commit 432ffa9

Please sign in to comment.