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
use crate::{
PacketLine, {DELIMITER_LINE, FLUSH_LINE, MAX_DATA_LEN, MAX_LINE_LEN, RESPONSE_END_LINE, U16_HEX_BYTES},
};
use bstr::BString;
use quick_error::quick_error;
quick_error! {
#[derive(Debug)]
pub enum Error {
HexDecode(err: String) {
display("Failed to decode the first four hex bytes indicating the line length: {}", err)
}
DataLengthLimitExceeded(length_in_bytes: usize) {
display("The data received claims to be larger than than the maximum allowed size: got {}, exceeds {}", length_in_bytes, MAX_DATA_LEN)
}
DataIsEmpty {
display("Received an invalid empty line")
}
InvalidLineLength {
display("Received an invalid line of length 3")
}
Line(data: BString, bytes_consumed: usize) {
display("{}", data)
}
NotEnoughData(bytes_needed: usize) {
display("Needing {} additional bytes to decode the line successfully", bytes_needed)
}
}
}
#[derive(Debug, Clone)]
pub enum Stream<'a> {
Complete {
line: PacketLine<'a>,
bytes_consumed: usize,
},
Incomplete {
bytes_needed: usize,
},
}
pub enum PacketLineOrWantedSize<'a> {
Line(PacketLine<'a>),
Wanted(u16),
}
pub fn hex_prefix(four_bytes: &[u8]) -> Result<PacketLineOrWantedSize<'_>, Error> {
debug_assert_eq!(four_bytes.len(), 4, "need four hex bytes");
for (line_bytes, line_type) in &[
(FLUSH_LINE, PacketLine::Flush),
(DELIMITER_LINE, PacketLine::Delimiter),
(RESPONSE_END_LINE, PacketLine::ResponseEnd),
] {
if four_bytes == *line_bytes {
return Ok(PacketLineOrWantedSize::Line(*line_type));
}
}
let mut buf = [0u8; U16_HEX_BYTES / 2];
hex::decode_to_slice(four_bytes, &mut buf).map_err(|err| Error::HexDecode(err.to_string()))?;
let wanted_bytes = u16::from_be_bytes(buf);
if wanted_bytes == 3 {
return Err(Error::InvalidLineLength);
}
if wanted_bytes == 4 {
return Err(Error::DataIsEmpty);
}
debug_assert!(
wanted_bytes as usize > U16_HEX_BYTES,
"by now there should be more wanted bytes than prefix bytes"
);
Ok(PacketLineOrWantedSize::Wanted(wanted_bytes - U16_HEX_BYTES as u16))
}
pub fn to_data_line(data: &[u8]) -> Result<PacketLine<'_>, Error> {
if data.len() > MAX_LINE_LEN {
return Err(Error::DataLengthLimitExceeded(data.len()));
}
Ok(PacketLine::Data(data))
}
pub fn streaming(data: &[u8]) -> Result<Stream<'_>, Error> {
let data_len = data.len();
if data_len < U16_HEX_BYTES {
return Ok(Stream::Incomplete {
bytes_needed: U16_HEX_BYTES - data_len,
});
}
let wanted_bytes = match hex_prefix(&data[..U16_HEX_BYTES])? {
PacketLineOrWantedSize::Wanted(s) => s as usize,
PacketLineOrWantedSize::Line(line) => {
return Ok(Stream::Complete {
line,
bytes_consumed: 4,
})
}
} + U16_HEX_BYTES;
if wanted_bytes > MAX_LINE_LEN {
return Err(Error::DataLengthLimitExceeded(wanted_bytes));
}
if data_len < wanted_bytes {
return Ok(Stream::Incomplete {
bytes_needed: wanted_bytes - data_len,
});
}
Ok(Stream::Complete {
line: to_data_line(&data[U16_HEX_BYTES..wanted_bytes])?,
bytes_consumed: wanted_bytes,
})
}
pub fn all_at_once(data: &[u8]) -> Result<PacketLine<'_>, Error> {
match streaming(data)? {
Stream::Complete { line, .. } => Ok(line),
Stream::Incomplete { bytes_needed } => Err(Error::NotEnoughData(bytes_needed)),
}
}