Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

fix: clippy lints and formatting (rustfmt) #393

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/aes_ctr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ mod tests {

/// Checks whether `crypt_in_place` produces the correct plaintext after one use and yields the
/// cipertext again after applying it again.
fn roundtrip<Aes>(key: &[u8], ciphertext: &mut [u8], expected_plaintext: &[u8])
fn roundtrip<Aes>(key: &[u8], ciphertext: &[u8], expected_plaintext: &[u8])
where
Aes: AesKind,
Aes::Cipher: KeyInit + BlockEncrypt,
Expand Down Expand Up @@ -183,57 +183,57 @@ mod tests {
// `7z a -phelloworld -mem=AES256 -mx=0 aes256_40byte.zip 40byte_data.txt`
#[test]
fn crypt_aes_256_0_byte() {
let mut ciphertext = [];
let ciphertext = [];
let expected_plaintext = &[];
let key = [
0x0b, 0xec, 0x2e, 0xf2, 0x46, 0xf0, 0x7e, 0x35, 0x16, 0x54, 0xe0, 0x98, 0x10, 0xb3,
0x18, 0x55, 0x24, 0xa3, 0x9e, 0x0e, 0x40, 0xe7, 0x92, 0xad, 0xb2, 0x8a, 0x48, 0xf4,
0x5c, 0xd0, 0xc0, 0x54,
];

roundtrip::<Aes256>(&key, &mut ciphertext, expected_plaintext);
roundtrip::<Aes256>(&key, &ciphertext, expected_plaintext);
}

#[test]
fn crypt_aes_128_5_byte() {
let mut ciphertext = [0x98, 0xa9, 0x8c, 0x26, 0x0e];
let ciphertext = [0x98, 0xa9, 0x8c, 0x26, 0x0e];
let expected_plaintext = b"asdf\n";
let key = [
0xe0, 0x25, 0x7b, 0x57, 0x97, 0x6a, 0xa4, 0x23, 0xab, 0x94, 0xaa, 0x44, 0xfd, 0x47,
0x4f, 0xa5,
];

roundtrip::<Aes128>(&key, &mut ciphertext, expected_plaintext);
roundtrip::<Aes128>(&key, &ciphertext, expected_plaintext);
}

#[test]
fn crypt_aes_192_5_byte() {
let mut ciphertext = [0x36, 0x55, 0x5c, 0x61, 0x3c];
let ciphertext = [0x36, 0x55, 0x5c, 0x61, 0x3c];
let expected_plaintext = b"asdf\n";
let key = [
0xe4, 0x4a, 0x88, 0x52, 0x8f, 0xf7, 0x0b, 0x81, 0x7b, 0x75, 0xf1, 0x74, 0x21, 0x37,
0x8c, 0x90, 0xad, 0xbe, 0x4a, 0x65, 0xa8, 0x96, 0x0e, 0xcc,
];

roundtrip::<Aes192>(&key, &mut ciphertext, expected_plaintext);
roundtrip::<Aes192>(&key, &ciphertext, expected_plaintext);
}

#[test]
fn crypt_aes_256_5_byte() {
let mut ciphertext = [0xc2, 0x47, 0xc0, 0xdc, 0x56];
let ciphertext = [0xc2, 0x47, 0xc0, 0xdc, 0x56];
let expected_plaintext = b"asdf\n";
let key = [
0x79, 0x5e, 0x17, 0xf2, 0xc6, 0x3d, 0x28, 0x9b, 0x4b, 0x4b, 0xbb, 0xa9, 0xba, 0xc9,
0xa5, 0xee, 0x3a, 0x4f, 0x0f, 0x4b, 0x29, 0xbd, 0xe9, 0xb8, 0x41, 0x9c, 0x41, 0xa5,
0x15, 0xb2, 0x86, 0xab,
];

roundtrip::<Aes256>(&key, &mut ciphertext, expected_plaintext);
roundtrip::<Aes256>(&key, &ciphertext, expected_plaintext);
}

#[test]
fn crypt_aes_128_40_byte() {
let mut ciphertext = [
let ciphertext = [
0xcf, 0x72, 0x6b, 0xa1, 0xb2, 0x0f, 0xdf, 0xaa, 0x10, 0xad, 0x9c, 0x7f, 0x6d, 0x1c,
0x8d, 0xb5, 0x16, 0x7e, 0xbb, 0x11, 0x69, 0x52, 0x8c, 0x89, 0x80, 0x32, 0xaa, 0x76,
0xa6, 0x18, 0x31, 0x98, 0xee, 0xdd, 0x22, 0x68, 0xb7, 0xe6, 0x77, 0xd2,
Expand All @@ -244,12 +244,12 @@ mod tests {
0x81, 0xb6,
];

roundtrip::<Aes128>(&key, &mut ciphertext, expected_plaintext);
roundtrip::<Aes128>(&key, &ciphertext, expected_plaintext);
}

#[test]
fn crypt_aes_192_40_byte() {
let mut ciphertext = [
let ciphertext = [
0xa6, 0xfc, 0x52, 0x79, 0x2c, 0x6c, 0xfe, 0x68, 0xb1, 0xa8, 0xb3, 0x07, 0x52, 0x8b,
0x82, 0xa6, 0x87, 0x9c, 0x72, 0x42, 0x3a, 0xf8, 0xc6, 0xa9, 0xc9, 0xfb, 0x61, 0x19,
0x37, 0xb9, 0x56, 0x62, 0xf4, 0xfc, 0x5e, 0x7a, 0xdd, 0x55, 0x0a, 0x48,
Expand All @@ -260,12 +260,12 @@ mod tests {
0xfe, 0xae, 0x1b, 0xba, 0x01, 0x97, 0x97, 0x79, 0xbb, 0xa6,
];

roundtrip::<Aes192>(&key, &mut ciphertext, expected_plaintext);
roundtrip::<Aes192>(&key, &ciphertext, expected_plaintext);
}

#[test]
fn crypt_aes_256_40_byte() {
let mut ciphertext = [
let ciphertext = [
0xa9, 0x99, 0xbd, 0xea, 0x82, 0x9b, 0x8f, 0x2f, 0xb7, 0x52, 0x2f, 0x6b, 0xd8, 0xf6,
0xab, 0x0e, 0x24, 0x51, 0x9e, 0x18, 0x0f, 0xc0, 0x8f, 0x54, 0x15, 0x80, 0xae, 0xbc,
0xa0, 0x5c, 0x8a, 0x11, 0x8d, 0x14, 0x7e, 0xc5, 0xb4, 0xae, 0xd3, 0x37,
Expand All @@ -277,6 +277,6 @@ mod tests {
0xc2, 0x07, 0x36, 0xb6,
];

roundtrip::<Aes256>(&key, &mut ciphertext, expected_plaintext);
roundtrip::<Aes256>(&key, &ciphertext, expected_plaintext);
}
}
5 changes: 2 additions & 3 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,7 @@ impl<'a> ZipFile<'a> {
pub fn is_dir(&self) -> bool {
self.name()
.chars()
.rev()
.next()
.next_back()
.map_or(false, |c| c == '/' || c == '\\')
}

Expand Down Expand Up @@ -991,7 +990,7 @@ impl<'a> Drop for ZipFile<'a> {
// Get the inner `Take` reader so all decryption, decompression and CRC calculation is skipped.
let mut reader: std::io::Take<&mut dyn std::io::Read> = match &mut self.reader {
ZipFileReader::NoReader => {
let innerreader = ::std::mem::replace(&mut self.crypto_reader, None);
let innerreader = self.crypto_reader.take();
innerreader.expect("Invalid reader state").into_inner()
}
reader => {
Expand Down
3 changes: 1 addition & 2 deletions src/read/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ impl ZipStreamFileMetadata {
pub fn is_dir(&self) -> bool {
self.name()
.chars()
.rev()
.next()
.next_back()
.map_or(false, |c| c == '/' || c == '\\')
}

Expand Down
4 changes: 2 additions & 2 deletions src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod write {
/// Unstable methods for [`FileOptions`].
pub trait FileOptionsExt {
/// Write the file with the given password using the deprecated ZipCrypto algorithm.
///
///
/// This is not recommended for new archives, as ZipCrypto is not secure.
fn with_deprecated_encryption(self, password: &[u8]) -> Self;
}
Expand All @@ -17,4 +17,4 @@ pub mod write {
self.with_deprecated_encryption(password)
}
}
}
}
13 changes: 9 additions & 4 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,12 @@ impl<W: Write + io::Seek> ZipWriter<W> {
self.files.push(file);
}
if let Some(keys) = options.encrypt_with {
let mut zipwriter = crate::zipcrypto::ZipCryptoWriter { writer: core::mem::replace(&mut self.inner, GenericZipWriter::Closed).unwrap(), buffer: vec![], keys };
let mut crypto_header = [0u8; 12];
let mut zipwriter = crate::zipcrypto::ZipCryptoWriter {
writer: core::mem::replace(&mut self.inner, GenericZipWriter::Closed).unwrap(),
buffer: vec![],
keys,
};
let crypto_header = [0u8; 12];

zipwriter.write_all(&crypto_header)?;
self.inner = GenericZipWriter::Storer(MaybeEncrypted::Encrypted(zipwriter));
Expand All @@ -428,10 +432,11 @@ impl<W: Write + io::Seek> ZipWriter<W> {
match core::mem::replace(&mut self.inner, GenericZipWriter::Closed) {
GenericZipWriter::Storer(MaybeEncrypted::Encrypted(writer)) => {
let crc32 = self.stats.hasher.clone().finalize();
self.inner = GenericZipWriter::Storer(MaybeEncrypted::Unencrypted(writer.finish(crc32)?))
self.inner =
GenericZipWriter::Storer(MaybeEncrypted::Unencrypted(writer.finish(crc32)?))
}
GenericZipWriter::Storer(w) => self.inner = GenericZipWriter::Storer(w),
_ => unreachable!()
_ => unreachable!(),
}
let writer = self.inner.get_plain();

Expand Down
10 changes: 7 additions & 3 deletions tests/zip_crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ use std::io::Read;

#[test]
fn encrypting_file() {
use zip::unstable::write::FileOptionsExt;
use std::io::{Read, Write};
use zip::unstable::write::FileOptionsExt;
let mut buf = vec![0; 2048];
let mut archive = zip::write::ZipWriter::new(std::io::Cursor::new(&mut buf));
archive.start_file("name", zip::write::FileOptions::default().with_deprecated_encryption(b"password")).unwrap();
archive
.start_file(
"name",
zip::write::FileOptions::default().with_deprecated_encryption(b"password"),
)
.unwrap();
archive.write_all(b"test").unwrap();
archive.finish().unwrap();
drop(archive);
Expand All @@ -35,7 +40,6 @@ fn encrypting_file() {
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
assert_eq!(buf, b"test");

}
#[test]
fn encrypted_file() {
Expand Down
Loading