From 75e8f6bab5a6525014f6f52c6eb608ab46de48af Mon Sep 17 00:00:00 2001 From: Lireer Date: Wed, 14 Oct 2020 16:29:08 +0200 Subject: [PATCH] use less feature gates if no further dependencies are needed --- src/compression.rs | 6 +++--- src/crc32.rs | 40 ++++++------------------------------- src/read.rs | 50 ++++++++++++++++++---------------------------- src/write.rs | 2 +- 4 files changed, 29 insertions(+), 69 deletions(-) diff --git a/src/compression.rs b/src/compression.rs index 84e69d15..83e0b45b 100644 --- a/src/compression.rs +++ b/src/compression.rs @@ -27,7 +27,7 @@ pub enum CompressionMethod { /// Encrypted using AES. /// The actual compression method has to be taken from the AES extra data field /// or from `ZipFileData`. - AES, + Aes, /// Unsupported compression method #[deprecated(since = "0.5.7", note = "use the constants instead")] Unsupported(u16), @@ -89,7 +89,7 @@ impl CompressionMethod { 8 => CompressionMethod::Deflated, #[cfg(feature = "bzip2")] 12 => CompressionMethod::Bzip2, - 99 => CompressionMethod::AES, + 99 => CompressionMethod::Aes, v => CompressionMethod::Unsupported(v), } } @@ -111,7 +111,7 @@ impl CompressionMethod { CompressionMethod::Deflated => 8, #[cfg(feature = "bzip2")] CompressionMethod::Bzip2 => 12, - CompressionMethod::AES => 99, + CompressionMethod::Aes => 99, CompressionMethod::Unsupported(v) => v, } } diff --git a/src/crc32.rs b/src/crc32.rs index 22c262a6..ebace898 100644 --- a/src/crc32.rs +++ b/src/crc32.rs @@ -10,7 +10,6 @@ pub struct Crc32Reader { inner: R, hasher: Hasher, check: u32, - #[cfg(feature = "aes-crypto")] /// Signals if `inner` stores aes encrypted data. /// AE-2 encrypted data doesn't use crc and sets the value to 0. ae2_encrypted: bool, @@ -19,16 +18,11 @@ pub struct Crc32Reader { impl Crc32Reader { /// Get a new Crc32Reader which checks the inner reader against checksum. /// The check is disabled if `ae2_encrypted == true`. - pub(crate) fn new( - inner: R, - checksum: u32, - #[cfg(feature = "aes-crypto")] ae2_encrypted: bool, - ) -> Crc32Reader { + pub(crate) fn new(inner: R, checksum: u32, ae2_encrypted: bool) -> Crc32Reader { Crc32Reader { inner, hasher: Hasher::new(), check: checksum, - #[cfg(feature = "aes-crypto")] ae2_encrypted, } } @@ -44,9 +38,7 @@ impl Crc32Reader { impl Read for Crc32Reader { fn read(&mut self, buf: &mut [u8]) -> io::Result { - let invalid_check = !buf.is_empty() && !self.check_matches(); - #[cfg(feature = "aes-crypto")] - let invalid_check = invalid_check && !self.ae2_encrypted; + let invalid_check = !buf.is_empty() && !self.check_matches() && !self.ae2_encrypted; let count = match self.inner.read(buf) { Ok(0) if invalid_check => { @@ -70,20 +62,10 @@ mod test { let data: &[u8] = b""; let mut buf = [0; 1]; - let mut reader = Crc32Reader::new( - data, - 0, - #[cfg(feature = "aes-crypto")] - false, - ); + let mut reader = Crc32Reader::new(data, 0, false); assert_eq!(reader.read(&mut buf).unwrap(), 0); - let mut reader = Crc32Reader::new( - data, - 1, - #[cfg(feature = "aes-crypto")] - false, - ); + let mut reader = Crc32Reader::new(data, 1, false); assert!(reader .read(&mut buf) .unwrap_err() @@ -96,12 +78,7 @@ mod test { let data: &[u8] = b"1234"; let mut buf = [0; 1]; - let mut reader = Crc32Reader::new( - data, - 0x9be3e0a3, - #[cfg(feature = "aes-crypto")] - false, - ); + let mut reader = Crc32Reader::new(data, 0x9be3e0a3, false); assert_eq!(reader.read(&mut buf).unwrap(), 1); assert_eq!(reader.read(&mut buf).unwrap(), 1); assert_eq!(reader.read(&mut buf).unwrap(), 1); @@ -116,12 +93,7 @@ mod test { let data: &[u8] = b"1234"; let mut buf = [0; 5]; - let mut reader = Crc32Reader::new( - data, - 0x9be3e0a3, - #[cfg(feature = "aes-crypto")] - false, - ); + let mut reader = Crc32Reader::new(data, 0x9be3e0a3, false); assert_eq!(reader.read(&mut buf[..0]).unwrap(), 0); assert_eq!(reader.read(&mut buf).unwrap(), 4); } diff --git a/src/read.rs b/src/read.rs index 80b6e7d7..23affbec 100644 --- a/src/read.rs +++ b/src/read.rs @@ -87,6 +87,14 @@ impl<'a> CryptoReader<'a> { CryptoReader::Aes { reader: r, .. } => r.into_inner(), } } + + /// Returns `true` if the data is encrypted using AE2. + pub fn is_ae2_encrypted(&self) -> bool { + #[cfg(feature = "aes-crypto")] + return matches!(self, CryptoReader::Aes { vendor_version: AesVendorVersion::Ae2, .. }); + #[cfg(not(feature = "aes-crypto"))] + false + } } enum ZipFileReader<'a> { @@ -224,19 +232,12 @@ fn make_reader<'a>( crc32: u32, reader: CryptoReader<'a>, ) -> ZipFileReader<'a> { - #[cfg(feature = "aes-crypto")] - let ae2_encrypted = matches!(reader, CryptoReader::Aes { - vendor_version: AesVendorVersion::Ae2, - .. - }); + let ae2_encrypted = reader.is_ae2_encrypted(); match compression_method { - CompressionMethod::Stored => ZipFileReader::Stored(Crc32Reader::new( - reader, - crc32, - #[cfg(feature = "aes-crypto")] - ae2_encrypted, - )), + CompressionMethod::Stored => { + ZipFileReader::Stored(Crc32Reader::new(reader, crc32, ae2_encrypted)) + } #[cfg(any( feature = "deflate", feature = "deflate-miniz", @@ -244,22 +245,12 @@ fn make_reader<'a>( ))] CompressionMethod::Deflated => { let deflate_reader = DeflateDecoder::new(reader); - ZipFileReader::Deflated(Crc32Reader::new( - deflate_reader, - crc32, - #[cfg(feature = "aes-crypto")] - ae2_encrypted, - )) + ZipFileReader::Deflated(Crc32Reader::new(deflate_reader, crc32, ae2_encrypted)) } #[cfg(feature = "bzip2")] CompressionMethod::Bzip2 => { let bzip2_reader = BzDecoder::new(reader); - ZipFileReader::Bzip2(Crc32Reader::new( - bzip2_reader, - crc32, - #[cfg(feature = "aes-crypto")] - ae2_encrypted, - )) + ZipFileReader::Bzip2(Crc32Reader::new(bzip2_reader, crc32, ae2_encrypted)) } _ => panic!("Compression method not supported"), } @@ -657,14 +648,11 @@ pub(crate) fn central_header_to_zip_file( Err(e) => return Err(e), } - #[cfg(feature = "aes-crypto")] - { - let aes_enabled = result.compression_method == CompressionMethod::AES; - if aes_enabled && result.aes_mode.is_none() { - return Err(ZipError::InvalidArchive( - "AES encryption without AES extra data field", - )); - } + let aes_enabled = result.compression_method == CompressionMethod::Aes; + if aes_enabled && result.aes_mode.is_none() { + return Err(ZipError::InvalidArchive( + "AES encryption without AES extra data field", + )); } // Account for shifted zip offsets. diff --git a/src/write.rs b/src/write.rs index 05236505..1c912966 100644 --- a/src/write.rs +++ b/src/write.rs @@ -831,7 +831,7 @@ impl GenericZipWriter { CompressionMethod::Bzip2 => { GenericZipWriter::Bzip2(BzEncoder::new(bare, bzip2::Compression::default())) } - CompressionMethod::AES => { + CompressionMethod::Aes => { return Err(ZipError::UnsupportedArchive( "AES compression is not supported for writing", ))