From cec56db9d4c84de13796ce7cd7dcd2631c443cec Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Thu, 8 Sep 2016 21:49:49 +0200 Subject: Refactored argument parsing, and added stub for extractfunction. --- disasm/disasm.cpp | 126 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 101 insertions(+), 25 deletions(-) (limited to 'disasm/disasm.cpp') diff --git a/disasm/disasm.cpp b/disasm/disasm.cpp index 53c74eb..d5b1333 100644 --- a/disasm/disasm.cpp +++ b/disasm/disasm.cpp @@ -1,5 +1,6 @@ #include "dumpobject.hpp" +#include "extractfunction.hpp" #include #include @@ -8,12 +9,14 @@ #include int parse_dumpobject_options(std::vector arguments); +int parse_extractfunction_options(std::vector arguments); void print_top_level_help(std::string program_name) { std::cout << "Usage " << program_name << ":" << std::endl; std::cout << std::endl; std::cout << "\t" << program_name << " " << "help" << std::endl; std::cout << "\t" << program_name << " " << "dumpobject" << std::endl; + std::cout << "\t" << program_name << " " << "extractfunction" << std::endl; } int main(int argc, char* argv[]) { @@ -64,12 +67,61 @@ int main(int argc, char* argv[]) { if(command == "dumpobject") { return parse_dumpobject_options(options); + } else if (command == "extractfunction") { + return parse_extractfunction_options(options); } else { print_top_level_help(program_name); return 0; } } +int parse_exe_file_option(boost::program_options::variables_map const& vm, boost::filesystem::path& file_path) { + if(vm.count("exe")) { + std::string exe_file = vm["exe"].as(); + + if(boost::filesystem::exists(exe_file)) { + if(!boost::filesystem::is_directory(exe_file)) { + file_path = exe_file; + } else { + std::cerr << exe_file << " is a folder" << std::endl; + return -1; + } + } else { + std::cerr << "file: " << exe_file << " does not exist" << std::endl; + return -1; + } + } else { + std::cerr << "exe_file option is required" << std::endl; + return -1; + } + return 0; +} + +int parse_object_id_option(boost::program_options::variables_map const& vm, uint32_t& object_id) { + if(vm.count("object_id")) { + object_id = vm["object_id"].as(); + if(object_id == 0) { + std::cerr << "object id cannot be 0" << std::endl; + return -1; + } + } else { + std::cerr << "object_id option is required" << std::endl; + return -1; + } + return 0; +} + +int parse_function_offset_option(boost::program_options::variables_map const& vm, uint32_t& function_offset) { + if(vm.count("function_offset")) { + std::string str = vm["function_offset"].as(); + function_offset = std::stoul(str, nullptr, 16); + } else { + std::cerr << "function_offset option is required" << std::endl; + return -1; + } + return 0; +} + int parse_dumpobject_options(std::vector arguments) { boost::program_options::options_description description; description.add_options() @@ -94,36 +146,60 @@ int parse_dumpobject_options(std::vector arguments) { } boost::filesystem::path file_path; - if(vm.count("exe")) { - std::string exe_file = vm["exe"].as(); - - if(boost::filesystem::exists(exe_file)) { - if(!boost::filesystem::is_directory(exe_file)) { - file_path = exe_file; - } else { - std::cerr << exe_file << " is a folder" << std::endl; - std::cerr << std::endl; - std::cerr << description << std::endl; - return -1; - } - } else { - std::cerr << "file: " << exe_file << " does not exist" << std::endl; - std::cerr << std::endl; - std::cerr << description << std::endl; - return -1; - } + if(int ret = parse_exe_file_option(vm, file_path)) { + return ret; } - uint32_t object_id; - if(vm.count("object_id")) { - object_id = vm["object_id"].as(); - if(object_id == 0) { - std::cerr << "object id cannot be 0" << std::endl; - return -1; - } + binparse::Value32 object_id; + if(int ret =parse_object_id_option(vm, object_id)) { + return ret; } std::ifstream file_stream(file_path.string()); dump_object(file_stream, object_id); } + +int parse_extractfunction_options(std::vector arguments) { + boost::program_options::options_description description; + description.add_options() + ("help,h", "produces this help message") + ("exe,e", boost::program_options::value()->required(), "The LE executable to parse the header for.") + ("object_id,o", boost::program_options::value()->required(), "The object number to disassemble") + ("function_offset,f", boost::program_options::value()->required(), "The function start offset in hexadecimal notation") + ; + + boost::program_options::variables_map vm; + boost::program_options::store(boost::program_options::command_line_parser(arguments).options(description).run(), vm); + + if(vm.count("help")) { + std::cout << description << std::endl; + return 0; + } + + try { + boost::program_options::notify(vm); + } catch(boost::program_options::required_option const& e) { + std::cout << e.what() << std::endl; + return -1; + } + + boost::filesystem::path file_path; + if(int ret = parse_exe_file_option(vm, file_path)) { + return ret; + } + + binparse::Value32 object_id; + if(int ret =parse_object_id_option(vm, object_id)) { + return ret; + } + + binparse::Offset32 function_offset; + if(int ret = parse_function_offset_option(vm, function_offset)) { + return ret; + } + + std::ifstream file_stream(file_path.string()); + + extract_function(file_stream, object_id, function_offset); +} -- cgit v1.2.3-70-g09d2