39 lines
2.0 KiB
Java
39 lines
2.0 KiB
Java
// Local C pseudocode export for a raw, user-selected C28x code range.
|
|
import ghidra.app.script.GhidraScript;
|
|
import ghidra.app.decompiler.DecompInterface;
|
|
import ghidra.app.decompiler.DecompileResults;
|
|
import ghidra.program.model.address.Address;
|
|
import ghidra.program.model.listing.Function;
|
|
import ghidra.program.model.listing.FunctionIterator;
|
|
import java.io.PrintWriter;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
public class SetguiExportC extends GhidraScript {
|
|
public void run() throws Exception {
|
|
Address entry = currentProgram.getMinAddress();
|
|
disassemble(entry);
|
|
createFunction(entry, null);
|
|
analyzeAll(currentProgram);
|
|
DecompInterface decompiler = new DecompInterface();
|
|
try (PrintWriter out = new PrintWriter(getScriptArgs()[0], StandardCharsets.UTF_8.name())) {
|
|
out.println("/* Ghidra C pseudocode. Recovered types/names may be incomplete. */");
|
|
if (!decompiler.openProgram(currentProgram))
|
|
throw new Exception(decompiler.getLastMessage());
|
|
FunctionIterator functions = currentProgram.getFunctionManager().getFunctions(true);
|
|
int count = 0;
|
|
while (functions.hasNext() && !monitor.isCancelled()) {
|
|
Function function = functions.next();
|
|
if (function.isExternal()) continue;
|
|
DecompileResults result = decompiler.decompileFunction(function, 10, monitor);
|
|
out.println("/* Address: " + function.getEntryPoint() + " */");
|
|
if (result.decompileCompleted() && result.getDecompiledFunction() != null) {
|
|
out.println(result.getDecompiledFunction().getC());
|
|
count++;
|
|
} else out.println("/* Decompilation failed: " + result.getErrorMessage() + " */");
|
|
}
|
|
if (count == 0) throw new Exception("No functions decompiled. Select a range beginning at a function entry.");
|
|
out.println("/* SETGUI_DECOMPILE_SUCCESS */");
|
|
} finally { decompiler.dispose(); }
|
|
}
|
|
}
|