Improved error messages for inflate decompression
This commit is contained in:
1 parent
205626bd62
commit
6f9f2adfbf
3 files changed
+27
-9
No files matched your search
+1
-1
@@ -21,7 +21,7 @@ byteorder = "1.2"
|
||||
lzw = "0.10"
|
||||
num-derive = "0.3"
|
||||
num-traits = "0.2"
|
||||
miniz_oxide = "0.3" # TODO: Put behind a feature?
|
||||
miniz_oxide = "0.3"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3.0"
|
||||
@@ -1,9 +1,10 @@
|
||||
//! All IO functionality needed for TIFF decoding
|
||||
|
||||
use crate::error::TiffResult;
|
||||
use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
|
||||
use lzw;
|
||||
use std::io::{self, Read, Seek};
|
||||
use miniz_oxide::inflate;
|
||||
use std::io::{self, Read, Seek};
|
||||
|
||||
/// Byte order of the TIFF file.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
@@ -75,15 +76,12 @@ pub struct DeflateReader {
|
||||
}
|
||||
|
||||
impl DeflateReader {
|
||||
pub fn new<R: Read + Seek>(
|
||||
reader: &mut SmartReader<R>,
|
||||
) -> io::Result<(usize, Self)> {
|
||||
pub fn new<R: Read + Seek>(reader: &mut SmartReader<R>) -> TiffResult<(usize, Self)> {
|
||||
let byte_order = reader.byte_order;
|
||||
let mut compressed = Vec::new();
|
||||
reader.read_to_end(&mut compressed)?;
|
||||
let uncompressed = inflate::decompress_to_vec_zlib(&compressed).map_err(|error| {
|
||||
std::io::Error::new(std::io::ErrorKind::InvalidData, format!("DEFLATE error: {:?}", error))
|
||||
})?;
|
||||
let uncompressed = inflate::decompress_to_vec_zlib(&compressed)?;
|
||||
|
||||
Ok((
|
||||
uncompressed.len(),
|
||||
Self {
|
||||
|
||||
+21
-1
@@ -5,6 +5,7 @@ use std::string;
|
||||
|
||||
use decoder::ifd::{Tag, Value};
|
||||
use decoder::{CompressionMethod, PhotometricInterpretation, PlanarConfiguration};
|
||||
use miniz_oxide::inflate::TINFLStatus;
|
||||
use ColorType;
|
||||
|
||||
/// Tiff error kinds.
|
||||
@@ -34,6 +35,7 @@ pub enum TiffFormatError {
|
||||
UnknownPredictor(u32),
|
||||
UnsignedIntegerExpected(Value),
|
||||
SignedIntegerExpected(Value),
|
||||
InflateError(InflateError),
|
||||
}
|
||||
|
||||
impl fmt::Display for TiffFormatError {
|
||||
@@ -55,10 +57,28 @@ impl fmt::Display for TiffFormatError {
|
||||
SignedIntegerExpected(ref val) => {
|
||||
write!(fmt, "Expected signed integer, {:?} found.", val)
|
||||
}
|
||||
InflateError(_) => write!(fmt, "Failed to decode inflate data."),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct InflateError {
|
||||
status: TINFLStatus,
|
||||
}
|
||||
|
||||
impl From<TINFLStatus> for TiffFormatError {
|
||||
fn from(status: TINFLStatus) -> Self {
|
||||
TiffFormatError::InflateError(InflateError { status })
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TINFLStatus> for TiffError {
|
||||
fn from(status: TINFLStatus) -> Self {
|
||||
TiffError::FormatError(status.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum TiffUnsupportedError {
|
||||
HorizontalPredictor(ColorType),
|
||||
@@ -138,7 +158,7 @@ impl Error for TiffError {
|
||||
}
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<& dyn Error> {
|
||||
fn cause(&self) -> Option<&dyn Error> {
|
||||
match *self {
|
||||
TiffError::IoError(ref e) => Some(e),
|
||||
_ => None,
|
||||
|
||||
Reference in new issue
Block a user