Skip to content

Commit

Permalink
Rename util functions, rename pdb data json names, simplify code in m…
Browse files Browse the repository at this point in the history
…ain function
  • Loading branch information
xezon committed Oct 13, 2024
1 parent b7dec7c commit 000b98a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
18 changes: 7 additions & 11 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ class Runner
for (const unassemblize::ExeSymbol &symbol : m_executable.get_symbols()) {
std::string sanitized_symbol_name = symbol.name;
#if defined(WIN32)
util::remove_characters(sanitized_symbol_name, "\\/:*?\"<>|");
util::remove_characters_inplace(sanitized_symbol_name, "\\/:*?\"<>|");
#endif
std::string file_name;
if (!o.output_file.empty()) {
// program.symbol.S
file_name = util::get_remove_file_ext(o.output_file) + "." + sanitized_symbol_name + ".S";
file_name = util::get_file_name_without_ext(o.output_file) + "." + sanitized_symbol_name + ".S";
}
dump_function_to_file(
file_name, m_executable, o.section_name.c_str(), symbol.address, symbol.address + symbol.size);
Expand Down Expand Up @@ -213,7 +213,7 @@ std::string get_config_file_name(const std::string &input_file, const std::strin
{
if (0 == strcasecmp(config_file.c_str(), auto_str)) {
// program.config.json
return util::get_remove_file_ext(input_file) + ".config.json";
return util::get_file_name_without_ext(input_file) + ".config.json";
}
return config_file;
}
Expand All @@ -222,7 +222,7 @@ std::string get_output_file_name(const std::string &input_file, const std::strin
{
if (0 == strcasecmp(output_file.c_str(), auto_str)) {
// program.S
return util::get_remove_file_ext(input_file) + ".S";
return util::get_file_name_without_ext(input_file) + ".S";
}
return output_file;
}
Expand Down Expand Up @@ -347,14 +347,9 @@ int main(int argc, char **argv)
}

const InputType type = get_input_type(input_file, input_type);
if (type == InputType::Unknown) {
printf("Unrecognized input file type '%s'. Exiting...\n", input_type);
return 1;
}

Runner runner;

if (InputType::Exe == type) {
Runner runner;
ExeOptions o;
o.input_file = input_file;
o.config_file = get_config_file_name(o.input_file, config_file);
Expand All @@ -368,6 +363,7 @@ int main(int argc, char **argv)
o.verbose = verbose;
return runner.process_exe(o) ? 0 : 1;
} else if (InputType::Pdb == type) {
Runner runner;
bool success;
{
PdbOptions o;
Expand All @@ -394,7 +390,7 @@ int main(int argc, char **argv)
}
return success ? 0 : 1;
} else {
// Impossible
printf("Unrecognized input file type '%s'. Exiting...\n", input_type);
return 1;
}
}
8 changes: 4 additions & 4 deletions pdbreadertypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ namespace unassemblize
void to_json(nlohmann::json &js, const PdbAddress &d)
{
js = nlohmann::json{
{"abs_virtual", d.absVirtual},
{"rel_virtual", d.relVirtual},
{"virtual_abs", d.absVirtual},
{"virtual_rel", d.relVirtual},
{"section", d.section},
{"offset", d.offset},
};
}

void from_json(const nlohmann::json &js, PdbAddress &d)
{
js.at("abs_virtual").get_to(d.absVirtual);
js.at("rel_virtual").get_to(d.relVirtual);
js.at("virtual_phy").get_to(d.absVirtual);
js.at("virtual_rel").get_to(d.relVirtual);
js.at("section").get_to(d.section);
js.at("offset").get_to(d.offset);
}
Expand Down
4 changes: 2 additions & 2 deletions util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ std::wstring to_utf16(const std::string &utf8)
return convert.from_bytes(utf8);
}

void remove_characters(std::string &s, const std::string &chars)
void remove_characters_inplace(std::string &s, const std::string &chars)
{
s.erase(
std::remove_if(s.begin(), s.end(), [&chars](const char &c) { return chars.find(c) != std::string::npos; }), s.end());
}

std::string get_remove_file_ext(const std::string &file_name)
std::string get_file_name_without_ext(const std::string &file_name)
{
const size_t pos = file_name.find_last_of(".");
if (pos != std::string::npos) {
Expand Down
4 changes: 2 additions & 2 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ std::string to_utf8(const wchar_t *utf16);
std::string to_utf8(const std::wstring &utf16);
std::wstring to_utf16(const char *utf8);
std::wstring to_utf16(const std::string &utf8);
void remove_characters(std::string &s, const std::string &chars);
std::string get_remove_file_ext(const std::string &file_name);
void remove_characters_inplace(std::string &s, const std::string &chars);
std::string get_file_name_without_ext(const std::string &file_name);
std::string get_file_path(const std::string &file_path);
std::string get_file_ext(const std::string &file_name);

Expand Down

0 comments on commit 000b98a

Please sign in to comment.