Skip to main content

ByteStream

Struct ByteStream 

Source
pub struct ByteStream { /* private fields */ }
Expand description

A position-tracked binary stream reader.

ByteStream wraps binary data and provides sequential reading operations while tracking the current position. It supports creating slices (views) into the data for parsing nested structures without copying.

§Design

The stream uses Arc<[u8]> internally, allowing cheap cloning and slicing without data duplication. Position tracking is relative to the slice, with abs_pos() providing the absolute position in the original data.

§Example

use gpg_inspector_lib::ByteStream;

let data = vec![0x01, 0x02, 0x03, 0x04];
let mut stream = ByteStream::new(data);

assert_eq!(stream.octet().unwrap(), 0x01);
assert_eq!(stream.pos(), 1);
assert_eq!(stream.remaining(), 3);

Implementations§

Source§

impl ByteStream

Source

pub fn new(bytes: Vec<u8>) -> Self

Creates a new stream from a vector of bytes.

The stream will own the data and start reading from position 0.

Source

pub fn from_arc(bytes: Arc<[u8]>) -> Self

Creates a new stream from an Arc<[u8]>.

Useful when sharing the same data across multiple streams or when the data is already in an Arc.

Source

pub fn slice(&self, start: usize, end: usize) -> Self

Creates a slice (view) into this stream’s data.

The slice shares the underlying data but has its own position tracker starting at 0. The start and end parameters are relative to the current stream’s bounds.

This is useful for parsing nested packet structures where you want to limit reads to a specific byte range.

Source

pub fn pos(&self) -> usize

Returns the current position within this stream (relative to start).

Source

pub fn abs_pos(&self) -> usize

Returns the absolute position in the original data.

For slices, this accounts for the slice’s offset from the beginning of the original data.

Source

pub fn remaining(&self) -> usize

Returns the number of bytes remaining to read.

Source

pub fn is_empty(&self) -> bool

Returns true if there are no more bytes to read.

Source

pub fn len(&self) -> usize

Returns the total length of this stream (or slice).

Source

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

Reads a single byte and advances the position.

§Errors

Returns Error::UnexpectedEnd if no bytes remain.

Source

pub fn peek(&self) -> Option<u8>

Returns the next byte without advancing the position.

Returns None if no bytes remain.

Source

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

Reads a big-endian 16-bit unsigned integer.

§Errors

Returns Error::UnexpectedEnd if fewer than 2 bytes remain.

Source

pub fn uint32(&mut self) -> Result<u32>

Reads a big-endian 32-bit unsigned integer.

§Errors

Returns Error::UnexpectedEnd if fewer than 4 bytes remain.

Source

pub fn bytes(&mut self, count: usize) -> Result<Vec<u8>>

Reads count bytes and returns them as a vector.

§Errors

Returns Error::UnexpectedEnd if fewer than count bytes remain.

Source

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

Reads count bytes and returns them as an uppercase hex string.

§Errors

Returns Error::UnexpectedEnd if fewer than count bytes remain.

Source

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

Reads count bytes and interprets them as UTF-8.

Invalid UTF-8 sequences are replaced with the Unicode replacement character.

§Errors

Returns Error::UnexpectedEnd if fewer than count bytes remain.

Source

pub fn rest(&mut self) -> Vec<u8>

Reads all remaining bytes and returns them as a vector.

After this call, remaining() will return 0.

Source

pub fn rest_as_hex(&mut self) -> String

Reads all remaining bytes and returns them as an uppercase hex string.

Source

pub fn multi_precision_integer(&mut self) -> Result<(u16, String)>

Reads an OpenPGP Multi-Precision Integer (MPI).

MPIs are stored as a 16-bit big-endian bit count followed by the integer bytes (big-endian, minimum length for the value).

Returns the bit length and the value as a hex string.

§Errors

Returns Error::UnexpectedEnd if the data is truncated.

Source

pub fn variable_length(&mut self) -> Result<usize>

Reads an OpenPGP variable-length value.

This is the new-format packet length encoding:

  • 0-191: one byte, literal value
  • 192-254: two bytes, ((first - 192) << 8) + second + 192
  • 255: five bytes, 32-bit big-endian length
§Errors

Returns Error::UnexpectedEnd if the data is truncated.

Source

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

Skips count bytes without returning them.

§Errors

Returns Error::UnexpectedEnd if fewer than count bytes remain.

Source

pub fn all_bytes(&self) -> &[u8]

Returns a reference to all bytes in this stream’s range.

Unlike rest(), this does not consume the bytes or advance the position.

Trait Implementations§

Source§

impl Clone for ByteStream

Source§

fn clone(&self) -> ByteStream

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.