use less feature gates if no further dependencies are needed
This commit is contained in:
parent
8f352c30f1
commit
75e8f6bab5
4 changed files with 29 additions and 69 deletions
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
40
src/crc32.rs
40
src/crc32.rs
|
@ -10,7 +10,6 @@ pub struct Crc32Reader<R> {
|
|||
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<R> {
|
|||
impl<R> Crc32Reader<R> {
|
||||
/// 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<R> {
|
||||
pub(crate) fn new(inner: R, checksum: u32, ae2_encrypted: bool) -> Crc32Reader<R> {
|
||||
Crc32Reader {
|
||||
inner,
|
||||
hasher: Hasher::new(),
|
||||
check: checksum,
|
||||
#[cfg(feature = "aes-crypto")]
|
||||
ae2_encrypted,
|
||||
}
|
||||
}
|
||||
|
@ -44,9 +38,7 @@ impl<R> Crc32Reader<R> {
|
|||
|
||||
impl<R: Read> Read for Crc32Reader<R> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
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);
|
||||
}
|
||||
|
|
50
src/read.rs
50
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<R: Read + io::Seek>(
|
|||
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.
|
||||
|
|
|
@ -831,7 +831,7 @@ impl<W: Write + io::Seek> GenericZipWriter<W> {
|
|||
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",
|
||||
))
|
||||
|
|
Loading…
Add table
Reference in a new issue