use crate::coder::Result;
use crate::error::{err, error};
pub fn consume_bytes<'a>(input: &mut &'a [u8], bytes: usize) -> Result<&'a [u8]> {
if bytes > input.len() {
return err("EOF");
}
let (bytes, remaining) = input.split_at(bytes);
*input = remaining;
Ok(bytes)
}
pub fn consume_byte(input: &mut &[u8]) -> Result<u8> {
Ok(consume_bytes(input, 1)?[0])
}
pub fn consume_byte_arrays<'a, const N: usize>(
input: &mut &'a [u8],
length: usize,
) -> Result<&'a [[u8; N]]> {
if input.len() / N < length {
return err("EOF");
}
let mid = length * N;
let (bytes, remaining) = unsafe { (input.get_unchecked(..mid), input.get_unchecked(mid..)) };
*input = remaining;
Ok(bytemuck::cast_slice(bytes))
}
pub fn expect_eof(input: &[u8]) -> Result<()> {
if cfg!(not(fuzzing)) && !input.is_empty() {
err("Expected EOF")
} else {
Ok(())
}
}
pub fn mul_length(length: usize, x: usize) -> Result<usize> {
length
.checked_mul(x)
.ok_or_else(|| error("length overflow"))
}