Skip to content

Commit

Permalink
Merge pull request zyantific#2 from th0rex/master
Browse files Browse the repository at this point in the history
Add support for wrapped formatter hooks
  • Loading branch information
athre0z authored Oct 4, 2017
2 parents a80e61e + fa5db42 commit d014f0b
Show file tree
Hide file tree
Showing 6 changed files with 365 additions and 15 deletions.
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fn build_library() {
format!("{}/Encoder.c", ZYDIS_SRC_PATH),
format!("{}/EncoderData.c", ZYDIS_SRC_PATH),
format!("{}/Formatter.c", ZYDIS_SRC_PATH),
format!("{}/FormatHelper.c", ZYDIS_SRC_PATH),
format!("{}/Mnemonic.c", ZYDIS_SRC_PATH),
format!("{}/Register.c", ZYDIS_SRC_PATH),
format!("{}/SharedData.c", ZYDIS_SRC_PATH),
Expand Down
32 changes: 32 additions & 0 deletions examples/formatter_hooks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
extern crate zydis;
use zydis::gen::*;
use zydis::*;

#[cfg_attr(rustfmt, rustfmt_skip)]
static CODE: &'static [u8] = &[
0x51, 0x8D, 0x45, 0xFF, 0x50, 0xFF, 0x75, 0x0C, 0xFF, 0x75, 0x08,
0xFF, 0x15, 0xA0, 0xA5, 0x48, 0x76, 0x85, 0xC0, 0x0F, 0x88, 0xFC,
0xDA, 0x02, 0x00u8,
];

fn print_mnemonic(
_formatter: &Formatter,
buffer: &mut Buffer,
_instruction: &ZydisDecodedInstruction,
) -> ZydisResult<()> {
buffer.append("abc")
}

fn main() {
let mut formatter = Formatter::new(ZYDIS_FORMATTER_STYLE_INTEL).unwrap();
formatter
.set_print_mnemonic(Box::new(print_mnemonic))
.unwrap();

let decoder = Decoder::new(ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_ADDRESS_WIDTH_64).unwrap();

for (mut instruction, ip) in decoder.instruction_iterator(CODE, 0) {
let insn = formatter.format_instruction(&mut instruction, 200);
println!("0x{:016X} {}", ip, insn.unwrap());
}
}
14 changes: 6 additions & 8 deletions examples/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ fn main() {

// Obtain offsets for relevant operands, skip others.
let (dyn_offs, dyn_len) = match op.type_ as ZydisOperandTypes {
ZYDIS_OPERAND_TYPE_MEMORY if op.mem.disp.hasDisplacement == 1 => (
insn.raw.disp.offset,
insn.raw.disp.size,
),
ZYDIS_OPERAND_TYPE_IMMEDIATE if op.imm.isRelative == 1 => (
insn.raw.imm[op_idx].offset,
insn.raw.imm[op_idx].size,
),
ZYDIS_OPERAND_TYPE_MEMORY if op.mem.disp.hasDisplacement == 1 => {
(insn.raw.disp.offset, insn.raw.disp.size)
}
ZYDIS_OPERAND_TYPE_IMMEDIATE if op.imm.isRelative == 1 => {
(insn.raw.imm[op_idx].offset, insn.raw.imm[op_idx].size)
}
_ => continue,
};

Expand Down
Loading

0 comments on commit d014f0b

Please sign in to comment.