GhidraScripts/ExportFuncBulk.java

57 lines
1.9 KiB
Java
Raw Normal View History

2024-01-22 05:51:31 +01:00
//TODO write a description for this script
//@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;
public class ExportFuncBulk extends GhidraScript {
2024-01-22 05:51:31 +01:00
@Override
public void run() throws Exception {
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
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();
println(matchingFunctions.size() + " matching function(s) exported to " + outputFile.getAbsolutePath());
2024-01-22 05:51:31 +01:00
}
}