rc_zip/
error.rs

1//! All error types used in this crate
2
3use crate::parse::Method;
4
5use super::encoding;
6
7/// Any zip-related error, from invalid archives to encoding problems.
8#[derive(Debug, thiserror::Error)]
9pub enum Error {
10    /// Not a valid zip file, or a variant that is unsupported.
11    #[error("format: {0}")]
12    Format(#[from] FormatError),
13
14    /// Something is not supported by this crate
15    #[error("unsupported: {0}")]
16    Unsupported(#[from] UnsupportedError),
17
18    /// Invalid UTF-8, Shift-JIS, or any problem encountered while decoding text in general.
19    #[error("encoding: {0:?}")]
20    Encoding(#[from] encoding::DecodingError),
21
22    /// I/O-related error
23    #[error("io: {0}")]
24    IO(#[from] std::io::Error),
25
26    /// Decompression-related error
27    #[error("{method:?} decompression error: {msg}")]
28    Decompression {
29        /// The compression method that failed
30        method: Method,
31        /// Additional information
32        msg: String,
33    },
34
35    /// Could not read as a zip because size could not be determined
36    #[error("size must be known to open zip file")]
37    UnknownSize,
38}
39
40impl Error {
41    /// Create a new error indicating that the given method is not supported.
42    pub fn method_not_supported(method: Method) -> Self {
43        Self::Unsupported(UnsupportedError::MethodNotSupported(method))
44    }
45
46    /// Create a new error indicating that the given method is not enabled.
47    pub fn method_not_enabled(method: Method) -> Self {
48        Self::Unsupported(UnsupportedError::MethodNotEnabled(method))
49    }
50}
51
52/// Some part of the zip format is not supported by this crate.
53#[derive(Debug, thiserror::Error)]
54pub enum UnsupportedError {
55    /// The compression method is not supported.
56    #[error("compression method not supported: {0:?}")]
57    MethodNotSupported(Method),
58
59    /// The compression method is supported, but not enabled in this build.
60    #[error("compression method supported, but not enabled in this build: {0:?}")]
61    MethodNotEnabled(Method),
62
63    /// The zip file uses a version of LZMA that is not supported.
64    #[error("only LZMA2.0 is supported, found LZMA{minor}.{major}")]
65    LzmaVersionUnsupported {
66        /// major version read from LZMA properties header, cf. appnote 5.8.8
67        major: u8,
68        /// minor version read from LZMA properties header, cf. appnote 5.8.8
69        minor: u8,
70    },
71
72    /// The LZMA properties header is not the expected size.
73    #[error("LZMA properties header wrong size: expected {expected} bytes, got {actual} bytes")]
74    LzmaPropertiesHeaderWrongSize {
75        /// expected size in bytes
76        expected: u16,
77        /// actual size in bytes, read from a u16, cf. appnote 5.8.8
78        actual: u16,
79    },
80}
81
82/// Specific zip format errors, mostly due to invalid zip archives but that could also stem from
83/// implementation shortcomings.
84#[derive(Debug, thiserror::Error)]
85pub enum FormatError {
86    /// The end of central directory record was not found.
87    ///
88    /// This usually indicates that the file being read is not a zip archive.
89    #[error("end of central directory record not found")]
90    DirectoryEndSignatureNotFound,
91
92    /// The zip64 end of central directory record could not be parsed.
93    ///
94    /// This is only returned when a zip64 end of central directory *locator* was found,
95    /// so the archive should be zip64, but isn't.
96    #[error("zip64 end of central directory record not found")]
97    Directory64EndRecordInvalid,
98
99    /// Corrupted/partial zip file: the offset we found for the central directory
100    /// points outside of the current file.
101    #[error("directory offset points outside of file")]
102    DirectoryOffsetPointsOutsideFile,
103
104    /// The central record is corrupted somewhat.
105    ///
106    /// This can happen when the end of central directory record advertises
107    /// a certain number of files, but we weren't able to read the same number of central directory
108    /// headers.
109    #[error("invalid central record: expected to read {expected} files, got {actual}")]
110    InvalidCentralRecord {
111        /// expected number of files
112        expected: u16,
113        /// actual number of files
114        actual: u16,
115    },
116
117    /// An extra field (that we support) was not decoded correctly.
118    ///
119    /// This can indicate an invalid zip archive, or an implementation error in this crate.
120    #[error("could not decode extra field")]
121    InvalidExtraField,
122
123    /// The header offset of an entry is invalid.
124    ///
125    /// This can indicate an invalid zip archive, or an invalid user-provided global offset
126    #[error("invalid header offset")]
127    InvalidHeaderOffset,
128
129    /// End of central directory record claims an impossible number of files.
130    ///
131    /// Each entry takes a minimum amount of size, so if the overall archive size is smaller than
132    /// claimed_records_count * minimum_entry_size, we know it's not a valid zip file.
133    #[error("impossible number of files: claims to have {claimed_records_count}, but zip size is {zip_size}")]
134    ImpossibleNumberOfFiles {
135        /// number of files claimed in the end of central directory record
136        claimed_records_count: u64,
137        /// total size of the zip file
138        zip_size: u64,
139    },
140
141    /// The local file header (before the file data) could not be parsed correctly.
142    #[error("invalid local file header")]
143    InvalidLocalHeader,
144
145    /// The data descriptor (after the file data) could not be parsed correctly.
146    #[error("invalid data descriptor")]
147    InvalidDataDescriptor,
148
149    /// The uncompressed size didn't match
150    #[error("uncompressed size didn't match: expected {expected}, got {actual}")]
151    WrongSize {
152        /// expected size in bytes (from the local header, data descriptor, etc.)
153        expected: u64,
154        /// actual size in bytes (from decompressing the entry)
155        actual: u64,
156    },
157
158    /// The CRC-32 checksum didn't match.
159    #[error("checksum didn't match: expected {expected:x?}, got {actual:x?}")]
160    WrongChecksum {
161        /// expected checksum (from the data descriptor, etc.)
162        expected: u32,
163        /// actual checksum (from decompressing the entry)
164        actual: u32,
165    },
166}
167
168impl From<Error> for std::io::Error {
169    fn from(e: Error) -> Self {
170        match e {
171            Error::IO(e) => e,
172            e => std::io::Error::new(std::io::ErrorKind::Other, e),
173        }
174    }
175}