pub struct DataBuffer { /* private fields */ }
Expand description
A data buffer object specifically turned to easily read and write binary values
Implementations§
Source§impl DataBuffer
impl DataBuffer
Sourcepub fn new() -> DataBuffer ⓘ
pub fn new() -> DataBuffer ⓘ
Construct a new, empty, DataBuffer
Sourcepub fn from_bytes(bytes: &[u8]) -> DataBuffer ⓘ
pub fn from_bytes(bytes: &[u8]) -> DataBuffer ⓘ
Borrows the data from a Vec<u8>
to produce a DataBuffer
.
This method is the equivalent of creating a new() DataBuffer
and calling the .write_bytes() method to effectively
copy all bytes from the borrowed vector into this buffer’s underlying
vector.
Sourcepub fn with_vec(bytes: Vec<u8>) -> DataBuffer ⓘ
pub fn with_vec(bytes: Vec<u8>) -> DataBuffer ⓘ
Takes ownership of a Vec<u8>
and uses it for this buffer’s
underlying vector. This is not a borrow, you will not be able to use
the original vector anymore after calling this method.
This method is useful to avoid excessive copying of data.
pub fn create(opcode: u8, header: PacketHeader) -> DataBuffer ⓘ
pub fn finish(&mut self)
Sourcepub fn resize(&mut self, size: usize)
pub fn resize(&mut self, size: usize)
Change the buffer size to size.
Note: You cannot shrink a buffer with this method
Sourcepub fn write_bytes(&mut self, bytes: &[u8])
pub fn write_bytes(&mut self, bytes: &[u8])
Append a byte array to the buffer. The buffer is automatically extended if needed
#Example
let mut buffer = DataBuffer::new();
buffer.write_bytes(&vec![0x1, 0xFF, 0x45]); // buffer contains [0x1, 0xFF, 0x45]
Sourcepub fn write_u8(&mut self, val: u8)
pub fn write_u8(&mut self, val: u8)
Append a byte (8 bits value) to the buffer
#Example
let mut buffer = DataBuffer::new();
buffer.write_u8(1) // buffer contains [0x1]
Sourcepub fn write_u16(&mut self, val: u16)
pub fn write_u16(&mut self, val: u16)
Append a word (16 bits value) to the buffer
#Example
let mut buffer = DataBuffer::new();
buffer.write_u16(1) // buffer contains [0x00, 0x1] if little endian
Sourcepub fn write_u32(&mut self, val: u32)
pub fn write_u32(&mut self, val: u32)
Append a double word (32 bits value) to the buffer
#Example
let mut buffer = DataBuffer::new();
buffer.write_u32(1) // buffer contains [0x00, 0x00, 0x00, 0x1] if little endian
Sourcepub fn write_u64(&mut self, val: u64)
pub fn write_u64(&mut self, val: u64)
Append a quaddruple word (64 bits value) to the buffer
#Example
let mut buffer = DataBuffer::new();
buffer.write_u64(1) // buffer contains [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1] if little endian
Sourcepub fn write_f32(&mut self, val: f32)
pub fn write_f32(&mut self, val: f32)
Append a 32 bits floating point number to the buffer.
#Example
let mut buffer = DataBuffer::new();
buffer.write_f32(0.1)
Sourcepub fn write_f64(&mut self, val: f64)
pub fn write_f64(&mut self, val: f64)
Append a 64 bits floating point number to the buffer.
#Example
let mut buffer = DataBuffer::new();
buffer.write_f64(0.1)
Sourcepub fn write_str(&mut self, val: &str)
pub fn write_str(&mut self, val: &str)
Append a string to the buffer.
Format The format is (u32)size + size * (u8)characters
#Example
let mut buffer = DataBuffer::new();
buffer.write_str("Hello")
Sourcepub fn write_ntstr(&mut self, val: &str)
pub fn write_ntstr(&mut self, val: &str)
Write a single null-terminated string to the buffer.
#Example
let mut buffer = DataBuffer::new();
buffer.write_ntstr("Hello");
Sourcepub fn write_dntstr(&mut self, val: &str)
pub fn write_dntstr(&mut self, val: &str)
Write a null-bookeneded string to the buffer.
#Example
let mut buffer = DataBuffer::new();
buffer.write_dntstr("Hello");
Sourcepub fn write_smart(&mut self, val: u16)
pub fn write_smart(&mut self, val: u16)
Writes a smart to the buffer, which is a dynamically-sized unit with a max value of 32768.
Sourcepub fn write_medium(&mut self, val: u32)
pub fn write_medium(&mut self, val: u32)
Writes a medium to the buffer, which is a tribyte word.
Sourcepub fn write_u8_neg(&mut self, val: u8)
pub fn write_u8_neg(&mut self, val: u8)
Writes an inverted-signededness u8 to the buffer.
Sourcepub fn write_i8_neg(&mut self, val: i8)
pub fn write_i8_neg(&mut self, val: i8)
Writes an inverted-signededness i8 to the buffer.
Sourcepub fn write_u8_add(&mut self, val: u8)
pub fn write_u8_add(&mut self, val: u8)
Writes a u8 + 128 to the buffer.
Sourcepub fn write_i8_add(&mut self, val: i8)
pub fn write_i8_add(&mut self, val: i8)
Writes a i8 + 128 to the buffer.
Sourcepub fn write_u8_sub(&mut self, val: u8)
pub fn write_u8_sub(&mut self, val: u8)
Writes a u8 - 128 to the buffer.
Sourcepub fn write_i8_sub(&mut self, val: i8)
pub fn write_i8_sub(&mut self, val: i8)
Writes a i8 - 128 to the buffer.
Sourcepub fn write_u16_le(&mut self, val: u16)
pub fn write_u16_le(&mut self, val: u16)
Writes a little-endian u16 to the buffer.
Sourcepub fn write_i16_le(&mut self, val: i16)
pub fn write_i16_le(&mut self, val: i16)
Writes a little-endian i16 to the buffer.
Sourcepub fn write_u16_le_add(&mut self, val: u16)
pub fn write_u16_le_add(&mut self, val: u16)
Writes a little-endian u16 + 128 to the buffer.
Sourcepub fn write_i16_le_add(&mut self, val: i16)
pub fn write_i16_le_add(&mut self, val: i16)
Writes a little-endian u16 + 128 to the buffer.
Sourcepub fn write_u32_le(&mut self, val: u32)
pub fn write_u32_le(&mut self, val: u32)
Writes a little-endian u32 to the buffer.
Sourcepub fn write_i32_le(&mut self, val: i32)
pub fn write_i32_le(&mut self, val: i32)
Writes a little-endian i32 to the buffer.
Sourcepub fn write_u32_me(&mut self, val: u32)
pub fn write_u32_me(&mut self, val: u32)
Writes a mixed-endian u32 to the buffer.
Sourcepub fn write_u32_me_reversed(&mut self, val: u32)
pub fn write_u32_me_reversed(&mut self, val: u32)
Writes a reverse mixed-endian u32 to the buffer.
Sourcepub fn read_bytes(&mut self, size: usize) -> Vec<u8> ⓘ
pub fn read_bytes(&mut self, size: usize) -> Vec<u8> ⓘ
Read a defined amount of raw bytes. The program crash if not enough bytes are available
Sourcepub fn read_u8(&mut self) -> u8
pub fn read_u8(&mut self) -> u8
Read one byte. The program crash if not enough bytes are available
#Example
let mut buffer = DataBuffer::from_bytes(&vec![0x1]);
let value = buffer.read_u8(); //Value contains 1
Sourcepub fn read_u16(&mut self) -> u16
pub fn read_u16(&mut self) -> u16
Read a 2-bytes long value. The program crash if not enough bytes are available
#Example
let mut buffer = DataBuffer::from_bytes(&vec![0x0, 0x1]);
let value = buffer.read_u16(); //Value contains 1
Sourcepub fn read_u32(&mut self) -> u32
pub fn read_u32(&mut self) -> u32
Read a four-bytes long value. The program crash if not enough bytes are available
#Example
let mut buffer = DataBuffer::from_bytes(&vec![0x0, 0x0, 0x0, 0x1]);
let value = buffer.read_u32(); // Value contains 1
Sourcepub fn read_u64(&mut self) -> u64
pub fn read_u64(&mut self) -> u64
Read an eight bytes long value. The program crash if not enough bytes are available
#Example
let mut buffer = DataBuffer::from_bytes(&vec![0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1]);
let value = buffer.read_u64(); //Value contains 1
Sourcepub fn read_f32(&mut self) -> f32
pub fn read_f32(&mut self) -> f32
Read a 32 bits floating point value. The program crash if not enough bytes are available
Sourcepub fn read_f64(&mut self) -> f64
pub fn read_f64(&mut self) -> f64
Read a 64 bits floating point value. The program crash if not enough bytes are available
Sourcepub fn read_string(&mut self) -> String
pub fn read_string(&mut self) -> String
Read a string.
Note : First it reads a 32 bits value representing the size, the read ‘size’ raw bytes.
Sourcepub fn read_ntstr(&mut self) -> String
pub fn read_ntstr(&mut self) -> String
Read a null-terminated string.
Sourcepub fn read_dntstr(&mut self) -> String
pub fn read_dntstr(&mut self) -> String
Read a null-bookended string.
Sourcepub fn read_smart(&mut self) -> u16
pub fn read_smart(&mut self) -> u16
Reads a smart from the buffer, which is a dynamically-sized unit with a max value of 32768.
Sourcepub fn read_medium(&mut self) -> u32
pub fn read_medium(&mut self) -> u32
Reads a medium from the buffer, which is a tribyte word.
Sourcepub fn read_u8_neg(&mut self) -> u8
pub fn read_u8_neg(&mut self) -> u8
Reads an inverted-signededness u8 from the buffer.
Sourcepub fn read_i8_neg(&mut self) -> i8
pub fn read_i8_neg(&mut self) -> i8
Reads an inverted-signededness i8 from the buffer.
Sourcepub fn read_u8_add(&mut self) -> u8
pub fn read_u8_add(&mut self) -> u8
Reads a u8 + 128 from the buffer, and subtracts the extra 128.
Sourcepub fn read_i8_add(&mut self) -> i8
pub fn read_i8_add(&mut self) -> i8
Reads a i8 + 128 from the buffer, and subtracts the extra 128.
Sourcepub fn read_u8_sub(&mut self) -> u8
pub fn read_u8_sub(&mut self) -> u8
Reads a u8 - 128 from the buffer, and adds back the missing 128.
Sourcepub fn read_i8_sub(&mut self) -> i8
pub fn read_i8_sub(&mut self) -> i8
Reads a i8 - 128 from the buffer, and adds back the missing 128.
Sourcepub fn read_u16_le(&mut self) -> u16
pub fn read_u16_le(&mut self) -> u16
Reads a little-endian u16 from the buffer.
Sourcepub fn read_i16_le(&mut self) -> i16
pub fn read_i16_le(&mut self) -> i16
Reads a little-endian i16 from the buffer.
Sourcepub fn read_u16_le_add(&mut self) -> u16
pub fn read_u16_le_add(&mut self) -> u16
Reads a little-endian u16 + 128 from the buffer, and subtracts the extra 128.
Sourcepub fn read_i16_le_add(&mut self) -> i16
pub fn read_i16_le_add(&mut self) -> i16
Reads a little-endian i16 + 128 from the buffer, and subtracts the extra 128.
Sourcepub fn read_u32_le(&mut self) -> u32
pub fn read_u32_le(&mut self) -> u32
Reads a little-endian u32 from the buffer.
Sourcepub fn read_i32_le(&mut self) -> i32
pub fn read_i32_le(&mut self) -> i32
Reads a little-endian u32 from the buffer.
Sourcepub fn read_u32_me(&mut self) -> u32
pub fn read_u32_me(&mut self) -> u32
Reads a mixed-endian u32 from the buffer.
Sourcepub fn read_u32_me_reversed(&mut self) -> u32
pub fn read_u32_me_reversed(&mut self) -> u32
Reads a reverse mixed-endian u32 from the buffer.
Sourcepub fn set_rpos(&mut self, rpos: usize)
pub fn set_rpos(&mut self, rpos: usize)
Set the reading cursor position.
Note : Set the reading cursor to min(newPosition, self.len())
to prevent overflow
Sourcepub fn set_wpos(&mut self, wpos: usize)
pub fn set_wpos(&mut self, wpos: usize)
Set the writing cursor position.
Note : Set the writing cursor to min(newPosition, self.len())
to prevent overflow
Sourcepub fn to_bytes(&self) -> Vec<u8> ⓘ
pub fn to_bytes(&self) -> Vec<u8> ⓘ
Returns a copy of this buffer’s underlying Vec<u8>
.
If you no longer need to use this buffer after calling this,
it is recommended to use .deconstruct() instead.
pub fn deconstruct(self) -> Vec<u8> ⓘ
Sourcepub fn read_bit(&mut self) -> bool
pub fn read_bit(&mut self) -> bool
Read 1 bit. Return true if the bit is set to 1, otherwhise, return false.
Note Bits are read from left to right
#Example
let mut buffer = DataBuffer::from_bytes(&vec![128]); // 10000000b
let value1 = buffer.read_bit(); //value1 contains true (eg: bit is 1)
let value2 = buffer.read_bit(); //value2 contains false (eg: bit is 0)
Sourcepub fn read_bits(&mut self, n: u8) -> u64
pub fn read_bits(&mut self, n: u8) -> u64
Read n bits. an return the corresponding value an u64.
Note 1 : We cannot read more than 64 bits
Note 2 Bits are read from left to right
#Example
let mut buffer = DataBuffer::from_bytes(&vec![128]); // 10000000b
let value = buffer.read_bits(3); // value contains 4 (eg: 100b)
Sourcepub fn flush_bit(&mut self)
pub fn flush_bit(&mut self)
Discard all the pending bits available for reading or writing and place the the corresponding cursor to the next byte.
Note 1 : If no bits are currently read or written, this function does nothing. Note 2 : This function is automatically called for each write or read operations. #Example
10010010 | 00000001
^
10010010 | 00000001 // read_bit called
^
10010010 | 00000001 // flush_bit() called
^
Sourcepub fn write_bit(&mut self, bit: bool)
pub fn write_bit(&mut self, bit: bool)
Append 1 bit value to the buffer. The bit is happened like this :
...| XXXXXXXX | 10000000 |....
Sourcepub fn write_bits(&mut self, value: u64, n: u8)
pub fn write_bits(&mut self, value: u64, n: u8)
Write the given value as a sequence of n bits
#Example
let mut buffer = DataBuffer::new();
buffer.write_bits(4, 3); // append 100b
Trait Implementations§
Source§impl Debug for DataBuffer
impl Debug for DataBuffer
Source§impl Read for DataBuffer
impl Read for DataBuffer
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl Write for DataBuffer
impl Write for DataBuffer
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)Auto Trait Implementations§
impl Freeze for DataBuffer
impl RefUnwindSafe for DataBuffer
impl Send for DataBuffer
impl Sync for DataBuffer
impl Unpin for DataBuffer
impl UnwindSafe for DataBuffer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<R> ReadBytesExt for R
impl<R> ReadBytesExt for R
Source§fn read_u8(&mut self) -> Result<u8, Error>
fn read_u8(&mut self) -> Result<u8, Error>
Source§fn read_i8(&mut self) -> Result<i8, Error>
fn read_i8(&mut self) -> Result<i8, Error>
Source§fn read_u16<T>(&mut self) -> Result<u16, Error>where
T: ByteOrder,
fn read_u16<T>(&mut self) -> Result<u16, Error>where
T: ByteOrder,
Source§fn read_i16<T>(&mut self) -> Result<i16, Error>where
T: ByteOrder,
fn read_i16<T>(&mut self) -> Result<i16, Error>where
T: ByteOrder,
Source§fn read_u24<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
fn read_u24<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
Source§fn read_i24<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
fn read_i24<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
Source§fn read_u32<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
fn read_u32<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
Source§fn read_i32<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
fn read_i32<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
Source§fn read_u48<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
fn read_u48<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
Source§fn read_i48<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
fn read_i48<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
Source§fn read_u64<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
fn read_u64<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
Source§fn read_i64<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
fn read_i64<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
Source§fn read_u128<T>(&mut self) -> Result<u128, Error>where
T: ByteOrder,
fn read_u128<T>(&mut self) -> Result<u128, Error>where
T: ByteOrder,
Source§fn read_i128<T>(&mut self) -> Result<i128, Error>where
T: ByteOrder,
fn read_i128<T>(&mut self) -> Result<i128, Error>where
T: ByteOrder,
Source§fn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error>where
T: ByteOrder,
fn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error>where
T: ByteOrder,
Source§fn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error>where
T: ByteOrder,
fn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error>where
T: ByteOrder,
Source§fn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error>where
T: ByteOrder,
fn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error>where
T: ByteOrder,
Source§fn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error>where
T: ByteOrder,
fn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error>where
T: ByteOrder,
Source§fn read_f32<T>(&mut self) -> Result<f32, Error>where
T: ByteOrder,
fn read_f32<T>(&mut self) -> Result<f32, Error>where
T: ByteOrder,
Source§fn read_f64<T>(&mut self) -> Result<f64, Error>where
T: ByteOrder,
fn read_f64<T>(&mut self) -> Result<f64, Error>where
T: ByteOrder,
Source§fn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error>where
T: ByteOrder,
fn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error>where
T: ByteOrder,
fn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error>where
T: ByteOrder,
fn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error>where
T: ByteOrder,
fn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>
fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>
Source§fn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error>where
T: ByteOrder,
fn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error>where
T: ByteOrder,
fn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error>where
T: ByteOrder,
fn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error>where
T: ByteOrder,
fn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
fn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
fn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
read_f32_into
instead