use less feature gates if no further dependencies are needed

This commit is contained in:
Lireer 2020-10-14 16:29:08 +02:00
parent 8f352c30f1
commit 75e8f6bab5
4 changed files with 29 additions and 69 deletions

View file

@ -27,7 +27,7 @@ pub enum CompressionMethod {
/// Encrypted using AES. /// Encrypted using AES.
/// The actual compression method has to be taken from the AES extra data field /// The actual compression method has to be taken from the AES extra data field
/// or from `ZipFileData`. /// or from `ZipFileData`.
AES, Aes,
/// Unsupported compression method /// Unsupported compression method
#[deprecated(since = "0.5.7", note = "use the constants instead")] #[deprecated(since = "0.5.7", note = "use the constants instead")]
Unsupported(u16), Unsupported(u16),
@ -89,7 +89,7 @@ impl CompressionMethod {
8 => CompressionMethod::Deflated, 8 => CompressionMethod::Deflated,
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
12 => CompressionMethod::Bzip2, 12 => CompressionMethod::Bzip2,
99 => CompressionMethod::AES, 99 => CompressionMethod::Aes,
v => CompressionMethod::Unsupported(v), v => CompressionMethod::Unsupported(v),
} }
} }
@ -111,7 +111,7 @@ impl CompressionMethod {
CompressionMethod::Deflated => 8, CompressionMethod::Deflated => 8,
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
CompressionMethod::Bzip2 => 12, CompressionMethod::Bzip2 => 12,
CompressionMethod::AES => 99, CompressionMethod::Aes => 99,
CompressionMethod::Unsupported(v) => v, CompressionMethod::Unsupported(v) => v,
} }
} }

View file

@ -10,7 +10,6 @@ pub struct Crc32Reader<R> {
inner: R, inner: R,
hasher: Hasher, hasher: Hasher,
check: u32, check: u32,
#[cfg(feature = "aes-crypto")]
/// Signals if `inner` stores aes encrypted data. /// Signals if `inner` stores aes encrypted data.
/// AE-2 encrypted data doesn't use crc and sets the value to 0. /// AE-2 encrypted data doesn't use crc and sets the value to 0.
ae2_encrypted: bool, ae2_encrypted: bool,
@ -19,16 +18,11 @@ pub struct Crc32Reader<R> {
impl<R> Crc32Reader<R> { impl<R> Crc32Reader<R> {
/// Get a new Crc32Reader which checks the inner reader against checksum. /// Get a new Crc32Reader which checks the inner reader against checksum.
/// The check is disabled if `ae2_encrypted == true`. /// The check is disabled if `ae2_encrypted == true`.
pub(crate) fn new( pub(crate) fn new(inner: R, checksum: u32, ae2_encrypted: bool) -> Crc32Reader<R> {
inner: R,
checksum: u32,
#[cfg(feature = "aes-crypto")] ae2_encrypted: bool,
) -> Crc32Reader<R> {
Crc32Reader { Crc32Reader {
inner, inner,
hasher: Hasher::new(), hasher: Hasher::new(),
check: checksum, check: checksum,
#[cfg(feature = "aes-crypto")]
ae2_encrypted, ae2_encrypted,
} }
} }
@ -44,9 +38,7 @@ impl<R> Crc32Reader<R> {
impl<R: Read> Read for Crc32Reader<R> { impl<R: Read> Read for Crc32Reader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let invalid_check = !buf.is_empty() && !self.check_matches(); let invalid_check = !buf.is_empty() && !self.check_matches() && !self.ae2_encrypted;
#[cfg(feature = "aes-crypto")]
let invalid_check = invalid_check && !self.ae2_encrypted;
let count = match self.inner.read(buf) { let count = match self.inner.read(buf) {
Ok(0) if invalid_check => { Ok(0) if invalid_check => {
@ -70,20 +62,10 @@ mod test {
let data: &[u8] = b""; let data: &[u8] = b"";
let mut buf = [0; 1]; let mut buf = [0; 1];
let mut reader = Crc32Reader::new( let mut reader = Crc32Reader::new(data, 0, false);
data,
0,
#[cfg(feature = "aes-crypto")]
false,
);
assert_eq!(reader.read(&mut buf).unwrap(), 0); assert_eq!(reader.read(&mut buf).unwrap(), 0);
let mut reader = Crc32Reader::new( let mut reader = Crc32Reader::new(data, 1, false);
data,
1,
#[cfg(feature = "aes-crypto")]
false,
);
assert!(reader assert!(reader
.read(&mut buf) .read(&mut buf)
.unwrap_err() .unwrap_err()
@ -96,12 +78,7 @@ mod test {
let data: &[u8] = b"1234"; let data: &[u8] = b"1234";
let mut buf = [0; 1]; let mut buf = [0; 1];
let mut reader = Crc32Reader::new( let mut reader = Crc32Reader::new(data, 0x9be3e0a3, false);
data,
0x9be3e0a3,
#[cfg(feature = "aes-crypto")]
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); 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 data: &[u8] = b"1234";
let mut buf = [0; 5]; let mut buf = [0; 5];
let mut reader = Crc32Reader::new( let mut reader = Crc32Reader::new(data, 0x9be3e0a3, false);
data,
0x9be3e0a3,
#[cfg(feature = "aes-crypto")]
false,
);
assert_eq!(reader.read(&mut buf[..0]).unwrap(), 0); assert_eq!(reader.read(&mut buf[..0]).unwrap(), 0);
assert_eq!(reader.read(&mut buf).unwrap(), 4); assert_eq!(reader.read(&mut buf).unwrap(), 4);
} }

View file

@ -87,6 +87,14 @@ impl<'a> CryptoReader<'a> {
CryptoReader::Aes { reader: r, .. } => r.into_inner(), 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> { enum ZipFileReader<'a> {
@ -224,19 +232,12 @@ fn make_reader<'a>(
crc32: u32, crc32: u32,
reader: CryptoReader<'a>, reader: CryptoReader<'a>,
) -> ZipFileReader<'a> { ) -> ZipFileReader<'a> {
#[cfg(feature = "aes-crypto")] let ae2_encrypted = reader.is_ae2_encrypted();
let ae2_encrypted = matches!(reader, CryptoReader::Aes {
vendor_version: AesVendorVersion::Ae2,
..
});
match compression_method { match compression_method {
CompressionMethod::Stored => ZipFileReader::Stored(Crc32Reader::new( CompressionMethod::Stored => {
reader, ZipFileReader::Stored(Crc32Reader::new(reader, crc32, ae2_encrypted))
crc32, }
#[cfg(feature = "aes-crypto")]
ae2_encrypted,
)),
#[cfg(any( #[cfg(any(
feature = "deflate", feature = "deflate",
feature = "deflate-miniz", feature = "deflate-miniz",
@ -244,22 +245,12 @@ fn make_reader<'a>(
))] ))]
CompressionMethod::Deflated => { CompressionMethod::Deflated => {
let deflate_reader = DeflateDecoder::new(reader); let deflate_reader = DeflateDecoder::new(reader);
ZipFileReader::Deflated(Crc32Reader::new( ZipFileReader::Deflated(Crc32Reader::new(deflate_reader, crc32, ae2_encrypted))
deflate_reader,
crc32,
#[cfg(feature = "aes-crypto")]
ae2_encrypted,
))
} }
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
CompressionMethod::Bzip2 => { CompressionMethod::Bzip2 => {
let bzip2_reader = BzDecoder::new(reader); let bzip2_reader = BzDecoder::new(reader);
ZipFileReader::Bzip2(Crc32Reader::new( ZipFileReader::Bzip2(Crc32Reader::new(bzip2_reader, crc32, ae2_encrypted))
bzip2_reader,
crc32,
#[cfg(feature = "aes-crypto")]
ae2_encrypted,
))
} }
_ => panic!("Compression method not supported"), _ => panic!("Compression method not supported"),
} }
@ -657,14 +648,11 @@ pub(crate) fn central_header_to_zip_file<R: Read + io::Seek>(
Err(e) => return Err(e), 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() {
let aes_enabled = result.compression_method == CompressionMethod::AES; return Err(ZipError::InvalidArchive(
if aes_enabled && result.aes_mode.is_none() { "AES encryption without AES extra data field",
return Err(ZipError::InvalidArchive( ));
"AES encryption without AES extra data field",
));
}
} }
// Account for shifted zip offsets. // Account for shifted zip offsets.

View file

@ -831,7 +831,7 @@ impl<W: Write + io::Seek> GenericZipWriter<W> {
CompressionMethod::Bzip2 => { CompressionMethod::Bzip2 => {
GenericZipWriter::Bzip2(BzEncoder::new(bare, bzip2::Compression::default())) GenericZipWriter::Bzip2(BzEncoder::new(bare, bzip2::Compression::default()))
} }
CompressionMethod::AES => { CompressionMethod::Aes => {
return Err(ZipError::UnsupportedArchive( return Err(ZipError::UnsupportedArchive(
"AES compression is not supported for writing", "AES compression is not supported for writing",
)) ))