use super::*;
#[derive(Debug, Clone)]
pub struct FileHashes {
pub md5_full: [u8; 16],
pub md5_16k: [u8; 16],
pub length: u64,
}
pub struct FileHasher {
full: md5_many::Md5State,
head: Md5,
head_consumed: usize,
length: u64,
many: md5_many::Md5Many,
}
impl FileHasher {
pub fn new() -> Self {
Self {
full: md5_many::Md5State::new(),
head: Md5::new(),
head_consumed: 0,
length: 0,
many: md5_many::Md5Many::new(),
}
}
pub fn update(&mut self, data: &[u8]) {
self.full.update(data);
self.length += data.len() as u64;
let room = HEAD_LEN - self.head_consumed;
if room > 0 {
let take = room.min(data.len());
self.head.update(&data[..take]);
self.head_consumed += take;
}
}
pub fn update_and_hash_slice(
&mut self,
padded_slice: &[u8],
actual_len: usize,
) -> SliceChecksum {
let mut slice_state = md5_many::Md5State::new();
let mut crc = crc32fast::Hasher::new();
let unpadded = &padded_slice[..actual_len];
self.length += actual_len as u64;
let room = HEAD_LEN - self.head_consumed;
if room > 0 {
let take = room.min(actual_len);
self.head.update(&unpadded[..take]);
self.head_consumed += take;
}
for chunk_start in (0..padded_slice.len()).step_by(64 * 1024) {
let chunk_end = (chunk_start + 64 * 1024).min(padded_slice.len());
let chunk = &padded_slice[chunk_start..chunk_end];
crc.update(chunk);
let chunk_actual_len = if chunk_start >= actual_len {
0
} else {
(actual_len - chunk_start).min(chunk.len())
};
if chunk_actual_len == chunk.len() {
let mut states = [self.full, slice_state];
self.many.update_many(&mut states, &[chunk, chunk]);
self.full = states[0];
slice_state = states[1];
} else if chunk_actual_len > 0 {
let unpadded_part = &chunk[..chunk_actual_len];
let padded_part = &chunk[chunk_actual_len..];
let mut states = [self.full, slice_state];
self.many
.update_many(&mut states, &[unpadded_part, unpadded_part]);
self.full = states[0];
slice_state = states[1];
slice_state.update(padded_part);
} else {
slice_state.update(chunk);
}
}
SliceChecksum {
md5: slice_state.finalize(),
crc32: crc.finalize(),
}
}
pub fn finish(self) -> FileHashes {
let mut md5_16k = [0u8; 16];
md5_16k.copy_from_slice(&self.head.finalize());
FileHashes {
md5_full: self.full.finalize(),
md5_16k,
length: self.length,
}
}
}
impl Default for FileHasher {
fn default() -> Self {
Self::new()
}
}
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
#[inline]
pub(super) unsafe fn gf16_clmul_reduce_neon(
low1: &mut poly16x8_t,
low2: &mut poly16x8_t,
mid1: poly16x8_t,
mid2: poly16x8_t,
high1: &mut poly16x8_t,
high2: &mut poly16x8_t,
) {
use std::arch::aarch64::*;
let hib = vuzpq_u8(vreinterpretq_u8_p16(*high1), vreinterpretq_u8_p16(*high2));
let lob = vuzpq_u8(vreinterpretq_u8_p16(*low1), vreinterpretq_u8_p16(*low2));
let mib = vuzpq_u8(vreinterpretq_u8_p16(mid1), vreinterpretq_u8_p16(mid2));
let lib = veorq_u8(hib.0, lob.1); let lob1 = veorq_u8(veorq_u8(lib, lob.0), mib.0); let hib0 = veorq_u8(veorq_u8(lib, hib.1), mib.1);
let th0_a = vsriq_n_u8::<4>(vshlq_n_u8::<4>(hib.1), hib0);
let th1_a = veorq_u8(hib.1, vshrq_n_u8::<4>(hib.1));
let mut th0 = veorq_u8(veorq_u8(th0_a, th1_a), hib0);
let th0_hi3 = vshrq_n_u8::<5>(th0);
const TBL: [u8; 16] = [0, 1, 2, 3, 5, 4, 7, 6, 0, 0, 0, 0, 0, 0, 0, 0];
let tbl_v = vld1q_u8(TBL.as_ptr());
let th0_hi3r = vqtbl1q_u8(tbl_v, th0_hi3);
th0 = veorq_u8(th0, vshrq_n_u8::<5>(hib.1));
let red_l = vdupq_n_p8(0x0b);
let hib1_new = vsliq_n_u8::<4>(th0_hi3r, th0);
let th1_new = vreinterpretq_u8_p8(vmulq_p8(vreinterpretq_p8_u8(th1_a), red_l));
let hib0_new = vreinterpretq_u8_p8(vmulq_p8(vreinterpretq_p8_u8(th0), red_l));
*low1 = vreinterpretq_p16_u8(lob.0);
*low2 = vreinterpretq_p16_u8(hib0_new);
*high1 = vreinterpretq_p16_u8(veorq_u8(hib1_new, th1_new));
*high2 = vreinterpretq_p16_u8(lob1);
}
pub fn slice_checksum(padded_slice: &[u8]) -> SliceChecksum {
let mut digest = Md5::new();
let mut crc = crc32fast::Hasher::new();
for chunk in padded_slice.chunks(64 * 1024) {
digest.update(chunk);
crc.update(chunk);
}
let mut md5_out = [0u8; 16];
md5_out.copy_from_slice(&digest.finalize());
SliceChecksum {
md5: md5_out,
crc32: crc.finalize(),
}
}
#[cfg(target_arch = "x86_64")]
pub(super) fn slice_checksums_batch(padded_slices: &[Vec<u8>]) -> Vec<SliceChecksum> {
if padded_slices.is_empty() {
return Vec::new();
}
let inputs: Vec<&[u8]> = padded_slices.iter().map(Vec::as_slice).collect();
let (md5s, crc32s) = rayon::join(
|| {
let mut outputs = vec![[0u8; 16]; inputs.len()];
md5_many::Md5Many::new().hash_many(&inputs, &mut outputs);
outputs
},
|| {
padded_slices
.par_iter()
.map(|slice| crc32fast::hash(slice))
.collect::<Vec<_>>()
},
);
md5s.into_iter()
.zip(crc32s)
.map(|(md5, crc32)| SliceChecksum { md5, crc32 })
.collect()
}