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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//! Allocation-free MIME media types and original header-field framing.
use structured_asn1::memory::{MemoryError, Work};
mod headers;
pub use headers::media_type_or_headers;
#[derive(Clone, Copy)]
enum Phase {
TypeStart,
Type,
Slash,
SubtypeStart,
Subtype,
Parameters,
AttributeStart,
Attribute,
Equals,
ValueStart,
Value,
AfterValue,
}
#[derive(Clone, Copy)]
enum Context {
Normal,
Comment { depth: usize, escaped: bool },
Quoted { escaped: bool },
}
/// Incremental allocation-free RFC2045 media-type syntax.
///
/// The default consumes original MIME octets and requires CRLF folding.
/// `XML_NORMALIZED = true` additionally accepts XML's already normalized LF.
/// This does not resolve registered media types or compare their semantics.
///
/// ```
/// use std::sync::Arc;
/// use structured_asn1::memory::{Memory, MemoryAccount};
/// use structured_cms::mime::Syntax;
/// let work = Memory::new(Arc::new(MemoryAccount::new(0, 1000)));
/// let mut value = Syntax::<false>::new();
/// assert!(value.feed("text/plain; charset=", &work).unwrap());
/// assert!(!value.complete());
/// assert!(value.feed("utf-8", &work).unwrap());
/// assert!(value.complete());
/// ```
pub struct Syntax<const XML_NORMALIZED: bool = false> {
valid: bool,
phase: Phase,
context: Context,
folding: u8,
}
impl<const XML_NORMALIZED: bool> Syntax<XML_NORMALIZED> {
/// Begins a value without allocation or input work.
#[must_use]
pub const fn new() -> Self {
Self {
valid: true,
phase: Phase::TypeStart,
context: Context::Normal,
folding: 0,
}
}
// RFC2045 §5.1 token/tspecials plus RFC822 §3.3/3.4 comments,
// quoted-pair and folding. This is MIME syntax, not HTTP's restricted-name
// grammar, registration lookup or media-type equivalence/normalization.
// https://www.rfc-editor.org/rfc/rfc2045.html#section-5.1
// https://www.rfc-editor.org/rfc/rfc822.html#section-3.3
fn byte(&mut self, byte: u8) -> bool {
if !byte.is_ascii() || (byte == b'\n' && self.folding != 1 && !XML_NORMALIZED) {
return false;
}
if self.folding != 0 {
if self.folding == 1 && byte == b'\n' {
self.folding = 2;
return true;
}
if self.folding == 2 && matches!(byte, b' ' | b'\t') {
self.folding = 0;
return true;
}
return false;
}
match self.context {
Context::Comment { depth, escaped } => {
if matches!(byte, b'\r' | b'\n') {
self.context = Context::Comment {
depth,
escaped: false,
};
self.folding = if byte == b'\r' { 1 } else { 2 };
} else if escaped {
self.context = Context::Comment {
depth,
escaped: false,
};
} else if byte == b'\\' {
self.context = Context::Comment {
depth,
escaped: true,
};
} else if byte == b'(' {
let Some(depth) = depth.checked_add(1) else {
return false;
};
self.context = Context::Comment {
depth,
escaped: false,
};
} else if byte == b')' {
self.context = if depth == 1 {
Context::Normal
} else {
Context::Comment {
depth: depth - 1,
escaped: false,
}
};
}
true
}
Context::Quoted { escaped } => {
if matches!(byte, b'\r' | b'\n') {
self.context = Context::Quoted { escaped: false };
self.folding = if byte == b'\r' { 1 } else { 2 };
} else if escaped {
self.context = Context::Quoted { escaped: false };
} else if byte == b'\\' {
self.context = Context::Quoted { escaped: true };
} else if byte == b'"' {
self.context = Context::Normal;
self.phase = Phase::AfterValue;
}
true
}
Context::Normal => self.normal(byte),
}
}
fn normal(&mut self, byte: u8) -> bool {
match byte {
b' ' | b'\t' => {
self.gap();
true
}
b'\r' | b'\n' => {
self.gap();
// XML 1.0 §2.11 normalizes physical CRLF to LF before
// Node::Text; character references can still carry CRLF.
// https://www.w3.org/TR/REC-xml/#sec-line-ends
self.folding = if byte == b'\r' { 1 } else { 2 };
true
}
b'(' => {
self.gap();
self.context = Context::Comment {
depth: 1,
escaped: false,
};
true
}
b'"' if matches!(self.phase, Phase::ValueStart) => {
self.context = Context::Quoted { escaped: false };
true
}
b'/' if matches!(self.phase, Phase::Type | Phase::Slash) => {
self.phase = Phase::SubtypeStart;
true
}
b';' if matches!(
self.phase,
Phase::Subtype | Phase::Parameters | Phase::Value | Phase::AfterValue
) =>
{
self.phase = Phase::AttributeStart;
true
}
b'=' if matches!(self.phase, Phase::Attribute | Phase::Equals) => {
self.phase = Phase::ValueStart;
true
}
_ if token(byte) => {
self.phase = match self.phase {
Phase::TypeStart | Phase::Type => Phase::Type,
Phase::SubtypeStart | Phase::Subtype => Phase::Subtype,
Phase::AttributeStart | Phase::Attribute => Phase::Attribute,
Phase::ValueStart | Phase::Value => Phase::Value,
_ => return false,
};
true
}
_ => false,
}
}
fn gap(&mut self) {
self.phase = match self.phase {
Phase::Type => Phase::Slash,
Phase::Subtype => Phase::Parameters,
Phase::Attribute => Phase::Equals,
Phase::Value => Phase::AfterValue,
other => other,
};
}
/// Admits the whole fragment before changing state, then consumes it.
/// Syntax failure is sticky across subsequent fragments. Resource failure
/// leaves the state unchanged. Input may be borrowed str or byte slices.
pub fn feed<W: Work>(
&mut self,
value: impl AsRef<[u8]>,
execution: &W,
) -> Result<bool, MemoryError> {
let value = value.as_ref();
// Each byte has bounded scalar dispatch only. Comment nesting is an
// iterative counter, never stack frames, allocations or recursive calls.
let cost = u64::try_from(value.len())
.ok()
.and_then(|n| n.checked_mul(4))
.ok_or(MemoryError::WorkExceeded)?;
execution.charge_work(cost)?;
if !self.valid {
return Ok(false);
}
for &byte in value {
if !self.byte(byte) {
self.valid = false;
return Ok(false);
}
}
Ok(true)
}
/// A complete MIME value, with no open parameter, quote, comment or fold.
#[must_use]
pub fn complete(&self) -> bool {
self.valid
&& self.folding == 0
&& matches!(self.context, Context::Normal)
&& matches!(
self.phase,
Phase::Subtype | Phase::Parameters | Phase::Value | Phase::AfterValue
)
}
}
fn token(byte: u8) -> bool {
matches!(byte, 33..=126)
&& !matches!(
byte,
b'(' | b')'
| b'<'
| b'>'
| b'@'
| b','
| b';'
| b':'
| b'\\'
| b'"'
| b'/'
| b'['
| b']'
| b'?'
| b'='
)
}
impl<const XML_NORMALIZED: bool> Default for Syntax<XML_NORMALIZED> {
fn default() -> Self {
Self::new()
}
}