dcb_tim::Header now checks for unknown flags.

This commit is contained in:
Filipe Rodrigues 2021-05-29 19:00:44 +01:00
parent c55ab5d80b
commit 8455998ae5
2 changed files with 11 additions and 2 deletions

View File

@ -26,6 +26,7 @@ impl Bytes for Header {
type FromError = FromBytesError;
type ToError = !;
#[bitmatch::bitmatch]
fn from_bytes(bytes: &Self::ByteArray) -> Result<Self, Self::FromError> {
let bytes = array_split!(bytes,
tag : 0x1,
@ -46,14 +47,18 @@ impl Bytes for Header {
// Else parse the flags
let flags = LittleEndian::read_u32(bytes.flags);
let bbp = match flags & 0b11 {
let (bbp, clut_present) = #[bitmatch]
match flags {
"0000_0000_0000_0000_0000_0000_0000_c0bb" => (b, c != 0),
_ => return Err(FromBytesError::UnknownFlag(flags)),
};
let bbp = match bbp {
0b00 => BitsPerPixel::Index4Bit,
0b01 => BitsPerPixel::Index8Bit,
0b10 => BitsPerPixel::Color16Bit,
0b11 => BitsPerPixel::Color24Bit,
_ => unreachable!(),
};
let clut_present = flags & 0b1000 != 0;
Ok(Self { bbp, clut_present })
}

View File

@ -10,4 +10,8 @@ pub enum FromBytesError {
/// Invalid version
#[error("Invalid version {_0:#x}")]
InvalidVersion(u8),
/// Unknown flag
#[error("Unknown flag {_0:#010x}")]
UnknownFlag(u32),
}