rc_zip/
error.rs

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