From 161bd87724d7ee802e6b892c03b2f9f026c494a6 Mon Sep 17 00:00:00 2001 From: Chris Hennick Date: Mon, 8 Apr 2024 12:09:23 -0700 Subject: [PATCH] Refactor to use boxed slices instead of Vec where possible --- src/aes.rs | 2 +- src/aes_ctr.rs | 8 ++++---- src/cp437.rs | 12 ++++++------ src/read.rs | 22 +++++++++++----------- src/types.rs | 4 ++-- src/write.rs | 4 ++-- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/aes.rs b/src/aes.rs index 55ea8bf3..985a3bbc 100644 --- a/src/aes.rs +++ b/src/aes.rs @@ -81,7 +81,7 @@ impl AesReader { // derive a key from the password and salt // the length depends on the aes key length let derived_key_len = 2 * key_length + PWD_VERIFY_LENGTH; - let mut derived_key: Vec = vec![0; derived_key_len]; + let mut derived_key: Box<[u8]> = vec![0; derived_key_len].into_boxed_slice(); // use PBKDF2 with HMAC-Sha1 to derive the key pbkdf2::pbkdf2::>(password, &salt, ITERATION_COUNT, &mut derived_key) diff --git a/src/aes_ctr.rs b/src/aes_ctr.rs index a1636b4b..d9f04b64 100644 --- a/src/aes_ctr.rs +++ b/src/aes_ctr.rs @@ -161,14 +161,14 @@ mod tests { { let mut key_stream = AesCtrZipKeyStream::::new(key); - let mut plaintext: Vec = ciphertext.to_vec(); - key_stream.crypt_in_place(plaintext.as_mut_slice()); - assert_eq!(plaintext, expected_plaintext.to_vec()); + let mut plaintext = ciphertext.to_vec().into_boxed_slice(); + key_stream.crypt_in_place(&mut plaintext); + assert_eq!(*plaintext, *expected_plaintext); // Round-tripping should yield the ciphertext again. let mut key_stream = AesCtrZipKeyStream::::new(key); key_stream.crypt_in_place(&mut plaintext); - assert_eq!(plaintext, ciphertext.to_vec()); + assert_eq!(*plaintext, *ciphertext); } #[test] diff --git a/src/cp437.rs b/src/cp437.rs index 25e26c7b..100e8db5 100644 --- a/src/cp437.rs +++ b/src/cp437.rs @@ -23,15 +23,15 @@ impl<'a> FromCp437 for &'a [u8] { } } -impl FromCp437 for Vec { - type Target = String; +impl FromCp437 for Box<[u8]> { + type Target = Box; fn from_cp437(self) -> Self::Target { if self.iter().all(|c| *c < 0x80) { - String::from_utf8(self).unwrap() + String::from_utf8(self.into()).unwrap() } else { - self.into_iter().map(to_char).collect() - } + self.iter().copied().map(to_char).collect() + }.into_boxed_str() } } @@ -201,6 +201,6 @@ mod test { use super::FromCp437; let data = vec![0xCC, 0xCD, 0xCD, 0xB9]; assert!(String::from_utf8(data.clone()).is_err()); - assert_eq!(&data.from_cp437(), "╠══╣"); + assert_eq!(&*data.from_cp437(), "╠══╣"); } } diff --git a/src/read.rs b/src/read.rs index 9ee33b41..c9291ab8 100644 --- a/src/read.rs +++ b/src/read.rs @@ -44,7 +44,7 @@ pub(crate) mod zip_archive { /// Extract immutable data from `ZipArchive` to make it cheap to clone #[derive(Debug)] pub(crate) struct Shared { - pub(crate) files: Vec, + pub(crate) files: Box<[super::ZipFileData]>, pub(crate) names_map: super::HashMap, usize>, pub(super) offset: u64, pub(super) dir_start: u64, @@ -75,7 +75,7 @@ pub(crate) mod zip_archive { pub struct ZipArchive { pub(super) reader: R, pub(super) shared: Arc, - pub(super) comment: Arc>, + pub(super) comment: Arc<[u8]>, } } @@ -494,7 +494,7 @@ impl ZipArchive { unsupported_zip_error("Support for multi-disk files is not implemented") } else { Ok(Shared { - files, + files: files.into(), names_map, offset: dir_info.archive_offset, dir_start: dir_info.directory_start, @@ -779,11 +779,11 @@ fn central_header_to_zip_file_inner( let file_name: Box = match is_utf8 { true => String::from_utf8_lossy(&file_name_raw).into(), - false => file_name_raw.clone().from_cp437().into_boxed_str(), + false => file_name_raw.from_cp437().into(), }; - let file_comment = match is_utf8 { + let file_comment: Box = match is_utf8 { true => String::from_utf8_lossy(&file_comment_raw).into(), - false => file_comment_raw.from_cp437().into_boxed_str(), + false => file_comment_raw.from_cp437().into(), }; // Construct the result @@ -802,7 +802,7 @@ fn central_header_to_zip_file_inner( compressed_size: compressed_size as u64, uncompressed_size: uncompressed_size as u64, file_name, - file_name_raw, + file_name_raw: file_name_raw.into(), extra_field: Arc::new(extra_field), central_extra_field: Arc::new(vec![]), file_comment, @@ -961,7 +961,7 @@ impl<'a> ZipFile<'a> { note = "by stripping `..`s from the path, the meaning of paths can change. `mangled_name` can be used if this behaviour is desirable" )] - pub fn sanitized_name(&self) -> std::path::PathBuf { + pub fn sanitized_name(&self) -> PathBuf { self.mangled_name() } @@ -977,7 +977,7 @@ impl<'a> ZipFile<'a> { /// [`ZipFile::enclosed_name`] is the better option in most scenarios. /// /// [`ParentDir`]: `Component::ParentDir` - pub fn mangled_name(&self) -> std::path::PathBuf { + pub fn mangled_name(&self) -> PathBuf { self.data.file_name_sanitized() } @@ -1147,7 +1147,7 @@ pub fn read_zipfile_from_stream<'a, R: Read>(reader: &'a mut R) -> ZipResult = match is_utf8 { true => String::from_utf8_lossy(&file_name_raw).into(), - false => file_name_raw.clone().from_cp437().into_boxed_str(), + false => file_name_raw.clone().from_cp437().into(), }; let mut result = ZipFileData { @@ -1162,7 +1162,7 @@ pub fn read_zipfile_from_stream<'a, R: Read>(reader: &'a mut R) -> ZipResult, /// Raw file name. To be used when file_name was incorrectly decoded. - pub file_name_raw: Vec, + pub file_name_raw: Box<[u8]>, /// Extra field usually used for storage expansion pub extra_field: Arc>, /// Extra field only written to central directory @@ -557,7 +557,7 @@ mod test { compressed_size: 0, uncompressed_size: 0, file_name: file_name.clone().into_boxed_str(), - file_name_raw: file_name.into_bytes(), + file_name_raw: file_name.into_bytes().into_boxed_slice(), extra_field: Arc::new(vec![]), central_extra_field: Arc::new(vec![]), file_comment: String::with_capacity(0).into_boxed_str(), diff --git a/src/write.rs b/src/write.rs index 19e5acb7..82305e4a 100644 --- a/src/write.rs +++ b/src/write.rs @@ -439,7 +439,7 @@ impl ZipWriter { Ok(ZipWriter { inner: Storer(MaybeEncrypted::Unencrypted(readwriter)), - files: metadata.files, + files: metadata.files.into(), files_by_name: metadata.names_map, stats: Default::default(), writing_to_file: false, @@ -609,7 +609,7 @@ impl ZipWriter { compressed_size: raw_values.compressed_size, uncompressed_size: raw_values.uncompressed_size, file_name: name.into(), - file_name_raw: Vec::new(), // Never used for saving + file_name_raw: vec![].into_boxed_slice(), // Never used for saving extra_field: options.extra_data, central_extra_field: options.central_extra_data, file_comment: String::with_capacity(0).into_boxed_str(),