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
enum SlipChar {
End = 0xC0,
Esc = 0xDB,
EscEnd = 0xDC,
EscEsc = 0xDD,
}
static HEADER: &'static [u8] = &[SlipChar::End as u8, SlipChar::Esc as u8,
SlipChar::EscEnd as u8, SlipChar::EscEsc as u8];
pub fn encode(input: &[u8], output: &mut [u8]) -> Result<usize, ()> {
for i in 0..4 {
output[i] = HEADER[i];
}
let mut offset = 4;
for i in 0..input.len() {
match input[i] {
0xDB => {
output[offset] = SlipChar::Esc as u8;
output[offset+1] = SlipChar::EscEsc as u8;
offset += 1;
},
0xC0 => {
output[offset] = SlipChar::Esc as u8;
output[offset+1] = SlipChar::EscEnd as u8;
offset += 1;
},
_ => {
output[offset] = input[i];
},
}
offset += 1;
}
output[offset] = SlipChar::End as u8;
offset += 1;
Ok(offset)
}
pub struct Slip {
esc_seq: Vec<u8>,
}
impl Slip {
pub fn new() -> Self {
Slip {
esc_seq: Vec::with_capacity(4),
}
}
pub fn decode<'a>(&mut self, input: &'a [u8], output: &'a mut [u8]) -> Result<(usize, &'a [u8]), ()> {
let mut in_byte = 0;
let mut out_byte = 0;
for i in 0..input.len() {
if out_byte > output.len() {
in_byte = i;
break;
}
if self.esc_seq.len() > 0 {
if self.esc_seq[0] == SlipChar::End as u8 {
if input[i] == HEADER[self.esc_seq.len()] {
self.esc_seq.push(input[i]);
if self.esc_seq.len() == HEADER.len() {
self.esc_seq.drain(..);
in_byte = i + 1;
break;
}
} else {
self.esc_seq.pop();
continue;
}
} else {
match input[i] {
0xDC => {
output[out_byte] = SlipChar::End as u8
},
0xDD => {
output[out_byte] = SlipChar::Esc as u8
},
_ => {
panic!("bad escape character");
},
}
self.esc_seq.drain(..);
}
} else {
match input[i] {
0xDB => {
self.esc_seq.push(SlipChar::Esc as u8);
},
0xC0 => {
self.esc_seq.push(SlipChar::End as u8);
in_byte = i + 1;
break;
},
_ => {
output[out_byte] = input[i];
out_byte = out_byte + 1;
},
}
}
in_byte = i + 1;
}
Ok((in_byte, &output[..out_byte]))
}
pub fn decode_packet<'a>(&mut self, input: &'a [u8], output: &'a mut [u8]) -> Result<(usize, &'a [u8]), ()> {
let mut offset = 0;
{
let context = self.decode(&input[offset..], output).unwrap();
offset = context.0;
}
{
let context = self.decode(&input[offset..], output).unwrap();
offset = offset + context.0;
}
let context = self.decode(&input[offset..], output).unwrap();
offset = offset + context.0;
Ok((offset, context.1))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_encode() {
let mut output: [u8; 200] = [0; 200];
let bytes_written = encode(&[0;0], &mut output).unwrap();
let expected = [0xc0, 0xdb, 0xdc, 0xdd, 0xc0];
assert_eq!(&expected, &output[..bytes_written]);
}
#[test]
fn empty_decode() {
let slipped = vec![0xc0, 0xdb, 0xdc, 0xdd, 0xc0];
let mut output: [u8; 200] = [0; 200];
let input = slipped.as_slice();
let mut offset = 0;
let mut slip = Slip::new();
{
let context = slip.decode(&input[offset..], &mut output).unwrap();
assert_eq!(1, context.0);
assert_eq!(&[0;0], context.1);
offset = context.0;
}
{
let context = slip.decode(&input[offset..], &mut output).unwrap();
assert_eq!(3, context.0);
assert_eq!(&[0;0], context.1);
offset = offset + context.0;
}
{
let context = slip.decode(&input[offset..], &mut output).unwrap();
assert_eq!(1, context.0);
assert_eq!(&[0;0], context.1);
offset = offset + context.0;
}
assert_eq!(slipped.len(), offset);
}
}