Refactor to use boxed slices instead of Vec where possible
This commit is contained in:
parent
cdfd36a1cc
commit
161bd87724
6 changed files with 26 additions and 26 deletions
|
@ -81,7 +81,7 @@ impl<R: Read> AesReader<R> {
|
||||||
// derive a key from the password and salt
|
// derive a key from the password and salt
|
||||||
// the length depends on the aes key length
|
// the length depends on the aes key length
|
||||||
let derived_key_len = 2 * key_length + PWD_VERIFY_LENGTH;
|
let derived_key_len = 2 * key_length + PWD_VERIFY_LENGTH;
|
||||||
let mut derived_key: Vec<u8> = 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
|
// use PBKDF2 with HMAC-Sha1 to derive the key
|
||||||
pbkdf2::pbkdf2::<Hmac<Sha1>>(password, &salt, ITERATION_COUNT, &mut derived_key)
|
pbkdf2::pbkdf2::<Hmac<Sha1>>(password, &salt, ITERATION_COUNT, &mut derived_key)
|
||||||
|
|
|
@ -161,14 +161,14 @@ mod tests {
|
||||||
{
|
{
|
||||||
let mut key_stream = AesCtrZipKeyStream::<Aes>::new(key);
|
let mut key_stream = AesCtrZipKeyStream::<Aes>::new(key);
|
||||||
|
|
||||||
let mut plaintext: Vec<u8> = ciphertext.to_vec();
|
let mut plaintext = ciphertext.to_vec().into_boxed_slice();
|
||||||
key_stream.crypt_in_place(plaintext.as_mut_slice());
|
key_stream.crypt_in_place(&mut plaintext);
|
||||||
assert_eq!(plaintext, expected_plaintext.to_vec());
|
assert_eq!(*plaintext, *expected_plaintext);
|
||||||
|
|
||||||
// Round-tripping should yield the ciphertext again.
|
// Round-tripping should yield the ciphertext again.
|
||||||
let mut key_stream = AesCtrZipKeyStream::<Aes>::new(key);
|
let mut key_stream = AesCtrZipKeyStream::<Aes>::new(key);
|
||||||
key_stream.crypt_in_place(&mut plaintext);
|
key_stream.crypt_in_place(&mut plaintext);
|
||||||
assert_eq!(plaintext, ciphertext.to_vec());
|
assert_eq!(*plaintext, *ciphertext);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
12
src/cp437.rs
12
src/cp437.rs
|
@ -23,15 +23,15 @@ impl<'a> FromCp437 for &'a [u8] {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromCp437 for Vec<u8> {
|
impl FromCp437 for Box<[u8]> {
|
||||||
type Target = String;
|
type Target = Box<str>;
|
||||||
|
|
||||||
fn from_cp437(self) -> Self::Target {
|
fn from_cp437(self) -> Self::Target {
|
||||||
if self.iter().all(|c| *c < 0x80) {
|
if self.iter().all(|c| *c < 0x80) {
|
||||||
String::from_utf8(self).unwrap()
|
String::from_utf8(self.into()).unwrap()
|
||||||
} else {
|
} 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;
|
use super::FromCp437;
|
||||||
let data = vec![0xCC, 0xCD, 0xCD, 0xB9];
|
let data = vec![0xCC, 0xCD, 0xCD, 0xB9];
|
||||||
assert!(String::from_utf8(data.clone()).is_err());
|
assert!(String::from_utf8(data.clone()).is_err());
|
||||||
assert_eq!(&data.from_cp437(), "╠══╣");
|
assert_eq!(&*data.from_cp437(), "╠══╣");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
22
src/read.rs
22
src/read.rs
|
@ -44,7 +44,7 @@ pub(crate) mod zip_archive {
|
||||||
/// Extract immutable data from `ZipArchive` to make it cheap to clone
|
/// Extract immutable data from `ZipArchive` to make it cheap to clone
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct Shared {
|
pub(crate) struct Shared {
|
||||||
pub(crate) files: Vec<super::ZipFileData>,
|
pub(crate) files: Box<[super::ZipFileData]>,
|
||||||
pub(crate) names_map: super::HashMap<Box<str>, usize>,
|
pub(crate) names_map: super::HashMap<Box<str>, usize>,
|
||||||
pub(super) offset: u64,
|
pub(super) offset: u64,
|
||||||
pub(super) dir_start: u64,
|
pub(super) dir_start: u64,
|
||||||
|
@ -75,7 +75,7 @@ pub(crate) mod zip_archive {
|
||||||
pub struct ZipArchive<R> {
|
pub struct ZipArchive<R> {
|
||||||
pub(super) reader: R,
|
pub(super) reader: R,
|
||||||
pub(super) shared: Arc<Shared>,
|
pub(super) shared: Arc<Shared>,
|
||||||
pub(super) comment: Arc<Vec<u8>>,
|
pub(super) comment: Arc<[u8]>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -494,7 +494,7 @@ impl<R: Read + Seek> ZipArchive<R> {
|
||||||
unsupported_zip_error("Support for multi-disk files is not implemented")
|
unsupported_zip_error("Support for multi-disk files is not implemented")
|
||||||
} else {
|
} else {
|
||||||
Ok(Shared {
|
Ok(Shared {
|
||||||
files,
|
files: files.into(),
|
||||||
names_map,
|
names_map,
|
||||||
offset: dir_info.archive_offset,
|
offset: dir_info.archive_offset,
|
||||||
dir_start: dir_info.directory_start,
|
dir_start: dir_info.directory_start,
|
||||||
|
@ -779,11 +779,11 @@ fn central_header_to_zip_file_inner<R: Read>(
|
||||||
|
|
||||||
let file_name: Box<str> = match is_utf8 {
|
let file_name: Box<str> = match is_utf8 {
|
||||||
true => String::from_utf8_lossy(&file_name_raw).into(),
|
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<str> = match is_utf8 {
|
||||||
true => String::from_utf8_lossy(&file_comment_raw).into(),
|
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
|
// Construct the result
|
||||||
|
@ -802,7 +802,7 @@ fn central_header_to_zip_file_inner<R: Read>(
|
||||||
compressed_size: compressed_size as u64,
|
compressed_size: compressed_size as u64,
|
||||||
uncompressed_size: uncompressed_size as u64,
|
uncompressed_size: uncompressed_size as u64,
|
||||||
file_name,
|
file_name,
|
||||||
file_name_raw,
|
file_name_raw: file_name_raw.into(),
|
||||||
extra_field: Arc::new(extra_field),
|
extra_field: Arc::new(extra_field),
|
||||||
central_extra_field: Arc::new(vec![]),
|
central_extra_field: Arc::new(vec![]),
|
||||||
file_comment,
|
file_comment,
|
||||||
|
@ -961,7 +961,7 @@ impl<'a> ZipFile<'a> {
|
||||||
note = "by stripping `..`s from the path, the meaning of paths can change.
|
note = "by stripping `..`s from the path, the meaning of paths can change.
|
||||||
`mangled_name` can be used if this behaviour is desirable"
|
`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()
|
self.mangled_name()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -977,7 +977,7 @@ impl<'a> ZipFile<'a> {
|
||||||
/// [`ZipFile::enclosed_name`] is the better option in most scenarios.
|
/// [`ZipFile::enclosed_name`] is the better option in most scenarios.
|
||||||
///
|
///
|
||||||
/// [`ParentDir`]: `Component::ParentDir`
|
/// [`ParentDir`]: `Component::ParentDir`
|
||||||
pub fn mangled_name(&self) -> std::path::PathBuf {
|
pub fn mangled_name(&self) -> PathBuf {
|
||||||
self.data.file_name_sanitized()
|
self.data.file_name_sanitized()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1147,7 +1147,7 @@ pub fn read_zipfile_from_stream<'a, R: Read>(reader: &'a mut R) -> ZipResult<Opt
|
||||||
|
|
||||||
let file_name: Box<str> = match is_utf8 {
|
let file_name: Box<str> = match is_utf8 {
|
||||||
true => String::from_utf8_lossy(&file_name_raw).into(),
|
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 {
|
let mut result = ZipFileData {
|
||||||
|
@ -1162,7 +1162,7 @@ pub fn read_zipfile_from_stream<'a, R: Read>(reader: &'a mut R) -> ZipResult<Opt
|
||||||
compressed_size: compressed_size as u64,
|
compressed_size: compressed_size as u64,
|
||||||
uncompressed_size: uncompressed_size as u64,
|
uncompressed_size: uncompressed_size as u64,
|
||||||
file_name,
|
file_name,
|
||||||
file_name_raw,
|
file_name_raw: file_name_raw.into(),
|
||||||
extra_field: Arc::new(extra_field),
|
extra_field: Arc::new(extra_field),
|
||||||
central_extra_field: Arc::new(vec![]),
|
central_extra_field: Arc::new(vec![]),
|
||||||
file_comment: String::with_capacity(0).into_boxed_str(), // file comment is only available in the central directory
|
file_comment: String::with_capacity(0).into_boxed_str(), // file comment is only available in the central directory
|
||||||
|
|
|
@ -387,7 +387,7 @@ pub struct ZipFileData {
|
||||||
/// Name of the file
|
/// Name of the file
|
||||||
pub file_name: Box<str>,
|
pub file_name: Box<str>,
|
||||||
/// Raw file name. To be used when file_name was incorrectly decoded.
|
/// Raw file name. To be used when file_name was incorrectly decoded.
|
||||||
pub file_name_raw: Vec<u8>,
|
pub file_name_raw: Box<[u8]>,
|
||||||
/// Extra field usually used for storage expansion
|
/// Extra field usually used for storage expansion
|
||||||
pub extra_field: Arc<Vec<u8>>,
|
pub extra_field: Arc<Vec<u8>>,
|
||||||
/// Extra field only written to central directory
|
/// Extra field only written to central directory
|
||||||
|
@ -557,7 +557,7 @@ mod test {
|
||||||
compressed_size: 0,
|
compressed_size: 0,
|
||||||
uncompressed_size: 0,
|
uncompressed_size: 0,
|
||||||
file_name: file_name.clone().into_boxed_str(),
|
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![]),
|
extra_field: Arc::new(vec![]),
|
||||||
central_extra_field: Arc::new(vec![]),
|
central_extra_field: Arc::new(vec![]),
|
||||||
file_comment: String::with_capacity(0).into_boxed_str(),
|
file_comment: String::with_capacity(0).into_boxed_str(),
|
||||||
|
|
|
@ -439,7 +439,7 @@ impl<A: Read + Write + Seek> ZipWriter<A> {
|
||||||
|
|
||||||
Ok(ZipWriter {
|
Ok(ZipWriter {
|
||||||
inner: Storer(MaybeEncrypted::Unencrypted(readwriter)),
|
inner: Storer(MaybeEncrypted::Unencrypted(readwriter)),
|
||||||
files: metadata.files,
|
files: metadata.files.into(),
|
||||||
files_by_name: metadata.names_map,
|
files_by_name: metadata.names_map,
|
||||||
stats: Default::default(),
|
stats: Default::default(),
|
||||||
writing_to_file: false,
|
writing_to_file: false,
|
||||||
|
@ -609,7 +609,7 @@ impl<W: Write + Seek> ZipWriter<W> {
|
||||||
compressed_size: raw_values.compressed_size,
|
compressed_size: raw_values.compressed_size,
|
||||||
uncompressed_size: raw_values.uncompressed_size,
|
uncompressed_size: raw_values.uncompressed_size,
|
||||||
file_name: name.into(),
|
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,
|
extra_field: options.extra_data,
|
||||||
central_extra_field: options.central_extra_data,
|
central_extra_field: options.central_extra_data,
|
||||||
file_comment: String::with_capacity(0).into_boxed_str(),
|
file_comment: String::with_capacity(0).into_boxed_str(),
|
||||||
|
|
Loading…
Add table
Reference in a new issue