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 self::Value::{
Ascii, Byte, Double, Float, List, Rational, RationalBig, SRational, SRationalBig, Signed, SignedBig,
Unsigned, UnsignedBig,
Ascii, Byte, Double, Float, List, Rational, RationalBig, SRational, SRationalBig, Signed,
SignedBig, Unsigned, UnsignedBig,
};
#[allow(unused_qualifications)]
@@ -38,9 +38,7 @@ impl Value {
pub fn into_u8(self) -> TiffResult<u8> {
match self {
Byte(val) => Ok(val),
val => Err(TiffError::FormatError(
TiffFormatError::ByteExpected(val),
)),
val => Err(TiffError::FormatError(TiffFormatError::ByteExpected(val))),
}
}
@@ -452,7 +450,7 @@ impl Entry {
let string = decoder.read_string(n)?;
Ok(Ascii(string))
}
},
}
(Type::UNDEFINED, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
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> {
let offsets = self.get_tag_u32_vec(Tag::StripOffsets)?;
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 {
return Err(TiffError::FormatError(TiffFormatError::RequiredTagEmpty(
+19 -9
View File
@@ -329,7 +329,7 @@ pub(crate) struct JpegReader {
impl JpegReader {
/// Constructs new JpegReader wrapping a SmartReader.
/// 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.
/// 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
@@ -342,7 +342,7 @@ impl JpegReader {
pub fn new<R>(
reader: &mut SmartReader<R>,
length: u32,
jpeg_tables: &Vec<u8>,
jpeg_tables: &Option<Vec<u8>>,
) -> io::Result<JpegReader>
where
R: Read + Seek,
@@ -353,15 +353,25 @@ impl JpegReader {
let mut segment = vec![0; length as usize];
reader.read_exact(&mut segment[..])?;
let mut jpeg_data = jpeg_tables.clone();
jpeg_data.truncate(jpeg_data.len() - 2);
match jpeg_tables {
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) => {
write!(fmt, "Unknown predictor “{}” encountered", predictor)
}
ByteExpected(ref val) => {
write!(fmt, "Expected byte, {:?} found.", val)
}
ByteExpected(ref val) => write!(fmt, "Expected byte, {:?} found.", val),
UnsignedIntegerExpected(ref val) => {
write!(fmt, "Expected unsigned integer, {:?} found.", val)
}
@@ -82,12 +80,8 @@ impl fmt::Display for TiffFormatError {
write!(fmt, "Expected signed integer, {:?} found.", val)
}
InflateError(_) => write!(fmt, "Failed to decode inflate data."),
Format(ref val) => {
write!(fmt, "Invalid format: {:?}.", val)
}
RequiredTagEmpty(ref val) => {
write!(fmt, "Required tag {:?} was empty.", val)
}
Format(ref val) => write!(fmt, "Invalid format: {:?}.", val),
RequiredTagEmpty(ref val) => write!(fmt, "Required tag {:?} was empty.", val),
__NonExhaustive => unreachable!(),
}
}