use arch::ArchDetail;
use std::ffi::CStr;
use std::ptr;
use std::slice;
use std::str;
use std::fmt::{self, Debug, Display, Error, Formatter};
use capstone_sys::*;
use constants::Arch;
#[derive(Debug)]
pub struct Instructions {
ptr: *mut cs_insn,
len: isize,
}
pub type InsnIdInt = u32;
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct InsnId(pub InsnIdInt);
pub type InsnGroupIdInt = u8;
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct InsnGroupId(pub InsnGroupIdInt);
pub type RegIdInt = u16;
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct RegId(pub RegIdInt);
impl Instructions {
pub(crate) unsafe fn from_raw_parts(ptr: *mut cs_insn, len: isize) -> Instructions {
Instructions { ptr: ptr, len: len }
}
pub(crate) fn new_empty() -> Instructions {
Instructions {
ptr: ptr::null_mut(),
len: 0,
}
}
pub fn len(&self) -> isize {
self.len
}
pub fn iter(&self) -> InstructionIterator {
InstructionIterator {
insns: self,
cur: 0,
}
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
}
impl Drop for Instructions {
fn drop(&mut self) {
unsafe {
cs_free(self.ptr, self.len as usize);
}
}
}
pub struct InstructionIterator<'a> {
insns: &'a Instructions,
cur: isize,
}
impl<'a> Iterator for InstructionIterator<'a> {
type Item = Insn;
fn next(&mut self) -> Option<Insn> {
if self.cur == self.insns.len {
None
} else {
let obj = unsafe { self.insns.ptr.offset(self.cur) };
self.cur += 1;
Some(unsafe { Insn(ptr::read(obj)) })
}
}
}
pub struct Insn(pub(crate) cs_insn);
pub struct InsnDetail<'a>(pub(crate) &'a cs_detail, pub(crate) Arch);
impl Insn {
pub fn mnemonic(&self) -> Option<&str> {
let cstr = unsafe { CStr::from_ptr(self.0.mnemonic.as_ptr()) };
str::from_utf8(cstr.to_bytes()).ok()
}
pub fn op_str(&self) -> Option<&str> {
let cstr = unsafe { CStr::from_ptr(self.0.op_str.as_ptr()) };
str::from_utf8(cstr.to_bytes()).ok()
}
pub fn id(&self) -> InsnId {
InsnId(self.0.id)
}
fn len(&self) -> usize {
self.0.size as usize
}
pub fn address(&self) -> u64 {
self.0.address as u64
}
pub fn bytes(&self) -> &[u8] {
&self.0.bytes[..self.len()]
}
pub(crate) unsafe fn detail(&self, arch: Arch) -> InsnDetail {
InsnDetail(&*self.0.detail, arch)
}
}
impl Debug for Insn {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
fmt.debug_struct("Insn")
.field("address", &self.address())
.field("len", &self.len())
.field("bytes", &self.bytes())
.field("mnemonic", &self.mnemonic())
.field("op_str", &self.op_str())
.finish()
}
}
impl Display for Insn {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "{:#x}: ", self.address())?;
if let Some(mnemonic) = self.mnemonic() {
write!(fmt, "{} ", mnemonic)?;
if let Some(op_str) = self.op_str() {
write!(fmt, "{}", op_str)?;
}
}
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct RegsIter<'a, T: 'a + Into<RegIdInt> + Copy>(slice::Iter<'a, T>);
impl<'a, T: 'a + Into<RegIdInt> + Copy> Iterator for RegsIter<'a, T> {
type Item = RegId;
fn next(&mut self) -> Option<Self::Item> {
match self.0.next() {
Some(x) => Some(RegId((*x).into())),
None => None,
}
}
}
#[derive(Debug, Clone)]
pub struct InsnGroupIter<'a>(slice::Iter<'a, InsnGroupIdInt>);
impl<'a> Iterator for InsnGroupIter<'a> {
type Item = InsnGroupId;
fn next(&mut self) -> Option<Self::Item> {
match self.0.next() {
Some(x) => Some(InsnGroupId(*x as InsnGroupIdInt)),
None => None,
}
}
}
impl<'a> InsnDetail<'a> {
pub fn regs_read(&self) -> RegsIter<u8> {
RegsIter((*self.0).regs_read[..self.regs_read_count() as usize].iter())
}
pub fn regs_read_count(&self) -> u8 {
(*self.0).regs_read_count
}
pub fn regs_write(&self) -> RegsIter<u8> {
RegsIter((*self.0).regs_write[..self.regs_write_count() as usize].iter())
}
pub fn regs_write_count(&self) -> u8 {
(*self.0).regs_write_count
}
pub fn groups(&self) -> InsnGroupIter {
InsnGroupIter((*self.0).groups[..self.groups_count() as usize].iter())
}
pub fn groups_count(&self) -> u8 {
(*self.0).groups_count
}
pub fn arch_detail(&self) -> ArchDetail {
macro_rules! def_arch_detail_match {
(
$( [ $ARCH:ident, $detail:ident, $insn_detail:ident, $arch:ident ] )*
) => {
use self::ArchDetail::*;
use Arch::*;
$( use arch::$arch::$insn_detail; )*
return match self.1 {
$(
$ARCH => {
$detail($insn_detail(unsafe { &self.0.__bindgen_anon_1.$arch }))
}
)*
_ => panic!("Unsupported detail arch"),
}
}
}
def_arch_detail_match!(
[ARM, ArmDetail, ArmInsnDetail, arm]
[ARM64, Arm64Detail, Arm64InsnDetail, arm64]
[MIPS, MipsDetail, MipsInsnDetail, mips]
[PPC, PpcDetail, PpcInsnDetail, ppc]
[SPARC, SparcDetail, SparcInsnDetail, sparc]
[X86, X86Detail, X86InsnDetail, x86]
[XCORE, XcoreDetail, XcoreInsnDetail, xcore]
);
}
}
impl<'a> Debug for InsnDetail<'a> {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
fmt.debug_struct("Detail")
.field("regs_read", &self.regs_read())
.field("regs_read_count", &self.regs_read_count())
.field("regs_write", &self.regs_write())
.field("regs_write_count", &self.regs_write_count())
.field("groups", &self.groups())
.field("groups_count", &self.groups_count())
.finish()
}
}
impl Display for Instructions {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
for instruction in self.iter() {
write!(fmt, "{:x}:\t", instruction.address())?;
for byte in instruction.bytes() {
write!(fmt, " {:02x}", byte)?;
}
let remainder = 16 * 3 - (instruction.bytes().len()) * 3;
for _ in 0..remainder {
write!(fmt, " ")?;
}
if let Some(mnemonic) = instruction.mnemonic() {
write!(fmt, " {}", mnemonic)?;
if let Some(op_str) = instruction.op_str() {
write!(fmt, " {}", op_str)?;
}
}
write!(fmt, "\n")?;
}
Ok(())
}
}