Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ rustdoc-args = ["--cfg", "docsrs"]
# groups
all = ["all-implementations", "all-algorithms"]
all-implementations = ["futures-io", "tokio"]
all-algorithms = ["brotli", "bzip2", "deflate", "gzip", "lzma", "xz", "zlib", "zstd", "deflate64"]
all-algorithms = ["brotli", "bzip2", "deflate", "gzip", "lz4", "lzma", "xz", "zlib", "zstd", "deflate64"]

# algorithms
deflate = ["flate2"]
gzip = ["flate2"]
lz4 = ["dep:lz4"]
lzma = ["dep:liblzma"]
xz = ["lzma"]
xz2 = ["xz"]
Expand All @@ -40,6 +41,7 @@ flate2 = { version = "1.0.13", optional = true }
futures-core = { version = "0.3", default-features = false }
futures-io = { version = "0.3", default-features = false, features = ["std"], optional = true }
libzstd = { package = "zstd", version = "0.13.1", optional = true, default-features = false }
lz4 = { version = "1.28.1", optional = true }
memchr = "2"
pin-project-lite = "0.2"
tokio = { version = "1.24.2", optional = true, default-features = false }
Expand Down Expand Up @@ -74,6 +76,10 @@ required-features = ["deflate"]
name = "gzip"
required-features = ["gzip"]

[[test]]
name = "lz4"
required-features = ["lz4"]

[[test]]
name = "lzma"
required-features = ["lzma"]
Expand Down
93 changes: 93 additions & 0 deletions src/codec/lz4/decoder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::io::Result;

use lz4::liblz4::{
check_error, LZ4FDecompressionContext, LZ4F_createDecompressionContext, LZ4F_decompress,
LZ4F_freeDecompressionContext, LZ4F_resetDecompressionContext, LZ4F_VERSION,
};

use crate::{codec::Decode, unshared::Unshared, util::PartialBuffer};

#[derive(Debug)]
struct DecoderContext {
ctx: LZ4FDecompressionContext,
}

#[derive(Debug)]
pub struct Lz4Decoder {
ctx: Unshared<DecoderContext>,
}

impl DecoderContext {
fn new() -> Result<Self> {
let mut context = LZ4FDecompressionContext(core::ptr::null_mut());
check_error(unsafe { LZ4F_createDecompressionContext(&mut context, LZ4F_VERSION) })?;
Ok(Self { ctx: context })
}
}

impl Drop for DecoderContext {
fn drop(&mut self) {
unsafe { LZ4F_freeDecompressionContext(self.ctx) };
}
}

impl Lz4Decoder {
pub(crate) fn new() -> Self {
Self {
ctx: Unshared::new(DecoderContext::new().unwrap()),
}
}
}

impl Decode for Lz4Decoder {
fn reinit(&mut self) -> Result<()> {
unsafe { LZ4F_resetDecompressionContext(self.ctx.get_mut().ctx) };
Ok(())
}

fn decode(
&mut self,
input: &mut PartialBuffer<impl AsRef<[u8]>>,
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
) -> Result<bool> {
let mut output_size = output.unwritten().len();
let mut input_size = input.unwritten().len();
let remaining = unsafe {
check_error(LZ4F_decompress(
self.ctx.get_mut().ctx,
output.unwritten_mut().as_mut_ptr(),
&mut output_size,
input.unwritten().as_ptr(),
&mut input_size,
core::ptr::null(),
))?
};
input.advance(input_size);
output.advance(output_size);
Ok(remaining == 0)
}

fn flush(
&mut self,
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
) -> Result<bool> {
self.decode(&mut PartialBuffer::new(&[][..]), output)?;

loop {
let old_len = output.written().len();
self.decode(&mut PartialBuffer::new(&[][..]), output)?;
if output.written().len() == old_len {
break;
}
}

Ok(!output.unwritten().is_empty())
}

fn finish(
&mut self,
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
) -> Result<bool> {
self.flush(output)
}
}
Loading