Debian Codegraph @

bindings/python/tests/test_skipdata.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 # Capstone Python bindings, by Nguyen Anh Quynnh <[email protected]>
4
5 from __future__ import print_function
6 from capstone import *
7 import binascii
8
9
10 X86_CODE32 = b"\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x00\x91\x92"
11 RANDOM_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"
12
13 all_tests = (
14 (CS_ARCH_X86, CS_MODE_32, X86_CODE32, "X86 32 (Intel syntax)", None),
15 (CS_ARCH_ARM, CS_MODE_ARM, RANDOM_CODE, "Arm", None),
16 )
17
18
19 # Sample callback for SKIPDATA option
20 def testcb(buffer, size, offset, userdata):
21 # always skip 2 bytes of data
22 return 2
23
24
25 # ## Test class Cs
26 def test_class():
27 for (arch, mode, code, comment, syntax) in all_tests:
28 print('*' * 16)
29 print("Platform: %s" %comment)
30 print("Code: %s" % code.hex(' '))
31 print("Disasm:")
32
33 try:
34 md = Cs(arch, mode)
35
36 if syntax is not None:
37 md.syntax = syntax
38
39 md.skipdata = True
40
41 # Default "data" instruction's name is ".byte". To rename it to "db", just use
42 # the code below.
43 md.skipdata_setup = ("db", None, None)
44
45 # NOTE: This example ignores SKIPDATA's callback (first None) & user_data (second None)
46 # Can also use dedicated setter
47 #md.skipdata_mnem = 'db'
48
49 # To customize the SKIPDATA callback, use the line below.
50 #md.skipdata_setup = (".db", testcb, None)
51
52 # Or use dedicated setter with custom parameter
53 #md.skipdata_callback = (testcb, 42)
54
55 # Or provide just a function
56 #md.skipdata_callback = testcb
57 # Note that reading this property will always return a tuple
58 #assert md.skipdata_callback == (testcb, None)
59
60 for insn in md.disasm(code, 0x1000):
61 #bytes = binascii.hexlify(insn.bytes)
62 #print("0x%x:\t%s\t%s\t// hex-code: %s" %(insn.address, insn.mnemonic, insn.op_str, bytes))
63 print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))
64
65 print("0x%x:" % (insn.address + insn.size))
66 print
67 except CsError as e:
68 print("ERROR: %s" % e)
69
70
71 if __name__ == '__main__':
72 test_class()