From e07c1dd116e67549dc24120dbb52f023bab47d0c Mon Sep 17 00:00:00 2001 From: ChrisDenton Date: Wed, 3 Apr 2019 04:40:36 +0100 Subject: [PATCH] Update __init__.py (#1453) 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 ce814199..d6f66fea 100644 --- a/bindings/python/capstone/__init__.py +++ b/bindings/python/capstone/__init__.py @@ -1073,11 +1073,11 @@ class Cs(object): 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): @@ -1101,11 +1101,11 @@ class Cs(object): 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):