[][src]Struct iced_x86::Encoder

pub struct Encoder { /* fields omitted */ }

Encodes instructions decoded by the decoder or instructions created by other code. See also BlockEncoder which can encode any number of instructions.

use iced_x86::*;

// xchg ah,[rdx+rsi+16h]
let bytes = b"\x86\x64\x32\x16";
let mut decoder = Decoder::new(64, bytes, DecoderOptions::NONE);
decoder.set_ip(0x1234_5678);
let instr = decoder.decode();

let mut encoder = Encoder::new(64);
match encoder.encode(&instr, 0x5555_5555) {
    Ok(len) => assert_eq!(4, len),
    Err(err) => panic!("{}", err),
}
// We're done, take ownership of the buffer
let buffer = encoder.take_buffer();
assert_eq!(vec![0x86, 0x64, 0x32, 0x16], buffer);

Implementations

impl Encoder[src]

#[must_use]pub fn new(bitness: u32) -> Self[src]

Creates an encoder

Panics

Panics if bitness is not one of 16, 32, 64.

Arguments

  • bitness: 16, 32 or 64

#[must_use]pub fn with_capacity(bitness: u32, capacity: usize) -> Self[src]

Creates an encoder with an initial buffer capacity

Panics

Panics if bitness is not one of 16, 32, 64.

Arguments

  • bitness: 16, 32 or 64
  • capacity: Initial capacity of the u8 buffer

pub fn encode(
    &mut self,
    instruction: &Instruction,
    rip: u64
) -> Result<usize, String>
[src]

Encodes an instruction and returns the size of the encoded instruction

Errors

Returns an error message on failure.

Arguments

  • instruction: Instruction to encode
  • rip: RIP of the encoded instruction

Examples

use iced_x86::*;

// je short $+4
let bytes = b"\x75\x02";
let mut decoder = Decoder::new(64, bytes, DecoderOptions::NONE);
decoder.set_ip(0x1234_5678);
let instr = decoder.decode();

let mut encoder = Encoder::new(64);
// Use a different IP (orig rip + 0x10)
match encoder.encode(&instr, 0x1234_5688) {
    Ok(len) => assert_eq!(2, len),
    Err(err) => panic!("{}", err),
}
// We're done, take ownership of the buffer
let buffer = encoder.take_buffer();
assert_eq!(vec![0x75, 0xF2], buffer);

pub fn write_u8(&mut self, value: u8)[src]

Writes a byte to the output buffer

Arguments

value: Value to write

Examples

use iced_x86::*;

let mut encoder = Encoder::new(64);
let instr = Instruction::with_reg_reg(Code::Add_r64_rm64, Register::R8, Register::RBP);
encoder.write_u8(0x90);
match encoder.encode(&instr, 0x5555_5555) {
    Ok(len) => assert_eq!(3, len),
    Err(err) => panic!("{}", err),
}
encoder.write_u8(0xCC);
// We're done, take ownership of the buffer
let buffer = encoder.take_buffer();
assert_eq!(vec![0x90, 0x4C, 0x03, 0xC5, 0xCC], buffer);

#[must_use]pub fn take_buffer(&mut self) -> Vec<u8>[src]

Returns the buffer and initializes the internal buffer to an empty vector. Should be called when you've encoded all instructions and need the raw instruction bytes. See also set_buffer().

pub fn set_buffer(&mut self, buffer: Vec<u8>)[src]

Overwrites the buffer with a new vector. The old buffer is dropped. See also take_buffer().

#[must_use]pub fn get_constant_offsets(&self) -> ConstantOffsets[src]

Gets the offsets of the constants (memory displacement and immediate) in the encoded instruction. The caller can use this information to add relocations if needed.

#[must_use]pub fn prevent_vex2(&self) -> bool[src]

Disables 2-byte VEX encoding and encodes all VEX instructions with the 3-byte VEX encoding

pub fn set_prevent_vex2(&mut self, new_value: bool)[src]

Disables 2-byte VEX encoding and encodes all VEX instructions with the 3-byte VEX encoding

Arguments

  • new_value: new value

#[must_use]pub fn vex_wig(&self) -> u32[src]

Value of the VEX.W bit to use if it's an instruction that ignores the bit. Default is 0.

pub fn set_vex_wig(&mut self, new_value: u32)[src]

Value of the VEX.W bit to use if it's an instruction that ignores the bit. Default is 0.

Arguments

  • new_value: new value (0 or 1)

#[must_use]pub fn vex_lig(&self) -> u32[src]

Value of the VEX.L bit to use if it's an instruction that ignores the bit. Default is 0.

pub fn set_vex_lig(&mut self, new_value: u32)[src]

Value of the VEX.L bit to use if it's an instruction that ignores the bit. Default is 0.

Arguments

  • new_value: new value (0 or 1)

#[must_use]pub fn evex_wig(&self) -> u32[src]

Value of the EVEX.W bit to use if it's an instruction that ignores the bit. Default is 0.

pub fn set_evex_wig(&mut self, new_value: u32)[src]

Value of the EVEX.W bit to use if it's an instruction that ignores the bit. Default is 0.

Arguments

  • new_value: new value (0 or 1)

#[must_use]pub fn evex_lig(&self) -> u32[src]

Value of the EVEX.L'L bits to use if it's an instruction that ignores the bits. Default is 0.

pub fn set_evex_lig(&mut self, new_value: u32)[src]

Value of the EVEX.L'L bits to use if it's an instruction that ignores the bits. Default is 0.

Arguments

  • new_value: new value (0 or 3)

#[must_use]pub fn bitness(&self) -> u32[src]

Gets the bitness (16, 32 or 64)

Auto Trait Implementations

impl !RefUnwindSafe for Encoder

impl Send for Encoder

impl Sync for Encoder

impl Unpin for Encoder

impl !UnwindSafe for Encoder

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.