summaryrefslogtreecommitdiff
path: root/le
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2016-06-22 00:15:08 +0200
committerDennis Brentjes <d.brentjes@gmail.com>2016-06-22 00:16:28 +0200
commit22231518b9c2c0b7f73c72a6ca834df659c63c7f (patch)
tree12b1d20efd3303583649966ddfe135a81ae8109b /le
parent46145fe7d5b1d9f0921121a088a3d61f968cc473 (diff)
downloadopenwar-22231518b9c2c0b7f73c72a6ca834df659c63c7f.tar.gz
openwar-22231518b9c2c0b7f73c72a6ca834df659c63c7f.tar.bz2
openwar-22231518b9c2c0b7f73c72a6ca834df659c63c7f.zip
Reduces the amount of boilerplate neccesary to parse the binary format.
Diffstat (limited to 'le')
-rw-r--r--le/CMakeLists.txt2
-rw-r--r--le/le_file.cpp46
-rw-r--r--le/le_file.hpp21
-rw-r--r--le/le_header.cpp35
-rw-r--r--le/le_header.hpp14
-rw-r--r--le/le_object_table.cpp23
-rw-r--r--le/le_object_table.hpp17
-rw-r--r--le/le_object_table_entry.cpp31
-rw-r--r--le/le_object_table_entry.hpp42
9 files changed, 155 insertions, 76 deletions
diff --git a/le/CMakeLists.txt b/le/CMakeLists.txt
index 5d849b3..c41d730 100644
--- a/le/CMakeLists.txt
+++ b/le/CMakeLists.txt
@@ -2,6 +2,8 @@
add_library(le STATIC
le_header.hpp le_header.cpp
le_file.hpp le_file.cpp
+ le_object_table.hpp le_object_table.cpp
+ le_object_table_entry.hpp le_object_table_entry.cpp
)
target_include_directories(le
diff --git a/le/le_file.cpp b/le/le_file.cpp
index 3cec50c..b01f13e 100644
--- a/le/le_file.cpp
+++ b/le/le_file.cpp
@@ -1,6 +1,7 @@
#include "le_file.hpp"
-#include "index_list.hpp"
+#include "parse.hpp"
+#include "output.hpp"
#include <boost/fusion/adapted/struct.hpp>
@@ -8,20 +9,22 @@ typedef std::array<uint8_t, 8> A1;
typedef std::array<uint8_t, 22> A2;
BOOST_FUSION_ADAPT_STRUCT(
- le::LEFile,
- (mz::MZHeader, mz_header)
+ le::File,
+ (mz::Header, mz_header)
(A1, unused_1)
(binparse::Value16, OEM_id)
(A2, OEM_info)
(binparse::Offset32, le_offset)
(std::vector<uint8_t>, dos_exe)
- (le::LEHeader, le_header)
- (std::vector<uint8_t>, le_exe)
+ (le::Header, le_header)
+ (le::ObjectTable, object_table)
)
namespace le {
-LEFile parse_file(std::istream& is) {
+File parse_file(std::istream& is) {
+ using binparse::parse;
+
auto mz_h = mz::parse_header(is);
auto unused = parse<std::array<uint8_t, 8>>(is, "unused_1");
auto OEM_id = parse<binparse::Value16>(is, "OEM_id");
@@ -33,8 +36,6 @@ LEFile parse_file(std::istream& is) {
binparse::dump_bytes(is, dos_exe, "dos_exe");
auto le_h = le::parse_header(is);
- std::string str(std::istreambuf_iterator<char>(is), {});
- std::vector<uint8_t> le_exe(str.begin(), str.end());
return {
mz_h,
@@ -44,37 +45,12 @@ LEFile parse_file(std::istream& is) {
le_offset,
dos_exe,
le_h,
- le_exe
};
}
-std::ostream& operator<<(std::ostream& os, std::array<uint8_t, 8> const& array) {
- return os;
-}
-
-std::ostream& operator<<(std::ostream& os, std::array<uint8_t, 22> const& array) {
- return os;
-}
-
-std::ostream& operator<<(std::ostream& os, std::vector<uint8_t> vec) {
- return os;
-}
-
-std::ostream& output_impl(std::ostream& os, LEFile const&, indices<>) {
- return os;
-}
-
-template <int I, int... Indices>
-std::ostream& output_impl(std::ostream& os, const LEFile& file, indices<I, Indices...>) {
- os << boost::fusion::extension::struct_member_name<LEFile, I>::call() << ": " << boost::fusion::at_c<I>(file) << std::endl;
- return output_impl(os, file, indices<Indices...>{});
-}
-
-std::ostream&operator<<(std::ostream& os, LEFile const& file)
+std::ostream&operator<<(std::ostream& os, File const& file)
{
- typedef build_indices<boost::fusion::result_of::size<LEFile>::value>::type indices;
-
- return output_impl(os, file, indices{});
+ return binparse::operator<<(os, file);
}
} \ No newline at end of file
diff --git a/le/le_file.hpp b/le/le_file.hpp
index 010c49b..56857c5 100644
--- a/le/le_file.hpp
+++ b/le/le_file.hpp
@@ -1,8 +1,10 @@
#pragma once
-#include "binparse.hpp"
+#include "types.hpp"
+
#include "mz_header.hpp"
#include "le_header.hpp"
+#include "le_object_table.hpp"
#include <cstdint>
#include <cstddef>
@@ -12,22 +14,23 @@
namespace le {
-using namespace binparse;
+using binparse::Value16;
+using binparse::Offset32;
-struct LEFile
+struct File
{
- mz::MZHeader mz_header;
+ mz::Header mz_header;
std::array<uint8_t, 8> unused_1;
Value16 OEM_id;
std::array<uint8_t, 22> OEM_info;
Offset32 le_offset;
std::vector<uint8_t> dos_exe;
- LEHeader le_header;
- std::vector<uint8_t> le_exe;
+ Header le_header;
+ ObjectTable object_table;
};
-LEFile parse_file(std::istream& is);
+File parse_file(std::istream& is);
-std::ostream& operator<<(std::ostream& os, LEFile const& file);
+std::ostream& operator<<(std::ostream& os, File const& file);
-}
+} \ No newline at end of file
diff --git a/le/le_header.cpp b/le/le_header.cpp
index f28cc4d..b2bd7bd 100644
--- a/le/le_header.cpp
+++ b/le/le_header.cpp
@@ -1,13 +1,13 @@
#include "le_header.hpp"
-#include "index_list.hpp"
-#include "binparse.hpp"
+#include "parse.hpp"
+#include "output.hpp"
#include <boost/fusion/adapted/struct.hpp>
BOOST_FUSION_ADAPT_STRUCT(
- le::LEHeader,
+ le::Header,
(binparse::Magic16, magic)
(binparse::Value8, B_ord)
(binparse::Value8, W_ord)
@@ -58,35 +58,16 @@ BOOST_FUSION_ADAPT_STRUCT(
namespace le {
-template <int... Indices>
-LEHeader parse_header_impl(std::istream& is, indices<Indices...>) {
-
- return {parse<typename std::decay<typename boost::fusion::result_of::at_c<LEHeader, Indices>::type>::type>(is, boost::fusion::extension::struct_member_name<LEHeader, Indices>::call())...};
+Header parse_header(std::istream& is) {
+ using binparse::parse;
+ return parse<Header>(is);
}
-LEHeader parse_header(std::istream& is) {
-
- typedef build_indices<boost::fusion::result_of::size<LEHeader>::value>::type indices;
-
- return parse_header_impl(is, indices{});
-}
-std::ostream& output_impl(std::ostream& os, LEHeader const&, indices<>) {
- return os;
-}
-
-template <int I, int... Indices>
-std::ostream& output_impl(std::ostream& os, const LEHeader& header, indices<I, Indices...>) {
- os << boost::fusion::extension::struct_member_name<LEHeader, I>::call() << ": " << boost::fusion::at_c<I>(header) << std::endl;
- return output_impl(os, header, indices<Indices...>{});
-}
-
-std::ostream& operator<<(std::ostream& os, LEHeader const& header)
+std::ostream& operator<<(std::ostream& os, Header const& header)
{
- typedef build_indices<boost::fusion::result_of::size<LEHeader>::value>::type indices;
-
- return output_impl(os, header, indices{});
+ return binparse::operator<<(os, header);
}
} \ No newline at end of file
diff --git a/le/le_header.hpp b/le/le_header.hpp
index 5d070b6..656b6b4 100644
--- a/le/le_header.hpp
+++ b/le/le_header.hpp
@@ -5,8 +5,6 @@
namespace le {
-using namespace binparse;
-
struct NotALEFileException : public std::runtime_error {
NotALEFileException();
};
@@ -51,7 +49,13 @@ enum class module_flags {
per_process_library_temrination = 0x40000000,
};
-struct LEHeader {
+using binparse::Magic16;
+using binparse::Value8;
+using binparse::Value16;
+using binparse::Value32;
+using binparse::Offset32;
+
+struct Header {
Magic16 magic;
Value8 B_ord;
Value8 W_ord;
@@ -100,8 +104,8 @@ struct LEHeader {
Value32 heapsize;
};
-LEHeader parse_header(std::istream& is);
+Header parse_header(std::istream& is);
-std::ostream& operator<<(std::ostream& os, LEHeader const& header);
+std::ostream& operator<<(std::ostream& os, Header const& header);
} \ No newline at end of file
diff --git a/le/le_object_table.cpp b/le/le_object_table.cpp
new file mode 100644
index 0000000..1155c35
--- /dev/null
+++ b/le/le_object_table.cpp
@@ -0,0 +1,23 @@
+#include "le_object_table.hpp"
+
+namespace le {
+
+ObjectTable parse_object_table(std::istream& is, Offset32 offset, Value32 nr_objects)
+{
+ is.seekg(0, std::ios::beg);
+ is.ignore(offset);
+
+ ObjectTable table;
+
+ for(Value32 i = Value32(0); i < nr_objects; i++) {
+ table.entries.emplace(i+1, parse_object_table_entry(is));
+ }
+ return table;
+}
+
+std::ostream& operator<<(std::ostream& os, const ObjectTable& table)
+{
+ return os;
+}
+
+} \ No newline at end of file
diff --git a/le/le_object_table.hpp b/le/le_object_table.hpp
new file mode 100644
index 0000000..8c57c60
--- /dev/null
+++ b/le/le_object_table.hpp
@@ -0,0 +1,17 @@
+#pragma once
+
+#include "le_object_table_entry.hpp"
+
+#include <map>
+
+namespace le {
+
+struct ObjectTable {
+ std::map<uint32_t, ObjectTableEntry> entries;
+};
+
+ObjectTable parse_object_table(std::istream& is, Offset32 offset, Value32 nr_objects);
+
+std::ostream& operator<<(std::ostream& os, ObjectTable const& table);
+
+} \ No newline at end of file
diff --git a/le/le_object_table_entry.cpp b/le/le_object_table_entry.cpp
new file mode 100644
index 0000000..92810aa
--- /dev/null
+++ b/le/le_object_table_entry.cpp
@@ -0,0 +1,31 @@
+#include "le_object_table_entry.hpp"
+
+#include "parse.hpp"
+#include "output.hpp"
+
+#include <boost/fusion/adapted/struct.hpp>
+
+BOOST_FUSION_ADAPT_STRUCT(
+ le::ObjectTableEntry,
+ (binparse::Value32, virtual_size)
+ (binparse::Offset32, reloc_base_address)
+ (binparse::Value32, ojbect_flags)
+ (binparse::Value32, page_table_index)
+ (binparse::Value32, nr_page_table_entries)
+ (binparse::Value32, reserved)
+)
+
+namespace le {
+
+ObjectTableEntry parse_object_table_entry(std::istream& is) {
+ using binparse::parse;
+
+ return parse<ObjectTableEntry>(is);
+}
+
+std::ostream& operator<<(std::ostream& os, const ObjectTableEntry& entry)
+{
+ return binparse::operator<<(os, entry);
+}
+
+}
diff --git a/le/le_object_table_entry.hpp b/le/le_object_table_entry.hpp
new file mode 100644
index 0000000..7b97873
--- /dev/null
+++ b/le/le_object_table_entry.hpp
@@ -0,0 +1,42 @@
+#pragma once
+
+#include "types.hpp"
+
+namespace le {
+
+using binparse::Value32;
+using binparse::Offset32;
+
+enum class ObjectFlags {
+ readable = 0x1,
+ writable = 0x2,
+ executable = 0x4,
+ resource = 0x8,
+ discardable = 0x10,
+ shared = 0x20,
+ has_preload_pages = 0x40,
+ has_invalid_pages = 0x80,
+ has_zero_filled_pages = 0x100,
+ is_resident = 0x200,
+ is_resident_and_contiguous = 0x300,
+ is_resident_and_long_lockable = 0x400,
+ alias_16_16_required = 0x1000,
+ big_bit_setting = 0x2000,
+ is_conforming_for_code = 0x4000,
+ IO_privilege_level = 0x8000,
+};
+
+struct ObjectTableEntry {
+ Value32 virtual_size;
+ Offset32 reloc_base_address;
+ Value32 ojbect_flags;
+ Value32 page_table_index;
+ Value32 nr_page_table_entries;
+ Value32 reserved;
+};
+
+ObjectTableEntry parse_object_table_entry(std::istream& is);
+
+std::ostream& operator<<(std::ostream& os, ObjectTableEntry const& entry);
+
+}