Struct t2plugin::slread::SliceReader
source · pub struct SliceReader<'a> { /* private fields */ }
Expand description
Tool to decode protocols from a byte slice.
The slice can be consumed as different types of integers (u16
, u32
, …) for binary
protocols or read line by line for text protocols.
All numbers read from the slice are assumed to be stored in big endian (network order).
Implementations§
source§impl<'a> SliceReader<'a>
impl<'a> SliceReader<'a>
sourcepub fn new(slice: &[u8]) -> SliceReader<'_>
pub fn new(slice: &[u8]) -> SliceReader<'_>
Creates a new SliceReader
from a slice of u8
.
sourcepub fn read_u24(&mut self) -> Result<u32>
pub fn read_u24(&mut self) -> Result<u32>
Reads and consumes a u24
from the slice.
u24
is actually a u32
but only consumes 3 bytes from the slice.
sourcepub fn read_u32(&mut self) -> Result<u32>
pub fn read_u32(&mut self) -> Result<u32>
Reads and consumes a u32
from the slice.
Example
use t2plugin::slread::SliceReader;
use t2plugin::packet::Packet;
...
let slr = SliceReader::new(packet.l7_header());
// read the first 32 bits of the packet payload
let id = try!(slr.read_u32());
sourcepub fn read_u48(&mut self) -> Result<u64>
pub fn read_u48(&mut self) -> Result<u64>
Reads and consumes a u48
from the slice.
u48
is actually a u64
but only consumes 6 bytes from the slice.
sourcepub fn read_copy(&mut self, buf: &mut [u8]) -> Result<()>
pub fn read_copy(&mut self, buf: &mut [u8]) -> Result<()>
Copies and consumes a sub-slice to a mutable buffer.
Example
use t2plugin::slread::SliceReader;
use t2plugin::packet::Packet;
...
let slr = SliceReader::new(packet.l7_header());
let mut data = [0u8; 6];
try!(slr.read_copy(&mut data));
sourcepub fn read_bytes(&mut self, count: usize) -> Result<&'a [u8]>
pub fn read_bytes(&mut self, count: usize) -> Result<&'a [u8]>
Returns and consumes a sub-slice containing count
bytes.
The lifetime of the returned slice is the same as the one of the slice provided when
creating this SliceReader
with the new
method.
sourcepub fn read_until(&mut self, byte: u8) -> Result<&'a [u8]>
pub fn read_until(&mut self, byte: u8) -> Result<&'a [u8]>
Reads buffer until the first occurence of byte
.
The lifetime of the returned slice is the same as the one of the slice provided when
creating this SliceReader
with the new
method.
sourcepub fn read_line(&mut self) -> Result<&'a [u8]>
pub fn read_line(&mut self) -> Result<&'a [u8]>
Reads a line from the buffer.
The line is returned as a slice of byte. This is necessary in order to also process lines which contain invalid UTF-8 characters.
The lifetime of the returned line is the same as the one of the slice provided when
creating this SliceReader
with the new
method.
Example
use t2plugin::slread::{SliceReader, TrimBytes};
use t2plugin::packet::Packet;
let slr = SliceReader::new(packet.l7_header());
// read the packet payload line by line
while let Ok(line) = slr.read_line() {
if line.starts_with(b"User-Agent: ") {
let ua = line[12 ..].trim();
// do something with HTTP user agent
}
}