parse-net 0.9.3

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
//! The text of a `phrase` (RFC 5322 section 3.2.5), such as a display name,
//! with its RFC 2047 encoded words decoded (section 5).

use super::words::{self, Text};
use crate::{
    class,
    error::{ErrorKind, ParseError, Result, err},
    limits::Limits,
    scan::{control_kind, is_control},
    text::{comment_end_folded, parse_quoted},
};

/// Whether `b` belongs to an atom of a phrase: `atext`, the `.` of
/// `obs-phrase`, or RFC 6532 UTF-8.
fn is_word_byte(b: u8) -> bool {
    WORD.get(usize::from(b)).copied().unwrap_or(false)
}

/// [`is_word_byte`] for every byte: one lookup instead of the class tests.
// Built at compile time; `b` runs over 0..256, the array's own bounds.
#[allow(clippy::indexing_slicing)]
const WORD: [bool; 256] = {
    let mut t = [false; 256];
    let mut b = 0usize;
    while b < 256 {
        t[b] = class::is_atext(b as u8) || b == b'.' as usize || b >= 0x80;
        b += 1;
    }
    t
};

/// The last word written.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Last {
    None,
    Text,
    Encoded,
}

/// Walks the phrase `v`, emitting its text.
fn walk(v: &[u8], limits: &Limits, emit: &mut impl FnMut(&[u8])) -> Result<()> {
    let mut text = Text::new();
    let (mut i, mut words) = (0usize, 0usize);
    let mut last = Last::None;
    // A comment since the last word: two encoded words with a comment between
    // them are not adjacent (RFC 2047 section 6.2), so a space separates them.
    let mut commented = false;
    while let Some(&b) = v.get(i) {
        let n = words::ws_len(v, i);
        if n > 0 {
            i += n;
            continue;
        }
        if b == b'(' {
            i = comment_end_folded(v, i, limits)?.0;
            commented = true;
            continue;
        }
        if b == b'"' {
            let (q, used) = parse_quoted(v.get(i..).unwrap_or(&[])).map_err(|e| e.shifted(i))?;
            if last != Last::None {
                emit(b" ");
            }
            // The content with its quoted pairs unescaped; a word inside a
            // quoted string is not decoded (RFC 2047 section 5).
            let raw = q.raw();
            if raw.is_ascii() {
                // `qdtext` and `quoted-pair` hold no control but HTAB in
                // ASCII, so the runs between escapes go out as they are.
                let mut rest = raw;
                while let Some(p) = rest.iter().position(|&c| c == b'\\') {
                    emit(rest.get(..p).unwrap_or(&[]));
                    emit(rest.get(p + 1..p + 2).unwrap_or(&[]));
                    rest = rest.get(p + 2..).unwrap_or(&[]);
                }
                emit(rest);
            } else {
                let mut k = 0usize;
                while let Some(&c) = raw.get(k) {
                    if c == b'\\' {
                        k += 1;
                    }
                    let c = raw.get(k).copied().unwrap_or(c);
                    text.check(c, i + 1 + k)?;
                    emit(&[c]);
                    k += 1;
                }
                if !text.is_complete() {
                    return err(ErrorKind::InvalidUtf8, i + used - 1);
                }
            }
            last = Last::Text;
            commented = false;
            i += used;
            continue;
        }
        if !is_word_byte(b) {
            let kind = if is_control(b) {
                control_kind(b)
            } else {
                ErrorKind::InvalidByte
            };
            return Err(ParseError::new(kind, i));
        }
        let rest = v.get(i..).unwrap_or(&[]);
        let end = i + rest
            .iter()
            .position(|&c| !is_word_byte(c))
            .unwrap_or(rest.len());
        // An encoded word is a whole atom between whitespace (RFC 2047
        // section 5); next to a special, a comment, or a quoted string, on
        // either side, it is text. An atom starts at the start, or after
        // whitespace (a fold ends in SP or HTAB), a `)`, or a `"`, so the
        // byte before it tells which.
        let alone = || {
            let before = i == 0 || matches!(v.get(i - 1), Some(b' ' | b'\t'));
            before && (end == v.len() || words::ws_len(v, end) > 0)
        };
        let encoded = if rest.starts_with(b"=?") && alone() {
            words::shape(v.get(..end).unwrap_or(&[]), i)?
        } else {
            None
        };
        if let Some(e) = encoded {
            words += 1;
            if words > usize::from(limits.encoded_words) {
                return err(ErrorKind::TooManyMembers, i);
            }
            if last == Last::Text || (last == Last::Encoded && commented) {
                emit(b" ");
            }
            words::decode_word(&e, i, emit)?;
            last = Last::Encoded;
        } else {
            if last != Last::None {
                emit(b" ");
            }
            let atom = v.get(i..end).unwrap_or(&[]);
            // An ASCII atom is `atext` and dots, all printable.
            if !atom.is_ascii() {
                for (k, &c) in atom.iter().enumerate() {
                    text.check(c, i + k)?;
                }
                if !text.is_complete() {
                    return err(ErrorKind::InvalidUtf8, end);
                }
            }
            emit(atom);
            last = Last::Text;
        }
        commented = false;
        i = end;
    }
    if last == Last::None {
        return err(ErrorKind::Empty, 0);
    }
    Ok(())
}

/// Decodes a `phrase` (RFC 5322 section 3.2.5), such as the display name
/// [`parse_mailbox`](super::parse_mailbox) returns raw, into `out` and returns
/// its text: comments are dropped, quoted strings lose their quotes and
/// their quoted pairs are unescaped, and the words are joined by one space;
/// an empty word, `""` or an encoded word with no text, still counts as one.
/// `CRLF WSP` folds are read as whitespace between words and inside
/// comments; a quoted string must be unfolded first
/// ([`unfold`](super::unfold)), as in
/// [`parse_mime_params`](super::parse_mime_params).
/// RFC 2047 encoded words are decoded where section 5 allows them in a
/// phrase: a whole atom with whitespace, or the start or end of the phrase,
/// on both sides, so never inside a quoted string or against a comment;
/// two encoded words with only whitespace between them are joined with none
/// (section 6.2). Encoded words are read as
/// [`decode_words`](super::decode_words) reads them, and every byte of the
/// text meets its rules: UTF-8, no control character but HTAB.
///
/// # Errors
///
/// [`ErrorKind::Empty`] for a phrase with no word (empty, or only comments
/// and whitespace); [`ErrorKind::InvalidByte`] for a special such as `,` or
/// `<` outside a quoted string or comment; a control kind for a control
/// byte, other than HTAB and the CRLF of a fold; [`ErrorKind::Control`] for
/// a C1 character; [`ErrorKind::InvalidUtf8`];
/// [`ErrorKind::UnterminatedQuote`]; [`ErrorKind::UnbalancedComment`],
/// [`ErrorKind::CommentTooDeep`]; the encoded-word errors of
/// [`decode_words`](super::decode_words), [`ErrorKind::TooManyMembers`]
/// beyond `max_encoded_words` among them; [`ErrorKind::BufferTooSmall`].
/// Nothing is written on error.
///
/// # Examples
///
/// ```
/// use parse_net::{mail, Limits};
///
/// let m = mail::parse_mailbox(b"\"Doe, Jane\" (Sales) <jane@example.com>", &Limits::DEFAULT)?;
/// let mut buf = [0u8; 64];
/// let name = mail::decode_phrase(m.display_name.unwrap_or_default(), &Limits::DEFAULT, &mut buf)?;
/// assert_eq!(name, "Doe, Jane");
///
/// let name = mail::decode_phrase(b"=?UTF-8?Q?Ren=C3=A9e?= \"O'Neil\"", &Limits::DEFAULT, &mut buf)?;
/// assert_eq!(name, "Ren\u{e9}e O'Neil");
/// # Ok::<(), parse_net::ParseError>(())
/// ```
pub fn decode_phrase<'b>(value: &[u8], limits: &Limits, out: &'b mut [u8]) -> Result<&'b str> {
    let mut len = 0usize;
    walk(value, limits, &mut |b: &[u8]| len += b.len())?;
    let Some(dst) = out.get_mut(..len) else {
        return err(ErrorKind::BufferTooSmall, 0);
    };
    let mut n = 0usize;
    walk(value, limits, &mut |b: &[u8]| {
        if let Some(d) = dst.get_mut(n..n + b.len()) {
            d.copy_from_slice(b);
        }
        n += b.len();
    })?;
    // The first walk checked the text; this is the safe conversion.
    core::str::from_utf8(dst).map_err(|e| ParseError::new(ErrorKind::InvalidUtf8, e.valid_up_to()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{string::String, vec, vec::Vec};

    fn dec(v: &[u8]) -> Result<String> {
        let mut buf = vec![0u8; 1024];
        decode_phrase(v, &Limits::DEFAULT, &mut buf).map(String::from)
    }

    fn fails(v: &[u8]) -> (ErrorKind, usize) {
        let e = dec(v).unwrap_err();
        (e.kind(), e.offset())
    }

    #[test]
    fn test_decode_phrase_words() {
        assert_eq!(dec(b"Jane Doe").unwrap(), "Jane Doe");
        assert_eq!(dec(b"  Jane \t  Doe  ").unwrap(), "Jane Doe");
        assert_eq!(dec(b"Jane\r\n Doe").unwrap(), "Jane Doe");
        assert_eq!(dec(b"John Q. Public").unwrap(), "John Q. Public");
        assert_eq!(dec(b"\"Doe, John\"").unwrap(), "Doe, John");
        assert_eq!(dec(b"\"a \\\"q\\\" \\\\ b\"").unwrap(), "a \"q\" \\ b");
        assert_eq!(dec(b"Jane\"Q\"Doe").unwrap(), "Jane Q Doe");
        assert_eq!(dec(b"\"  spaced  \" x").unwrap(), "  spaced   x");
        assert_eq!(dec(b"\"\"").unwrap(), "");
        assert_eq!(dec(b"John (the (nested) one) Doe").unwrap(), "John Doe");
        assert_eq!(dec(b"(lead)John(trail)").unwrap(), "John");
        assert_eq!(
            dec("J\u{f6}rg M\u{fc}ller".as_bytes()).unwrap(),
            "J\u{f6}rg M\u{fc}ller"
        );
        assert_eq!(
            dec("\"\u{e9}t\u{e9}\"".as_bytes()).unwrap(),
            "\u{e9}t\u{e9}"
        );
        assert_eq!(dec(b"\"tab\there\"").unwrap(), "tab\there");
    }

    #[test]
    fn test_decode_phrase_encoded_words() {
        assert_eq!(
            dec(b"=?UTF-8?Q?Caf=C3=A9?= Owner").unwrap(),
            "Caf\u{e9} Owner"
        );
        assert_eq!(
            dec(b"Owner =?utf-8?q?Caf=C3=A9?=").unwrap(),
            "Owner Caf\u{e9}"
        );
        assert_eq!(dec(b"=?UTF-8?Q?a?= =?UTF-8?Q?b?=").unwrap(), "ab");
        assert_eq!(dec(b"=?UTF-8?Q?a?=\r\n =?UTF-8?Q?b?=").unwrap(), "ab");
        assert_eq!(dec(b"=?UTF-8?Q?a?= (c) =?UTF-8?Q?b?=").unwrap(), "a b");
        assert_eq!(dec(b"=?UTF-8?Q?a_b?=").unwrap(), "a b");
        assert_eq!(
            dec(b"=?ISO-8859-1?Q?Andr=E9?= Pirard").unwrap(),
            "Andr\u{e9} Pirard"
        );
        assert_eq!(
            dec(b"=?UTF-8?B?5Lya6K2w44Gu44GK55+l44KJ44Gb?=").unwrap(),
            "\u{4f1a}\u{8b70}\u{306e}\u{304a}\u{77e5}\u{3089}\u{305b}"
        );
        // Inside a quoted string, or next to a special, it is text.
        assert_eq!(dec(b"\"=?UTF-8?Q?a?=\"").unwrap(), "=?UTF-8?Q?a?=");
        assert_eq!(dec(b"=?UTF-8?Q?a?=(c)").unwrap(), "=?UTF-8?Q?a?=");
        assert_eq!(dec(b"=?UTF-8?Q?a?=\"b\"").unwrap(), "=?UTF-8?Q?a?= b");
        // The same on the side before the word.
        assert_eq!(dec(b"(c)=?UTF-8?Q?a?=").unwrap(), "=?UTF-8?Q?a?=");
        assert_eq!(dec(b"\"x\"=?UTF-8?Q?a?=").unwrap(), "x =?UTF-8?Q?a?=");
        assert_eq!(dec(b"(c) =?UTF-8?Q?a?=").unwrap(), "a");
        assert_eq!(dec(b"\"x\" =?UTF-8?Q?a?=").unwrap(), "x a");
        assert_eq!(
            dec(b"=?UTF-8?Q?a?= (c)=?UTF-8?Q?b?=").unwrap(),
            "a =?UTF-8?Q?b?="
        );
        // An empty encoded word is an empty word, as an empty quoted string is.
        assert_eq!(dec(b"=?UTF-8?B??=").unwrap(), "");
        assert_eq!(dec(b"x =?UTF-8?Q??= y").unwrap(), "x  y");
        assert_eq!(dec(b"x \"\" y").unwrap(), "x  y");
        // Not the shape of an encoded word: text.
        assert_eq!(dec(b"=?x").unwrap(), "=?x");
        assert_eq!(dec(b"a=?UTF-8?Q?b?=").unwrap(), "a=?UTF-8?Q?b?=");
    }

    #[test]
    fn test_decode_phrase_errors() {
        assert_eq!(fails(b""), (ErrorKind::Empty, 0));
        assert_eq!(fails(b"   "), (ErrorKind::Empty, 0));
        assert_eq!(fails(b"(only a comment)"), (ErrorKind::Empty, 0));
        assert_eq!(fails(b"Doe, John"), (ErrorKind::InvalidByte, 3));
        assert_eq!(fails(b"a <b>"), (ErrorKind::InvalidByte, 2));
        assert_eq!(fails(b"a@b"), (ErrorKind::InvalidByte, 1));
        assert_eq!(fails(b"a\x01b").0, ErrorKind::Control);
        assert_eq!(fails(b"a\rb").0, ErrorKind::BareCr);
        assert_eq!(fails(b"a\nb").0, ErrorKind::LineFeed);
        assert_eq!(fails(b"a\0b").0, ErrorKind::Nul);
        assert_eq!(fails(b"\"open"), (ErrorKind::UnterminatedQuote, 5));
        assert_eq!(fails(b"a (open").0, ErrorKind::UnbalancedComment);
        assert_eq!(fails(b"a \xff"), (ErrorKind::InvalidUtf8, 2));
        assert_eq!(fails(b"a \xc3 b"), (ErrorKind::InvalidUtf8, 3));
        assert_eq!(fails(b"\"\xc3\" b"), (ErrorKind::InvalidUtf8, 2));
        assert_eq!(fails(b"a\xc2\x85b"), (ErrorKind::Control, 2));
        assert_eq!(fails(b"\"\\\xc2\\\x85\"").0, ErrorKind::Control);
        assert_eq!(fails(b"=?KOI8-R?Q?a?=").0, ErrorKind::UnsupportedCharset);
        assert_eq!(fails(b"x =?UTF-8?Q?=0D?=").0, ErrorKind::ControlInDecoded);
        assert_eq!(fails(b"=?UTF-8?B?YQ=?=").0, ErrorKind::InvalidEncodedWord);
        let l = Limits::DEFAULT.max_encoded_words(1);
        let mut buf = [0u8; 16];
        assert_eq!(
            decode_phrase(b"=?UTF-8?Q?a?= =?UTF-8?Q?b?=", &l, &mut buf)
                .unwrap_err()
                .kind(),
            ErrorKind::TooManyMembers
        );
        // Nothing is written when the buffer is short.
        let mut small = [b'#'; 4];
        assert_eq!(
            decode_phrase(b"Jane Doe", &Limits::DEFAULT, &mut small)
                .unwrap_err()
                .kind(),
            ErrorKind::BufferTooSmall
        );
        assert_eq!(small, [b'#'; 4]);
    }

    /// A reader with no shortcuts: words made only of atoms, quoted
    /// strings, whitespace, and comments without nesting, so the expected
    /// text is the words joined by one space.
    fn reference(words: &[(bool, String)]) -> String {
        let mut out = String::new();
        for (k, (_, w)) in words.iter().enumerate() {
            if k > 0 {
                out.push(' ');
            }
            out.push_str(w);
        }
        out
    }

    proptest::proptest! {
        #[test]
        fn decode_phrase_matches_reference(
            words in proptest::collection::vec(
                (proptest::bool::ANY, "[ -~\u{e9}\u{4f1a}]{0,8}", "[a-zA-Z0-9!#$%&'*+/=?^_`{|}~.\u{e9}-]{1,8}"),
                1..6,
            ),
            gaps in proptest::collection::vec(0usize..4, 6),
        ) {
            let mut raw = Vec::new();
            let mut expect = Vec::new();
            for (k, (quoted, q, atom)) in words.iter().enumerate() {
                if k > 0 {
                    match gaps.get(k).copied().unwrap_or(0) {
                        0 => raw.push(b' '),
                        1 => raw.extend_from_slice(b"\t "),
                        2 => raw.extend_from_slice(b" (note) "),
                        _ => raw.extend_from_slice(b"\r\n "),
                    }
                }
                if *quoted {
                    raw.push(b'"');
                    for c in q.bytes() {
                        if c == b'"' || c == b'\\' {
                            raw.push(b'\\');
                        }
                        raw.push(c);
                    }
                    raw.push(b'"');
                    expect.push((true, q.clone()));
                } else {
                    raw.extend_from_slice(atom.as_bytes());
                    expect.push((false, atom.clone()));
                }
            }
            // Atoms that happen to look like encoded words decode: keep
            // to the plain case here.
            proptest::prop_assume!(!expect.iter().any(|(q, w)| !q && w.starts_with("=?")));
            proptest::prop_assert_eq!(dec(&raw).unwrap(), reference(&expect));
        }

        #[test]
        fn decode_phrase_never_panics(v in proptest::collection::vec(proptest::num::u8::ANY, 0..64)) {
            let mut buf = [0u8; 256];
            if let Ok(s) = decode_phrase(&v, &Limits::DEFAULT, &mut buf) {
                proptest::prop_assert!(!s.chars().any(|c| c.is_control() && c != '\t'));
            }
        }
    }
}