parse-net 0.7.0

Grammar for Internet message fields: RFC 9110 field syntax, media types, RFC 9651 Structured Fields, HTTP and RFC 5322 dates, negotiation, entity tags, ranges, Content-Disposition, Cache-Control, Forwarded, auth challenges, and the email lexical layer. no_std, no allocation, sans-IO, zero dependencies.
Documentation
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! The RFC 9651 section 4.2 parsing algorithms over a cursor.
//!
//! Every function takes the cursor at the position the RFC describes and
//! leaves it after what it consumed. Inner lists cannot nest (the grammar
//! allows one level), so there is no recursion anywhere: depth is at most 2.

use super::{BareItem, ByteSeq, Decimal, DisplayString, SfString};
use crate::{
    class,
    error::{ErrorKind, ParseError, Result},
    limits::Limits,
    scan::control_kind,
    utf8::Utf8,
};

/// The most digits an Integer may have.
const INTEGER_DIGITS: usize = 15;
/// The largest Integer magnitude.
pub(crate) const INTEGER_MAX: i64 = 999_999_999_999_999;

/// A position in a field value.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Cursor<'a> {
    pub(crate) s: &'a [u8],
    pub(crate) i: usize,
}

impl<'a> Cursor<'a> {
    pub(crate) const fn new(s: &'a [u8]) -> Self {
        Self { s, i: 0 }
    }

    pub(crate) fn peek(&self) -> Option<u8> {
        self.s.get(self.i).copied()
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.i >= self.s.len()
    }

    pub(crate) fn bump(&mut self) {
        self.i += 1;
    }

    pub(crate) fn fail(&self, kind: ErrorKind) -> ParseError {
        match self.peek() {
            Some(b) if crate::scan::is_control(b) => ParseError::new(control_kind(b), self.i),
            _ => ParseError::new(kind, self.i),
        }
    }

    /// Discards leading SP (section 4.2: only SP around the whole value and
    /// inside inner lists).
    pub(crate) fn skip_sp(&mut self) {
        while self.peek() == Some(b' ') {
            self.i += 1;
        }
    }

    /// Discards leading OWS (SP and HTAB, around list and dictionary
    /// separators).
    pub(crate) fn skip_ows(&mut self) {
        while matches!(self.peek(), Some(b' ' | b'\t')) {
            self.i += 1;
        }
    }

    fn slice(&self, from: usize) -> &'a [u8] {
        self.s.get(from..self.i).unwrap_or(&[])
    }
}

/// Section 4.2.3.3: a key.
pub(crate) fn key<'a>(c: &mut Cursor<'a>, limits: &Limits) -> Result<&'a str> {
    let start = c.i;
    match c.peek() {
        Some(b) if class::is_sf_key_start(b) => c.bump(),
        _ => return Err(c.fail(ErrorKind::InvalidKey)),
    }
    while c.peek().is_some_and(class::is_sf_key_char) {
        c.bump();
    }
    if c.i - start > usize::from(limits.key_len) {
        return Err(ParseError::new(ErrorKind::TooLong, start));
    }
    // Keys are ASCII by construction.
    Ok(core::str::from_utf8(c.slice(start)).unwrap_or(""))
}

/// Section 4.2.4: an Integer or a Decimal.
fn number(c: &mut Cursor<'_>) -> Result<BareItem<'static>> {
    let start = c.i;
    let negative = c.peek() == Some(b'-');
    if negative {
        c.bump();
    }
    if !c.peek().is_some_and(|b| b.is_ascii_digit()) {
        return Err(c.fail(ErrorKind::InvalidNumber));
    }
    let mut int: i64 = 0;
    let mut int_digits = 0usize;
    while let Some(d) = c.peek().filter(u8::is_ascii_digit) {
        int_digits += 1;
        if int_digits > INTEGER_DIGITS {
            return Err(ParseError::new(ErrorKind::NumberOverflow, c.i));
        }
        int = int * 10 + i64::from(d - b'0');
        c.bump();
    }
    if c.peek() != Some(b'.') {
        return Ok(BareItem::Integer(if negative { -int } else { int }));
    }
    // A Decimal: at most 12 integer digits and 1 to 3 fractional digits.
    if int_digits > 12 {
        return Err(ParseError::new(ErrorKind::NumberOverflow, c.i));
    }
    c.bump();
    let mut frac: i64 = 0;
    let mut frac_digits = 0u32;
    while let Some(d) = c.peek().filter(u8::is_ascii_digit) {
        frac_digits += 1;
        if frac_digits > 3 {
            return Err(ParseError::new(ErrorKind::NumberOverflow, c.i));
        }
        frac = frac * 10 + i64::from(d - b'0');
        c.bump();
    }
    if frac_digits == 0 {
        return Err(ParseError::new(ErrorKind::InvalidNumber, start));
    }
    let thousandths = int * 1000 + frac * 10_i64.pow(3 - frac_digits);
    Ok(BareItem::Decimal(Decimal(if negative {
        -thousandths
    } else {
        thousandths
    })))
}

/// Section 4.2.5: a String. Returns the raw content (escapes kept).
fn string<'a>(c: &mut Cursor<'a>, limits: &Limits) -> Result<SfString<'a>> {
    c.bump(); // the opening DQUOTE
    let start = c.i;
    let mut decoded = 0usize;
    let mut escapes = false;
    loop {
        match c.peek() {
            None => return Err(ParseError::new(ErrorKind::UnterminatedQuote, c.i)),
            Some(b'"') => break,
            Some(b'\\') => {
                c.bump();
                match c.peek() {
                    Some(b'"' | b'\\') => {
                        escapes = true;
                        c.bump();
                    }
                    None => return Err(ParseError::new(ErrorKind::UnterminatedQuote, c.i)),
                    Some(_) => return Err(c.fail(ErrorKind::InvalidQuotedPair)),
                }
            }
            Some(b) if class::is_sf_string_char(b) => c.bump(),
            Some(_) => return Err(c.fail(ErrorKind::InvalidByte)),
        }
        decoded += 1;
        if decoded > usize::from(limits.string_len) {
            return Err(ParseError::new(ErrorKind::TooLong, start));
        }
    }
    let raw = c.slice(start);
    c.bump(); // the closing DQUOTE
    Ok(SfString {
        // ASCII by construction.
        raw: core::str::from_utf8(raw).unwrap_or(""),
        escapes,
    })
}

/// Section 4.2.6: a Token.
fn token<'a>(c: &mut Cursor<'a>, limits: &Limits) -> Result<&'a str> {
    let start = c.i;
    c.bump(); // ALPHA or "*", checked by the caller
    while c.peek().is_some_and(class::is_sf_token_char) {
        c.bump();
    }
    if c.i - start > usize::from(limits.token_len) {
        return Err(ParseError::new(ErrorKind::TooLong, start));
    }
    Ok(core::str::from_utf8(c.slice(start)).unwrap_or(""))
}

/// Section 4.2.7: a Byte Sequence. Padding is optional and non-zero pad bits
/// are accepted, as the section says recipients should.
fn byte_seq<'a>(c: &mut Cursor<'a>, limits: &Limits) -> Result<ByteSeq<'a>> {
    c.bump(); // ":"
    let start = c.i;
    while c.peek().is_some_and(class::is_base64_char) {
        c.bump();
    }
    let body = c.i - start;
    let mut pad = 0usize;
    while c.peek() == Some(b'=') {
        pad += 1;
        c.bump();
    }
    match c.peek() {
        Some(b':') => {}
        None => return Err(ParseError::new(ErrorKind::UnterminatedQuote, c.i)),
        Some(_) => return Err(c.fail(ErrorKind::InvalidBase64)),
    }
    // A base64 text of `body` symbols decodes when `body % 4 != 1`; padding,
    // when present, completes the last quantum and nothing else.
    let valid = body % 4 != 1 && (pad == 0 || ((body + pad) % 4 == 0 && pad <= 2));
    if !valid {
        return Err(ParseError::new(ErrorKind::InvalidBase64, start + body));
    }
    let raw = c.slice(start);
    c.bump(); // ":"
    let seq = ByteSeq { raw };
    if seq.decoded_len() > limits.byte_seq_len as usize {
        return Err(ParseError::new(ErrorKind::TooLong, start));
    }
    Ok(seq)
}

/// Section 4.2.8: a Boolean.
fn boolean(c: &mut Cursor<'_>) -> Result<bool> {
    c.bump(); // "?"
    let v = match c.peek() {
        Some(b'1') => true,
        Some(b'0') => false,
        _ => return Err(c.fail(ErrorKind::InvalidByte)),
    };
    c.bump();
    Ok(v)
}

/// Section 4.2.9: a Date: `@` and an Integer, within years 1 to 9999 (D-030).
fn date(c: &mut Cursor<'_>) -> Result<i64> {
    c.bump(); // "@"
    let at = c.i;
    match number(c)? {
        BareItem::Integer(secs) => {
            if secs < crate::HttpDate::MIN.unix() || secs > crate::HttpDate::MAX.unix() {
                return Err(ParseError::new(ErrorKind::OutOfRange, at));
            }
            Ok(secs)
        }
        _ => Err(ParseError::new(ErrorKind::InvalidNumber, at)),
    }
}

/// Section 4.2.10: a Display String: lowercase `%xx` only, the decoded bytes
/// UTF-8 validated as they are read (no buffer).
fn display_string<'a>(c: &mut Cursor<'a>, limits: &Limits) -> Result<DisplayString<'a>> {
    c.bump(); // "%"
    if c.peek() != Some(b'"') {
        return Err(c.fail(ErrorKind::InvalidByte));
    }
    c.bump();
    let start = c.i;
    let mut utf8 = Utf8::new();
    let mut decoded = 0usize;
    loop {
        let b = match c.peek() {
            None => return Err(ParseError::new(ErrorKind::UnterminatedQuote, c.i)),
            Some(b'"') => break,
            Some(b'%') => {
                let at = c.i;
                let hex = |x: Option<u8>| {
                    x.filter(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
                        .and_then(class::hex_value)
                };
                let hi = hex(c.s.get(c.i + 1).copied());
                let lo = hex(c.s.get(c.i + 2).copied());
                let (Some(hi), Some(lo)) = (hi, lo) else {
                    return Err(ParseError::new(ErrorKind::InvalidPercentEncoding, at));
                };
                c.i += 3;
                if !utf8.push((hi << 4) | lo) {
                    return Err(ParseError::new(ErrorKind::InvalidUtf8, at));
                }
                decoded += 1;
                continue;
            }
            Some(b) if (0x20..=0x7e).contains(&b) => b,
            Some(_) => return Err(c.fail(ErrorKind::InvalidByte)),
        };
        // A literal byte is ASCII: it must not interrupt a multi-byte
        // character decoded from triples.
        if !utf8.is_complete() || !utf8.push(b) {
            return Err(ParseError::new(ErrorKind::InvalidUtf8, c.i));
        }
        c.bump();
        decoded += 1;
        if decoded > usize::from(limits.string_len) {
            return Err(ParseError::new(ErrorKind::TooLong, start));
        }
    }
    if !utf8.is_complete() {
        return Err(ParseError::new(ErrorKind::InvalidUtf8, c.i));
    }
    let raw = c.slice(start);
    c.bump(); // the closing DQUOTE
    Ok(DisplayString { raw })
}

/// Section 4.2.3.1: a bare item.
pub(crate) fn bare_item<'a>(c: &mut Cursor<'a>, limits: &Limits) -> Result<BareItem<'a>> {
    match c.peek() {
        Some(b'-' | b'0'..=b'9') => number(c),
        Some(b'"') => string(c, limits).map(BareItem::String),
        Some(b) if b == b'*' || b.is_ascii_alphabetic() => token(c, limits).map(BareItem::Token),
        Some(b':') => byte_seq(c, limits).map(BareItem::ByteSeq),
        Some(b'?') => boolean(c).map(BareItem::Boolean),
        Some(b'@') => date(c).map(BareItem::Date),
        Some(b'%') => display_string(c, limits).map(BareItem::DisplayString),
        _ => Err(c.fail(ErrorKind::InvalidByte)),
    }
}

/// Section 4.2.3.2: parameters. Calls `f` for each, in order; returns the
/// count.
pub(crate) fn params<'a>(
    c: &mut Cursor<'a>,
    limits: &Limits,
    mut f: impl FnMut(&'a str, BareItem<'a>) -> Result<()>,
) -> Result<usize> {
    let mut count = 0usize;
    while c.peek() == Some(b';') {
        let at = c.i;
        c.bump();
        c.skip_sp();
        let k = key(c, limits)?;
        let v = if c.peek() == Some(b'=') {
            c.bump();
            bare_item(c, limits)?
        } else {
            BareItem::Boolean(true)
        };
        count += 1;
        if count > usize::from(limits.params) {
            return Err(ParseError::new(ErrorKind::TooManyMembers, at));
        }
        f(k, v)?;
    }
    Ok(count)
}

/// The events a parse produces, for views and visitors alike.
pub(crate) trait Events<'a> {
    fn item(&mut self, bare: BareItem<'a>) -> Result<()>;
    fn param(&mut self, key: &'a str, value: BareItem<'a>) -> Result<()>;
    fn inner_start(&mut self) -> Result<()>;
    fn inner_end(&mut self) -> Result<()>;
    fn key(&mut self, key: &'a str) -> Result<()>;
}

/// Events that are discarded: validation only.
pub(crate) struct Nothing;

impl<'a> Events<'a> for Nothing {
    fn item(&mut self, _: BareItem<'a>) -> Result<()> {
        Ok(())
    }
    fn param(&mut self, _: &'a str, _: BareItem<'a>) -> Result<()> {
        Ok(())
    }
    fn inner_start(&mut self) -> Result<()> {
        Ok(())
    }
    fn inner_end(&mut self) -> Result<()> {
        Ok(())
    }
    fn key(&mut self, _: &'a str) -> Result<()> {
        Ok(())
    }
}

/// Section 4.2.1.2: an inner list, the cursor at `(`.
fn inner_list<'a>(c: &mut Cursor<'a>, limits: &Limits, ev: &mut impl Events<'a>) -> Result<()> {
    ev.inner_start()?;
    c.bump(); // "("
    let mut count = 0usize;
    loop {
        c.skip_sp();
        if c.peek() == Some(b')') {
            c.bump();
            ev.inner_end()?;
            let _ = params(c, limits, |k, v| ev.param(k, v))?;
            return Ok(());
        }
        if c.is_empty() {
            return Err(ParseError::new(ErrorKind::UnterminatedQuote, c.i));
        }
        let at = c.i;
        let bare = bare_item(c, limits)?;
        count += 1;
        if count > usize::from(limits.inner_list_members) {
            return Err(ParseError::new(ErrorKind::TooManyMembers, at));
        }
        ev.item(bare)?;
        let _ = params(c, limits, |k, v| ev.param(k, v))?;
        match c.peek() {
            Some(b' ' | b')') => {}
            None => return Err(ParseError::new(ErrorKind::UnterminatedQuote, c.i)),
            Some(_) => return Err(c.fail(ErrorKind::ExpectedSeparator)),
        }
    }
}

/// Section 4.2.1.1: an item or an inner list.
pub(crate) fn member<'a>(
    c: &mut Cursor<'a>,
    limits: &Limits,
    ev: &mut impl Events<'a>,
) -> Result<()> {
    if c.peek() == Some(b'(') {
        inner_list(c, limits, ev)
    } else {
        let bare = bare_item(c, limits)?;
        ev.item(bare)?;
        params(c, limits, |k, v| ev.param(k, v)).map(|_| ())
    }
}

/// After a list or dictionary member: OWS, then the end or `,` and OWS and
/// another member. Returns whether another member follows.
fn separator(c: &mut Cursor<'_>) -> Result<bool> {
    c.skip_ows();
    if c.is_empty() {
        return Ok(false);
    }
    if c.peek() != Some(b',') {
        return Err(c.fail(ErrorKind::ExpectedSeparator));
    }
    c.bump();
    c.skip_ows();
    if c.is_empty() {
        return Err(ParseError::new(ErrorKind::TrailingSeparator, c.i));
    }
    Ok(true)
}

/// Section 4.2.1: a list; returns the member count.
pub(crate) fn list<'a>(
    c: &mut Cursor<'a>,
    limits: &Limits,
    ev: &mut impl Events<'a>,
) -> Result<usize> {
    let mut count = 0usize;
    if c.is_empty() {
        return Ok(0);
    }
    loop {
        let at = c.i;
        count += 1;
        if count > usize::from(limits.list_members) {
            return Err(ParseError::new(ErrorKind::TooManyMembers, at));
        }
        member(c, limits, ev)?;
        if !separator(c)? {
            return Ok(count);
        }
    }
}

/// Section 4.2.2: a dictionary; returns the member count (duplicates
/// included: views keep every member, and lookups take the last).
pub(crate) fn dictionary<'a>(
    c: &mut Cursor<'a>,
    limits: &Limits,
    ev: &mut impl Events<'a>,
) -> Result<usize> {
    let mut count = 0usize;
    if c.is_empty() {
        return Ok(0);
    }
    loop {
        let at = c.i;
        count += 1;
        if count > usize::from(limits.dictionary_members) {
            return Err(ParseError::new(ErrorKind::TooManyMembers, at));
        }
        let k = key(c, limits)?;
        ev.key(k)?;
        if c.peek() == Some(b'=') {
            c.bump();
            member(c, limits, ev)?;
        } else {
            ev.item(BareItem::Boolean(true))?;
            let _ = params(c, limits, |k, v| ev.param(k, v))?;
        }
        if !separator(c)? {
            return Ok(count);
        }
    }
}

/// Section 4.2: the top level. Leading and trailing SP are discarded (SP
/// only); `body` parses the rest; anything left is an error.
pub(crate) fn field<'a, T>(
    value: &'a [u8],
    body: impl FnOnce(&mut Cursor<'a>) -> Result<T>,
) -> Result<(T, usize, usize)> {
    let mut c = Cursor::new(value);
    c.skip_sp();
    let start = c.i;
    // Trailing SP is discarded before parsing, so a list or dictionary does
    // not see it as a separator.
    let mut end = value.len();
    while end > start && value.get(end - 1) == Some(&b' ') {
        end -= 1;
    }
    let mut inner = Cursor {
        s: value.get(..end).unwrap_or(&[]),
        i: start,
    };
    let out = body(&mut inner)?;
    if !inner.is_empty() {
        return Err(inner.fail(ErrorKind::TrailingData));
    }
    Ok((out, start, end))
}