http-compress 23.4.0

A high-performance async library for HTTP compression/decompression, supporting Brotli, Deflate, and Gzip algorithms. Provides both compression and decompression capabilities with optimized memory usage, ideal for HTTP clients/servers and network programming.
Documentation
use super::*;

/// Finalizes a 64-bit hash with the XXH64 avalanche mixer.
///
/// # Arguments
///
/// - `u64` - Input hash state.
///
/// # Returns
///
/// - `u64` - Mixed 64-bit hash value.
#[inline(always)]
fn avalanche_xxh64(mut value: u64) -> u64 {
    value ^= value >> 33;
    value = value.wrapping_mul(XXH3_PRIME64_2);
    value ^= value >> 29;
    value = value.wrapping_mul(XXH3_PRIME64_3);
    value ^= value >> 32;
    value
}

/// Finalizes a 64-bit hash with the XXH3 avalanche mixer.
///
/// # Arguments
///
/// - `u64` - Input hash state.
///
/// # Returns
///
/// - `u64` - Mixed 64-bit hash value.
#[inline(always)]
fn avalanche_xxh3(mut value: u64) -> u64 {
    value ^= value >> 37;
    value = value.wrapping_mul(XXH3_PRIME_MX1);
    value ^= value >> 32;
    value
}

/// Computes the exact XXH3-64 hash for default-seed inputs of `0..=16` bytes inline.
///
/// # Safety
///
/// - `input_ptr` must be valid for reads of `len` bytes and `len <= XXH3_SHORT_LIMIT`.
///
/// # Arguments
///
/// - `*const u8` - Pointer to initialized input bytes.
/// - `usize` - Input length (`0..=16`).
///
/// # Returns
///
/// - `u64` - Bit-identical XXH3-64 hash for the default seed and secret.
#[inline(always)]
pub(crate) unsafe fn hash_short_prefix(input_ptr: *const u8, len: usize) -> u64 {
    if len > 8 {
        // SAFETY: `9 <= len <= 16` guarantees both 8-byte reads are in bounds.
        let first: u64 = unsafe { u64::from_le(ptr::read_unaligned(input_ptr.cast::<u64>())) };
        let last: u64 =
            unsafe { u64::from_le(ptr::read_unaligned(input_ptr.add(len - 8).cast::<u64>())) };
        let low: u64 = XXH3_SECRET_9_16_LO ^ first;
        let high: u64 = XXH3_SECRET_9_16_HI ^ last;
        let mul_result: u128 = (low as u128).wrapping_mul(high as u128);
        let acc: u64 = (len as u64)
            .wrapping_add(low.swap_bytes())
            .wrapping_add(high)
            .wrapping_add((mul_result as u64) ^ ((mul_result >> 64) as u64));
        return avalanche_xxh3(acc);
    }
    if len >= 4 {
        // SAFETY: `4 <= len <= 8` guarantees both 4-byte reads are in bounds.
        let first: u32 = unsafe { u32::from_le(ptr::read_unaligned(input_ptr.cast::<u32>())) };
        let last: u32 =
            unsafe { u32::from_le(ptr::read_unaligned(input_ptr.add(len - 4).cast::<u32>())) };
        let combined: u64 = (last as u64) | ((first as u64) << 32);
        let mut value: u64 = XXH3_SECRET_4_8 ^ combined;
        value ^= value.rotate_left(49) ^ value.rotate_left(24);
        value = value.wrapping_mul(XXH3_PRIME_MX2);
        value ^= (value >> 35).wrapping_add(len as u64);
        value = value.wrapping_mul(XXH3_PRIME_MX2);
        value ^= value >> 28;
        return value;
    }
    if len > 0 {
        // SAFETY: `1 <= len <= 3` guarantees indices `0`, `len >> 1`, and `len - 1` are in bounds.
        let b0: u32 = unsafe { *input_ptr } as u32;
        let b_mid: u32 = unsafe { *input_ptr.add(len >> 1) } as u32;
        let b_last: u32 = unsafe { *input_ptr.add(len - 1) } as u32;
        let combined: u32 = b_last | ((len as u32) << 8) | (b0 << 16) | (b_mid << 24);
        return avalanche_xxh64(XXH3_SECRET_1_3 ^ (combined as u64));
    }
    XXH3_EMPTY_HASH
}

/// Computes the XXH3-64 hash for medium buffered inputs (`17..=128` bytes) out of line.
///
/// # Arguments
///
/// - `&[u8]` - Initialized prefix bytes.
///
/// # Returns
///
/// - `u64` - XXH3-64 hash for the default seed and secret.
#[cold]
#[inline(never)]
pub(crate) fn hash_medium_prefix(bytes: &[u8]) -> u64 {
    xxhash3_64::Hasher::oneshot(bytes)
}