Skip to content

Commit

Permalink
hash: convert names to lowercase when matching
Browse files Browse the repository at this point in the history
  • Loading branch information
roccodev committed Nov 4, 2023
1 parent 02e9b31 commit a59ce4a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xc3-file-loader"
version = "1.3.0"
version = "1.3.1"
authors = ["RoccoDev <[email protected]>"]
edition = "2021"
license = "gpl-3.0"
Expand Down Expand Up @@ -28,4 +28,4 @@ panic = "abort"
[profile.release]
panic = "abort"
lto = true
strip = true
strip = true
5 changes: 4 additions & 1 deletion src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ pub struct IdentityHasher(u64);

pub type PreHashedSet<K> = std::collections::HashSet<K, IdentityHasher>;

pub fn crc32(name: &str) -> u32 {
/// Calculates a CRC-32 hash of the given string,
/// converting any uppercase characters to lowercase beforehand.
pub fn crc32_lowercase(name: &str) -> u32 {
if name.is_empty() {
return 0;
}
let mut hash = 0u32;
for b in name.bytes() {
let b = if (b'A'..=b'Z').contains(&b) { b + 32 } else { b };
let val = hash ^ b as u32;
hash = (hash >> 8) ^ CRC_TABLE[val as usize & 0xff];
}
Expand Down
6 changes: 3 additions & 3 deletions src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::borrow::Borrow;
use std::ffi::{CStr, CString};
use std::mem::MaybeUninit;

use crate::hash::{crc32, PreHashedSet};
use crate::hash::{crc32_lowercase, PreHashedSet};

/// Top-level paths to skip when matching romfs paths
static TOP_LEVEL_BLACKLIST: [&str; 5] = ["bf3.ard", "bf3.arh", "movie", "sound", "skyline"];
Expand Down Expand Up @@ -36,7 +36,7 @@ impl FileLoader {
}

pub fn is_blocked(&self, file_name: &str) -> bool {
self.block_list.contains(&crc32(file_name))
self.block_list.contains(&crc32_lowercase(file_name))
}

unsafe fn import_dir(&mut self, path: &str, level: usize) -> Result<()> {
Expand Down Expand Up @@ -88,7 +88,7 @@ impl FileLoader {
fn register_file(&mut self, path: &str) {
assert!(path.len() >= 4); // rom:/<file name>
let path = &path[4..];
let hash = crc32(path);
let hash = crc32_lowercase(path);
dbg_println!("[XC3-Files] Registering {path}");
if !self.block_list.insert(hash) {
// The game also uses CRC-32 internally to cache resources. It's likely that
Expand Down

0 comments on commit a59ce4a

Please sign in to comment.