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