From d54039e713d50b03644873599abc0db2c3583c97 Mon Sep 17 00:00:00 2001 From: Christopher Denton Date: Wed, 27 Mar 2019 22:38:18 +0000 Subject: [PATCH] Update __init__.py Pass bytearrays by reference instead of copying to bytes. --- bindings/python/capstone/__init__.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/bindings/python/capstone/__init__.py b/bindings/python/capstone/__init__.py index c1fa0e58dc..d5ea918b80 100644 --- a/bindings/python/capstone/__init__.py +++ b/bindings/python/capstone/__init__.py @@ -1053,11 +1053,11 @@ def disasm(self, code, offset, count=0): print(code) code = code.encode() print(code)''' - # Hi, Hacker! Unicorn's memory accessors give you back bytearrays, but they - # cause TypeErrors when you hand them into Capstone. + # Pass a bytearray by reference + size = len(code) if isinstance(code, bytearray): - code = bytes(code) - res = _cs.cs_disasm(self.csh, code, len(code), offset, count, ctypes.byref(all_insn)) + code = ctypes.byref(ctypes.c_char.from_buffer(code)) + res = _cs.cs_disasm(self.csh, code, size, offset, count, ctypes.byref(all_insn)) if res > 0: try: for i in range(res): @@ -1081,11 +1081,11 @@ def disasm_lite(self, code, offset, count=0): raise CsError(CS_ERR_DIET) all_insn = ctypes.POINTER(_cs_insn)() - # Hi, Hacker! Unicorn's memory accessors give you back bytearrays, but they - # cause TypeErrors when you hand them into Capstone. + size = len(code) + # Pass a bytearray by reference if isinstance(code, bytearray): - code = bytes(code) - res = _cs.cs_disasm(self.csh, code, len(code), offset, count, ctypes.byref(all_insn)) + code = ctypes.byref(ctypes.c_char.from_buffer(code)) + res = _cs.cs_disasm(self.csh, code, size, offset, count, ctypes.byref(all_insn)) if res > 0: try: for i in range(res):