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/// Decoder window-size limit (128 MiB = `1 << 27`), matching upstream zstd's
29/// default `ZSTD_d_windowLogMax` (`ZSTD_WINDOWLOG_LIMIT_DEFAULT = 27`). Frames
30/// advertising a larger window are rejected to bound allocation on untrusted
31/// input. The spec permits larger windows, but no standard zstd encoder emits
32/// them by default, so matching the upstream limit keeps the drop-in contract:
33/// every frame a stock zstd decoder accepts, we accept too — and crucially,
34/// every frame OUR encoder emits (up to `window_log 27` at level 22) round-trips
35/// through our own decoder. A non-power-of-two cap below `1 << 27` would reject
36/// our own level-22 / streaming output (whose window header carries the full
37/// 128 MiB). Decompression-bomb protection lives on the OUTPUT path, not here.
38pub const MAXIMUM_ALLOWED_WINDOW_SIZE: u64 = 1 << 27;