Struct iced_x86::Encoder

source ·
pub struct Encoder { /* private fields */ }
Expand description

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::with_ip(64, bytes, 0x1234_5678, DecoderOptions::NONE);
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!(buffer, vec![0x86, 0x64, 0x32, 0x16]);

Implementations§

source§

impl Encoder

source

pub fn new(bitness: u32) -> Self

Creates an encoder

Panics

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

Arguments
  • bitness: 16, 32 or 64
source

pub fn try_new(bitness: u32) -> Result<Self, IcedError>

Creates an encoder

Errors

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

Arguments
  • bitness: 16, 32 or 64
source

pub fn try_with_capacity( bitness: u32, capacity: usize ) -> Result<Self, IcedError>

Creates an encoder with an initial buffer capacity

Errors

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

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

pub fn encode( &mut self, instruction: &Instruction, rip: u64 ) -> Result<usize, IcedError>

Encodes an instruction and returns the size of the encoded instruction

Errors

Returns an error if it failed to encode the instruction.

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::with_ip(64, bytes, 0x1234_5678, DecoderOptions::NONE);
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!(buffer, vec![0x75, 0xF2]);
source

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

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::with2(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!(buffer, vec![0x90, 0x4C, 0x03, 0xC5, 0xCC]);
source

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

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

source

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

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

source

pub fn get_constant_offsets(&self) -> ConstantOffsets

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.

source

pub const fn prevent_vex2(&self) -> bool

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

source

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

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

Arguments
  • new_value: new value
source

pub const fn vex_wig(&self) -> u32

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

source

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

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

pub const fn vex_lig(&self) -> u32

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

source

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

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

pub const fn evex_wig(&self) -> u32

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

source

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

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

pub const fn evex_lig(&self) -> u32

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

source

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

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

pub const fn mvex_wig(&self) -> u32

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

source

pub fn set_mvex_wig(&mut self, new_value: u32)

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

Arguments
  • new_value: new value (0 or 1)
source

pub const fn bitness(&self) -> u32

Gets the bitness (16, 32 or 64)

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