Rizin
unix-like reverse engineering framework and cli tools
capstone.Cs Class Reference
Inheritance diagram for capstone.Cs:

Public Member Functions

def __init__ (self, arch, mode)
 
def __del__ (self)
 
def diet (self)
 
def x86_reduce (self)
 
def syntax (self)
 
def syntax (self, style)
 
def skipdata (self)
 
def skipdata (self, opt)
 
def skipdata_setup (self)
 
def skipdata_setup (self, opt)
 
def skipdata_mnem (self)
 
def skipdata_mnem (self, mnem)
 
def skipdata_callback (self)
 
def skipdata_callback (self, val)
 
def mnemonic_setup (self, id, mnem)
 
def support (self, query)
 
def detail (self)
 
def detail (self, opt)
 
def imm_unsigned (self)
 
def imm_unsigned (self, opt)
 
def mode (self)
 
def mode (self, opt)
 
def errno (self)
 
def reg_name (self, reg_id, default=None)
 
def insn_name (self, insn_id, default=None)
 
def group_name (self, group_id, default=None)
 
def disasm (self, code, offset, count=0)
 
def disasm_lite (self, code, offset, count=0)
 

Public Attributes

 csh
 
 disasm
 
 skipdata_setup
 

Private Attributes

 _mode
 
 _syntax
 
 _detail
 
 _imm_unsigned
 
 _diet
 
 _x86reduce
 
 _skipdata_mnem
 
 _skipdata_cb
 
 _skipdata_opt
 
 _skipdata
 

Detailed Description

Definition at line 798 of file __init__.py.

Constructor & Destructor Documentation

◆ __init__()

def capstone.Cs.__init__ (   self,
  arch,
  mode 
)

Definition at line 799 of file __init__.py.

799  def __init__(self, arch, mode):
800  # verify version compatibility with the core before doing anything
801  (major, minor, _combined) = cs_version()
802  if major != CS_API_MAJOR or minor != CS_API_MINOR:
803  self.csh = None
804  # our binding version is different from the core's API version
805  raise CsError(CS_ERR_VERSION)
806 
807  self.arch, self._mode = arch, mode
808  self.csh = ctypes.c_size_t()
809  status = _cs.cs_open(arch, mode, ctypes.byref(self.csh))
810  if status != CS_ERR_OK:
811  self.csh = None
812  raise CsError(status)
813 
814  try:
815  import ccapstone
816  # rewire disasm to use the faster version
817  self.disasm = ccapstone.Cs(self).disasm
818  except:
819  pass
820 
821  if arch == CS_ARCH_X86:
822  # Intel syntax is default for X86
823  self._syntax = CS_OPT_SYNTAX_INTEL
824  else:
825  self._syntax = None
826 
827  self._detail = False # by default, do not produce instruction details
828  self._imm_unsigned = False # by default, print immediate operands as signed numbers
829  self._diet = cs_support(CS_SUPPORT_DIET)
830  self._x86reduce = cs_support(CS_SUPPORT_X86_REDUCE)
831 
832  # default mnemonic for SKIPDATA
833  self._skipdata_mnem = ".byte"
834  self._skipdata_cb = (None, None)
835  # store reference to option object to avoid it being freed
836  # because C code uses it by reference
837  self._skipdata_opt = _cs_opt_skipdata()
838  self._skipdata = False
839 
840 
841 
CAPSTONE_EXPORT unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Definition: cs.c:357
CAPSTONE_EXPORT bool CAPSTONE_API cs_support(int query)
Definition: cs.c:368

References capstone.cs_version().

◆ __del__()

def capstone.Cs.__del__ (   self)

Definition at line 843 of file __init__.py.

843  def __del__(self):
844  if self.csh:
845  try:
846  status = _cs.cs_close(ctypes.byref(self.csh))
847  if status != CS_ERR_OK:
848  raise CsError(status)
849  except: # _cs might be pulled from under our feet
850  pass
851 
852 

References capstone._dummy_cs.csh, capstone.Cs.csh, and MCInst.csh.

Member Function Documentation

◆ detail() [1/2]

def capstone.Cs.detail (   self)

Definition at line 968 of file __init__.py.

968  def detail(self):
969  return self._detail
970 
971 

References capstone._dummy_cs._detail, and capstone.Cs._detail.

Referenced by capstone.Cs.detail().

◆ detail() [2/2]

def capstone.Cs.detail (   self,
  opt 
)

Definition at line 974 of file __init__.py.

974  def detail(self, opt): # opt is boolean type, so must be either 'True' or 'False'
975  if opt == False:
976  status = _cs.cs_option(self.csh, CS_OPT_DETAIL, CS_OPT_OFF)
977  else:
978  status = _cs.cs_option(self.csh, CS_OPT_DETAIL, CS_OPT_ON)
979  if status != CS_ERR_OK:
980  raise CsError(status)
981  # save detail
982  self._detail = opt
983 
984 

References capstone._dummy_cs._detail, capstone.Cs._detail, capstone._dummy_cs.csh, capstone.Cs.csh, MCInst.csh, and capstone.Cs.detail().

◆ diet()

def capstone.Cs.diet (   self)

Definition at line 859 of file __init__.py.

859  def diet(self):
860  return self._diet
861 
862 

References capstone.Cs._diet.

◆ disasm()

def capstone.Cs.disasm (   self,
  code,
  offset,
  count = 0 
)

Definition at line 1048 of file __init__.py.

1048  def disasm(self, code, offset, count=0):
1049  all_insn = ctypes.POINTER(_cs_insn)()
1050  '''if not _python2:
1051  print(code)
1052  code = code.encode()
1053  print(code)'''
1054  # Pass a bytearray by reference
1055  size = len(code)
1056  if isinstance(code, bytearray):
1057  code = ctypes.byref(ctypes.c_char.from_buffer(code))
1058  res = _cs.cs_disasm(self.csh, code, size, offset, count, ctypes.byref(all_insn))
1059  if res > 0:
1060  try:
1061  for i in range(res):
1062  yield CsInsn(self, all_insn[i])
1063  finally:
1064  _cs.cs_free(all_insn, res)
1065  else:
1066  status = _cs.cs_errno(self.csh)
1067  if status != CS_ERR_OK:
1068  raise CsError(status)
1069  return
1070  yield
1071 
1072 
size_t len
Definition: 6502dis.c:15

References capstone._dummy_cs.csh, capstone.Cs.csh, MCInst.csh, capstone.Cs.disasm, len, and capstone.range.

◆ disasm_lite()

def capstone.Cs.disasm_lite (   self,
  code,
  offset,
  count = 0 
)

Definition at line 1076 of file __init__.py.

1076  def disasm_lite(self, code, offset, count=0):
1077  if self._diet:
1078  # Diet engine cannot provide @mnemonic & @op_str
1079  raise CsError(CS_ERR_DIET)
1080 
1081  all_insn = ctypes.POINTER(_cs_insn)()
1082  size = len(code)
1083  # Pass a bytearray by reference
1084  if isinstance(code, bytearray):
1085  code = ctypes.byref(ctypes.c_char.from_buffer(code))
1086  res = _cs.cs_disasm(self.csh, code, size, offset, count, ctypes.byref(all_insn))
1087  if res > 0:
1088  try:
1089  for i in range(res):
1090  insn = all_insn[i]
1091  yield (insn.address, insn.size, insn.mnemonic.decode('ascii'), insn.op_str.decode('ascii'))
1092  finally:
1093  _cs.cs_free(all_insn, res)
1094  else:
1095  status = _cs.cs_errno(self.csh)
1096  if status != CS_ERR_OK:
1097  raise CsError(status)
1098  return
1099  yield
1100 
1101 
1102 # print out debugging info

References capstone.Cs._diet, capstone._dummy_cs.csh, capstone.Cs.csh, MCInst.csh, len, and capstone.range.

◆ errno()

def capstone.Cs.errno (   self)

Definition at line 1020 of file __init__.py.

1020  def errno(self):
1021  return _cs.cs_errno(self.csh)
1022 

References capstone._dummy_cs.csh, capstone.Cs.csh, and MCInst.csh.

Referenced by capstone.CsError.__str__().

◆ group_name()

def capstone.Cs.group_name (   self,
  group_id,
  default = None 
)

Definition at line 1040 of file __init__.py.

1040  def group_name(self, group_id, default=None):
1041  if self._diet:
1042  # Diet engine cannot provide group name
1043  raise CsError(CS_ERR_DIET)
1044 
1045  return _ascii_name_or_default(_cs.cs_group_name(self.csh, group_id), default)
1046 
def _ascii_name_or_default(name, default)
Definition: __init__.py:525

References capstone._ascii_name_or_default(), capstone.Cs._diet, capstone._dummy_cs.csh, capstone.Cs.csh, and MCInst.csh.

◆ imm_unsigned() [1/2]

def capstone.Cs.imm_unsigned (   self)

Definition at line 987 of file __init__.py.

987  def imm_unsigned(self):
988  return self._imm_unsigned
989 
990 

References capstone.Cs._imm_unsigned.

Referenced by capstone.Cs.imm_unsigned().

◆ imm_unsigned() [2/2]

def capstone.Cs.imm_unsigned (   self,
  opt 
)

Definition at line 993 of file __init__.py.

993  def imm_unsigned(self, opt): # opt is boolean type, so must be either 'True' or 'False'
994  if opt == False:
995  status = _cs.cs_option(self.csh, CS_OPT_UNSIGNED, CS_OPT_OFF)
996  else:
997  status = _cs.cs_option(self.csh, CS_OPT_UNSIGNED, CS_OPT_ON)
998  if status != CS_ERR_OK:
999  raise CsError(status)
1000  # save detail
1001  self._imm_unsigned = opt
1002 
1003 

References capstone.Cs._imm_unsigned, capstone._dummy_cs.csh, capstone.Cs.csh, MCInst.csh, and capstone.Cs.imm_unsigned().

◆ insn_name()

def capstone.Cs.insn_name (   self,
  insn_id,
  default = None 
)

Definition at line 1032 of file __init__.py.

1032  def insn_name(self, insn_id, default=None):
1033  if self._diet:
1034  # Diet engine cannot provide instruction name
1035  raise CsError(CS_ERR_DIET)
1036 
1037  return _ascii_name_or_default(_cs.cs_insn_name(self.csh, insn_id), default)
1038 

References capstone._ascii_name_or_default(), capstone.Cs._diet, capstone._dummy_cs.csh, capstone.Cs.csh, and MCInst.csh.

◆ mnemonic_setup()

def capstone.Cs.mnemonic_setup (   self,
  id,
  mnem 
)

Definition at line 948 of file __init__.py.

948  def mnemonic_setup(self, id, mnem):
949  _mnem_opt = _cs_opt_mnem()
950  _mnem_opt.id = id
951  if mnem:
952  _mnem_opt.mnemonic = mnem.encode()
953  else:
954  _mnem_opt.mnemonic = mnem
955  status = _cs.cs_option(self.csh, CS_OPT_MNEMONIC, ctypes.cast(ctypes.byref(_mnem_opt), ctypes.c_void_p))
956  if status != CS_ERR_OK:
957  raise CsError(status)
958 
959 

References capstone._dummy_cs.csh, capstone.Cs.csh, and MCInst.csh.

◆ mode() [1/2]

def capstone.Cs.mode (   self)

Definition at line 1006 of file __init__.py.

1006  def mode(self):
1007  return self._mode
1008 
1009 
const char int mode
Definition: ioapi.h:137

References rz_debug_t._mode, and capstone.Cs._mode.

Referenced by capstone.Cs.mode(), and test_group_name.GroupTest.run().

◆ mode() [2/2]

def capstone.Cs.mode (   self,
  opt 
)

Definition at line 1012 of file __init__.py.

1012  def mode(self, opt): # opt is new disasm mode, of int type
1013  status = _cs.cs_option(self.csh, CS_OPT_MODE, opt)
1014  if status != CS_ERR_OK:
1015  raise CsError(status)
1016  # save mode
1017  self._mode = opt
1018 

References rz_debug_t._mode, capstone.Cs._mode, capstone._dummy_cs.csh, capstone.Cs.csh, MCInst.csh, and capstone.Cs.mode().

Referenced by test_group_name.GroupTest.run().

◆ reg_name()

def capstone.Cs.reg_name (   self,
  reg_id,
  default = None 
)

Definition at line 1024 of file __init__.py.

1024  def reg_name(self, reg_id, default=None):
1025  if self._diet:
1026  # Diet engine cannot provide register name
1027  raise CsError(CS_ERR_DIET)
1028 
1029  return _ascii_name_or_default(_cs.cs_reg_name(self.csh, reg_id), default)
1030 

References capstone._ascii_name_or_default(), capstone.Cs._diet, capstone._dummy_cs.csh, capstone.Cs.csh, and MCInst.csh.

◆ skipdata() [1/2]

def capstone.Cs.skipdata (   self)

Definition at line 887 of file __init__.py.

887  def skipdata(self):
888  return self._skipdata
889 
890 

References capstone.Cs._skipdata.

Referenced by capstone.Cs.skipdata().

◆ skipdata() [2/2]

def capstone.Cs.skipdata (   self,
  opt 
)

Definition at line 893 of file __init__.py.

893  def skipdata(self, opt):
894  if opt == False:
895  status = _cs.cs_option(self.csh, CS_OPT_SKIPDATA, CS_OPT_OFF)
896  else:
897  status = _cs.cs_option(self.csh, CS_OPT_SKIPDATA, CS_OPT_ON)
898  if status != CS_ERR_OK:
899  raise CsError(status)
900 
901  # save this option
902  self._skipdata = opt
903 
904 

References capstone.Cs._skipdata, capstone._dummy_cs.csh, capstone.Cs.csh, MCInst.csh, and capstone.Cs.skipdata().

◆ skipdata_callback() [1/2]

def capstone.Cs.skipdata_callback (   self)

Definition at line 935 of file __init__.py.

935  def skipdata_callback(self):
936  return self._skipdata_cb
937 
938 

References capstone.Cs._skipdata_cb.

Referenced by capstone.Cs.skipdata_callback().

◆ skipdata_callback() [2/2]

def capstone.Cs.skipdata_callback (   self,
  val 
)

Definition at line 940 of file __init__.py.

940  def skipdata_callback(self, val):
941  if not isinstance(val, tuple):
942  val = (val, None)
943  func, data = val
944  self.skipdata_setup = (self._skipdata_mnem, func, data)
945 
946 

References capstone.Cs._skipdata_mnem, capstone.Cs.skipdata_callback(), capstone.Cs.skipdata_setup, and cs_struct.skipdata_setup.

◆ skipdata_mnem() [1/2]

def capstone.Cs.skipdata_mnem (   self)

Definition at line 925 of file __init__.py.

925  def skipdata_mnem(self):
926  return self._skipdata_mnem
927 
928 

References capstone.Cs._skipdata_mnem.

Referenced by capstone.Cs.skipdata_mnem().

◆ skipdata_mnem() [2/2]

def capstone.Cs.skipdata_mnem (   self,
  mnem 
)

Definition at line 930 of file __init__.py.

930  def skipdata_mnem(self, mnem):
931  self.skipdata_setup = (mnem,) + self._skipdata_cb
932 
933 

References capstone.Cs.skipdata_mnem().

◆ skipdata_setup() [1/2]

def capstone.Cs.skipdata_setup (   self)

Definition at line 906 of file __init__.py.

906  def skipdata_setup(self):
907  return (self._skipdata_mnem,) + self._skipdata_cb
908 
909 

References capstone.Cs._skipdata_cb, capstone.Cs._skipdata_mnem, and capstone.Cs.skipdata_setup.

Referenced by capstone.Cs.skipdata_callback().

◆ skipdata_setup() [2/2]

def capstone.Cs.skipdata_setup (   self,
  opt 
)

Definition at line 911 of file __init__.py.

911  def skipdata_setup(self, opt):
912  _mnem, _cb, _ud = opt
913  self._skipdata_opt.mnemonic = _mnem.encode()
914  self._skipdata_opt.callback = CS_SKIPDATA_CALLBACK(_cb or 0)
915  self._skipdata_opt.user_data = ctypes.cast(_ud, ctypes.c_void_p)
916  status = _cs.cs_option(self.csh, CS_OPT_SKIPDATA_SETUP, ctypes.cast(ctypes.byref(self._skipdata_opt), ctypes.c_void_p))
917  if status != CS_ERR_OK:
918  raise CsError(status)
919 
920  self._skipdata_mnem = _mnem
921  self._skipdata_cb = (_cb, _ud)
922 
923 
CS_SKIPDATA_CALLBACK
Definition: __init__.py:371

References capstone.Cs._skipdata_cb, capstone.Cs._skipdata_mnem, capstone.Cs._skipdata_opt, capstone.CS_SKIPDATA_CALLBACK, capstone._dummy_cs.csh, capstone.Cs.csh, MCInst.csh, and capstone.Cs.skipdata_setup.

Referenced by capstone.Cs.skipdata_callback().

◆ support()

def capstone.Cs.support (   self,
  query 
)

Definition at line 962 of file __init__.py.

962  def support(self, query):
963  return cs_support(query)
964 
965 

References capstone.cs_support().

◆ syntax() [1/2]

def capstone.Cs.syntax (   self)

Definition at line 871 of file __init__.py.

871  def syntax(self):
872  return self._syntax
873 
874 

References capstone.Cs._syntax.

Referenced by capstone.Cs.syntax().

◆ syntax() [2/2]

def capstone.Cs.syntax (   self,
  style 
)

Definition at line 877 of file __init__.py.

877  def syntax(self, style):
878  status = _cs.cs_option(self.csh, CS_OPT_SYNTAX, style)
879  if status != CS_ERR_OK:
880  raise CsError(status)
881  # save syntax
882  self._syntax = style
883 
884 

References capstone.Cs._syntax, capstone._dummy_cs.csh, capstone.Cs.csh, MCInst.csh, and capstone.Cs.syntax().

◆ x86_reduce()

def capstone.Cs.x86_reduce (   self)

Definition at line 865 of file __init__.py.

865  def x86_reduce(self):
866  return self._x86reduce
867 
868 

References capstone.Cs._x86reduce.

Member Data Documentation

◆ _detail

capstone.Cs._detail
private

Definition at line 827 of file __init__.py.

Referenced by capstone.Cs.detail().

◆ _diet

capstone.Cs._diet
private

◆ _imm_unsigned

capstone.Cs._imm_unsigned
private

Definition at line 828 of file __init__.py.

Referenced by capstone.Cs.imm_unsigned().

◆ _mode

capstone.Cs._mode
private

Definition at line 807 of file __init__.py.

Referenced by capstone.Cs.mode().

◆ _skipdata

capstone.Cs._skipdata
private

Definition at line 838 of file __init__.py.

Referenced by capstone.Cs.skipdata().

◆ _skipdata_cb

capstone.Cs._skipdata_cb
private

Definition at line 834 of file __init__.py.

Referenced by capstone.Cs.skipdata_callback(), and capstone.Cs.skipdata_setup().

◆ _skipdata_mnem

capstone.Cs._skipdata_mnem
private

◆ _skipdata_opt

capstone.Cs._skipdata_opt
private

Definition at line 837 of file __init__.py.

Referenced by capstone.Cs.skipdata_setup().

◆ _syntax

capstone.Cs._syntax
private

Definition at line 823 of file __init__.py.

Referenced by capstone.Cs.syntax().

◆ _x86reduce

capstone.Cs._x86reduce
private

Definition at line 830 of file __init__.py.

Referenced by capstone.Cs.x86_reduce().

◆ csh

◆ disasm

capstone.Cs.disasm

Definition at line 817 of file __init__.py.

Referenced by capstone.Cs.disasm().

◆ skipdata_setup

capstone.Cs.skipdata_setup

Definition at line 931 of file __init__.py.

Referenced by capstone.Cs.skipdata_callback(), and capstone.Cs.skipdata_setup().


The documentation for this class was generated from the following file: