From cea325b7451c1fb8dd22462ec2e7b5b88ea9b547 Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Sun, 19 Jun 2016 20:23:07 +0200 Subject: Adds an LE executable parser. Adds the binparse library that holds common components for the MZ and LE parsers. --- binparse/binparse.cpp | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 binparse/binparse.cpp (limited to 'binparse/binparse.cpp') diff --git a/binparse/binparse.cpp b/binparse/binparse.cpp new file mode 100644 index 0000000..1dc4fc6 --- /dev/null +++ b/binparse/binparse.cpp @@ -0,0 +1,132 @@ + +#include "binparse.hpp" + +namespace binparse { + +UnexpectedEOS::UnexpectedEOS() +: std::runtime_error("Unexpected end of stream.") +{} + +UnexpectedEOS::UnexpectedEOS(std::__cxx11::string location) +: std::runtime_error("Unexpected end of stream after " + location) +{} + +template<> +uint8_t parse(std::istream& is, std::string name) { + if(!is) { + throw UnexpectedEOS(); + } + char data = 0; + is.read(&data, 1); + if(!is) { + throw UnexpectedEOS(name); + } + + return (unsigned char) data; +} + +template<> +uint16_t parse(std::istream& is, std::string name) { + if(!is) { + throw UnexpectedEOS(); + } + char data[2] = {0, 0}; + is.read(data, 2); + if(!is) { + throw UnexpectedEOS(name); + } + + return ( + ((unsigned char) data[1]) << 8) + | (unsigned char) data[0]; +} + +template<> +uint32_t parse(std::istream& is, std::string name) { + if(!is) { + throw UnexpectedEOS(); + } + char data[4] = {0, 0}; + is.read(data, sizeof(data)); + if(!is) { + throw UnexpectedEOS(name); + } + + return ( + ((unsigned char) data[3]) << 24 + | ((unsigned char) data[2]) << 16 + | ((unsigned char) data[1]) << 8 + | ((unsigned char) data[0]) + ); +} + +template<> +Value8 parse(std::istream& is, std::string name) { + return Value8(parse(is, name)); +} + +template<> +Value16 parse(std::istream& is, std::string name) { + return Value16(parse(is, name)); +} + +template<> +Magic16 parse(std::istream& is, std::string name) { + return Magic16(parse(is, name)); +} + +template<> +Offset16 parse(std::istream& is, std::string name) { + return Offset16(parse(is, name)); +} + +template<> +Value32 parse(std::istream& is, std::string name) { + return Value32(parse(is, name)); +} + +template<> +Offset32 parse(std::istream& is, std::string name) { + return Offset32(parse(is, name)); +} + +std::string to_string(Magic16 magic) { + char* c = reinterpret_cast(&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(v); +} + +std::ostream& operator<<(std::ostream& os, Offset8 o) +{ + return os << std::hex << std::setw(2) << std::setfill('0') << static_cast(o); +} + +std::ostream& operator<<(std::ostream& os, Magic16 m) { + return os << std::hex << std::setw(4) << std::setfill('0') << static_cast(m) << " (" << to_string(m) << ")"; +} + +std::ostream& operator<<(std::ostream& os, Value16 v) { + return os << std::dec << static_cast(v); +} + +std::ostream& operator<<(std::ostream& os, Offset16 o) { + return os << std::hex << std::setw(sizeof(o) * 2) << std::setfill('0') << static_cast(o); +} + +std::ostream& operator<<(std::ostream& os, Value32 v) { + return os << std::dec << static_cast(v); +} + +std::ostream& operator<<(std::ostream& os, Offset32 o) { + return os << std::hex << std::setw(sizeof(o) * 2) << std::setfill('0') << static_cast(o); +} + +} \ No newline at end of file -- cgit v1.2.3-70-g09d2