This commit is contained in:
Guus Waals 2025-05-29 22:18:55 +08:00
parent b0b4683f7d
commit 35a220de3f
7 changed files with 157 additions and 7244 deletions

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,7 @@
#if _WIN32
#include <Windows.h>
#include <d3d8.h>
#include <dinput.h>
#else
#include "win32_shim.h"
#endif
@ -21,6 +22,7 @@ typedef uint16_t word;
typedef uint32_t dword;
typedef unsigned long ulong;
typedef unsigned char uchar;
typedef unsigned long uint;
struct undefined3 {
uint8_t _0;

File diff suppressed because it is too large Load Diff

View File

@ -13,6 +13,7 @@ dlgs.h
excpt.h
float.h
guiddef.h
Guiddef.h
imm.h
io.h
jmorecfg.h
@ -48,7 +49,6 @@ rpcnsip.h
rpcnterr.h
servprov.h
shellapi.h
snddef.h
stdarg.h
stdint.h
stdio.h

View File

@ -13,6 +13,6 @@ public class DumpTypes extends GhidraScript {
@Override
protected void run() throws Exception {
TypeDumper dumper = new TypeDumper(this);
dumper.run();
dumper.run();
}
}

View File

@ -1,6 +1,8 @@
// Source code is decompiled from a .class file using FernFlower decompiler.
package re3lib;
import ghidra.app.script.GhidraScript;
import ghidra.app.script.ScriptMessage;
import ghidra.program.model.data.*;
import ghidra.program.model.data.Enum;
import ghidra.util.Msg;
@ -18,7 +20,7 @@ public class DataTypeWriter {
private static String[] INTEGRAL_MODIFIERS = new String[] { "signed", "unsigned", "const", "static", "volatile",
"mutable" };
private static String EOL = System.getProperty("line.separator");
private Writer writer;
private DataTypeManager dtm;
private DataOrganization dataOrganization;
@ -30,7 +32,7 @@ public class DataTypeWriter {
final DataType dataType;
final String code;
final Set<String> dependencies;
Block(DataType dataType, String code, Set<String> dependencies) {
this.dataType = dataType;
this.code = code;
@ -108,18 +110,18 @@ public class DataTypeWriter {
public void write(List<DataType> dataTypes, TaskMonitor monitor, boolean throwExceptionOnInvalidType)
throws IOException, CancelledException {
monitor.initialize((long) dataTypes.size());
// Step 1: Create blocks for each data type
Map<String, Block> blocks = new HashMap<>();
int cnt = 0;
for (DataType dataType : dataTypes) {
monitor.checkCancelled();
if (dataType == null || blacklistedTypes.contains(dataType.getDisplayName())) {
continue;
}
try {
Block block = createBlock(dataType);
if (block != null) {
@ -131,30 +133,34 @@ public class DataTypeWriter {
}
Msg.error(this, "Failed to process data type: " + dataType.getDisplayName(), e);
}
++cnt;
monitor.setProgress((long) cnt);
}
// Step 2: Topological sort and write
List<Block> sortedBlocks = topologicalSort(blocks);
for (Block block : sortedBlocks) {
writer.write(block.code);
writer.write(EOL);
}
writer.flush();
}
private Block createBlock(DataType dt) throws IOException {
if (dt instanceof FunctionDefinition || dt instanceof FactoryDataType) {
return null; // Skip these types
if (dt instanceof FactoryDataType) {
return null; // Skip only factory types
}
dt = dt.clone(this.dtm);
Set<String> dependencies = new HashSet<>();
StringBuilder code = new StringBuilder();
if (dt.getDisplayName().contains("HIE_tduLinkedObject")) {
System.out.println("DEBUG " + dt.getDisplayName());
}
if (dt.equals(DataType.DEFAULT)) {
code.append("typedef unsigned char ").append(DataType.DEFAULT.getName()).append(";");
} else if (dt instanceof Dynamic) {
@ -175,16 +181,18 @@ public class DataTypeWriter {
writeTypeDefBlock((TypeDef) dt, code, dependencies);
} else if (dt instanceof BuiltInDataType) {
writeBuiltInBlock((BuiltInDataType) dt, code, dependencies);
} else if (dt instanceof FunctionDefinition) {
return null;
} else {
code.append(comment("Unable to write datatype. Type unrecognized: " + dt.getClass()));
}
return new Block(dt, code.toString(), dependencies);
}
private void writeStructureBlock(Structure struct, StringBuilder code, Set<String> dependencies) {
String structName = struct.getDisplayName();
// Struct definition
code.append("struct ").append(structName).append(" {");
String descrip = struct.getDescription();
@ -192,65 +200,60 @@ public class DataTypeWriter {
code.append(" ").append(comment(descrip));
}
code.append(EOL);
// Process components
for (DataTypeComponent component : struct.getComponents()) {
writeComponentBlock(component, struct, code, dependencies);
}
code.append(annotator.getSuffix(struct, null));
code.append("};");
}
private void writeUnionBlock(Union union, StringBuilder code, Set<String> dependencies) {
String unionName = union.getDisplayName();
// Forward declaration
code.append("typedef union ").append(unionName).append(" ").append(unionName)
.append(", *P").append(unionName).append(";").append(EOL);
// Union definition
// Union definition (no forward declaration needed - let dependency ordering
// handle it)
code.append("union ").append(unionName).append(" {");
String descrip = union.getDescription();
if (descrip != null && descrip.length() > 0) {
code.append(" ").append(comment(descrip));
}
code.append(EOL);
// Process components
for (DataTypeComponent component : union.getComponents()) {
writeComponentBlock(component, union, code, dependencies);
}
code.append(annotator.getSuffix(union, null));
code.append("};");
}
private void writeComponentBlock(DataTypeComponent component, Composite composite, StringBuilder code, Set<String> dependencies) {
private void writeComponentBlock(DataTypeComponent component, Composite composite, StringBuilder code,
Set<String> dependencies) {
code.append(" ");
code.append(annotator.getPrefix(composite, component));
String fieldName = component.getFieldName();
String originalName = fieldName;
boolean needsFixing = fieldName != null && fieldName.contains("?");
if (fieldName == null || fieldName.length() == 0 || needsFixing) {
fieldName = component.getDefaultFieldName();
}
DataType componentDataType = component.getDataType();
// Add dependency only if it's not a pointer (pointers can be forward declared)
if (!isPointerType(componentDataType)) {
DataType depType = getImmediateDependencyType(componentDataType);
if (depType != null && !isBuiltInType(depType)) {
dependencies.add(depType.getDisplayName());
}
DataType depType = getImmediateDependencyType(componentDataType);
if (depType != null) {
dependencies.add(depType.getDisplayName());
}
code.append(getTypeDeclaration(fieldName, componentDataType, component.getLength()));
code.append(";");
code.append(annotator.getSuffix(composite, component));
String comment = component.getComment();
String commentText = comment != null && comment.length() > 0 ? comment : "";
if (needsFixing) {
@ -264,7 +267,7 @@ public class DataTypeWriter {
private void writeEnumBlock(Enum enumm, StringBuilder code, Set<String> dependencies) {
String enumName = enumm.getDisplayName();
if (enumName.startsWith("define_") && enumName.length() > 7 && enumm.getCount() == 1) {
long val = enumm.getValues()[0];
code.append("#define ").append(enumName.substring(7)).append(" ").append(Long.toString(val));
@ -275,7 +278,7 @@ public class DataTypeWriter {
code.append(" ").append(comment(description));
}
code.append(EOL);
String[] names = enumm.getNames();
for (int j = 0; j < names.length; ++j) {
code.append(" ");
@ -297,20 +300,77 @@ public class DataTypeWriter {
}
}
private void writeFunctionDefinitionBlock(FunctionDefinition funcDef, String name, StringBuilder code,
Set<String> dependencies) {
DataType returnType = funcDef.getReturnType();
ParameterDefinition[] params = funcDef.getArguments();
// Add return type dependency if not built-in
if (returnType != null && !isBuiltInType(returnType)) {
DataType depType = getImmediateDependencyType(returnType);
if (depType != null) {
dependencies.add(depType.getDisplayName());
}
}
// Build function typedef
code.append("typedef ");
if (returnType != null) {
code.append(getTypeDeclaration("", returnType, -1)).append(" ");
} else {
code.append("void ");
}
code.append("(*").append(name).append(")(");
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
ParameterDefinition param = params[i];
DataType paramType = param.getDataType();
// Add parameter type dependency if not built-in
if (!isBuiltInType(paramType) && !isPointerType(paramType)) {
DataType depType = getImmediateDependencyType(paramType);
if (depType != null) {
dependencies.add(depType.getDisplayName());
}
}
String paramName = param.getName();
if (paramName == null || paramName.isEmpty()) {
paramName = "param" + (i + 1);
}
code.append(getTypeDeclaration(paramName, paramType, -1));
if (i < params.length - 1) {
code.append(", ");
}
}
} else {
code.append("void");
}
code.append(");");
}
private void writeTypeDefBlock(TypeDef typeDef, StringBuilder code, Set<String> dependencies) {
String typedefName = typeDef.getDisplayName();
DataType dataType = typeDef.getDataType();
String dataTypeName = dataType.getDisplayName();
if (!isIntegral(typedefName, dataTypeName)) {
// Add dependency only if it's not a pointer
if (!isPointerType(dataType)) {
DataType depType = getImmediateDependencyType(dataType);
if (depType != null && !isBuiltInType(depType)) {
dependencies.add(depType.getDisplayName());
}
// Handle function definition typedefs
if (dataType instanceof FunctionDefinition) {
writeFunctionDefinitionBlock((FunctionDefinition) dataType, typedefName, code, dependencies);
} // Could be pointer to function
else if (dataType instanceof Pointer && ((Pointer) dataType).getDataType() instanceof FunctionDefinition) {
writeFunctionDefinitionBlock((FunctionDefinition) ((Pointer) dataType).getDataType(), typedefName, code,
dependencies);
} else {
DataType depType = getImmediateDependencyType(dataType);
if (depType != null && !isBuiltInType(depType)) {
dependencies.add(depType.getDisplayName());
}
String typedefString = getTypeDeclaration(typedefName, dataType, -1);
code.append("typedef ").append(typedefString).append(";");
}
@ -322,7 +382,7 @@ public class DataTypeWriter {
code.append(declaration);
}
}
private boolean isPointerType(DataType dt) {
return dt instanceof Pointer;
}
@ -338,6 +398,8 @@ public class DataTypeWriter {
dt = ((Array) dt).getDataType();
} else if (dt instanceof BitFieldDataType) {
dt = ((BitFieldDataType) dt).getBaseDataType();
} else if (dt instanceof Pointer) {
dt = ((Pointer) dt).getDataType();
} else {
break;
}
@ -349,22 +411,22 @@ public class DataTypeWriter {
if (name == null) {
name = "";
}
StringBuilder sb = new StringBuilder();
if (dataType instanceof BitFieldDataType) {
BitFieldDataType bfDt = (BitFieldDataType) dataType;
name = name + ":" + bfDt.getDeclaredBitSize();
dataType = bfDt.getBaseDataType();
}
// Handle arrays and pointers
while (dataType instanceof Array) {
Array array = (Array) dataType;
name = name + "[" + array.getNumElements() + "]";
dataType = array.getDataType();
}
while (dataType instanceof Pointer) {
Pointer pointer = (Pointer) dataType;
DataType elem = pointer.getDataType();
@ -377,12 +439,12 @@ public class DataTypeWriter {
name = "(" + name + ")";
}
}
String prefix = getDataTypePrefix(dataType);
String dataTypeString;
if (dataType instanceof AbstractIntegerDataType) {
dataTypeString = ((AbstractIntegerDataType)dataType).getCDeclaration();
dataTypeString = ((AbstractIntegerDataType) dataType).getCDeclaration();
} else {
dataTypeString = dataType.getDisplayName();
}
@ -391,7 +453,7 @@ public class DataTypeWriter {
if (name.length() != 0) {
componentString = componentString + " " + name;
}
return componentString;
}
@ -400,8 +462,9 @@ public class DataTypeWriter {
if (dataType instanceof TypeDef) {
return "";
}
// Only add struct/union prefix for direct struct/union references (not typedefs)
// Only add struct/union prefix for direct struct/union references (not
// typedefs)
if (dataType instanceof Structure) {
return "struct ";
} else if (dataType instanceof Union) {
@ -419,7 +482,7 @@ public class DataTypeWriter {
return true;
}
}
boolean endsWithIntegralType = false;
for (String type : INTEGRAL_TYPES) {
if (typedefName.endsWith(" " + type)) {
@ -427,13 +490,13 @@ public class DataTypeWriter {
break;
}
}
for (String modifier : INTEGRAL_MODIFIERS) {
if (typedefName.indexOf(modifier + " ") >= 0 || typedefName.indexOf(" " + modifier) >= 0) {
return true;
}
}
if (endsWithIntegralType) {
return true;
} else if (typedefName.endsWith(" " + basetypeName)) {
@ -447,31 +510,32 @@ public class DataTypeWriter {
List<Block> result = new ArrayList<>();
Set<String> visited = new HashSet<>();
Set<String> visiting = new HashSet<>();
for (Block block : blocks.values()) {
if (!visited.contains(block.dataType.getDisplayName())) {
topologicalSortVisit(block, blocks, visited, visiting, result);
}
}
return result;
}
private void topologicalSortVisit(Block block, Map<String, Block> blocks,
Set<String> visited, Set<String> visiting, List<Block> result) {
private void topologicalSortVisit(Block block, Map<String, Block> blocks,
Set<String> visited, Set<String> visiting, List<Block> result) {
String blockName = block.dataType.getDisplayName();
if (visiting.contains(blockName)) {
// Circular dependency detected, but we'll continue (forward declarations should handle this)
// Circular dependency detected, but we'll continue (forward declarations should
// handle this)
return;
}
if (visited.contains(blockName)) {
return;
}
visiting.add(blockName);
// Visit dependencies first
for (String dep : block.dependencies) {
Block depBlock = blocks.get(dep);
@ -479,7 +543,7 @@ public class DataTypeWriter {
topologicalSortVisit(depBlock, blocks, visited, visiting, result);
}
}
visiting.remove(blockName);
visited.add(blockName);
result.add(block);

View File

@ -11,11 +11,14 @@ import ghidra.app.script.GhidraScript;
import ghidra.program.model.data.CategoryPath;
import ghidra.program.model.data.Composite;
import ghidra.program.model.data.DataType;
import ghidra.program.model.data.Enum;
import ghidra.program.model.data.EnumDataType;
import ghidra.program.model.data.ProgramBasedDataTypeManager;
import ghidra.program.model.data.Structure;
import ghidra.program.model.data.TypeDef;
import ghidra.program.model.data.TypedefDataType;
import ghidra.program.model.data.Union;
import ghidra.program.model.data.UnionDataType;
import ghidra.program.model.listing.Program;
public class TypeDumper {
@ -74,6 +77,7 @@ public class TypeDumper {
Iterator<DataType> it = dtm.getAllDataTypes();
while (it.hasNext()) {
DataType dt = it.next();
if (typeBlacklist.contains(dt.getDisplayName()))
continue;
@ -81,15 +85,18 @@ public class TypeDumper {
if (catPath.getPathElements().length > 0 && categoryPathBlacklist.contains(catPath.getPathElements()[0]))
continue;
// if (dt.getName().equals("ImageBaseOffset32"))
// throw new Exception("Found: " + dt.getDisplayName() + " - " +
// catPath.getPathElements()[0] + " - " + dt.getClass().getSimpleName());
if (dt instanceof Structure || dt instanceof TypeDef || dt instanceof EnumDataType
|| dt instanceof Union || dt instanceof Enum) {
if (dt instanceof Structure || dt instanceof TypeDef || dt instanceof EnumDataType) {
// script.println("Adding: " + dt.getDisplayName() + " - " +
// dt.getClass().getSimpleName());
filteredTypes.add(dt);
if (dt.getDisplayName().contains("NormalizeFn"))
script.println("DEBUG " + dt.getDisplayName() + " - " + dt.getClass().getSimpleName());
if (dt.getDisplayName().contains("tdstObjectTypeElement_") ||
dt.getDisplayName().contains("ObjectTypeElementHandle"))
filteredTypes.add(dt);
}
// }
}
String s = "";