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
- 解析前缀(Legacy, REX, VEX, EVEX, XOP)
- 解析操作码
- 解析 ModR/M 和 SIB 字节(如果存在)
- 解析偏移量(如果存在)
- 解析立即数(如果存在)
- 构建操作数信息
§示例 / 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
impl Decoder
Sourcepub fn new(machine_mode: MachineMode, stack_width: u8) -> Result<Self>
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)?;Sourcepub fn set_mode(&mut self, mode: DecoderMode, enabled: bool)
pub fn set_mode(&mut self, mode: DecoderMode, enabled: bool)
设置解码器模式
Set a specific decoder mode.
§参数 / Arguments
mode- 要设置的模式 / The mode to setenabled- 是否启用该模式 / 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 分支模式Sourcepub const fn modes(&self) -> &DecoderModes
pub const fn modes(&self) -> &DecoderModes
Sourcepub const fn is_mode_enabled(&self, mode: DecoderMode) -> bool
pub const fn is_mode_enabled(&self, mode: DecoderMode) -> bool
Sourcepub const fn machine_mode(&self) -> MachineMode
pub const fn machine_mode(&self) -> MachineMode
Sourcepub const fn stack_width(&self) -> u8
pub const fn stack_width(&self) -> u8
获取栈宽度(位)/ Get the stack width
Sourcepub fn decode(&self, bytes: &[u8]) -> Result<DecodedInstruction>
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, rspSourcepub fn decode_with_address(
&self,
bytes: &[u8],
address: u64,
) -> Result<DecodedInstruction>
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 decodeaddress- 指令地址(用于 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§
Auto Trait Implementations§
impl Freeze for Decoder
impl RefUnwindSafe for Decoder
impl Send for Decoder
impl Sync for Decoder
impl Unpin for Decoder
impl UnsafeUnpin for Decoder
impl UnwindSafe for Decoder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more