Skip to main content

async_zip/
error.rs

1// Copyright (c) 2021 Harry [Majored] [hello@majored.pw]
2// MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE)
3
4//! A module which holds relevant error reporting structures/types.
5
6use std::fmt::{Display, Formatter};
7use thiserror::Error;
8
9/// A Result type alias over ZipError to minimise repetition.
10pub type Result<V> = std::result::Result<V, ZipError>;
11
12#[derive(Debug, PartialEq, Eq)]
13pub enum Zip64ErrorCase {
14    TooManyFiles,
15    LargeFile,
16}
17
18impl Display for Zip64ErrorCase {
19    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20        match self {
21            Self::TooManyFiles => write!(f, "More than 65536 files in archive"),
22            Self::LargeFile => write!(f, "File is larger than 4 GiB"),
23        }
24    }
25}
26
27/// An enum of possible errors and their descriptions.
28#[non_exhaustive]
29#[derive(Debug, Error)]
30pub enum ZipError {
31    #[error("feature not supported: '{0}'")]
32    FeatureNotSupported(&'static str),
33    #[error("compression not supported: {0}")]
34    CompressionNotSupported(u16),
35    #[error("host attribute compatibility not supported: {0}")]
36    AttributeCompatibilityNotSupported(u16),
37    #[error("attempted to read a ZIP64 file whilst on a 32-bit target")]
38    TargetZip64NotSupported,
39    #[error("attempted to write a ZIP file with force_no_zip64 when ZIP64 is needed: {0}")]
40    Zip64Needed(Zip64ErrorCase),
41    #[error("end of file has not been reached")]
42    EOFNotReached,
43    #[error("extra fields exceeded maximum size")]
44    ExtraFieldTooLarge,
45    #[error("comment exceeded maximum size")]
46    CommentTooLarge,
47    #[error("filename exceeded maximum size")]
48    FileNameTooLarge,
49    #[error("filename contained an embedded NUL byte")]
50    FileNameContainsNul { filename: Vec<u8> },
51    #[error("attempted to convert non-UTF8 bytes to a string/str")]
52    StringNotUtf8,
53
54    #[error("unable to locate the end of central directory record")]
55    UnableToLocateEOCDR,
56    #[error("extra field size was indicated to be {0} but fewer than {0} bytes remain")]
57    InvalidExtraFieldHeader(u16),
58    #[error("zip64 extended information field was incomplete")]
59    Zip64ExtendedFieldIncomplete,
60    #[error("an extra field with id {0:#x} was duplicated in the header")]
61    DuplicateExtraFieldHeader(u16),
62
63    #[error("an upstream reader returned an error: {0}")]
64    UpstreamReadError(#[from] std::io::Error),
65    #[error("a computed CRC32 value did not match the expected value")]
66    CRC32CheckError,
67    #[error("entry index was out of bounds")]
68    EntryIndexOutOfBounds,
69    #[error("the local file header name did not match the central directory name")]
70    LocalFileHeaderNameMismatch,
71    #[error("local file header data-descriptor flag did not match the central directory flag")]
72    LocalFileHeaderDataDescriptorMismatch,
73    #[error("local file header sizes did not match the central directory sizes")]
74    LocalFileHeaderSizeMismatch,
75    #[error("entry data range was invalid")]
76    InvalidEntryDataRange,
77    #[error("entry data range ({start:#x}..{end:#x}) overlapped ZIP structure at {boundary:#x}")]
78    EntryDataRangeOverlap { start: u64, end: u64, boundary: u64 },
79    #[error("ZIP version {version} is too low for compression method {compression}; expected {required}")]
80    InvalidCompressionVersion { version: u16, required: u16, compression: u16 },
81    #[error("Encountered an unexpected header (actual: {0:#x}, expected: {1:#x}).")]
82    UnexpectedHeaderError(u32, u32),
83
84    #[error("Info-ZIP Unicode Comment Extra Field was incomplete")]
85    InfoZipUnicodeCommentFieldIncomplete,
86    #[error("Info-ZIP Unicode Path Extra Field was incomplete")]
87    InfoZipUnicodePathFieldIncomplete,
88    #[error("Info-ZIP Unicode Path Extra Field contains invalid UTF-8")]
89    InfoZipUnicodePathFieldInvalidUtf8,
90
91    #[error("the end of central directory offset ({0:#x}) did not match the actual offset ({1:#x})")]
92    InvalidEndOfCentralDirectoryOffset(u64, u64),
93    #[error("the central directory size ({expected:#x}) did not match the observed byte span ({actual:#x})")]
94    InvalidCentralDirectorySize { expected: u64, actual: u64 },
95    #[error("the central directory range ({start:#x}..{end:#x}) extends past the end record offset ({boundary:#x})")]
96    InvalidCentralDirectoryRange { start: u64, end: u64, boundary: u64 },
97    #[error("the central directory end ({directory_end:#x}) did not bind to the end record at {end_record:#x}")]
98    InvalidCentralDirectoryBinding { directory_end: u64, end_record: u64 },
99    #[error("the central directory entry count ({entries}) exceeds the available central directory range")]
100    InvalidCentralDirectoryEntryCount { entries: u64 },
101    #[error("the zip64 end of central directory locator was not found")]
102    MissingZip64EndOfCentralDirectoryLocator,
103    #[error("the zip64 end of central directory locator offset ({0:#x}) did not match the actual offset ({1:#x})")]
104    InvalidZip64EndOfCentralDirectoryLocatorOffset(u64, u64),
105    #[error("the zip64 end of central directory record size ({0}) was smaller than the minimum 44 bytes")]
106    InvalidZip64EndOfCentralDirectorySize(u64),
107    #[error(
108        "the {field} in the end of central directory record ({legacy:#x}) did not match the zip64 end record ({zip64:#x})"
109    )]
110    MismatchedZip64EndOfCentralDirectoryField { field: &'static str, legacy: u64, zip64: u64 },
111    #[error(
112        "zip64 extended information field was too long: expected {expected} bytes, but {actual} bytes were provided"
113    )]
114    Zip64ExtendedInformationFieldTooLong { expected: usize, actual: usize },
115}