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