1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use libc;
use std::ptr;
use constants::*;
use ffi::{cs_close,cs_open,cs_disasm};
use instruction::{Insn,Instructions};
pub struct Capstone {
csh: libc::size_t,
}
impl Capstone {
pub fn new(arch: CsArch, mode: CsMode) -> Option<Capstone> {
let mut handle: libc::size_t = 0;
if let CsErr::CS_ERR_OK = unsafe { cs_open(arch, mode, &mut handle) } {
Some(Capstone {
csh: handle
})
} else {
None
}
}
pub fn disasm(&self, code: &[u8], addr: u64, count: isize) -> Option<Instructions> {
let mut ptr: *const Insn = ptr::null();
let insn_count = unsafe { cs_disasm(self.csh, code.as_ptr(), code.len() as libc::size_t,
addr, count as libc::size_t, &mut ptr) };
if insn_count == 0 {
return None
}
Some(Instructions::from_raw_parts(ptr, insn_count as isize))
}
}
impl Drop for Capstone {
fn drop(&mut self) {
unsafe { cs_close(&mut self.csh) };
}
}