blob: c871c07dbdfc7de1683f455fee2137e765641262 [file] [log] [blame]
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +08001#!/usr/bin/env python
2
3# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
4
Nguyen Anh Quynh749046b2014-04-12 01:15:10 +08005from __future__ import print_function
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +08006from capstone import *
7import binascii
Nguyen Anh Quynh03d1e1f2015-04-27 11:51:48 +08008from xprint import to_hex
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +08009
Nguyen Anh Quynh749046b2014-04-12 01:15:10 +080010
11X86_CODE32 = b"\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x00\x91\x92"
12RANDOM_CODE = b"\xed\x00\x00\x00\x00\x1a\x5a\x0f\x1f\xff\xc2\x09\x80\x00\x00\x00\x07\xf7\xeb\x2a\xff\xff\x7f\x57\xe3\x01\xff\xff\x7f\x57\xeb\x00\xf0\x00\x00\x24\xb2\x4f\x00\x78"
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080013
14all_tests = (
Niels Boehm32b63462016-06-13 12:25:24 +020015 (CS_ARCH_X86, CS_MODE_32, X86_CODE32, "X86 32 (Intel syntax)", None),
16 (CS_ARCH_ARM, CS_MODE_ARM, RANDOM_CODE, "Arm", None),
Nguyen Anh Quynh749046b2014-04-12 01:15:10 +080017)
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080018
19
Nguyen Anh Quynh301d7402014-04-10 22:34:27 +080020# Sample callback for SKIPDATA option
Nguyen Anh Quynhcbc7dd92014-07-10 15:57:42 +080021def testcb(buffer, size, offset, userdata):
Nguyen Anh Quynh301d7402014-04-10 22:34:27 +080022 # always skip 2 bytes of data
23 return 2
24
25
Nguyen Anh Quynh749046b2014-04-12 01:15:10 +080026# ## Test class Cs
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080027def test_class():
28 for (arch, mode, code, comment, syntax) in all_tests:
29 print('*' * 16)
30 print("Platform: %s" %comment)
31 print("Code: %s" % to_hex(code))
32 print("Disasm:")
33
34 try:
35 md = Cs(arch, mode)
36
Niels Boehm32b63462016-06-13 12:25:24 +020037 if syntax is not None:
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080038 md.syntax = syntax
39
40 md.skipdata = True
Nguyen Anh Quynh301d7402014-04-10 22:34:27 +080041
Семён Марьясин6c548142018-12-31 10:42:44 +030042 # Default "data" instruction's name is ".byte". To rename it to "db", just use
Nguyen Anh Quynhb64d1cf2014-04-10 23:05:28 +080043 # the code below.
Семён Марьясин6c548142018-12-31 10:42:44 +030044 md.skipdata_setup = ("db", None, None)
Nguyen Anh Quynhb64d1cf2014-04-10 23:05:28 +080045 # NOTE: This example ignores SKIPDATA's callback (first None) & user_data (second None)
Семён Марьясин6c548142018-12-31 10:42:44 +030046 # Can also use dedicated setter
47 md.skipdata_mnem = 'db'
Nguyen Anh Quynhb64d1cf2014-04-10 23:05:28 +080048
Семён Марьясин6c548142018-12-31 10:42:44 +030049 # To customize the SKIPDATA callback, use the line below.
50 md.skipdata_setup = (".db", testcb, None)
51 # Or use dedicated setter with custom parameter
52 md.skipdata_cb = (testcb, 42)
53 # Or provide just a function
54 md.skipdata_cb = testcb
55 # Note that reading this property will always return a tuple
Nguyen Anh Quynha593f8d2018-12-31 15:51:50 +080056 assert md.skipdata_cb == (testcb, None)
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080057
58 for insn in md.disasm(code, 0x1000):
59 #bytes = binascii.hexlify(insn.bytes)
60 #print("0x%x:\t%s\t%s\t// hex-code: %s" %(insn.address, insn.mnemonic, insn.op_str, bytes))
Nguyen Anh Quynh749046b2014-04-12 01:15:10 +080061 print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080062
63 print("0x%x:" % (insn.address + insn.size))
64 print
65 except CsError as e:
Nguyen Anh Quynh749046b2014-04-12 01:15:10 +080066 print("ERROR: %s" % e)
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080067
68
Nguyen Anh Quynh749046b2014-04-12 01:15:10 +080069if __name__ == '__main__':
70 test_class()