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
use crate::decoders::DecodeResult;
use super::message::MessageStream;
pub fn seek_next_part(stream: &mut MessageStream, boundary: &[u8]) -> bool {
if !boundary.is_empty() {
let mut pos = stream.pos;
let mut match_count = 0;
for ch in &stream.data[pos..] {
pos += 1;
if ch == &boundary[match_count] {
match_count += 1;
if match_count == boundary.len() {
stream.pos = 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<'x>(
stream: &MessageStream<'x>,
start_pos: usize,
boundary: &[u8],
_is_word: bool,
) -> (usize, DecodeResult) {
let mut read_pos = start_pos;
if !boundary.is_empty() {
let mut match_count = 0;
for ch in &stream.data[read_pos..] {
read_pos += 1;
if ch == &boundary[match_count] {
match_count += 1;
if match_count == boundary.len() {
if is_boundary_end(stream, read_pos) {
let match_pos = read_pos - match_count;
return (
read_pos - start_pos,
if start_pos < match_pos {
DecodeResult::Borrowed((start_pos, match_pos))
} else {
DecodeResult::Empty
},
);
} else {
match_count = 0;
}
}
continue;
} else if match_count > 0 {
if ch == &boundary[0] {
match_count = 1;
continue;
} else {
match_count = 0;
}
}
}
(0, DecodeResult::Empty)
} else if start_pos < stream.data.len() {
(
stream.data.len() - start_pos,
DecodeResult::Borrowed((start_pos, stream.data.len())),
)
} else {
(0, DecodeResult::Empty)
}
}
#[inline(always)]
pub fn is_boundary_end(stream: &MessageStream, pos: usize) -> bool {
matches!(
stream.data.get(pos..),
Some([b'\n' | b'\r' | b' ' | b'\t', ..]) | Some([b'-', b'-', ..]) | Some([]) | None
)
}
pub fn skip_multipart_end(stream: &mut MessageStream) -> bool {
match stream.data.get(stream.pos..stream.pos + 2) {
Some(b"--") => {
if let Some(byte) = stream.data.get(stream.pos + 2) {
if !(*byte).is_ascii_whitespace() {
return false;
}
}
stream.pos += 2;
true
}
_ => false,
}
}
#[inline(always)]
pub fn skip_crlf(stream: &mut MessageStream) {
for ch in &stream.data[stream.pos..] {
match ch {
b'\r' | b' ' | b'\t' => stream.pos += 1,
b'\n' => {
stream.pos += 1;
break;
}
_ => break,
}
}
}
#[inline(always)]
pub fn seek_crlf_end(stream: &MessageStream, mut start_pos: usize) -> usize {
for ch in &stream.data[start_pos..] {
if ch.is_ascii_whitespace() {
start_pos += 1;
} else {
break;
}
}
start_pos
}