#[non_exhaustive]pub struct ResourceLimits {
pub max_pixels: Option<u64>,
pub max_memory_bytes: Option<u64>,
pub max_output_bytes: Option<u64>,
pub max_width: Option<u32>,
pub max_height: Option<u32>,
pub max_input_bytes: Option<u64>,
pub max_frames: Option<u32>,
pub max_animation_ms: Option<u64>,
pub threading: ThreadingPolicy,
}Expand description
Resource limits for encode/decode operations.
Used to prevent DoS attacks and resource exhaustion. All fields are optional;
None means no limit for that resource.
Codecs enforce what they can — not all codecs support all limit types.
Use the check_* methods for caller-side validation before decode/encode.
§Example
use zencodec::ResourceLimits;
let limits = ResourceLimits::none()
.with_max_pixels(100_000_000)
.with_max_memory(512 * 1024 * 1024);Typical usage with a decoder:
// Parse-time rejection (before any pixel work)
let info = config.probe_header(data)?;
limits.check_image_info(&info)?;Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.max_pixels: Option<u64>Maximum total pixels (width × height).
max_memory_bytes: Option<u64>Maximum memory allocation in bytes.
max_output_bytes: Option<u64>Maximum encoded output size in bytes (encode only).
max_width: Option<u32>Maximum image width in pixels.
max_height: Option<u32>Maximum image height in pixels.
max_input_bytes: Option<u64>Maximum input data size in bytes (decode only).
max_frames: Option<u32>Maximum number of animation frames.
max_animation_ms: Option<u64>Maximum total animation duration in milliseconds.
threading: ThreadingPolicyThreading policy for the codec.
Defaults to ThreadingPolicy::Unlimited.
Implementations§
Source§impl ResourceLimits
impl ResourceLimits
Sourcepub fn with_max_pixels(self, max: u64) -> Self
pub fn with_max_pixels(self, max: u64) -> Self
Set maximum total pixels.
Sourcepub fn with_max_memory(self, bytes: u64) -> Self
pub fn with_max_memory(self, bytes: u64) -> Self
Set maximum memory allocation in bytes.
Sourcepub fn with_max_output(self, bytes: u64) -> Self
pub fn with_max_output(self, bytes: u64) -> Self
Set maximum encoded output size in bytes.
Sourcepub fn with_max_width(self, width: u32) -> Self
pub fn with_max_width(self, width: u32) -> Self
Set maximum image width in pixels.
Sourcepub fn with_max_height(self, height: u32) -> Self
pub fn with_max_height(self, height: u32) -> Self
Set maximum image height in pixels.
Sourcepub fn with_max_input_bytes(self, bytes: u64) -> Self
pub fn with_max_input_bytes(self, bytes: u64) -> Self
Set maximum input data size in bytes (decode only).
Sourcepub fn with_max_frames(self, frames: u32) -> Self
pub fn with_max_frames(self, frames: u32) -> Self
Set maximum number of animation frames.
Sourcepub fn with_max_animation_ms(self, ms: u64) -> Self
pub fn with_max_animation_ms(self, ms: u64) -> Self
Set maximum total animation duration in milliseconds.
Sourcepub fn with_threading(self, policy: ThreadingPolicy) -> Self
pub fn with_threading(self, policy: ThreadingPolicy) -> Self
Set threading policy.
Sourcepub fn threading(&self) -> ThreadingPolicy
pub fn threading(&self) -> ThreadingPolicy
Current threading policy.
Sourcepub fn check_dimensions(
&self,
width: u32,
height: u32,
) -> Result<(), LimitExceeded>
pub fn check_dimensions( &self, width: u32, height: u32, ) -> Result<(), LimitExceeded>
Check image dimensions against max_width, max_height, and max_pixels.
Sourcepub fn check_memory(&self, bytes: u64) -> Result<(), LimitExceeded>
pub fn check_memory(&self, bytes: u64) -> Result<(), LimitExceeded>
Check a memory estimate against max_memory_bytes.
Sourcepub fn check_input_size(&self, bytes: u64) -> Result<(), LimitExceeded>
pub fn check_input_size(&self, bytes: u64) -> Result<(), LimitExceeded>
Check input data size against max_input_bytes.
Sourcepub fn check_output_size(&self, bytes: u64) -> Result<(), LimitExceeded>
pub fn check_output_size(&self, bytes: u64) -> Result<(), LimitExceeded>
Check encoded output size against max_output_bytes.
Sourcepub fn check_frames(&self, count: u32) -> Result<(), LimitExceeded>
pub fn check_frames(&self, count: u32) -> Result<(), LimitExceeded>
Check frame count against max_frames.
Sourcepub fn check_animation_ms(&self, ms: u64) -> Result<(), LimitExceeded>
pub fn check_animation_ms(&self, ms: u64) -> Result<(), LimitExceeded>
Check animation duration against max_animation_ms.
Sourcepub fn check_image_info(&self, info: &ImageInfo) -> Result<(), LimitExceeded>
pub fn check_image_info(&self, info: &ImageInfo) -> Result<(), LimitExceeded>
Check ImageInfo from probe_header() against all
applicable limits. This is the fastest rejection point — call it
immediately after probing, before any pixel work.
Checks: max_width, max_height, max_pixels, max_frames.
Sourcepub fn check_output_info(&self, info: &OutputInfo) -> Result<(), LimitExceeded>
pub fn check_output_info(&self, info: &OutputInfo) -> Result<(), LimitExceeded>
Check OutputInfo against dimension limits.
Checks: max_width, max_height, max_pixels.
Trait Implementations§
Source§impl Clone for ResourceLimits
impl Clone for ResourceLimits
Source§fn clone(&self) -> ResourceLimits
fn clone(&self) -> ResourceLimits
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more