//! Message block processing for the hash function.
//! Breaks messages into 16-byte blocks and calls inner_compress.

use super::inner_compress::inner_compress;

/// Process a message by breaking it into 16-byte blocks.
/// Each block is converted to [u32; 4] (little-endian) and compressed into state.
pub fn process_message(state: &mut [u32; 8], msg: &[u8]) {
    let mut offset = 0;
    while offset + 16 <= msg.len() {
        let block = u32x4_from_bytes(&msg[offset..offset + 16]);
        inner_compress(state, &block);
        offset += 16;
    }

    // Handle remaining bytes (partial block, zero-padded)
    if offset < msg.len() {
        let mut padded = [0u8; 16];
        let remaining = msg.len() - offset;
        padded[..remaining].copy_from_slice(&msg[offset..]);
        let block = u32x4_from_bytes(&padded);
        inner_compress(state, &block);
    }
}

/// Convert 16 bytes to [u32; 4] (little-endian).
pub fn u32x4_from_bytes(b: &[u8]) -> [u32; 4] {
    [
        u32::from_le_bytes([b[0], b[1], b[2], b[3]]),
        u32::from_le_bytes([b[4], b[5], b[6], b[7]]),
        u32::from_le_bytes([b[8], b[9], b[10], b[11]]),
        u32::from_le_bytes([b[12], b[13], b[14], b[15]]),
    ]
}
