blob: 7f7aed52eb5d6a84fa2f72f16fb28791b4313225 [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),
13 (CS_ARCH_ARM, 0, RANDOM_CODE, "Arm", 0),
14 )
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
33### Test class Cs
34def test_class():
35 for (arch, mode, code, comment, syntax) in all_tests:
36 print('*' * 16)
37 print("Platform: %s" %comment)
38 print("Code: %s" % to_hex(code))
39 print("Disasm:")
40
41 try:
42 md = Cs(arch, mode)
43
44 if syntax != 0:
45 md.syntax = syntax
46
47 md.skipdata = True
48 # To rename "data" instruction's mnemonic to "db", uncomment the line below
49 # This example ignores SKIPDATA's callback (first None) & user_data (second None)
50 md.skipdata_opt = ("db", None, None)
51
52 for insn in md.disasm(code, 0x1000):
53 #bytes = binascii.hexlify(insn.bytes)
54 #print("0x%x:\t%s\t%s\t// hex-code: %s" %(insn.address, insn.mnemonic, insn.op_str, bytes))
55 print("0x%x:\t%s\t%s" %(insn.address, insn.mnemonic, insn.op_str))
56
57 print("0x%x:" % (insn.address + insn.size))
58 print
59 except CsError as e:
60 print("ERROR: %s" %e)
61
62
63test_class()