Skip to content

Commit

Permalink
ghidra: small refactor and inline serializing of parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
kumarak committed Oct 24, 2024
1 parent 25f0cd4 commit a2346b1
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions scripts/ghidra/PatchestryDecompileFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,8 @@ private HighVariable lValueOf(Varnode node) throws Exception {
return var;
}

private String getDisplayName(DataType data_type) throws Exception {
return data_type.getDisplayName().replaceAll(" ", "");
}

private void serializePointerType(Pointer ptr) throws Exception {
name("name").value(getDisplayName(ptr));
name("name").value(ptr.getDisplayName());
name("kind").value("pointer");
name("size").value(ptr.getLength());
DataType elem_type = ptr.getDataType();
Expand All @@ -222,7 +218,7 @@ private void serializePointerType(Pointer ptr) throws Exception {
}

private void serializeTypedefType(TypeDef typedef) throws Exception {
name("name").value(getDisplayName(typedef));
name("name").value(typedef.getDisplayName());
name("kind").value("typedef");
name("size").value(typedef.getLength());

Expand All @@ -238,7 +234,7 @@ private void serializeTypedefType(TypeDef typedef) throws Exception {
}

private void serializeArrayType(Array arr) throws Exception {
name("name").value(getDisplayName(arr));
name("name").value(arr.getDisplayName());
name("kind").value("array");
name("size").value(arr.getLength());
name("num_elements").value(arr.getNumElements());
Expand All @@ -249,16 +245,15 @@ private void serializeArrayType(Array arr) throws Exception {
}

private void serializeBuiltinType(DataType data_type, String kind) throws Exception {
name("name").value(getDisplayName(data_type));
name("name").value(data_type.getDisplayName());
name("size").value(data_type.getLength());
name("kind").value(kind);
}

private void serializeCompositeType(Composite data_type, String kind) throws Exception {
name("name").value(getDisplayName(data_type));
name("name").value(data_type.getDisplayName());
name("kind").value(kind);
name("size").value(data_type.getLength());
name("num_fields").value(data_type.getNumComponents());
name("fields").beginArray();

for (int i = 0; i < data_type.getNumComponents(); i++) {
Expand All @@ -280,12 +275,15 @@ private void serializeFunctionDefinition(FunctionDefinition fd) throws Exception
name("name").value(fd.getDisplayName());
name("kind").value("function");
name("return_type").value(label(fd.getReturnType()));
name("has_varargs").value(fd.hasVarArgs());
name("is_variadic").value(fd.hasVarArgs());
ParameterDefinition[] arguments = fd.getArguments();
name("parameters").beginArray();
for (int i = 0; i < arguments.length; i++) {
beginObject();
name("name").value(arguments[i].getName());
String name = arguments[i].getName();
if (name != null && !name.isEmpty()) {
name("name").value(arguments[i].getName());
}
name("size").value(arguments[i].getLength());
name("type").value(label(arguments[i].getDataType()));
endObject();
Expand Down Expand Up @@ -347,7 +345,16 @@ private void serialize(FunctionPrototype proto) throws Exception {

name("parameters").beginArray();
for (int i = 0; i < proto.getNumParams(); i++) {
serialize(proto.getParam(i).getHighVariable());
HighVariable hv = proto.getParam(i).getHighVariable();
if (hv != null) {
beginObject();
String hv_name = hv.getName();
if (hv_name != null && !hv_name.isEmpty()) {
name("name").value(hv_name);
}
name("type").value(label(hv.getDataType()));
endObject();
}
}
endArray();
}
Expand Down

0 comments on commit a2346b1

Please sign in to comment.