blob: bcc3b39f686bb8da15b2d8818caba0496f63653f [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 Quynh10983292014-05-17 09:51:15 +08008from xprint import to_x, to_hex, to_x_32
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 = (
15 (CS_ARCH_X86, CS_MODE_32, X86_CODE32, "X86 32 (Intel syntax)", 0),
Nguyen Anh Quynh1eccbab2014-04-10 17:48:19 +080016 (CS_ARCH_ARM, CS_MODE_ARM, RANDOM_CODE, "Arm", 0),
Nguyen Anh Quynh749046b2014-04-12 01:15:10 +080017)
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080018
19
Nguyen Anh Quynh749046b2014-04-12 01:15:10 +080020# ## Test cs_disasm_quick()
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080021def test_cs_disasm_quick():
22 for (arch, mode, code, comment, syntax) in all_tests:
23 print('*' * 40)
Nguyen Anh Quynh749046b2014-04-12 01:15:10 +080024 print("Platform: %s" % comment)
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080025 print("Disasm:"),
Nguyen Anh Quynh749046b2014-04-12 01:15:10 +080026 print(to_hex(code))
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080027 for insn in cs_disasm_quick(arch, mode, code, 0x1000):
Nguyen Anh Quynh749046b2014-04-12 01:15:10 +080028 print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080029 print
30
31
Nguyen Anh Quynh301d7402014-04-10 22:34:27 +080032# Sample callback for SKIPDATA option
Nguyen Anh Quynhcbc7dd92014-07-10 15:57:42 +080033def testcb(buffer, size, offset, userdata):
Nguyen Anh Quynh301d7402014-04-10 22:34:27 +080034 # always skip 2 bytes of data
35 return 2
36
37
Nguyen Anh Quynh749046b2014-04-12 01:15:10 +080038# ## Test class Cs
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080039def test_class():
40 for (arch, mode, code, comment, syntax) in all_tests:
41 print('*' * 16)
42 print("Platform: %s" %comment)
43 print("Code: %s" % to_hex(code))
44 print("Disasm:")
45
46 try:
47 md = Cs(arch, mode)
48
49 if syntax != 0:
50 md.syntax = syntax
51
52 md.skipdata = True
Nguyen Anh Quynh301d7402014-04-10 22:34:27 +080053
Nguyen Anh Quynhb64d1cf2014-04-10 23:05:28 +080054 # Default "data" instruction's name is ".byte". To rename it to "db", just uncomment
55 # the code below.
56 # md.skipdata_setup = ("db", None, None)
57 # NOTE: This example ignores SKIPDATA's callback (first None) & user_data (second None)
58
59 # To customize the SKIPDATA callback, uncomment the line below.
Nguyen Anh Quynh301d7402014-04-10 22:34:27 +080060 # md.skipdata_setup = (".db", CS_SKIPDATA_CALLBACK(testcb), None)
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080061
62 for insn in md.disasm(code, 0x1000):
63 #bytes = binascii.hexlify(insn.bytes)
64 #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 +080065 print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080066
67 print("0x%x:" % (insn.address + insn.size))
68 print
69 except CsError as e:
Nguyen Anh Quynh749046b2014-04-12 01:15:10 +080070 print("ERROR: %s" % e)
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080071
72
Nguyen Anh Quynh749046b2014-04-12 01:15:10 +080073if __name__ == '__main__':
74 test_class()