Skip to content

Commit

Permalink
Merge pull request #109 from Juliapixel/main
Browse files Browse the repository at this point in the history
Ignore reserved bits in chunks
  • Loading branch information
kornelski authored Sep 18, 2024
2 parents ce638c7 + 49751eb commit c29d6d4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
11 changes: 7 additions & 4 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ quick_error! {
display("Invalid Chunk header: {err:x?}")
}

#[allow(deprecated)]
#[deprecated]
/// Some bits were invalid
ReservedBitSet {
display("Reserved bits set")
}

/// The ALPH chunk preprocessing info flag was invalid
InvalidAlphaPreprocessing {
display("Alpha chunk preprocessing flag invalid")
}

/// Invalid compression method
InvalidCompressionMethod {
display("Invalid compression method")
Expand Down Expand Up @@ -721,10 +728,6 @@ impl<R: BufRead + Seek> WebPDecoder<R> {
}
let duration = extended::read_3_bytes(&mut self.r)?;
let frame_info = self.r.read_u8()?;
let reserved = frame_info & 0b11111100;
if reserved != 0 {
return Err(DecodingError::ReservedBitSet);
}
let use_alpha_blending = frame_info & 0b00000010 == 0;
let dispose = frame_info & 0b00000001 != 0;

Expand Down
16 changes: 3 additions & 13 deletions src/extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,14 @@ pub(crate) fn read_extended_header<R: Read>(
) -> Result<WebPExtendedInfo, DecodingError> {
let chunk_flags = reader.read_u8()?;

let reserved_first = chunk_flags & 0b11000000;
let icc_profile = chunk_flags & 0b00100000 != 0;
let alpha = chunk_flags & 0b00010000 != 0;
let exif_metadata = chunk_flags & 0b00001000 != 0;
let xmp_metadata = chunk_flags & 0b00000100 != 0;
let animation = chunk_flags & 0b00000010 != 0;
let reserved_second = chunk_flags & 0b00000001;

let reserved_third = read_3_bytes(reader)?;

if reserved_first != 0 || reserved_second != 0 || reserved_third != 0 {
return Err(DecodingError::ReservedBitSet);
}
// reserved bytes are ignored
let _reserved_bytes = read_3_bytes(reader)?;

let canvas_width = read_3_bytes(reader)? + 1;
let canvas_height = read_3_bytes(reader)? + 1;
Expand Down Expand Up @@ -304,19 +299,14 @@ pub(crate) fn read_alpha_chunk<R: BufRead>(
) -> Result<AlphaChunk, DecodingError> {
let info_byte = reader.read_u8()?;

let reserved = info_byte & 0b11000000;
let preprocessing = (info_byte & 0b00110000) >> 4;
let filtering = (info_byte & 0b00001100) >> 2;
let compression = info_byte & 0b00000011;

if reserved != 0 {
return Err(DecodingError::ReservedBitSet);
}

let preprocessing = match preprocessing {
0 => false,
1 => true,
_ => return Err(DecodingError::ReservedBitSet),
_ => return Err(DecodingError::InvalidAlphaPreprocessing),
};

let filtering_method = match filtering {
Expand Down

0 comments on commit c29d6d4

Please sign in to comment.