diff --git a/src/decoder/image.rs b/src/decoder/image.rs index 5330328..4af80e9 100644 --- a/src/decoder/image.rs +++ b/src/decoder/image.rs @@ -330,15 +330,28 @@ impl Image { strip_decoder = Some(StripDecodeState { rows_per_strip }); tile_attributes = None; - if chunk_offsets.len() != chunk_bytes.len() - || rows_per_strip == 0 - || u32::try_from(chunk_offsets.len())? - != (height.saturating_sub(1) / rows_per_strip + 1) * planes as u32 - { + if chunk_offsets.len() != chunk_bytes.len() { return Err(TiffError::FormatError( TiffFormatError::InconsistentSizesEncountered, )); } + + if rows_per_strip == 0 { + return Err(TiffError::FormatError( + TiffFormatError::InconsistentSizesEncountered, + )); + } + + if u32::try_from(chunk_offsets.len())? + != height.div_ceil(rows_per_strip) * planes as u32 + { + return Err(TiffError::FormatError( + TiffFormatError::IncorrectChunkCount { + expected: height.div_ceil(rows_per_strip) * planes as u32, + found: chunk_offsets.len() as u32, + }, + )); + } } (false, false, true, true) => { chunk_type = ChunkType::Tile; @@ -371,14 +384,23 @@ impl Image { .into_u64_vec()?; let tile = tile_attributes.as_ref().unwrap(); - if chunk_offsets.len() != chunk_bytes.len() - || chunk_offsets.len() - != tile.tiles_down() * tile.tiles_across() * planes as usize - { + + if chunk_offsets.len() != chunk_bytes.len() { return Err(TiffError::FormatError( TiffFormatError::InconsistentSizesEncountered, )); } + + if chunk_offsets.len() != tile.tiles_down() * tile.tiles_across() * planes as usize + { + return Err(TiffError::FormatError( + TiffFormatError::IncorrectChunkCount { + expected: (tile.tiles_down() * tile.tiles_across() * planes as usize) + as u32, + found: chunk_offsets.len() as u32, + }, + )); + } } (_, _, _, _) => { return Err(TiffError::FormatError( diff --git a/src/error.rs b/src/error.rs index 9311915..95c0a65 100644 --- a/src/error.rs +++ b/src/error.rs @@ -70,6 +70,9 @@ quick_error! { InconsistentSizesEncountered { display("inconsistent sizes encountered") } + IncorrectChunkCount {expected: u32, found: u32} { + display("incorrect number of chunk offsets encoded for the tile count of the image, expected {expected} but found {found}") + } InvalidDimensions(width: u32, height: u32) { display("invalid dimensions: {width}x{height}") }