Skip to main content

libzstd_rs_sys/
c2rust-lib.rs

1//! zstd, short for *Zstandard*, is a fast lossless compression algorithm, targeting real-time
2//! compression scenarios at zlib-level and better compression ratios. The zstd compression library
3//! provides in-memory compression and decompression functions.
4//!
5//! The library supports regular compression levels from 1 up to [`ZSTD_maxCLevel`], which is
6//! currently 22. Levels >= 20, labeled `--ultra`, should be used with caution, as they require
7//! more memory. The library also offers negative compression levels, which extend the range of
8//! speed vs. ratio preferences. The lower the level, the faster the speed (at the cost of
9//! compression).
10//!
11//! (De-)compression can be done in:
12//!   - a single step (described as *Simple API*) using [`ZSTD_compress`] and [`ZSTD_decompress`]
13//!   - a single step, reusing a context (described as *Explicit context*) using
14//!     [`ZSTD_createCCtx`] and [`ZSTD_createDCtx`] to create the context, and using
15//!     [`ZSTD_compressCCtx`] and [`ZSTD_decompressDCtx`] to (de-)compress
16//!   - unbounded multiple steps (described as *Streaming compression*), using
17//!     [`ZSTD_createCStream`] and [`ZSTD_createDStream`] to create a stream, [`ZSTD_initCStream`]
18//!     and [`ZSTD_initDStream`] to (re-)initialize the stream for (de)-compression, and
19//!     [`ZSTD_compressStream2`] and [`ZSTD_decompressStream`] to consume input
20//!
21//! The compression ratio achievable on small data can be highly improved using a dictionary.
22//! Dictionary compression can be performed in:
23//!   - a single step (described as *Simple dictionary API*), using [`ZSTD_compress_usingDict`] and
24//!     [`ZSTD_decompress_usingDict`]
25//!   - a single step, reusing a dictionary (described as *Bulk-processing dictionary API*), using
26//!     [`ZSTD_createCDict`] and [`ZSTD_createDDict`] to create a dictionary, and using
27//!     [`ZSTD_compress_usingCDict`] and [`ZSTD_decompress_usingDDict`] to (de-)compress using the
28//!     dictionary
29//!
30//! Advanced experimental APIs should never be used with a dynamically-linked library. They are not
31//! "stable"; their definitions or signatures may change in the future. Only static linking is
32//! allowed.
33
34#![allow(non_camel_case_types)]
35#![allow(non_snake_case)]
36#![allow(non_upper_case_globals)]
37#![allow(unused_assignments)]
38#![allow(clippy::too_many_arguments)]
39#![cfg_attr(all(feature = "nightly", test), feature(test))]
40#![cfg_attr(feature = "nightly", feature(likely_unlikely))]
41#![cfg_attr(feature = "nightly", feature(cold_path))]
42#![cfg_attr(feature = "nightly", feature(hint_prefetch))]
43// FIXME
44#![allow(clippy::missing_safety_doc)]
45
46pub mod lib {
47    pub mod common {
48        pub(crate) mod allocations;
49        pub(crate) mod bits;
50        pub(crate) mod bitstream;
51        pub mod debug;
52        pub(crate) mod entropy_common;
53        pub(crate) mod error_private;
54        pub(crate) mod fse;
55        pub(crate) mod fse_decompress;
56        pub mod huf;
57        pub(crate) mod mem;
58        pub(crate) mod pool;
59        pub(crate) mod reader;
60        pub(crate) mod xxhash;
61        pub mod zstd_common;
62        pub(crate) mod zstd_internal;
63        pub mod zstd_trace;
64    } // mod common
65    pub mod compress {
66        // FIXME
67        #![allow(clippy::wildcard_in_or_patterns)]
68        #![allow(clippy::if_same_then_else)]
69        #![allow(clippy::collapsible_if)]
70        #![allow(clippy::eq_op)]
71        pub mod fse_compress;
72        pub mod hist;
73        pub mod huf_compress;
74        pub mod zstd_compress;
75        pub(crate) mod zstd_compress_internal;
76        pub mod zstd_compress_literals;
77        pub mod zstd_compress_sequences;
78        pub mod zstd_compress_superblock;
79        pub mod zstd_double_fast;
80        pub mod zstd_fast;
81        pub mod zstd_lazy;
82        pub mod zstd_ldm;
83        pub mod zstd_opt;
84        pub mod zstd_preSplit;
85        mod zstdmt_compress;
86    } // mod compress
87    pub mod decompress;
88    pub(crate) mod polyfill;
89    pub(crate) mod dictBuilder {
90        #![allow(clippy::collapsible_if)]
91        pub(crate) mod cover;
92        pub(crate) mod divsufsort;
93        pub(crate) mod fastcover;
94        pub(crate) mod zdict;
95    } // mod dictBuilder
96    pub(crate) mod legacy {
97        pub(crate) mod zstd_v05;
98        pub(crate) mod zstd_v06;
99        pub(crate) mod zstd_v07;
100    } // mod legacy
101    pub mod zdict;
102    pub mod zstd;
103} // mod lib
104
105pub use crate::lib::zstd::{
106    ZSTD_ParamSwitch_e, ZSTD_ResetDirective, ZSTD_cParameter, ZSTD_dParameter,
107    ZSTD_dictAttachPref_e, ZSTD_format_e, ZSTD_frameProgression, ZSTD_inBuffer, ZSTD_outBuffer,
108    ZSTD_strategy, ZSTD_BLOCKSIZELOG_MAX, ZSTD_BLOCKSIZE_MAX, ZSTD_BLOCKSIZE_MAX_MIN,
109    ZSTD_CLEVEL_DEFAULT, ZSTD_CONTENTSIZE_ERROR, ZSTD_CONTENTSIZE_UNKNOWN,
110    ZSTD_FRAMEHEADERSIZE_MAX, ZSTD_MAGICNUMBER, ZSTD_MAGIC_DICTIONARY, ZSTD_MAGIC_SKIPPABLE_MASK,
111    ZSTD_MAGIC_SKIPPABLE_START, ZSTD_SKIPPABLEHEADERSIZE, ZSTD_VERSION_MAJOR, ZSTD_VERSION_MINOR,
112    ZSTD_VERSION_NUMBER, ZSTD_VERSION_RELEASE, ZSTD_WINDOWLOG_LIMIT_DEFAULT, ZSTD_WINDOWLOG_MAX_32,
113    ZSTD_WINDOWLOG_MAX_64,
114};
115
116pub use crate::lib::decompress::{
117    zstd_ddict::{
118        ZSTD_DDict, ZSTD_createDDict, ZSTD_createDDict_byReference, ZSTD_estimateDDictSize,
119        ZSTD_freeDDict, ZSTD_getDictID_fromDDict, ZSTD_sizeof_DDict,
120    },
121    zstd_decompress::{
122        ZSTD_DCtx_getParameter, ZSTD_DCtx_loadDictionary, ZSTD_DCtx_refDDict, ZSTD_DCtx_refPrefix,
123        ZSTD_DCtx_reset, ZSTD_DCtx_setParameter, ZSTD_DStream, ZSTD_DStreamInSize,
124        ZSTD_DStreamOutSize, ZSTD_copyDCtx, ZSTD_createDCtx, ZSTD_createDStream,
125        ZSTD_dParam_getBounds, ZSTD_decompress, ZSTD_decompressBegin,
126        ZSTD_decompressBegin_usingDDict, ZSTD_decompressBegin_usingDict, ZSTD_decompressBound,
127        ZSTD_decompressContinue, ZSTD_decompressDCtx, ZSTD_decompressStream,
128        ZSTD_decompressStream_simpleArgs, ZSTD_decompress_usingDDict, ZSTD_decompress_usingDict,
129        ZSTD_decompressionMargin, ZSTD_findDecompressedSize, ZSTD_findFrameCompressedSize,
130        ZSTD_freeDCtx, ZSTD_getDecompressedSize, ZSTD_getDictID_fromDict, ZSTD_getDictID_fromFrame,
131        ZSTD_getFrameContentSize, ZSTD_initDStream, ZSTD_initDStream_usingDDict,
132        ZSTD_initDStream_usingDict, ZSTD_insertBlock, ZSTD_isFrame, ZSTD_isSkippableFrame,
133        ZSTD_nextSrcSizeToDecompress, ZSTD_sizeof_DCtx,
134    },
135    zstd_decompress_block::ZSTD_decompressBlock,
136    ZSTD_DCtx,
137};
138
139pub use crate::lib::common::zstd_common::{
140    ZSTD_getErrorName, ZSTD_isError, ZSTD_versionNumber, ZSTD_versionString,
141};
142
143pub use crate::lib::dictBuilder::cover::{
144    ZDICT_optimizeTrainFromBuffer_cover, ZDICT_trainFromBuffer_cover,
145};
146pub use crate::lib::dictBuilder::fastcover::{
147    ZDICT_optimizeTrainFromBuffer_fastCover, ZDICT_trainFromBuffer_fastCover,
148};
149pub use crate::lib::dictBuilder::zdict::ZDICT_trainFromBuffer_legacy;
150pub use crate::lib::zdict::{
151    experimental::{ZDICT_cover_params_t, ZDICT_fastCover_params_t, ZDICT_legacy_params_t},
152    ZDICT_getDictID, ZDICT_getErrorName, ZDICT_isError, ZDICT_params_t, ZDICT_trainFromBuffer,
153};
154
155pub use crate::lib::compress::zstd_compress::{
156    ZSTD_CCtx, ZSTD_CCtx_getParameter, ZSTD_CCtx_loadDictionary, ZSTD_CCtx_refCDict,
157    ZSTD_CCtx_refPrefix, ZSTD_CCtx_reset, ZSTD_CCtx_setParameter, ZSTD_CCtx_setPledgedSrcSize,
158    ZSTD_CDict, ZSTD_CStreamInSize, ZSTD_CStreamOutSize, ZSTD_EndDirective, ZSTD_cParam_getBounds,
159    ZSTD_compress, ZSTD_compress2, ZSTD_compressBlock, ZSTD_compressBound, ZSTD_compressCCtx,
160    ZSTD_compressStream, ZSTD_compressStream2, ZSTD_compress_usingCDict, ZSTD_compress_usingDict,
161    ZSTD_copyCCtx, ZSTD_createCCtx, ZSTD_createCDict, ZSTD_createCDict_byReference,
162    ZSTD_createCStream, ZSTD_endStream, ZSTD_flushStream, ZSTD_freeCCtx, ZSTD_freeCDict,
163    ZSTD_getBlockSize, ZSTD_getDictID_fromCDict, ZSTD_getFrameProgression, ZSTD_initCStream,
164    ZSTD_initCStream_srcSize, ZSTD_initCStream_usingCDict, ZSTD_initCStream_usingDict,
165    ZSTD_maxCLevel, ZSTD_minCLevel, ZSTD_sequenceBound, ZSTD_sizeof_CCtx, ZSTD_sizeof_CDict,
166    ZSTD_BLOCKSPLITTER_LEVEL_MAX, ZSTD_CHAINLOG_MAX_32, ZSTD_CHAINLOG_MAX_64, ZSTD_CHAINLOG_MIN,
167    ZSTD_HASHLOG_MIN, ZSTD_LDM_BUCKETSIZELOG_MAX, ZSTD_LDM_BUCKETSIZELOG_MIN, ZSTD_LDM_HASHLOG_MIN,
168    ZSTD_LDM_HASHRATELOG_MIN, ZSTD_LDM_MINMATCH_MAX, ZSTD_LDM_MINMATCH_MIN, ZSTD_MINMATCH_MAX,
169    ZSTD_MINMATCH_MIN, ZSTD_OVERLAPLOG_MAX, ZSTD_OVERLAPLOG_MIN, ZSTD_SEARCHLOG_MIN,
170    ZSTD_SRCSIZEHINT_MIN, ZSTD_TARGETCBLOCKSIZE_MAX, ZSTD_TARGETCBLOCKSIZE_MIN,
171    ZSTD_TARGETLENGTH_MAX, ZSTD_TARGETLENGTH_MIN, ZSTD_WINDOWLOG_MIN,
172};
173
174pub mod internal {
175    // Needed by benchzstd
176    pub use crate::lib::common::xxhash::ZSTD_XXH64;
177
178    // Needed by fileio
179    pub use crate::lib::common::mem::{MEM_readLE24, MEM_readLE32};
180
181    // Needed by fileio_asyncio
182    pub use crate::lib::common::pool::{
183        POOL_add, POOL_create, POOL_ctx, POOL_free, POOL_function, POOL_joinJobs,
184    };
185}
186
187crate::lib::polyfill::cfg_select!(
188    feature = "custom-prefix" => {
189        #[cfg(feature = "export-symbols")]
190        macro_rules! prefix {
191            ($name:expr) => {
192                concat!(env!("LIBZSTD_RS_SYS_PREFIX"), stringify!($name))
193            };
194        }
195    }
196    feature = "semver-prefix" => {
197        #[cfg(feature = "export-symbols")]
198        macro_rules! prefix {
199            ($name:expr) => {
200                concat!(
201                    "LIBZSTD_RS_SYS_v",
202                    env!("CARGO_PKG_VERSION_MAJOR"),
203                    "_",
204                    env!("CARGO_PKG_VERSION_MINOR"),
205                    "_x_",
206                    stringify!($name)
207                )
208            };
209        }
210    }
211    _ => {
212        #[cfg(feature = "export-symbols")]
213        macro_rules! prefix {
214            ($name:expr) => {
215                stringify!($name)
216            };
217        }
218    }
219);
220
221#[cfg(feature = "export-symbols")]
222pub(crate) use prefix;