From 460728606bf0b232808add924f572e6b812b33b0 Mon Sep 17 00:00:00 2001 From: ChanTsune <41658782+ChanTsune@users.noreply.github.com> Date: Sun, 13 Aug 2023 14:50:17 +0900 Subject: [PATCH] use DecompressReader --- lib/src/archive/entry.rs | 34 ++++++++++++++++++++-------------- lib/src/archive/entry/read.rs | 2 +- lib/src/compress.rs | 4 +++- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/src/archive/entry.rs b/lib/src/archive/entry.rs index 45f137df..6a012281 100644 --- a/lib/src/archive/entry.rs +++ b/lib/src/archive/entry.rs @@ -6,6 +6,7 @@ mod options; mod read; mod write; +pub(crate) use self::read::MutexRead; pub use self::{builder::*, header::*, meta::*, name::*, options::*}; use self::{private::*, read::*, write::*}; use crate::{ @@ -14,6 +15,7 @@ use crate::{ MIN_CHUNK_BYTES_SIZE, }, cipher::{DecryptCbcAes256Reader, DecryptCbcCamellia256Reader, DecryptReader}, + compress::DecompressReader, hash::verify_password, }; use std::{ @@ -71,9 +73,9 @@ impl Entry for ChunkEntry { } /// [`Read`] -pub struct EntryDataReader(Box); +pub struct EntryDataReader(DecompressReader<'static, DecryptReader>); -impl Read for EntryDataReader { +impl Read for EntryDataReader { fn read(&mut self, buf: &mut [u8]) -> io::Result { self.0.read(buf) } @@ -100,11 +102,11 @@ impl TryFrom for ReadEntryContainer { } } -struct EntryIterator<'a> { - entry: Box, +struct EntryIterator { + entry: EntryDataReader, } -impl<'a> Iterator for EntryIterator<'a> { +impl Iterator for EntryIterator { type Item = io::Result; fn next(&mut self) -> Option { @@ -150,7 +152,9 @@ impl SolidReadEntry { )?; let reader = decompress_reader(reader, self.header.compression)?; - Ok(EntryIterator { entry: reader }) + Ok(EntryIterator { + entry: EntryDataReader(reader), + }) } } @@ -342,7 +346,7 @@ impl Entry for NonSolidReadEntry { } impl ReadEntry for NonSolidReadEntry { - type Reader = EntryDataReader; + type Reader = EntryDataReader>>; #[inline] fn header(&self) -> &EntryHeader { @@ -361,7 +365,7 @@ impl ReadEntry for NonSolidReadEntry { } impl NonSolidReadEntry { - fn reader(self, password: Option<&str>) -> io::Result { + fn reader(self, password: Option<&str>) -> io::Result>>> { let raw_data_reader = io::Cursor::new(self.data); let decrypt_reader = decrypt_reader( raw_data_reader, @@ -426,15 +430,17 @@ fn decrypt_reader( } /// Decompress reader according to compression type. -fn decompress_reader<'r, R: Read + Sync + Send + 'r>( +fn decompress_reader<'r, R: Read>( reader: R, compression: Compression, -) -> io::Result> { +) -> io::Result> { Ok(match compression { - Compression::No => Box::new(reader), - Compression::Deflate => Box::new(flate2::read::ZlibDecoder::new(reader)), - Compression::ZStandard => Box::new(MutexRead::new(zstd::Decoder::new(reader)?)), - Compression::XZ => Box::new(xz2::read::XzDecoder::new(reader)), + Compression::No => DecompressReader::Store(reader), + Compression::Deflate => DecompressReader::Deflate(flate2::read::ZlibDecoder::new(reader)), + Compression::ZStandard => { + DecompressReader::ZStd(MutexRead::new(zstd::Decoder::new(reader)?)) + } + Compression::XZ => DecompressReader::Xz(xz2::read::XzDecoder::new(reader)), }) } diff --git a/lib/src/archive/entry/read.rs b/lib/src/archive/entry/read.rs index 4b5e1024..89095927 100644 --- a/lib/src/archive/entry/read.rs +++ b/lib/src/archive/entry/read.rs @@ -7,7 +7,7 @@ use std::io::Read; use std::sync::Mutex; // NOTE: zstd crate not support Sync + Send trait -pub(super) struct MutexRead(Mutex); +pub(crate) struct MutexRead(Mutex); impl MutexRead { pub(super) fn new(reader: R) -> Self { diff --git a/lib/src/compress.rs b/lib/src/compress.rs index fc00c358..a61cca24 100644 --- a/lib/src/compress.rs +++ b/lib/src/compress.rs @@ -1,8 +1,10 @@ +use crate::archive::MutexRead; use crate::io::{TryIntoInner, TryIntoInnerWrite}; use flate2::{read::ZlibDecoder, write::ZlibEncoder}; use std::io::{BufReader, Read, Result, Write}; use xz2::{read::XzDecoder, write::XzEncoder}; use zstd::stream::{read::Decoder as ZStdDecoder, write::Encoder as ZstdEncoder}; + mod deflate; mod xz; mod zstandard; @@ -50,7 +52,7 @@ impl<'w, W: Write> TryIntoInnerWrite for CompressionWriter<'w, W> {} pub(crate) enum DecompressReader<'r, R: Read> { Store(R), Deflate(ZlibDecoder), - ZStd(ZStdDecoder<'r, BufReader>), + ZStd(MutexRead>>), Xz(XzDecoder), }