Skip to main content

lzma_turbo/
lib.rs

1//! LZMA and LZMA2 decoding, ported from the 7-Zip reference decoder.
2//!
3//! The decoder in this crate is a port of Igor Pavlov's `LzmaDec.c` and
4//! `Lzma2Dec.c` from the LZMA SDK (public domain), kept structurally faithful
5//! to the original so that its speed carries over: one large decode loop with
6//! the range coder, probability base and dictionary pointers held in locals,
7//! limits checked once per symbol under a caller-guaranteed margin, and a
8//! separate careful path for the bytes near buffer edges.
9//!
10//! The encoder is a port of the same SDK's `LzFind.c`, `LzmaEnc.c` and
11//! `Lzma2Enc.c`, and is bit-exact with it. See `docs/encoder.md`.
12//!
13//! # Example
14//!
15//! The reader adapters need `std`; without it the example below cannot run, so
16//! it is compiled only when the `std` feature is on.
17//!
18#![cfg_attr(feature = "std", doc = "```no_run")]
19#![cfg_attr(not(feature = "std"), doc = "```ignore")]
20//! use std::fs::File;
21//! use std::io::Read;
22//! use lzma_turbo::LzmaReader;
23//!
24//! # fn main() -> std::io::Result<()> {
25//! let mut out = Vec::new();
26//! LzmaReader::new(File::open("archive.lzma")?)?.read_to_end(&mut out)?;
27//! # Ok(())
28//! # }
29//! ```
30//!
31//! # Provenance
32//!
33//! Derived from `C/LzmaDec.c` and `C/Lzma2Dec.c` of the LZMA SDK, which are in
34//! the public domain. Each ported function names its C counterpart in a
35//! comment.
36
37#![cfg_attr(not(feature = "std"), no_std)]
38#![forbid(unsafe_op_in_unsafe_fn)]
39#![warn(missing_docs, rust_2018_idioms)]
40
41extern crate alloc;
42
43#[cfg(feature = "std")]
44extern crate std;
45
46mod buffer;
47#[cfg(feature = "enc")]
48mod enc;
49mod error;
50#[cfg(feature = "filters")]
51pub mod filters;
52#[cfg(feature = "kernel-ab")]
53mod kernel_ab;
54mod lzma;
55mod lzma2;
56mod lzma_alone;
57
58#[cfg(feature = "std")]
59mod mt;
60#[cfg(feature = "std")]
61mod reader;
62#[cfg(feature = "std")]
63pub mod run;
64
65#[cfg(feature = "xz")]
66pub mod xz;
67
68#[cfg(feature = "enc")]
69pub use enc::{
70    BLOCK_SIZE_AUTO, BLOCK_SIZE_SOLID, LZMA_MATCH_LEN_MAX, LZMA_MATCH_LEN_MIN, Lzma2Encoder,
71    Lzma2PushEncoder, Lzma2Writer, LzmaEncProps, LzmaEncoder, LzmaPushEncoder, LzmaWriter,
72    MatchFinderKind, NormalizedProps, SeqInStream, SeqOutStream, SliceStream, auto_block_size,
73    encode_lzma_alone, encode_lzma2, encode_lzma2_mt,
74};
75/// The `.xz` writer, behind the `xz` feature.
76#[cfg(all(feature = "enc", feature = "xz"))]
77pub use enc::{
78    DEFAULT_BLOCK_SIZE, XzEncoder, XzWriter, encode_xz, encode_xz_mt, encode_xz_with_filters,
79};
80pub use error::{Error, FinishMode, Progress, Status, XzErrorKind};
81pub use lzma::consts::{LZMA_PROPS_SIZE, LZMA_REQUIRED_INPUT_MAX};
82pub use lzma::{LzmaDecoder, LzmaProps};
83pub use lzma_alone::{LZMA_ALONE_HEADER_SIZE, LzmaAloneHeader};
84pub use lzma2::Lzma2Decoder;
85#[cfg(feature = "std")]
86pub use lzma2::scan::run_boundaries;
87pub use lzma2::scan::{Lzma2Run, Lzma2RunChunks, Lzma2RunScanner};
88
89#[cfg(feature = "std")]
90pub use mt::adaptive::{AdaptiveLedger, DrainStatus, Lzma2AdaptiveDecoder};
91#[cfg(all(feature = "std", feature = "crc"))]
92pub use mt::checksum;
93#[cfg(all(feature = "std", feature = "crc"))]
94pub use mt::checksum::{BlockChecks, Checksum, ChecksumPlan, Segment, SegmentCheck};
95#[cfg(feature = "std")]
96pub use mt::{Lzma2MtOptions, Lzma2ParallelDecoder, Lzma2ParallelReader, mt_memory_estimate};
97#[cfg(feature = "std")]
98pub use reader::{Lzma2Reader, LzmaReader};
99#[cfg(feature = "xz")]
100pub use xz::{XzAdaptiveDecoder, XzError, XzOptions, XzParallelReader, XzReader};
101
102#[cfg(feature = "crc")]
103pub mod crc;
104#[cfg(any(feature = "crypto", feature = "native-crypto"))]
105pub mod crypto;
106#[cfg(any(feature = "crc-host", feature = "crypto-host"))]
107pub mod hooks;
108
109/// Whether this build decodes with the assembly loop ported from the LZMA
110/// SDK's `Asm/` tree rather than with the portable Rust port of the C loop.
111/// False without the `asm` feature and on every target that has no such loop.
112pub const ASM_LOOP: bool = lzma::decode_opt::ENABLED;
113
114/// Crate version, for consumers that record which decoder produced an output.
115pub const VERSION: &str = env!("CARGO_PKG_VERSION");