1212import os
1313
1414from multiprocessing import Pool , Queue , Manager
15+
16+ from idascript import IDA_PATH_ENV , get_ida_path
1517from binexport import ProgramBinExport
1618from binexport .utils import logger
1719from binexport .types import DisassemblerBackend
@@ -54,48 +56,37 @@ def recursive_file_iter(p: Path) -> Generator[Path, None, None]:
5456 yield from recursive_file_iter (f )
5557
5658
57- def export_job (ingress , egress , backend : DisassemblerBackend ) -> bool :
59+ def export_job (ingress , egress , backend : DisassemblerBackend ) -> None :
5860 while True :
5961 try :
6062 file = ingress .get (timeout = 0.5 )
61- res = ProgramBinExport .from_binary_file (
62- file .as_posix (), backend = backend , open_export = False
63- )
63+ res = ProgramBinExport .generate (file .as_posix (), backend = backend )
6464 egress .put ((file , res ))
65- except Exception as e :
66- # Might not be printed as triggered withing a fork
67- logger .error (traceback .format_exception (e ).decode ())
68- egress .put ((file , e ))
6965 except queue .Empty :
7066 pass
7167 except KeyboardInterrupt :
7268 break
69+ except Exception as e :
70+ # Might not be printed as triggered withing a fork
71+ logger .error (traceback .format_exception (e ))
72+ egress .put ((file , e ))
7373
7474
75- def __check_path () -> bool :
76- global IDA_BINARY
77- if "PATH" in os .environ :
78- for p in os .environ ["PATH" ].split (":" ):
79- for bin_name in __get_names ():
80- if (Path (p ) / bin_name ).exists ():
81- IDA_BINARY = (Path (p ) / bin_name ).resolve ()
82- return True
83- return False
84-
8575def check_disassembler_availability (disass : DisassemblerBackend , disass_path : str ) -> bool :
8676 """
8777 Check if the disassembler is available in the system.
78+ It also set the necessary environment variables.
79+
8880 :param disass: Disassembler backend to check
8981 :param disass_path: Path of the disassembler (if not in PATH)
9082 :return: True if the disassembler is available, False otherwise
9183 """
9284 if disass == DisassemblerBackend .IDA :
9385 if disass_path :
9486 ida_path = Path (disass_path )
95- os .environ [" IDA_PATH_ENV" ] = str (ida_path ) if ida_path . is_dir () else str ( ida_path . parent )
87+ os .environ [IDA_PATH_ENV ] = str (ida_path )
9688 try :
97- from idascript import __check_path
98- return __check_path ()
89+ return bool (get_ida_path ())
9990 except ImportError :
10091 logger .error ("Cannot import idascript python module" )
10192 return False
@@ -106,12 +97,12 @@ def check_disassembler_availability(disass: DisassemblerBackend, disass_path: st
10697 os .environ ["GHIDRA_PATH" ] = disass_path
10798 return ghidra_path .exists ()
10899 else :
109- logger .error (f"Ghidra path { ghidra_path } does not exist" )
100+ logger .error (f"Ghidra path { disass_path } does not exist" )
110101 return False
111102
112103 elif disass == DisassemblerBackend .BINARY_NINJA :
113104 try :
114- import binaryninja
105+ import binaryninja # type: ignore
115106 except ImportError :
116107 logger .error ("Cannot import binaryninja python module" )
117108 return False
@@ -133,8 +124,8 @@ def check_disassembler_availability(disass: DisassemblerBackend, disass_path: st
133124 "--disass-path" ,
134125 type = click .Path (exists = True ),
135126 default = "" ,
136- help = "Path of the disassembler (if not in PATH )" \
137- "Can be provided with IDA_PATH, GHIDRA_PATH env variables " ,
127+ help = "Path of the disassembler (dir or binary for IDA, dir for Ghidra )" \
128+ "(if not provided search $PATH or environment variable IDA_PATH, GHIDRA_PATH) " ,
138129)
139130@click .option ("-t" , "--threads" , type = int , default = 1 , help = "Thread number to use" )
140131@click .option ("-v" , "--verbose" , count = True , help = "To activate or not the verbosity" )
@@ -150,13 +141,6 @@ def main(disassembler: str,
150141 binexporter is a very simple utility to generate a .BinExport file
151142 for a given binary or a directory. It opens all binary files and export
152143 the them seamlessly.
153-
154- :param disassembler: Disassembler engine to use
155- :param disass_path: Path of the disassembler (if not in PATH)
156- :param input_file: Path of the binary to export
157- :param threads: number of threads to use
158- :param verbose: To activate or not the verbosity
159- :param stop_on_error: Stop if any of the worker raises an exception
160144 """
161145
162146 logging .basicConfig (format = "%(message)s" , level = logging .DEBUG if verbose else logging .INFO )
@@ -198,7 +182,7 @@ def main(disassembler: str,
198182 if isinstance (res , Exception ):
199183 logger .error (f"Error while processing { path } : { res } " )
200184 if stop_on_error :
201- logger .error (traceback .format_exception (res ). decode () )
185+ logger .error (traceback .format_exception (res ))
202186 pool .terminate ()
203187 break
204188 else :
0 commit comments