From cb900819d9cb9996a9d44908eca97cb915598da8 Mon Sep 17 00:00:00 2001 From: ruffson Date: Sat, 26 Sep 2020 14:07:27 +0200 Subject: [PATCH] Remove tile processing and add error handling I removed the code that checked the tile attributes. Because I would rather later on PR something that works than a half-baked solution. --- src/decoder/mod.rs | 23 +++++++++++------------ src/error.rs | 4 ++++ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index 6e98275..cda4d76 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -931,20 +931,19 @@ impl Decoder { } pub fn read_jpeg(&mut self) -> TiffResult { - let offsets: Vec; - let bytes: Vec; + let offsets = self.get_tag_u32_vec(Tag::StripOffsets)?; + let bytes = self.get_tag_u32_vec(Tag::StripByteCounts)?; let jpeg_tables: Vec = self.get_tag_u8_vec(Tag::JPEGTables)?; - match self.find_tag(Tag::TileOffsets) { - Ok(None) => { - offsets = self.get_tag_u32_vec(Tag::StripOffsets)?; - bytes = self.get_tag_u32_vec(Tag::StripByteCounts)?; - } - Ok(_) => { - offsets = self.get_tag_u32_vec(Tag::TileOffsets)?; - bytes = self.get_tag_u32_vec(Tag::TileByteCounts)?; - } - Err(e) => return Err(e), + if offsets.len() == 0 { + return Err(TiffError::FormatError(TiffFormatError::RequiredTagEmpty( + Tag::StripOffsets, + ))); + } + if offsets.len() != bytes.len() { + return Err(TiffError::FormatError(TiffFormatError::Format( + String::from("Size mismatch of StripOffsets and StripByteCounts"), + ))); } let mut res_img = Vec::with_capacity(offsets[0] as usize); diff --git a/src/error.rs b/src/error.rs index 8bef943..15e0e7e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -50,6 +50,7 @@ pub enum TiffFormatError { SignedIntegerExpected(Value), InflateError(InflateError), Format(String), + RequiredTagEmpty(Tag), #[doc(hidden)] /// Do not match against this variant. It may get removed. __NonExhaustive, @@ -84,6 +85,9 @@ impl fmt::Display for TiffFormatError { Format(ref val) => { write!(fmt, "Invalid format: {:?}.", val) } + RequiredTagEmpty(ref val) => { + write!(fmt, "Required tag {:?} was empty.", val) + } __NonExhaustive => unreachable!(), } }