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
|
use crate::common::sized_buffer::Error as SizedBufferError;
use crate::common::sized_buffer::SizedBuffer;
use std::fmt;
#[derive(Debug)]
pub enum Error {
ExpectedArray(char),
ExpectedString(char),
InvalidLength,
Buffer(SizedBufferError),
}
#[derive(Debug)]
pub enum State {
Wait,
Done(Vec<Vec<u8>>),
}
pub struct Parser {
pub argv: Vec<Vec<u8>>,
argc_rem: u64,
arg_len: i64,
}
impl Parser {
pub fn new() -> Self {
Self {
argv: Vec::new(),
argc_rem: 0,
arg_len: -1,
}
}
pub fn parse<const N: usize>(
&mut self,
buffer: &mut SizedBuffer<[u8; N]>,
) -> Result<State, Error> {
if self.argc_rem == 0 {
assert_eq!(self.arg_len, -1);
/* argc_rem wasn't initialized yet. The current buffer was never read, we need to
* figure out the number of args. */
if buffer.peek() != b'*' {
return Err(Error::ExpectedArray(buffer.peek() as char));
}
let arr_lf_offset = match buffer.find_offset(b'\n') {
Some(offset) => offset,
/* Couldn't find \n, wait for more data */
None => return Ok(State::Wait),
};
self.argc_rem = read_data_len(buffer, arr_lf_offset)? as u64;
buffer.skip_byte(b'\n').map_err(Error::Buffer)?;
}
/* We already started reading a command. We can process the arguments. */
while self.argc_rem > 0 {
/* arg_len wasn't initialized yet. We need to find the size of the string argument. */
if self.arg_len == -1 {
if buffer.peek() != b'$' {
return Err(Error::ExpectedString(buffer.peek() as char));
}
let str_lf_offset = match buffer.find_offset(b'\n') {
Some(offset) => offset,
/* Couldn't find \n, wait for more data */
None => return Ok(State::Wait),
};
self.arg_len = read_data_len(buffer, str_lf_offset)? as i64;
buffer.skip_byte(b'\n').map_err(Error::Buffer)?;
} else {
match buffer.read_count(self.arg_len as usize) {
/* Not enough data, wait for more */
Err(_) => return Ok(State::Wait),
/* Push the new string onto the args list */
Ok(arg) => self.argv.push(arg.to_vec()),
}
buffer.skip_byte(b'\n').map_err(Error::Buffer)?;
self.argc_rem -= 1;
self.arg_len = -1;
}
}
let argv = self.argv.clone();
self.argv.clear();
Ok(State::Done(argv))
}
}
fn read_data_len<const N: usize>(
buffer: &mut SizedBuffer<[u8; N]>,
n: usize,
) -> Result<usize, Error> {
/* Read from 1 to skip the data tag */
let mut len: usize = 0;
for (i, x) in buffer.read_count_unchecked(n)[1..].iter().rev().enumerate() {
if *x < 48 || *x > 57 {
return Err(Error::InvalidLength);
}
len += (x - 48) as usize * 10usize.pow(i as u32);
}
Ok(len)
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let res = match self {
Error::ExpectedArray(got) => format!("Expected array, got {:?}.", *got as char),
Error::ExpectedString(got) => format!("Expected string, got {:?}.", *got as char),
Error::InvalidLength => "Invalid header size.".to_string(),
Error::Buffer(err) => format!("Error read or writing to/from the buffer: {err:?}"),
};
write!(f, "{}", res)
}
}
|