#[non_exhaustive]pub struct Limits { /* private fields */ }Expand description
Caps a decoder enforces while it decodes.
Every check runs per output chunk, before the chunk is written, so a
stream that would pass a cap stops at the cap; a decompression bomb costs
at most max_output bytes of output and the time to produce them.
| Limit | Default | Cost |
|---|---|---|
max_output | 64 MiB | one compare per output chunk |
max_ratio | off | one multiply and compare per output chunk |
ratio_floor | 1 MiB | (the ratio is not checked below it) |
max_header (gzip) | 64 KiB | one counter per header byte |
max_members (gzip) | 1024 | one counter per member |
The ratio cap is off by default: DEFLATE’s largest possible ratio is about 1032 to 1, legitimate data (zero-filled images, repeated logs) reaches hundreds to one, and the absolute cap is what bounds memory. Turn it on when you know your data, for example at 50 for JSON APIs.
§Examples
use compress_lib::Limits;
let limits = Limits::new().with_max_output(1 << 20).with_max_ratio(50);
assert_eq!(limits.max_output(), 1 << 20);
assert_eq!(limits.max_ratio(), 50);Implementations§
Source§impl Limits
impl Limits
Sourcepub const DEFAULT_MAX_OUTPUT: u64
pub const DEFAULT_MAX_OUTPUT: u64
The default absolute output cap: 64 MiB.
Sourcepub const DEFAULT_RATIO_FLOOR: u64
pub const DEFAULT_RATIO_FLOOR: u64
The default output size below which the ratio is not checked: 1 MiB.
Sourcepub const DEFAULT_MAX_HEADER: usize
pub const DEFAULT_MAX_HEADER: usize
The default cap on gzip optional header fields together: 64 KiB.
Sourcepub const DEFAULT_MAX_MEMBERS: u32 = 1024
pub const DEFAULT_MAX_MEMBERS: u32 = 1024
The default cap on gzip members in one stream: 1024.
Sourcepub const fn unlimited() -> Self
pub const fn unlimited() -> Self
No output cap and no ratio cap, for trusted data such as your own files. Named so that its use stands out in review.
§Examples
use compress_lib::Limits;
assert_eq!(Limits::unlimited().max_output(), u64::MAX);Sourcepub const fn with_max_output(self, bytes: u64) -> Self
pub const fn with_max_output(self, bytes: u64) -> Self
Sets the total output a decoder may produce.
§Examples
let limits = compress_lib::Limits::new().with_max_output(16 << 20);
assert_eq!(limits.max_output(), 16 << 20);Sourcepub const fn with_max_ratio(self, ratio: u32) -> Self
pub const fn with_max_ratio(self, ratio: u32) -> Self
Sets the largest allowed ratio of output to input consumed, checked
once the output passes the ratio floor. 0 turns the check off.
§Examples
let limits = compress_lib::Limits::new().with_max_ratio(100);
assert_eq!(limits.max_ratio(), 100);Sourcepub const fn with_ratio_floor(self, bytes: u64) -> Self
pub const fn with_ratio_floor(self, bytes: u64) -> Self
Sets the output size below which the ratio is not checked (small inputs legitimately have high ratios).
§Examples
let limits = compress_lib::Limits::new().with_ratio_floor(64 << 10);
assert_eq!(limits.ratio_floor(), 64 << 10);Sourcepub const fn with_max_header(self, bytes: usize) -> Self
pub const fn with_max_header(self, bytes: usize) -> Self
Sets the cap on the gzip header’s optional fields (FEXTRA, FNAME, FCOMMENT) together.
§Examples
let limits = compress_lib::Limits::new().with_max_header(1024);
assert_eq!(limits.max_header(), 1024);Sourcepub const fn with_max_members(self, members: u32) -> Self
pub const fn with_max_members(self, members: u32) -> Self
Sets the cap on members in one gzip stream.
§Examples
let limits = compress_lib::Limits::new().with_max_members(1);
assert_eq!(limits.max_members(), 1);Sourcepub const fn max_output(&self) -> u64
pub const fn max_output(&self) -> u64
Sourcepub const fn ratio_floor(&self) -> u64
pub const fn ratio_floor(&self) -> u64
The output size below which the ratio is not checked.
§Examples
assert_eq!(compress_lib::Limits::new().ratio_floor(), 1 << 20);Sourcepub const fn max_header(&self) -> usize
pub const fn max_header(&self) -> usize
The gzip header field cap.
§Examples
assert_eq!(compress_lib::Limits::new().max_header(), 64 << 10);