Skip to main content

Limits

Struct Limits 

Source
#[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.

LimitDefaultCost
max_output64 MiBone compare per output chunk
max_ratiooffone multiply and compare per output chunk
ratio_floor1 MiB(the ratio is not checked below it)
max_header (gzip)64 KiBone counter per header byte
max_members (gzip)1024one 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

Source

pub const DEFAULT_MAX_OUTPUT: u64

The default absolute output cap: 64 MiB.

Source

pub const DEFAULT_RATIO_FLOOR: u64

The default output size below which the ratio is not checked: 1 MiB.

Source

pub const DEFAULT_MAX_HEADER: usize

The default cap on gzip optional header fields together: 64 KiB.

Source

pub const DEFAULT_MAX_MEMBERS: u32 = 1024

The default cap on gzip members in one stream: 1024.

Source

pub const fn new() -> Self

The safe defaults (see the table on Limits).

§Examples
use compress_lib::Limits;

assert_eq!(Limits::new().max_output(), 64 << 20);
assert_eq!(Limits::new(), Limits::default());
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

pub const fn max_output(&self) -> u64

The total output cap.

§Examples
assert_eq!(compress_lib::Limits::new().max_output(), 64 << 20);
Source

pub const fn max_ratio(&self) -> u32

The ratio cap (0 = off).

§Examples
assert_eq!(compress_lib::Limits::new().max_ratio(), 0);
Source

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);
Source

pub const fn max_header(&self) -> usize

The gzip header field cap.

§Examples
assert_eq!(compress_lib::Limits::new().max_header(), 64 << 10);
Source

pub const fn max_members(&self) -> u32

The gzip member cap.

§Examples
assert_eq!(compress_lib::Limits::new().max_members(), 1024);

Trait Implementations§

Source§

impl Clone for Limits

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Limits

Source§

impl Debug for Limits

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Limits

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for Limits

Source§

impl Hash for Limits

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Limits

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Limits

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.