Skip to content

Commit f9f0aec

Browse files
michielp1807folkertdev
authored andcommitted
Add more doc comments from zstd.h
Also exposes a couple of methods publicly which are mentioned in the docs
1 parent 034c5d4 commit f9f0aec

File tree

6 files changed

+608
-22
lines changed

6 files changed

+608
-22
lines changed

c2rust-lib.rs

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
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+
134
#![allow(non_camel_case_types)]
235
#![allow(non_snake_case)]
336
#![allow(non_upper_case_globals)]
@@ -78,8 +111,8 @@ pub use crate::lib::zstd::{
78111

79112
pub use crate::lib::decompress::{
80113
zstd_ddict::{
81-
ZSTD_DDict, ZSTD_createDDict, ZSTD_createDDict_byReference, ZSTD_freeDDict,
82-
ZSTD_getDictID_fromDDict, ZSTD_sizeof_DDict,
114+
ZSTD_DDict, ZSTD_createDDict, ZSTD_createDDict_byReference, ZSTD_estimateDDictSize,
115+
ZSTD_freeDDict, ZSTD_getDictID_fromDDict, ZSTD_sizeof_DDict,
83116
},
84117
zstd_decompress::{
85118
ZSTD_DCtx_getParameter, ZSTD_DCtx_loadDictionary, ZSTD_DCtx_refDDict, ZSTD_DCtx_refPrefix,
@@ -116,18 +149,18 @@ pub use crate::lib::compress::zstd_compress::{
116149
ZSTD_CDict, ZSTD_CStreamInSize, ZSTD_CStreamOutSize, ZSTD_EndDirective, ZSTD_cParam_getBounds,
117150
ZSTD_compress, ZSTD_compress2, ZSTD_compressBlock, ZSTD_compressBound, ZSTD_compressCCtx,
118151
ZSTD_compressStream, ZSTD_compressStream2, ZSTD_compress_usingCDict, ZSTD_compress_usingDict,
119-
ZSTD_copyCCtx, ZSTD_createCCtx, ZSTD_createCDict, ZSTD_createCDict_byReference, ZSTD_endStream,
120-
ZSTD_flushStream, ZSTD_freeCCtx, ZSTD_freeCDict, ZSTD_getBlockSize, ZSTD_getDictID_fromCDict,
121-
ZSTD_getFrameProgression, ZSTD_initCStream, ZSTD_initCStream_srcSize,
122-
ZSTD_initCStream_usingCDict, ZSTD_initCStream_usingDict, ZSTD_maxCLevel, ZSTD_minCLevel,
123-
ZSTD_sequenceBound, ZSTD_sizeof_CCtx, ZSTD_sizeof_CDict, ZSTD_BLOCKSIZE_MAX_MIN,
124-
ZSTD_BLOCKSPLITTER_LEVEL_MAX, ZSTD_CHAINLOG_MAX_32, ZSTD_CHAINLOG_MAX_64, ZSTD_CHAINLOG_MIN,
125-
ZSTD_HASHLOG_MIN, ZSTD_LDM_BUCKETSIZELOG_MAX, ZSTD_LDM_BUCKETSIZELOG_MIN, ZSTD_LDM_HASHLOG_MIN,
126-
ZSTD_LDM_HASHRATELOG_MIN, ZSTD_LDM_MINMATCH_MAX, ZSTD_LDM_MINMATCH_MIN, ZSTD_MINMATCH_MAX,
127-
ZSTD_MINMATCH_MIN, ZSTD_OVERLAPLOG_MAX, ZSTD_OVERLAPLOG_MIN, ZSTD_SEARCHLOG_MIN,
128-
ZSTD_SKIPPABLEHEADERSIZE, ZSTD_SRCSIZEHINT_MIN, ZSTD_TARGETCBLOCKSIZE_MAX,
129-
ZSTD_TARGETCBLOCKSIZE_MIN, ZSTD_TARGETLENGTH_MAX, ZSTD_TARGETLENGTH_MIN,
130-
ZSTD_WINDOWLOG_LIMIT_DEFAULT, ZSTD_WINDOWLOG_MIN,
152+
ZSTD_copyCCtx, ZSTD_createCCtx, ZSTD_createCDict, ZSTD_createCDict_byReference,
153+
ZSTD_createCStream, ZSTD_endStream, ZSTD_flushStream, ZSTD_freeCCtx, ZSTD_freeCDict,
154+
ZSTD_getBlockSize, ZSTD_getDictID_fromCDict, ZSTD_getFrameProgression, ZSTD_initCStream,
155+
ZSTD_initCStream_srcSize, ZSTD_initCStream_usingCDict, ZSTD_initCStream_usingDict,
156+
ZSTD_maxCLevel, ZSTD_minCLevel, ZSTD_sequenceBound, ZSTD_sizeof_CCtx, ZSTD_sizeof_CDict,
157+
ZSTD_BLOCKSIZE_MAX_MIN, ZSTD_BLOCKSPLITTER_LEVEL_MAX, ZSTD_CHAINLOG_MAX_32,
158+
ZSTD_CHAINLOG_MAX_64, ZSTD_CHAINLOG_MIN, ZSTD_HASHLOG_MIN, ZSTD_LDM_BUCKETSIZELOG_MAX,
159+
ZSTD_LDM_BUCKETSIZELOG_MIN, ZSTD_LDM_HASHLOG_MIN, ZSTD_LDM_HASHRATELOG_MIN,
160+
ZSTD_LDM_MINMATCH_MAX, ZSTD_LDM_MINMATCH_MIN, ZSTD_MINMATCH_MAX, ZSTD_MINMATCH_MIN,
161+
ZSTD_OVERLAPLOG_MAX, ZSTD_OVERLAPLOG_MIN, ZSTD_SEARCHLOG_MIN, ZSTD_SKIPPABLEHEADERSIZE,
162+
ZSTD_SRCSIZEHINT_MIN, ZSTD_TARGETCBLOCKSIZE_MAX, ZSTD_TARGETCBLOCKSIZE_MIN,
163+
ZSTD_TARGETLENGTH_MAX, ZSTD_TARGETLENGTH_MIN, ZSTD_WINDOWLOG_LIMIT_DEFAULT, ZSTD_WINDOWLOG_MIN,
131164
};
132165

133166
pub mod internal {

lib/common/zstd_common.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,66 @@ use crate::lib::common::error_private::{
55
};
66
use crate::lib::zstd::*;
77

8+
/// Get the zstd version number
9+
///
10+
/// # Returns
11+
///
12+
/// - the runtime library version, as `(MAJOR*100*100 + MINOR*100 + RELEASE)`
813
#[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(ZSTD_versionNumber))]
914
pub const extern "C" fn ZSTD_versionNumber() -> core::ffi::c_uint {
1015
ZSTD_VERSION_NUMBER as core::ffi::c_uint
1116
}
17+
18+
/// Get a zstd version string
19+
///
20+
/// # Returns
21+
///
22+
/// - the runtime library version, like "1.5.8"
1223
#[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(ZSTD_versionString))]
1324
pub const extern "C" fn ZSTD_versionString() -> *const core::ffi::c_char {
1425
c"1.5.8".as_ptr()
1526
}
27+
28+
/// Most functions returning a `size_t` value can be tested for errors, using [`ZSTD_isError`].
29+
///
30+
/// # Returns
31+
///
32+
/// - 1 if the provided code is an error
33+
/// - 0 otherwise
1634
#[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(ZSTD_isError))]
1735
pub const extern "C" fn ZSTD_isError(code: size_t) -> core::ffi::c_uint {
1836
ERR_isError(code) as _
1937
}
38+
39+
/// Provides a readable error string from a function result
2040
#[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(ZSTD_getErrorName))]
2141
pub extern "C" fn ZSTD_getErrorName(code: size_t) -> *const core::ffi::c_char {
2242
ERR_getErrorName(code)
2343
}
44+
45+
/// Convert a result into an error code, which can be compared to the errors enum list
2446
#[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(ZSTD_getErrorCode))]
2547
pub extern "C" fn ZSTD_getErrorCode(code: size_t) -> ZSTD_ErrorCode {
2648
ERR_getErrorCode(code)
2749
}
50+
51+
/// Provides a readable error string from an error code
52+
///
53+
/// Unlike [`ZSTD_getErrorName`], this method should not be used on `size_t` function results
2854
#[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(ZSTD_getErrorString))]
2955
pub extern "C" fn ZSTD_getErrorString(code: ZSTD_ErrorCode) -> *const core::ffi::c_char {
3056
ERR_getErrorString(code)
3157
}
58+
59+
/// This is mainly used for Zstd's determinism test suite, which is only run when this function
60+
/// returns 1.
61+
///
62+
/// # Returns
63+
///
64+
/// - 1 if the library is built using standard compilation flags and participates in determinism
65+
/// guarantees with other builds of the same version.
66+
/// - 0 if the library was compiled with non-standard compilation flags that change the output of
67+
/// the compressor.
3268
#[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(ZSTD_isDeterministicBuild))]
3369
pub extern "C" fn ZSTD_isDeterministicBuild() -> core::ffi::c_int {
3470
true as core::ffi::c_int

lib/decompress/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,15 @@ impl From<u32> for BlockType {
171171
#[derive(Default)]
172172
#[repr(C)]
173173
pub struct ZSTD_FrameHeader {
174+
/// if set to [`crate::ZSTD_CONTENTSIZE_UNKNOWN`], it means this field is not available, 0 means "empty"
174175
pub frameContentSize: core::ffi::c_ulonglong,
176+
/// can be very large, up to <= `frameContentSize`
175177
pub windowSize: core::ffi::c_ulonglong,
176178
pub blockSizeMax: core::ffi::c_uint,
179+
/// if set to [`ZSTD_skippableFrame`], `frameContentSize` is the size of skippable content
177180
pub frameType: ZSTD_FrameType_e,
178181
pub headerSize: core::ffi::c_uint,
182+
/// for [`ZSTD_skippableFrame`], contains the skippable magic variant \[0-15]
179183
pub dictID: core::ffi::c_uint,
180184
pub checksumFlag: core::ffi::c_uint,
181185
pub _reserved1: core::ffi::c_uint,
@@ -210,6 +214,17 @@ impl Workspace {
210214
}
211215

212216
pub type ZSTD_DCtx = ZSTD_DCtx_s;
217+
218+
/// Decompression context
219+
///
220+
/// When decompressing many times, it is recommended to allocate a context only once, and reuse it
221+
/// for each successive compression operation. This will make workload friendlier for system's
222+
/// memory.
223+
///
224+
/// You can create a decompression context with [`crate::ZSTD_createDCtx`] and free it with
225+
/// [`crate::ZSTD_freeDCtx`].
226+
///
227+
/// Use one context per thread for parallel execution.
213228
#[repr(C)]
214229
pub struct ZSTD_DCtx_s {
215230
LLTptr: Option<NonNull<SymbolTable<512>>>,

lib/decompress/zstd_ddict.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ impl ZSTD_DDict {
6464
}
6565

6666
pub type ZSTD_dictContentType_e = core::ffi::c_uint;
67+
/// Refuses to load a dictionary if it does not respect Zstandard's specification, starting with
68+
/// [`ZSTD_MAGIC_DICTIONARY`]
6769
pub const ZSTD_dct_fullDict: ZSTD_dictContentType_e = 2;
70+
/// Ensures dictionary is always loaded as `rawContent`, even if it starts with [`ZSTD_MAGIC_DICTIONARY`]
6871
pub const ZSTD_dct_rawContent: ZSTD_dictContentType_e = 1;
72+
/// Dictionary is "full" when starting with [`ZSTD_MAGIC_DICTIONARY`], otherwise it is "rawContent"
6973
pub const ZSTD_dct_auto: ZSTD_dictContentType_e = 0;
7074

7175
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -75,7 +79,9 @@ enum DictLoadMethod {
7579
}
7680

7781
pub type ZSTD_dictLoadMethod_e = core::ffi::c_uint;
82+
/// Copy dictionary content internally
7883
pub const ZSTD_dlm_byCopy: ZSTD_dictLoadMethod_e = DictLoadMethod::ByCopy as _;
84+
/// Reference dictionary content -- the dictionary buffer must outlive its users
7985
pub const ZSTD_dlm_byRef: ZSTD_dictLoadMethod_e = DictLoadMethod::ByRef as _;
8086

8187
pub const ZSTD_MAGIC_DICTIONARY: core::ffi::c_uint = 0xec30a437 as core::ffi::c_uint;
@@ -231,7 +237,15 @@ pub unsafe extern "C" fn ZSTD_createDDict_advanced(
231237

232238
/// Create a digested dictionary, to start decompression without startup delay.
233239
///
234-
/// `dict` content is copied inside the [`ZSTD_DDict`], so `dict` can be released after [`ZSTD_DDict`] creation
240+
/// The `dict`'s content is copied inside the [`ZSTD_DDict`], so `dict` can be released after
241+
/// [`ZSTD_DDict`] creation.
242+
///
243+
/// The DDict can be freed using [`ZSTD_freeDDict`].
244+
///
245+
/// # Returns
246+
///
247+
/// - a [`ZSTD_DDict`] if it was successfully created
248+
/// - NULL if there was an error creating the dictionary
235249
#[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(ZSTD_createDDict))]
236250
pub unsafe extern "C" fn ZSTD_createDDict(
237251
dict: *const core::ffi::c_void,
@@ -306,6 +320,9 @@ pub unsafe extern "C" fn ZSTD_initStaticDDict(
306320
ddict
307321
}
308322

323+
/// Free the memory allocated with [`ZSTD_createDDict`].
324+
///
325+
/// If a NULL pointer is passed, no operation is performed.
309326
#[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(ZSTD_freeDDict))]
310327
pub unsafe extern "C" fn ZSTD_freeDDict(ddict: *mut ZSTD_DDict) -> size_t {
311328
if ddict.is_null() {
@@ -336,6 +353,12 @@ pub const extern "C" fn ZSTD_estimateDDictSize(
336353
}
337354
}
338355

356+
/// Get the _current_ memory usage of the [`ZSTD_DDict`]
357+
///
358+
/// # Returns
359+
///
360+
/// - the size of the DDict, including the size of the DDict's `dictBuffer` if present
361+
/// - 0 if the `ddict` is NULL
339362
#[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(ZSTD_sizeof_DDict))]
340363
pub unsafe extern "C" fn ZSTD_sizeof_DDict(ddict: *const ZSTD_DDict) -> size_t {
341364
if ddict.is_null() {

0 commit comments

Comments
 (0)