Skip to content

Commit 8034cf3

Browse files
authored
Merge pull request #2 from ismaileke/advanced-binary
added missing functions
2 parents e7ae434 + fc7d7e5 commit 8034cf3

1 file changed

Lines changed: 63 additions & 9 deletions

File tree

src/lib.rs

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ pub mod binary {
22
/// A binary stream for reading and writing primitive types
33
/// in both big-endian and little-endian byte order.
44
///
5-
/// Commonly used for Minecraft Bedrock protocol data.
5+
/// Commonly used for RakNet & Minecraft Bedrock protocol data.
66
pub struct Stream {
77
buffer: Vec<u8>,
8-
offset: u32,
8+
offset: u32
99
}
1010

1111
impl Stream {
@@ -30,6 +30,12 @@ pub mod binary {
3030
self.offset = 0;
3131
}
3232

33+
/// Returns `true` if the stream has reached the end of the buffer.
34+
#[inline]
35+
pub fn feof(&self) -> bool {
36+
self.offset >= self.buffer.len() as u32
37+
}
38+
3339
/// Sets the read/write offset.
3440
///
3541
/// Panics if the offset is greater than the buffer length.
@@ -111,7 +117,7 @@ pub mod binary {
111117
self.buffer.push(value);
112118
}
113119

114-
// ===== 16-bit integers =====
120+
// ===== 16-bit (short) integers =====
115121

116122
/// Reads an unsigned 16-bit integer (big-endian).
117123
#[inline]
@@ -195,6 +201,34 @@ pub mod binary {
195201
self.buffer.extend_from_slice(&bytes[0..3]);
196202
}
197203

204+
/// Reads a 24-bit unsigned integer (big-endian).
205+
#[inline]
206+
pub fn get_u24_be(&mut self) -> u32 {
207+
let bytes = self.get(3);
208+
u32::from_be_bytes([0, bytes[0], bytes[1], bytes[2]])
209+
}
210+
211+
/// Writes a 24-bit unsigned integer (big-endian).
212+
#[inline]
213+
pub fn put_u24_be(&mut self, value: u32) {
214+
let bytes = value.to_be_bytes();
215+
self.buffer.extend_from_slice(&bytes[1..4]);
216+
}
217+
218+
/// Reads a 24-bit unsigned integer (little-endian).
219+
#[inline]
220+
pub fn get_u24_le(&mut self) -> u32 {
221+
let bytes = self.get(3);
222+
u32::from_le_bytes([bytes[0], bytes[1], bytes[2], 0])
223+
}
224+
225+
/// Writes a 24-bit unsigned integer (little-endian).
226+
#[inline]
227+
pub fn put_u24_le(&mut self, value: u32) {
228+
let bytes = value.to_le_bytes();
229+
self.buffer.extend_from_slice(&bytes[0..3]);
230+
}
231+
198232
// ===== 32-bit integers =====
199233

200234
/// Reads an unsigned 32-bit integer (big-endian).
@@ -223,6 +257,32 @@ pub mod binary {
223257
self.buffer.extend_from_slice(&value.to_le_bytes());
224258
}
225259

260+
/// Reads a signed 32-bit integer (big-endian).
261+
#[inline]
262+
pub fn get_i32_be(&mut self) -> i32 {
263+
let bytes = self.get(4);
264+
i32::from_be_bytes(bytes.try_into().unwrap())
265+
}
266+
267+
/// Writes a signed 32-bit integer (big-endian).
268+
#[inline]
269+
pub fn put_i32_be(&mut self, value: i32) {
270+
self.buffer.extend_from_slice(&value.to_be_bytes());
271+
}
272+
273+
/// Reads a signed 32-bit integer (little-endian).
274+
#[inline]
275+
pub fn get_i32_le(&mut self) -> i32 {
276+
let bytes = self.get(4);
277+
i32::from_le_bytes(bytes.try_into().unwrap())
278+
}
279+
280+
/// Writes a signed 32-bit integer (little-endian).
281+
#[inline]
282+
pub fn put_i32_le(&mut self, value: i32) {
283+
self.buffer.extend_from_slice(&value.to_le_bytes());
284+
}
285+
226286
// ===== 32-bit floats =====
227287

228288
/// Reads a 32-bit floating-point number (big-endian).
@@ -414,11 +474,5 @@ pub mod binary {
414474
let encoded = ((value << 1) ^ (value >> 63)) as u64;
415475
self.put_var_u64(encoded);
416476
}
417-
418-
/// Returns `true` if the stream has reached the end of the buffer.
419-
#[inline]
420-
pub fn feof(&self) -> bool {
421-
self.offset >= self.buffer.len() as u32
422-
}
423477
}
424478
}

0 commit comments

Comments
 (0)