Skip to main content

lz4/
lib.rs

1//! `lz4r` — a pure-Rust implementation of the LZ4 compression algorithms and
2//! command-line programs (equivalent to the `programs/` tree of LZ4 v1.10.0).
3//!
4//! # Crate layout
5//!
6//! | Module       | Contents |
7//! |--------------|----------|
8//! | `block`      | Low-level block compression and decompression (LZ4 spec §§ 1–3). |
9//! | `frame`      | LZ4 Frame format (magic number, header, content checksum). |
10//! | `hc`         | High-compression (`lz4hc`) encoder variants. |
11//! | `io`         | File-level I/O: compress / decompress single and multiple files. |
12//! | `file`       | Streaming `Read`/`Write` wrappers over the Frame API. |
13//! | `cli`        | Command-line argument parsing and dispatch. |
14//! | `bench`      | Throughput benchmarking infrastructure. |
15//! | `xxhash`     | XXH32 content-checksum wrapper. |
16//! | `lorem`      | Deterministic lorem ipsum generator (benchmark corpus). |
17//! | `timefn`     | Monotonic high-resolution timer. |
18//! | `threadpool` | Fixed-size work-stealing thread pool. |
19//! | `config`     | Compile-time configuration constants. |
20//! | `util`       | File enumeration and sizing utilities. |
21
22pub mod config;
23pub mod lorem;
24pub mod timefn;
25
26#[cfg(feature = "c-abi")]
27pub mod abi;
28pub mod bench;
29pub mod block;
30pub mod cli;
31pub mod file;
32pub mod frame;
33pub mod hc;
34pub mod io;
35pub mod threadpool;
36pub mod util;
37pub mod xxhash;
38
39// ── Version constants (mirrors lz4.h lines 131–143) ──────────────────────────
40pub const LZ4_VERSION_MAJOR: u32 = 1;
41pub const LZ4_VERSION_MINOR: u32 = 10;
42pub const LZ4_VERSION_RELEASE: u32 = 0;
43pub const LZ4_VERSION_NUMBER: u32 =
44    LZ4_VERSION_MAJOR * 100 * 100 + LZ4_VERSION_MINOR * 100 + LZ4_VERSION_RELEASE;
45pub const LZ4_VERSION_STRING: &str = "1.10.0";
46/// Git commit hash string injected at build time via the `LZ4_GIT_COMMIT`
47/// environment variable.  Empty when the variable is not set.
48pub const LZ4_GIT_COMMIT_STRING: &str = "";
49
50/// Returns the runtime version number (equivalent to LZ4_versionNumber()).
51pub fn version_number() -> u32 {
52    LZ4_VERSION_NUMBER
53}
54
55/// Returns the runtime version string (equivalent to LZ4_versionString()).
56pub fn version_string() -> &'static str {
57    LZ4_VERSION_STRING
58}
59
60// ── Distance / inplace constants ──────────────────────────────────────────────
61pub const LZ4_DISTANCE_MAX: usize = 65_535;
62pub const COMPRESS_INPLACE_MARGIN: usize = LZ4_DISTANCE_MAX + 32;
63
64/// Returns the size in bytes of the internal stream state (LZ4_sizeofState()).
65pub fn size_of_state() -> i32 {
66    core::mem::size_of::<block::types::StreamStateInternal>() as i32
67}
68
69/// Margin required for in-place decompression (lz4.h line 670).
70pub fn decompress_inplace_margin(compressed_size: usize) -> usize {
71    (compressed_size >> 8) + 32
72}
73
74/// Minimum buffer size for in-place decompression (lz4.h line 672).
75pub fn decompress_inplace_buffer_size(decompressed_size: usize) -> usize {
76    decompressed_size + decompress_inplace_margin(decompressed_size)
77}
78
79/// Minimum buffer size for in-place compression (lz4.h line 678).
80pub fn compress_inplace_buffer_size(max_compressed_size: usize) -> usize {
81    max_compressed_size + COMPRESS_INPLACE_MARGIN
82}
83
84// ── Top-level re-exports ──────────────────────────────────────────────────────
85pub use block::compress::compress_default as lz4_compress_default;
86pub use block::decompress_api::decompress_safe as lz4_decompress_safe;
87
88// Block API — one-shot compression (needed by e2e tests)
89pub use block::{
90    compress_bound, compress_dest_size, compress_fast, decompress_safe_partial,
91    decompress_safe_using_dict, Lz4Error, LZ4_ACCELERATION_DEFAULT, LZ4_ACCELERATION_MAX,
92    LZ4_MAX_INPUT_SIZE,
93};
94
95// Error types
96pub use block::decompress_core::DecompressError;
97
98// Frame API convenience re-exports
99pub use frame::{lz4f_compress_frame, lz4f_decompress};