Function pwn::util::flat::flat

source · []
pub fn flat(item: impl Flatten) -> Vec<u8>
Expand description

Flatten an item into bytes in a crate::context-aware way. Un-filled bytes in the buffer will be replaced with 'a'.

Examples

Packing a simple list of numbers

use pwn::{flat, context, I386};
context::set_arch(I386);
assert_eq!(flat(&[1u32, 2, 3]), b"\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00")

Pack a complex sequence of items with offsets

use pwn::{flat, context, I386, Flatten};
context::set_arch(I386);
let mapping: Vec<(usize, Box<dyn Flatten>)> = vec![
    (12, Box::new(0x41414141u32)),
    (24, Box::new(b"Hello!\xff"))
];
assert_eq!(flat(&*mapping), b"aaaaaaaaaaaaAAAAaaaaaaaaHello!\xff");

Warnings

  • Sequences of u8 will not be packed according to the context, and instead returned as passed. This is to allow byte-strings to be included in flat().
  • Overlapping values will result in a panic.