perf: Use boxed slice for archive comment, since it can't be concatenated
This commit is contained in:
parent
d996593016
commit
bd473ef75b
4 changed files with 21 additions and 22 deletions
|
@ -21,8 +21,7 @@ pub enum BasicFileOperation<'k> {
|
||||||
ShallowCopy(Box<FileOperation<'k>>),
|
ShallowCopy(Box<FileOperation<'k>>),
|
||||||
DeepCopy(Box<FileOperation<'k>>),
|
DeepCopy(Box<FileOperation<'k>>),
|
||||||
MergeWithOtherFile {
|
MergeWithOtherFile {
|
||||||
first: Box<FileOperation<'k>>,
|
operations: Box<[(FileOperation<'k>, bool)]>
|
||||||
rest: Box<[FileOperation<'k>]>
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,8 +42,8 @@ pub struct FileOperation<'k> {
|
||||||
|
|
||||||
#[derive(Arbitrary, Clone, Debug)]
|
#[derive(Arbitrary, Clone, Debug)]
|
||||||
pub struct FuzzTestCase<'k> {
|
pub struct FuzzTestCase<'k> {
|
||||||
comment: Vec<u8>,
|
comment: Box<[u8]>,
|
||||||
operations: Vec<(FileOperation<'k>, bool)>,
|
operations: Box<[(FileOperation<'k>, bool)]>,
|
||||||
flush_on_finish_file: bool,
|
flush_on_finish_file: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,14 +88,13 @@ where
|
||||||
do_operation(writer, &base, false, flush_on_finish_file)?;
|
do_operation(writer, &base, false, flush_on_finish_file)?;
|
||||||
writer.deep_copy_file_from_path(&base.path, &path)?;
|
writer.deep_copy_file_from_path(&base.path, &path)?;
|
||||||
}
|
}
|
||||||
BasicFileOperation::MergeWithOtherFile { first, rest } => {
|
BasicFileOperation::MergeWithOtherFile { operations } => {
|
||||||
let mut other_writer = zip::ZipWriter::new(Cursor::new(Vec::new()));
|
let mut other_writer = zip::ZipWriter::new(Cursor::new(Vec::new()));
|
||||||
let _ = do_operation(&mut other_writer, &first, false, flush_on_finish_file);
|
operations.iter().for_each(|(operation, abort)| {
|
||||||
rest.iter().for_each(|operation| {
|
|
||||||
let _ = do_operation(
|
let _ = do_operation(
|
||||||
&mut other_writer,
|
&mut other_writer,
|
||||||
&operation,
|
&operation,
|
||||||
false,
|
*abort,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -133,11 +131,11 @@ fuzz_target!(|test_case: FuzzTestCase| {
|
||||||
final_reopen = true;
|
final_reopen = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (operation, abort) in test_case.operations {
|
for (operation, abort) in test_case.operations.into_iter() {
|
||||||
let _ = do_operation(
|
let _ = do_operation(
|
||||||
&mut writer,
|
&mut writer,
|
||||||
&operation,
|
&operation,
|
||||||
abort,
|
*abort,
|
||||||
test_case.flush_on_finish_file,
|
test_case.flush_on_finish_file,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -352,7 +352,7 @@ pub(crate) struct CentralDirectoryInfo {
|
||||||
impl<R> ZipArchive<R> {
|
impl<R> ZipArchive<R> {
|
||||||
pub(crate) fn from_finalized_writer(
|
pub(crate) fn from_finalized_writer(
|
||||||
files: IndexMap<Box<str>, ZipFileData>,
|
files: IndexMap<Box<str>, ZipFileData>,
|
||||||
comment: Vec<u8>,
|
comment: Box<[u8]>,
|
||||||
reader: R,
|
reader: R,
|
||||||
central_start: u64,
|
central_start: u64,
|
||||||
) -> ZipResult<Self> {
|
) -> ZipResult<Self> {
|
||||||
|
@ -368,7 +368,7 @@ impl<R> ZipArchive<R> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
reader,
|
reader,
|
||||||
shared,
|
shared,
|
||||||
comment: comment.into_boxed_slice().into(),
|
comment: comment.into(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ pub struct CentralDirectoryEnd {
|
||||||
pub number_of_files: u16,
|
pub number_of_files: u16,
|
||||||
pub central_directory_size: u32,
|
pub central_directory_size: u32,
|
||||||
pub central_directory_offset: u32,
|
pub central_directory_offset: u32,
|
||||||
pub zip_file_comment: Vec<u8>,
|
pub zip_file_comment: Box<[u8]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CentralDirectoryEnd {
|
impl CentralDirectoryEnd {
|
||||||
|
@ -38,7 +38,7 @@ impl CentralDirectoryEnd {
|
||||||
let central_directory_size = reader.read_u32_le()?;
|
let central_directory_size = reader.read_u32_le()?;
|
||||||
let central_directory_offset = reader.read_u32_le()?;
|
let central_directory_offset = reader.read_u32_le()?;
|
||||||
let zip_file_comment_length = reader.read_u16_le()? as usize;
|
let zip_file_comment_length = reader.read_u16_le()? as usize;
|
||||||
let mut zip_file_comment = vec![0; zip_file_comment_length];
|
let mut zip_file_comment = vec![0; zip_file_comment_length].into_boxed_slice();
|
||||||
reader.read_exact(&mut zip_file_comment)?;
|
reader.read_exact(&mut zip_file_comment)?;
|
||||||
|
|
||||||
Ok(CentralDirectoryEnd {
|
Ok(CentralDirectoryEnd {
|
||||||
|
|
17
src/write.rs
17
src/write.rs
|
@ -125,7 +125,7 @@ pub(crate) mod zip_writer {
|
||||||
pub(super) stats: ZipWriterStats,
|
pub(super) stats: ZipWriterStats,
|
||||||
pub(super) writing_to_file: bool,
|
pub(super) writing_to_file: bool,
|
||||||
pub(super) writing_raw: bool,
|
pub(super) writing_raw: bool,
|
||||||
pub(super) comment: Vec<u8>,
|
pub(super) comment: Box<[u8]>,
|
||||||
pub(super) flush_on_finish_file: bool,
|
pub(super) flush_on_finish_file: bool,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -683,7 +683,7 @@ impl<W: Write + Seek> ZipWriter<W> {
|
||||||
stats: Default::default(),
|
stats: Default::default(),
|
||||||
writing_to_file: false,
|
writing_to_file: false,
|
||||||
writing_raw: false,
|
writing_raw: false,
|
||||||
comment: Vec::new(),
|
comment: Box::new([]),
|
||||||
flush_on_finish_file: false,
|
flush_on_finish_file: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -696,16 +696,16 @@ impl<W: Write + Seek> ZipWriter<W> {
|
||||||
/// Set ZIP archive comment.
|
/// Set ZIP archive comment.
|
||||||
pub fn set_comment<S>(&mut self, comment: S)
|
pub fn set_comment<S>(&mut self, comment: S)
|
||||||
where
|
where
|
||||||
S: Into<String>,
|
S: Into<Box<str>>,
|
||||||
{
|
{
|
||||||
self.set_raw_comment(comment.into().into())
|
self.set_raw_comment(comment.into().into_boxed_bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set ZIP archive comment.
|
/// Set ZIP archive comment.
|
||||||
///
|
///
|
||||||
/// This sets the raw bytes of the comment. The comment
|
/// This sets the raw bytes of the comment. The comment
|
||||||
/// is typically expected to be encoded in UTF-8
|
/// is typically expected to be encoded in UTF-8
|
||||||
pub fn set_raw_comment(&mut self, comment: Vec<u8>) {
|
pub fn set_raw_comment(&mut self, comment: Box<[u8]>) {
|
||||||
self.comment = comment;
|
self.comment = comment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -718,7 +718,7 @@ impl<W: Write + Seek> ZipWriter<W> {
|
||||||
///
|
///
|
||||||
/// This returns the raw bytes of the comment. The comment
|
/// This returns the raw bytes of the comment. The comment
|
||||||
/// is typically expected to be encoded in UTF-8
|
/// is typically expected to be encoded in UTF-8
|
||||||
pub const fn get_raw_comment(&self) -> &Vec<u8> {
|
pub const fn get_raw_comment(&self) -> &[u8] {
|
||||||
&self.comment
|
&self.comment
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2458,10 +2458,11 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_crash_short_read() {
|
fn test_crash_short_read() {
|
||||||
let mut writer = ZipWriter::new(Cursor::new(Vec::new()));
|
let mut writer = ZipWriter::new(Cursor::new(Vec::new()));
|
||||||
let comment: Vec<u8> = vec![
|
let comment = vec![
|
||||||
1, 80, 75, 5, 6, 237, 237, 237, 237, 237, 237, 237, 237, 44, 255, 191, 255, 255, 255,
|
1, 80, 75, 5, 6, 237, 237, 237, 237, 237, 237, 237, 237, 44, 255, 191, 255, 255, 255,
|
||||||
255, 255, 255, 255, 255, 16,
|
255, 255, 255, 255, 255, 16,
|
||||||
];
|
]
|
||||||
|
.into_boxed_slice();
|
||||||
writer.set_raw_comment(comment);
|
writer.set_raw_comment(comment);
|
||||||
let options = SimpleFileOptions::default()
|
let options = SimpleFileOptions::default()
|
||||||
.compression_method(Stored)
|
.compression_method(Stored)
|
||||||
|
|
Loading…
Add table
Reference in a new issue