summaryrefslogtreecommitdiff
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
parent46145fe7d5b1d9f0921121a088a3d61f968cc473 (diff)
downloadopenwar-22231518b9c2c0b7f73c72a6ca834df659c63c7f.tar.gz
openwar-22231518b9c2c0b7f73c72a6ca834df659c63c7f.tar.bz2
openwar-22231518b9c2c0b7f73c72a6ca834df659c63c7f.zip
Reduces the amount of boilerplate neccesary to parse the binary format.
-rw-r--r--binparse/CMakeLists.txt7
-rw-r--r--binparse/output.cpp61
-rw-r--r--binparse/output.hpp57
-rw-r--r--binparse/parse.cpp (renamed from binparse/binparse.cpp)42
-rw-r--r--binparse/parse.hpp (renamed from binparse/binparse.hpp)43
-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
-rw-r--r--mz/mz_header.cpp38
-rw-r--r--mz/mz_header.hpp12
16 files changed, 321 insertions, 170 deletions
diff --git a/binparse/CMakeLists.txt b/binparse/CMakeLists.txt
index 00fc246..cbc83b5 100644
--- a/binparse/CMakeLists.txt
+++ b/binparse/CMakeLists.txt
@@ -1,7 +1,8 @@
add_library(binparse STATIC
types.hpp
- binparse.hpp binparse.cpp
+ parse.hpp parse.cpp
+ output.hpp output.cpp
)
target_include_directories(binparse
@@ -10,4 +11,8 @@ target_include_directories(binparse
target_compile_options(binparse
PUBLIC "-std=c++14"
+)
+
+target_link_libraries(binparse
+ PRIVATE fusion-utils
) \ No newline at end of file
diff --git a/binparse/output.cpp b/binparse/output.cpp
new file mode 100644
index 0000000..78babd8
--- /dev/null
+++ b/binparse/output.cpp
@@ -0,0 +1,61 @@
+#include "output.hpp"
+
+#include <iomanip>
+
+namespace binparse {
+
+std::string to_string(Magic16 magic) {
+ char* c = reinterpret_cast<char*>(&magic);
+ return std::string(c, sizeof(Magic16));
+}
+
+std::ostream& operator<<(std::ostream& os, uint8_t i) {
+ return os << (unsigned int) i;
+}
+
+std::ostream& operator<<(std::ostream& os, Value8 v)
+{
+ return os << std::dec << static_cast<uint8_t>(v);
+}
+
+std::ostream& operator<<(std::ostream& os, Offset8 o)
+{
+ return os << std::hex << std::setw(2) << std::setfill('0') << static_cast<uint8_t>(o);
+}
+
+std::ostream& operator<<(std::ostream& os, Magic16 m) {
+ return os << std::hex << std::setw(4) << std::setfill('0') << static_cast<uint16_t>(m) << " (" << to_string(m) << ")";
+}
+
+std::ostream& operator<<(std::ostream& os, Value16 v) {
+ return os << std::dec << static_cast<uint16_t>(v);
+}
+
+std::ostream& operator<<(std::ostream& os, Offset16 o) {
+ return os << std::hex << std::setw(sizeof(o) * 2) << std::setfill('0') << static_cast<uint16_t>(o);
+}
+
+std::ostream& operator<<(std::ostream& os, Value32 v) {
+ return os << std::dec << static_cast<uint32_t>(v);
+}
+
+std::ostream& operator<<(std::ostream& os, Offset32 o) {
+ return os << std::hex << std::setw(sizeof(o) * 2) << std::setfill('0') << static_cast<uint32_t>(o);
+}
+
+std::ostream&operator<<(std::ostream& os, std::array<uint8_t, 8> a)
+{
+ return os;
+}
+
+std::ostream&operator<<(std::ostream& os, std::array<uint8_t, 22> a)
+{
+ return os;
+}
+
+std::ostream&operator<<(std::ostream& os, std::vector<uint8_t> vec)
+{
+ return os;
+}
+
+}
diff --git a/binparse/output.hpp b/binparse/output.hpp
new file mode 100644
index 0000000..b16dd89
--- /dev/null
+++ b/binparse/output.hpp
@@ -0,0 +1,57 @@
+#pragma once
+
+#include "types.hpp"
+
+#include "index_list.hpp"
+
+#include <boost/fusion/support/is_sequence.hpp>
+#include <boost/fusion/sequence/intrinsic/at_c.hpp>
+#include <boost/fusion/adapted/struct/detail/extension.hpp>
+
+#include <array>
+#include <vector>
+
+namespace binparse {
+
+std::string to_string(Magic16 magic);
+
+//overload inside this namespace to output unsigned char as value not as characters.
+std::ostream& operator<<(std::ostream& os, uint8_t);
+
+std::ostream& operator<<(std::ostream& os, Value8 v);
+std::ostream& operator<<(std::ostream& os, Offset8 o);
+
+std::ostream& operator<<(std::ostream& os, Magic16 m);
+std::ostream& operator<<(std::ostream& os, Value16 v);
+std::ostream& operator<<(std::ostream& os, Offset16 o);
+
+std::ostream& operator<<(std::ostream& os, Value32 v);
+std::ostream& operator<<(std::ostream& os, Offset32 o);
+
+std::ostream& operator<<(std::ostream& os, std::array<uint8_t, 8> a);
+std::ostream& operator<<(std::ostream& os, std::array<uint8_t, 22> a);
+
+std::ostream& operator<<(std::ostream& os, std::vector<uint8_t> vec);
+
+template <typename T, typename std::enable_if<boost::fusion::traits::is_sequence<T>::value>::type* = nullptr>
+std::ostream& operator<<(std::ostream&, T const&);
+
+template<typename T>
+std::ostream& output_impl(std::ostream& os, T const&, indices<>) {
+ return os;
+}
+
+template <typename T, int I, int... Indices>
+std::ostream& output_impl(std::ostream& os, T const& t, indices<I, Indices...>) {
+ os << boost::fusion::extension::struct_member_name<T, I>::call() << ": " << boost::fusion::at_c<I>(t) << std::endl;
+ return output_impl(os, t, indices<Indices...>{});
+}
+
+template <typename T, typename std::enable_if<boost::fusion::traits::is_sequence<T>::value>::type*>
+std::ostream& operator<<(std::ostream& os, T const& t) {
+ typedef typename build_indices<boost::fusion::result_of::size<T>::value>::type indices;
+
+ return output_impl<T>(os, t, indices{});
+}
+
+} \ No newline at end of file
diff --git a/binparse/binparse.cpp b/binparse/parse.cpp
index eae6143..cf7c0be 100644
--- a/binparse/binparse.cpp
+++ b/binparse/parse.cpp
@@ -1,5 +1,4 @@
-
-#include "binparse.hpp"
+#include "parse.hpp"
namespace binparse {
@@ -135,43 +134,4 @@ void dump_bytes(std::istream& is, std::vector<uint8_t>& buffer, std::string name
}
}
-std::string to_string(Magic16 magic) {
- char* c = reinterpret_cast<char*>(&magic);
- return std::string(c, sizeof(Magic16));
-}
-
-std::ostream& operator<<(std::ostream& os, uint8_t i) {
- return os << (unsigned int) i;
-}
-
-std::ostream& operator<<(std::ostream& os, Value8 v)
-{
- return os << std::dec << static_cast<uint8_t>(v);
-}
-
-std::ostream& operator<<(std::ostream& os, Offset8 o)
-{
- return os << std::hex << std::setw(2) << std::setfill('0') << static_cast<uint8_t>(o);
-}
-
-std::ostream& operator<<(std::ostream& os, Magic16 m) {
- return os << std::hex << std::setw(4) << std::setfill('0') << static_cast<uint16_t>(m) << " (" << to_string(m) << ")";
-}
-
-std::ostream& operator<<(std::ostream& os, Value16 v) {
- return os << std::dec << static_cast<uint16_t>(v);
-}
-
-std::ostream& operator<<(std::ostream& os, Offset16 o) {
- return os << std::hex << std::setw(sizeof(o) * 2) << std::setfill('0') << static_cast<uint16_t>(o);
-}
-
-std::ostream& operator<<(std::ostream& os, Value32 v) {
- return os << std::dec << static_cast<uint32_t>(v);
-}
-
-std::ostream& operator<<(std::ostream& os, Offset32 o) {
- return os << std::hex << std::setw(sizeof(o) * 2) << std::setfill('0') << static_cast<uint32_t>(o);
-}
-
} \ No newline at end of file
diff --git a/binparse/binparse.hpp b/binparse/parse.hpp
index e649ce7..8015ba3 100644
--- a/binparse/binparse.hpp
+++ b/binparse/parse.hpp
@@ -1,8 +1,15 @@
+#pragma once
+
#include "types.hpp"
-#include <string>
-#include <ostream>
-#include <iomanip>
+#include "index_list.hpp"
+
+#include <boost/fusion/support/is_sequence.hpp>
+#include <boost/fusion/sequence/intrinsic/at_c.hpp>
+#include <boost/fusion/adapted/struct/detail/extension.hpp>
+
+#include <stdexcept>
+#include <array>
#include <vector>
namespace binparse {
@@ -13,7 +20,7 @@ struct UnexpectedEOS : public std::runtime_error {
UnexpectedEOS(std::string location);
};
-template<typename T>
+template<typename T, typename std::enable_if<!boost::fusion::traits::is_sequence<T>::value>::type* = nullptr>
T parse(std::istream& is, std::string name);
template<>
@@ -46,21 +53,23 @@ std::array<uint8_t, 8> parse<std::array<uint8_t, 8>>(std::istream& is, std::stri
template<>
std::array<uint8_t, 22> parse<std::array<uint8_t, 22>>(std::istream& is, std::string name);
-void dump_bytes(std::istream& is, std::vector<uint8_t>& buffer, std::string name);
-
-std::string to_string(Magic16 magic);
+template <typename T, typename std::enable_if<boost::fusion::traits::is_sequence<T>::value>::type* = nullptr>
+T parse(std::istream& is);
-//overload inside this namespace to output unsigned char as value not as characters.
-std::ostream& operator<<(std::ostream& os, uint8_t);
-
-std::ostream& operator<<(std::ostream& os, Value8 v);
-std::ostream& operator<<(std::ostream& os, Offset8 o);
+template <typename T, int... Indices>
+T parse_impl(std::istream& is, indices<Indices...>) {
+
+ return {parse<typename std::decay<typename boost::fusion::result_of::at_c<T, Indices>::type>::type>(is, boost::fusion::extension::struct_member_name<T, Indices>::call())...};
+
+}
-std::ostream& operator<<(std::ostream& os, Magic16 m);
-std::ostream& operator<<(std::ostream& os, Value16 v);
-std::ostream& operator<<(std::ostream& os, Offset16 o);
+template <typename T, typename std::enable_if<boost::fusion::traits::is_sequence<T>::value>::type*>
+T parse(std::istream& is) {
+ typedef typename build_indices<boost::fusion::result_of::size<T>::value>::type indices;
+
+ return parse_impl<T>(is, indices{});
+}
-std::ostream& operator<<(std::ostream& os, Value32 v);
-std::ostream& operator<<(std::ostream& os, Offset32 o);
+void dump_bytes(std::istream& is, std::vector<uint8_t>& buffer, std::string name);
} \ No newline at end of file
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);
+
+}
diff --git a/mz/mz_header.cpp b/mz/mz_header.cpp
index 02241cf..c33270f 100644
--- a/mz/mz_header.cpp
+++ b/mz/mz_header.cpp
@@ -1,18 +1,16 @@
#include "mz_header.hpp"
-#include "index_list.hpp"
-
-#include "binparse.hpp"
+#include "parse.hpp"
+#include "output.hpp"
#include <boost/fusion/adapted/struct.hpp>
-#include <boost/fusion/adapted/struct/detail/extension.hpp>
#include <vector>
#include <iomanip>
BOOST_FUSION_ADAPT_STRUCT(
- mz::MZHeader,
+ mz::Header,
(binparse::Magic16, magic)
(binparse::Value16, bytes_in_last_block)
(binparse::Value16, blocks_in_file)
@@ -35,35 +33,15 @@ NotAMZFileException::NotAMZFileException()
: std::runtime_error("This stream does not contain a valid MZ executable")
{}
-template <int... Indices>
-MZHeader parse_header_impl(std::istream& is, indices<Indices...>) {
-
- return {parse<typename std::decay<typename boost::fusion::result_of::at_c<MZHeader, Indices>::type>::type>(is, boost::fusion::extension::struct_member_name<MZHeader, Indices>::call())...};
-
-}
-
-MZHeader parse_header(std::istream& is) {
-
- typedef build_indices<boost::fusion::result_of::size<MZHeader>::value>::type indices;
+Header parse_header(std::istream& is) {
+ using binparse::parse;
- return parse_header_impl(is, indices{});
+ return parse<Header>(is);
}
-std::ostream& output_impl(std::ostream& os, MZHeader const&, indices<>) {
- return os;
-}
-
-template <int I, int... Indices>
-std::ostream& output_impl(std::ostream& os, const MZHeader& header, indices<I, Indices...>) {
- os << boost::fusion::extension::struct_member_name<MZHeader, I>::call() << ": " << boost::fusion::at_c<I>(header) << std::endl;
- return output_impl(os, header, indices<Indices...>{});
-}
-
-std::ostream& operator<<(std::ostream& os, MZHeader const& header)
+std::ostream& operator<<(std::ostream& os, Header const& header)
{
- typedef build_indices<boost::fusion::result_of::size<MZHeader>::value>::type indices;
-
- return output_impl(os, header, indices{});
+ return binparse::operator<<(os, header);
}
}
diff --git a/mz/mz_header.hpp b/mz/mz_header.hpp
index 9bef928..c783335 100644
--- a/mz/mz_header.hpp
+++ b/mz/mz_header.hpp
@@ -10,8 +10,6 @@
namespace mz {
-using namespace binparse;
-
struct NotAMZFileException : public std::runtime_error {
NotAMZFileException();
};
@@ -22,7 +20,11 @@ struct UnexpectedEOS : public std::runtime_error {
UnexpectedEOS(std::string location);
};
-struct MZHeader {
+using binparse::Magic16;
+using binparse::Value16;
+using binparse::Offset16;
+
+struct Header {
Magic16 magic;
Value16 bytes_in_last_block;
Value16 blocks_in_file;
@@ -39,8 +41,8 @@ struct MZHeader {
Value16 overlay_number;
};
-MZHeader parse_header(std::istream& is);
+Header parse_header(std::istream& is);
-std::ostream& operator<<(std::ostream& os, MZHeader const& header);
+std::ostream& operator<<(std::ostream& os, Header const& header);
}