Skip to main content

Decoder

Struct Decoder 

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

主解码器实例 / Main decoder instance

解码器保存机器模式和栈宽度的配置,用于解码指令字节。

The decoder holds the configuration for machine mode and stack width, and is used to decode instruction bytes.

§工作流程 / Workflow

  1. 解析前缀(Legacy, REX, VEX, EVEX, XOP)
  2. 解析操作码
  3. 解析 ModR/M 和 SIB 字节(如果存在)
  4. 解析偏移量(如果存在)
  5. 解析立即数(如果存在)
  6. 构建操作数信息

§示例 / Example

use zydis_rs::{Decoder, MachineMode, StackWidth};

// 创建 64 位模式解码器 / Create 64-bit mode decoder
let decoder = Decoder::new(MachineMode::Long64, StackWidth::Width64)?;

// 解码指令 / Decode instruction
let code = &[0x48, 0x89, 0xE5]; // mov rbp, rsp
let instruction = decoder.decode(code)?;

println!("Mnemonic: {:?}", instruction.mnemonic);
println!("Length: {}", instruction.length);
println!("Operand count: {}", instruction.operand_count);

§注意事项 / Notes

  • 解码器是无状态的,可以安全地在多个线程之间共享
  • 单条指令的最大长度为 15 字节
  • 64 位模式下会自动处理 REX 前缀

Implementations§

Source§

impl Decoder

Source

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

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

Create a new decoder for the specified machine mode.

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

返回解码器实例或错误

§错误 / Errors

如果机器模式无效,返回 Error::InvalidMachineMode

§示例 / Example
use zydis_rs::{Decoder, MachineMode, StackWidth};

let decoder = Decoder::new(MachineMode::Long64, StackWidth::Width64)?;
Source

pub fn new_64bit() -> Self

创建 64 位长模式解码器

Create a decoder for 64-bit long mode.

§示例 / Example
use zydis_rs::Decoder;

let decoder = Decoder::new_64bit();
Source

pub fn new_32bit() -> Self

创建 32 位保护模式解码器

Create a decoder for 32-bit protected mode.

§示例 / Example
use zydis_rs::Decoder;

let decoder = Decoder::new_32bit();
Source

pub fn set_mode(&mut self, mode: DecoderMode, enabled: bool)

设置解码器模式

Set a specific decoder mode.

§参数 / Arguments
  • mode - 要设置的模式 / The mode to set
  • enabled - 是否启用该模式 / Whether to enable the mode
§示例 / Example
use zydis_rs::{Decoder, decoder::DecoderMode};

let mut decoder = Decoder::new_64bit();
decoder.set_mode(DecoderMode::Minimal, true);  // 启用最小模式
decoder.set_mode(DecoderMode::AmdBranches, false);  // 禁用 AMD 分支模式
Source

pub const fn modes(&self) -> &DecoderModes

获取当前解码器模式

Get the current decoder modes.

§返回 / Returns

返回当前解码器模式配置

Source

pub const fn is_mode_enabled(&self, mode: DecoderMode) -> bool

检查特定模式是否启用

Check if a specific mode is enabled.

§参数 / Arguments
  • mode - 要检查的模式 / The mode to check
§返回 / Returns

如果该模式启用则返回 true

Source

pub const fn machine_mode(&self) -> MachineMode

获取机器模式 / Get the machine mode

§返回 / Returns

解码器配置的机器模式

Source

pub const fn stack_width(&self) -> u8

获取栈宽度(位)/ Get the stack width

Source

pub const fn is_64_bit(&self) -> bool

检查是否为 64 位模式 / Check if this is 64-bit mode

Source

pub fn decode(&self, bytes: &[u8]) -> Result<DecodedInstruction>

从字节解码单条指令

Decode a single instruction from bytes.

§参数 / Arguments
  • bytes - 要解码的指令字节 / The instruction bytes to decode
§返回 / Returns

成功时返回 Ok(DecodedInstruction),失败时返回错误

§错误 / Errors
  • Error::InsufficientBytes - 字节不足,无法构成有效指令
  • Error::InvalidOpcode - 无效的操作码
  • Error::InstructionTooLong - 指令长度超过 15 字节
§示例 / Example
use zydis_rs::Decoder;

let decoder = Decoder::new_64bit();
let instruction = decoder.decode(&[0x48, 0x89, 0xE5])?;  // MOV rbp, rsp
Source

pub fn decode_with_address( &self, bytes: &[u8], address: u64, ) -> Result<DecodedInstruction>

解码单条指令并指定地址

Decode a single instruction with a specific address.

§参数 / Arguments
  • bytes - 要解码的指令字节 / The instruction bytes to decode
  • address - 指令地址(用于 RIP 相对寻址)/ The address of the instruction (for RIP-relative addressing)
§返回 / Returns

成功时返回解码后的指令,失败时返回错误

§注意 / Notes

在 64 位模式下,某些指令(如 [rip + disp32])需要知道指令地址才能正确解析。 使用此方法可以确保 RIP 相对寻址被正确处理。

§示例 / Example
use zydis_rs::Decoder;

let decoder = Decoder::new_64bit();
// RIP 相对寻址需要指定地址 / RIP-relative addressing requires address
let instruction = decoder.decode_with_address(&[0x48, 0x8B, 0x05, 0x00, 0x00, 0x00, 0x00], 0x1000)?;

Trait Implementations§

Source§

impl Clone for Decoder

Source§

fn clone(&self) -> Decoder

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 Decoder

Source§

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

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

impl Default for Decoder

Source§

fn default() -> Self

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

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.