From 35c7dd8a90847f69229fa089138d88bc9905ed0d Mon Sep 17 00:00:00 2001 From: crypto Date: Sun, 12 Sep 2021 08:40:15 +0300 Subject: [PATCH] Bridge to IDA: errors fixed --- Infos.cpp | 9 +++++-- Main.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++---- TypeInfo.cpp | 31 +++++++++++++--------- 3 files changed, 94 insertions(+), 19 deletions(-) diff --git a/Infos.cpp b/Infos.cpp index 180983d..599b52a 100644 --- a/Infos.cpp +++ b/Infos.cpp @@ -1636,7 +1636,12 @@ String __fastcall InfoRec::MakeCppPrototype(int Adr, String FType) if (kind == ikFunc) { if (type != "") - result = TrimTypeName(type); + { + result = SanitizeName(TrimTypeName(type)); + typeKind = GetTypeKind(result, &size); + if (typeKind == ikRecord || typeKind == ikVMT) + result = "struct " + result + "*"; + } else result = "DWORD"; } @@ -1659,7 +1664,7 @@ String __fastcall InfoRec::MakeCppPrototype(int Adr, String FType) typeKind = GetTypeKind(argType, &size); if (typeKind == ikRecord || typeKind == ikVMT) result += "struct "; - result += argType; + result += SanitizeName(argType); if (typeKind == ikVMT) result += "*"; } diff --git a/Main.cpp b/Main.cpp index 3003dc9..1f6e744 100644 --- a/Main.cpp +++ b/Main.cpp @@ -13575,6 +13575,7 @@ CPPvsDELPHIdecl ForwardDeclarations[] = { {"wchar_t*", "string"}, {"wchar_t*", "WideString"}, {"wchar_t*", "UString"}, + {"wchar_t*", "UnicodeString"}, {"wchar_t", "Char"}, {"wchar_t", "WideChar"}, {"void*", "Pointer"}, @@ -13720,6 +13721,7 @@ void __fastcall TFMain_11011981::OutputForwardDeclarationsOfKind(FILE* hF, BYTE break; case ikRecord: case ikClass: + case ikMethod: fprintf(hF, "struct %s;\n", RTTIName.c_str()); break; case ikPointer: @@ -13730,8 +13732,6 @@ void __fastcall TFMain_11011981::OutputForwardDeclarationsOfKind(FILE* hF, BYTE break; case ikProcedure: break; - case ikMethod: - break; case ikInterface: fprintf(hF, "struct %s_vt\n", RTTIName.c_str()); fprintf(hF, "{\n"); @@ -13749,16 +13749,41 @@ void __fastcall TFMain_11011981::OutputForwardDeclarationsOfKind(FILE* hF, BYTE } } //--------------------------------------------------------------------------- +typedef struct _VMT_PROC +{ + DWORD Adr; + int CurIdx; + BYTE Multiple; +} VMT_PROC, *PVMT_PROC; +//--------------------------------------------------------------------------- +PVMT_PROC __fastcall GetProcFromVmtList(TList* list, DWORD procAdr) +{ + PVMT_PROC Result = 0; + + for (int n = 0; n < list->Count; n++) + { + PVMT_PROC vmtProc = (PVMT_PROC)list->Items[n]; + if (vmtProc->Adr == procAdr) + { + Result = vmtProc; + break; + } + } + return Result; +} +//--------------------------------------------------------------------------- void __fastcall TFMain_11011981::CreateCppHeaderFile(FILE* hF) { BYTE len, RTTIKind; - int n, m, id, adr, kind, pos, size, sort, virtNum; + int n, m, id, adr, kind, pos, size, sort, virtNum, idx; PUnitRec recU; PInfoRec recN; String unitName, RTTIName, str, name; PFIELDINFO fInfo; TList* virtList; + TList* vmtProcs; PMethodRec recM; + PVMT_PROC vmtProc; //Save sort style sort = RTTISortField; @@ -13788,6 +13813,8 @@ void __fastcall TFMain_11011981::CreateCppHeaderFile(FILE* hF) OutputForwardDeclarationsOfKind(hF, ikClass); fprintf(hF, "//\n"); OutputForwardDeclarationsOfKind(hF, ikRecord); + fprintf(hF, "//\n"); + OutputForwardDeclarationsOfKind(hF, ikMethod); fprintf(hF, "//\n"); OutputForwardDeclarationsOfKind(hF, ikInterface); //Restore old sort style @@ -13913,6 +13940,28 @@ void __fastcall TFMain_11011981::CreateCppHeaderFile(FILE* hF) //Output virtual functions virtList = new TList; virtNum = LoadVirtualTable(adr, virtList); + //Fill VMT_PROCS list + vmtProcs = new TList; + for (m = 0, id = 0; m < virtNum; m++) + { + recM = (PMethodRec)virtList->Items[m]; + if (recM->id >= 0) + { + vmtProc = GetProcFromVmtList(vmtProcs, recM->address); + if (!vmtProc) + { + vmtProc = new VMT_PROC; + vmtProc->Adr = recM->address; + vmtProc->CurIdx = 0; + vmtProc->Multiple = 0; + vmtProcs->Add((void*)vmtProc); + } + else + { + vmtProc->Multiple = 1; + } + } + } //Output function prorotype declarations for (m = 0, id = 0; m < virtNum; m++) { @@ -13943,17 +13992,30 @@ void __fastcall TFMain_11011981::CreateCppHeaderFile(FILE* hF) { name = recM->name; recN = GetInfoRec(recM->address); + vmtProc = GetProcFromVmtList(vmtProcs, recM->address); + idx = -1; + if (vmtProc && vmtProc->Multiple) + { + idx = vmtProc->CurIdx; + vmtProc->CurIdx++; + } if (recN) { if (name == "") name = recN->GetName(); - fprintf(hF, "P%s_m%lX sub_%08lX;", RTTIName.c_str(), id, recM->address); + fprintf(hF, "P%s_m%lX %s_sub_%08lX", RTTIName.c_str(), id, RTTIName.c_str(), recM->address); + if (idx >= 0) + fprintf(hF, "_%d", idx); + fprintf(hF, ";"); if (name != "") fprintf(hF, "//%s", name.c_str()); } else { - fprintf(hF, "P%s_m%lX sub_%08lX;", RTTIName.c_str(), id, recM->address); + fprintf(hF, "P%s_m%lX %s_sub_%08lX", RTTIName.c_str(), id, RTTIName.c_str(), recM->address); + if (idx >= 0) + fprintf(hF, "_%d", idx); + fprintf(hF, ";"); if (name != "") fprintf(hF, "//%s", name.c_str()); } @@ -13962,6 +14024,7 @@ void __fastcall TFMain_11011981::CreateCppHeaderFile(FILE* hF) } } fprintf(hF, "};\n\n"); + delete vmtProcs; delete virtList; //Output fields diff --git a/TypeInfo.cpp b/TypeInfo.cpp index 215f0bd..0967ff2 100644 --- a/TypeInfo.cpp +++ b/TypeInfo.cpp @@ -1420,7 +1420,7 @@ String __fastcall TFTypeInfo_11011981::GetCppTypeInfo(DWORD adr, int* o_pSize, i typeKind = GetTypeKind(typname, &size); if (typeKind == ikVMT || typeKind == ikProcedure) result += "struct "; - result += typname; + result += SanitizeName(typname); if (typeKind == ikVMT) result += "*"; if (paramFlags & PfVar) @@ -1440,9 +1440,9 @@ String __fastcall TFTypeInfo_11011981::GetCppTypeInfo(DWORD adr, int* o_pSize, i } typeKind = GetTypeKind(name, &size); if (typeKind == ikVMT) - result = name + "* " + result; + result = SanitizeName(name) + "* " + result; else - result = name + " " + result; + result = SanitizeName(name) + " " + result; } if (DelphiVersion > 6) { @@ -1486,7 +1486,7 @@ String __fastcall TFTypeInfo_11011981::GetCppTypeInfo(DWORD adr, int* o_pSize, i typeKind = GetTypeKind(typname, &size); if (typeKind == ikVMT || typeKind == ikProcedure) result += "struct "; - result += typname; + result += SanitizeName(typname); if (typeKind == ikVMT) result += "*"; if (paramFlags & PfVar) @@ -1502,9 +1502,9 @@ String __fastcall TFTypeInfo_11011981::GetCppTypeInfo(DWORD adr, int* o_pSize, i typname = GetTypeName(resultTypeAdr); typeKind = GetTypeKind(typname, &size); if (typeKind == ikVMT) - result = typname + "* " + result; + result = SanitizeName(typname) + "* " + result; else - result = typname + " " + result; + result = SanitizeName(typname) + " " + result; } } } @@ -1713,7 +1713,7 @@ String __fastcall TFTypeInfo_11011981::GetCppTypeInfo(DWORD adr, int* o_pSize, i if (typeKind == ikInterface) size = 4; if (typeKind == ikRecord || typeKind == ikVMT) result += "struct "; - result += FieldInfo->Type; + result += SanitizeName(FieldInfo->Type); if (typeKind == ikVMT) result += "*"; } @@ -1830,7 +1830,7 @@ String __fastcall TFTypeInfo_11011981::GetCppTypeInfo(DWORD adr, int* o_pSize, i pos += 4; //elType elType = *((DWORD*)(Code + pos)); pos += 4; - result = GetTypeName(elType); + result = SanitizeName(GetTypeName(elType)); //varType pos += 4; if (DelphiVersion >= 6) @@ -1845,7 +1845,7 @@ String __fastcall TFTypeInfo_11011981::GetCppTypeInfo(DWORD adr, int* o_pSize, i { //DynArrElType elType = *((DWORD*)(Code + pos)); - result = GetTypeName(elType); + result = SanitizeName(GetTypeName(elType)); } break; case ikUString: @@ -1898,7 +1898,7 @@ String __fastcall TFTypeInfo_11011981::GetCppTypeInfo(DWORD adr, int* o_pSize, i typeKind = GetTypeKind(typname, &size); if (typeKind == ikRecord || typeKind == ikVMT) result += "struct "; - result += typname; + result += SanitizeName(typname); if (typeKind == ikVMT) result += "*"; if (paramFlags & 1) result += "*"; @@ -1910,7 +1910,14 @@ String __fastcall TFTypeInfo_11011981::GetCppTypeInfo(DWORD adr, int* o_pSize, i result += ")"; if (resultTypeAdr) - result = GetTypeName(resultTypeAdr) + " " + result; + { + typname = GetTypeName(resultTypeAdr); + typeKind = GetTypeKind(typname, &size); + if (typeKind == ikRecord || typeKind == ikVMT) + result = "struct " + SanitizeName(typname) + "* " + result; + else + result = SanitizeName(typname) + " " + result; + } else result = "void " + result; } @@ -1972,7 +1979,7 @@ String __fastcall TFTypeInfo_11011981::GetCppTypeInfo(DWORD adr, int* o_pSize, i if (typeKind == ikInterface) size = 4; if (typeKind == ikRecord || typeKind == ikVMT) result += "struct "; - result += fInfo->Type; + result += SanitizeName(fInfo->Type); if (typeKind == ikVMT) result += "*"; }