summaryrefslogtreecommitdiff
path: root/binparse
diff options
context:
space:
mode:
Diffstat (limited to 'binparse')
-rw-r--r--binparse/output.cpp8
-rw-r--r--binparse/output.hpp2
-rw-r--r--binparse/parse.cpp35
-rw-r--r--binparse/parse.hpp6
-rw-r--r--binparse/types.hpp4
5 files changed, 40 insertions, 15 deletions
diff --git a/binparse/output.cpp b/binparse/output.cpp
index 16447a7..de9db29 100644
--- a/binparse/output.cpp
+++ b/binparse/output.cpp
@@ -35,6 +35,13 @@ 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, Value24 v)
+{
+ std::array<uint8_t, 3> va = v;
+ uint32_t tv = (va[2] << 16 | va[1] << 8 | va[0] << 0);
+ return os << std::dec << tv;
+}
+
std::ostream& operator<<(std::ostream& os, Value32 v) {
return os << std::dec << static_cast<uint32_t>(v);
}
@@ -63,4 +70,5 @@ std::ostream&operator<<(std::ostream& os, std::vector<uint8_t> vec)
return os;
}
+
}
diff --git a/binparse/output.hpp b/binparse/output.hpp
index 3919bef..d942fca 100644
--- a/binparse/output.hpp
+++ b/binparse/output.hpp
@@ -29,6 +29,8 @@ 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, Value24 v);
+
std::ostream& operator<<(std::ostream& os, Value32 v);
std::ostream& operator<<(std::ostream& os, Offset32 o);
diff --git a/binparse/parse.cpp b/binparse/parse.cpp
index a26e72e..de0f04d 100644
--- a/binparse/parse.cpp
+++ b/binparse/parse.cpp
@@ -80,6 +80,12 @@ Offset16 parse<Offset16>(std::istream& is, std::string name) {
}
template<>
+Value24 parse<Value24>(std::istream& is, std::__cxx11::string name)
+{
+ return Value24(parse<std::array<uint8_t, 3>>(is, name));
+}
+
+template<>
Value32 parse<Value32>(std::istream& is, std::string name) {
return Value32(parse<uint32_t>(is, name));
}
@@ -97,13 +103,13 @@ PString8 parse<PString8>(std::istream& is, std::string name) {
return PString8(std::string(data.begin(), data.end()));
}
-template<>
-std::array<uint8_t, 8> parse<std::array<uint8_t, 8>>(std::istream& is, std::string name) {
+template<typename T, unsigned int N>
+std::array<T, N> parse(std::istream& is, std::string name) {
if(!is) {
throw UnexpectedEOS();
}
- std::array<uint8_t, 8> buffer;
+ std::array<T, N> buffer;
is.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
if(!is) {
@@ -114,19 +120,18 @@ std::array<uint8_t, 8> parse<std::array<uint8_t, 8>>(std::istream& is, std::stri
}
template<>
+std::array<uint8_t, 3> parse<std::array<uint8_t, 3>>(std::istream& is, std::string name) {
+ return parse<uint8_t, 3>(is, name);
+}
+
+template<>
+std::array<uint8_t, 8> parse<std::array<uint8_t, 8>>(std::istream& is, std::string name) {
+ return parse<uint8_t, 8>(is, name);
+}
+
+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;
+ return parse<uint8_t, 22>(is, name);
}
void dump_bytes(std::istream& is, std::vector<uint8_t>& buffer, std::string name)
diff --git a/binparse/parse.hpp b/binparse/parse.hpp
index e02b230..5c641d8 100644
--- a/binparse/parse.hpp
+++ b/binparse/parse.hpp
@@ -49,6 +49,9 @@ template<>
Offset16 parse<Offset16>(std::istream& is, std::string name);
template<>
+Value24 parse<Value24>(std::istream& is, std::string name);
+
+template<>
Value32 parse<Value32>(std::istream& is, std::string name);
template<>
@@ -58,6 +61,9 @@ template<>
PString8 parse<PString8>(std::istream& is, std::string name);
template<>
+std::array<uint8_t, 3> parse<std::array<uint8_t, 3>>(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<>
diff --git a/binparse/types.hpp b/binparse/types.hpp
index d1b1910..5d7f7d7 100644
--- a/binparse/types.hpp
+++ b/binparse/types.hpp
@@ -3,6 +3,7 @@
#include <boost/serialization/strong_typedef.hpp>
#include <cstdint>
+#include <array>
namespace binparse {
@@ -19,6 +20,9 @@ BOOST_STRONG_TYPEDEF(uint32_t, Value32)
BOOST_STRONG_TYPEDEF(std::string, PString8);
+typedef std::array<uint8_t, 3> uint24_t;
+BOOST_STRONG_TYPEDEF(uint24_t, Value24);
+
template <typename T>
T operator+(T const lh, T const rh) {
return T(0 + lh + rh);