-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use std::ops::{Deref, DerefMut}; | ||
use crate::{ffi, *}; | ||
use crate::ffi::ZydisEncoderEncodeInstruction; | ||
|
||
#[repr(transparent)] | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
pub struct EncoderRequest(ffi::EncoderRequest); | ||
|
||
impl Deref for EncoderRequest { | ||
type Target = ffi::EncoderRequest; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl DerefMut for EncoderRequest { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl EncoderRequest { | ||
pub fn new32(mnemonic: Mnemonic) -> Self { | ||
Self::new(MachineMode::LONG_COMPAT_32, mnemonic) | ||
} | ||
|
||
pub fn new64(mnemonic: Mnemonic) -> Self { | ||
Self::new(MachineMode::LONG_64, mnemonic) | ||
} | ||
|
||
/// Create a new encoder request from scratch. | ||
pub fn new(machine_mode: MachineMode, mnemonic: Mnemonic) -> Self { | ||
let mut request: ffi::EncoderRequest = unsafe { core::mem::zeroed() }; | ||
request.machine_mode = machine_mode; | ||
request.mnemonic = mnemonic; | ||
Self(request) | ||
} | ||
|
||
pub fn encode_into(&self, buf: &mut [u8]) -> Result<usize> { | ||
unsafe { | ||
let mut length = buf.len(); | ||
ZydisEncoderEncodeInstruction(&self.0, buf.as_ptr() as _, &mut length).as_result()?; | ||
Ok(length) | ||
} | ||
} | ||
|
||
pub fn encode(&self) -> Result<Vec<u8>> { | ||
let mut out = vec![0; MAX_INSTRUCTION_LENGTH]; | ||
let length = self.encode_into(&mut out[..])?; | ||
out.resize(length, 0); | ||
Ok(out) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::ffi::EncoderOperand; | ||
use super::*; | ||
|
||
#[test] | ||
fn encode_int3() { | ||
let req = EncoderRequest::new64(Mnemonic::INT3); | ||
let enc = req.encode().unwrap(); | ||
assert_eq!(enc, vec![0xCC]); | ||
} | ||
|
||
#[test] | ||
fn encode_mov() { | ||
let mut req = EncoderRequest::new64(Mnemonic::MOV); | ||
|
||
req.operand_count = 2; | ||
req.operands[0] = EncoderOperand:: | ||
|
||
let enc = req.encode().unwrap(); | ||
assert_eq!(enc, vec![0xCC]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters