2024-01-22 05:51:31 +01:00
|
|
|
//TODO write a description for this script
|
2024-02-11 20:20:19 +01:00
|
|
|
//@author DrFrugal
|
2024-01-22 05:51:31 +01:00
|
|
|
//@category Export
|
|
|
|
//@keybinding
|
|
|
|
//@menupath
|
|
|
|
//@toolbar bomb
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import ghidra.app.decompiler.DecompInterface;
|
|
|
|
import ghidra.app.decompiler.DecompileOptions;
|
|
|
|
import ghidra.app.decompiler.DecompileResults;
|
|
|
|
import ghidra.app.script.GhidraScript;
|
|
|
|
import ghidra.program.model.listing.Function;
|
|
|
|
|
2024-02-11 20:20:19 +01:00
|
|
|
public class ExportFuncBulk extends GhidraScript {
|
2024-01-22 05:51:31 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() throws Exception {
|
|
|
|
|
2024-02-11 20:20:19 +01:00
|
|
|
List<Function> matchingFunctions = BulkUtils.getFunctions(this);
|
|
|
|
if(matchingFunctions.size() == 0) {
|
2024-01-22 05:51:31 +01:00
|
|
|
println("Abort: no functions match the provided regex");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String programName = currentProgram.getName();
|
|
|
|
DecompInterface decompiler = new DecompInterface();
|
|
|
|
DecompileOptions options = new DecompileOptions();
|
|
|
|
options.setMaxWidth(1024); // easier parsing if everything is in one line
|
|
|
|
decompiler.setOptions(options);
|
|
|
|
decompiler.openProgram(currentProgram);
|
|
|
|
|
|
|
|
String fileName = programName + ".c";
|
|
|
|
File outputFile = BulkUtils.askForOutputFile(fileName);
|
|
|
|
if(outputFile == null){
|
|
|
|
println("Abort: export dialog has been cancelled");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintWriter decompWriter = new PrintWriter(outputFile);
|
|
|
|
int decompileTimeout = 10; // seconds
|
2024-02-11 20:20:19 +01:00
|
|
|
matchingFunctions.forEach(function -> {
|
2024-01-22 05:51:31 +01:00
|
|
|
decompWriter.write("// ADDRESS - 0x" + function.getEntryPoint().toString().toUpperCase() + "\n");
|
|
|
|
DecompileResults decompileResults = decompiler.decompileFunction(function, decompileTimeout, null);
|
|
|
|
String decompiledCode = decompileResults.getDecompiledFunction().getC();
|
|
|
|
decompWriter.println(decompiledCode.trim() + "\n\n");
|
|
|
|
});
|
|
|
|
decompWriter.close();
|
2024-02-11 20:20:19 +01:00
|
|
|
println(matchingFunctions.size() + " matching function(s) exported to " + outputFile.getAbsolutePath());
|
2024-01-22 05:51:31 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|