Skip to main content

jbig2_decoder/
limits.rs

1#[cfg(verus_keep_ghost)]
2use vstd::prelude::*;
3
4/// Explicit resource limits for untrusted JBIG2 input.
5///
6/// The defaults implement the approved Phase 1 resource envelope. Callers may
7/// use stricter values. Every field is an inclusive maximum.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9#[non_exhaustive]
10#[cfg_attr(verus_keep_ghost, verus_verify)]
11pub struct Limits {
12    /// Maximum page or region width.
13    pub max_width: u32,
14    /// Maximum page or region height.
15    pub max_height: u32,
16    /// Maximum pixels in one page or bitmap.
17    pub max_pixels_per_page: u64,
18    /// Maximum concurrently decoder-managed bytes.
19    pub max_decoder_managed_memory_bytes: u64,
20    /// Maximum bytes consumed from one input stream.
21    pub max_stream_bytes: u64,
22    /// Maximum pages in one decode operation.
23    pub max_pages: u32,
24    /// Maximum segments in one decode operation.
25    pub max_segments: u32,
26    /// Maximum symbols in one decode operation.
27    pub max_symbols: u32,
28    /// Maximum references declared by one segment.
29    pub max_references_per_segment: u32,
30    /// Maximum references across one decode operation.
31    pub max_total_references: u64,
32    /// Maximum reference-graph traversal depth.
33    pub max_reference_graph_depth: u32,
34    /// Maximum nested decoding depth.
35    pub max_nesting_depth: u32,
36    /// Maximum abstract work units charged by a decode operation.
37    pub max_work_units: u64,
38    /// Maximum cumulative decoded bitmap bytes.
39    pub max_cumulative_decoded_bitmap_bytes: u64,
40    /// Maximum bytes retained for segment and global state.
41    pub max_retained_state_bytes: u64,
42}
43
44impl Limits {
45    #[cfg_attr(verus_keep_ghost, verus_spec(out =>
46        ensures
47            out.max_width == 1_000_000,
48            out.max_height == 1_000_000,
49            out.max_pixels_per_page == 1_000_000_000,
50            out.max_decoder_managed_memory_bytes == 1_073_741_824,
51            out.max_stream_bytes == 1_073_741_824,
52            out.max_pages == 10_000,
53            out.max_segments == 100_000,
54            out.max_symbols == 1_000_000,
55            out.max_references_per_segment == 4_096,
56            out.max_total_references == 1_000_000,
57            out.max_reference_graph_depth == 128,
58            out.max_nesting_depth == 128,
59            out.max_work_units == 2_000_000_000,
60            out.max_cumulative_decoded_bitmap_bytes == 4_294_967_296,
61            out.max_retained_state_bytes == 536_870_912,
62    ))]
63    #[cfg_attr(verus_keep_ghost, allow(unused, verus_impl_method_marker))]
64    pub(crate) fn project_default() -> Self {
65        Self {
66            max_width: 1_000_000,
67            max_height: 1_000_000,
68            max_pixels_per_page: 1_000_000_000,
69            max_decoder_managed_memory_bytes: 1_073_741_824,
70            max_stream_bytes: 1_073_741_824,
71            max_pages: 10_000,
72            max_segments: 100_000,
73            max_symbols: 1_000_000,
74            max_references_per_segment: 4_096,
75            max_total_references: 1_000_000,
76            max_reference_graph_depth: 128,
77            max_nesting_depth: 128,
78            max_work_units: 2_000_000_000,
79            max_cumulative_decoded_bitmap_bytes: 4_294_967_296,
80            max_retained_state_bytes: 536_870_912,
81        }
82    }
83}
84
85#[cfg_attr(verus_keep_ghost, verifier::verify)]
86impl Default for Limits {
87    #[cfg_attr(verus_keep_ghost, verus_spec(out =>
88        ensures
89            out.max_width == 1_000_000,
90            out.max_height == 1_000_000,
91            out.max_pixels_per_page == 1_000_000_000,
92            out.max_decoder_managed_memory_bytes == 1_073_741_824,
93            out.max_stream_bytes == 1_073_741_824,
94            out.max_pages == 10_000,
95            out.max_segments == 100_000,
96            out.max_symbols == 1_000_000,
97            out.max_references_per_segment == 4_096,
98            out.max_total_references == 1_000_000,
99            out.max_reference_graph_depth == 128,
100            out.max_nesting_depth == 128,
101            out.max_work_units == 2_000_000_000,
102            out.max_cumulative_decoded_bitmap_bytes == 4_294_967_296,
103            out.max_retained_state_bytes == 536_870_912,
104    ))]
105    #[cfg_attr(verus_keep_ghost, allow(unused, verus_impl_method_marker))]
106    fn default() -> Self {
107        Self::project_default()
108    }
109}