summaryrefslogtreecommitdiff
path: root/disasm/disasm.cpp
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2016-09-08 21:49:49 +0200
committerDennis Brentjes <d.brentjes@gmail.com>2016-09-08 21:49:49 +0200
commitcec56db9d4c84de13796ce7cd7dcd2631c443cec (patch)
treed21847b479ee5bf4f263abd124a8dc7a3293e1cd /disasm/disasm.cpp
parent35be012af254617b72ecbe4bca718f3ce96c1fd2 (diff)
downloadopenwar-cec56db9d4c84de13796ce7cd7dcd2631c443cec.tar.gz
openwar-cec56db9d4c84de13796ce7cd7dcd2631c443cec.tar.bz2
openwar-cec56db9d4c84de13796ce7cd7dcd2631c443cec.zip
Refactored argument parsing, and added stub for extractfunction.
Diffstat (limited to 'disasm/disasm.cpp')
-rw-r--r--disasm/disasm.cpp126
1 files changed, 101 insertions, 25 deletions
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 <boost/filesystem.hpp>
#include <boost/program_options.hpp>
@@ -8,12 +9,14 @@
#include <fstream>
int parse_dumpobject_options(std::vector<std::string> arguments);
+int parse_extractfunction_options(std::vector<std::string> 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<std::string>();
+
+ 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<uint32_t>();
+ 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<std::string>();
+ 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<std::string> arguments) {
boost::program_options::options_description description;
description.add_options()
@@ -94,36 +146,60 @@ int parse_dumpobject_options(std::vector<std::string> arguments) {
}
boost::filesystem::path file_path;
- if(vm.count("exe")) {
- std::string exe_file = vm["exe"].as<std::string>();
-
- 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<uint32_t>();
- 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<std::string> arguments) {
+ boost::program_options::options_description description;
+ description.add_options()
+ ("help,h", "produces this help message")
+ ("exe,e", boost::program_options::value<std::string>()->required(), "The LE executable to parse the header for.")
+ ("object_id,o", boost::program_options::value<uint32_t>()->required(), "The object number to disassemble")
+ ("function_offset,f", boost::program_options::value<std::string>()->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);
+}