From deaa8bd4382f2f6b5418dc28a5062ab8f2fba3e8 Mon Sep 17 00:00:00 2001 From: DrFrugal Date: Mon, 22 Jan 2024 03:29:57 +0100 Subject: [PATCH] some little refactoring to reduce code redundancy --- GhidraParser/GhidraParser.cpp | 58 +++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/GhidraParser/GhidraParser.cpp b/GhidraParser/GhidraParser.cpp index c6c3cea..af71ccf 100644 --- a/GhidraParser/GhidraParser.cpp +++ b/GhidraParser/GhidraParser.cpp @@ -13,7 +13,7 @@ std::string file_path; bool dbg; bool dbg_prnt_invld_only; -bool use_all = false; // 46 invalid parses +bool use_all = true; // 46 invalid parses FunctionInfo fi; std::string func; @@ -168,12 +168,12 @@ void sort_prune_varmap(std::map> *varmap) __forceinline bool ln_is(std::string &ln, int idx, const char* lk_for) { int len = strlen(lk_for); - return ln.length() - idx > len && ln.substr(idx, len) == lk_for; + return ln.length() - idx + 1 > len && ln.substr(idx, len) == lk_for; } -void smp_ln_proc(std::string &ln, int i) +__forceinline void set_str_if_empty(std::string *init, std::string repl) { - + *init = init->empty() ? repl : *init; } int main() @@ -206,7 +206,6 @@ int main() file_path = R"(C:\Users\alphaomega\Documents\Wow.exe.c.test.txt)"; } - auto tstart = std::chrono::high_resolution_clock::now(); std::ifstream source_file(file_path); @@ -216,18 +215,10 @@ int main() return 1; } - //std::regex return_regex(R"(^\s+?return\s+([^;]+);$)"); - //std::regex usage_regex(R"(^.+"(Usage: [^;]+)N{0}"\){0,1};$)"); // N{0} is just a workaround, since )" in the raw string would terminate it immediately std::regex usage_regex(R"(,"(Usage: [^;]+)N{0}"[, \)])"); // N{0} is just a workaround, since )" in the raw string would terminate it immediately - //std::regex lua_is_regex(R"(lua_is(.+?)\(L,(.+?)\))"); - //std::regex lua_to_regex(R"(lua_to(.+?)\(L,(.+?)[,\)])"); std::regex lua_is_regex(R"(lua_is(.+?)\()"); std::regex lua_to_regex(R"(lua_to(.+?)\()"); - - - //bool found_return_after_push; - std::string skp_push_utl = ""; // skip push parsing until this line has been reached std::string lp_utl = ""; std::map push_track; @@ -246,13 +237,13 @@ int main() if (!in_func) { // searching for next function if (ln.starts_with("// ADDRESS - ")) - { // found address ln - create new FunctionInfo + { // found address ln - create new FunctionInfo and reset variables fi = {}; fi.address = parse_int(ln.substr(13)); out_index = 0; enc_usg = false; - skp_push_utl = ""; lp_utl = ""; + skp_push_utl = ""; push_track = {}; continue; } @@ -292,9 +283,13 @@ int main() { case 'c': case 'd': + if (ln_is(ln, i, "do {")) + { // setting loop end, if not already in a loop + set_str_if_empty(&lp_utl, ind + "}"); + } if (ln_is(ln, i, "case ") || ln_is(ln, i, "default:")) { - if (push_track[ind.length() / 2]) skp_push_utl = !skp_push_utl.empty() ? skp_push_utl : ind + "}"; + if (push_track[ind.length() / 2]) set_str_if_empty(&skp_push_utl, ind + "}"); else push_track[ind.length() / 2] = false; // only execute if a previous case didn't already contain a push break; } @@ -310,14 +305,14 @@ int main() case 'e': if (ln_is(ln, i, "else ")) { // enough characters left to be if statement - if (push_track[ind.length() / 2]) skp_push_utl = !skp_push_utl.empty() ? skp_push_utl : ln[ln.length() - 1] == '{' ? ind + "}" : "\1"; + if (push_track[ind.length() / 2]) set_str_if_empty(&skp_push_utl, ln[ln.length() - 1] == '{' ? ind + "}" : "\1"); break; } break; case 'g': if (!fi.param_out.empty() && ln_is(ln, i, "goto ")) { - skp_push_utl = !skp_push_utl.empty() ? skp_push_utl : ln.substr(i + 5, ln.length() - i - 6) + ":"; + set_str_if_empty(&skp_push_utl, ln.substr(i + 5, ln.length() - i - 6) + ":"); break; } break; @@ -332,12 +327,18 @@ int main() fi.param_out_cnt = (fi.param_out_cnt == 0 || fi.param_out_cnt == ret_val) ? ret_val : -1; if (ret_val == fi.param_out.size()) { // found a return statement and return value matches output param count - skp_push_utl = !skp_push_utl.empty() ? skp_push_utl : "\1skip2end"; + set_str_if_empty(&skp_push_utl, "\1skip2end"); } } break; } break; + case 'w': + if (ln_is(ln, i, "while (")) + { // setting loop end, if not already in a loop + set_str_if_empty(&lp_utl, ind + "}"); + } + break; } //pdbg(ln); //pdbg(ind + "<-"); @@ -358,10 +359,14 @@ int main() process_varmap_regex(&fi.param_in, ln, &lua_is_regex); process_varmap_regex(&fi.param_in, ln, &lua_to_regex); } - if (fi.param_out_cnt != -1 && skp_push_utl.empty()) + if (fi.param_out_cnt != -1 && skp_push_utl.empty() && process_varmap_regex(&fi.param_out, ln, &lua_push_regex)) { - if (process_varmap_regex(&fi.param_out, ln, &lua_push_regex)) - { + if (!lp_utl.empty()) + { // found lua_push* inside a loop + fi.param_out_cnt = -1; + } + else + { // normal processing int lvl = is_if_ln ? ind.length() / 2 : (ind.length() - 2) / 2; // do not subtract 1 block level, if this was a simple if line while (lvl >= 1) { // if a push was found, track it for this and all lower block levels @@ -369,13 +374,12 @@ int main() lvl--; } } - } - if (!skp_push_utl.empty()) - { - int i = 0; + } - if (skp_push_utl == ln) + if (!lp_utl.empty() && ln.starts_with(lp_utl)) + lp_utl = ""; + if (!skp_push_utl.empty() && ln.starts_with(skp_push_utl)) skp_push_utl = ""; // reset skip since line has been reached now if (skp_push_utl == "\1") skp_push_utl = ""; // reset temporary skip which was used for 1 line