Skip to main content

Encoder

Struct Encoder 

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

x86/x64 指令编码器

The Encoder struct is responsible for converting instruction requests into raw bytes that can be executed by the CPU.

编码器负责将指令请求转换为可执行的机器码。

§支持的指令 / Supported Instructions

  • 整数运算:ADD, SUB, AND, OR, XOR, CMP, TEST
  • 数据传送:MOV, LEA, MOVSX, MOVZX
  • 跳转指令:JMP, JE, JNE, JL, JG, JLE, JGE, CALL, RET
  • 栈操作:PUSH, POP
  • AVX 指令:VMOVAPS, VADDPS, VSUBPS, VMULPS, VDIVPS, VXORPS 等
  • AVX-512 指令:VPADDD, VPSUBD, VPANDD, VPORD 等

§示例 / Example

use zydis_rs::{Encoder, EncoderRequest, Mnemonic, Register, MachineMode};

let encoder = Encoder::new(MachineMode::Long64, 64)?;

// 编码 MOV RAX, 0x123456789ABCDEF0
let request = EncoderRequest::new(Mnemonic::MOV)
    .with_reg(Register::RAX)
    .with_imm(0x123456789ABCDEF0u64);
let bytes = encoder.encode(&request)?;

// 编码 ADD RAX, RBX
let request2 = EncoderRequest::new(Mnemonic::ADD)
    .with_reg(Register::RAX)
    .with_reg(Register::RBX);
let bytes2 = encoder.encode(&request2)?;

§注意事项 / Notes

  • 单条指令最大长度为 15 字节
  • 64 位模式下某些指令需要 REX 前缀
  • AVX 指令使用 VEX 前缀
  • AVX-512 指令使用 EVEX 前缀

Implementations§

Source§

impl Encoder

Source

pub fn new(mode: MachineMode, stack_width: u8) -> Result<Self>

创建指定机器模式的编码器

Create a new encoder with the specified machine mode and stack width.

§参数 / Arguments
  • mode - 机器模式 / The machine mode (Long64, Protected32, etc.)
  • stack_width - 栈宽度(16, 32 或 64 位)/ The stack width in bits (16, 32, or 64)
§返回 / Returns

返回编码器实例或错误

§错误 / Errors
  • Error::InvalidStackWidth - 如果栈宽度不是 16、32 或 64
§示例 / Example
use zydis_rs::{Encoder, MachineMode};

let encoder = Encoder::new(MachineMode::Long64, 64)?;
Source

pub const fn machine_mode(&self) -> MachineMode

获取机器模式 / Get the machine mode

Source

pub const fn stack_width(&self) -> u8

获取栈宽度 / Get the stack width

Source

pub fn encode(&self, request: &EncoderRequest) -> Result<Vec<u8>>

编码指令请求为字节序列

Encode an instruction request to bytes.

§参数 / Arguments
  • request - 要编码的指令请求 / The instruction request to encode
§返回 / Returns

包含编码后指令字节的向量

§错误 / Errors
  • Error::UnsupportedInstruction - 不支持的指令
  • Error::InvalidOperand - 无效的操作数
  • Error::EncodeError - 编码过程中的其他错误
§示例 / Example
use zydis_rs::{Encoder, MachineMode, EncoderRequest, Mnemonic, Register};

let encoder = Encoder::new(MachineMode::Long64, 64)?;
let request = EncoderRequest::new(Mnemonic::MOV)
    .with_reg(Register::RAX)
    .with_reg(Register::RBX);

let bytes = encoder.encode(&request)?;
Source

pub fn encode_to_buffer( &self, request: &EncoderRequest, buffer: &mut [u8], ) -> Result<usize>

编码指令到预分配的缓冲区

Encode an instruction to a pre-allocated buffer.

§参数 / Arguments
  • request - 要编码的指令请求 / The instruction request to encode
  • buffer - 写入编码字节的缓冲区(至少 15 字节)/ The buffer to write the encoded bytes to
§返回 / Returns

写入缓冲区的字节数

§错误 / Errors
  • Error::BufferTooSmall - 缓冲区太小(< 15 字节)
  • Error::UnsupportedInstruction - 不支持的指令
  • Error::InvalidOperand - 无效的操作数
§示例 / Example
use zydis_rs::{Encoder, EncoderRequest, Mnemonic, Register, MachineMode};

let encoder = Encoder::new(MachineMode::Long64, 64)?;
let request = EncoderRequest::new(Mnemonic::NOP);

let mut buffer = [0u8; 15];
let len = encoder.encode_to_buffer(&request, &mut buffer)?;
assert_eq!(len, 1);  // NOP is 0x90
assert_eq!(buffer[0], 0x90);

Trait Implementations§

Source§

impl Clone for Encoder

Source§

fn clone(&self) -> Encoder

Returns a duplicate 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 Encoder

Source§

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

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

impl Default for Encoder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Copy for Encoder

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.