blob: 3e6c2b1e4c2da2b9b99ffab8d8d15e2ad501da81 [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
5from capstone import *
6import binascii
7
8X86_CODE32 = "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x00\x91\x92"
9RANDOM_CODE = "\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"
10
11all_tests = (
12 (CS_ARCH_X86, CS_MODE_32, X86_CODE32, "X86 32 (Intel syntax)", 0),
Nguyen Anh Quynh1eccbab2014-04-10 17:48:19 +080013 (CS_ARCH_ARM, CS_MODE_ARM, RANDOM_CODE, "Arm", 0),
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080014 )
15
16
17def to_hex(s):
18 return " ".join("0x" + "{0:x}".format(ord(c)).zfill(2) for c in s) # <-- Python 3 is OK
19
20
21### Test cs_disasm_quick()
22def test_cs_disasm_quick():
23 for (arch, mode, code, comment, syntax) in all_tests:
24 print('*' * 40)
25 print("Platform: %s" %comment)
26 print("Disasm:"),
27 print to_hex(code)
28 for insn in cs_disasm_quick(arch, mode, code, 0x1000):
29 print("0x%x:\t%s\t%s" %(insn.address, insn.mnemonic, insn.op_str))
30 print
31
32
Nguyen Anh Quynh301d7402014-04-10 22:34:27 +080033# Sample callback for SKIPDATA option
34def testcb(offset, userdata):
35 # always skip 2 bytes of data
36 return 2
37
38
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080039### Test class Cs
40def test_class():
41 for (arch, mode, code, comment, syntax) in all_tests:
42 print('*' * 16)
43 print("Platform: %s" %comment)
44 print("Code: %s" % to_hex(code))
45 print("Disasm:")
46
47 try:
48 md = Cs(arch, mode)
49
50 if syntax != 0:
51 md.syntax = syntax
52
53 md.skipdata = True
Nguyen Anh Quynh301d7402014-04-10 22:34:27 +080054
Nguyen Anh Quynhb64d1cf2014-04-10 23:05:28 +080055 # Default "data" instruction's name is ".byte". To rename it to "db", just uncomment
56 # the code below.
57 # md.skipdata_setup = ("db", None, None)
58 # NOTE: This example ignores SKIPDATA's callback (first None) & user_data (second None)
59
60 # To customize the SKIPDATA callback, uncomment the line below.
Nguyen Anh Quynh301d7402014-04-10 22:34:27 +080061 # md.skipdata_setup = (".db", CS_SKIPDATA_CALLBACK(testcb), None)
Nguyen Anh Quynhf0c577f2014-04-10 16:09:15 +080062
63 for insn in md.disasm(code, 0x1000):
64 #bytes = binascii.hexlify(insn.bytes)
65 #print("0x%x:\t%s\t%s\t// hex-code: %s" %(insn.address, insn.mnemonic, insn.op_str, bytes))
66 print("0x%x:\t%s\t%s" %(insn.address, insn.mnemonic, insn.op_str))
67
68 print("0x%x:" % (insn.address + insn.size))
69 print
70 except CsError as e:
71 print("ERROR: %s" %e)
72
73
74test_class()