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>

source

pub fn new(slice: &[u8]) -> SliceReader<'_>

Creates a new SliceReader from a slice of u8.

source

pub fn read_u8(&mut self) -> Result<u8>

Reads and consumes a u8 from the slice.

source

pub fn read_u16(&mut self) -> Result<u16>

Reads and consumes a u16 from the slice.

source

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.

source

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());
source

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.

source

pub fn read_u64(&mut self) -> Result<u64>

Reads and consumes a u64 from the slice.

source

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));
source

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.

source

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.

source

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
    }
}
source

pub fn skip(&mut self, count: usize)

Skips count bytes of the slice.

source

pub fn rewind(&mut self, count: usize) -> Result<()>

Seeks back count bytes in the slice.

source

pub fn left(&self) -> usize

Return the number of bytes left in the slice.

source

pub fn pos(&self) -> usize

Returns the current position in the buffer.

source

pub fn cut_tail(&mut self, count: usize) -> Result<usize>

Shortens the slice by count bytes.

On success, returns the slice new length.

Trait Implementations§

source§

impl<'a> Seek for SliceReader<'a>

source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64>

Seek to an offset, in bytes, in a stream. Read more
1.55.0 · source§

fn rewind(&mut self) -> Result<(), Error>

Rewind to the beginning of a stream. Read more
source§

fn stream_len(&mut self) -> Result<u64, Error>

🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
1.51.0 · source§

fn stream_position(&mut self) -> Result<u64, Error>

Returns the current seek position from the start of the stream. Read more

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for SliceReader<'a>

§

impl<'a> Send for SliceReader<'a>

§

impl<'a> Sync for SliceReader<'a>

§

impl<'a> Unpin for SliceReader<'a>

§

impl<'a> UnwindSafe for SliceReader<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.