Unverified Commit 6b304cbf authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

fix(turbopack): correct lzzzz LZ4F free signature

Vendor lzzzz 2.0.0 with LZ4F_freeCompressionContext declared as returning the LZ4F_errorCode_t used by bundled liblz4. This removes the wasm linker signature warning so linked WASI builds can deny linker diagnostics.
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent e19d4894
...@@ -5,3 +5,4 @@ examples/ ...@@ -5,3 +5,4 @@ examples/
AGENTS.md AGENTS.md
CLAUDE.md CLAUDE.md
packages/next/dist/docs/ packages/next/dist/docs/
turbopack/vendor/lzzzz/
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
.github/actions/*/dist/** -text linguist-vendored .github/actions/*/dist/** -text linguist-vendored
packages/next/bundles/** -text linguist-vendored packages/next/bundles/** -text linguist-vendored
packages/next/compiled/** -text linguist-vendored packages/next/compiled/** -text linguist-vendored
turbopack/vendor/lzzzz/** -text linguist-vendored
# Make next/src/build folder indexable for github search # Make next/src/build folder indexable for github search
build/** linguist-generated=false build/** linguist-generated=false
...@@ -60,6 +60,9 @@ test/e2e/app-dir/server-source-maps/fixtures/default/external-pkg/sourcemapped.j ...@@ -60,6 +60,9 @@ test/e2e/app-dir/server-source-maps/fixtures/default/external-pkg/sourcemapped.j
test/development/mcp-server/fixtures/default-template/app/build-error/page.tsx test/development/mcp-server/fixtures/default-template/app/build-error/page.tsx
test/development/mcp-server/fixtures/compilation-errors-app/app/syntax-error/page.tsx test/development/mcp-server/fixtures/compilation-errors-app/app/syntax-error/page.tsx
# vendored third-party crate, kept byte-identical to the published source.
/turbopack/vendor/lzzzz
# turbopack crates, disable for some tests and precompiled dependencies. # turbopack crates, disable for some tests and precompiled dependencies.
/turbopack/crates/*/js/src/compiled /turbopack/crates/*/js/src/compiled
/turbopack/crates/turbopack/bench.json /turbopack/crates/turbopack/bench.json
......
...@@ -4286,8 +4286,6 @@ dependencies = [ ...@@ -4286,8 +4286,6 @@ dependencies = [
[[package]] [[package]]
name = "lzzzz" name = "lzzzz"
version = "2.0.0" version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb42ba2edf1f0491b2e2c85fd49cf2a7adad3cab92ea0e29bff82d6fdafcefda"
dependencies = [ dependencies = [
"cc", "cc",
] ]
......
...@@ -18,7 +18,8 @@ members = [ ...@@ -18,7 +18,8 @@ members = [
] ]
exclude = [ exclude = [
"rspack/crates/binding" "rspack/crates/binding",
"turbopack/vendor/lzzzz",
] ]
[workspace.lints.clippy] [workspace.lints.clippy]
...@@ -370,6 +371,9 @@ vergen-gitcl = { version = "1.0.8", features = ["cargo"] } ...@@ -370,6 +371,9 @@ vergen-gitcl = { version = "1.0.8", features = ["cargo"] }
webbrowser = "1.0.6" webbrowser = "1.0.6"
[patch.crates-io] [patch.crates-io]
# lzzzz 2.0.0 declares LZ4F_freeCompressionContext with the wrong return type. Keep the vendored
# patch until https://github.com/picoHz/lzzzz includes the matching LZ4F_errorCode_t signature.
lzzzz = { path = "turbopack/vendor/lzzzz" }
bincode = { git = "https://github.com/bgw/bincode.git", branch = "bgw/patches" } bincode = { git = "https://github.com/bgw/bincode.git", branch = "bgw/patches" }
virtue = { git = "https://github.com/bgw/virtue.git", branch = "bgw/fix-generic-default-parsing" } virtue = { git = "https://github.com/bgw/virtue.git", branch = "bgw/fix-generic-default-parsing" }
# We have to very frequently bump the swc major version of mdxjs-rs # We have to very frequently bump the swc major version of mdxjs-rs
......
{"v":1}
\ No newline at end of file
{
"git": {
"sha1": "469f2a7ead6ac977940e34052966d87bc3949282"
},
"path_in_vcs": ""
}
\ No newline at end of file
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2021"
name = "lzzzz"
version = "2.0.0"
authors = ["picoHz <picoHz@outlook.com>"]
build = "build.rs"
include = [
"src/**/*",
"build.rs",
"vendor/liblz4/*",
"Cargo.toml",
"LICENSE",
]
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = "Full-featured liblz4 binding for Rust."
homepage = "https://github.com/picoHz/lzzzz"
documentation = "https://docs.rs/lzzzz"
readme = "README.md"
keywords = [
"lz4",
"lz4f",
"lz4-hc",
"compression",
"decompression",
]
categories = [
"compression",
"api-bindings",
]
license = "MIT"
repository = "https://github.com/picoHz/lzzzz"
[package.metadata.docs.rs]
all-features = true
rustdoc-args = [
"--cfg",
"docsrs",
]
[lib]
name = "lzzzz"
path = "src/lib.rs"
[dev-dependencies.assert_fs]
version = "1.0.6"
[dev-dependencies.base64]
version = "0.22.1"
[dev-dependencies.bytes]
version = "1.1.0"
[dev-dependencies.criterion]
version = "0.5.1"
[dev-dependencies.lazy_static]
version = "1.4.0"
[dev-dependencies.rand]
version = "0.8.4"
features = ["small_rng"]
[dev-dependencies.rayon]
version = "1.5.1"
[dev-dependencies.static_assertions]
version = "1.1.0"
[build-dependencies.cc]
version = "1.0.72"
features = ["parallel"]
[package]
name = "lzzzz"
version = "2.0.0"
authors = ["picoHz <picoHz@outlook.com>"]
edition = "2021"
description = "Full-featured liblz4 binding for Rust."
keywords = ["lz4", "lz4f", "lz4-hc", "compression", "decompression"]
categories = ["compression", "api-bindings"]
repository = "https://github.com/picoHz/lzzzz"
homepage = "https://github.com/picoHz/lzzzz"
documentation = "https://docs.rs/lzzzz"
license = "MIT"
readme = "README.md"
include = ["src/**/*", "build.rs", "vendor/liblz4/*", "Cargo.toml", "LICENSE"]
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[dev-dependencies]
assert_fs = "1.0.6"
base64 = "0.22.1"
bytes = "1.1.0"
lazy_static = "1.4.0"
rand = { version = "0.8.4", features = ["small_rng"] }
rayon = "1.5.1"
static_assertions = "1.1.0"
criterion = "0.5.1"
[build-dependencies]
cc = { version = "1.0.72", features = ["parallel"] }
[[bench]]
name = "lzzzz"
harness = false
path = "benches/lzzzz.rs"
MIT License
Copyright (c) 2020 picoHz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
<div align="center">
<img alt="lzzzz" src="lzzzz.png" height="80" />
Full-featured [liblz4](https://github.com/lz4/lz4) binding for Rust
[![Crates.io](https://img.shields.io/crates/v/lzzzz.svg)](https://crates.io/crates/lzzzz)
[![GitHub license](https://img.shields.io/github/license/picoHz/lzzzz.svg)](https://github.com/picoHz/lzzzz/blob/master/LICENSE)
[![Rustdoc](https://img.shields.io/badge/doc-rustdoc-green.svg)](https://docs.rs/lzzzz)
![Rust](https://github.com/picoHz/lzzzz/workflows/Rust/badge.svg)
</div>
---
## About
Rust APIs for the [LZ4](https://lz4.github.io/lz4/) compression algorithm.
- Supports almost all liblz4 features
- Zero dependencies except liblz4
- Tested on Windows / macOS / Linux
## Usage
Add this to your `Cargo.toml`:
```toml
[dependencies]
lzzzz = "1.0.3"
```
[API Documentation](https://docs.rs/lzzzz)
## Features
- LZ4
- Compression (Block / Streaming)
- Decompression (Block / Streaming)
- Partial Decompression
- Custom Dictionary
- LZ4_HC
- Compression (Block / Streaming)
- Partial Compression
- Custom Dictionary
- LZ4F
- Compression
- Decompression
- Custom Dictionary
- Streaming I/O (`Read` / `BufRead` / `Write`)
## Examples
### Block Mode
```rust
use lzzzz::{lz4, lz4_hc, lz4f};
let data = b"The quick brown fox jumps over the lazy dog.";
// LZ4 compression
let mut comp = Vec::new();
lz4::compress_to_vec(data, &mut comp, lz4::ACC_LEVEL_DEFAULT)?;
// LZ4_HC compression
let mut comp = Vec::new();
lz4_hc::compress_to_vec(data, &mut comp, lz4_hc::CLEVEL_DEFAULT)?;
// LZ4/LZ4_HC decompression
let mut decomp = vec![0; data.len()];
lz4::decompress(&comp, &mut decomp)?;
// LZ4F compression
let prefs = lz4f::Preferences::default();
let mut comp = Vec::new();
lz4f::compress_to_vec(data, &mut comp, &prefs)?;
// LZ4F decompression
let mut decomp = Vec::new();
lz4f::decompress_to_vec(&comp, &mut decomp)?;
```
### Streaming Mode
```rust
use lzzzz::{lz4, lz4_hc};
let data = b"The quick brown fox jumps over the lazy dog.";
// LZ4 compression
let mut comp = lz4::Compressor::new()?;
let mut buf = Vec::new();
comp.next_to_vec(data, &mut buf, lz4::ACC_LEVEL_DEFAULT)?;
// LZ4_HC compression
let mut comp = lz4_hc::Compressor::new()?;
let mut buf = Vec::new();
comp.next_to_vec(data, &mut buf)?;
// LZ4/LZ4_HC decompression
let mut decomp = lz4::Decompressor::new()?;
let result = decomp.next(&data, data.len())?;
```
```rust
use lzzzz::lz4f::{WriteCompressor, ReadDecompressor, Preferences};
use std::{fs::File, io::prelude::*};
// LZ4F Write-based compression
let mut f = File::create("foo.lz4")?;
let mut w = WriteCompressor::new(&mut f, Preferences::default())?;
w.write_all(b"Hello world!")?;
// LZ4F Read-based decompression
let mut f = File::open("foo.lz4")?;
let mut r = ReadDecompressor::new(&mut f)?;
let mut buf = Vec::new();
r.read_to_end(&mut buf)?;
```
fn main() -> Result<(), cc::Error> {
let sources = &["lz4.c", "lz4hc.c", "lz4frame.c", "xxhash.c"][..];
let dir = std::path::Path::new("vendor/liblz4");
cc::Build::new()
.files(sources.iter().map(|file| dir.join(file)))
.try_compile("lz4")
}
#![allow(unsafe_code)]
use super::binding;
use std::ffi::CStr;
/// Returns the version number of liblz4.
///
/// # Example
///
/// ```
/// assert_eq!(lzzzz::version_number(), 11000); // 1.9.4
/// ```
pub fn version_number() -> u32 {
unsafe { binding::LZ4_versionNumber() as u32 }
}
/// Returns the version string of liblz4.
///
/// # Example
///
/// ```
/// assert_eq!(lzzzz::version_string(), "1.10.0");
/// ```
pub fn version_string() -> &'static str {
unsafe {
CStr::from_ptr(binding::LZ4_versionString())
.to_str()
.unwrap()
}
}
use std::os::raw::{c_char, c_int};
#[link(name = "lz4")]
extern "C" {
pub fn LZ4_versionNumber() -> c_int;
pub fn LZ4_versionString() -> *const c_char;
}
use std::{convert, error, fmt, io, result};
/// A list specifying general categories of LZ4 error.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
/// The state initialization failed for some reason.
InitializationFailed,
/// The compression failed for some reason.
CompressionFailed,
/// The decompression failed for some reason.
DecompressionFailed,
/// The frame header had an invalid value.
FrameHeaderInvalid,
/// The decompressor reached unexpected EOF.
CompressedDataIncomplete,
/// Dictionary data was not consistent during the streaming decompression.
DictionaryChangedDuringDecompression,
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> result::Result<(), fmt::Error> {
<Self as fmt::Debug>::fmt(self, f)
}
}
/// The error type for LZ4 operations.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Error {
kind: ErrorKind,
}
impl Error {
pub(crate) const fn new(kind: ErrorKind) -> Self {
Self { kind }
}
/// Returns the corresponding `ErrorKind` for this error.
pub const fn kind(self) -> ErrorKind {
self.kind
}
}
impl convert::From<Error> for io::Error {
fn from(err: Error) -> Self {
Self::new(io::ErrorKind::Other, err)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> result::Result<(), fmt::Error> {
<ErrorKind as fmt::Display>::fmt(&self.kind, f)
}
}
impl error::Error for Error {}
/// A specialized [`Result`] type for LZ4 operations.
///
/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
pub type Result<T> = result::Result<T, Error>;
mod api;
mod binding;
mod error;
pub use api::{version_number, version_string};
pub use error::{Error, ErrorKind, Result};
pub(crate) const DEFAULT_BUF_SIZE: usize = 8 * 1024;
pub(crate) const DICTIONARY_SIZE: usize = 64 * 1024;
//! Full-featured liblz4 binding for Rust.
#![deny(unsafe_code)]
#![deny(clippy::all)]
#![cfg_attr(docsrs, feature(doc_cfg))]
mod common;
pub mod lz4;
pub mod lz4_hc;
pub mod lz4f;
pub use common::*;
use std::{
mem,
os::raw::{c_char, c_int, c_void},
};
const LZ4_MEMORY_USAGE: usize = 14;
const LZ4_STREAMSIZE_U64: usize = (1 << (LZ4_MEMORY_USAGE - 3)) + 4;
pub const LZ4_STREAMSIZE: usize = LZ4_STREAMSIZE_U64 * mem::size_of::<u64>();
#[repr(C)]
pub struct LZ4Stream {
_private: [u64; LZ4_STREAMSIZE_U64],
}
#[repr(C)]
pub struct LZ4DecStream {
_private: [u8; 0],
}
extern "C" {
pub fn LZ4_compress_fast_extState(
state: *mut c_void,
src: *const c_char,
dst: *mut c_char,
src_size: c_int,
dst_capacity: c_int,
acceleration: c_int,
) -> c_int;
pub fn LZ4_compress_fast_extState_fastReset(
state: *mut c_void,
src: *const c_char,
dst: *mut c_char,
src_size: c_int,
dst_capacity: c_int,
acceleration: c_int,
) -> c_int;
pub fn LZ4_compress_destSize(
src: *const c_char,
dst: *mut c_char,
src_size: *mut c_int,
target_dst_size: c_int,
) -> c_int;
pub fn LZ4_decompress_safe(
src: *const c_char,
dst: *mut c_char,
compressed_size: c_int,
dst_capacity: c_int,
) -> c_int;
pub fn LZ4_decompress_safe_partial(
src: *const c_char,
dst: *mut c_char,
src_size: c_int,
target_output_size: c_int,
dst_capacity: c_int,
) -> c_int;
pub fn LZ4_decompress_safe_usingDict(
src: *const c_char,
dst: *mut c_char,
compressed_size: c_int,
dst_capacity: c_int,
dict_start: *const c_char,
dict_size: c_int,
) -> c_int;
pub fn LZ4_decompress_safe_partial_usingDict(
src: *const c_char,
dst: *mut c_char,
compressed_size: c_int,
target_output_size: c_int,
dst_capacity: c_int,
dict_start: *const c_char,
dict_size: c_int,
) -> c_int;
pub fn LZ4_createStream() -> *mut LZ4Stream;
pub fn LZ4_freeStream(ptr: *mut LZ4Stream) -> c_int;
pub fn LZ4_initStream(buffer: *mut c_void, size: usize) -> *mut LZ4Stream;
pub fn LZ4_loadDict(ptr: *mut LZ4Stream, dictionary: *const c_char, dict_size: c_int) -> c_int;
pub fn LZ4_loadDictSlow(ptr: *mut LZ4Stream, dictionary: *const c_char, dict_size: c_int) -> c_int;
pub fn LZ4_saveDict(
ptr: *mut LZ4Stream,
safe_buffer: *mut c_char,
max_dict_size: c_int,
) -> c_int;
pub fn LZ4_compress_fast_continue(
ptr: *mut LZ4Stream,
src: *const c_char,
dst: *mut c_char,
src_size: c_int,
dst_capacity: c_int,
acceleration: c_int,
) -> c_int;
pub fn LZ4_createStreamDecode() -> *mut LZ4DecStream;
pub fn LZ4_freeStreamDecode(stream: *mut LZ4DecStream) -> c_int;
pub fn LZ4_setStreamDecode(
ptr: *mut LZ4DecStream,
dictionary: *const c_char,
dict_size: c_int,
) -> c_int;
pub fn LZ4_decompress_safe_continue(
ptr: *mut LZ4DecStream,
src: *const c_char,
dst: *mut c_char,
src_size: c_int,
dst_capacity: c_int,
) -> c_int;
pub fn LZ4_attach_dictionary(
working_stream: *mut LZ4Stream,
dictionary_stream: *const LZ4Stream,
);
pub fn LZ4_resetStream_fast(streamPtr: *mut LZ4Stream);
}
#![allow(unsafe_code)]
use super::super::binding;
use crate::{Error, ErrorKind, Result};
use std::{
cell::RefCell,
ops::Deref,
os::raw::{c_char, c_int, c_void},
};
const LZ4_MAX_INPUT_SIZE: usize = 0x7E00_0000;
pub const fn compress_bound(input_size: usize) -> usize {
(input_size <= LZ4_MAX_INPUT_SIZE) as usize * (input_size + (input_size / 255) + 16)
}
pub const fn size_of_state() -> usize {
binding::LZ4_STREAMSIZE
}
pub fn compress_fast_ext_state(
state: &mut [u8],
src: &[u8],
dst: *mut u8,
dst_len: usize,
acceleration: i32,
) -> usize {
unsafe {
binding::LZ4_compress_fast_extState(
state.as_mut_ptr() as *mut c_void,
src.as_ptr() as *const c_char,
dst as *mut c_char,
src.len() as c_int,
dst_len as c_int,
acceleration as c_int,
) as usize
}
}
pub fn compress_fast_ext_state_fast_reset(
state: &mut [u8],
src: &[u8],
dst: *mut u8,
dst_len: usize,
acceleration: i32,
) -> usize {
unsafe {
binding::LZ4_compress_fast_extState_fastReset(
state.as_mut_ptr() as *mut c_void,
src.as_ptr() as *const c_char,
dst as *mut c_char,
src.len() as c_int,
dst_len as c_int,
acceleration as c_int,
) as usize
}
}
pub fn compress_dest_size(
src: &[u8],
dst: &mut [u8],
) -> Result<(usize, usize)> {
let mut src_size: c_int = src.len() as c_int;
let result = unsafe {
binding::LZ4_compress_destSize(
src.as_ptr() as *const c_char,
dst.as_mut_ptr() as *mut c_char,
&mut src_size as *mut c_int,
dst.len() as c_int,
)
};
if result == 0 {
Err(Error::new(ErrorKind::CompressionFailed))
} else {
Ok((src_size as usize, result as usize))
}
}
pub fn decompress_safe(src: &[u8], dst: &mut [u8]) -> Result<usize> {
let result = unsafe {
binding::LZ4_decompress_safe(
src.as_ptr() as *const c_char,
dst.as_mut_ptr() as *mut c_char,
src.len() as c_int,
dst.len() as c_int,
)
};
if result < 0 {
Err(Error::new(ErrorKind::DecompressionFailed))
} else {
Ok(result as usize)
}
}
pub fn decompress_safe_partial(src: &[u8], dst: &mut [u8], original_size: usize) -> Result<usize> {
let result = unsafe {
binding::LZ4_decompress_safe_partial(
src.as_ptr() as *const c_char,
dst.as_mut_ptr() as *mut c_char,
src.len() as c_int,
original_size as c_int,
dst.len() as c_int,
)
};
if result < 0 {
Err(Error::new(ErrorKind::DecompressionFailed))
} else {
Ok(result as usize)
}
}
pub fn decompress_safe_using_dict(src: &[u8], dst: &mut [u8], dict: &[u8]) -> Result<usize> {
let result = unsafe {
binding::LZ4_decompress_safe_usingDict(
src.as_ptr() as *const c_char,
dst.as_mut_ptr() as *mut c_char,
src.len() as c_int,
dst.len() as c_int,
dict.as_ptr() as *const c_char,
dict.len() as c_int,
)
};
if result < 0 {
Err(Error::new(ErrorKind::DecompressionFailed))
} else {
Ok(result as usize)
}
}
pub fn decompress_safe_partial_using_dict(
src: &[u8],
dst: &mut [u8],
original_size: usize,
dict: &[u8],
) -> Result<usize> {
let result = unsafe {
binding::LZ4_decompress_safe_partial_usingDict(
src.as_ptr() as *const c_char,
dst.as_mut_ptr() as *mut c_char,
src.len() as c_int,
original_size as c_int,
dst.len() as c_int,
dict.as_ptr() as *const c_char,
dict.len() as c_int,
)
};
if result < 0 {
Err(Error::new(ErrorKind::DecompressionFailed))
} else {
Ok(result as usize)
}
}
#[derive(Clone)]
pub struct ExtState(RefCell<Box<[u8]>>);
impl ExtState {
fn new() -> Self {
let size = size_of_state() + 1;
Self(RefCell::new(vec![0; size].into_boxed_slice()))
}
pub fn with<F, R>(f: F) -> R
where
F: FnOnce(&Self, bool) -> R,
{
EXT_STATE.with(|state| {
let reset = {
let mut state = state.borrow_mut();
let last = state.len() - 1;
if state[last] == 0 {
state[last] = 1;
false
} else {
true
}
};
(f)(state, reset)
})
}
}
impl Deref for ExtState {
type Target = RefCell<Box<[u8]>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
thread_local!(static EXT_STATE: ExtState = ExtState::new());
mod api;
use crate::{Error, ErrorKind, Result};
use api::ExtState;
use std::cmp;
/// Calculates the maximum size of the compressed output.
///
/// If `original_size` is too large to compress, this returns `0`.
#[must_use]
pub const fn max_compressed_size(original_size: usize) -> usize {
api::compress_bound(original_size)
}
/// Performs LZ4 block compression.
///
/// Ensure that the destination slice has enough capacity.
/// If `dst.len()` is smaller than `lz4::max_compressed_size(src.len())`,
/// this function may fail.
///
/// Returns the number of bytes written into the destination buffer.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// let data = b"The quick brown fox jumps over the lazy dog.";
/// let mut buf = [0u8; 256];
///
/// // The slice should have enough capacity.
/// assert!(buf.len() >= lz4::max_compressed_size(data.len()));
///
/// let len = lz4::compress(data, &mut buf, lz4::ACC_LEVEL_DEFAULT)?;
/// let compressed = &buf[..len];
///
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(compressed, &mut buf[..data.len()])?;
/// # assert_eq!(&buf[..len], &data[..]);
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn compress(src: &[u8], dst: &mut [u8], acc: i32) -> Result<usize> {
compress_to_ptr(src, dst.as_mut_ptr(), dst.len(), acc)
}
fn compress_to_ptr(src: &[u8], dst: *mut u8, dst_len: usize, acc: i32) -> Result<usize> {
if src.is_empty() {
return Ok(0);
}
let acc = cmp::min(acc, 33_554_431);
let len = ExtState::with(|state, reset| {
let mut state = state.borrow_mut();
if reset {
api::compress_fast_ext_state_fast_reset(&mut state, src, dst, dst_len, acc)
} else {
api::compress_fast_ext_state(&mut state, src, dst, dst_len, acc)
}
});
if len > 0 {
Ok(len)
} else {
Err(Error::new(ErrorKind::CompressionFailed))
}
}
/// Appends compressed data to `Vec<u8>`.
///
/// Returns the number of bytes appended to the given `Vec<u8>`.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// let data = b"The quick brown fox jumps over the lazy dog.";
/// let mut buf = Vec::new();
///
/// lz4::compress_to_vec(data, &mut buf, lz4::ACC_LEVEL_DEFAULT)?;
/// # let compressed = &buf;
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(compressed, &mut buf[..data.len()])?;
/// # assert_eq!(&buf[..len], &data[..]);
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn compress_to_vec(src: &[u8], dst: &mut Vec<u8>, acc: i32) -> Result<usize> {
let orig_len = dst.len();
dst.reserve(max_compressed_size(src.len()));
#[allow(unsafe_code)]
unsafe {
let result = compress_to_ptr(
src,
dst.as_mut_ptr().add(orig_len),
dst.capacity() - orig_len,
acc,
);
dst.set_len(orig_len + result.as_ref().unwrap_or(&0));
result
}
}
/// Compress data to fill `dst`.
///
/// This function either compresses the entire `src` buffer into `dst` if it's
/// large enough, or will fill `dst` with as much data as possible from `src`.
///
/// Returns a pair `(read, wrote)` giving the number of bytes read from `src`
/// and the number of bytes written to `dst`.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// let data = b"The quick brown fox jumps over the lazy dog.";
/// let mut buf = [0u8; 256];
///
/// // This slice should have enough capacity.
/// assert!(buf.len() >= lz4::max_compressed_size(data.len()));
///
/// let (read, wrote) = lz4::compress_fill(data, &mut buf)?;
/// assert_eq!(read, data.len());
/// let compressed = &buf[..wrote];
///
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(compressed, &mut buf[..data.len()])?;
/// # assert_eq!(&buf[..len], &data[..]);
///
/// // This slice doesn't have enough capacity, but we can fill it.
/// let mut smallbuf = [0u8; 32];
/// assert!(smallbuf.len() < lz4::max_compressed_size(data.len()));
///
/// let (read, wrote) = lz4::compress_fill(data, &mut smallbuf)?;
/// assert_eq!(wrote, smallbuf.len());
/// let remaining_data = &data[read..];
///
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(&smallbuf, &mut buf)?;
/// # assert_eq!(&buf[..len], &data[..read]);
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn compress_fill(src: &[u8], dst: &mut [u8]) -> Result<(usize, usize)> {
api::compress_dest_size(src, dst)
}
/// Decompresses an LZ4 block.
///
/// The length of the destination slice must be equal to the original data length.
///
/// Returns the number of bytes written into the destination buffer.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// const ORIGINAL_SIZE: usize = 44;
/// const COMPRESSED_DATA: &str =
/// "8B1UaGUgcXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nLg==";
///
/// let data = base64::decode(COMPRESSED_DATA).unwrap();
/// let mut buf = [0u8; ORIGINAL_SIZE];
///
/// lz4::decompress(&data[..], &mut buf[..])?;
///
/// assert_eq!(
/// &buf[..],
/// &b"The quick brown fox jumps over the lazy dog."[..]
/// );
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn decompress(src: &[u8], dst: &mut [u8]) -> Result<usize> {
api::decompress_safe(src, dst)
}
/// Decompresses an LZ4 block until the destination slice fills up.
///
/// Returns the number of bytes written into the destination buffer.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// const ORIGINAL_SIZE: usize = 44;
/// const COMPRESSED_DATA: &str =
/// "8B1UaGUgcXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nLg==";
///
/// let data = base64::decode(COMPRESSED_DATA).unwrap();
/// let mut buf = [0u8; 24];
///
/// lz4::decompress_partial(&data[..], &mut buf[..], ORIGINAL_SIZE)?;
///
/// assert_eq!(&buf[..], &b"The quick brown fox jump"[..]);
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn decompress_partial(src: &[u8], dst: &mut [u8], original_size: usize) -> Result<usize> {
api::decompress_safe_partial(src, dst, original_size)
}
/// Decompresses an LZ4 block with a dictionary.
///
/// Returns the number of bytes written into the destination buffer.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// const ORIGINAL_SIZE: usize = 44;
/// const COMPRESSED_DATA: &str = "DywAFFAgZG9nLg==";
/// const DICT_DATA: &[u8] = b"The quick brown fox jumps over the lazy cat.";
///
/// let data = base64::decode(COMPRESSED_DATA).unwrap();
/// let mut buf = [0u8; ORIGINAL_SIZE];
///
/// lz4::decompress_with_dict(&data[..], &mut buf[..], DICT_DATA)?;
///
/// assert_eq!(
/// &buf[..],
/// &b"The quick brown fox jumps over the lazy dog."[..]
/// );
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn decompress_with_dict(src: &[u8], dst: &mut [u8], dict: &[u8]) -> Result<usize> {
api::decompress_safe_using_dict(src, dst, dict)
}
/// Decompresses an LZ4 block with a dictionary until the destination slice fills up.
///
/// Returns the number of bytes written into the destination buffer.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// const ORIGINAL_SIZE: usize = 44;
/// const COMPRESSED_DATA: &str = "DywAFFAgZG9nLg==";
/// const DICT_DATA: &[u8] = b"The quick brown fox jumps over the lazy cat.";
///
/// let data = base64::decode(COMPRESSED_DATA).unwrap();
/// let mut buf = [0u8; 24];
///
/// lz4::decompress_partial_with_dict(&data[..], &mut buf[..], ORIGINAL_SIZE, DICT_DATA)?;
///
/// assert_eq!(
/// &buf[..],
/// &b"The quick brown fox jump"[..]
/// );
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn decompress_partial_with_dict(
src: &[u8],
dst: &mut [u8],
original_size: usize,
dict: &[u8],
) -> Result<usize> {
api::decompress_safe_partial_using_dict(src, dst, original_size, dict)
}
//! LZ4 compression and decompression.
//!
//! LZ4: Extremely fast compression algorithm.
//!
//! # Acceleration factor
//! Some functions take the acceleration factor.
//!
//! Larger value increases the processing speed in exchange for the
//! lesser compression ratio.
//!
//! ```
//! # use lzzzz::lz4;
//! # let data = b"The quick brown fox jumps over the lazy dog.";
//! # let mut buf = Vec::new();
//! // The default factor is 1 so both have the same meaning.
//! lz4::compress_to_vec(data, &mut buf, lz4::ACC_LEVEL_DEFAULT)?;
//! lz4::compress_to_vec(data, &mut buf, 1)?;
//!
//! // Factors lower than 1 are interpreted as 1 so these are also the same as above.
//! lz4::compress_to_vec(data, &mut buf, 0)?;
//! lz4::compress_to_vec(data, &mut buf, -100)?;
//!
//! // Faster but less effective compression.
//! lz4::compress_to_vec(data, &mut buf, 1000)?;
//!
//! # Ok::<(), std::io::Error>(())
//! ```
mod binding;
mod block;
mod stream;
pub use block::*;
pub use stream::*;
/// Predefined acceleration level (1).
pub const ACC_LEVEL_DEFAULT: i32 = 1;
#![allow(unsafe_code)]
use super::super::{
binding,
binding::{LZ4DecStream, LZ4Stream},
};
use crate::{Error, ErrorKind, Result};
use std::{
mem::{size_of, MaybeUninit},
os::raw::{c_char, c_int, c_void},
ptr::NonNull,
ptr::null_mut
};
#[allow(clippy::large_enum_variant)]
enum Stream {
Stack(LZ4Stream),
Heap(NonNull<LZ4Stream>),
}
pub struct CompressionContext {
stream: Stream,
}
unsafe impl Send for CompressionContext {}
impl CompressionContext {
pub fn new() -> Result<Self> {
let mut stream = MaybeUninit::<LZ4Stream>::uninit();
unsafe {
let ptr = binding::LZ4_initStream(
stream.as_mut_ptr() as *mut c_void,
size_of::<LZ4Stream>(),
);
if !ptr.is_null() {
return Ok(Self {
stream: Stream::Stack(stream.assume_init()),
});
}
NonNull::new(binding::LZ4_createStream())
}
.ok_or_else(|| Error::new(ErrorKind::InitializationFailed))
.map(|stream| Self {
stream: Stream::Heap(stream),
})
}
fn get_ptr(&mut self) -> *mut LZ4Stream {
match &mut self.stream {
Stream::Stack(stream) => stream as *mut LZ4Stream,
Stream::Heap(ptr) => ptr.as_ptr(),
}
}
pub fn next(&mut self, src: &[u8], dst: *mut u8, dst_len: usize, acceleration: i32) -> usize {
unsafe {
binding::LZ4_compress_fast_continue(
self.get_ptr(),
src.as_ptr() as *const c_char,
dst as *mut c_char,
src.len() as c_int,
dst_len as c_int,
acceleration as c_int,
) as usize
}
}
pub fn load_dict(&mut self, dict: &[u8]) {
unsafe {
binding::LZ4_loadDict(
self.get_ptr(),
dict.as_ptr() as *const c_char,
dict.len() as c_int,
);
}
}
pub fn load_dict_slow(&mut self, dict: &[u8]) {
unsafe {
binding::LZ4_loadDictSlow(
self.get_ptr(),
dict.as_ptr() as *const c_char,
dict.len() as c_int,
);
}
}
pub fn save_dict(&mut self, dict: &mut [u8]) {
unsafe {
binding::LZ4_saveDict(
self.get_ptr(),
dict.as_ptr() as *mut c_char,
dict.len() as c_int,
);
}
}
pub fn attach_dict(&mut self, dict_stream: Option<&mut CompressionContext>) {
unsafe {
if dict_stream.is_none() {
// Note(sewer56): When detaching dictionary, we need to reset the stream state
// This behaviour is consistent with what the LZ4 library itself does internally.
binding::LZ4_resetStream_fast(self.get_ptr());
}
let dict_ptr = dict_stream.map(|ctx| ctx.get_ptr()).unwrap_or(null_mut());
binding::LZ4_attach_dictionary(self.get_ptr(), dict_ptr);
}
}
}
impl Drop for CompressionContext {
fn drop(&mut self) {
if let Stream::Heap(mut ptr) = self.stream {
unsafe {
binding::LZ4_freeStream(ptr.as_mut());
}
}
}
}
pub struct DecompressionContext {
stream: NonNull<LZ4DecStream>,
}
unsafe impl Send for DecompressionContext {}
impl DecompressionContext {
pub fn new() -> Result<Self> {
unsafe {
let ptr = NonNull::new(binding::LZ4_createStreamDecode());
ptr.ok_or_else(|| Error::new(ErrorKind::InitializationFailed))
.map(|stream| Self { stream })
}
}
pub fn reset(&mut self, dict: &[u8]) -> Result<()> {
let result = unsafe {
binding::LZ4_setStreamDecode(
self.stream.as_ptr(),
dict.as_ptr() as *const c_char,
dict.len() as c_int,
)
};
if result == 1 {
Ok(())
} else {
Err(Error::new(ErrorKind::InitializationFailed))
}
}
pub fn decompress(&mut self, src: &[u8], dst: *mut u8, dst_len: usize) -> Result<usize> {
let result = unsafe {
binding::LZ4_decompress_safe_continue(
self.stream.as_ptr(),
src.as_ptr() as *const c_char,
dst as *mut c_char,
src.len() as c_int,
dst_len as c_int,
)
};
if result < 0 {
Err(Error::new(ErrorKind::DecompressionFailed))
} else {
Ok(result as usize)
}
}
}
impl Drop for DecompressionContext {
fn drop(&mut self) {
unsafe {
binding::LZ4_freeStreamDecode(self.stream.as_mut());
}
}
}
mod api;
use crate::{
common::{DEFAULT_BUF_SIZE, DICTIONARY_SIZE},
lz4, Error, ErrorKind, Result,
};
use api::{CompressionContext, DecompressionContext};
use std::{borrow::Cow, cmp, collections::LinkedList, pin::Pin};
/// Streaming LZ4 compressor.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// let data = b"The quick brown fox jumps over the lazy dog.";
/// let mut buf = [0u8; 256];
///
/// // The slice should have enough capacity.
/// assert!(buf.len() >= lz4::max_compressed_size(data.len()));
///
/// let mut comp = lz4::Compressor::new()?;
/// let len = comp.next(data, &mut buf, lz4::ACC_LEVEL_DEFAULT)?;
/// let compressed = &buf[..len];
///
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(compressed, &mut buf[..data.len()])?;
/// # assert_eq!(&buf[..len], &data[..]);
/// # Ok::<(), std::io::Error>(())
/// ```
pub struct Compressor<'a> {
ctx: CompressionContext,
dict: Pin<Cow<'a, [u8]>>,
safe_buf: Vec<u8>,
}
impl<'a> Compressor<'a> {
/// Creates a new `Compressor`.
pub fn new() -> Result<Self> {
Ok(Self {
ctx: CompressionContext::new()?,
dict: Pin::new(Cow::Borrowed(&[])),
safe_buf: Vec::new(),
})
}
/// Creates a new `Compressor` with a dictionary.
pub fn with_dict<D>(dict: D) -> Result<Self>
where
D: Into<Cow<'a, [u8]>>,
{
let mut comp = Self {
dict: Pin::new(dict.into()),
..Self::new()?
};
comp.ctx.load_dict(&comp.dict);
Ok(comp)
}
/// Creates a new `Compressor` with a dictionary.
/// This variant which consumes more initialization time to better reference the dictionary,
/// resulting in slightly improved compression ratios at expense of time.
pub fn with_dict_slow<D>(dict: D) -> Result<Self>
where
D: Into<Cow<'a, [u8]>>,
{
let mut comp = Self {
dict: Pin::new(dict.into()),
..Self::new()?
};
comp.ctx.load_dict_slow(&comp.dict);
Ok(comp)
}
/// Performs LZ4 streaming compression.
///
/// Returns the number of bytes written into the destination buffer.
pub fn next(&mut self, src: &[u8], dst: &mut [u8], acc: i32) -> Result<usize> {
self.next_to_ptr(src, dst.as_mut_ptr(), dst.len(), acc)
}
fn next_to_ptr(&mut self, src: &[u8], dst: *mut u8, dst_len: usize, acc: i32) -> Result<usize> {
let is_empty = src.is_empty() && dst_len == 0;
let dst_len = self.ctx.next(src, dst, dst_len, acc);
self.save_dict();
if dst_len > 0 {
Ok(dst_len)
} else if is_empty {
Ok(0)
} else {
Err(Error::new(ErrorKind::CompressionFailed))
}
}
/// Appends compressed data to `Vec<u8>`.
///
/// Returns the number of bytes appended to the given `Vec<u8>`.
pub fn next_to_vec(&mut self, src: &[u8], dst: &mut Vec<u8>, acc: i32) -> Result<usize> {
let orig_len = dst.len();
dst.reserve(lz4::max_compressed_size(src.len()));
#[allow(unsafe_code)]
unsafe {
let result = self.next_to_ptr(
src,
dst.as_mut_ptr().add(orig_len),
dst.capacity() - orig_len,
acc,
);
dst.set_len(orig_len + result.as_ref().unwrap_or(&0));
result
}
}
fn save_dict(&mut self) {
self.safe_buf.resize(DICTIONARY_SIZE, 0);
self.ctx.save_dict(&mut self.safe_buf);
}
/// Attaches a dictionary stream for efficient dictionary reuse.
///
/// This allows efficient re-use of a static dictionary multiple times by referencing
/// the dictionary stream in-place rather than copying it.
///
/// # Arguments
///
/// * `dict_stream` - The dictionary stream to attach, or None to unset any existing dictionary
///
/// # Notes
///
/// - The dictionary stream must have been prepared using `with_dict()` or `with_dict_slow()`
/// - The dictionary will only remain attached through the first compression call
/// - The dictionary stream (and its source buffer) must remain valid through the compression session
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// let dict_data = b"some dictionary data";
/// let data = b"data to compress";
///
/// // Create dictionary stream
/// let mut dict_comp = lz4::Compressor::with_dict(dict_data)?;
///
/// // Create working stream and attach dictionary
/// let mut comp = lz4::Compressor::new()?;
/// comp.attach_dict(Some(&mut dict_comp));
///
/// // Compress data using the attached dictionary
/// let mut buf = [0u8; 256];
/// let len = comp.next(data, &mut buf, lz4::ACC_LEVEL_DEFAULT)?;
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn attach_dict(&mut self, dict_stream: Option<&mut Compressor<'a>>) {
if let Some(dict) = dict_stream {
self.ctx.attach_dict(Some(&mut dict.ctx));
} else {
self.ctx.attach_dict(None);
}
}
}
/// Streaming LZ4 decompressor.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// const ORIGINAL_SIZE: usize = 44;
/// const COMPRESSED_DATA: &str =
/// "8B1UaGUgcXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nLg==";
///
/// let data = base64::decode(COMPRESSED_DATA).unwrap();
///
/// let mut decomp = lz4::Decompressor::new()?;
/// let result = decomp.next(&data[..], ORIGINAL_SIZE)?;
///
/// assert_eq!(result, &b"The quick brown fox jumps over the lazy dog."[..]);
/// # Ok::<(), std::io::Error>(())
/// ```
pub struct Decompressor<'a> {
ctx: DecompressionContext,
cache: LinkedList<Vec<u8>>,
cache_len: usize,
last_len: usize,
dict: Pin<Cow<'a, [u8]>>,
}
impl<'a> Decompressor<'a> {
/// Creates a new `Decompressor`.
pub fn new() -> Result<Self> {
Ok(Self {
ctx: DecompressionContext::new()?,
cache: LinkedList::new(),
cache_len: 0,
last_len: 0,
dict: Pin::new(Cow::Borrowed(&[])),
})
}
/// Creates a new `Decompressor` with a dictionary.
pub fn with_dict<D>(dict: D) -> Result<Self>
where
D: Into<Cow<'a, [u8]>>,
{
let mut decomp = Self {
dict: Pin::new(dict.into()),
..Self::new()?
};
decomp.ctx.reset(&decomp.dict)?;
Ok(decomp)
}
/// Decompresses an LZ4 block.
pub fn next(&mut self, src: &[u8], original_size: usize) -> Result<&[u8]> {
if self
.cache
.back()
.map(|v| v.capacity() - v.len())
.filter(|n| *n >= original_size)
.is_none()
{
self.cache.push_back(Vec::with_capacity(cmp::max(
original_size,
DEFAULT_BUF_SIZE,
)));
}
let back = self.cache.back_mut().unwrap();
let orig_len = back.len();
#[allow(unsafe_code)]
unsafe {
let dst_len = self.ctx.decompress(
src,
back.as_mut_ptr().add(orig_len),
back.capacity() - orig_len,
)?;
back.set_len(orig_len + dst_len);
self.cache_len += dst_len;
}
self.last_len = original_size;
while let Some(len) = self
.cache
.front()
.map(Vec::len)
.filter(|n| self.cache_len - n >= DICTIONARY_SIZE)
{
self.cache.pop_front();
self.cache_len -= len;
}
Ok(self.data())
}
fn data(&self) -> &[u8] {
if let Some(back) = self.cache.back() {
let offset = back.len() - self.last_len;
&back[offset..]
} else {
&[]
}
}
}
use std::os::raw::{c_char, c_int, c_void};
const LZ4HC_HASH_LOG: usize = 15;
const LZ4HC_HASHTABLESIZE: usize = 1 << LZ4HC_HASH_LOG;
const LZ4HC_DICTIONARY_LOGSIZE: usize = 16;
const LZ4HC_MAXD: usize = 1 << LZ4HC_DICTIONARY_LOGSIZE;
pub const LZ4_STREAMHCSIZE: usize = 4 * LZ4HC_HASHTABLESIZE + 2 * LZ4HC_MAXD + 56;
#[repr(C)]
pub struct LZ4StreamHC {
_private: [u8; 0],
}
extern "C" {
pub fn LZ4_compress_HC_extStateHC(
state: *mut c_void,
src: *const c_char,
dst: *mut c_char,
src_size: c_int,
dst_capacity: c_int,
compression_level: c_int,
) -> c_int;
pub fn LZ4_compress_HC_extStateHC_fastReset(
state: *mut c_void,
src: *const c_char,
dst: *mut c_char,
src_size: c_int,
dst_capacity: c_int,
compression_level: c_int,
) -> c_int;
pub fn LZ4_compress_HC_destSize(
state: *mut c_void,
src: *const c_char,
dst: *mut c_char,
src_size_ptr: *mut c_int,
target_dst_dize: c_int,
compression_level: c_int,
) -> c_int;
pub fn LZ4_createStreamHC() -> *mut LZ4StreamHC;
pub fn LZ4_freeStreamHC(ptr: *mut LZ4StreamHC) -> c_int;
pub fn LZ4_loadDictHC(
ptr: *mut LZ4StreamHC,
dictionary: *const c_char,
dict_size: c_int,
) -> c_int;
pub fn LZ4_saveDictHC(
ptr: *mut LZ4StreamHC,
safe_buffer: *mut c_char,
max_dict_size: c_int,
) -> c_int;
pub fn LZ4_compress_HC_continue(
ptr: *mut LZ4StreamHC,
src: *const c_char,
dst: *mut c_char,
src_size: c_int,
dst_capacity: c_int,
) -> c_int;
pub fn LZ4_compress_HC_continue_destSize(
ptr: *mut LZ4StreamHC,
src: *const c_char,
dst: *mut c_char,
src_size_ptr: *mut c_int,
target_dst_size: c_int,
) -> c_int;
pub fn LZ4_setCompressionLevel(ptr: *mut LZ4StreamHC, compression_level: c_int);
pub fn LZ4_favorDecompressionSpeed(ptr: *mut LZ4StreamHC, favor: c_int);
pub fn LZ4_attach_HC_dictionary(
working_stream: *mut LZ4StreamHC,
dictionary_stream: *const LZ4StreamHC
);
pub fn LZ4_resetStreamHC_fast(
streamPtr: *mut LZ4StreamHC,
compressionLevel: c_int
);
}
#![allow(unsafe_code)]
use super::super::binding;
use std::{
cell::RefCell,
ops::Deref,
os::raw::{c_char, c_int, c_void},
};
pub const fn size_of_state() -> usize {
binding::LZ4_STREAMHCSIZE
}
pub fn compress_ext_state(
state: &mut [u8],
src: &[u8],
dst: *mut u8,
dst_len: usize,
compression_level: i32,
) -> usize {
unsafe {
binding::LZ4_compress_HC_extStateHC(
state.as_mut_ptr() as *mut c_void,
src.as_ptr() as *const c_char,
dst as *mut c_char,
src.len() as c_int,
dst_len as c_int,
compression_level as c_int,
) as usize
}
}
pub fn compress_ext_state_fast_reset(
state: &mut [u8],
src: &[u8],
dst: *mut u8,
dst_len: usize,
compression_level: i32,
) -> usize {
unsafe {
binding::LZ4_compress_HC_extStateHC_fastReset(
state.as_mut_ptr() as *mut c_void,
src.as_ptr() as *const c_char,
dst as *mut c_char,
src.len() as c_int,
dst_len as c_int,
compression_level as c_int,
) as usize
}
}
pub fn compress_dest_size(
state: &mut [u8],
src: &[u8],
dst: &mut [u8],
compression_level: i32,
) -> (usize, usize) {
let mut src_len = src.len() as i32;
let dst_len = unsafe {
binding::LZ4_compress_HC_destSize(
state.as_mut_ptr() as *mut c_void,
src.as_ptr() as *const c_char,
dst.as_mut_ptr() as *mut c_char,
&mut src_len as *mut c_int,
dst.len() as c_int,
compression_level as c_int,
) as usize
};
(src_len as usize, dst_len)
}
#[derive(Clone)]
pub struct ExtState(RefCell<Box<[u8]>>);
impl ExtState {
fn new() -> Self {
let size = size_of_state() + 1;
Self(RefCell::new(vec![0; size].into_boxed_slice()))
}
pub fn with<F, R>(f: F) -> R
where
F: FnOnce(&Self, bool) -> R,
{
EXT_STATE.with(|state| {
let reset = {
let mut state = state.borrow_mut();
let last = state.len() - 1;
if state[last] == 0 {
state[last] = 1;
false
} else {
true
}
};
(f)(state, reset)
})
}
}
impl Deref for ExtState {
type Target = RefCell<Box<[u8]>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
thread_local!(static EXT_STATE: ExtState = ExtState::new());
mod api;
use crate::{lz4, Error, ErrorKind, Result};
use api::ExtState;
use std::{cmp, io::Cursor};
/// Performs LZ4_HC block compression.
///
/// Ensure that the destination slice has enough capacity.
/// If `dst.len()` is smaller than `lz4::max_compressed_size(src.len())`,
/// this function may fail.
///
/// Returns the number of bytes written into the destination buffer.
///
/// # Example
///
/// ```
/// use lzzzz::{lz4, lz4_hc};
///
/// let data = b"The quick brown fox jumps over the lazy dog.";
/// let mut buf = [0u8; 256];
///
/// // The slice should have enough capacity.
/// assert!(buf.len() >= lz4::max_compressed_size(data.len()));
///
/// let len = lz4_hc::compress(data, &mut buf, lz4_hc::CLEVEL_DEFAULT)?;
/// let compressed = &buf[..len];
///
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(
/// # compressed,
/// # &mut buf[..data.len()],
/// # )?;
/// # assert_eq!(&buf[..len], &data[..]);
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn compress(src: &[u8], dst: &mut [u8], level: i32) -> Result<usize> {
compress_to_ptr(src, dst.as_mut_ptr(), dst.len(), level)
}
fn compress_to_ptr(src: &[u8], dst: *mut u8, dst_len: usize, level: i32) -> Result<usize> {
if src.is_empty() {
return Ok(0);
}
let len = ExtState::with(|state, reset| {
if reset {
api::compress_ext_state_fast_reset(&mut state.borrow_mut(), src, dst, dst_len, level)
} else {
api::compress_ext_state(&mut state.borrow_mut(), src, dst, dst_len, level)
}
});
if len > 0 {
Ok(len)
} else {
Err(Error::new(ErrorKind::CompressionFailed))
}
}
/// Compress data to fill `dst`.
///
/// This function either compresses the entire `src` buffer into `dst` if it's
/// large enough, or will fill `dst` with as much data as possible from `src`.
///
/// Returns a pair `(read, wrote)` giving the number of bytes read from `src`
/// and the number of bytes written to `dst`.
///
/// # Example
///
/// ```
/// use lzzzz::{lz4, lz4_hc};
///
/// let data = b"The quick brown fox jumps over the lazy dog.";
/// let mut buf = [0u8; 256];
///
/// // This slice should have enough capacity.
/// assert!(buf.len() >= lz4::max_compressed_size(data.len()));
///
/// let (read, wrote) = lz4_hc::compress_fill(data, &mut buf, lz4_hc::CLEVEL_DEFAULT)?;
/// assert_eq!(read, data.len());
/// let compressed = &buf[..wrote];
///
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(compressed, &mut buf[..data.len()])?;
/// # assert_eq!(&buf[..len], &data[..]);
///
/// // This slice doesn't have enough capacity, but we can fill it.
/// let mut smallbuf = [0u8; 32];
/// assert!(smallbuf.len() < lz4::max_compressed_size(data.len()));
///
/// let (read, wrote) = lz4_hc::compress_fill(data, &mut smallbuf, lz4_hc::CLEVEL_DEFAULT)?;
/// assert_eq!(wrote, smallbuf.len());
/// let remaining_data = &data[read..];
///
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(&smallbuf, &mut buf)?;
/// # assert_eq!(&buf[..len], &data[..read]);
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn compress_fill(src: &[u8], dst: &mut [u8], level: i32) -> Result<(usize, usize)> {
if src.is_empty() {
return Ok((0, 0));
}
let (read, wrote) = ExtState::with(|state, _reset| {
api::compress_dest_size(&mut state.borrow_mut(), src, dst, level)
});
if wrote > 0 {
Ok((read, wrote))
} else {
Err(Error::new(ErrorKind::CompressionFailed))
}
}
/// Compresses data until the destination slice fills up.
///
/// Returns the number of bytes written into the destination buffer.
///
/// # Example
///
/// ```
/// use lzzzz::{lz4, lz4_hc};
/// use std::io::Cursor;
///
/// let data = b"The quick brown fox jumps over the lazy dog.";
/// let mut buf = [0u8; 16];
///
/// let mut src = Cursor::new(&data[..]);
/// let len = lz4_hc::compress_partial(&mut src, &mut buf, lz4_hc::CLEVEL_DEFAULT)?;
/// let compressed = &buf[..len];
///
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(
/// # compressed,
/// # &mut buf[..data.len()],
/// # )?;
/// # assert_eq!(&buf[..len], &data[..src.position() as usize]);
/// # Ok::<(), std::io::Error>(())
/// ```
#[deprecated(since = "1.1.0", note = "Use compress_fill instead.")]
pub fn compress_partial<T>(src: &mut Cursor<T>, dst: &mut [u8], level: i32) -> Result<usize>
where
T: AsRef<[u8]>,
{
let src_ref = src.get_ref().as_ref();
let pos = cmp::min(src_ref.len(), src.position() as usize);
let src_ref = &src_ref[pos..];
if src_ref.is_empty() || dst.is_empty() {
return Ok(0);
}
let (src_len, dst_len) = ExtState::with(|state, _| {
api::compress_dest_size(&mut state.borrow_mut(), src_ref, dst, level)
});
src.set_position(src.position() + src_len as u64);
Ok(dst_len)
}
/// Appends compressed data to `Vec<u8>`.
///
/// Returns the number of bytes appended to the given `Vec<u8>`.
///
/// # Example
///
/// ```
/// use lzzzz::{lz4, lz4_hc};
///
/// let data = b"The quick brown fox jumps over the lazy dog.";
/// let mut buf = Vec::new();
///
/// lz4_hc::compress_to_vec(data, &mut buf, lz4_hc::CLEVEL_DEFAULT)?;
///
/// # let compressed = &buf;
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(
/// # compressed,
/// # &mut buf[..data.len()],
/// # )?;
/// # assert_eq!(&buf[..len], &data[..]);
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn compress_to_vec(src: &[u8], dst: &mut Vec<u8>, level: i32) -> Result<usize> {
let orig_len = dst.len();
dst.reserve(lz4::max_compressed_size(src.len()));
#[allow(unsafe_code)]
unsafe {
let result = compress_to_ptr(
src,
dst.as_mut_ptr().add(orig_len),
dst.capacity() - orig_len,
level,
);
dst.set_len(orig_len + result.as_ref().unwrap_or(&0));
result
}
}
//! LZ4_HC compression.
//!
//! LZ4_HC: High compression variant of LZ4.
//!
//! # Decompression
//! The `lz4_hc` module doesn't provide decompression functionality.
//! Use the [`lz4`] module instead.
//!
//! [`lz4`]: ../lz4/index.html
mod binding;
mod block;
mod stream;
pub use block::*;
pub use stream::*;
/// Predefined compression level (3).
pub const CLEVEL_MIN: i32 = 3;
/// Predefined compression level (9).
pub const CLEVEL_DEFAULT: i32 = 9;
/// Predefined compression level (10).
pub const CLEVEL_OPT_MIN: i32 = 10;
/// Predefined compression level (12).
pub const CLEVEL_MAX: i32 = 12;
/// Decompression speed mode flag.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum FavorDecSpeed {
Disabled,
Enabled,
}
impl Default for FavorDecSpeed {
fn default() -> Self {
Self::Disabled
}
}
#![allow(unsafe_code)]
use super::super::{binding, binding::LZ4StreamHC};
use crate::{Error, ErrorKind, Result};
use std::{
os::raw::{c_char, c_int},
ptr::NonNull,
ptr::null_mut
};
pub struct CompressionContext {
stream: NonNull<LZ4StreamHC>,
}
unsafe impl Send for CompressionContext {}
impl CompressionContext {
pub fn new() -> Result<Self> {
let ptr = unsafe { NonNull::new(binding::LZ4_createStreamHC()) };
ptr.ok_or_else(|| Error::new(ErrorKind::InitializationFailed))
.map(|stream| Self { stream })
}
pub fn set_compression_level(&mut self, compression_level: i32) {
unsafe {
binding::LZ4_setCompressionLevel(self.stream.as_ptr(), compression_level as c_int)
}
}
pub fn set_favor_dec_speed(&mut self, flag: bool) {
unsafe { binding::LZ4_favorDecompressionSpeed(self.stream.as_ptr(), i32::from(flag)) }
}
pub fn load_dict(&mut self, dict: &[u8]) {
unsafe {
binding::LZ4_loadDictHC(
self.stream.as_ptr(),
dict.as_ptr() as *const c_char,
dict.len() as c_int,
);
}
}
pub fn save_dict(&mut self, dict: &mut [u8]) {
unsafe {
binding::LZ4_saveDictHC(
self.stream.as_ptr(),
dict.as_ptr() as *mut c_char,
dict.len() as c_int,
);
}
}
pub fn next(&mut self, src: &[u8], dst: *mut u8, dst_len: usize) -> Result<usize> {
if src.is_empty() {
return Ok(0);
}
let dst_len = unsafe {
binding::LZ4_compress_HC_continue(
self.stream.as_ptr(),
src.as_ptr() as *const c_char,
dst as *mut c_char,
src.len() as c_int,
dst_len as c_int,
) as usize
};
if dst_len > 0 {
Ok(dst_len)
} else {
Err(Error::new(ErrorKind::CompressionFailed))
}
}
pub fn next_partial(&mut self, src: &[u8], dst: &mut [u8]) -> Result<(usize, usize)> {
if src.is_empty() || dst.is_empty() {
return Ok((0, 0));
}
let mut src_len = src.len() as c_int;
let dst_len = unsafe {
binding::LZ4_compress_HC_continue_destSize(
self.stream.as_ptr(),
src.as_ptr() as *const c_char,
dst.as_mut_ptr() as *mut c_char,
&mut src_len as *mut c_int,
dst.len() as c_int,
) as usize
};
if dst_len > 0 {
Ok((src_len as usize, dst_len))
} else {
Err(Error::new(ErrorKind::CompressionFailed))
}
}
pub fn attach_dict(&mut self, dict_stream: Option<&CompressionContext>, compression_level: i32) {
unsafe {
if dict_stream.is_none() {
// Note(sewer56): When detaching dictionary, we need to reset the stream state
// This behaviour is consistent with what the LZ4 library itself does internally.
// The LZ4HC API does not have a way to retrieve compression level, so we must pass it manually,
// since the HC API differs here.
binding::LZ4_resetStreamHC_fast(self.stream.as_ptr(), compression_level);
}
let dict_ptr = dict_stream.map(|ctx| ctx.stream.as_ptr()).unwrap_or(null_mut());
binding::LZ4_attach_HC_dictionary(self.stream.as_ptr(), dict_ptr);
}
}
}
impl Drop for CompressionContext {
fn drop(&mut self) {
unsafe {
binding::LZ4_freeStreamHC(self.stream.as_mut());
}
}
}
mod api;
use crate::{common::DICTIONARY_SIZE, lz4, lz4_hc::FavorDecSpeed, Result};
use api::CompressionContext;
use std::{borrow::Cow, cmp, io::Cursor, pin::Pin};
/// Streaming LZ4_HC compressor.
///
/// # Example
///
/// ```
/// use lzzzz::{lz4, lz4_hc};
///
/// let data = b"The quick brown fox jumps over the lazy dog.";
/// let mut buf = [0u8; 256];
///
/// // The slice should have enough capacity.
/// assert!(buf.len() >= lz4::max_compressed_size(data.len()));
///
/// let mut comp = lz4_hc::Compressor::new()?;
/// let len = comp.next(data, &mut buf)?;
/// let compressed = &buf[..len];
///
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(compressed, &mut buf[..data.len()])?;
/// # assert_eq!(&buf[..len], &data[..]);
/// # Ok::<(), std::io::Error>(())
/// ```
pub struct Compressor<'a> {
ctx: CompressionContext,
dict: Pin<Cow<'a, [u8]>>,
safe_buf: Vec<u8>,
}
impl<'a> Compressor<'a> {
/// Creates a new `Compressor`.
pub fn new() -> Result<Self> {
Ok(Self {
ctx: CompressionContext::new()?,
dict: Pin::new(Cow::Borrowed(&[])),
safe_buf: Vec::new(),
})
}
/// Creates a new `Compressor` with a dictionary.
pub fn with_dict<D>(dict: D, compression_level: i32) -> Result<Self>
where
D: Into<Cow<'a, [u8]>>,
{
// Note(sewer56).
// The LZ4 documentation states the following:
// - In order for LZ4_loadDictHC() to create the correct data structure,
// it is essential to set the compression level _before_ loading the dictionary.
// Therefore this API requires a `compression_level`.
let mut comp = Self {
dict: Pin::new(dict.into()),
..Self::new()?
};
comp.ctx.set_compression_level(compression_level);
comp.ctx.load_dict(&comp.dict);
Ok(comp)
}
/// Sets the compression level.
pub fn set_compression_level(&mut self, level: i32) {
self.ctx.set_compression_level(level);
}
/// Sets the decompression speed mode flag.
pub fn set_favor_dec_speed(&mut self, dec_speed: FavorDecSpeed) {
self.ctx
.set_favor_dec_speed(dec_speed == FavorDecSpeed::Enabled);
}
/// Performs LZ4_HC streaming compression.
///
/// Returns the number of bytes written into the destination buffer.
pub fn next(&mut self, src: &[u8], dst: &mut [u8]) -> Result<usize> {
self.next_to_ptr(src, dst.as_mut_ptr(), dst.len())
}
fn next_to_ptr(&mut self, src: &[u8], dst: *mut u8, dst_len: usize) -> Result<usize> {
let result = self.ctx.next(src, dst, dst_len)?;
self.save_dict();
Ok(result)
}
/// Performs LZ4_HC streaming compression to fill `dst`.
///
/// This function either compresses the entire `src` buffer into `dst` if it's
/// large enough, or will fill `dst` with as much data as possible from `src`.
///
/// Returns a pair `(read, wrote)` giving the number of bytes read from `src`
/// and the number of bytes written to `dst`.
///
/// # Example
///
/// ```
/// use lzzzz::{lz4, lz4_hc};
///
/// let data = b"The quick brown fox jumps over the lazy dog.";
///
/// let mut smallbuf = [0u8; 32];
/// assert!(smallbuf.len() < lz4::max_compressed_size(data.len()));
///
/// let mut comp = lz4_hc::Compressor::new()?;
/// let (read, wrote) = comp.next_fill(data, &mut smallbuf)?;
/// let remaining_data = &data[read..];
///
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(&smallbuf, &mut buf)?;
/// # assert_eq!(&buf[..len], &data[..read]);
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn next_fill(&mut self, src: &[u8], dst: &mut [u8]) -> Result<(usize, usize)> {
let (src_len, dst_len) = self.ctx.next_partial(src, dst)?;
self.save_dict();
Ok((src_len, dst_len))
}
/// Compresses data until the destination slice fills up.
///
/// Returns the number of bytes written into the destination buffer.
#[deprecated(since = "1.1.0", note = "Use next_fill instead.")]
pub fn next_partial<T>(&mut self, src: &mut Cursor<T>, dst: &mut [u8]) -> Result<usize>
where
T: AsRef<[u8]>,
{
let src_ref = src.get_ref().as_ref();
let pos = cmp::min(src_ref.len(), src.position() as usize);
let src_ref = &src_ref[pos..];
let (src_len, dst_len) = self.ctx.next_partial(src_ref, dst)?;
src.set_position(src.position() + src_len as u64);
self.save_dict();
Ok(dst_len)
}
/// Appends a compressed frame to `Vec<u8>`.
///
/// Returns the number of bytes appended to the given `Vec<u8>`.
pub fn next_to_vec(&mut self, src: &[u8], dst: &mut Vec<u8>) -> Result<usize> {
let orig_len = dst.len();
dst.reserve(lz4::max_compressed_size(src.len()));
#[allow(unsafe_code)]
unsafe {
let result = self.next_to_ptr(
src,
dst.as_mut_ptr().add(orig_len),
dst.capacity() - orig_len,
);
dst.set_len(orig_len + result.as_ref().unwrap_or(&0));
result
}
}
fn save_dict(&mut self) {
self.safe_buf.resize(DICTIONARY_SIZE, 0);
self.ctx.save_dict(&mut self.safe_buf);
}
/// Attaches a dictionary stream for efficient dictionary reuse.
///
/// This allows efficient re-use of a static dictionary multiple times by referencing
/// the dictionary stream in-place rather than copying it.
///
/// # Arguments
///
/// * `dict_stream` - The dictionary stream to attach, or None to unset any existing dictionary
/// * `compression_level` - The compression level to use (CLEVEL_MIN to CLEVEL_MAX)
///
/// # Notes
///
/// - The dictionary stream must have been prepared using `with_dict()`
/// - The dictionary will only remain attached through the first compression call
/// - The dictionary stream (and its source buffer) must remain valid through the compression session
///
/// # Example
///
/// ```
/// use lzzzz::lz4_hc;
///
/// let dict_data = b"dictionary data";
/// let data = b"data to compress";
///
/// // Create dictionary stream
/// let dict_comp = lz4_hc::Compressor::with_dict(dict_data, lz4_hc::CLEVEL_DEFAULT)?;
///
/// // Create working stream and attach dictionary
/// let mut comp = lz4_hc::Compressor::new()?;
/// comp.attach_dict(Some(&dict_comp), lz4_hc::CLEVEL_DEFAULT);
///
/// // Compress data using the attached dictionary
/// let mut buf = [0u8; 256];
/// let len = comp.next(data, &mut buf)?;
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn attach_dict(&mut self, dict_stream: Option<&Compressor<'a>>, compression_level: i32) {
if let Some(dict) = dict_stream {
self.ctx.attach_dict(Some(&dict.ctx), compression_level);
} else {
self.ctx.attach_dict(None, compression_level);
}
}
}
#![allow(unsafe_code)]
use super::{
binding,
binding::{
LZ4FCompressionCtx, LZ4FCompressionDict, LZ4FCompressionOptions, LZ4FDecompressionCtx,
LZ4FDecompressionOptions,
},
Dictionary,
};
use crate::lz4f::{Error, ErrorKind, FrameInfo, Preferences, Result};
use std::{mem::MaybeUninit, os::raw::c_void, ptr::NonNull};
pub const LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH: usize = 5;
pub const LZ4F_HEADER_SIZE_MAX: usize = 19;
pub struct CompressionContext {
ctx: NonNull<LZ4FCompressionCtx>,
dict: Option<Dictionary>,
}
unsafe impl Send for CompressionContext {}
impl CompressionContext {
pub fn new(dict: Option<Dictionary>) -> Result<Self> {
let ctx = MaybeUninit::<*mut LZ4FCompressionCtx>::uninit();
unsafe {
let code = binding::LZ4F_createCompressionContext(
ctx.as_ptr() as *mut *mut binding::LZ4FCompressionCtx,
binding::LZ4F_getVersion(),
);
result_from_code(code).and_then(|_| {
Ok(Self {
ctx: NonNull::new(ctx.assume_init())
.ok_or_else(|| crate::Error::new(crate::ErrorKind::InitializationFailed))?,
dict,
})
})
}
}
pub fn begin(&mut self, dst: *mut u8, dst_len: usize, prefs: &Preferences) -> Result<usize> {
let code = unsafe {
if let Some(dict) = &self.dict {
binding::LZ4F_compressBegin_usingCDict(
self.ctx.as_ptr(),
dst as *mut c_void,
dst_len,
dict.handle().0.as_ptr(),
prefs as *const Preferences,
)
} else {
binding::LZ4F_compressBegin(self.ctx.as_ptr(), dst as *mut c_void, dst_len, prefs)
}
};
result_from_code(code).map(|_| code)
}
pub fn update(
&mut self,
dst: *mut u8,
dst_len: usize,
src: &[u8],
stable_src: bool,
) -> Result<usize> {
let opt = LZ4FCompressionOptions::stable(stable_src);
let code = unsafe {
binding::LZ4F_compressUpdate(
self.ctx.as_ptr(),
dst as *mut c_void,
dst_len,
src.as_ptr() as *const c_void,
src.len(),
&opt as *const LZ4FCompressionOptions,
)
};
result_from_code(code).map(|_| code)
}
pub fn flush(&mut self, dst: *mut u8, dst_len: usize, stable_src: bool) -> Result<usize> {
let opt = LZ4FCompressionOptions::stable(stable_src);
let code = unsafe {
binding::LZ4F_flush(
self.ctx.as_ptr(),
dst as *mut c_void,
dst_len,
&opt as *const LZ4FCompressionOptions,
)
};
result_from_code(code).map(|_| code)
}
pub fn end(&mut self, dst: *mut u8, dst_len: usize, stable_src: bool) -> Result<usize> {
let opt = LZ4FCompressionOptions::stable(stable_src);
let code = unsafe {
binding::LZ4F_compressEnd(
self.ctx.as_ptr(),
dst as *mut c_void,
dst_len,
&opt as *const LZ4FCompressionOptions,
)
};
result_from_code(code).map(|_| code)
}
pub fn compress_bound(src_size: usize, prefs: &Preferences) -> usize {
unsafe { binding::LZ4F_compressBound(src_size, prefs as *const Preferences) }
}
}
impl Drop for CompressionContext {
fn drop(&mut self) {
unsafe {
binding::LZ4F_freeCompressionContext(self.ctx.as_ptr());
}
}
}
pub struct DecompressionContext {
ctx: NonNull<LZ4FDecompressionCtx>,
}
unsafe impl Send for DecompressionContext {}
impl DecompressionContext {
pub fn new() -> Result<Self> {
let ctx = MaybeUninit::<*mut LZ4FDecompressionCtx>::uninit();
unsafe {
let code = binding::LZ4F_createDecompressionContext(
ctx.as_ptr() as *mut *mut binding::LZ4FDecompressionCtx,
binding::LZ4F_getVersion(),
);
result_from_code(code).and_then(|_| {
Ok(Self {
ctx: NonNull::new(ctx.assume_init())
.ok_or_else(|| crate::Error::new(crate::ErrorKind::InitializationFailed))?,
})
})
}
}
pub fn get_frame_info(&self, src: &[u8]) -> Result<(FrameInfo, usize)> {
let mut info = MaybeUninit::<FrameInfo>::uninit();
let mut src_len = src.len();
let code = unsafe {
binding::LZ4F_getFrameInfo(
self.ctx.as_ptr(),
info.as_mut_ptr(),
src.as_ptr() as *const c_void,
&mut src_len as *mut usize,
)
};
result_from_code(code).map(|_| (unsafe { info.assume_init() }, src_len))
}
pub fn decompress_dict(
&mut self,
src: &[u8],
dst: &mut [u8],
dict: &[u8],
stable_dst: bool,
) -> Result<(usize, usize, usize)> {
let mut dst_len = dst.len();
let mut src_len = src.len();
let opt = LZ4FDecompressionOptions::stable(stable_dst);
let code = unsafe {
binding::LZ4F_decompress_usingDict(
self.ctx.as_ptr(),
dst.as_mut_ptr() as *mut c_void,
&mut dst_len as *mut usize,
src.as_ptr() as *const c_void,
&mut src_len as *mut usize,
dict.as_ptr() as *const c_void,
dict.len(),
&opt as *const LZ4FDecompressionOptions,
)
};
result_from_code(code).map(|_| (src_len, dst_len, code))
}
pub fn reset(&mut self) {
unsafe {
binding::LZ4F_resetDecompressionContext(self.ctx.as_ptr());
}
}
}
impl Drop for DecompressionContext {
fn drop(&mut self) {
unsafe {
binding::LZ4F_freeDecompressionContext(self.ctx.as_ptr());
}
}
}
pub fn compress_frame_bound(src_size: usize, prefs: &Preferences) -> usize {
unsafe { binding::LZ4F_compressFrameBound(src_size, prefs as *const Preferences) }
}
pub fn header_size(src: &[u8]) -> usize {
unsafe { binding::LZ4F_headerSize(src.as_ptr() as *const c_void, src.len()) }
}
pub fn compress(src: &[u8], dst: *mut u8, dst_len: usize, prefs: &Preferences) -> Result<usize> {
let code = unsafe {
binding::LZ4F_compressFrame(
dst as *mut c_void,
dst_len,
src.as_ptr() as *const c_void,
src.len(),
prefs as *const Preferences,
)
};
result_from_code(code).map(|_| code)
}
fn result_from_code(code: usize) -> Result<()> {
Err(Error::new(match code.wrapping_neg() {
1 => ErrorKind::Generic,
2 => ErrorKind::MaxBlockSizeInvalid,
3 => ErrorKind::BlockModeInvalid,
4 => ErrorKind::ContentChecksumFlagInvalid,
5 => ErrorKind::CompressionLevelInvalid,
6 => ErrorKind::HeaderVersionWrong,
7 => ErrorKind::BlockChecksumInvalid,
8 => ErrorKind::ReservedFlagSet,
9 => ErrorKind::AllocationFailed,
10 => ErrorKind::SrcSizeTooLarge,
11 => ErrorKind::DstMaxSizeTooSmall,
12 => ErrorKind::FrameHeaderIncomplete,
13 => ErrorKind::FrameTypeUnknown,
14 => ErrorKind::FrameSizeWrong,
15 => ErrorKind::SrcPtrWrong,
16 => ErrorKind::DecompressionFailed,
17 => ErrorKind::HeaderChecksumInvalid,
18 => ErrorKind::ContentChecksumInvalid,
19 => ErrorKind::FrameDecodingAlreadyStarted,
_ => return Ok(()),
}))
}
pub struct DictionaryHandle(NonNull<LZ4FCompressionDict>);
unsafe impl Send for DictionaryHandle {}
unsafe impl Sync for DictionaryHandle {}
impl DictionaryHandle {
pub fn new(data: &[u8]) -> Result<Self> {
let dict = unsafe { binding::LZ4F_createCDict(data.as_ptr() as *const c_void, data.len()) };
NonNull::new(dict)
.ok_or_else(|| crate::Error::new(crate::ErrorKind::InitializationFailed).into())
.map(Self)
}
}
impl Drop for DictionaryHandle {
fn drop(&mut self) {
unsafe {
binding::LZ4F_freeCDict(self.0.as_ptr());
}
}
}
use super::{FrameInfo, Preferences};
use std::os::raw::{c_uint, c_void};
#[allow(non_camel_case_types)]
type size_t = usize;
#[repr(C)]
pub struct LZ4FCompressionCtx {
_private: [u8; 0],
}
#[repr(C)]
pub struct LZ4FDecompressionCtx {
_private: [u8; 0],
}
#[repr(C)]
pub struct LZ4FCompressionDict {
_private: [u8; 0],
}
#[derive(Debug, Default, Copy, Clone)]
#[repr(C)]
pub struct LZ4FCompressionOptions {
pub stable_src: c_uint,
pub _reserved: [c_uint; 3],
}
impl LZ4FCompressionOptions {
pub fn stable(stable: bool) -> Self {
Self {
stable_src: u32::from(stable),
..Default::default()
}
}
}
#[derive(Debug, Default, Copy, Clone)]
#[repr(C)]
pub struct LZ4FDecompressionOptions {
pub stable_dst: c_uint,
pub _reserved: [c_uint; 3],
}
impl LZ4FDecompressionOptions {
pub fn stable(stable: bool) -> Self {
Self {
stable_dst: u32::from(stable),
..Default::default()
}
}
}
extern "C" {
pub fn LZ4F_getVersion() -> c_uint;
pub fn LZ4F_compressBound(src_size: size_t, prefs: *const Preferences) -> size_t;
pub fn LZ4F_compressFrameBound(src_size: size_t, prefs: *const Preferences) -> size_t;
pub fn LZ4F_compressFrame(
dst_buffer: *mut c_void,
dst_capacity: size_t,
src_buffer: *const c_void,
src_size: size_t,
prefs: *const Preferences,
) -> size_t;
pub fn LZ4F_decompress_usingDict(
ctx: *mut LZ4FDecompressionCtx,
dst_buffer: *mut c_void,
dst_size_ptr: *mut size_t,
src_buffer: *const c_void,
src_size_ptr: *mut size_t,
dict: *const c_void,
dict_size: size_t,
opt: *const LZ4FDecompressionOptions,
) -> size_t;
pub fn LZ4F_createCDict(
dict_buffer: *const c_void,
dict_size: size_t,
) -> *mut LZ4FCompressionDict;
pub fn LZ4F_freeCDict(dict: *mut LZ4FCompressionDict);
pub fn LZ4F_createCompressionContext(
ctx: *mut *mut LZ4FCompressionCtx,
version: c_uint,
) -> size_t;
pub fn LZ4F_freeCompressionContext(ctx: *mut LZ4FCompressionCtx) -> size_t;
pub fn LZ4F_compressBegin(
ctx: *mut LZ4FCompressionCtx,
dst_buffer: *mut c_void,
dst_capacity: size_t,
prefs: *const Preferences,
) -> size_t;
pub fn LZ4F_compressBegin_usingCDict(
ctx: *mut LZ4FCompressionCtx,
dst_buffer: *mut c_void,
dst_capacity: size_t,
dist: *const LZ4FCompressionDict,
prefs: *const Preferences,
) -> size_t;
pub fn LZ4F_compressUpdate(
ctx: *mut LZ4FCompressionCtx,
dst_buffer: *mut c_void,
dst_capacity: size_t,
src_buffer: *const c_void,
src_size: size_t,
opt: *const LZ4FCompressionOptions,
) -> size_t;
pub fn LZ4F_flush(
ctx: *mut LZ4FCompressionCtx,
dst_buffer: *mut c_void,
dst_capacity: size_t,
opt: *const LZ4FCompressionOptions,
) -> size_t;
pub fn LZ4F_compressEnd(
ctx: *mut LZ4FCompressionCtx,
dst_buffer: *mut c_void,
dst_capacity: size_t,
opt: *const LZ4FCompressionOptions,
) -> size_t;
pub fn LZ4F_createDecompressionContext(
ctx: *mut *mut LZ4FDecompressionCtx,
version: c_uint,
) -> size_t;
pub fn LZ4F_freeDecompressionContext(ctx: *mut LZ4FDecompressionCtx) -> size_t;
pub fn LZ4F_resetDecompressionContext(ctx: *mut LZ4FDecompressionCtx);
pub fn LZ4F_headerSize(src: *const c_void, src_size: size_t) -> size_t;
pub fn LZ4F_getFrameInfo(
ctx: *mut LZ4FDecompressionCtx,
frame_info_ptr: *mut FrameInfo,
src_buffer: *const c_void,
src_size_ptr: *mut size_t,
) -> size_t;
}
use super::{api::DictionaryHandle, Result};
use std::sync::Arc;
/// A pre-compiled dictionary for the efficient compression.
#[derive(Clone)]
pub struct Dictionary(Arc<DictionaryHandle>);
impl Dictionary {
/// Builds a new `Dictionary`.
pub fn new(data: &[u8]) -> Result<Self> {
Ok(Self(Arc::new(DictionaryHandle::new(data)?)))
}
pub(crate) fn handle(&self) -> &DictionaryHandle {
&self.0
}
}
#[cfg(test)]
mod tests {
use super::Dictionary;
#[test]
fn create_dictionary() {
assert!(Dictionary::new(&[]).is_ok());
assert!(Dictionary::new(&b"quick brown fox jumps over the lazy dog"[..]).is_ok());
}
}
use std::{convert, fmt, io};
/// A list specifying general categories of LZ4F error.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
#[allow(missing_docs)]
pub enum ErrorKind {
Generic,
MaxBlockSizeInvalid,
BlockModeInvalid,
ContentChecksumFlagInvalid,
CompressionLevelInvalid,
HeaderVersionWrong,
BlockChecksumInvalid,
ReservedFlagSet,
AllocationFailed,
SrcSizeTooLarge,
DstMaxSizeTooSmall,
FrameHeaderIncomplete,
FrameTypeUnknown,
FrameSizeWrong,
SrcPtrWrong,
DecompressionFailed,
HeaderChecksumInvalid,
ContentChecksumInvalid,
FrameDecodingAlreadyStarted,
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> {
<Self as fmt::Debug>::fmt(self, f)
}
}
/// The error type for LZ4F operations.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Error {
Lz4f(ErrorKind),
Common(crate::ErrorKind),
}
impl Error {
pub(super) const fn new(kind: ErrorKind) -> Self {
Self::Lz4f(kind)
}
}
impl convert::From<Error> for io::Error {
fn from(err: Error) -> Self {
Self::new(io::ErrorKind::Other, err)
}
}
impl convert::From<crate::Error> for Error {
fn from(err: crate::Error) -> Self {
Self::Common(err.kind())
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> {
match self {
Self::Lz4f(kind) => <ErrorKind as fmt::Display>::fmt(kind, f),
Self::Common(kind) => <crate::ErrorKind as fmt::Display>::fmt(kind, f),
}
}
}
impl std::error::Error for Error {}
/// A specialized [`Result`] type for LZ4F operations.
///
/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
pub type Result<T> = std::result::Result<T, Error>;
//! LZ4 Frame Compressor/Decompressor
use super::{api, Result};
use crate::{common::DEFAULT_BUF_SIZE, lz4f::Preferences, Error, ErrorKind};
use std::{cell::RefCell, ops::Deref};
/// Calculates the maximum size of the compressed output.
///
/// If `original_size` is too large to compress, this returns `0`.
///
/// Returned values are reliable only for [`compress`] and [`compress_to_vec`].
/// Streaming compressors may produce larger compressed frames.
///
/// [`compress`]: fn.compress.html
/// [`compress_to_vec`]: fn.compress_to_vec.html
#[must_use]
pub fn max_compressed_size(original_size: usize, prefs: &Preferences) -> usize {
api::compress_frame_bound(original_size, prefs)
}
/// Performs LZ4F compression.
///
/// Ensure that the destination slice has enough capacity.
/// If `dst.len()` is smaller than `lz4f::max_compressed_size(src.len())`,
/// this function may fail.
///
/// Returns the number of bytes written into the destination buffer.
///
/// # Example
///
/// Compress data with the default compression mode:
/// ```
/// use lzzzz::lz4f;
///
/// let prefs = lz4f::Preferences::default();
/// let data = b"The quick brown fox jumps over the lazy dog.";
/// let mut buf = [0u8; 2048];
///
/// // The slice should have enough capacity.
/// assert!(buf.len() >= lz4f::max_compressed_size(data.len(), &prefs));
///
/// let len = lz4f::compress(data, &mut buf, &prefs)?;
/// let compressed = &buf[..len];
/// # let mut buf = Vec::new();
/// # lz4f::decompress_to_vec(compressed, &mut buf)?;
/// # assert_eq!(buf.as_slice(), &data[..]);
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn compress(src: &[u8], dst: &mut [u8], prefs: &Preferences) -> Result<usize> {
compress_to_ptr(src, dst.as_mut_ptr(), dst.len(), prefs)
}
fn compress_to_ptr(src: &[u8], dst: *mut u8, dst_len: usize, prefs: &Preferences) -> Result<usize> {
let mut prefs = *prefs;
if prefs.frame_info().content_size() > 0 {
prefs.set_content_size(src.len());
}
api::compress(src, dst, dst_len, &prefs)
}
/// Appends a compressed frame to `Vec<u8>`.
///
/// Returns the number of bytes appended to the given `Vec<u8>`.
///
/// # Example
///
/// Compress data with the default compression mode:
/// ```
/// use lzzzz::lz4f;
///
/// let prefs = lz4f::Preferences::default();
/// let data = b"The quick brown fox jumps over the lazy dog.";
/// let mut buf = Vec::new();
///
/// let len = lz4f::compress_to_vec(data, &mut buf, &prefs)?;
/// let compressed = &buf;
/// # let mut buf = Vec::new();
/// # lz4f::decompress_to_vec(compressed, &mut buf)?;
/// # assert_eq!(buf.as_slice(), &data[..]);
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn compress_to_vec(src: &[u8], dst: &mut Vec<u8>, prefs: &Preferences) -> Result<usize> {
let orig_len = dst.len();
dst.reserve(max_compressed_size(src.len(), prefs));
#[allow(unsafe_code)]
unsafe {
let result = compress_to_ptr(
src,
dst.as_mut_ptr().add(orig_len),
dst.capacity() - orig_len,
prefs,
);
dst.set_len(orig_len + result.as_ref().unwrap_or(&0));
result
}
}
/// Decompresses an LZ4 frame.
///
/// Returns the number of bytes appended to the given `Vec<u8>`.
///
/// # Example
///
/// ```
/// use lzzzz::lz4f;
///
/// const COMPRESSED_DATA: &str =
/// "BCJNGGBAgiwAAIBUaGUgcXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nLgAAAAA=";
///
/// let data = base64::decode(COMPRESSED_DATA).unwrap();
/// let mut buf = Vec::new();
///
/// lz4f::decompress_to_vec(&data[..], &mut buf)?;
///
/// assert_eq!(
/// &buf[..],
/// &b"The quick brown fox jumps over the lazy dog."[..]
/// );
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn decompress_to_vec(src: &[u8], dst: &mut Vec<u8>) -> Result<usize> {
let header_len = dst.len();
let mut src_offset = 0;
let mut dst_offset = header_len;
DecompressionCtx::with(|ctx| {
let mut ctx = ctx.borrow_mut();
ctx.reset();
loop {
dst.resize_with(dst.len() + DEFAULT_BUF_SIZE, Default::default);
match ctx.decompress_dict(&src[src_offset..], &mut dst[dst_offset..], &[], false) {
Ok((src_len, dst_len, expected)) => {
src_offset += src_len;
dst_offset += dst_len;
if expected == 0 {
dst.resize_with(dst_offset, Default::default);
return Ok(dst_offset - header_len);
} else if src_offset >= src.len() {
dst.resize_with(header_len, Default::default);
return Err(Error::new(ErrorKind::CompressedDataIncomplete).into());
}
}
Err(err) => {
dst.resize_with(header_len, Default::default);
return Err(err);
}
}
}
})
}
struct DecompressionCtx(RefCell<api::DecompressionContext>);
impl DecompressionCtx {
fn new() -> Self {
Self(RefCell::new(api::DecompressionContext::new().unwrap()))
}
fn with<F, R>(f: F) -> R
where
F: FnOnce(&RefCell<api::DecompressionContext>) -> R,
{
DECOMPRESSION_CTX.with(|state| (f)(state))
}
}
impl Deref for DecompressionCtx {
type Target = RefCell<api::DecompressionContext>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
thread_local!(static DECOMPRESSION_CTX: DecompressionCtx = DecompressionCtx::new());
use std::os::raw::{c_uint, c_ulonglong};
/// Block size flag.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
#[repr(C)]
pub enum BlockSize {
Default = 0,
Max64KB = 4,
Max256KB = 5,
Max1MB = 6,
Max4MB = 7,
}
impl Default for BlockSize {
fn default() -> Self {
Self::Default
}
}
/// Block mode flag.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[repr(C)]
pub enum BlockMode {
Linked,
Independent,
}
impl Default for BlockMode {
fn default() -> Self {
Self::Linked
}
}
/// Content checksum flag.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[repr(C)]
pub enum ContentChecksum {
Disabled,
Enabled,
}
impl Default for ContentChecksum {
fn default() -> Self {
Self::Disabled
}
}
/// Frame type flag.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[repr(C)]
pub enum FrameType {
Frame,
SkippableFrame,
}
impl Default for FrameType {
fn default() -> Self {
Self::Frame
}
}
/// Block checksum flag.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[repr(C)]
pub enum BlockChecksum {
Disabled,
Enabled,
}
impl Default for BlockChecksum {
fn default() -> Self {
Self::Disabled
}
}
/// LZ4 Frame parameters.
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
#[repr(C)]
pub struct FrameInfo {
block_size: BlockSize,
block_mode: BlockMode,
content_checksum: ContentChecksum,
frame_type: FrameType,
content_size: c_ulonglong,
dict_id: c_uint,
block_checksum: BlockChecksum,
}
impl FrameInfo {
/// Returns the block size.
pub const fn block_size(&self) -> BlockSize {
self.block_size
}
/// Returns the block mode.
pub const fn block_mode(&self) -> BlockMode {
self.block_mode
}
/// Returns the content checksum.
pub const fn content_checksum(&self) -> ContentChecksum {
self.content_checksum
}
/// Returns the frame type.
pub const fn frame_type(&self) -> FrameType {
self.frame_type
}
/// Returns the content size.
pub const fn content_size(&self) -> usize {
self.content_size as usize
}
/// Returns the dictionary id.
pub const fn dict_id(&self) -> u32 {
self.dict_id
}
/// Returns the block checksum.
pub const fn block_checksum(&self) -> BlockChecksum {
self.block_checksum
}
pub(super) fn set_block_size(&mut self, block_size: BlockSize) {
self.block_size = block_size;
}
pub(super) fn set_block_mode(&mut self, block_mode: BlockMode) {
self.block_mode = block_mode;
}
pub(super) fn set_content_checksum(&mut self, checksum: ContentChecksum) {
self.content_checksum = checksum;
}
pub(super) fn set_content_size(&mut self, size: usize) {
self.content_size = size as c_ulonglong;
}
pub(super) fn set_dict_id(&mut self, dict_id: u32) {
self.dict_id = dict_id as c_uint;
}
pub(super) fn set_block_checksum(&mut self, checksum: BlockChecksum) {
self.block_checksum = checksum;
}
}
//! LZ4F compression and decompression.
//!
//! LZ4F: LZ4 Frame Format.
mod api;
mod binding;
mod dictionary;
mod error;
mod frame;
mod frame_info;
mod preferences;
mod stream;
pub use dictionary::*;
pub use error::*;
pub use frame::*;
pub use frame_info::*;
pub use preferences::*;
pub use stream::{comp::*, decomp::*};
use super::frame_info::{BlockChecksum, BlockMode, BlockSize, ContentChecksum, FrameInfo};
use std::os::raw::{c_int, c_uint};
/// Predefined compression level (0).
pub const CLEVEL_DEFAULT: i32 = 0;
/// Predefined compression level (10).
pub const CLEVEL_HIGH: i32 = 10;
/// Predefined compression level (12).
pub const CLEVEL_MAX: i32 = 12;
/// Auto flush mode flag.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[repr(C)]
pub enum AutoFlush {
Disabled,
Enabled,
}
impl Default for AutoFlush {
fn default() -> Self {
Self::Disabled
}
}
/// Decompression speed mode flag.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[repr(C)]
pub enum FavorDecSpeed {
Disabled,
Enabled,
}
impl Default for FavorDecSpeed {
fn default() -> Self {
Self::Disabled
}
}
/// Compression preferences.
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
#[repr(C)]
pub struct Preferences {
frame_info: FrameInfo,
compression_level: c_int,
auto_flush: AutoFlush,
favor_dec_speed: FavorDecSpeed,
_reserved: [c_uint; 3],
}
impl Preferences {
/// Returns the frame info.
pub const fn frame_info(&self) -> FrameInfo {
self.frame_info
}
/// Returns the compression level.
pub const fn compression_level(&self) -> i32 {
self.compression_level
}
/// Returns the auto flush mode flag.
pub const fn auto_flush(&self) -> AutoFlush {
self.auto_flush
}
/// Returns the decompression speed mode flag.
pub const fn favor_dec_speed(&self) -> FavorDecSpeed {
self.favor_dec_speed
}
pub(super) fn set_block_size(&mut self, block_size: BlockSize) {
self.frame_info.set_block_size(block_size);
}
pub(super) fn set_block_mode(&mut self, block_mode: BlockMode) {
self.frame_info.set_block_mode(block_mode);
}
pub(super) fn set_content_checksum(&mut self, checksum: ContentChecksum) {
self.frame_info.set_content_checksum(checksum);
}
pub(super) fn set_content_size(&mut self, size: usize) {
self.frame_info.set_content_size(size);
}
pub(super) fn set_dict_id(&mut self, dict_id: u32) {
self.frame_info.set_dict_id(dict_id);
}
pub(super) fn set_block_checksum(&mut self, checksum: BlockChecksum) {
self.frame_info.set_block_checksum(checksum);
}
pub(super) fn set_compression_level(&mut self, level: i32) {
self.compression_level = level as c_int;
}
pub(super) fn set_favor_dec_speed(&mut self, dec_speed: FavorDecSpeed) {
self.favor_dec_speed = dec_speed;
}
pub(super) fn set_auto_flush(&mut self, auto_flush: AutoFlush) {
self.auto_flush = auto_flush;
}
}
/// Builds a custom `Preferences`.
///
/// # Example
///
/// ```
/// use lzzzz::lz4f::{BlockSize, PreferencesBuilder, CLEVEL_MAX};
///
/// let pref = PreferencesBuilder::new()
/// .block_size(BlockSize::Max1MB)
/// .compression_level(CLEVEL_MAX)
/// .build();
/// ```
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct PreferencesBuilder {
prefs: Preferences,
}
impl PreferencesBuilder {
/// Creates a new `PreferencesBuilder`.
pub fn new() -> Self {
Default::default()
}
/// Sets the block size.
pub fn block_size(&mut self, block_size: BlockSize) -> &mut Self {
self.prefs.set_block_size(block_size);
self
}
/// Sets the block mode.
pub fn block_mode(&mut self, block_mode: BlockMode) -> &mut Self {
self.prefs.set_block_mode(block_mode);
self
}
/// Sets the content checksum.
pub fn content_checksum(&mut self, checksum: ContentChecksum) -> &mut Self {
self.prefs.set_content_checksum(checksum);
self
}
/// Sets the content size.
///
/// A value greater than 0 enables the content size field in the frame header and
/// automatically replaced with an actual content size.
pub fn content_size(&mut self, size: usize) -> &mut Self {
self.prefs.set_content_size(size);
self
}
/// Sets the dictionary id.
pub fn dict_id(&mut self, dict_id: u32) -> &mut Self {
self.prefs.set_dict_id(dict_id);
self
}
/// Sets the block checksum.
pub fn block_checksum(&mut self, checksum: BlockChecksum) -> &mut Self {
self.prefs.set_block_checksum(checksum);
self
}
/// Sets the compression level.
pub fn compression_level(&mut self, level: i32) -> &mut Self {
self.prefs.set_compression_level(level);
self
}
/// Sets the decompression speed mode flag.
pub fn favor_dec_speed(&mut self, dec_speed: FavorDecSpeed) -> &mut Self {
self.prefs.set_favor_dec_speed(dec_speed);
self
}
/// Sets the auto flush mode flag.
pub fn auto_flush(&mut self, auto_flush: AutoFlush) -> &mut Self {
self.prefs.set_auto_flush(auto_flush);
self
}
/// Builds a `Preferences` with this configuration.
pub const fn build(&self) -> Preferences {
self.prefs
}
}
impl From<Preferences> for PreferencesBuilder {
fn from(prefs: Preferences) -> Self {
Self { prefs }
}
}
#[cfg(test)]
mod tests {
use crate::lz4f::{
BlockChecksum, BlockMode, BlockSize, ContentChecksum, FavorDecSpeed, Preferences,
PreferencesBuilder, CLEVEL_DEFAULT, CLEVEL_HIGH, CLEVEL_MAX,
};
use std::{i32, u32};
#[test]
fn preferences_builder() {
assert_eq!(PreferencesBuilder::new().build(), Preferences::default());
assert_eq!(
PreferencesBuilder::new()
.favor_dec_speed(FavorDecSpeed::Enabled)
.build()
.favor_dec_speed,
FavorDecSpeed::Enabled
);
assert_eq!(
PreferencesBuilder::new()
.block_size(BlockSize::Max64KB)
.build()
.frame_info
.block_size(),
BlockSize::Max64KB
);
assert_eq!(
PreferencesBuilder::new()
.block_size(BlockSize::Max256KB)
.build()
.frame_info
.block_size(),
BlockSize::Max256KB
);
assert_eq!(
PreferencesBuilder::new()
.block_size(BlockSize::Max1MB)
.build()
.frame_info
.block_size(),
BlockSize::Max1MB
);
assert_eq!(
PreferencesBuilder::new()
.block_size(BlockSize::Max4MB)
.build()
.frame_info
.block_size(),
BlockSize::Max4MB
);
assert_eq!(
PreferencesBuilder::new()
.content_checksum(ContentChecksum::Enabled)
.build()
.frame_info
.content_checksum(),
ContentChecksum::Enabled
);
assert_eq!(
PreferencesBuilder::new()
.block_mode(BlockMode::Independent)
.build()
.frame_info
.block_mode(),
BlockMode::Independent
);
assert_eq!(
PreferencesBuilder::new()
.compression_level(i32::MAX)
.build()
.compression_level,
i32::MAX
);
assert_eq!(
PreferencesBuilder::new()
.compression_level(CLEVEL_DEFAULT)
.build()
.compression_level,
CLEVEL_DEFAULT
);
assert_eq!(
PreferencesBuilder::new()
.compression_level(CLEVEL_HIGH)
.build()
.compression_level,
CLEVEL_HIGH
);
assert_eq!(
PreferencesBuilder::new()
.compression_level(CLEVEL_MAX)
.build()
.compression_level,
CLEVEL_MAX
);
assert_eq!(
PreferencesBuilder::new()
.compression_level(i32::MIN)
.build()
.compression_level,
i32::MIN
);
assert_eq!(
PreferencesBuilder::new()
.block_checksum(BlockChecksum::Enabled)
.build()
.frame_info
.block_checksum(),
BlockChecksum::Enabled
);
assert_eq!(
PreferencesBuilder::new()
.dict_id(u32::MAX)
.build()
.frame_info
.dict_id(),
u32::MAX
);
assert_eq!(
PreferencesBuilder::new()
.dict_id(u32::MIN)
.build()
.frame_info
.dict_id(),
u32::MIN
);
}
}
use super::{Compressor, Dictionary, Preferences};
use crate::lz4f::Result;
use std::{
fmt,
io::{BufRead, Read},
};
/// The [`BufRead`]-based streaming compressor.
///
/// # Example
///
/// ```
/// # use std::env;
/// # use std::path::Path;
/// # use lzzzz::{Error, Result};
/// # use assert_fs::prelude::*;
/// # let tmp_dir = assert_fs::TempDir::new().unwrap().into_persistent();
/// # env::set_current_dir(tmp_dir.path()).unwrap();
/// #
/// # tmp_dir.child("foo.txt").write_str("Hello").unwrap();
/// #
/// use lzzzz::lz4f::BufReadCompressor;
/// use std::{
/// fs::File,
/// io::{prelude::*, BufReader},
/// };
///
/// let mut f = File::open("foo.txt")?;
/// let mut b = BufReader::new(f);
/// let mut r = BufReadCompressor::new(&mut b, Default::default())?;
///
/// let mut buf = Vec::new();
/// r.read_to_end(&mut buf)?;
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html
pub struct BufReadCompressor<R: BufRead> {
pub(super) inner: R,
pub(super) comp: Compressor,
consumed: usize,
}
impl<R: BufRead> BufReadCompressor<R> {
/// Creates a new `BufReadCompressor<R>`.
pub fn new(reader: R, prefs: Preferences) -> Result<Self> {
Ok(Self {
inner: reader,
comp: Compressor::new(prefs, None)?,
consumed: 0,
})
}
/// Creates a new `BufReadCompressor<R>` with a dictionary.
pub fn with_dict(reader: R, prefs: Preferences, dict: Dictionary) -> Result<Self> {
Ok(Self {
inner: reader,
comp: Compressor::new(prefs, Some(dict))?,
consumed: 0,
})
}
/// Returns ownership of the reader.
pub fn into_inner(self) -> R {
self.inner
}
/// Returns a mutable reference to the reader.
pub fn get_mut(&mut self) -> &mut R {
&mut self.inner
}
/// Returns a shared reference to the reader.
pub fn get_ref(&self) -> &R {
&self.inner
}
}
impl<R> fmt::Debug for BufReadCompressor<R>
where
R: BufRead + fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("BufReadCompressor")
.field("reader", &self.inner)
.field("prefs", &self.comp.prefs())
.finish()
}
}
impl<R: BufRead> Read for BufReadCompressor<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let consumed = {
let inner_buf = self.inner.fill_buf()?;
if inner_buf.is_empty() {
self.comp.end(false)?;
if self.comp.buf().is_empty() {
return Ok(0);
}
0
} else {
self.comp.update(inner_buf, false)?;
inner_buf.len()
}
};
self.inner.consume(consumed);
let len = std::cmp::min(buf.len(), self.comp.buf().len() - self.consumed);
buf[..len].copy_from_slice(&self.comp.buf()[self.consumed..][..len]);
self.consumed += len;
if self.consumed >= self.comp.buf().len() {
self.comp.clear_buf();
self.consumed = 0;
}
Ok(len)
}
}
impl<R: BufRead> BufRead for BufReadCompressor<R> {
fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
let _ = self.read(&mut [])?;
Ok(&self.comp.buf()[self.consumed..])
}
fn consume(&mut self, amt: usize) {
self.consumed += amt;
if self.consumed >= self.comp.buf().len() {
self.comp.clear_buf();
self.consumed = 0;
}
}
}
//! Streaming LZ4F compressors.
mod bufread;
mod read;
mod write;
use crate::lz4f::Result;
pub use bufread::*;
pub use read::*;
pub use write::*;
use crate::lz4f::{
api::{CompressionContext, LZ4F_HEADER_SIZE_MAX},
Dictionary, Preferences,
};
pub(crate) struct Compressor {
ctx: CompressionContext,
prefs: Preferences,
state: State,
buffer: Vec<u8>,
}
impl Compressor {
pub fn new(prefs: Preferences, dict: Option<Dictionary>) -> Result<Self> {
Ok(Self {
ctx: CompressionContext::new(dict)?,
prefs,
state: State::Created,
buffer: Vec::with_capacity(LZ4F_HEADER_SIZE_MAX),
})
}
pub fn prefs(&self) -> &Preferences {
&self.prefs
}
fn begin(&mut self) -> Result<()> {
if let State::Created = self.state {
assert!(self.buffer.is_empty());
self.state = State::Active;
let len = self.ctx.begin(
self.buffer.as_mut_ptr(),
self.buffer.capacity(),
&self.prefs,
)?;
#[allow(unsafe_code)]
unsafe {
self.buffer.set_len(len);
}
}
Ok(())
}
pub fn update(&mut self, src: &[u8], stable_src: bool) -> Result<()> {
self.begin()?;
let ext_len = CompressionContext::compress_bound(src.len(), &self.prefs);
self.buffer.reserve(ext_len);
let offset = self.buffer.len();
#[allow(unsafe_code)]
unsafe {
let len = self.ctx.update(
self.buffer.as_mut_ptr().add(offset),
self.buffer.capacity() - offset,
src,
stable_src,
)?;
self.buffer.set_len(offset + len);
if len == 0 {
self.flush(stable_src)
} else {
Ok(())
}
}
}
pub fn flush(&mut self, stable_src: bool) -> Result<()> {
self.begin()?;
let ext_len = CompressionContext::compress_bound(0, &self.prefs);
self.buffer.reserve(ext_len);
let offset = self.buffer.len();
#[allow(unsafe_code)]
unsafe {
let len = self.ctx.flush(
self.buffer.as_mut_ptr().add(offset),
self.buffer.capacity() - offset,
stable_src,
)?;
self.buffer.set_len(offset + len);
}
Ok(())
}
pub fn end(&mut self, stable_src: bool) -> Result<()> {
self.begin()?;
if let State::Active = self.state {
self.state = State::Finished;
let ext_len = CompressionContext::compress_bound(0, &self.prefs);
self.buffer.reserve(ext_len);
let offset = self.buffer.len();
#[allow(unsafe_code)]
unsafe {
let len = self.ctx.end(
self.buffer.as_mut_ptr().add(offset),
self.buffer.capacity() - offset,
stable_src,
)?;
self.buffer.set_len(offset + len);
}
}
Ok(())
}
pub fn buf(&self) -> &[u8] {
&self.buffer
}
pub fn clear_buf(&mut self) {
self.buffer.clear();
}
}
pub(crate) enum State {
Created,
Active,
Finished,
}
use super::{BufReadCompressor, Dictionary, Preferences};
use crate::lz4f::Result;
use std::{
fmt,
io::{BufReader, Read},
};
/// The [`Read`]-based streaming compressor.
///
/// # Example
///
/// ```
/// # use std::env;
/// # use std::path::Path;
/// # use lzzzz::{Error, Result};
/// # use assert_fs::prelude::*;
/// # let tmp_dir = assert_fs::TempDir::new().unwrap().into_persistent();
/// # env::set_current_dir(tmp_dir.path()).unwrap();
/// #
/// # tmp_dir.child("foo.txt").write_str("Hello").unwrap();
/// #
/// use lzzzz::lz4f::ReadCompressor;
/// use std::{fs::File, io::prelude::*};
///
/// let mut f = File::open("foo.txt")?;
/// let mut r = ReadCompressor::new(&mut f, Default::default())?;
///
/// let mut buf = Vec::new();
/// r.read_to_end(&mut buf)?;
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
pub struct ReadCompressor<R: Read> {
inner: BufReadCompressor<BufReader<R>>,
}
impl<R: Read> ReadCompressor<R> {
/// Creates a new `ReadCompressor<R>`.
pub fn new(reader: R, prefs: Preferences) -> Result<Self> {
Ok(Self {
inner: BufReadCompressor::new(BufReader::new(reader), prefs)?,
})
}
/// Creates a new `ReadCompressor<R>` with a dictionary.
pub fn with_dict(reader: R, prefs: Preferences, dict: Dictionary) -> Result<Self> {
Ok(Self {
inner: BufReadCompressor::with_dict(BufReader::new(reader), prefs, dict)?,
})
}
/// Returns ownership of the reader.
pub fn into_inner(self) -> R {
self.inner.into_inner().into_inner()
}
/// Returns a mutable reference to the reader.
pub fn get_mut(&mut self) -> &mut R {
self.inner.get_mut().get_mut()
}
/// Returns a shared reference to the reader.
pub fn get_ref(&self) -> &R {
self.inner.get_ref().get_ref()
}
}
impl<R> fmt::Debug for ReadCompressor<R>
where
R: Read + fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("ReadCompressor")
.field("reader", &self.inner.inner.get_ref())
.field("prefs", &self.inner.comp.prefs())
.finish()
}
}
impl<R: Read> Read for ReadCompressor<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.inner.read(buf)
}
}
use super::{Compressor, Dictionary, Preferences};
use crate::lz4f::Result;
use std::{fmt, io::Write};
/// The [`Write`]-based streaming compressor.
///
/// # Example
///
/// ```
/// # use std::env;
/// # use std::path::Path;
/// # use lzzzz::{Error, Result};
/// # use assert_fs::prelude::*;
/// # let tmp_dir = assert_fs::TempDir::new().unwrap().into_persistent();
/// # env::set_current_dir(tmp_dir.path()).unwrap();
/// use lzzzz::lz4f::WriteCompressor;
/// use std::{fs::File, io::prelude::*};
///
/// let mut f = File::create("foo.lz4")?;
/// let mut w = WriteCompressor::new(&mut f, Default::default())?;
///
/// w.write_all(b"Hello world!")?;
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
pub struct WriteCompressor<W: Write> {
inner: Option<W>,
comp: Compressor,
}
impl<W: Write> WriteCompressor<W> {
/// Creates a new `WriteCompressor<W>`.
pub fn new(writer: W, prefs: Preferences) -> Result<Self> {
Ok(Self {
inner: Some(writer),
comp: Compressor::new(prefs, None)?,
})
}
/// Creates a new `WriteCompressor<W>` with a dictionary.
pub fn with_dict(writer: W, prefs: Preferences, dict: Dictionary) -> Result<Self> {
Ok(Self {
inner: Some(writer),
comp: Compressor::new(prefs, Some(dict))?,
})
}
/// Returns a mutable reference to the writer.
pub fn get_mut(&mut self) -> &mut W {
self.inner.as_mut().unwrap()
}
/// Returns a shared reference to the writer.
pub fn get_ref(&self) -> &W {
self.inner.as_ref().unwrap()
}
/// Returns the ownership of the writer, finishing the stream in the process.
pub fn into_inner(mut self) -> W {
let _ = self.end();
self.inner.take().unwrap()
}
fn end(&mut self) -> std::io::Result<()> {
if let Some(device) = &mut self.inner {
self.comp.end(false)?;
device.write_all(self.comp.buf())?;
self.comp.clear_buf();
device.flush()?;
}
Ok(())
}
}
impl<W> fmt::Debug for WriteCompressor<W>
where
W: Write + fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("WriteCompressor")
.field("writer", &self.inner)
.field("prefs", &self.comp.prefs())
.finish()
}
}
impl<W: Write> Write for WriteCompressor<W> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.comp.update(buf, false)?;
self.inner.as_mut().unwrap().write_all(self.comp.buf())?;
self.comp.clear_buf();
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
self.comp.flush(false)?;
self.inner.as_mut().unwrap().write_all(self.comp.buf())?;
self.comp.clear_buf();
self.inner.as_mut().unwrap().flush()
}
}
impl<W: Write> Drop for WriteCompressor<W> {
fn drop(&mut self) {
let _ = self.end();
}
}
use super::Decompressor;
use crate::lz4f::{FrameInfo, Result};
use std::{
borrow::Cow,
fmt,
io::{BufRead, Read},
};
/// The [`BufRead`]-based streaming decompressor.
///
/// # Example
///
/// ```
/// # use std::env;
/// # use std::path::Path;
/// # use lzzzz::{Error, Result};
/// # use assert_fs::prelude::*;
/// # let tmp_dir = assert_fs::TempDir::new().unwrap().into_persistent();
/// # env::set_current_dir(tmp_dir.path()).unwrap();
/// #
/// # let mut buf = Vec::new();
/// # lzzzz::lz4f::compress_to_vec(b"Hello world!", &mut buf, &Default::default())?;
/// # tmp_dir.child("foo.lz4").write_binary(&buf).unwrap();
/// #
/// use lzzzz::lz4f::BufReadDecompressor;
/// use std::{
/// fs::File,
/// io::{prelude::*, BufReader},
/// };
///
/// let mut f = File::open("foo.lz4")?;
/// let mut b = BufReader::new(f);
/// let mut r = BufReadDecompressor::new(&mut b)?;
///
/// let mut buf = Vec::new();
/// r.read_to_end(&mut buf)?;
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html
pub struct BufReadDecompressor<'a, R: BufRead> {
pub(super) inner: R,
decomp: Decompressor<'a>,
consumed: usize,
}
impl<'a, R: BufRead> BufReadDecompressor<'a, R> {
/// Creates a new `BufReadDecompressor<R>`.
pub fn new(reader: R) -> Result<Self> {
Ok(Self {
inner: reader,
decomp: Decompressor::new()?,
consumed: 0,
})
}
/// Sets the dictionary.
pub fn set_dict<D>(&mut self, dict: D)
where
D: Into<Cow<'a, [u8]>>,
{
self.decomp.set_dict(dict);
}
/// Reads the frame header and returns `FrameInfo`.
///
/// Calling this function before any `Read` or `BufRead` operations
/// does not consume the frame body.
pub fn read_frame_info(&mut self) -> std::io::Result<FrameInfo> {
loop {
if let Some(frame) = self.decomp.frame_info() {
return Ok(frame);
}
self.decomp.decode_header_only(true);
let _ = self.read(&mut [])?;
self.decomp.decode_header_only(false);
}
}
/// Returns ownership of the reader.
pub fn into_inner(self) -> R {
self.inner
}
/// Returns a mutable reference to the reader.
pub fn get_mut(&mut self) -> &mut R {
&mut self.inner
}
/// Returns a shared reference to the reader.
pub fn get_ref(&self) -> &R {
&self.inner
}
}
impl<R> fmt::Debug for BufReadDecompressor<'_, R>
where
R: BufRead + fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("BufReadDecompressor")
.field("reader", &self.inner)
.finish()
}
}
impl<R: BufRead> Read for BufReadDecompressor<'_, R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
loop {
let inner_buf = self.inner.fill_buf()?;
let consumed = self.decomp.decompress(inner_buf)?;
self.inner.consume(consumed);
if consumed == 0 {
break;
}
}
let len = std::cmp::min(buf.len(), self.decomp.buf().len() - self.consumed);
buf[..len].copy_from_slice(&self.decomp.buf()[self.consumed..][..len]);
self.consumed += len;
if self.consumed >= self.decomp.buf().len() {
self.decomp.clear_buf();
self.consumed = 0;
}
Ok(len)
}
}
impl<R: BufRead> BufRead for BufReadDecompressor<'_, R> {
fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
let _ = self.read(&mut [])?;
Ok(&self.decomp.buf()[self.consumed..])
}
fn consume(&mut self, amt: usize) {
self.consumed += amt;
if self.consumed >= self.decomp.buf().len() {
self.decomp.clear_buf();
self.consumed = 0;
}
}
}
//! Streaming LZ4F decompressors.
mod bufread;
mod read;
mod write;
pub use bufread::*;
pub use read::*;
pub use write::*;
use crate::{
common::DEFAULT_BUF_SIZE,
lz4f::{
api::{
header_size, DecompressionContext, LZ4F_HEADER_SIZE_MAX,
LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH,
},
FrameInfo, Result,
},
Error, ErrorKind,
};
use std::{borrow::Cow, cmp, pin::Pin, ptr};
#[derive(Clone, Copy, PartialEq)]
struct DictPtr(*const u8, usize);
#[allow(unsafe_code)]
unsafe impl Send for DictPtr {}
enum State {
Header {
header: [u8; LZ4F_HEADER_SIZE_MAX],
header_len: usize,
},
Body {
frame_info: FrameInfo,
comp_dict: Option<DictPtr>,
},
}
pub(crate) struct Decompressor<'a> {
ctx: DecompressionContext,
state: State,
buffer: Vec<u8>,
dict: Pin<Cow<'a, [u8]>>,
header_only: bool,
}
impl<'a> Decompressor<'a> {
pub fn new() -> Result<Self> {
Ok(Self {
ctx: DecompressionContext::new()?,
state: State::Header {
header: [0; LZ4F_HEADER_SIZE_MAX],
header_len: 0,
},
buffer: Vec::new(),
dict: Pin::new(Cow::Borrowed(&[])),
header_only: false,
})
}
pub fn set_dict<D>(&mut self, dict: D)
where
D: Into<Cow<'a, [u8]>>,
{
self.dict = Pin::new(dict.into());
}
pub fn frame_info(&self) -> Option<FrameInfo> {
if let State::Body { frame_info, .. } = self.state {
Some(frame_info)
} else {
None
}
}
pub fn decode_header_only(&mut self, flag: bool) {
self.header_only = flag;
}
pub fn decompress(&mut self, src: &[u8]) -> Result<usize> {
let mut header_consumed = 0;
if let State::Header {
ref mut header,
ref mut header_len,
} = &mut self.state
{
if *header_len < LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH {
let len = cmp::min(LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH - *header_len, src.len());
header[*header_len..*header_len + len].copy_from_slice(&src[..len]);
*header_len += len;
header_consumed += len;
}
if *header_len >= LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH {
let exact_header_len = header_size(&header[..*header_len]);
if exact_header_len > LZ4F_HEADER_SIZE_MAX {
return Err(Error::new(ErrorKind::FrameHeaderInvalid).into());
}
let src = &src[header_consumed..];
if *header_len < exact_header_len {
let len = cmp::min(exact_header_len - *header_len, src.len());
header[*header_len..*header_len + len].copy_from_slice(&src[..len]);
*header_len += len;
header_consumed += len;
}
if *header_len >= exact_header_len {
let (frame, rep) = self.ctx.get_frame_info(&header[..*header_len])?;
header_consumed = cmp::min(header_consumed, rep);
self.state = State::Body {
frame_info: frame,
comp_dict: None,
}
}
}
}
if let State::Header { header, header_len } = self.state {
if src.is_empty() {
self.ctx.get_frame_info(&header[..header_len])?;
}
}
if self.header_only {
return Ok(header_consumed);
}
let src = &src[header_consumed..];
let dict_ptr = self.dict_ptr();
if let State::Body {
ref mut comp_dict, ..
} = &mut self.state
{
if dict_ptr != *comp_dict.get_or_insert(dict_ptr) {
return Err(Error::new(ErrorKind::DictionaryChangedDuringDecompression).into());
}
let len = self.buffer.len();
if len < DEFAULT_BUF_SIZE {
self.buffer.resize_with(DEFAULT_BUF_SIZE, Default::default)
}
let (src_len, dst_len, _) =
self.ctx
.decompress_dict(src, &mut self.buffer[len..], &self.dict, false)?;
self.buffer.resize_with(len + dst_len, Default::default);
Ok(src_len + header_consumed)
} else {
Ok(header_consumed)
}
}
fn dict_ptr(&self) -> DictPtr {
let dict = &self.dict;
if dict.is_empty() {
DictPtr(ptr::null(), 0)
} else {
DictPtr(dict.as_ptr(), dict.len())
}
}
pub fn buf(&self) -> &[u8] {
&self.buffer
}
pub fn clear_buf(&mut self) {
self.buffer.clear();
}
}
use super::BufReadDecompressor;
use crate::lz4f::{FrameInfo, Result};
use std::{
borrow::Cow,
fmt,
io::{BufReader, Read},
};
/// The [`Read`]-based streaming decompressor.
///
/// # Example
///
/// ```
/// # use std::env;
/// # use std::path::Path;
/// # use lzzzz::{Error, Result};
/// # use assert_fs::prelude::*;
/// # let tmp_dir = assert_fs::TempDir::new().unwrap().into_persistent();
/// # env::set_current_dir(tmp_dir.path()).unwrap();
/// #
/// # let mut buf = Vec::new();
/// # lzzzz::lz4f::compress_to_vec(b"Hello world!", &mut buf, &Default::default())?;
/// # tmp_dir.child("foo.lz4").write_binary(&buf).unwrap();
/// #
/// use lzzzz::lz4f::ReadDecompressor;
/// use std::{fs::File, io::prelude::*};
///
/// let mut f = File::open("foo.lz4")?;
/// let mut r = ReadDecompressor::new(&mut f)?;
///
/// let mut buf = Vec::new();
/// r.read_to_end(&mut buf)?;
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
pub struct ReadDecompressor<'a, R: Read> {
inner: BufReadDecompressor<'a, BufReader<R>>,
}
impl<R> fmt::Debug for ReadDecompressor<'_, R>
where
R: Read + fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("ReadDecompressor")
.field("reader", &self.inner.inner.get_ref())
.finish()
}
}
impl<'a, R: Read> ReadDecompressor<'a, R> {
/// Creates a new `ReadDecompressor<R>`.
pub fn new(reader: R) -> Result<Self> {
Ok(Self {
inner: BufReadDecompressor::new(BufReader::new(reader))?,
})
}
/// Sets the dictionary.
pub fn set_dict<D>(&mut self, dict: D)
where
D: Into<Cow<'a, [u8]>>,
{
self.inner.set_dict(dict);
}
/// Reads the frame header and returns `FrameInfo`.
///
/// Calling this function before any `Read` operations
/// does not consume the frame body.
pub fn read_frame_info(&mut self) -> std::io::Result<FrameInfo> {
self.inner.read_frame_info()
}
/// Returns ownership of the reader.
pub fn into_inner(self) -> R {
self.inner.into_inner().into_inner()
}
/// Returns a mutable reference to the reader.
pub fn get_mut(&mut self) -> &mut R {
self.inner.get_mut().get_mut()
}
/// Returns a shared reference to the reader.
pub fn get_ref(&self) -> &R {
self.inner.get_ref().get_ref()
}
}
impl<R: Read> Read for ReadDecompressor<'_, R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.inner.read(buf)
}
}
use crate::lz4f::{Decompressor, FrameInfo, Result};
use std::{borrow::Cow, fmt, io::Write};
/// The [`Write`]-based streaming decompressor.
///
/// # Example
///
/// ```
/// # use std::env;
/// # use std::path::Path;
/// # use lzzzz::{Error, Result};
/// # use assert_fs::prelude::*;
/// # let tmp_dir = assert_fs::TempDir::new().unwrap().into_persistent();
/// # env::set_current_dir(tmp_dir.path()).unwrap();
/// use lzzzz::lz4f::{compress_to_vec, WriteDecompressor};
/// use std::{fs::File, io::prelude::*};
///
/// let mut f = File::create("foo.txt")?;
/// let mut w = WriteDecompressor::new(&mut f)?;
///
/// let mut buf = Vec::new();
/// compress_to_vec(b"Hello world!", &mut buf, &Default::default())?;
///
/// w.write_all(&buf)?;
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
pub struct WriteDecompressor<'a, W: Write> {
inner: W,
decomp: Decompressor<'a>,
}
impl<'a, W: Write> WriteDecompressor<'a, W> {
/// Creates a new `WriteDecompressor<W>`.
pub fn new(writer: W) -> Result<Self> {
Ok(Self {
inner: writer,
decomp: Decompressor::new()?,
})
}
/// Sets the dictionary.
pub fn set_dict<D>(&mut self, dict: D)
where
D: Into<Cow<'a, [u8]>>,
{
self.decomp.set_dict(dict);
}
/// Returns `FrameInfo` if the frame header is already decoded.
/// Otherwise, returns `None`.
pub fn frame_info(&self) -> Option<FrameInfo> {
self.decomp.frame_info()
}
/// Sets the 'header-only' mode.
///
/// When the 'header-only' mode is enabled, the decompressor doesn't
/// consume the frame body and `write()` always returns `Ok(0)`
/// if the frame header is already decoded.
pub fn decode_header_only(&mut self, flag: bool) {
self.decomp.decode_header_only(flag);
}
/// Returns a mutable reference to the writer.
pub fn get_mut(&mut self) -> &mut W {
&mut self.inner
}
/// Returns a shared reference to the writer.
pub fn get_ref(&self) -> &W {
&self.inner
}
/// Returns ownership of the writer.
pub fn into_inner(self) -> W {
self.inner
}
}
impl<W> fmt::Debug for WriteDecompressor<'_, W>
where
W: Write + fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("WriteDecompressor")
.field("writer", &self.inner)
.finish()
}
}
impl<W: Write> Write for WriteDecompressor<'_, W> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let report = self.decomp.decompress(buf)?;
self.inner.write_all(self.decomp.buf())?;
self.decomp.clear_buf();
Ok(report)
}
fn flush(&mut self) -> std::io::Result<()> {
self.inner.flush()
}
}
# make install artefact
liblz4.pc
LZ4 Library
Copyright (c) 2011-2020, Yann Collet
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ################################################################
# LZ4 library - Makefile
# Copyright (C) Yann Collet 2011-2023
# All rights reserved.
#
# This Makefile is validated for Linux, macOS, *BSD, Hurd, Solaris, MSYS2 targets
#
# BSD license
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# You can contact the author at :
# - LZ4 source repository : https://github.com/lz4/lz4
# - LZ4 forum froup : https://groups.google.com/forum/#!forum/lz4c
# ################################################################
SED ?= sed
# Version numbers
LIBVER_MAJOR_SCRIPT:=`$(SED) -n '/define[[:blank:]][[:blank:]]*LZ4_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./lz4.h`
LIBVER_MINOR_SCRIPT:=`$(SED) -n '/define[[:blank:]][[:blank:]]*LZ4_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./lz4.h`
LIBVER_PATCH_SCRIPT:=`$(SED) -n '/define[[:blank:]][[:blank:]]*LZ4_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./lz4.h`
LIBVER_SCRIPT:= $(LIBVER_MAJOR_SCRIPT).$(LIBVER_MINOR_SCRIPT).$(LIBVER_PATCH_SCRIPT)
LIBVER_MAJOR := $(shell echo $(LIBVER_MAJOR_SCRIPT))
LIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT))
LIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT))
LIBVER := $(shell echo $(LIBVER_SCRIPT))
BUILD_SHARED:=yes
BUILD_STATIC:=yes
CPPFLAGS+= -DXXH_NAMESPACE=LZ4_
USERCFLAGS:= -O3 $(CFLAGS)
DEBUGFLAGS:= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
-Wswitch-enum -Wdeclaration-after-statement -Wstrict-prototypes \
-Wundef -Wpointer-arith -Wstrict-aliasing=1
CFLAGS = $(DEBUGFLAGS) $(USERCFLAGS)
ALLFLAGS = $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
SRCFILES := $(sort $(wildcard *.c))
include ../Makefile.inc
# OS X linker doesn't support -soname, and use different extension
# see : https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/DynamicLibraryDesignGuidelines.html
ifeq ($(TARGET_OS), Darwin)
SHARED_EXT = dylib
SHARED_EXT_MAJOR = $(LIBVER_MAJOR).$(SHARED_EXT)
SHARED_EXT_VER = $(LIBVER).$(SHARED_EXT)
SONAME_FLAGS = -install_name $(libdir)/liblz4.$(SHARED_EXT_MAJOR) -compatibility_version $(LIBVER_MAJOR) -current_version $(LIBVER) -dynamiclib
else
ifeq ($(WINBASED),yes)
SHARED_EXT = dll
SHARED_EXT_VER = $(SHARED_EXT)
else # posix non-mac
SONAME_FLAGS = -Wl,-soname=liblz4.$(SHARED_EXT).$(LIBVER_MAJOR)
SHARED_EXT = so
SHARED_EXT_MAJOR = $(SHARED_EXT).$(LIBVER_MAJOR)
SHARED_EXT_VER = $(SHARED_EXT).$(LIBVER)
endif
endif
.PHONY: default
default: lib-release
# silent mode by default; verbose can be triggered by V=1 or VERBOSE=1
$(V)$(VERBOSE).SILENT:
.PHONY:lib-release
lib-release: DEBUGFLAGS :=
lib-release: lib liblz4.pc
.PHONY: lib
lib: liblz4.a liblz4
.PHONY: all
all: lib liblz4.pc
.PHONY: all32
all32: CFLAGS+=-m32
all32: all
CLEAN += liblz4.a
liblz4.a: $(SRCFILES)
ifeq ($(BUILD_STATIC),yes) # can be disabled on command line
@echo compiling static library
$(COMPILE.c) $^
$(AR) rcs $@ *.o
endif
ifeq ($(WINBASED),yes)
CLEAN += $(liblz4-dll.rc)
liblz4-dll.rc: liblz4-dll.rc.in
@echo creating library resource
$(SED) -e 's|@LIBLZ4@|$(LIBLZ4_NAME)|' \
-e 's|@LIBVER_MAJOR@|$(LIBVER_MAJOR)|g' \
-e 's|@LIBVER_MINOR@|$(LIBVER_MINOR)|g' \
-e 's|@LIBVER_PATCH@|$(LIBVER_PATCH)|g' \
$< >$@
liblz4-dll.o: liblz4-dll.rc
$(WINDRES) -i liblz4-dll.rc -o liblz4-dll.o
CLEAN += $(LIBLZ4_EXP)
$(LIBLZ4): $(SRCFILES) liblz4-dll.o
ifeq ($(BUILD_SHARED),yes)
@echo compiling dynamic library $(LIBVER)
$(CC) $(ALLFLAGS) -DLZ4_DLL_EXPORT=1 -shared $^ -o $@ -Wl,--out-implib,$(LIBLZ4_EXP)
endif
else # not windows
$(LIBLZ4): $(SRCFILES)
ifeq ($(BUILD_SHARED),yes)
@echo compiling dynamic library $(LIBVER)
$(CC) $(ALLFLAGS) -shared $^ -fPIC -fvisibility=hidden $(SONAME_FLAGS) -o $@
@echo creating versioned links
$(LN_SF) $@ liblz4.$(SHARED_EXT_MAJOR)
$(LN_SF) $@ liblz4.$(SHARED_EXT)
endif
endif
CLEAN += $(LIBLZ4)
.PHONY: liblz4
liblz4: $(LIBLZ4)
CLEAN += liblz4.pc
liblz4.pc: liblz4.pc.in Makefile
@echo creating pkgconfig
$(SED) -e 's|@PREFIX@|$(prefix)|' \
-e 's|@LIBDIR@|$(libdir)|' \
-e 's|@INCLUDEDIR@|$(includedir)|' \
-e 's|@VERSION@|$(LIBVER)|' \
-e 's|=${prefix}/|=$${prefix}/|' \
$< >$@
.PHONY: clean
clean:
ifeq ($(WINBASED),yes)
$(RM) *.rc
endif
$(RM) $(CLEAN) core *.o *.a
$(RM) *.$(SHARED_EXT) *.$(SHARED_EXT_MAJOR) *.$(SHARED_EXT_VER)
@echo Cleaning library completed
#-----------------------------------------------------------------------------
# make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets
#-----------------------------------------------------------------------------
ifeq ($(POSIX_ENV),Yes)
.PHONY: listL120
listL120: # extract lines >= 120 characters in *.{c,h}, by Takayuki Matsuoka (note : $$, for Makefile compatibility)
find . -type f -name '*.c' -o -name '*.h' | while read -r filename; do awk 'length > 120 {print FILENAME "(" FNR "): " $$0}' $$filename; done
DESTDIR ?=
# directory variables : GNU conventions prefer lowercase
# see https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html
# support both lower and uppercase (BSD), use lower in script
PREFIX ?= /usr/local
prefix ?= $(PREFIX)
EXEC_PREFIX ?= $(prefix)
exec_prefix ?= $(EXEC_PREFIX)
BINDIR ?= $(exec_prefix)/bin
bindir ?= $(BINDIR)
LIBDIR ?= $(exec_prefix)/lib
libdir ?= $(LIBDIR)
INCLUDEDIR ?= $(prefix)/include
includedir ?= $(INCLUDEDIR)
ifneq (,$(filter $(TARGET_OS),OpenBSD FreeBSD NetBSD DragonFly MidnightBSD))
PKGCONFIGDIR ?= $(prefix)/libdata/pkgconfig
else
PKGCONFIGDIR ?= $(libdir)/pkgconfig
endif
pkgconfigdir ?= $(PKGCONFIGDIR)
.PHONY: install
install: lib liblz4.pc
$(MAKE_DIR) $(DESTDIR)$(pkgconfigdir)/ $(DESTDIR)$(includedir)/ $(DESTDIR)$(libdir)/ $(DESTDIR)$(bindir)/
$(INSTALL_DATA) liblz4.pc $(DESTDIR)$(pkgconfigdir)/
@echo Installing libraries in $(DESTDIR)$(libdir)
ifeq ($(BUILD_STATIC),yes)
$(INSTALL_DATA) liblz4.a $(DESTDIR)$(libdir)/liblz4.a
$(INSTALL_DATA) lz4frame_static.h $(DESTDIR)$(includedir)/lz4frame_static.h
$(INSTALL_DATA) lz4file.h $(DESTDIR)$(includedir)/lz4file.h
endif
ifeq ($(BUILD_SHARED),yes)
# Traditionally, one installs the DLLs in the bin directory as programs
# search them first in their directory. This allows to not pollute system
# directories (like c:/windows/system32), nor modify the PATH variable.
ifeq ($(WINBASED),yes)
$(INSTALL_PROGRAM) $(LIBLZ4) $(DESTDIR)$(bindir)
$(INSTALL_PROGRAM) $(LIBLZ4_EXP) $(DESTDIR)$(libdir)
else
$(INSTALL_PROGRAM) liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir)
$(LN_SF) liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT_MAJOR)
$(LN_SF) liblz4.$(SHARED_EXT_VER) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT)
endif
endif
@echo Installing headers in $(DESTDIR)$(includedir)
$(INSTALL_DATA) lz4.h $(DESTDIR)$(includedir)/lz4.h
$(INSTALL_DATA) lz4hc.h $(DESTDIR)$(includedir)/lz4hc.h
$(INSTALL_DATA) lz4frame.h $(DESTDIR)$(includedir)/lz4frame.h
@echo lz4 libraries installed
.PHONY: uninstall
uninstall:
$(RM) $(DESTDIR)$(pkgconfigdir)/liblz4.pc
ifeq (WINBASED,yes)
$(RM) $(DESTDIR)$(bindir)/$(LIBLZ4)
$(RM) $(DESTDIR)$(libdir)/$(LIBLZ4_EXP)
else
$(RM) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT)
$(RM) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT_MAJOR)
$(RM) $(DESTDIR)$(libdir)/liblz4.$(SHARED_EXT_VER)
endif
$(RM) $(DESTDIR)$(libdir)/liblz4.a
$(RM) $(DESTDIR)$(includedir)/lz4.h
$(RM) $(DESTDIR)$(includedir)/lz4hc.h
$(RM) $(DESTDIR)$(includedir)/lz4frame.h
$(RM) $(DESTDIR)$(includedir)/lz4frame_static.h
$(RM) $(DESTDIR)$(includedir)/lz4file.h
@echo lz4 libraries successfully uninstalled
endif
LZ4 - Library Files
================================
The `/lib` directory contains many files, but depending on project's objectives,
not all of them are required.
Limited systems may want to reduce the nb of source files to include
as a way to reduce binary size and dependencies.
Capabilities are added at the "level" granularity, detailed below.
#### Level 1 : Minimal LZ4 build
The minimum required is **`lz4.c`** and **`lz4.h`**,
which provides the fast compression and decompression algorithms.
They generate and decode data using the [LZ4 block format].
#### Level 2 : High Compression variant
For more compression ratio at the cost of compression speed,
the High Compression variant called **lz4hc** is available.
Add files **`lz4hc.c`** and **`lz4hc.h`**.
This variant also compresses data using the [LZ4 block format],
and depends on regular `lib/lz4.*` source files.
#### Level 3 : Frame support, for interoperability
In order to produce compressed data compatible with `lz4` command line utility,
it's necessary to use the [official interoperable frame format].
This format is generated and decoded automatically by the **lz4frame** library.
Its public API is described in `lib/lz4frame.h`.
In order to work properly, lz4frame needs all other modules present in `/lib`,
including, lz4 and lz4hc, and also **xxhash**.
So it's necessary to also include `xxhash.c` and `xxhash.h`.
#### Level 4 : File compression operations
As a helper around file operations,
the library has been recently extended with `lz4file.c` and `lz4file.h`
(still considered experimental at the time of this writing).
These helpers allow opening, reading, writing, and closing files
using transparent LZ4 compression / decompression.
As a consequence, using `lz4file` adds a dependency on `<stdio.h>`.
`lz4file` relies on `lz4frame` in order to produce compressed data
conformant to the [LZ4 Frame format] specification.
Consequently, to enable this capability,
it's necessary to include all `*.c` and `*.h` files from `lib/` directory.
#### Advanced / Experimental API
Definitions which are not guaranteed to remain stable in future versions,
are protected behind macros, such as `LZ4_STATIC_LINKING_ONLY`.
As the name suggests, these definitions should only be invoked
in the context of static linking ***only***.
Otherwise, dependent application may fail on API or ABI break in the future.
The associated symbols are also not exposed by the dynamic library by default.
Should they be nonetheless needed, it's possible to force their publication
by using build macros `LZ4_PUBLISH_STATIC_FUNCTIONS`
and `LZ4F_PUBLISH_STATIC_FUNCTIONS`.
#### Build macros
The following build macro can be selected to adjust source code behavior at compilation time :
- `LZ4_FAST_DEC_LOOP` : this triggers a speed optimized decompression loop, more powerful on modern cpus.
This loop works great on `x86`, `x64` and `aarch64` cpus, and is automatically enabled for them.
It's also possible to enable or disable it manually, by passing `LZ4_FAST_DEC_LOOP=1` or `0` to the preprocessor.
For example, with `gcc` : `-DLZ4_FAST_DEC_LOOP=1`,
and with `make` : `CPPFLAGS+=-DLZ4_FAST_DEC_LOOP=1 make lz4`.
- `LZ4_DISTANCE_MAX` : control the maximum offset that the compressor will allow.
Set to 65535 by default, which is the maximum value supported by lz4 format.
Reducing maximum distance will reduce opportunities for LZ4 to find matches,
hence will produce a worse compression ratio.
Setting a smaller max distance could allow compatibility with specific decoders with limited memory budget.
This build macro only influences the compressed output of the compressor.
- `LZ4_DISABLE_DEPRECATE_WARNINGS` : invoking a deprecated function will make the compiler generate a warning.
This is meant to invite users to update their source code.
Should this be a problem, it's generally possible to make the compiler ignore these warnings,
for example with `-Wno-deprecated-declarations` on `gcc`,
or `_CRT_SECURE_NO_WARNINGS` for Visual Studio.
This build macro offers another project-specific method
by defining `LZ4_DISABLE_DEPRECATE_WARNINGS` before including the LZ4 header files.
- `LZ4_FORCE_SW_BITCOUNT` : by default, the compression algorithm tries to determine lengths
by using bitcount instructions, generally implemented as fast single instructions in many cpus.
In case the target cpus doesn't support it, or compiler intrinsic doesn't work, or feature bad performance,
it's possible to use an optimized software path instead.
This is achieved by setting this build macros.
In most cases, it's not expected to be necessary,
but it can be legitimately considered for less common platforms.
- `LZ4_ALIGN_TEST` : alignment test ensures that the memory area
passed as argument to become a compression state is suitably aligned.
This test can be disabled if it proves flaky, by setting this value to 0.
- `LZ4_USER_MEMORY_FUNCTIONS` : replace calls to `<stdlib.h>`'s `malloc()`, `calloc()` and `free()`
by user-defined functions, which must be named `LZ4_malloc()`, `LZ4_calloc()` and `LZ4_free()`.
User functions must be available at link time.
- `LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION` :
Remove support of dynamic memory allocation.
For more details, see description of this macro in `lib/lz4.c`.
- `LZ4_STATIC_LINKING_ONLY_ENDIANNESS_INDEPENDENT_OUTPUT` : experimental feature aimed at producing the same
compressed output on platforms of different endianness (i.e. little-endian and big-endian).
Output on little-endian platforms shall remain unchanged, while big-endian platforms will start producing
the same output as little-endian ones. This isn't expected to impact backward- and forward-compatibility
in any way.
- `LZ4_FREESTANDING` : by setting this build macro to 1,
LZ4/HC removes dependencies on the C standard library,
including allocation functions and `memmove()`, `memcpy()`, and `memset()`.
This build macro is designed to help use LZ4/HC in restricted environments
(embedded, bootloader, etc).
For more details, see description of this macro in `lib/lz4.h`.
- `LZ4_HEAPMODE` : Select how stateless compression functions like `LZ4_compress_default()`
allocate memory for their hash table,
in memory stack (0:default, fastest), or in memory heap (1:requires malloc()).
- `LZ4HC_HEAPMODE` : Select how stateless HC compression functions like `LZ4_compress_HC()`
allocate memory for their workspace:
in stack (0), or in heap (1:default).
Since workspace is rather large, stack can be inconvenient, hence heap mode is recommended.
- `LZ4F_HEAPMODE` : selects how `LZ4F_compressFrame()` allocates the compression state,
either on stack (default, value 0) or using heap memory (value 1).
#### Makefile variables
The following `Makefile` variables can be selected to alter the profile of produced binaries :
- `BUILD_SHARED` : generate `liblz4` dynamic library (enabled by default)
- `BUILD_STATIC` : generate `liblz4` static library (enabled by default)
#### Amalgamation
lz4 source code can be amalgamated into a single file.
One can combine all source code into `lz4_all.c` by using following command:
```
cat lz4.c lz4hc.c lz4frame.c > lz4_all.c
```
(`cat` file order is important) then compile `lz4_all.c`.
All `*.h` files present in `/lib` remain necessary to compile `lz4_all.c`.
#### Windows : using MinGW+MSYS to create DLL
DLL can be created using MinGW+MSYS with the `make liblz4` command.
This command creates `dll\liblz4.dll` and the import library `dll\liblz4.lib`.
To override the `dlltool` command when cross-compiling on Linux, just set the `DLLTOOL` variable. Example of cross compilation on Linux with mingw-w64 64 bits:
```
make BUILD_STATIC=no CC=x86_64-w64-mingw32-gcc DLLTOOL=x86_64-w64-mingw32-dlltool OS=Windows_NT
```
The import library is only required with Visual C++.
The header files `lz4.h`, `lz4hc.h`, `lz4frame.h` and the dynamic library
`dll\liblz4.dll` are required to compile a project using gcc/MinGW.
The dynamic library has to be added to linking options.
It means that if a project that uses LZ4 consists of a single `test-dll.c`
file it should be linked with `dll\liblz4.dll`. For example:
```
$(CC) $(CFLAGS) -Iinclude/ test-dll.c -o test-dll dll\liblz4.dll
```
The compiled executable will require LZ4 DLL which is available at `dll\liblz4.dll`.
#### Miscellaneous
Other files present in the directory are not source code. They are :
- `LICENSE` : contains the BSD license text
- `Makefile` : `make` script to compile and install lz4 library (static and dynamic)
- `liblz4.pc.in` : for `pkg-config` (used in `make install`)
- `README.md` : this file
[official interoperable frame format]: ../doc/lz4_Frame_format.md
[LZ4 Frame format]: ../doc/lz4_Frame_format.md
[LZ4 block format]: ../doc/lz4_Block_format.md
#### License
All source material within __lib__ directory are BSD 2-Clause licensed.
See [LICENSE](LICENSE) for details.
The license is also reminded at the top of each source file.
# ##########################################################################
# LZ4 programs - Makefile
# Copyright (C) Yann Collet 2016-2020
#
# GPL v2 License
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# You can contact the author at :
# - LZ4 homepage : http://www.lz4.org
# - LZ4 source repository : https://github.com/lz4/lz4
# ##########################################################################
VOID := /dev/null
LZ4DIR := ../include
LIBDIR := ../static
DLLDIR := ../dll
CFLAGS ?= -O3 # can select custom flags. For example : CFLAGS="-O2 -g" make
CFLAGS += -Wall -Wextra -Wundef -Wcast-qual -Wcast-align -Wshadow -Wswitch-enum \
-Wdeclaration-after-statement -Wstrict-prototypes \
-Wpointer-arith -Wstrict-aliasing=1
CFLAGS += $(MOREFLAGS)
CPPFLAGS:= -I$(LZ4DIR) -DXXH_NAMESPACE=LZ4_
FLAGS := $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
# Define *.exe as extension for Windows systems
ifneq (,$(filter Windows%,$(OS)))
EXT =.exe
else
EXT =
endif
.PHONY: default fullbench-dll fullbench-lib
default: all
all: fullbench-dll fullbench-lib
fullbench-lib: fullbench.c xxhash.c
$(CC) $(FLAGS) $^ -o $@$(EXT) $(LIBDIR)/liblz4_static.lib
fullbench-dll: fullbench.c xxhash.c
$(CC) $(FLAGS) $^ -o $@$(EXT) -DLZ4_DLL_IMPORT=1 $(DLLDIR)/liblz4.dll
clean:
@$(RM) fullbench-dll$(EXT) fullbench-lib$(EXT) \
@echo Cleaning completed
LZ4 Windows binary package
====================================
#### The package contents
- `lz4.exe` : Command Line Utility, supporting gzip-like arguments
- `dll\msys-lz4-1.dll` : The DLL of LZ4 library, compiled by msys
- `dll\liblz4.dll.a` : The import library of LZ4 library for Visual C++
- `example\` : The example of usage of LZ4 library
- `include\` : Header files required with LZ4 library
- `static\liblz4_static.lib` : The static LZ4 library
#### Usage of Command Line Interface
Command Line Interface (CLI) supports gzip-like arguments.
By default CLI takes an input file and compresses it to an output file:
```
Usage: lz4 [arg] [input] [output]
```
The full list of commands for CLI can be obtained with `-h` or `-H`. The ratio can
be improved with commands from `-3` to `-16` but higher levels also have slower
compression. CLI includes in-memory compression benchmark module with compression
levels starting from `-b` and ending with `-e` with iteration time of `-i` seconds.
CLI supports aggregation of parameters i.e. `-b1`, `-e18`, and `-i1` can be joined
into `-b1e18i1`.
#### The example of usage of static and dynamic LZ4 libraries with gcc/MinGW
Use `cd example` and `make` to build `fullbench-dll` and `fullbench-lib`.
`fullbench-dll` uses a dynamic LZ4 library from the `dll` directory.
`fullbench-lib` uses a static LZ4 library from the `lib` directory.
#### Using LZ4 DLL with gcc/MinGW
The header files from `include\` and the dynamic library `dll\msys-lz4-1.dll`
are required to compile a project using gcc/MinGW.
The dynamic library has to be added to linking options.
It means that if a project that uses LZ4 consists of a single `test-dll.c`
file it should be linked with `dll\msys-lz4-1.dll`. For example:
```
gcc $(CFLAGS) -Iinclude\ test-dll.c -o test-dll dll\msys-lz4-1.dll
```
The compiled executable will require LZ4 DLL which is available at `dll\msys-lz4-1.dll`.
#### The example of usage of static and dynamic LZ4 libraries with Visual C++
Open `example\fullbench-dll.sln` to compile `fullbench-dll` that uses a
dynamic LZ4 library from the `dll` directory. The solution works with Visual C++
2010 or newer. When one will open the solution with Visual C++ newer than 2010
then the solution will be upgraded to the current version.
#### Using LZ4 DLL with Visual C++
The header files from `include\` and the import library `dll\liblz4.dll.a`
are required to compile a project using Visual C++.
1. The header files should be added to `Additional Include Directories` that can
be found in project properties `C/C++` then `General`.
2. The import library has to be added to `Additional Dependencies` that can
be found in project properties `Linker` then `Input`.
If one will provide only the name `liblz4.dll.a` without a full path to the library
the directory has to be added to `Linker\General\Additional Library Directories`.
The compiled executable will require LZ4 DLL which is available at `dll\msys-lz4-1.dll`.
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Express 2012 for Windows Desktop
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fullbench-dll", "fullbench-dll.vcxproj", "{13992FD2-077E-4954-B065-A428198201A9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Debug|x64 = Debug|x64
Release|Win32 = Release|Win32
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{13992FD2-077E-4954-B065-A428198201A9}.Debug|Win32.ActiveCfg = Debug|Win32
{13992FD2-077E-4954-B065-A428198201A9}.Debug|Win32.Build.0 = Debug|Win32
{13992FD2-077E-4954-B065-A428198201A9}.Debug|x64.ActiveCfg = Debug|x64
{13992FD2-077E-4954-B065-A428198201A9}.Debug|x64.Build.0 = Debug|x64
{13992FD2-077E-4954-B065-A428198201A9}.Release|Win32.ActiveCfg = Release|Win32
{13992FD2-077E-4954-B065-A428198201A9}.Release|Win32.Build.0 = Release|Win32
{13992FD2-077E-4954-B065-A428198201A9}.Release|x64.ActiveCfg = Release|x64
{13992FD2-077E-4954-B065-A428198201A9}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
This diff is collapsed.
# LZ4 - Fast LZ compression algorithm
# Copyright (C) 2011-2020, Yann Collet.
# BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
prefix=@PREFIX@
libdir=@LIBDIR@
includedir=@INCLUDEDIR@
Name: lz4
Description: extremely fast lossless compression algorithm library
URL: http://www.lz4.org/
Version: @VERSION@
Libs: -L${libdir} -llz4
Cflags: -I${includedir}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment