Error::BadDecodingDataSize -> InputBufferTooSmall

This commit is contained in:
Ivan Smirnov 2021-12-01 16:01:17 +00:00
parent d35fa000fa
commit d7c8346e39
2 changed files with 8 additions and 6 deletions

View file

@ -36,7 +36,10 @@ where
Pixel<N>: SupportedChannels, Pixel<N>: SupportedChannels,
{ {
if data.len() < QOI_HEADER_SIZE + QOI_PADDING { if data.len() < QOI_HEADER_SIZE + QOI_PADDING {
return Err(Error::BadDecodingDataSize { size: data.len() }); return Err(Error::InputBufferTooSmall {
size: data.len(),
required: QOI_HEADER_SIZE + QOI_PADDING,
});
} }
let header = Header::from_bytes(unsafe { let header = Header::from_bytes(unsafe {
// Safety: Header is a POD type and we have just checked that the data fits it // Safety: Header is a POD type and we have just checked that the data fits it

View file

@ -2,14 +2,14 @@ use std::error::Error as StdError;
use std::fmt::{self, Display}; use std::fmt::{self, Display};
use std::result::Result as StdResult; use std::result::Result as StdResult;
use crate::consts::{QOI_HEADER_SIZE, QOI_MAGIC, QOI_PADDING}; use crate::consts::QOI_MAGIC;
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Error { pub enum Error {
InvalidChannels { channels: u8 }, InvalidChannels { channels: u8 },
EmptyImage { width: u32, height: u32 }, EmptyImage { width: u32, height: u32 },
BadEncodingDataSize { size: usize, expected: usize }, BadEncodingDataSize { size: usize, expected: usize },
BadDecodingDataSize { size: usize }, InputBufferTooSmall { size: usize, required: usize },
OutputBufferTooSmall { size: usize, required: usize }, OutputBufferTooSmall { size: usize, required: usize },
InvalidMagic { magic: u32 }, InvalidMagic { magic: u32 },
// TODO: invalid colorspace // TODO: invalid colorspace
@ -29,9 +29,8 @@ impl Display for Error {
Self::BadEncodingDataSize { size, expected } => { Self::BadEncodingDataSize { size, expected } => {
write!(f, "bad data size when encoding: {} (expected: {})", size, expected) write!(f, "bad data size when encoding: {} (expected: {})", size, expected)
} }
Self::BadDecodingDataSize { size } => { Self::InputBufferTooSmall { size, required } => {
let min_size = QOI_HEADER_SIZE + QOI_PADDING; write!(f, "input buffer size too small: {} (minimum required: {})", size, required)
write!(f, "bad data size when decoding: {} (minimum required: {})", size, min_size)
} }
Self::OutputBufferTooSmall { size, required } => { Self::OutputBufferTooSmall { size, required } => {
write!(f, "output buffer size too small: {} (minimum required: {})", size, required) write!(f, "output buffer size too small: {} (minimum required: {})", size, required)