use core::slice::{from_raw_parts, from_raw_parts_mut};
use crate::{
Stream, hash_xof as root_hash_xof, hash32 as root_hash32, hash64 as root_hash64,
hash128 as root_hash128, hash256 as root_hash256,
seed::{hash_xof, hash32, hash64, hash128, hash256, stream},
};
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct IhashU128 {
pub lo: u64,
pub hi: u64,
}
#[inline(always)]
const fn pack_c_u128(digest: u128) -> IhashU128 {
IhashU128 {
lo: digest as u64,
hi: (digest >> 64) as u64,
}
}
#[inline(always)]
unsafe fn slice_from_raw<'a>(data: *const u8, len: usize) -> &'a [u8] {
if len == 0 || data.is_null() {
&[]
} else {
unsafe { from_raw_parts(data, len) }
}
}
#[inline(always)]
unsafe fn slice_mut_from_raw<'a>(out: *mut u8, len: usize) -> Option<&'a mut [u8]> {
if len == 0 || out.is_null() {
None
} else {
Some(unsafe { from_raw_parts_mut(out, len) })
}
}
#[inline(always)]
unsafe fn with_io_slices(
src: *const u8,
src_len: usize,
dst: *mut u8,
dst_len: usize,
f: impl FnOnce(&[u8], &mut [u8]),
) {
let Some(out) = (unsafe { slice_mut_from_raw(dst, dst_len) }) else {
return;
};
f(unsafe { slice_from_raw(src, src_len) }, out);
}
#[inline(always)]
unsafe fn write_digest<const N: usize>(
data: *const u8,
len: usize,
out: *mut u8,
f: impl FnOnce(&[u8]) -> [u8; N],
) {
unsafe { with_io_slices(data, len, out, N, |d, dst| dst.copy_from_slice(&f(d))) };
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ihash_hash32(data: *const u8, len: usize) -> u32 {
root_hash32(unsafe { slice_from_raw(data, len) })
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ihash_hash32_seed(data: *const u8, len: usize, seed: u64) -> u32 {
hash32(unsafe { slice_from_raw(data, len) }, seed)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ihash_hash64(data: *const u8, len: usize) -> u64 {
root_hash64(unsafe { slice_from_raw(data, len) })
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ihash_hash64_seed(data: *const u8, len: usize, seed: u64) -> u64 {
hash64(unsafe { slice_from_raw(data, len) }, seed)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ihash_hash128(data: *const u8, len: usize) -> IhashU128 {
pack_c_u128(root_hash128(unsafe { slice_from_raw(data, len) }))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ihash_hash128_seed(data: *const u8, len: usize, seed: u64) -> IhashU128 {
pack_c_u128(hash128(unsafe { slice_from_raw(data, len) }, seed))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ihash_hash128_out(data: *const u8, len: usize, out: *mut u8) {
unsafe { write_digest(data, len, out, |d| root_hash128(d).to_le_bytes()) };
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ihash_hash128_out_seed(
data: *const u8,
len: usize,
seed: u64,
out: *mut u8,
) {
unsafe { write_digest(data, len, out, |d| hash128(d, seed).to_le_bytes()) };
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ihash_hash256(data: *const u8, len: usize, out: *mut u8) {
unsafe { write_digest(data, len, out, root_hash256) };
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ihash_hash256_seed(data: *const u8, len: usize, seed: u64, out: *mut u8) {
unsafe { write_digest(data, len, out, |d| hash256(d, seed)) };
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ihash_hash_xof(data: *const u8, len: usize, out: *mut u8, out_len: usize) {
unsafe { with_io_slices(data, len, out, out_len, root_hash_xof) };
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ihash_hash_xof_seed(
data: *const u8,
len: usize,
seed: u64,
out: *mut u8,
out_len: usize,
) {
unsafe { with_io_slices(data, len, out, out_len, |d, dst| hash_xof(d, seed, dst)) };
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ihash_xor_stream(
key: *const u8,
key_len: usize,
offset: u64,
buf: *mut u8,
buf_len: usize,
) {
unsafe {
with_io_slices(key, key_len, buf, buf_len, |k, dst| {
Stream::new(k).seek(offset).xor_keystream(dst);
});
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ihash_xor_stream_seed(
key: *const u8,
key_len: usize,
seed: u64,
offset: u64,
buf: *mut u8,
buf_len: usize,
) {
unsafe {
with_io_slices(key, key_len, buf, buf_len, |k, dst| {
stream(k, seed).seek(offset).xor_keystream(dst);
});
}
}