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
/*
 * Copyright Stalwart Labs Ltd. See the COPYING
 * file at the top-level directory of this distribution.
 *
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
 * option. This file may not be copied, modified, or distributed
 * except according to those terms.
 */

use std::{iter::Peekable, ops::Range, slice::Iter};

pub mod fields;
pub mod header;
pub mod message;
pub mod mime;
pub mod preview;

pub struct MessageStream<'x> {
    data: &'x [u8],
    iter: Peekable<Iter<'x, u8>>,
    pos: usize,
    restore_pos: usize,
}

impl<'x> MessageStream<'x> {
    pub fn new(data: &'x [u8]) -> MessageStream<'x> {
        MessageStream {
            data,
            iter: data.iter().peekable(),
            pos: 0,
            restore_pos: 0,
        }
    }

    #[inline(always)]
    pub fn peek(&mut self) -> Option<&&u8> {
        self.iter.peek()
    }

    #[inline(always)]
    pub fn offset(&self) -> usize {
        std::cmp::min(self.pos, self.data.len())
    }

    #[inline(always)]
    pub fn remaining(&self) -> usize {
        self.data.len() - self.offset()
    }

    #[inline(always)]
    pub fn checkpoint(&mut self) {
        self.restore_pos = self.offset();
    }

    #[inline(always)]
    pub fn restore(&mut self) {
        self.iter = self.data[self.restore_pos..].iter().peekable();
        self.pos = self.restore_pos;
        self.restore_pos = 0;
    }

    #[inline(always)]
    pub fn reset(&mut self) {
        self.restore_pos = 0;
    }

    #[inline(always)]
    pub fn peek_bytes(&self, len: usize) -> Option<&[u8]> {
        let pos = self.offset();
        self.data.get(pos..pos + len)
    }

    #[inline(always)]
    pub fn peek_char(&mut self, ch: u8) -> bool {
        matches!(self.peek(), Some(&&ch_) if ch_ == ch)
    }

    #[inline(always)]
    pub fn skip_bytes(&mut self, len: usize) {
        self.pos += len;
        self.iter = self.data[self.pos..].iter().peekable();
    }

    #[inline(always)]
    pub fn try_skip(&mut self, bytes: &[u8]) -> bool {
        if self.peek_bytes(bytes.len()) == Some(bytes) {
            self.skip_bytes(bytes.len());
            true
        } else {
            false
        }
    }

    #[inline(always)]
    pub fn try_skip_char(&mut self, ch: u8) -> bool {
        if self.peek_char(ch) {
            self.next();
            true
        } else {
            false
        }
    }

    #[inline(always)]
    pub fn get_bytes(&self, range: Range<usize>) -> &'x [u8] {
        &self.data[range]
    }

    #[inline(always)]
    pub fn seek_end(&mut self) {
        self.pos = self.data.len();
        self.iter = [][..].iter().peekable();
    }

    #[inline(always)]
    pub fn next_is_space(&mut self) -> bool {
        matches!(self.next(), Some(b' ' | b'\t'))
    }

    #[inline(always)]
    pub fn peek_next_is_space(&mut self) -> bool {
        matches!(self.peek(), Some(b' ' | b'\t'))
    }

    #[inline(always)]
    pub fn try_next_is_space(&mut self) -> bool {
        if self.peek_next_is_space() {
            self.next();
            true
        } else {
            false
        }
    }

    #[allow(clippy::len_without_is_empty)]
    #[inline(always)]
    pub fn len(&self) -> usize {
        self.data.len()
    }

    #[inline(always)]
    pub fn is_eof(&mut self) -> bool {
        self.iter.peek().is_none()
    }
}

impl<'x> Iterator for MessageStream<'x> {
    type Item = &'x u8;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        self.pos += 1;
        self.iter.next()
    }
}