mirror of
https://gitlab.com/qemu-project/capstone.git
synced 2025-11-11 08:16:15 +08:00
give reg_name, insn_name, group_name a customizable default instead of returning something else for id 0
remove the check for self._raw.id on reg_name and group_name (in CsInsn, since the to_name functions don't operate on the current instruction) Add reg_name, insn_name and group_name to Cs. update test_group_name.py with the new api.
This commit is contained in:
@@ -435,6 +435,8 @@ def copy_ctypes(src):
|
||||
ctypes.pointer(dst)[0] = src
|
||||
return dst
|
||||
|
||||
def _ascii_name_or_default(name, default):
|
||||
return default if name is None else name.decode('ascii')
|
||||
|
||||
# Python-style class to disasm code
|
||||
class CsInsn(object):
|
||||
@@ -573,43 +575,31 @@ class CsInsn(object):
|
||||
return _cs.cs_errno(self._cs.csh)
|
||||
|
||||
# get the register name, given the register ID
|
||||
def reg_name(self, reg_id):
|
||||
if self._raw.id == 0:
|
||||
raise CsError(CS_ERR_SKIPDATA)
|
||||
|
||||
def reg_name(self, reg_id, default=None):
|
||||
if self._cs._diet:
|
||||
# Diet engine cannot provide register name
|
||||
raise CsError(CS_ERR_DIET)
|
||||
|
||||
if reg_id == 0:
|
||||
return "(invalid)"
|
||||
|
||||
return _cs.cs_reg_name(self._cs.csh, reg_id).decode('ascii')
|
||||
return _ascii_name_or_default(_cs.cs_reg_name(self._cs.csh, reg_id), default)
|
||||
|
||||
# get the instruction name
|
||||
def insn_name(self):
|
||||
def insn_name(self, default=None):
|
||||
if self._cs._diet:
|
||||
# Diet engine cannot provide instruction name
|
||||
raise CsError(CS_ERR_DIET)
|
||||
|
||||
if self._raw.id == 0:
|
||||
return "(invalid)"
|
||||
return default
|
||||
|
||||
return _cs.cs_insn_name(self._cs.csh, self.id).decode('ascii')
|
||||
return _ascii_name_or_default(_cs.cs_insn_name(self._cs.csh, self.id).decode('ascii'), default)
|
||||
|
||||
# get the group name
|
||||
def group_name(self, group_id):
|
||||
if self._raw.id == 0:
|
||||
raise CsError(CS_ERR_SKIPDATA)
|
||||
|
||||
def group_name(self, group_id, default=None):
|
||||
if self._cs._diet:
|
||||
# Diet engine cannot provide group name
|
||||
raise CsError(CS_ERR_DIET)
|
||||
|
||||
if group_id == 0:
|
||||
return "(invalid)"
|
||||
|
||||
return _cs.cs_group_name(self._cs.csh, group_id).decode('ascii')
|
||||
return _ascii_name_or_default(_cs.cs_group_name(self._cs.csh, group_id), default)
|
||||
|
||||
|
||||
# verify if this insn belong to group with id as @group_id
|
||||
@@ -862,6 +852,33 @@ class Cs(object):
|
||||
# save mode
|
||||
self._mode = opt
|
||||
|
||||
# get the last error code
|
||||
def errno(self):
|
||||
return _cs.cs_errno(self.csh)
|
||||
|
||||
# get the register name, given the register ID
|
||||
def reg_name(self, reg_id, default=None):
|
||||
if self._diet:
|
||||
# Diet engine cannot provide register name
|
||||
raise CsError(CS_ERR_DIET)
|
||||
|
||||
return _ascii_name_or_default(_cs.cs_reg_name(self.csh, reg_id), default)
|
||||
|
||||
# get the instruction name, given the instruction ID
|
||||
def insn_name(self, insn_id, default=None):
|
||||
if self._diet:
|
||||
# Diet engine cannot provide instruction name
|
||||
raise CsError(CS_ERR_DIET)
|
||||
|
||||
return _ascii_name_or_default(_cs.cs_insn_name(self.csh, insn_id), default)
|
||||
|
||||
# get the group name
|
||||
def group_name(self, group_id, default=None):
|
||||
if self._diet:
|
||||
# Diet engine cannot provide group name
|
||||
raise CsError(CS_ERR_DIET)
|
||||
|
||||
return _ascii_name_or_default(_cs.cs_group_name(self.csh, group_id), default)
|
||||
|
||||
# Disassemble binary & return disassembled instructions in CsInsn objects
|
||||
def disasm(self, code, offset, count=0):
|
||||
|
||||
@@ -11,10 +11,6 @@ from capstone.x86 import *
|
||||
from capstone.xcore import *
|
||||
import sys
|
||||
|
||||
# yes this is bad, importing ctypes like this,
|
||||
# but the Cs object did not have the group_name function
|
||||
from capstone import _cs
|
||||
|
||||
class GroupTest:
|
||||
def __init__(self, name, arch, mode, data):
|
||||
self.name = name
|
||||
@@ -27,7 +23,7 @@ class GroupTest:
|
||||
cap = Cs(self.arch, self.mode)
|
||||
for group_id in xrange(0,255):
|
||||
name = self.data.get(group_id)
|
||||
res = _cs.cs_group_name(cap.csh, group_id)
|
||||
res = cap.group_name(group_id)
|
||||
if res != name:
|
||||
print("ERROR: id = %u expected '%s', but got '%s'" %(group_id, name, res))
|
||||
print("")
|
||||
|
||||
Reference in New Issue
Block a user