-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathParseDataType.java
100 lines (75 loc) · 2.82 KB
/
ParseDataType.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Parse Datatype
//@author Florian Magin
//@category Data Types
//@keybinding
//@menupath
//@toolbar
import docking.DialogComponentProvider;
import ghidra.app.script.GhidraScript;
import ghidra.app.util.cparser.C.CParser;
import ghidra.app.util.cparser.C.ParseException;
import ghidra.program.model.data.DataType;
import ghidra.program.model.data.DataTypeManager;
import javax.swing.*;
public class ParseDataType extends GhidraScript {
@Override
protected void run() throws Exception {
var dialog = new ParseStructDialog();
state.getTool().showDialog(dialog);
}
class ParseStructDialog extends DialogComponentProvider {
private final DataTypeManager main_gdt;
private JTextArea textInput;
JTextArea typeOutput;
private DataType currentType;
JButton parseButton;
JSplitPane splitter;
private ParseStructDialog() {
super("Parse Data Type", false, true, true, true);
setPreferredSize(500, 400);
// GUI SETUP
this.addCancelButton();
this.parseButton = new JButton("Parse");
this.parseButton.addActionListener((e) -> { this.parseType();});
this.parseButton.setToolTipText("Parse the type and preview the result");
this.addButton(parseButton);
this.addApplyButton();
this.setApplyToolTip("Add the last parsed type to the current data types");
this.setApplyEnabled(false);
textInput = new JTextArea(12, 50);
textInput.setWrapStyleWord(true);
textInput.setLineWrap(true);
typeOutput = new JTextArea(12, 50);
typeOutput.setWrapStyleWord(true);
typeOutput.setLineWrap(true);
typeOutput.setEditable(false);
splitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
textInput, typeOutput);
addWorkPanel(splitter);
// Parser Setup
main_gdt = currentProgram.getDataTypeManager();
}
private void parseType() {
var text = this.textInput.getText();
var parser = new CParser(main_gdt);
DataType type;
try {
type = parser.parse(text);
} catch (ParseException e) {
typeOutput.setText(e.toString());
this.setApplyEnabled(false);
return;
}
currentType = type;
typeOutput.setText(currentType.toString());
this.setApplyEnabled(true);
}
@Override
protected void applyCallback() {
int transaction_id = main_gdt.startTransaction("Parsed");
main_gdt.addDataType(currentType, null);
main_gdt.endTransaction(transaction_id, true);
this.close();
}
}
}