Struct UncheckedPacket

Source
pub struct UncheckedPacket {
    pub kind: Kind,
    pub data: Vec<u8>,
    pub checksum: [u8; 2],
}

Fields§

§kind: Kind§data: Vec<u8>§checksum: [u8; 2]

Implementations§

Source§

impl UncheckedPacket

Source

pub fn expected_checksum(&self) -> Result<u8, Error>

Return the integer parsed from the hexadecimal expected checksum.

let packet = UncheckedPacket {
    kind: Kind::Packet,
    data: b"Hello, World!".to_vec(),
    checksum: *b"BA",
};
assert_eq!(packet.expected_checksum().unwrap(), 186);
Source

pub fn actual_checksum(&self) -> u8

Return the actual checksum, derived from the data.

let packet = UncheckedPacket {
    kind: Kind::Packet,
    data: b"Hello, World!".to_vec(),
    checksum: *b"BA",
};
assert_eq!(packet.actual_checksum(), 105);

As per the GDB specification, this is currently a sum of all characters, modulo 256. The same result can be compared with

(input.bytes().map(|x| usize::from(x)).sum::<usize>() % 256) as u8

however, this function is more efficient and won’t go out of bounds.

Source

pub fn is_valid(&self) -> bool

Returns true if the checksums match.

assert_eq!(UncheckedPacket {
    kind: Kind::Packet,
    data: b"Rust is an amazing programming language".to_vec(),
    checksum: *b"00",
}.is_valid(), false);
assert_eq!(UncheckedPacket {
    kind: Kind::Packet,
    data: b"Rust is an amazing programming language".to_vec(),
    checksum: *b"ZZ",
}.is_valid(), false);
assert_eq!(UncheckedPacket {
    kind: Kind::Packet,
    data: b"Rust is an amazing programming language".to_vec(),
    checksum: *b"C7",
}.is_valid(), true);
Source

pub fn check(self) -> Option<CheckedPacket>

Will return a checked packet if, and only if, the checksums match. If you know the packet wasn’t corrupted and want to bypass the check, use CheckedPacket::assume_checked.

Source

pub fn encode<W>(&self, w: &mut W) -> Result<()>
where W: Write,

Encode the packet into a long binary string, written to a writer of choice. You can receive a Vec by taking advantage of the fact that they implement io::Write:

let mut encoded = Vec::new();
UncheckedPacket {
    kind: Kind::Packet,
    data: b"these must be escaped: # $ } *".to_vec(),
    checksum: *b"00",
}.encode(&mut encoded);
assert_eq!(
    encoded,
    b"$these must be escaped: }\x03 }\x04 }] }\x0a#00".to_vec()
);

Currently multiple series repeated characters aren’t shortened, however, this may change at any time and you should not rely on the output of this function being exactly one of multiple representations.

Examples found in repository?
examples/repl.rs (line 26)
4fn main() -> Result<(), Error> {
5    println!("Listening on port 1337...");
6    let mut server = GdbServer::listen("0.0.0.0:1337")?;
7    println!("Connected!");
8
9    while let Some(packet) = server.next_packet()? {
10        println!(
11            "-> {:?} {:?}",
12            packet.kind,
13            std::str::from_utf8(&packet.data)
14        );
15
16        print!(": ");
17        io::stdout().flush()?;
18        let mut response = String::new();
19        io::stdin().read_line(&mut response)?;
20        if response.ends_with('\n') {
21            response.truncate(response.len() - 1);
22        }
23        let response = CheckedPacket::from_data(Kind::Packet, response.into_bytes());
24
25        let mut bytes = Vec::new();
26        response.encode(&mut bytes).unwrap();
27        println!("<- {:?}", std::str::from_utf8(&bytes));
28
29        server.dispatch(&response)?;
30    }
31
32    println!("EOF");
33    Ok(())
34}

Trait Implementations§

Source§

impl Clone for UncheckedPacket

Source§

fn clone(&self) -> UncheckedPacket

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for UncheckedPacket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for UncheckedPacket

Source§

fn eq(&self, other: &UncheckedPacket) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for UncheckedPacket

Source§

impl StructuralPartialEq for UncheckedPacket

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> 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.