#[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 about 2.7 GiB for a square tile, and to several 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. Its
LimitDetail::kind names the option, and LimitDetail::retry_with
gives a Raised factor, which the message names
too. 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
non 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 annof your own choosing, the largest cost you will accept. Read the factor fromLimitDetail::retry_withto size it, or letDecodeOptions::with_retryset it: a file that needsRaised(3)is refused underRaised(2). Do not parse the message; 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 file that asks for far more work than its size allows.
let bytes = std::fs::read("image.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. `limit.kind()` says which, and `limit.retry_with()`
// the factor.
Err(Error::Limit(limit)) => {
println!("refused: {limit}");
// 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
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.