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