Skip to main content

structured_zstd/common/
mod.rs

1//! Values and interfaces shared between the encoding side
2//! and the decoding side.
3
4// --- FRAMES ---
5/// This magic number is included at the start of a single Zstandard frame
6pub const MAGIC_NUM: u32 = 0xFD2F_B528;
7/// Window size refers to the minimum amount of memory needed to decode any given frame.
8///
9/// The minimum window size is defined as 1 KB
10pub const MIN_WINDOW_SIZE: u64 = 1024;
11/// Window size refers to the minimum amount of memory needed to decode any given frame.
12///
13/// The maximum window size allowed by the spec is 3.75TB
14pub const MAX_WINDOW_SIZE: u64 = (1 << 41) + 7 * (1 << 38);
15
16// --- BLOCKS ---
17/// While the spec limits block size to 128KB, the implementation uses
18/// 128kibibytes
19///
20/// <https://github.com/facebook/zstd/blob/eca205fc7849a61ab287492931a04960ac58e031/doc/educational_decoder/zstd_decompress.c#L28-L29>
21pub const MAX_BLOCK_SIZE: u32 = 128 * 1024;
22/// Smallest accepted block-size target (upstream `ZSTD_TARGETCBLOCKSIZE_MIN`):
23/// below this the per-block header overhead dominates any latency benefit.
24/// Re-exported at the crate root as the single source of truth; the C ABI
25/// parameter bounds import it from there.
26pub const MIN_TARGET_BLOCK_SIZE: u32 = 1340;
27
28/// The decoder's default window ceiling (128 MiB = `1 << 27`), upstream zstd's
29/// default `ZSTD_d_windowLogMax` (`ZSTD_WINDOWLOG_LIMIT_DEFAULT = 27`). On the
30/// paths where the decoder holds the window itself, a frame advertising a
31/// larger one is refused to bound that allocation on untrusted input, until the
32/// caller raises the ceiling (`FrameDecoder::set_max_window_size`). Every frame
33/// our encoder emits by default (up to `window_log 27` at level 22) fits it.
34pub const MAXIMUM_ALLOWED_WINDOW_SIZE: u64 = 1 << 27;
35
36/// The largest window this build decodes on any path, however far the ceiling
37/// is raised: upstream `ZSTD_WINDOWLOG_MAX`, `1 << 31` on 64-bit targets and
38/// `1 << 30` on 32-bit ones, where a larger window could not be addressed.
39#[cfg(target_pointer_width = "64")]
40pub const MAX_DECODER_WINDOW_SIZE: u64 = 1 << 31;
41/// The largest window this build decodes on any path, however far the ceiling
42/// is raised: upstream `ZSTD_WINDOWLOG_MAX`, `1 << 31` on 64-bit targets and
43/// `1 << 30` on 32-bit ones, where a larger window could not be addressed.
44#[cfg(not(target_pointer_width = "64"))]
45pub const MAX_DECODER_WINDOW_SIZE: u64 = 1 << 30;
46
47#[cfg(feature = "caller-workspace")]
48pub(crate) mod inline_vec;