use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Limit {
NestingDepth,
StreamDecoded,
DecodedTotal,
FilterChain,
ResolveDepth,
PageTreeDepth,
TreeDepth,
StringLength,
ContentOperators,
XObjectDepth,
ImagePixels,
ReadWindow,
}
impl Limit {
pub const ALL: [Self; 12] = [
Self::NestingDepth,
Self::StreamDecoded,
Self::DecodedTotal,
Self::FilterChain,
Self::ResolveDepth,
Self::PageTreeDepth,
Self::TreeDepth,
Self::StringLength,
Self::ContentOperators,
Self::XObjectDepth,
Self::ImagePixels,
Self::ReadWindow,
];
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::NestingDepth => "max_nesting_depth",
Self::StreamDecoded => "max_stream_decoded",
Self::DecodedTotal => "max_decoded_total",
Self::FilterChain => "max_filter_chain",
Self::ResolveDepth => "max_resolve_depth",
Self::PageTreeDepth => "max_page_tree_depth",
Self::TreeDepth => "max_tree_depth",
Self::StringLength => "max_string_length",
Self::ContentOperators => "max_content_operators",
Self::XObjectDepth => "max_xobject_depth",
Self::ImagePixels => "max_image_pixels",
Self::ReadWindow => "max_read_window",
}
}
#[must_use]
pub const fn value_in(self, limits: &Limits) -> u64 {
match self {
Self::NestingDepth => limits.max_nesting_depth as u64,
Self::StreamDecoded => limits.max_stream_decoded as u64,
Self::DecodedTotal => limits.max_decoded_total as u64,
Self::FilterChain => limits.max_filter_chain as u64,
Self::ResolveDepth => limits.max_resolve_depth as u64,
Self::PageTreeDepth => limits.max_page_tree_depth as u64,
Self::TreeDepth => limits.max_tree_depth as u64,
Self::StringLength => limits.max_string_length as u64,
Self::ContentOperators => limits.max_content_operators,
Self::XObjectDepth => limits.max_xobject_depth as u64,
Self::ImagePixels => limits.max_image_pixels,
Self::ReadWindow => limits.max_read_window as u64,
}
}
}
impl fmt::Display for Limit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[expect(
clippy::struct_field_names,
reason = "the field names are the ADR's public limit names (`max_*`)"
)]
pub struct Limits {
max_nesting_depth: usize,
max_stream_decoded: usize,
max_decoded_total: usize,
max_filter_chain: usize,
max_resolve_depth: usize,
max_page_tree_depth: usize,
max_tree_depth: usize,
max_string_length: usize,
max_xref_sections: usize,
max_object_number: u32,
max_warnings: usize,
max_content_operators: u64,
max_xobject_depth: usize,
max_image_pixels: u64,
max_read_window: usize,
max_script_length: usize,
}
impl Default for Limits {
fn default() -> Self {
Self::new()
}
}
macro_rules! limit_accessors {
($( $(#[$doc:meta])* $field:ident, $with:ident: $ty:ty; )*) => {
$(
$(#[$doc])*
#[doc = ""]
#[doc = "Added in 0.2.0."]
#[must_use]
pub const fn $field(&self) -> $ty {
self.$field
}
#[doc = concat!("Sets [`", stringify!($field), "`](Self::", stringify!($field), ").")]
#[doc = ""]
#[doc = "Added in 0.2.0."]
#[must_use]
pub const fn $with(mut self, value: $ty) -> Self {
self.$field = value;
self
}
)*
};
}
impl Limits {
#[must_use]
pub const fn new() -> Self {
Self {
max_nesting_depth: 256,
max_stream_decoded: 64 * 1024 * 1024,
max_decoded_total: 1024 * 1024 * 1024,
max_filter_chain: 8,
max_resolve_depth: 32,
max_page_tree_depth: 256,
max_tree_depth: 64,
max_string_length: 16 * 1024 * 1024,
max_xref_sections: 4096,
max_object_number: 8_388_607,
max_warnings: 1000,
max_content_operators: 10_000_000,
max_xobject_depth: 32,
max_image_pixels: 1 << 28,
max_read_window: 256 * 1024 * 1024,
max_script_length: 64 * 1024,
}
}
#[must_use]
pub const fn unlimited() -> Self {
Self {
max_nesting_depth: usize::MAX,
max_stream_decoded: usize::MAX,
max_decoded_total: usize::MAX,
max_filter_chain: usize::MAX,
max_resolve_depth: usize::MAX,
max_page_tree_depth: usize::MAX,
max_tree_depth: usize::MAX,
max_string_length: usize::MAX,
max_xref_sections: usize::MAX,
max_object_number: u32::MAX,
max_warnings: usize::MAX,
max_content_operators: u64::MAX,
max_xobject_depth: usize::MAX,
max_image_pixels: u64::MAX,
max_read_window: usize::MAX,
max_script_length: usize::MAX,
}
}
limit_accessors! {
max_nesting_depth, with_max_nesting_depth: usize;
max_stream_decoded, with_max_stream_decoded: usize;
max_decoded_total, with_max_decoded_total: usize;
max_filter_chain, with_max_filter_chain: usize;
max_resolve_depth, with_max_resolve_depth: usize;
max_page_tree_depth, with_max_page_tree_depth: usize;
max_tree_depth, with_max_tree_depth: usize;
max_string_length, with_max_string_length: usize;
max_xref_sections, with_max_xref_sections: usize;
max_object_number, with_max_object_number: u32;
max_warnings, with_max_warnings: usize;
max_content_operators, with_max_content_operators: u64;
max_xobject_depth, with_max_xobject_depth: usize;
max_image_pixels, with_max_image_pixels: u64;
max_read_window, with_max_read_window: usize;
}
#[must_use]
pub const fn max_script_length(&self) -> usize {
self.max_script_length
}
#[must_use]
pub const fn with_max_script_length(mut self, value: usize) -> Self {
self.max_script_length = value;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DecodeBudget {
remaining: usize,
}
impl DecodeBudget {
#[must_use]
pub const fn new(limits: &Limits) -> Self {
Self {
remaining: limits.max_decoded_total,
}
}
#[must_use]
pub const fn remaining(&self) -> usize {
self.remaining
}
pub(crate) const fn spend(&mut self, bytes: usize) {
self.remaining = self.remaining.saturating_sub(bytes);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_match_the_adr_table() {
let l = Limits::default();
assert_eq!(
[
l.max_nesting_depth(),
l.max_stream_decoded(),
l.max_decoded_total(),
l.max_filter_chain(),
l.max_resolve_depth(),
l.max_page_tree_depth(),
l.max_tree_depth(),
l.max_string_length(),
l.max_xref_sections(),
l.max_warnings(),
l.max_xobject_depth(),
l.max_read_window(),
l.max_script_length(),
],
[
256,
64 << 20,
1 << 30,
8,
32,
256,
64,
16 << 20,
4096,
1000,
32,
256 << 20,
64 << 10
]
);
}
#[test]
fn defaults_for_wide_limits_match_the_adr_table() {
let l = Limits::default();
assert_eq!(
(l.max_object_number(), l.max_content_operators(), l.max_image_pixels()),
(8_388_607, 10_000_000, 1 << 28)
);
}
#[test]
fn with_builder_changes_only_its_field() {
let l = Limits::default().with_max_filter_chain(2);
assert_eq!(
l,
Limits {
max_filter_chain: 2,
..Limits::default()
}
);
}
#[test]
fn limit_value_in_reads_the_matching_field() {
let l = Limits::default().with_max_string_length(7);
assert_eq!(Limit::StringLength.value_in(&l), 7);
}
#[test]
fn limit_names_are_distinct() {
let mut names: Vec<_> = Limit::ALL.iter().map(|l| l.name()).collect();
names.sort_unstable();
names.dedup();
assert_eq!(names.len(), Limit::ALL.len());
}
}