Deal with empty jpeg_tables

This commit is contained in:
ruffson committed 2020-09-26 14:35:06 +02:00
1 parent 53aefea6a4
commit cb5319c426
4 files changed
+32 -25

No files matched your search

+4 -6
View File
@@ -10,8 +10,8 @@ use tags::{Tag, Type};
use {TiffError, TiffFormatError, TiffResult, TiffUnsupportedError}; use {TiffError, TiffFormatError, TiffResult, TiffUnsupportedError};
use self::Value::{ use self::Value::{
Ascii, Byte, Double, Float, List, Rational, RationalBig, SRational, SRationalBig, Signed, SignedBig, Ascii, Byte, Double, Float, List, Rational, RationalBig, SRational, SRationalBig, Signed,
Unsigned, UnsignedBig, SignedBig, Unsigned, UnsignedBig,
}; };
#[allow(unused_qualifications)] #[allow(unused_qualifications)]
@@ -38,9 +38,7 @@ impl Value {
pub fn into_u8(self) -> TiffResult<u8> { pub fn into_u8(self) -> TiffResult<u8> {
match self { match self {
Byte(val) => Ok(val), Byte(val) => Ok(val),
val => Err(TiffError::FormatError( val => Err(TiffError::FormatError(TiffFormatError::ByteExpected(val))),
TiffFormatError::ByteExpected(val),
)),
} }
} }
@@ -452,7 +450,7 @@ impl Entry {
let string = decoder.read_string(n)?; let string = decoder.read_string(n)?;
Ok(Ascii(string)) Ok(Ascii(string))
} }
}, }
(Type::UNDEFINED, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { (Type::UNDEFINED, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
Ok(Byte(u8::from(decoder.read_byte()?))) Ok(Byte(u8::from(decoder.read_byte()?)))
}), }),
+6 -1
View File
@@ -933,7 +933,12 @@ impl<R: Read + Seek> Decoder<R> {
pub fn read_jpeg(&mut self) -> TiffResult<DecodingResult> { pub fn read_jpeg(&mut self) -> TiffResult<DecodingResult> {
let offsets = self.get_tag_u32_vec(Tag::StripOffsets)?; let offsets = self.get_tag_u32_vec(Tag::StripOffsets)?;
let bytes = self.get_tag_u32_vec(Tag::StripByteCounts)?; let bytes = self.get_tag_u32_vec(Tag::StripByteCounts)?;
let jpeg_tables: Vec<u8> = self.get_tag_u8_vec(Tag::JPEGTables)?;
let jpeg_tables: Option<Vec<u8>> = match self.find_tag(Tag::JPEGTables) {
Ok(None) => None,
Ok(_) => Some(self.get_tag_u8_vec(Tag::JPEGTables)?),
Err(e) => return Err(e),
};
if offsets.len() == 0 { if offsets.len() == 0 {
return Err(TiffError::FormatError(TiffFormatError::RequiredTagEmpty( return Err(TiffError::FormatError(TiffFormatError::RequiredTagEmpty(
+19 -9
View File
@@ -329,7 +329,7 @@ pub(crate) struct JpegReader {
impl JpegReader { impl JpegReader {
/// Constructs new JpegReader wrapping a SmartReader. /// Constructs new JpegReader wrapping a SmartReader.
/// Because JPEG compression in TIFF allows to save quantization and/or huffman tables in one /// Because JPEG compression in TIFF allows to save quantization and/or huffman tables in one
/// central loacation, the constructor accepts this data as `jpeg_tables` here containing either /// central location, the constructor accepts this data as `jpeg_tables` here containing either
/// or both. /// or both.
/// These `jpeg_tables` are simply prepended to the remaining jpeg image data. /// These `jpeg_tables` are simply prepended to the remaining jpeg image data.
/// Because these `jpeg_tables` start with a `SOI` (HEX: `0xFFD8`) or __start of image__ marker /// Because these `jpeg_tables` start with a `SOI` (HEX: `0xFFD8`) or __start of image__ marker
@@ -342,7 +342,7 @@ impl JpegReader {
pub fn new<R>( pub fn new<R>(
reader: &mut SmartReader<R>, reader: &mut SmartReader<R>,
length: u32, length: u32,
jpeg_tables: &Vec<u8>, jpeg_tables: &Option<Vec<u8>>,
) -> io::Result<JpegReader> ) -> io::Result<JpegReader>
where where
R: Read + Seek, R: Read + Seek,
@@ -353,15 +353,25 @@ impl JpegReader {
let mut segment = vec![0; length as usize]; let mut segment = vec![0; length as usize];
reader.read_exact(&mut segment[..])?; reader.read_exact(&mut segment[..])?;
let mut jpeg_data = jpeg_tables.clone(); match jpeg_tables {
jpeg_data.truncate(jpeg_data.len() - 2); Some(tables) => {
let mut jpeg_data = tables.clone();
jpeg_data.truncate(jpeg_data.len() - 2);
jpeg_data.extend_from_slice(&mut segment[2..]);
jpeg_data.extend_from_slice(&mut segment[2..]); Ok(JpegReader {
buffer: io::Cursor::new(jpeg_data),
byte_order: order,
})
},
None => {
Ok(JpegReader {
buffer: io::Cursor::new(segment),
byte_order: order,
})
}
}
Ok(JpegReader {
buffer: io::Cursor::new(jpeg_data),
byte_order: order,
})
} }
} }
+3 -9
View File
@@ -72,9 +72,7 @@ impl fmt::Display for TiffFormatError {
UnknownPredictor(ref predictor) => { UnknownPredictor(ref predictor) => {
write!(fmt, "Unknown predictor “{}” encountered", predictor) write!(fmt, "Unknown predictor “{}” encountered", predictor)
} }
ByteExpected(ref val) => { ByteExpected(ref val) => write!(fmt, "Expected byte, {:?} found.", val),
write!(fmt, "Expected byte, {:?} found.", val)
}
UnsignedIntegerExpected(ref val) => { UnsignedIntegerExpected(ref val) => {
write!(fmt, "Expected unsigned integer, {:?} found.", val) write!(fmt, "Expected unsigned integer, {:?} found.", val)
} }
@@ -82,12 +80,8 @@ impl fmt::Display for TiffFormatError {
write!(fmt, "Expected signed integer, {:?} found.", val) write!(fmt, "Expected signed integer, {:?} found.", val)
} }
InflateError(_) => write!(fmt, "Failed to decode inflate data."), InflateError(_) => write!(fmt, "Failed to decode inflate data."),
Format(ref val) => { Format(ref val) => write!(fmt, "Invalid format: {:?}.", val),
write!(fmt, "Invalid format: {:?}.", val) RequiredTagEmpty(ref val) => write!(fmt, "Required tag {:?} was empty.", val),
}
RequiredTagEmpty(ref val) => {
write!(fmt, "Required tag {:?} was empty.", val)
}
__NonExhaustive => unreachable!(), __NonExhaustive => unreachable!(),
} }
} }