Skip to content

Commit

Permalink
fix: restructuring of code
Browse files Browse the repository at this point in the history
Signed-off-by: VyacheslavIurevich <[email protected]>
  • Loading branch information
VyacheslavIurevich committed Jul 16, 2024
1 parent 1232a9d commit 0fa5ba5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
65 changes: 37 additions & 28 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Postprocessor main"""

# pylint: disable=wrong-import-position, import-error, too-many-locals
# pylint: disable=wrong-import-position, import-error
import pyhidra

pyhidra.start()
Expand All @@ -11,43 +11,52 @@
LIBRARY_LIST = ["stdio.h", "stdlib.h", "inttypes.h"]


def init_decompiler(program):
"""Decompiler initialization"""
options = DecompileOptions()
options.grabFromProgram(program)
decompiler = DecompInterface()
decompiler.setOptions(options)
decompiler.openProgram(program)
return decompiler


def put_functions(program, file_writer, monitor):
"""Puts all functions and their signatures into C code file"""
decompiler = init_decompiler(program)
functions_code = []
for function in program.getFunctionManager().getFunctions(True):
if tools.exclude_function(function):
continue
results = decompiler.decompileFunction(function, 0, monitor)
decompiled_function = results.getDecompiledFunction()
function_signature = decompiled_function.getSignature()
function_signature_replaced_types = tools.replace_types(function_signature)
function_code = decompiled_function.getC()
function_code_replaced_types = tools.replace_types(function_code)
functions_code.append(function_code_replaced_types)
file_writer.println(function_signature_replaced_types + '\n')
used_concats = set()
for function_code in functions_code:
if "CONCAT" in function_code:
used_concats = \
tools.put_concat(file_writer, function_code, used_concats)
file_writer.println(function_code)
decompiler.closeProgram()
decompiler.dispose()


def export_c_code(binary_file_path, output_file_path):
"""Exporting c code to a file"""
with pyhidra.open_program(binary_file_path) as flat_api:
program = flat_api.getCurrentProgram()
options = DecompileOptions()
options.grabFromProgram(program)

decompiler = DecompInterface()
decompiler.setOptions(options)
decompiler.openProgram(program)

f = File(output_file_path)
c_file_writer = PrintWriter(f)
for lib in LIBRARY_LIST:
c_file_writer.println(f"#include <{lib}>")
tools.write_program_data_types(program, c_file_writer, flat_api.monitor)
functions_code = []
for function in program.getFunctionManager().getFunctions(True):
if tools.exclude_function(function):
continue
results = decompiler.decompileFunction(function, 0, flat_api.monitor)
decompiled_function = results.getDecompiledFunction()
function_signature = decompiled_function.getSignature()
function_signature_replaced_types = tools.replace_types(function_signature)
function_code = decompiled_function.getC()
function_code_replaced_types = tools.replace_types(function_code)
functions_code.append(function_code_replaced_types)
c_file_writer.println(function_signature_replaced_types + '\n')
used_concats = set()
for function_code in functions_code:
if "CONCAT" in function_code:
used_concats = \
tools.put_concat(c_file_writer, function_code, used_concats)
c_file_writer.println(function_code)
put_functions(program, c_file_writer, flat_api.monitor)
c_file_writer.close()
decompiler.closeProgram()
decompiler.dispose()


export_c_code("resources/in/bmp-header.out", "resources/out/test.c")
6 changes: 3 additions & 3 deletions src/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
pyhidra.start()
from ghidra.program.model.data import DataTypeWriter

TYPES_TO_REPLACE = OrderedDict(uint="unsigned int", \
ushort = "unsigned short", \
ulong = "unsigned long")
TYPES_TO_REPLACE = OrderedDict(uint="unsigned int",
ushort="unsigned short",
ulong="unsigned long")
CONCAT_LEN = 6 # = len("CONCAT")
BYTE_SIZE = 8
HEX_BASE = 16
Expand Down

0 comments on commit 0fa5ba5

Please sign in to comment.