Skip to main content

WorkLimit

Enum WorkLimit 

Source
#[non_exhaustive]
pub enum WorkLimit { Default, Raised(u32), Unlimited, }
Expand description

How far a decode may go against one of its bounds: the DecodeOptions::packet_work_limit and DecodeOptions::code_block_work_limit work bounds, and the DecodeOptions::sample_limit on the image’s size.

Each work bound scales with the size of the file, so a small file cannot ask for minutes of work, and the sample limit holds the memory a file may ask for to an estimated 1.4 GiB for a square tile, and to a few GiB for a tall, narrow one (see the sample limit). A valid file can still go past a bound, when it codes many samples in very few bytes or is very large. The decode then fails with Error::Limit, and its message names the option and a Raised factor. Treat the factor as a hint, not a measure of the file:

  • For the sample limit it is exact for the check it names: the image’s samples are known from its header. Palette expansion or sRGB output is checked later, against the same limit, and may need a higher factor.
  • For the code-block bound it is what the tiles seen so far need, exact for a file of one tile.
  • For the packet bound it is the next factor up from the one that failed (or, when the refusal comes from a POC sequence’s reach or a reduction’s dropped code-blocks, the factor that check needs), and the file may need more: the walk stops as soon as it runs out, so it does not know the total. Double n on each retry rather than stepping by one: each retry costs more, and a file can need a factor of dozens.

Which value to pick:

  • For a file from a source you trust, use Unlimited. The decode then takes as long as the file asks.
  • To keep a ceiling, use Raised(n) with an n of your own choosing, the largest cost you will accept. Read the factor from the message to size it: a file that needs Raised(3) is refused under Raised(2). Do not parse the message in code; it is a diagnostic, not a stable format.

Only these three limits can be changed. The other guards also end in Error::Limit, and no option here raises them (see Error::Limit). A damaged file can hit a guard too, so do not lift a bound on that evidence alone.

use rust_j2k::{DecodeOptions, Error, WorkLimit, decode, decode_with};

// A valid 1 KB file that asks for far more work than its size allows.
let bytes = include_bytes!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/tests/fixtures/tier1_work_bound/t1bomb_1024_64_88.j2k"
));
let image = match decode(bytes) {
    // The example fails if the bomb is ever admitted by default.
    Ok(_) => panic!("expected the work bound to refuse this file"),
    // Past a limit. The message says which, and the factor.
    Err(Error::Limit(why)) => {
        println!("refused: {why}");
        // The file is trusted here, so lift all three limits. To keep a
        // ceiling, use `WorkLimit::Raised(n)` for the one named instead.
        let options = DecodeOptions::default()
            .with_sample_limit(WorkLimit::Unlimited)
            .with_code_block_work_limit(WorkLimit::Unlimited)
            .with_packet_work_limit(WorkLimit::Unlimited);
        decode_with(bytes, &options).expect("a valid file decodes once the limits are lifted")
    }
    Err(e) => panic!("not a work-bound failure: {e}"),
};
assert_eq!((image.width, image.height), (1024, 1024));

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Default

The bound as decode describes it.

§

Raised(u32)

The default bound times this factor: for a work bound, both its floor and the work it allows per byte. Raised(1) sets the same bound as Default, though the two do not compare equal. Raised(0) is Error::InvalidOptions.

§

Unlimited

No bound. For a work bound, the decode does all the work the file asks for, however long that takes; for the sample limit, it allocates all the memory the file asks for (see DecodeOptions::sample_limit). The other two limits keep their own settings, and the guards with no option (listed on Error::Limit) stay in force.

Trait Implementations§

Source§

impl Clone for WorkLimit

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 Debug for WorkLimit

Source§

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

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

impl Default for WorkLimit

Source§

fn default() -> Self

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

impl Eq for WorkLimit

Source§

impl PartialEq for WorkLimit

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 WorkLimit

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.