issue #34: adding support for xz2

This commit is contained in:
owen66 2020-12-02 00:57:51 +00:00
parent 85c287c4e4
commit c9a202c7ac
3 changed files with 41 additions and 0 deletions

View File

@ -29,6 +29,7 @@ miniz_oxide = "0.4.3"
#rand = "0.7.3"
log = "0.4.11"
thiserror = "1.0.22"
xz2 = "0.1.6"
[dev-dependencies]
futures-await-test = "0.3.0"

View File

@ -110,3 +110,40 @@ mod miniz_oxide {
}
}
}
/// Implementation for the [`Decompressor`] trait on [`xz2::Stream`].
///
/// This implements lzma compression as used in Tor.
mod xz2 {
use super::{Decompressor, Status, StatusKind};
use anyhow::{anyhow, Result};
use xz2::stream::{Action, Error, Status as Xz2Status, Stream};
impl Decompressor for Stream {
fn process(&mut self, inp: &[u8], out: &mut [u8], finished: bool) -> Result<Status> {
let action = if finished {
Action::Finish
} else {
Action::Run
};
let res = xz2::stream::Stream::process(self, inp, out, action);
let statuskind = match res {
Ok(Xz2Status::StreamEnd) => StatusKind::Done,
Ok(Xz2Status::Ok) => StatusKind::Written,
Ok(Xz2Status::GetCheck) => StatusKind::Written,
Ok(Xz2Status::MemNeeded) => StatusKind::OutOfSpace,
Err(Error::MemLimit) => StatusKind::OutOfSpace,
Err(Error::Mem) => StatusKind::OutOfSpace,
other => return Err(anyhow!("xz2 compression error: {:?}", other)),
};
Ok(Status {
status: statuskind,
consumed: xz2::stream::Stream::total_in(self) as usize,
written: xz2::stream::Stream::total_out(self) as usize,
})
}
}
}

View File

@ -232,6 +232,9 @@ fn get_decompressor(encoding: Option<&str>) -> Result<Box<dyn Decompressor>> {
Some("deflate") => Ok(miniz_oxide::inflate::stream::InflateState::new_boxed(
miniz_oxide::DataFormat::Zlib,
)),
Some("lzma") | Some("xz") => Ok(Box::new(
xz2::stream::Stream::new_lzma_decoder(u64::max_value()).unwrap(),
)),
Some(other) => Err(Error::BadEncoding(other.into()).into()),
}
}