summaryrefslogtreecommitdiff
path: root/run/run.cpp
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2021-01-25 23:23:42 +0100
committerDennis Brentjes <d.brentjes@gmail.com>2021-01-25 23:23:42 +0100
commit67bfeba1035dedf98d67cb00ea89e550de673aa4 (patch)
tree547928f5efd4098f73f045e914dc7c38ca10e330 /run/run.cpp
parent3cb35ec664734cfe04bf788b3a9c11402fd0f878 (diff)
downloadopenwar-67bfeba1035dedf98d67cb00ea89e550de673aa4.tar.gz
openwar-67bfeba1035dedf98d67cb00ea89e550de673aa4.tar.bz2
openwar-67bfeba1035dedf98d67cb00ea89e550de673aa4.zip
Adds runner and 32-bit-runner to start implementing software interrupts.
Diffstat (limited to 'run/run.cpp')
-rw-r--r--run/run.cpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/run/run.cpp b/run/run.cpp
new file mode 100644
index 0000000..2f02a7b
--- /dev/null
+++ b/run/run.cpp
@@ -0,0 +1,111 @@
+#include <distorm.h>
+
+#include <boost/program_options.hpp>
+#include <boost/filesystem.hpp>
+#include <boost/asio.hpp>
+
+#include <iostream>
+#include <fstream>
+
+#include "le_file.hpp"
+#include "le_parse_util.hpp"
+
+#include "common.hpp"
+
+static boost::asio::io_context io_context;
+static le::File file;
+static boost::asio::local::stream_protocol::socket connection_socket(io_context);
+
+static uint32_t message_size;
+
+void action_handler(boost::system::error_code const& error, size_t)
+{
+ if(!error)
+ {
+ }
+
+ io_context.post([](){
+ connection_socket.async_receive(boost::asio::buffer(&message_size, sizeof message_size), 0, action_handler);
+ });
+}
+
+void accept_handler(boost::system::error_code const& error)
+{
+ if(!error)
+ {
+ std::vector<uint8_t> binary = load_binary(file);
+ uint32_t base_address = file.object_table.entries.at(file.le_header.EIP_object).reloc_base_address;
+ uint32_t binary_size = binary.size();
+ BinaryLoadMessage message {
+ .binary_size = binary_size,
+ .base_address = base_address,
+ .eip = file.le_header.EIP + base_address
+ };
+
+ connection_socket.send(boost::asio::buffer(&message, sizeof message));
+ connection_socket.send(boost::asio::buffer(binary, binary.size()));
+
+ io_context.post([](){
+ connection_socket.async_receive(boost::asio::buffer(&message_size, sizeof message_size), 0, action_handler);
+ });
+ }
+}
+
+int main(int argc, char* argv[])
+{
+ boost::program_options::options_description description;
+ description.add_options()
+ ("help,h", "produces this help message")
+ ("exe,e", boost::program_options::value<std::string>(), "The LE executable to parse the header for.")
+ ;
+
+ boost::program_options::variables_map vm;
+ boost::program_options::store(boost::program_options::parse_command_line(argc, argv, description), vm);
+ boost::program_options::notify(vm);
+
+ if(vm.count("help")) {
+ std::cout << description << std::endl;
+ return 0;
+ }
+
+ 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;
+ }
+ } else {
+ std::cerr << "Option \"exe_file\" is required";
+ std::cerr << std::endl;
+ std::cerr << description << std::endl;
+ return -1;
+ }
+
+ std::ifstream file_stream(file_path, std::ios::binary);
+ file_stream.unsetf(std::ios::skipws);
+ file = le::parse_file(file_stream);
+
+ boost::filesystem::remove(socket_name);
+
+ boost::asio::local::stream_protocol::endpoint ep(socket_name);
+ boost::asio::local::stream_protocol::acceptor acceptor(io_context, ep);
+ acceptor.async_accept(connection_socket, accept_handler);
+
+ io_context.run();
+
+ boost::filesystem::remove(socket_name);
+ return 0;
+}