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
use num_traits::FromPrimitive;
use std::convert::TryInto;
use std::str;
use super::{read_traits::*, utils::read_uleb128};
use crate::{id, BanchoMessage, Packet};
pub struct PayloadReader<'a> {
pub payload: &'a [u8],
pub index: usize,
}
impl<'a> PayloadReader<'a> {
#[inline(always)]
pub fn new(payload: &'a [u8]) -> Self {
PayloadReader { payload, index: 0 }
}
#[inline(always)]
fn read(&mut self, length: usize) -> Option<&[u8]> {
let data = self.payload.get(self.index..self.index + length)?;
self.index += length;
Some(data)
}
#[inline(always)]
pub fn read_integer<N: NumberAsBytes<N>>(&mut self) -> Option<N> {
let data = self.read(std::mem::size_of::<N>())?;
N::from_le_bytes(data)
}
#[inline(always)]
pub fn read_i32_list<N: NumberAsBytes<N>>(&mut self) -> Option<Vec<i32>> {
let length_data = self.read(std::mem::size_of::<N>())?;
let int_count = N::from_le_bytes(length_data)?.as_usize();
let mut data: Vec<i32> = Vec::with_capacity(int_count);
for _ in 0..int_count {
data.push(i32::from_le_bytes(match self.read(4)?.try_into() {
Ok(u) => u,
Err(_) => return None,
}));
}
Some(data)
}
#[inline(always)]
pub fn read_message(&mut self) -> Option<BanchoMessage> {
Some(BanchoMessage {
sender: self.read_string()?,
content: self.read_string()?,
target: self.read_string()?,
sender_id: self.read_integer()?,
})
}
#[inline(always)]
pub fn read_string(&mut self) -> Option<String> {
if self.payload.get(self.index)? != &11u8 {
return None;
}
self.index += 1;
let data_length = self.read_uleb128()? as usize;
let cur = self.index;
self.index += data_length;
let data = self.payload.get(cur..self.index)?;
Some(
match str::from_utf8(data) {
Ok(s) => s,
Err(_) => return None,
}
.into(),
)
}
#[inline(always)]
pub fn read_uleb128(&mut self) -> Option<u32> {
let (val, length) = read_uleb128(&self.payload.get(self.index..)?)?;
self.index += length;
Some(val)
}
}
pub struct PacketReader {
pub buf: Vec<u8>,
pub index: usize,
pub current_packet: id,
pub payload_length: usize,
pub finish: bool,
pub payload_count: u16,
pub packet_count: u16,
}
impl PacketReader {
#[inline(always)]
pub fn from_vec(body: Vec<u8>) -> Self {
PacketReader {
buf: body,
index: 0,
current_packet: id::OSU_UNKNOWN_PACKET,
payload_length: 0,
finish: false,
payload_count: 0,
packet_count: 0,
}
}
#[inline(always)]
pub fn reset(&mut self) {
self.finish = false;
self.index = 0;
self.current_packet = id::OSU_UNKNOWN_PACKET;
self.payload_length = 0;
self.payload_count = 0;
self.packet_count = 0;
}
#[inline(always)]
pub fn next(&mut self) -> Option<Packet> {
if (self.buf.len() - self.index) < 7 {
self.finish = true;
return None;
}
let header = &self.buf[self.index..self.index + 7];
self.index += 7;
let packet_id = id::from_u8(header[0]).unwrap_or_else(|| {
println!("[PacketReader]: unknown packet id({})", header[0]);
id::OSU_UNKNOWN_PACKET
});
let length = u32::from_le_bytes(match header[3..=6].try_into() {
Ok(b) => b,
Err(_) => {
return Some(Packet {
id: packet_id,
payload: None,
})
}
});
self.packet_count += 1;
self.current_packet = packet_id.clone();
let payload = match length {
0 => None,
_ => {
self.payload_count += 1;
self.payload_length = length as usize;
self.index += self.payload_length;
self.buf.get(self.index - self.payload_length..self.index)
}
};
Some(Packet {
id: packet_id,
payload,
})
}
#[inline(always)]
pub fn read_header(body: Vec<u8>) -> Option<(id, u32)> {
if body.len() < 7 {
return None;
}
let header = &body[..7];
Some((
id::from_u8(header[0]).unwrap_or(id::OSU_UNKNOWN_PACKET),
u32::from_le_bytes(match header[3..=6].try_into() {
Ok(b) => b,
Err(_) => {
return None;
}
}),
))
}
}