Skip to main content

cadkit_core/
options.rs

1//! Reader options and resource limits.
2
3/// Hard resource limits that protect against malicious or corrupt input.
4/// Every reader must check sizes taken from the file against these before allocating or looping.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
6#[serde(default)]
7#[non_exhaustive]
8pub struct Limits {
9    /// Largest accepted input file.
10    pub max_input_bytes: u64,
11    /// Largest total of decompressed data per document.
12    pub max_decompressed_bytes: u64,
13    /// Most records/objects/elements per document.
14    pub max_objects: u64,
15    /// Deepest nesting (complex elements, cells, block references).
16    pub max_depth: u32,
17    /// Longest single string in bytes.
18    pub max_string_bytes: u32,
19    /// Most vertices / control points / knots in a single entity.
20    pub max_vertices: u32,
21    /// Most vertex-like items (vertices, control points, hatch edges, attributes) in the whole
22    /// document. Guards against inputs that reference the same data many times.
23    pub max_total_vertices: u64,
24}
25
26impl Default for Limits {
27    fn default() -> Self {
28        Self::new()
29    }
30}
31
32impl Limits {
33    /// The default limits (2 GiB input, 4 GiB decompressed, 20M objects, nesting depth 64).
34    pub const fn new() -> Self {
35        Self {
36            max_input_bytes: 2 << 30,
37            max_decompressed_bytes: 4 << 30,
38            max_objects: 20_000_000,
39            max_depth: 64,
40            max_string_bytes: 16 << 20,
41            max_vertices: 10_000_000,
42            max_total_vertices: 100_000_000,
43        }
44    }
45
46    /// Tighter resource limits suitable for untrusted multi-tenant web or API inputs
47    /// (64 MiB input, 256 MiB decompressed, 500k objects, max nesting depth 32).
48    pub const fn strict() -> Self {
49        Self {
50            max_input_bytes: 64 << 20,
51            max_decompressed_bytes: 256 << 20,
52            max_objects: 500_000,
53            max_depth: 32,
54            max_string_bytes: 4 << 20,
55            max_vertices: 1_000_000,
56            max_total_vertices: 5_000_000,
57        }
58    }
59
60    /// Sets [`Limits::max_input_bytes`].
61    pub const fn with_max_input_bytes(mut self, bytes: u64) -> Self {
62        self.max_input_bytes = bytes;
63        self
64    }
65
66    /// Sets [`Limits::max_decompressed_bytes`].
67    pub const fn with_max_decompressed_bytes(mut self, bytes: u64) -> Self {
68        self.max_decompressed_bytes = bytes;
69        self
70    }
71
72    /// Sets [`Limits::max_objects`].
73    pub const fn with_max_objects(mut self, count: u64) -> Self {
74        self.max_objects = count;
75        self
76    }
77
78    /// Sets [`Limits::max_depth`].
79    pub const fn with_max_depth(mut self, depth: u32) -> Self {
80        self.max_depth = depth;
81        self
82    }
83
84    /// Sets [`Limits::max_string_bytes`].
85    pub const fn with_max_string_bytes(mut self, bytes: u32) -> Self {
86        self.max_string_bytes = bytes;
87        self
88    }
89
90    /// Sets [`Limits::max_vertices`].
91    pub const fn with_max_vertices(mut self, count: u32) -> Self {
92        self.max_vertices = count;
93        self
94    }
95
96    /// Sets [`Limits::max_total_vertices`].
97    pub const fn with_max_total_vertices(mut self, count: u64) -> Self {
98        self.max_total_vertices = count;
99        self
100    }
101}
102
103/// Options accepted by every reader.
104#[derive(Debug, Clone, PartialEq, Default, serde::Serialize, serde::Deserialize)]
105#[serde(default)]
106#[non_exhaustive]
107pub struct ReadOptions {
108    /// Resource limits.
109    pub limits: Limits,
110    /// Keep original record bytes in [`crate::Entity::raw`].
111    pub keep_raw: bool,
112    /// Code page (WHATWG label, e.g. `"windows-1254"`) for 8-bit text when the file does not
113    /// declare one. `None` uses the reader's default (windows-1252).
114    pub fallback_codepage: Option<String>,
115}
116
117impl ReadOptions {
118    /// Creates default reader options.
119    pub fn new() -> Self {
120        Self::default()
121    }
122
123    /// Overrides the resource [`Limits`].
124    pub fn with_limits(mut self, limits: Limits) -> Self {
125        self.limits = limits;
126        self
127    }
128
129    /// Configures whether original native record bytes are retained in [`crate::Entity::raw`].
130    pub fn with_keep_raw(mut self, keep_raw: bool) -> Self {
131        self.keep_raw = keep_raw;
132        self
133    }
134
135    /// Sets the fallback 8-bit code page label (for example `"windows-1254"` or `"cp857"`).
136    pub fn with_fallback_codepage(mut self, codepage: impl Into<String>) -> Self {
137        self.fallback_codepage = Some(codepage.into());
138        self
139    }
140}