From 33280b7c9e8f982d8f22ee793489025448ee2694 Mon Sep 17 00:00:00 2001 From: gennyble Date: Sat, 15 Jul 2023 18:59:36 -0500 Subject: weezl errors propagate instead of panic is that how you spell that workd? --- gifed/src/block/indexedimage.rs | 9 ++++++--- gifed/src/reader/mod.rs | 4 ++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/gifed/src/block/indexedimage.rs b/gifed/src/block/indexedimage.rs index 8742901..1868382 100644 --- a/gifed/src/block/indexedimage.rs +++ b/gifed/src/block/indexedimage.rs @@ -1,4 +1,4 @@ -use weezl::encode::Encoder; +use weezl::{encode::Encoder, LzwError}; use crate::{reader::DecodeError, EncodeError}; @@ -129,8 +129,11 @@ impl CompressedImage { let data: Vec = blocks.into_iter().map(<_>::into_iter).flatten().collect(); //TODO: remove unwrap - let mut decompressor = weezl::decode::Decoder::new(weezl::BitOrder::Lsb, lzw_code_size); - let indicies = decompressor.decode(&data).unwrap(); + let mut decompressor = weezl::decode::Decoder::new(weezl::BitOrder::Msb, lzw_code_size); + let indicies = match decompressor.decode(&data) { + Err(LzwError::InvalidCode) => Err(DecodeError::LzwInvalidCode), + Ok(o) => Ok(o), + }?; Ok(IndexedImage { image_descriptor, diff --git a/gifed/src/reader/mod.rs b/gifed/src/reader/mod.rs index b1bcc32..aed2142 100644 --- a/gifed/src/reader/mod.rs +++ b/gifed/src/reader/mod.rs @@ -295,6 +295,7 @@ pub enum DecodeError { IoError(std::io::Error), UnknownVersionString, UnexpectedEof, + LzwInvalidCode, ColorIndexOutOfBounds, InvalidVersion, UnknownBlock { byte: u8 }, @@ -312,6 +313,9 @@ impl fmt::Display for DecodeError { DecodeError::UnexpectedEof => { write!(f, "Found the end of the data at a weird spot") } + DecodeError::LzwInvalidCode => { + write!(f, "the LZW stream contained invalid data") + } DecodeError::ColorIndexOutOfBounds => { write!( f, -- cgit 1.4.1-3-g733a5