#pragma once #include #include #include #include "decompiler/unknown_opcode_exception.h" class BinaryReader { public: static std::vector ReadAll(std::string fileName) { std::ifstream file(fileName.c_str(), std::ios::binary | std::ios::ate); if (file.is_open()) { size_t fileSizeInBytes = size_t(file.tellg()); std::vector fileContents(fileSizeInBytes); file.seekg(0, std::ios::beg); file.read(reinterpret_cast(fileContents.data()), fileContents.size()); return fileContents; } else { throw std::runtime_error("Can't open file"); } } BinaryReader(std::vector&& data) { mSize = data.size(); std::copy(data.begin(), data.end(), std::ostream_iterator(mStream)); mStream.seekg(std::ios::beg); } size_t Size() const { return mSize; } void Seek(unsigned int pos) { if (!mStream.seekg(pos)) { throw InternalDecompilerError(); } } unsigned int Position() { return static_cast(mStream.tellg()); } unsigned int ReadU32() { return InternalRead(); } signed int ReadS32() { return InternalRead(); } signed short int ReadS16() { return InternalRead(); } unsigned short int ReadU16() { return InternalRead(); } unsigned char ReadU8() { return InternalRead(); } signed char ReadS8() { return InternalRead(); } private: template T InternalRead() { T r = {}; if (!mStream.read(reinterpret_cast(&r), sizeof(r))) { throw InternalDecompilerError(); } return r; } std::stringstream mStream; size_t mSize = 0; };