use crate::error::{Error, Result};
pub(crate) const MAGIC: u32 = 0xFD2F_B528;
pub(crate) const SKIPPABLE_MAGIC_MASK: u32 = 0xFFFF_FFF0;
pub(crate) const SKIPPABLE_MAGIC: u32 = 0x184D_2A50;
pub(crate) const WINDOWLOG_MAX: u32 = if usize::BITS == 32 { 30 } else { 31 };
const WINDOWLOG_ABSOLUTEMIN: u32 = 10;
pub(crate) const STARTING_INPUT_LENGTH: usize = 5;
pub(crate) const FRAME_HEADER_SIZE_MIN: usize = 6;
pub(crate) const BLOCK_HEADER_SIZE: usize = 3;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FrameHeader {
pub content_size: Option<u64>,
pub window_size: u64,
pub dict_id: u32,
pub has_checksum: bool,
pub header_size: usize,
}
impl FrameHeader {
pub fn parse(src: &[u8]) -> Result<FrameHeader> {
if src.len() < 4 {
return Err(Error::Truncated);
}
let magic = u32::from_le_bytes(src[..4].try_into().unwrap());
if magic != MAGIC {
return Err(Error::UnknownFrame);
}
let Some(&fhd) = src.get(4) else {
return Err(Error::Truncated);
};
let dict_code = fhd & 3;
let checksum = (fhd >> 2) & 1 != 0;
let single = (fhd >> 5) & 1 != 0;
let fcs_id = fhd >> 6;
let size = 5
+ usize::from(!single)
+ [0, 1, 2, 4][dict_code as usize]
+ [usize::from(single), 2, 4, 8][fcs_id as usize];
if src.len() < size {
return Err(Error::Truncated);
}
if fhd & 0x08 != 0 {
return Err(Error::UnsupportedFrameParameter);
}
let mut pos = 5;
let mut window_size = 0u64;
if !single {
let wl = src[pos];
pos += 1;
let log = u32::from(wl >> 3) + WINDOWLOG_ABSOLUTEMIN;
if log > WINDOWLOG_MAX {
return Err(Error::WindowTooLarge);
}
let base = 1u64 << log;
window_size = base + (base / 8) * u64::from(wl & 7);
}
let le = |p: usize, n: usize| -> u64 {
let mut b = [0u8; 8];
b[..n].copy_from_slice(&src[p..p + n]);
u64::from_le_bytes(b)
};
let dict_len = [0, 1, 2, 4][dict_code as usize];
let dict_id = le(pos, dict_len) as u32;
pos += dict_len;
let content_size = match fcs_id {
0 => single.then(|| u64::from(src[pos])),
1 => Some(le(pos, 2) + 256),
2 => Some(le(pos, 4)),
_ => Some(le(pos, 8)),
};
if single {
window_size = content_size.unwrap_or(0);
}
Ok(FrameHeader {
content_size,
window_size,
dict_id,
has_checksum: checksum,
header_size: size,
})
}
pub(crate) fn block_size_max(&self) -> usize {
self.window_size.min(crate::block::BLOCK_SIZE_MAX as u64) as usize
}
}
#[inline]
pub(crate) fn read_le32(src: &[u8], pos: usize) -> u32 {
u32::from_le_bytes(src[pos..pos + 4].try_into().unwrap())
}
#[inline]
pub(crate) fn is_skippable(magic: u32) -> bool {
magic & SKIPPABLE_MAGIC_MASK == SKIPPABLE_MAGIC
}
#[inline]
pub(crate) fn header_size(fhd: u8) -> usize {
let single = (fhd >> 5) & 1 != 0;
5 + usize::from(!single)
+ [0, 1, 2, 4][(fhd & 3) as usize]
+ [usize::from(single), 2, 4, 8][(fhd >> 6) as usize]
}