Skip to main content

zstd_native/
error.rs

1use core::fmt;
2
3/// Errors returned by the decoder and the encoder.
4///
5/// Corrupt input never panics or reads out of bounds; it returns one of
6/// these.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[non_exhaustive]
9pub enum Error {
10    /// The input doesn't start with a zstd (or skippable) frame magic number.
11    UnknownFrame,
12    /// The input ends before the frame does.
13    Truncated,
14    /// The compressed data is corrupt.
15    Corrupted,
16    /// The frame's content checksum doesn't match the decompressed data.
17    ChecksumMismatch,
18    /// The frame header uses a reserved bit or an unsupported parameter.
19    UnsupportedFrameParameter,
20    /// The frame's window is larger than the decoder's limit.
21    WindowTooLarge,
22    /// The frame needs a dictionary that wasn't given, or a different one.
23    DictionaryMismatch,
24    /// A dictionary given to the decoder is invalid.
25    InvalidDictionary,
26    /// The output buffer is too small for the decompressed data.
27    OutputTooSmall,
28    /// The decompressed size is larger than this platform can hold in memory.
29    OutputTooLarge,
30    /// The compressor got a different amount of input than the pledged
31    /// source size.
32    SrcSizeWrong,
33    /// The operation is not possible at this stage of a stream (for example,
34    /// setting the pledged source size in the middle of a frame).
35    StageWrong,
36    /// The buffers the compression parameters need don't fit in this
37    /// platform's memory (libzstd's `memory_allocation` error): for
38    /// example, multithreaded compression with a large job size and many
39    /// workers on a 32-bit target.
40    MemoryAllocation,
41}
42
43impl fmt::Display for Error {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        f.write_str(match self {
46            Error::UnknownFrame => "unknown frame magic number (not zstd data)",
47            Error::Truncated => "compressed data is truncated",
48            Error::Corrupted => "compressed data is corrupt",
49            Error::ChecksumMismatch => "content checksum mismatch",
50            Error::UnsupportedFrameParameter => "unsupported frame parameter",
51            Error::WindowTooLarge => "frame window is larger than the decoder limit",
52            Error::DictionaryMismatch => "frame requires a different dictionary",
53            Error::InvalidDictionary => "invalid dictionary",
54            Error::OutputTooSmall => "output buffer is too small",
55            Error::OutputTooLarge => "decompressed size is too large",
56            Error::SrcSizeWrong => "input size differs from the pledged source size",
57            Error::StageWrong => "operation not possible at this stage of the stream",
58            Error::MemoryAllocation => "not enough memory for the compression parameters",
59        })
60    }
61}
62
63impl core::error::Error for Error {}
64
65#[cfg(feature = "std")]
66impl From<Error> for std::io::Error {
67    fn from(e: Error) -> Self {
68        let kind = match e {
69            Error::Truncated => std::io::ErrorKind::UnexpectedEof,
70            Error::MemoryAllocation => std::io::ErrorKind::OutOfMemory,
71            _ => std::io::ErrorKind::InvalidData,
72        };
73        std::io::Error::new(kind, e)
74    }
75}
76
77pub(crate) type Result<T> = core::result::Result<T, Error>;