From ce8d74772b6a8b92b61a84a23489f187d481177c Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Sat, 1 Jun 2024 14:16:38 -0700 Subject: [PATCH 1/5] Remove python2 leftovers --- bindings/python/Makefile | 27 ------------------------- bindings/python/capstone/__init__.py | 20 ++++-------------- bindings/python/setup.py | 10 ++------- bindings/python/test_aarch64.py | 1 - bindings/python/test_alpha.py | 1 - bindings/python/test_arm.py | 1 - bindings/python/test_basic.py | 10 +-------- bindings/python/test_bpf.py | 1 - bindings/python/test_customized_mnem.py | 1 - bindings/python/test_detail.py | 1 - bindings/python/test_evm.py | 4 ---- bindings/python/test_hppa.py | 1 - bindings/python/test_iter.py | 1 - bindings/python/test_lite.py | 1 - bindings/python/test_m680x.py | 8 +------- bindings/python/test_m68k.py | 1 - bindings/python/test_mips.py | 1 - bindings/python/test_mos65xx.py | 1 - bindings/python/test_ppc.py | 1 - bindings/python/test_riscv.py | 1 - bindings/python/test_sh.py | 1 - bindings/python/test_skipdata.py | 2 -- bindings/python/test_sparc.py | 1 - bindings/python/test_systemz.py | 1 - bindings/python/test_tms320c64x.py | 1 - bindings/python/test_tricore.py | 1 - bindings/python/test_wasm.py | 1 - bindings/python/test_x86.py | 3 +-- bindings/python/test_xcore.py | 1 - bindings/python/xprint.py | 21 ++++--------------- 30 files changed, 13 insertions(+), 113 deletions(-) diff --git a/bindings/python/Makefile b/bindings/python/Makefile index 10b9af3134..ea271c0533 100644 --- a/bindings/python/Makefile +++ b/bindings/python/Makefile @@ -1,4 +1,3 @@ -PYTHON2 ?= python2 PYTHON3 ?= python3 .PHONY: gen_const install install3 install_cython sdist sdist3 bdist bdist3 clean check @@ -7,14 +6,6 @@ gen_const: cd .. && $(PYTHON3) const_generator.py python install: - rm -rf src/ - if test -n "${DESTDIR}"; then \ - $(PYTHON2) setup.py build install --root="${DESTDIR}"; \ - else \ - $(PYTHON2) setup.py build install; \ - fi - -install3: rm -rf src/ if test -n "${DESTDIR}"; then \ $(PYTHON3) setup.py build install --root="${DESTDIR}"; \ @@ -24,14 +15,6 @@ install3: # NOTE: Newer cython can be installed by: sudo pip install --upgrade cython install_cython: - rm -rf src/ - if test -n "${DESTDIR}"; then \ - $(PYTHON2) setup_cython.py build install --root="${DESTDIR}"; \ - else \ - $(PYTHON2) setup_cython.py build install; \ - fi - -install3_cython: rm -rf src/ if test -n "${DESTDIR}"; then \ $(PYTHON3) setup_cython.py build install --root="${DESTDIR}"; \ @@ -41,21 +24,11 @@ install3_cython: # build & upload PyPi package with source code of the core sdist: - rm -rf src/ dist/ - $(PYTHON2) setup.py sdist register upload - -# build & upload PyPi package with source code of the core -sdist3: rm -rf src/ dist/ $(PYTHON3) setup.py sdist register upload # build & upload PyPi package with prebuilt core bdist: - rm -rf src/ dist/ - $(PYTHON2) setup.py bdist_wheel register upload - -# build & upload PyPi package with prebuilt core -bdist3: rm -rf src/ dist/ $(PYTHON3) setup.py bdist_wheel register upload diff --git a/bindings/python/capstone/__init__.py b/bindings/python/capstone/__init__.py index 154e6f1105..288b705688 100755 --- a/bindings/python/capstone/__init__.py +++ b/bindings/python/capstone/__init__.py @@ -1,9 +1,6 @@ # Capstone Python bindings, by Nguyen Anh Quynnh -import os, sys -from platform import system -_python2 = sys.version_info[0] < 3 -if _python2: - range = xrange +import os +import sys __all__ = [ 'Cs', @@ -549,13 +546,8 @@ class CsError(Exception): def __init__(self, errno): self.errno = errno - if _python2: - def __str__(self): - return _cs.cs_strerror(self.errno) - - else: - def __str__(self): - return _cs.cs_strerror(self.errno).decode() + def __str__(self): + return _cs.cs_strerror(self.errno).decode() # return the core's version @@ -1215,10 +1207,6 @@ def group_name(self, group_id, default=None): # Disassemble binary & return disassembled instructions in CsInsn objects def disasm(self, code, offset, count=0): all_insn = ctypes.POINTER(_cs_insn)() - '''if not _python2: - print(code) - code = code.encode() - print(code)''' # Pass a bytearray by reference size = len(code) view = memoryview(code) diff --git a/bindings/python/setup.py b/bindings/python/setup.py index 55314760fc..1f69803f85 100755 --- a/bindings/python/setup.py +++ b/bindings/python/setup.py @@ -13,10 +13,6 @@ from distutils.command.sdist import sdist from setuptools.command.bdist_egg import bdist_egg -PYTHON2 = sys.version_info[0] == 2 -if PYTHON2: - import io - SYSTEM = sys.platform # adapted from commit e504b81 of Nguyen Tan Cong @@ -220,13 +216,11 @@ def run(self): author_email='aquynh@gmail.com', description='Capstone disassembly engine', url='https://www.capstone-engine.org', - long_description=io.open('README.txt', encoding="utf8").read() if PYTHON2 else open('README.txt', encoding="utf8").read(), + long_description=open('README.txt', encoding="utf8").read(), long_description_content_type='text/markdown', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', + python_requires='>=3.6', classifiers=[ 'License :: OSI Approved :: BSD License', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', ], cmdclass=cmdclass, diff --git a/bindings/python/test_aarch64.py b/bindings/python/test_aarch64.py index 00d32009fe..12f8ee083a 100755 --- a/bindings/python/test_aarch64.py +++ b/bindings/python/test_aarch64.py @@ -2,7 +2,6 @@ # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * from capstone.aarch64 import * from xprint import to_hex, to_x, to_x_32 diff --git a/bindings/python/test_alpha.py b/bindings/python/test_alpha.py index 404371ea31..056288ae2d 100755 --- a/bindings/python/test_alpha.py +++ b/bindings/python/test_alpha.py @@ -2,7 +2,6 @@ # Capstone Python bindings, by Dmitry Sibirtsev -from __future__ import print_function from capstone import * from capstone.alpha import * from xprint import to_x, to_hex diff --git a/bindings/python/test_arm.py b/bindings/python/test_arm.py index f220884cb0..3056744628 100755 --- a/bindings/python/test_arm.py +++ b/bindings/python/test_arm.py @@ -2,7 +2,6 @@ # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * from capstone.arm import * from xprint import to_hex, to_x_32 diff --git a/bindings/python/test_basic.py b/bindings/python/test_basic.py index 9e893ba982..a114b1f0c5 100755 --- a/bindings/python/test_basic.py +++ b/bindings/python/test_basic.py @@ -1,15 +1,10 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * -import binascii -import sys from xprint import to_hex -_python3 = sys.version_info.major == 3 - X86_CODE16 = b"\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00" X86_CODE32 = b"\xba\xcd\xab\x00\x00\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00" @@ -102,10 +97,7 @@ def test_cs_disasm_quick(): def test_different_data_formats(): - if _python3: - data = bytes.fromhex('4831C948F7E1043B48BB0A2F62696E2F2F736852530A545F5257545E0F05') - else: - data = bytes(bytearray.fromhex('4831C948F7E1043B48BB0A2F62696E2F2F736852530A545F5257545E0F05')) + data = bytes.fromhex('4831C948F7E1043B48BB0A2F62696E2F2F736852530A545F5257545E0F05') mnemonics = ['xor', 'mul', 'add', 'movabs', 'push', 'pop', 'push', 'push', 'push', 'pop', 'syscall'] disassembler = Cs(CS_ARCH_X86, CS_MODE_64) for name, code in ( diff --git a/bindings/python/test_bpf.py b/bindings/python/test_bpf.py index 721a2b2ecb..f7f5538a07 100755 --- a/bindings/python/test_bpf.py +++ b/bindings/python/test_bpf.py @@ -3,7 +3,6 @@ # Capstone Python bindings # BPF tests by david942j , 2019 -from __future__ import print_function from capstone import * from capstone.bpf import * from xprint import to_hex, to_x, to_x_32 diff --git a/bindings/python/test_customized_mnem.py b/bindings/python/test_customized_mnem.py index 2efc0a99bc..157e6d096d 100755 --- a/bindings/python/test_customized_mnem.py +++ b/bindings/python/test_customized_mnem.py @@ -2,7 +2,6 @@ # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * from capstone.x86 import * from xprint import to_hex diff --git a/bindings/python/test_detail.py b/bindings/python/test_detail.py index f57797bc0e..4f966109fd 100755 --- a/bindings/python/test_detail.py +++ b/bindings/python/test_detail.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * from xprint import to_hex diff --git a/bindings/python/test_evm.py b/bindings/python/test_evm.py index 86dffab696..3fd90fdfc5 100755 --- a/bindings/python/test_evm.py +++ b/bindings/python/test_evm.py @@ -2,14 +2,10 @@ # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * -import sys from xprint import to_hex -_python3 = sys.version_info.major == 3 - EVM_CODE = b"\x60\x61\x50" diff --git a/bindings/python/test_hppa.py b/bindings/python/test_hppa.py index 15e0e6017e..dfbe08fa3d 100755 --- a/bindings/python/test_hppa.py +++ b/bindings/python/test_hppa.py @@ -2,7 +2,6 @@ # Capstone Python bindings, by Dmitry Sibirtsev -from __future__ import print_function from capstone import * from capstone.hppa import * from xprint import to_x, to_hex diff --git a/bindings/python/test_iter.py b/bindings/python/test_iter.py index 89b9656248..26ae93bc43 100755 --- a/bindings/python/test_iter.py +++ b/bindings/python/test_iter.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * from xprint import to_hex diff --git a/bindings/python/test_lite.py b/bindings/python/test_lite.py index 44569a038f..524372043f 100755 --- a/bindings/python/test_lite.py +++ b/bindings/python/test_lite.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * from xprint import to_hex diff --git a/bindings/python/test_m680x.py b/bindings/python/test_m680x.py index 07dbfc683b..8512ff0ccb 100755 --- a/bindings/python/test_m680x.py +++ b/bindings/python/test_m680x.py @@ -2,11 +2,8 @@ # Capstone Python bindings, by Wolfgang Schwotzer -from __future__ import print_function -import sys from capstone import * from capstone.m680x import * -_python3 = sys.version_info.major == 3 s_access = ( @@ -40,10 +37,7 @@ # print hex dump from string all upper case def to_hex_uc(string): - if _python3: - return " ".join("0x%02x" % c for c in string) - else: - return " ".join("0x%02x" % ord(c) for c in string) + return " ".join("0x%02x" % c for c in string) # print short hex dump from byte array all upper case def to_hex_short_uc(byte_array): diff --git a/bindings/python/test_m68k.py b/bindings/python/test_m68k.py index 124bf357a3..6efdfaedbd 100755 --- a/bindings/python/test_m68k.py +++ b/bindings/python/test_m68k.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nicolas PLANEL -from __future__ import print_function from capstone import * from capstone.m68k import * from xprint import to_hex diff --git a/bindings/python/test_mips.py b/bindings/python/test_mips.py index 7de53749ad..b6a2502995 100755 --- a/bindings/python/test_mips.py +++ b/bindings/python/test_mips.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * from capstone.mips import * from xprint import to_hex, to_x diff --git a/bindings/python/test_mos65xx.py b/bindings/python/test_mos65xx.py index 51c12b9444..ae5db53c54 100755 --- a/bindings/python/test_mos65xx.py +++ b/bindings/python/test_mos65xx.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Sebastian Macke -from __future__ import print_function from capstone import * from capstone.mos65xx import * from xprint import to_hex, to_x diff --git a/bindings/python/test_ppc.py b/bindings/python/test_ppc.py index 87442c6b77..9f2b493865 100755 --- a/bindings/python/test_ppc.py +++ b/bindings/python/test_ppc.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * from capstone.ppc import * from xprint import to_hex, to_x, to_x_32 diff --git a/bindings/python/test_riscv.py b/bindings/python/test_riscv.py index 892de005f3..7f7acae25e 100755 --- a/bindings/python/test_riscv.py +++ b/bindings/python/test_riscv.py @@ -2,7 +2,6 @@ # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * from capstone.riscv import * from xprint import to_x, to_hex diff --git a/bindings/python/test_sh.py b/bindings/python/test_sh.py index 03dfed9011..e5c2f3514e 100755 --- a/bindings/python/test_sh.py +++ b/bindings/python/test_sh.py @@ -2,7 +2,6 @@ # Capstone Python bindings, by Peace-Maker -from __future__ import print_function from capstone import * from capstone.sh import * from xprint import to_x, to_hex diff --git a/bindings/python/test_skipdata.py b/bindings/python/test_skipdata.py index 98f027bf4f..0fd7d7fca1 100755 --- a/bindings/python/test_skipdata.py +++ b/bindings/python/test_skipdata.py @@ -2,9 +2,7 @@ # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * -import binascii from xprint import to_hex diff --git a/bindings/python/test_sparc.py b/bindings/python/test_sparc.py index 214a16eff6..e1b1344ab3 100755 --- a/bindings/python/test_sparc.py +++ b/bindings/python/test_sparc.py @@ -2,7 +2,6 @@ # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * from capstone.sparc import * from xprint import to_hex, to_x_32 diff --git a/bindings/python/test_systemz.py b/bindings/python/test_systemz.py index 175cda4179..a628494fd8 100755 --- a/bindings/python/test_systemz.py +++ b/bindings/python/test_systemz.py @@ -2,7 +2,6 @@ # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * from capstone.systemz import * from xprint import to_x, to_hex diff --git a/bindings/python/test_tms320c64x.py b/bindings/python/test_tms320c64x.py index c6851d2ddb..1bb64b0c11 100755 --- a/bindings/python/test_tms320c64x.py +++ b/bindings/python/test_tms320c64x.py @@ -2,7 +2,6 @@ # Capstone Python bindings, by Fotis Loukos -from __future__ import print_function from capstone import * from capstone.tms320c64x import * from xprint import to_x, to_hex, to_x_32 diff --git a/bindings/python/test_tricore.py b/bindings/python/test_tricore.py index e49720dfb9..632a026ada 100755 --- a/bindings/python/test_tricore.py +++ b/bindings/python/test_tricore.py @@ -2,7 +2,6 @@ # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * from capstone.tricore import * from xprint import to_hex, to_x diff --git a/bindings/python/test_wasm.py b/bindings/python/test_wasm.py index 9aade84c2d..eea8549827 100755 --- a/bindings/python/test_wasm.py +++ b/bindings/python/test_wasm.py @@ -2,7 +2,6 @@ # Capstone Python bindings, by Peace-Maker -from __future__ import print_function from capstone import * from capstone.wasm import * from xprint import to_hex diff --git a/bindings/python/test_x86.py b/bindings/python/test_x86.py index 9684cdf3da..4bcf4238bd 100755 --- a/bindings/python/test_x86.py +++ b/bindings/python/test_x86.py @@ -1,10 +1,9 @@ #!/usr/bin/env python3 # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * from capstone.x86 import * -from xprint import to_hex, to_x, to_x_32 +from xprint import to_hex, to_x X86_CODE64 = b"\x55\x48\x8b\x05\xb8\x13\x00\x00\xe9\xea\xbe\xad\xde\xff\x25\x23\x01\x00\x00\xe8\xdf\xbe\xad\xde\x74\xff" diff --git a/bindings/python/test_xcore.py b/bindings/python/test_xcore.py index d0531a7aca..628a5a0c8c 100755 --- a/bindings/python/test_xcore.py +++ b/bindings/python/test_xcore.py @@ -2,7 +2,6 @@ # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function from capstone import * from capstone.xcore import * from xprint import to_x, to_hex diff --git a/bindings/python/xprint.py b/bindings/python/xprint.py index 70affaca5c..5e31dcf448 100755 --- a/bindings/python/xprint.py +++ b/bindings/python/xprint.py @@ -1,28 +1,15 @@ #!/usr/bin/env python # Capstone Python bindings, by Nguyen Anh Quynnh -from __future__ import print_function -import sys -_python3 = sys.version_info.major == 3 - def to_hex(s, prefix_0x = True): - if _python3: - if prefix_0x: - return " ".join("0x{0:02x}".format(c) for c in s) # <-- Python 3 is OK - else: - return " ".join("{0:02x}".format(c) for c in s) # <-- Python 3 is OK + if prefix_0x: + return " ".join("0x{0:02x}".format(c) for c in s) # <-- Python 3 is OK else: - if prefix_0x: - return " ".join("0x{0:02x}".format(ord(c)) for c in s) - else: - return " ".join("{0:02x}".format(ord(c)) for c in s) + return " ".join("{0:02x}".format(c) for c in s) # <-- Python 3 is OK def to_hex2(s): - if _python3: - r = "".join("{0:02x}".format(c) for c in s) # <-- Python 3 is OK - else: - r = "".join("{0:02x}".format(ord(c)) for c in s) + r = "".join("{0:02x}".format(c) for c in s) # <-- Python 3 is OK while r[0] == '0': r = r[1:] return r From 22dd5cb541c713a6c9e822c7217e9fdb85e20e81 Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Sat, 1 Jun 2024 14:25:58 -0700 Subject: [PATCH 2/5] Remove python2 references from BUILDING.txt --- bindings/python/BUILDING.txt | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/bindings/python/BUILDING.txt b/bindings/python/BUILDING.txt index e527b153eb..8b84b44927 100644 --- a/bindings/python/BUILDING.txt +++ b/bindings/python/BUILDING.txt @@ -6,11 +6,6 @@ $ sudo make install - To install Capstone for Python 3, run the command below: - (Note: this requires python3 installed in your machine) - - $ sudo make install3 - To control the install destination, set the DESTDIR environment variable. 2. For better Python performance, install cython-based binding with: @@ -19,7 +14,7 @@ Note that this requires Cython installed first. To install Cython, see below. - + 3. To install Cython, you have to ensure that the header files and the static library for Python are installed beforehand. @@ -38,13 +33,13 @@ install the required Cython version using your repository. E.g. on Ubuntu, do: - + $ sudo apt-get install cython However, our cython-based binding requires Cython version 0.19 or newer, but sometimes distributions only provide older version. Make sure to verify the current installed version before going into section 2 above. - + E.g, on Ubuntu, you can verify the current Cython version with: $ apt-cache policy cython From d7e5b24b36b833032a548974b6d9804b3d5ed9d6 Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Sun, 2 Jun 2024 00:36:37 -0700 Subject: [PATCH 3/5] Remove some leftover install3 references --- .github/workflows/CITest.yml | 4 ++-- bindings/python/Makefile | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/CITest.yml b/.github/workflows/CITest.yml index cfb781228a..e37375b08b 100644 --- a/.github/workflows/CITest.yml +++ b/.github/workflows/CITest.yml @@ -179,7 +179,7 @@ jobs: run: | cp libcapstone.* bindings/python/prebuilt cd bindings/python - make install3 + make install cd .. BUILD_TESTS=no make tests @@ -188,7 +188,7 @@ jobs: run: | pip install cython cd bindings/python - make install3_cython + make install_cython cd .. python -c "import capstone;print(capstone.debug())" | grep Cython BUILD_TESTS=no make tests diff --git a/bindings/python/Makefile b/bindings/python/Makefile index ea271c0533..4fc8b8f49e 100644 --- a/bindings/python/Makefile +++ b/bindings/python/Makefile @@ -1,6 +1,6 @@ PYTHON3 ?= python3 -.PHONY: gen_const install install3 install_cython sdist sdist3 bdist bdist3 clean check +.PHONY: gen_const install install_cython sdist bdist clean check gen_const: cd .. && $(PYTHON3) const_generator.py python From 4addef53c46a047a679c769ad7d74e8f5bb40a25 Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Fri, 28 Jun 2024 13:46:32 -0700 Subject: [PATCH 4/5] Update shebangs to python3 --- contrib/objdump/objdump-m68k.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contrib/objdump/objdump-m68k.py b/contrib/objdump/objdump-m68k.py index 3454de3558..5e10b8e19b 100644 --- a/contrib/objdump/objdump-m68k.py +++ b/contrib/objdump/objdump-m68k.py @@ -1,6 +1,5 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 -from __future__ import print_function import sys import bitstring from capstone import * From ddfad673f676ddc8a1c911b4f205bcfa677d9ae5 Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Fri, 28 Jun 2024 22:31:55 -0700 Subject: [PATCH 5/5] Delete suite/test_corpus.py --- suite/test_corpus.py | 170 ------------------------------------------- 1 file changed, 170 deletions(-) delete mode 100755 suite/test_corpus.py diff --git a/suite/test_corpus.py b/suite/test_corpus.py deleted file mode 100755 index 1d970ac5f0..0000000000 --- a/suite/test_corpus.py +++ /dev/null @@ -1,170 +0,0 @@ -#!/usr/bin/python -# Test tool to compare Capstone output with llvm-mc. By Nguyen Anh Quynh, 2014 -import sys -import os -from capstone import * - -def test_file(fname): - print("Test %s" %fname); - f = open(fname) - lines = f.readlines() - f.close() - - if not lines[0].startswith('# '): - print("ERROR: decoding information is missing") - return - - # skip '# ' at the front, then split line to get out hexcode - # Note: option can be '', or 'None' - #print lines[0] - #print lines[0][2:].split(', ') - (arch, mode, option) = lines[0][2:].split(', ') - mode = mode.replace(' ', '') - option = option.strip() - - archs = { - "CS_ARCH_ARM": CS_ARCH_ARM, - "CS_ARCH_AARCH64": CS_ARCH_AARCH64, - "CS_ARCH_MIPS": CS_ARCH_MIPS, - "CS_ARCH_PPC": CS_ARCH_PPC, - "CS_ARCH_SPARC": CS_ARCH_SPARC, - "CS_ARCH_SYSZ": CS_ARCH_SYSZ, - "CS_ARCH_X86": CS_ARCH_X86, - "CS_ARCH_XCORE": CS_ARCH_XCORE, - "CS_ARCH_RISCV": CS_ARCH_RISCV, - "CS_ARCH_TRICORE": CS_ARCH_TRICORE, - "CS_ARCH_ALPHA": CS_ARCH_ALPHA, - "CS_ARCH_HPPA": CS_ARCH_HPPA, - } - - modes = { - "CS_MODE_16": CS_MODE_16, - "CS_MODE_32": CS_MODE_32, - "CS_MODE_64": CS_MODE_64, - "CS_MODE_MIPS32": CS_MODE_MIPS32, - "CS_MODE_MIPS64": CS_MODE_MIPS64, - "0": CS_MODE_ARM, - "CS_MODE_ARM": CS_MODE_ARM, - "CS_MODE_THUMB": CS_MODE_THUMB, - "CS_MODE_ARM+CS_MODE_V8": CS_MODE_ARM+CS_MODE_V8, - "CS_MODE_THUMB+CS_MODE_V8": CS_MODE_THUMB+CS_MODE_V8, - "CS_MODE_THUMB+CS_MODE_MCLASS": CS_MODE_THUMB+CS_MODE_MCLASS, - "CS_MODE_THUMB+CS_MODE_V8+CS_MODE_MCLASS": CS_MODE_THUMB+CS_MODE_V8+CS_MODE_MCLASS, - "CS_MODE_LITTLE_ENDIAN": CS_MODE_LITTLE_ENDIAN, - "CS_MODE_BIG_ENDIAN": CS_MODE_BIG_ENDIAN, - "CS_MODE_64+CS_MODE_LITTLE_ENDIAN": CS_MODE_64+CS_MODE_LITTLE_ENDIAN, - "CS_MODE_64+CS_MODE_BIG_ENDIAN": CS_MODE_64+CS_MODE_BIG_ENDIAN, - "CS_MODE_MIPS32+CS_MODE_MICRO": CS_MODE_MIPS32+CS_MODE_MICRO, - "CS_MODE_MIPS32+CS_MODE_MICRO+CS_MODE_BIG_ENDIAN": CS_MODE_MIPS32+CS_MODE_MICRO+CS_MODE_BIG_ENDIAN, - "CS_MODE_MIPS32+CS_MODE_BIG_ENDIAN+CS_MODE_MICRO": CS_MODE_MIPS32+CS_MODE_MICRO+CS_MODE_BIG_ENDIAN, - "CS_MODE_BIG_ENDIAN+CS_MODE_V9": CS_MODE_BIG_ENDIAN + CS_MODE_V9, - "CS_MODE_MIPS32+CS_MODE_BIG_ENDIAN": CS_MODE_MIPS32+CS_MODE_BIG_ENDIAN, - "CS_MODE_MIPS32+CS_MODE_LITTLE_ENDIAN": CS_MODE_MIPS32+CS_MODE_LITTLE_ENDIAN, - "CS_MODE_MIPS64+CS_MODE_LITTLE_ENDIAN": CS_MODE_MIPS64+CS_MODE_LITTLE_ENDIAN, - "CS_MODE_MIPS64+CS_MODE_BIG_ENDIAN": CS_MODE_MIPS64+CS_MODE_BIG_ENDIAN, - "CS_MODE_RISCV32": CS_MODE_RISCV32, - "CS_MODE_RISCV64": CS_MODE_RISCV64, - "CS_MODE_TRICORE_110": CS_MODE_TRICORE_110, - "CS_MODE_TRICORE_120": CS_MODE_TRICORE_120, - "CS_MODE_TRICORE_130": CS_MODE_TRICORE_130, - "CS_MODE_TRICORE_131": CS_MODE_TRICORE_131, - "CS_MODE_TRICORE_160": CS_MODE_TRICORE_160, - "CS_MODE_TRICORE_161": CS_MODE_TRICORE_161, - "CS_MODE_TRICORE_162": CS_MODE_TRICORE_162, - "CS_MODE_BIG_ENDIAN+CS_MODE_QPX": CS_MODE_BIG_ENDIAN+CS_MODE_QPX, - "CS_MODE_HPPA_11": CS_MODE_HPPA_11, - "CS_MODE_HPPA_20": CS_MODE_HPPA_20, - "CS_MODE_HPPA_20W": CS_MODE_HPPA_20W, - - } - - mc_modes = { - ("CS_ARCH_X86", "CS_MODE_32"): 0, - ("CS_ARCH_X86", "CS_MODE_64"): 1, - ("CS_ARCH_ARM", "CS_MODE_ARM"): 2, - ("CS_ARCH_ARM", "CS_MODE_THUMB"): 3, - ("CS_ARCH_ARM", "CS_MODE_ARM+CS_MODE_V8"): 4, - ("CS_ARCH_ARM", "CS_MODE_THUMB+CS_MODE_V8"): 5, - ("CS_ARCH_ARM", "CS_MODE_THUMB+CS_MODE_MCLASS"): 6, - ("CS_ARCH_ARM", "CS_MODE_THUMB+CS_MODE_V8+CS_MODE_MCLASS"): 7, - ("CS_ARCH_AARCH64", "0"): 8, - ("CS_ARCH_MIPS", "CS_MODE_MIPS32+CS_MODE_BIG_ENDIAN"): 9, - ("CS_ARCH_MIPS", "CS_MODE_MIPS32+CS_MODE_MICRO"): 10, - ("CS_ARCH_MIPS", "CS_MODE_MIPS64"): 11, - ("CS_ARCH_MIPS", "CS_MODE_MIPS32"): 12, - ("CS_ARCH_MIPS", "CS_MODE_MIPS64+CS_MODE_BIG_ENDIAN"): 13, - ("CS_ARCH_MIPS", "CS_MODE_MIPS32+CS_MODE_MICRO+CS_MODE_BIG_ENDIAN"): 14, - ("CS_ARCH_MIPS", "CS_MODE_MIPS32+CS_MODE_BIG_ENDIAN+CS_MODE_MICRO"): 14, - ("CS_ARCH_PPC", "CS_MODE_BIG_ENDIAN"): 15, - ("CS_ARCH_SPARC", "CS_MODE_BIG_ENDIAN"): 16, - ("CS_ARCH_SPARC", "CS_MODE_BIG_ENDIAN+CS_MODE_V9"): 17, - ("CS_ARCH_SYSZ", "0"): 18, - ("CS_ARCH_XCORE", "0"): 19, - ("CS_ARCH_MIPS", "CS_MODE_MIPS32R6+CS_MODE_BIG_ENDIAN"): 20, - ("CS_ARCH_MIPS", "CS_MODE_MIPS32R6+CS_MODE_MICRO+CS_MODE_BIG_ENDIAN"): 21, - ("CS_ARCH_MIPS", "CS_MODE_MIPS32R6"): 22, - ("CS_ARCH_MIPS", "CS_MODE_MIPS32R6+CS_MODE_MICRO"): 23, - ("CS_ARCH_M68K", "0"): 24, - ("CS_ARCH_M680X", "CS_MODE_M680X_6809"): 25, - ("CS_ARCH_EVM", "0"): 26, - ("CS_ARCH_BPF", "CS_MODE_LITTLE_ENDIAN+CS_MODE_BPF_CLASSIC"): 30, - ("CS_ARCH_BPF", "CS_MODE_LITTLE_ENDIAN+CS_MODE_BPF_EXTENDED"): 31, - ("CS_ARCH_BPF", "CS_MODE_BIG_ENDIAN+CS_MODE_BPF_CLASSIC"): 32, - ("CS_ARCH_BPF", "CS_MODE_BIG_ENDIAN+CS_MODE_BPF_EXTENDED"): 33, - ("CS_ARCH_RISCV", "CS_MODE_RISCV32"): 45, - ("CS_ARCH_RISCV", "CS_MODE_RISCV64"): 46, - ("CS_ARCH_TRICORE", "CS_MODE_TRICORE_110"): 47, - ("CS_ARCH_TRICORE", "CS_MODE_TRICORE_120"): 48, - ("CS_ARCH_TRICORE", "CS_MODE_TRICORE_130"): 49, - ("CS_ARCH_TRICORE", "CS_MODE_TRICORE_131"): 50, - ("CS_ARCH_TRICORE", "CS_MODE_TRICORE_160"): 51, - ("CS_ARCH_TRICORE", "CS_MODE_TRICORE_161"): 52, - ("CS_ARCH_TRICORE", "CS_MODE_TRICORE_162"): 53, - ("CS_ARCH_PPC", "CS_MODE_BIG_ENDIAN+CS_MODE_QPX"): 54, - ("CS_ARCH_ALPHA", "CS_MODE_LITTLE_ENDIAN"): 55, - ("CS_ARCH_ALPHA", "CS_MODE_BIG_ENDIAN"): 56, - ("CS_ARCH_HPPA", "CS_MODE_HPPA_11+CS_MODE_BIG_ENDIAN"): 57, - ("CS_ARCH_HPPA", "CS_MODE_HPPA_20+CS_MODE_BIG_ENDIAN"): 58, - ("CS_ARCH_LOONGARCH", "CS_MODE_LOONGARCH32"): 59, - ("CS_ARCH_LOONGARCH", "CS_MODE_LOONGARCH64"): 60, - } - - #if not option in ('', 'None'): - # print archs[arch], modes[mode], options[option] - - for line in lines[1:]: - # ignore all the input lines having # in front. - if line.startswith('#'): - continue - if line.startswith('// '): - line=line[3:] - #print("Check %s" %line) - code = line.split(' = ')[0] - if len(code) < 2: - continue - if code.find('//') >= 0: - continue - hex_code = code.replace('0x', '') - hex_code = hex_code.replace(',', '') - hex_code = hex_code.replace(' ', '') - try: - hex_data = hex_code.strip().decode('hex') - except: - print "skipping", hex_code - fout = open("fuzz/corpus/%s_%s" % (os.path.basename(fname), hex_code), 'w') - if (arch, mode) not in mc_modes: - print "fail", arch, mode - fout.write(unichr(mc_modes[(arch, mode)])) - fout.write(hex_data) - fout.close() - - -if __name__ == '__main__': - if len(sys.argv) == 1: - fnames = sys.stdin.readlines() - for fname in fnames: - test_file(fname.strip()) - else: - #print("Usage: ./test_mc.py ") - test_file(sys.argv[1]) -