From 1bb400dcb258f135a3f92f6242e728f0475325c1 Mon Sep 17 00:00:00 2001 From: igorechek06 Date: Sat, 10 Aug 2024 17:58:14 +0900 Subject: Unify zip errors --- src/zip/datetime.rs | 4 ++-- src/zip/driver.rs | 16 ++++++------- src/zip/error.rs | 68 +++++++++++++++++----------------------------------- src/zip/file/info.rs | 4 ++-- src/zip/file/read.rs | 14 ++++++----- 5 files changed, 42 insertions(+), 64 deletions(-) (limited to 'src') diff --git a/src/zip/datetime.rs b/src/zip/datetime.rs index a4b1b55..e101334 100644 --- a/src/zip/datetime.rs +++ b/src/zip/datetime.rs @@ -18,13 +18,13 @@ impl DosDateTime for DateTime { date as u32 >> 5 & 0xF, date as u32 & 0x1F, ) - .ok_or(ZipError::InvalidDate)? + .ok_or(ZipError::InvalidField("date"))? .and_hms_opt( (time as u32 >> 11) & 0x1F, (time as u32 >> 5) & 0x3F, (time as u32 & 0x1F) * 2, ) - .ok_or(ZipError::InvalidTime)? + .ok_or(ZipError::InvalidField("time"))? .and_local_timezone(tz) .unwrap()) } diff --git a/src/zip/driver.rs b/src/zip/driver.rs index 9dccfd0..81629dd 100644 --- a/src/zip/driver.rs +++ b/src/zip/driver.rs @@ -83,11 +83,11 @@ impl ArchiveRead for Zip { + io.read_vec( (limit as usize) .checked_sub(18) - .ok_or(ZipError::EocdrNotFound)?, + .ok_or(ZipError::StructNotFound("Eocdr"))?, )? .windows(4) .rposition(|v| v == EOCDR_SIGNATURE) - .ok_or(ZipError::EocdrNotFound)? as u64; + .ok_or(ZipError::StructNotFound("Eocdr"))? as u64; // Read eocdr io.seek(SeekFrom::Start(pos + 4))?; @@ -106,7 +106,7 @@ impl ArchiveRead for Zip { io.seek(SeekFrom::Start(eocdr64locator.eocdr64_pointer))?; let buf = io.read_arr::<56>()?; if buf[0..4] != EOCDR64_SIGNATURE { - return Err(ZipError::InvalidEocdr64Signature); + return Err(ZipError::InvalidSignature("Eocdr64")); } let eocdr64: Eocdr64 = deserialize(&buf[4..]).unwrap(); if eocdr64.cd_pointer + eocdr64.cd_size > eocdr64locator.eocdr64_pointer { @@ -139,14 +139,14 @@ impl ArchiveRead for Zip { let buf = cd_reader.read_arr::<46>()?; if buf[..4] != CDR_SIGNATURE { - return Err(ZipError::InvalidCdrSignature); + return Err(ZipError::InvalidSignature("Cdr")); } let cdr: Cdr = deserialize(&buf[4..46]).unwrap(); let bit_flag = BitFlag::new(cdr.bit_flag); let name = cd_reader.read_vec(cdr.name_len as usize)?; let name = if bit_flag.is_utf8() { - String::from_utf8(name).map_err(|_| ZipError::InvalidFileName)? + String::from_utf8(name).map_err(|_| ZipError::InvalidField("file_name"))? } else { String::from_cp437(name) }; @@ -155,7 +155,7 @@ impl ArchiveRead for Zip { let comment = cd_reader.read_vec(cdr.comment_len as usize)?; let comment = if bit_flag.is_utf8() { - String::from_utf8(comment).map_err(|_| ZipError::InvalidFileComment)? + String::from_utf8(comment).map_err(|_| ZipError::InvalidField("file_comment"))? } else { String::from_cp437(comment) }; @@ -216,7 +216,7 @@ impl ArchiveRead for Zip { 0x9901 => { let aes: AesField = deserialize(&data.read_arr::<7>()?).unwrap(); if aes.id != [0x41, 0x45] { - return Err(ZipError::InvalidExtraFields); + return Err(ZipError::InvalidField("extra_fields")); } encryption_method = match aes.strength { 0x01 => EncryptionMethod::Aes128, @@ -232,7 +232,7 @@ impl ArchiveRead for Zip { } if compression_method == 99 { - return Err(ZipError::AesExtraFieldNotFound); + return Err(ZipError::StructNotFound("AesExtensibleData")); } indexes.insert(name.clone(), i); diff --git a/src/zip/error.rs b/src/zip/error.rs index dbb6bb5..fbc2ba1 100644 --- a/src/zip/error.rs +++ b/src/zip/error.rs @@ -8,27 +8,18 @@ pub type ZipResult = Result; pub enum ZipError { Io(IoError), - EocdrNotFound, - InvalidEocdr64Signature, - InvalidFileHeaderSignature, - InvalidCdrSignature, - + // Driver errors + StructNotFound(&'static str), + InvalidSignature(&'static str), + InvalidField(&'static str), + Unsupported(&'static str), Overlapping(&'static str, &'static str), - UnsupportedCompressionMethod(u16), - UnsupportedEncryptionMethod, - InvalidDate, - InvalidTime, - InvalidFileName, - InvalidExtraFields, - AesExtraFieldNotFound, - InvalidFileComment, - + // API errors FileNotFound, WrongPassword, PasswordIsNotSpecified, - CompressedDataIsUnseekable, - EncryptedDataIsUnseekable, + UnseekableFile, } impl From for ZipError { @@ -41,9 +32,10 @@ impl PartialEq for ZipError { fn eq(&self, other: &Self) -> bool { match (self, other) { (Self::Io(l0), Self::Io(r0)) => l0.kind() == r0.kind(), - (Self::UnsupportedCompressionMethod(l0), Self::UnsupportedCompressionMethod(r0)) => { - l0 == r0 - } + (Self::StructNotFound(l0), Self::StructNotFound(r0)) => l0 == r0, + (Self::InvalidSignature(l0), Self::InvalidSignature(r0)) => l0 == r0, + (Self::InvalidField(l0), Self::InvalidField(r0)) => l0 == r0, + (Self::Overlapping(l0, l1), Self::Overlapping(r0, r1)) => l0 == r0 && l1 == r1, _ => core::mem::discriminant(self) == core::mem::discriminant(other), } } @@ -56,42 +48,26 @@ impl Display for ZipError { match self { Self::Io(error) => write!(f, "{}", error), - Self::EocdrNotFound => write!(f, "End of central directory record not found"), - Self::InvalidEocdr64Signature => { - write!( - f, - "Invalid signature of zip64 end of central directory record" - ) + Self::StructNotFound(struct_name) => { + write!(f, "Struct '{}' not found", struct_name) } - Self::InvalidFileHeaderSignature => { - write!(f, "Invalid file header signature") + Self::InvalidSignature(struct_name) => { + write!(f, "Invalid signature of struct '{}'", struct_name) } - Self::InvalidCdrSignature => { - write!(f, "Invalid signature of central directory record") + Self::InvalidField(field_name) => { + write!(f, "Field '{}' has invalid data", field_name) } - - Self::Overlapping(struct1, struct2) => { - write!(f, "`{}` overlapt `{}`", struct1, struct2) - } - - Self::UnsupportedCompressionMethod(id) => { - writeln!(f, "Unsupported compression method `{}`", id) + Self::Unsupported(data_type) => { + writeln!(f, "Unsupported {}", data_type) } - Self::UnsupportedEncryptionMethod => { - writeln!(f, "Unsupported encryption method") + Self::Overlapping(struct_name1, struct_name2) => { + write!(f, "`{}` overlap `{}`", struct_name1, struct_name2) } - Self::InvalidDate => write!(f, "Invalid date"), - Self::InvalidTime => write!(f, "Invalid time"), - Self::InvalidFileName => write!(f, "Invalid file name"), - Self::InvalidExtraFields => write!(f, "Invalid extra fields"), - Self::AesExtraFieldNotFound => write!(f, "Aes extra field not found"), - Self::InvalidFileComment => write!(f, "Invalid file comment"), Self::FileNotFound => write!(f, "File not found"), Self::WrongPassword => write!(f, "Wrong password"), Self::PasswordIsNotSpecified => write!(f, "Password is not specified"), - Self::CompressedDataIsUnseekable => write!(f, "Compressed data is unseekable"), - Self::EncryptedDataIsUnseekable => write!(f, "Encrypted data is unseekable"), + Self::UnseekableFile => write!(f, "File is unseekable"), } } } diff --git a/src/zip/file/info.rs b/src/zip/file/info.rs index 38ea984..93b9f43 100644 --- a/src/zip/file/info.rs +++ b/src/zip/file/info.rs @@ -10,7 +10,7 @@ pub enum CompressionMethod { Lzma, Zstd, Xz, - Unsupported(u16), + Unsupported, } impl CompressionMethod { @@ -23,7 +23,7 @@ impl CompressionMethod { 14 => Self::Lzma, 93 => Self::Zstd, 95 => Self::Xz, - _ => Self::Unsupported(id), + _ => Self::Unsupported, } } } diff --git a/src/zip/file/read.rs b/src/zip/file/read.rs index d25655e..567fb75 100644 --- a/src/zip/file/read.rs +++ b/src/zip/file/read.rs @@ -116,7 +116,9 @@ impl Encryption> { Aes256::new(key.into()), )?) } - EncryptionMethod::Unsupported => return Err(ZipError::UnsupportedEncryptionMethod), + EncryptionMethod::Unsupported => { + return Err(ZipError::Unsupported("encryption method")) + } }) } } @@ -141,7 +143,7 @@ impl Seek for Encryption { Self::None(io) => io.seek(pos), _ => Err(IoError::new( IoErrorKind::Unsupported, - ZipError::EncryptedDataIsUnseekable, + ZipError::UnseekableFile, )), } } @@ -182,8 +184,8 @@ impl Compression { } CompressionMethod::Zstd => Self::Zstd(ZstdDecoder::new(io)?), CompressionMethod::Xz => Self::Xz(XzDecoder::new(io)), - CompressionMethod::Unsupported(id) => { - return Err(ZipError::UnsupportedCompressionMethod(id)) + CompressionMethod::Unsupported => { + return Err(ZipError::Unsupported("compression method")); } }) } @@ -209,7 +211,7 @@ impl Seek for Compression { Compression::Store(io) => io.seek(pos), _ => Err(IoError::new( IoErrorKind::Unsupported, - ZipError::CompressedDataIsUnseekable, + ZipError::UnseekableFile, )), } } @@ -235,7 +237,7 @@ impl<'d, Io: Read + Seek> ZipFileReader<'d, Io> { let buf = io.read_arr::<30>()?; if buf[..4] != FILE_HEADER_SIGNATURE { - return Err(ZipError::InvalidFileHeaderSignature); + return Err(ZipError::InvalidSignature("FileHeader")); } let cursor = io.seek(SeekFrom::Start( -- cgit v1.2.3