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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use super::Command;
use std::fmt::{self, Display, Formatter};
use crate::{CommandCode, Key, MouseButton, Enum};
/// Error enum returned by [`Command::from_bytes`].
#[derive(Debug)]
pub enum CommandBytesError {
/// Encountered a byte that isn't a valid [`CommandCode`].
InvalidCommandCode(u8),
/// Encountered a byte that isn't a valid [`Key`].
InvalidKey(u8),
/// Encountered a byte that isn't a valid [`MouseButton`].
InvalidMouseButton(u8),
/// Encountered a byte sequence that isn't a valid Unicode scalar.
InvalidUnicodeScalar(u32),
/// Encountered a byte sequence that isn't a valid UTF-8 string.
InvalidUTF8,
/// Expected the buffer to be at least this many bytes in length.
BufferTooShort(usize),
}
use CommandBytesError::*;
impl Display for CommandBytesError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
InvalidCommandCode(byte) => write!(f, "Invalid command code byte ({})", byte),
InvalidKey(byte) => write!(f, "Invalid key byte ({})", byte),
InvalidMouseButton(byte) => write!(f, "Invalid mouse button byte ({})", byte),
InvalidUnicodeScalar(ucs) => write!(f, "Invalid Unicode scalar ({:#010X})", ucs),
InvalidUTF8 => write!(f, "Invalid UTF-8 string"),
BufferTooShort(len) => write!(f, "Expected buffer to be at least {} bytes in length", len),
}
}
}
impl std::error::Error for CommandBytesError {}
fn parse_i32(b_0: u8, b_1: u8) -> i32 {
(((b_0 as i16) << 8) | (b_1 as i16)) as i32
}
fn parse_u32(b_0: u8, b_1: u8) -> u32 {
(((b_0 as u16) << 8) | (b_1 as u16)) as u32
}
fn parse_char(b_0: u8, b_1: u8, b_2: u8, b_3: u8) -> Result<char, CommandBytesError> {
let ch = ((b_0 as u32) << 24) | ((b_1 as u32) << 16) | ((b_2 as u32) << 8) | (b_3 as u32);
std::char::from_u32(ch).ok_or(InvalidUnicodeScalar(ch))
}
fn parse_string(buf: &[u8]) -> Result<String, CommandBytesError> {
String::from_utf8(buf.to_owned()).map_err(|_| InvalidUTF8)
}
fn parse_command_code(byte: u8) -> Result<CommandCode, CommandBytesError> {
CommandCode::from_u8(byte).ok_or(InvalidCommandCode(byte))
}
fn parse_key(byte: u8) -> Result<Key, CommandBytesError> {
Key::from_u8(byte).ok_or(InvalidKey(byte))
}
fn parse_mouse_button(byte: u8) -> Result<MouseButton, CommandBytesError> {
MouseButton::from_u8(byte).ok_or(InvalidMouseButton(byte))
}
fn check_buffer_length(buf: &[u8], len: usize) -> Result<(), CommandBytesError> {
if buf.len() >= len {
Ok(())
} else {
Err(BufferTooShort(len))
}
}
impl Command {
/// Construct a [`Command`] from a sequence of bytes.
///
/// The first byte in the buffer must be a [`CommandCode`]. This identifies
/// the command and its arguments. Following the command identifier is a
/// sequence of bytes that encode the arguments of the command. [`Key`] and
/// [`MouseButton`] are single bytes. For integer arguments (used for moving
/// the mouse and scrolling), signed 16-bit big-endian integers are used.
///
/// Since a negative delay is impossible, the millisecond parameter for the
/// `Delay` command is unsigned.
///
/// An ASCII character is a single byte. An ASCII string is a length
/// followed by a sequence of bytes. The length is an unsigned 16-bit
/// big-endian integer.
///
/// A Unicode character (or more specifically, a Unicode scalar value) is
/// 32-bits. A Unicode string is similar to an ASCII string in that it has a
/// length followed by a sequence of bytes, however the sequence of bytes
/// are a UTF-8 encoded string.
///
/// The function returns the command and the number of bytes that were read
/// from the buffer.
///
/// # Examples
///
/// ```
/// use tfc::{Command, CommandCode, Key};
///
/// let bytes = &[
/// CommandCode::MouseMoveRel as u8, 255, 214, 0, 64,
/// CommandCode::KeyClick as u8, Key::K as u8,
/// CommandCode::UnicodeString as u8, 0, 4, 0xF0, 0x9F, 0xA4, 0xAA,
/// ];
///
/// let (command, len) = Command::from_bytes(bytes).unwrap();
/// assert_eq!(len, 5);
/// assert_eq!(command, Command::MouseMoveRel(-42, 64));
///
/// let bytes = &bytes[len..];
/// let (command, len) = Command::from_bytes(bytes).unwrap();
/// assert_eq!(len, 2);
/// assert_eq!(command, Command::KeyClick(Key::K));
///
/// let bytes = &bytes[len..];
/// let (command, len) = Command::from_bytes(bytes).unwrap();
/// assert_eq!(len, 7);
/// assert_eq!(command, Command::UnicodeString("🤪".to_owned()));
/// ```
pub fn from_bytes(buf: &[u8]) -> Result<(Command, usize), CommandBytesError> {
if buf.is_empty() {
return Err(BufferTooShort(1));
}
match parse_command_code(buf[0])? {
CommandCode::Delay => {
check_buffer_length(buf, 3)?;
Ok((Command::Delay(parse_u32(buf[1], buf[2])), 3))
}
CommandCode::KeyDown => {
check_buffer_length(buf, 2)?;
Ok((Command::KeyDown(parse_key(buf[1])?), 2))
}
CommandCode::KeyUp => {
check_buffer_length(buf, 2)?;
Ok((Command::KeyUp(parse_key(buf[1])?), 2))
}
CommandCode::KeyClick => {
check_buffer_length(buf, 2)?;
Ok((Command::KeyClick(parse_key(buf[1])?), 2))
}
CommandCode::MouseMoveRel => {
check_buffer_length(buf, 5)?;
Ok((Command::MouseMoveRel(parse_i32(buf[1], buf[2]), parse_i32(buf[3], buf[4])), 5))
}
CommandCode::MouseMoveAbs => {
check_buffer_length(buf, 5)?;
Ok((Command::MouseMoveAbs(parse_i32(buf[1], buf[2]), parse_i32(buf[3], buf[4])), 5))
}
CommandCode::MouseScroll => {
check_buffer_length(buf, 5)?;
Ok((Command::MouseScroll(parse_i32(buf[1], buf[2]), parse_i32(buf[3], buf[4])), 5))
}
CommandCode::MouseDown => {
check_buffer_length(buf, 2)?;
Ok((Command::MouseDown(parse_mouse_button(buf[1])?), 2))
}
CommandCode::MouseUp => {
check_buffer_length(buf, 2)?;
Ok((Command::MouseUp(parse_mouse_button(buf[1])?), 2))
}
CommandCode::MouseClick => {
check_buffer_length(buf, 2)?;
Ok((Command::MouseClick(parse_mouse_button(buf[1])?), 2))
}
CommandCode::AsciiCharDown => {
check_buffer_length(buf, 2)?;
Ok((Command::AsciiCharDown(buf[1]), 2))
}
CommandCode::AsciiCharUp => {
check_buffer_length(buf, 2)?;
Ok((Command::AsciiCharUp(buf[1]), 2))
}
CommandCode::AsciiChar => {
check_buffer_length(buf, 2)?;
Ok((Command::AsciiChar(buf[1]), 2))
}
CommandCode::AsciiString => {
check_buffer_length(buf, 3)?;
let len = 3 + parse_u32(buf[1], buf[2]) as usize;
check_buffer_length(buf, len)?;
Ok((Command::AsciiString(buf[3..len].to_owned()), len))
}
CommandCode::UnicodeCharDown => {
check_buffer_length(buf, 5)?;
Ok((Command::UnicodeCharDown(parse_char(buf[1], buf[2], buf[3], buf[4])?), 5))
}
CommandCode::UnicodeCharUp => {
check_buffer_length(buf, 5)?;
Ok((Command::UnicodeCharUp(parse_char(buf[1], buf[2], buf[3], buf[4])?), 5))
}
CommandCode::UnicodeChar => {
check_buffer_length(buf, 5)?;
Ok((Command::UnicodeChar(parse_char(buf[1], buf[2], buf[3], buf[4])?), 5))
}
CommandCode::UnicodeString => {
check_buffer_length(buf, 3)?;
let len = 3 + parse_u32(buf[1], buf[2]) as usize;
check_buffer_length(buf, len)?;
Ok((Command::UnicodeString(parse_string(&buf[3..len])?), len))
}
}
}
}