summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2016-06-19 22:05:32 +0200
committerDennis Brentjes <d.brentjes@gmail.com>2016-06-19 22:05:32 +0200
commitde95ca22ec87cc8b79ceb7beba475301461713a6 (patch)
treedce723a717ac0ba583de992777d7d968c3be870b
parentcea325b7451c1fb8dd22462ec2e7b5b88ea9b547 (diff)
downloadopenwar-de95ca22ec87cc8b79ceb7beba475301461713a6.tar.gz
openwar-de95ca22ec87cc8b79ceb7beba475301461713a6.tar.bz2
openwar-de95ca22ec87cc8b79ceb7beba475301461713a6.zip
adds an LE file parser and refactors some code.
-rw-r--r--binparse/binparse.cpp45
-rw-r--r--binparse/binparse.hpp9
-rw-r--r--le/CMakeLists.txt2
-rw-r--r--le/le_header.cpp6
-rw-r--r--le/le_header.hpp2
-rw-r--r--le/le_header_parser.cpp2
-rw-r--r--mz/mz_header.cpp6
-rw-r--r--mz/mz_header.hpp2
-rw-r--r--mz/mz_header_parser.cpp2
9 files changed, 66 insertions, 10 deletions
diff --git a/binparse/binparse.cpp b/binparse/binparse.cpp
index 1dc4fc6..eae6143 100644
--- a/binparse/binparse.cpp
+++ b/binparse/binparse.cpp
@@ -90,6 +90,51 @@ Offset32 parse<Offset32>(std::istream& is, std::string name) {
return Offset32(parse<uint32_t>(is, name));
}
+template<>
+std::array<uint8_t, 8> parse<std::array<uint8_t, 8>>(std::istream& is, std::string name) {
+ if(!is) {
+ throw UnexpectedEOS();
+ }
+
+ std::array<uint8_t, 8> buffer;
+ is.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
+
+ if(!is) {
+ throw UnexpectedEOS(name);
+ }
+
+ return buffer;
+}
+
+template<>
+std::array<uint8_t, 22> parse<std::array<uint8_t, 22>>(std::istream& is, std::string name) {
+ if(!is) {
+ throw UnexpectedEOS();
+ }
+
+ std::array<uint8_t, 22> buffer;
+ is.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
+
+ if(!is) {
+ throw UnexpectedEOS(name);
+ }
+
+ return buffer;
+}
+
+void dump_bytes(std::istream& is, std::vector<uint8_t>& buffer, std::string name)
+{
+ if(!is) {
+ throw UnexpectedEOS();
+ }
+
+ is.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
+
+ if(!is) {
+ throw UnexpectedEOS(name);
+ }
+}
+
std::string to_string(Magic16 magic) {
char* c = reinterpret_cast<char*>(&magic);
return std::string(c, sizeof(Magic16));
diff --git a/binparse/binparse.hpp b/binparse/binparse.hpp
index 1620ab5..5b21086 100644
--- a/binparse/binparse.hpp
+++ b/binparse/binparse.hpp
@@ -3,6 +3,7 @@
#include <string>
#include <ostream>
#include <iomanip>
+#include <vector>
namespace binparse {
@@ -39,6 +40,14 @@ Value32 parse<Value32>(std::istream& is, std::string name);
template<>
Offset32 parse<Offset32>(std::istream& is, std::string name);
+template<>
+std::array<uint8_t, 8> parse<std::array<uint8_t, 8>>(std::istream& is, std::string name);
+
+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);
//overload inside this namespace to output unsigned char as value not as characters.
diff --git a/le/CMakeLists.txt b/le/CMakeLists.txt
index a4887c5..646f68b 100644
--- a/le/CMakeLists.txt
+++ b/le/CMakeLists.txt
@@ -1,6 +1,7 @@
add_library(le STATIC
le_header.hpp le_header.cpp
+ le_file.hpp le_file.cpp
)
target_include_directories(le
@@ -10,6 +11,7 @@ target_include_directories(le
target_link_libraries(le
PUBLIC fusion-utils
PUBLIC binparse
+ PUBLIC mz
)
add_executable(le_header_parser
diff --git a/le/le_header.cpp b/le/le_header.cpp
index a429add..f28cc4d 100644
--- a/le/le_header.cpp
+++ b/le/le_header.cpp
@@ -59,17 +59,17 @@ BOOST_FUSION_ADAPT_STRUCT(
namespace le {
template <int... Indices>
-LEHeader parse_file_impl(std::istream& is, indices<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())...};
}
-LEHeader parse_file(std::istream& is) {
+LEHeader parse_header(std::istream& is) {
typedef build_indices<boost::fusion::result_of::size<LEHeader>::value>::type indices;
- return parse_file_impl(is, indices{});
+ return parse_header_impl(is, indices{});
}
std::ostream& output_impl(std::ostream& os, LEHeader const&, indices<>) {
diff --git a/le/le_header.hpp b/le/le_header.hpp
index 9501127..5d070b6 100644
--- a/le/le_header.hpp
+++ b/le/le_header.hpp
@@ -100,7 +100,7 @@ struct LEHeader {
Value32 heapsize;
};
-LEHeader parse_file(std::istream& is);
+LEHeader parse_header(std::istream& is);
std::ostream& operator<<(std::ostream& os, LEHeader const& header);
diff --git a/le/le_header_parser.cpp b/le/le_header_parser.cpp
index 524c60d..e8c7096 100644
--- a/le/le_header_parser.cpp
+++ b/le/le_header_parser.cpp
@@ -50,7 +50,7 @@ int main(int argc, char* argv[]) {
std::ifstream file(file_path.string());
file.seekg(0x2aa8);
- auto x = le::parse_file(file);
+ auto x = le::parse_header(file);
std::cout << x << std::endl;
return 0;
diff --git a/mz/mz_header.cpp b/mz/mz_header.cpp
index 323c9cd..02241cf 100644
--- a/mz/mz_header.cpp
+++ b/mz/mz_header.cpp
@@ -36,17 +36,17 @@ NotAMZFileException::NotAMZFileException()
{}
template <int... Indices>
-MZHeader parse_file_impl(std::istream& is, indices<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_file(std::istream& is) {
+MZHeader parse_header(std::istream& is) {
typedef build_indices<boost::fusion::result_of::size<MZHeader>::value>::type indices;
- return parse_file_impl(is, indices{});
+ return parse_header_impl(is, indices{});
}
std::ostream& output_impl(std::ostream& os, MZHeader const&, indices<>) {
diff --git a/mz/mz_header.hpp b/mz/mz_header.hpp
index fd42b43..9bef928 100644
--- a/mz/mz_header.hpp
+++ b/mz/mz_header.hpp
@@ -39,7 +39,7 @@ struct MZHeader {
Value16 overlay_number;
};
-MZHeader parse_file(std::istream& is);
+MZHeader parse_header(std::istream& is);
std::ostream& operator<<(std::ostream& os, MZHeader const& header);
diff --git a/mz/mz_header_parser.cpp b/mz/mz_header_parser.cpp
index 66718d1..6010331 100644
--- a/mz/mz_header_parser.cpp
+++ b/mz/mz_header_parser.cpp
@@ -49,7 +49,7 @@ int main(int argc, char* argv[]) {
}
std::ifstream file(file_path.string());
- auto x = mz::parse_file(file);
+ auto x = mz::parse_header(file);
std::cout << x << std::endl;
return 0;