use super::TABLE;
pub(in crate::headers) fn encoded_length_if_shorter(input: &[u8]) -> Option<usize> {
let budget_bits = input.len().checked_mul(8).and_then(|x| x.checked_sub(8))?;
let mut total_bits: usize = 0;
for &byte in input {
total_bits += usize::from(TABLE[byte as usize].1);
if total_bits > budget_bits {
return None;
}
}
Some(total_bits.div_ceil(8))
}
pub(in crate::headers) fn encode_into(input: &[u8], buf: &mut Vec<u8>) {
let mut accumulator: u64 = 0;
let mut bits_used: u8 = 0;
for &byte in input {
let (code, bit_length) = TABLE[byte as usize];
accumulator |= u64::from(code) << (64 - bits_used - bit_length);
bits_used += bit_length;
while bits_used >= 8 {
buf.push((accumulator >> 56) as u8);
accumulator <<= 8;
bits_used -= 8;
}
}
if bits_used > 0 {
accumulator |= 0xFF_u64 << (56 - bits_used);
buf.push((accumulator >> 56) as u8);
}
}