1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Bounds applied to untrusted input.
const MIB: usize = 1024 * 1024;
/// Upper bounds enforced while reading. Every reader has a `_with` variant
/// taking these; the plain variants use [`Limits::default`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Limits {
/// Largest accepted file, in bytes. Default 64 MiB.
pub max_input_bytes: usize,
/// Largest accepted inflated payload; for zip containers, the total of all
/// extracted entries. Default 256 MiB.
pub max_inflated_bytes: usize,
/// Most entries read from a zip container. Default 4096.
pub max_archive_entries: usize,
/// Most top-level protobuf fields kept by a
/// [`Document`](crate::Document). Each costs far more memory than the two
/// bytes it can be stored in. Default 250 000.
pub max_fields: usize,
/// Largest accepted SVGA 1.x `movie.spec`; parsed JSON takes many times
/// its size. Default 16 MiB.
pub max_spec_bytes: usize,
/// Most images, sprites, frames, shapes and audios decoded into a
/// [`Movie`](crate::Movie). A frame takes far more memory decoded than
/// encoded, so this bounds the typed view. Default 4 million.
pub max_elements: usize,
}
impl Default for Limits {
fn default() -> Self {
Self {
max_input_bytes: 64 * MIB,
max_inflated_bytes: 256 * MIB,
max_archive_entries: 4096,
max_fields: 250_000,
max_spec_bytes: 16 * MIB,
max_elements: 4_000_000,
}
}
}
impl Limits {
#[must_use]
pub fn with_max_input_bytes(self, max_input_bytes: usize) -> Self {
Self {
max_input_bytes,
..self
}
}
#[must_use]
pub fn with_max_inflated_bytes(self, max_inflated_bytes: usize) -> Self {
Self {
max_inflated_bytes,
..self
}
}
#[must_use]
pub fn with_max_archive_entries(self, max_archive_entries: usize) -> Self {
Self {
max_archive_entries,
..self
}
}
#[must_use]
pub fn with_max_fields(self, max_fields: usize) -> Self {
Self { max_fields, ..self }
}
#[must_use]
pub fn with_max_spec_bytes(self, max_spec_bytes: usize) -> Self {
Self {
max_spec_bytes,
..self
}
}
#[must_use]
pub fn with_max_elements(self, max_elements: usize) -> Self {
Self {
max_elements,
..self
}
}
}