diff --git a/repak/src/pak.rs b/repak/src/pak.rs index 9223643..bf919bc 100644 --- a/repak/src/pak.rs +++ b/repak/src/pak.rs @@ -599,12 +599,7 @@ fn generate_path_hash_index( ) -> Result<(), super::Error> { writer.write_u32::(entries.len() as u32)?; for (path, offset) in entries.keys().zip(offsets) { - let utf16le_path = path - .to_lowercase() - .encode_utf16() - .flat_map(|c| c.to_le_bytes()) - .collect::>(); - let path_hash = fnv64(&utf16le_path, path_hash_seed); + let path_hash = fnv64_path(path, path_hash_seed); writer.write_u64::(path_hash)?; writer.write_u32::(*offset)?; } @@ -614,17 +609,26 @@ fn generate_path_hash_index( Ok(()) } -fn fnv64(data: &[u8], offset: u64) -> u64 { +fn fnv64(data: I, offset: u64) -> u64 +where + I: IntoIterator, +{ const OFFSET: u64 = 0xcbf29ce484222325; const PRIME: u64 = 0x00000100000001b3; let mut hash = OFFSET.wrapping_add(offset); - for &b in data { + for b in data.into_iter() { hash ^= b as u64; hash = hash.wrapping_mul(PRIME); } hash } +fn fnv64_path(path: &str, offset: u64) -> u64 { + let lower = path.to_lowercase(); + let data = lower.encode_utf16().flat_map(u16::to_le_bytes); + fnv64(data, offset) +} + fn split_path_child(path: &str) -> Option<(&str, &str)> { if path == "/" || path.is_empty() { None