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 super::*;
pub struct Encoder {
header_written: bool,
}
impl Encoder {
pub fn new() -> Self {
Encoder {
header_written: false,
}
}
pub fn encode(&mut self, input: &[u8], output: &mut [u8]) -> Result<usize> {
let mut out_byte = 0;
if !self.header_written {
if output.len() < HEADER.len() {
return Err(Error::NoOutputSpaceForHeader);
}
for i in 0..HEADER.len() {
output[i] = HEADER[i];
}
out_byte = HEADER.len();
self.header_written = true;
}
for i in 0..input.len() {
match input[i] {
ESC => {
if (output.len() - out_byte) < 2 {
return Err(Error::NoOutputSpaceForEscEscapeSequence);
}
output[out_byte] = ESC;
output[out_byte + 1] = ESC_ESC;
out_byte += 2;
}
END => {
if (output.len() - out_byte) < 2 {
return Err(Error::NoOutputSpaceForEndEscapeSequence);
}
output[out_byte] = ESC;
output[out_byte + 1] = ESC_END;
out_byte += 2;
}
_ => {
if (output.len() - out_byte) < 1 {
return Err(Error::NoOutputSpaceForInputData);
}
output[out_byte] = input[i];
out_byte += 1;
}
}
}
Ok(out_byte)
}
pub fn finish(self, output: &mut [u8]) -> Result<usize> {
if output.len() < 1 {
return Err(Error::NoOutputSpaceForEndByte);
}
output[0] = END;
Ok(1)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_encode() {
const EXPECTED: [u8; 5] = [0xc0, 0xdb, 0xdc, 0xdd, 0xc0];
let mut output: [u8; 32] = [0; 32];
let mut slip = Encoder::new();
let mut bytes_written = slip.encode(&[0;0], &mut output).unwrap();
assert_eq!(4, bytes_written);
bytes_written += slip.finish(&mut output[bytes_written..]).unwrap();
assert_eq!(5, bytes_written);
assert_eq!(&EXPECTED, &output[..bytes_written]);
}
#[test]
fn encode_esc_esc_sequence() {
const INPUT: [u8; 3] = [0x01, ESC, 0x03];
const EXPECTED: [u8; 9] = [0xc0, 0xdb, 0xdc, 0xdd, 0x01, ESC, ESC_ESC, 0x03, 0xc0];
let mut output: [u8; 32] = [0; 32];
let mut slip = Encoder::new();
let mut bytes_written = slip.encode(&INPUT, &mut output).unwrap();
assert_eq!(5 + INPUT.len(), bytes_written);
bytes_written += slip.finish(&mut output[bytes_written..]).unwrap();
assert_eq!(6 + INPUT.len(), bytes_written);
assert_eq!(&EXPECTED, &output[..bytes_written]);
}
#[test]
fn encode_end_esc_sequence() {
const INPUT: [u8; 3] = [0x01, END, 0x03];
const EXPECTED: [u8; 9] = [0xc0, 0xdb, 0xdc, 0xdd, 0x01, ESC, ESC_END, 0x03, 0xc0];
let mut output: [u8; 32] = [0; 32];
let mut slip = Encoder::new();
let mut bytes_written = slip.encode(&INPUT, &mut output).unwrap();
assert_eq!(5 + INPUT.len(), bytes_written);
bytes_written += slip.finish(&mut output[bytes_written..]).unwrap();
assert_eq!(6 + INPUT.len(), bytes_written);
assert_eq!(&EXPECTED, &output[..bytes_written]);
}
#[test]
fn multi_part_encode() {
const INPUT_1: [u8; 4] = [0x01, 0x02, 0x03, ESC];
const INPUT_2: [u8; 4] = [0x05, END, 0x07, 0x08];
const INPUT_3: [u8; 4] = [0x09, 0x0a, ESC, 0x0c];
const EXPECTED: &[u8] = &[
0xc0, 0xdb, 0xdc, 0xdd, 0x01, 0x02, 0x03, ESC,
ESC_ESC, 0x05, ESC, ESC_END, 0x07, 0x08, 0x09, 0x0a,
ESC, ESC_ESC, 0x0c, 0xc0
];
let mut output: [u8; 32] = [0; 32];
let mut slip = Encoder::new();
let mut bytes_written = slip.encode(&INPUT_1, &mut output).unwrap();
let expected_bytes_written = 4 + INPUT_1.len() + 1;
assert_eq!(expected_bytes_written, bytes_written);
bytes_written += slip.encode(&INPUT_2, &mut output[bytes_written..]).unwrap();
let expected_bytes_written = expected_bytes_written + INPUT_2.len() + 1;
assert_eq!(expected_bytes_written, bytes_written);
bytes_written += slip.encode(&INPUT_3, &mut output[bytes_written..]).unwrap();
let expected_bytes_written = expected_bytes_written + INPUT_3.len() + 1;
assert_eq!(expected_bytes_written, bytes_written);
bytes_written += slip.finish(&mut output[bytes_written..]).unwrap();
assert_eq!(expected_bytes_written + 1, bytes_written);
assert_eq!(EXPECTED, &output[..bytes_written]);
}
}