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
use std::{borrow::Cow, cell::Cell};
pub struct MessageStream<'x> {
pub data: &'x [u8],
pub pos: Cell<usize>,
}
impl<'x> MessageStream<'x> {
pub fn new(data: &'x [u8]) -> MessageStream<'x> {
MessageStream {
data,
pos: 0.into(),
}
}
#[inline(always)]
pub fn get_bytes(&self, from: usize, to: usize) -> Option<&'x [u8]> {
self.data.get(from..to)
}
pub fn get_string(&self, from: usize, to: usize) -> Option<Cow<'x, str>> {
String::from_utf8_lossy(self.data.get(from..to)?).into()
}
#[inline(always)]
pub fn is_eof(&self) -> bool {
self.pos.get() >= self.data.len()
}
#[inline(always)]
pub fn set_pos(&self, pos: usize) {
self.pos.set(pos);
}
#[inline(always)]
pub fn get_pos(&self) -> usize {
self.pos.get()
}
#[inline(always)]
pub fn next(&self) -> Option<&'x u8> {
let pos = self.pos.get();
let result = self.data.get(pos);
self.pos.set(pos + 1);
result
}
#[inline(always)]
pub fn peek(&self) -> Option<&'x u8> {
self.data.get(self.pos.get())
}
#[inline(always)]
pub fn advance(&self, pos: usize) {
self.pos.set(self.pos.get() + pos)
}
pub fn seek_next_part(&self, boundary: &[u8]) -> bool {
if !boundary.is_empty() {
let mut pos = self.pos.get();
let mut match_count = 0;
for ch in self.data[pos..].iter() {
pos += 1;
if ch == &boundary[match_count] {
match_count += 1;
if match_count == boundary.len() {
self.pos.set(pos);
return true;
} else {
continue;
}
} else if match_count > 0 {
if ch == &boundary[0] {
match_count = 1;
continue;
} else {
match_count = 0;
}
}
}
}
false
}
pub fn get_bytes_to_boundary(&self, boundary: &[u8]) -> (bool, Option<Cow<'x, [u8]>>) {
let mut pos = self.pos.get();
let start_pos = pos;
return if !boundary.is_empty() {
let mut match_count = 0;
for ch in self.data[pos..].iter() {
pos += 1;
if ch == &boundary[match_count] {
match_count += 1;
if match_count == boundary.len() {
if self.is_boundary_end(pos) {
let match_pos = pos - match_count;
self.pos.set(pos);
return (
true,
if start_pos < match_pos {
Cow::from(&self.data[start_pos..match_pos]).into()
} else {
None
},
);
} else {
match_count = 0;
}
}
continue;
} else if match_count > 0 {
if ch == &boundary[0] {
match_count = 1;
continue;
} else {
match_count = 0;
}
}
}
(false, None)
} else if pos < self.data.len() {
self.pos.set(self.data.len());
(true, Cow::from(&self.data[start_pos..]).into())
} else {
(false, None)
};
}
#[inline(always)]
pub fn skip_crlf(&self) {
let mut pos = self.pos.get();
for ch in self.data[pos..].iter() {
match ch {
b'\r' | b' ' | b'\t' => pos += 1,
b'\n' => {
self.pos.set(pos + 1);
break;
}
_ => break,
}
}
}
#[inline(always)]
pub fn skip_byte(&self, ch: &u8) -> bool {
let pos = self.pos.get();
if self.data.get(pos) == Some(ch) {
self.pos.set(pos + 1);
true
} else {
false
}
}
#[inline(always)]
pub fn is_boundary_end(&self, pos: usize) -> bool {
matches!(
self.data.get(pos..),
Some([b'\n' | b'\r' | b' ' | b'\t', ..]) | Some([b'-', b'-', ..]) | Some([]) | None
)
}
pub fn skip_multipart_end(&self) -> bool {
let pos = self.pos.get();
match self.data.get(pos..pos + 2) {
Some(b"--") => {
if let Some(byte) = self.data.get(pos + 2) {
if !(*byte).is_ascii_whitespace() {
return false;
}
}
self.pos.set(pos + 2);
true
}
_ => false,
}
}
pub fn rewind(&self, r: usize) {
self.pos.set(self.pos.get() - r);
}
}