Last active
April 12, 2021 12:51
-
-
Save Journeyman1337/82a6c25c5254a5eca05a6882250f4250 to your computer and use it in GitHub Desktop.
Template class that can read binary data from an array and from a file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2021 Daniel Valcour | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
the Software, and to permit persons to whom the Software is furnished to do so, | |
subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
#pragma once | |
#include "util.h" | |
#include <istream> | |
#include <string> | |
#include <cstdint> | |
#include <vector> | |
#include <filesystem> | |
#include <memory> | |
/// <summary> | |
/// Buffer of bytes from an array. | |
/// </summary> | |
class membuf : public std::basic_streambuf<char> { | |
public: | |
membuf(const uint8_t* p, size_t l) { | |
setg((char*)p, (char*)p, (char*)p + l); | |
} | |
}; | |
/// <summary> | |
/// Overloaded istream for reading an from an array of bytes. | |
/// </summary> | |
class memstream : public std::istream { | |
public: | |
memstream(const uint8_t* p, size_t l) : | |
std::istream(&_buffer), | |
_buffer(p, l) { | |
rdbuf(&_buffer); | |
} | |
private: | |
membuf _buffer; | |
}; | |
enum DataEndian | |
{ | |
BigEndian, | |
LittleEndian | |
}; | |
template<DataEndian DE = BigEndian> | |
class BinaryReader | |
{ | |
private: | |
std::unique_ptr<std::istream> in_stream; | |
size_t size; | |
size_t pos; | |
public: | |
BinaryReader() = default; | |
~BinaryReader() = default; | |
BinaryReader(const BinaryReader& other) = delete; | |
void OpenFile(const std::string& path) | |
{ | |
auto f_path = std::filesystem::path(path); | |
if (!std::filesystem::exists(f_path)) | |
{ | |
throw util::runtime_error("File not found at path \"{}\".", path); | |
} | |
this->in_stream = std::make_unique<std::ifstream>(path); | |
this->size = std::filesystem::file_size(f_path); | |
} | |
void OpenBytes(std::byte* bytes, size_t count) | |
{ | |
if (bytes == nullptr || count == 0) | |
{ | |
throw util::runtime_error("Invalid byte array to stream."); | |
} | |
this->in_stream = std::make_unique<memstream>(reinterpret_cast<uint8_t*>(bytes), count); | |
this->size = count; | |
} | |
void Close() | |
{ | |
this->in_stream.release(); | |
this->size = 0; | |
this->pos = 0; | |
} | |
bool IsOpen() | |
{ | |
return this->in_stream.get() != nullptr; | |
} | |
template<typename T> | |
T Read() | |
{ | |
const size_t size = sizeof(T); | |
static_assert(size == 1 || size == 2 || size == 4 || size == 8, "Can not read type of unsupported size using BinaryReader. Supported sizes byte sizes are 1, 2, 4, and 8."); | |
if (this->pos + size > this->size) | |
{ | |
throw std::runtime_error("Reading binary data out of range."); | |
} | |
char dat[size]; | |
this->in_stream->read(dat, size); | |
this->pos += size; | |
if (DE == BigEndian) | |
{ | |
switch (size) | |
{ | |
case 1: | |
return (dat[0]); | |
case 2: | |
return (dat[1] << 0) | (dat[0] << 8); | |
case 4: | |
return (dat[3] << 0) | (dat[2] << 8) | (dat[1] << 16) | (dat[0] << 24); | |
case 8: | |
return (dat[7] << 0) | (dat[6] << 8) | (dat[5] << 16) | (dat[4] << 24) | (dat[3] << 32) | (dat[2] << 40) | (dat[1] << 48) | (dat[0] << 56); | |
} | |
} | |
else | |
{ | |
switch (size) | |
{ | |
case 1: | |
return (dat[0]); | |
case 2: | |
return (dat[0] << 0) | (dat[1] << 8); | |
case 4: | |
return (dat[0] << 0) | (dat[1] << 8) | (dat[2] << 16) | (dat[3] << 24); | |
case 8: | |
return (dat[0] << 0) | (dat[1] << 8) | (dat[2] << 16) | (dat[3] << 24) | (dat[4] << 32) | (dat[5] << 40) | (dat[6] << 48) | (dat[7] << 56); | |
} | |
} | |
return 0; | |
} | |
void ReadBytes(std::byte* bytes, size_t count) | |
{ | |
if (this->pos + count >= this->size) | |
{ | |
throw std::runtime_error("Reading binary data out of range."); | |
} | |
this->in_stream->read(bytes, count); | |
this->pos += count; | |
} | |
std::string ReadString(size_t size = 0) | |
{ | |
if (this->pos + size >= this->size) | |
{ | |
throw std::runtime_error("Reading binary data out of range."); | |
} | |
auto ret = std::string(size, ' '); | |
this->in_stream->read(ret.data(), size); | |
this->pos += size; | |
return ret; | |
} | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2021 Daniel Valcour | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
the Software, and to permit persons to whom the Software is furnished to do so, | |
subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
#include "util.h" | |
#include "BinaryReader.h" | |
int main() | |
{ | |
try | |
{ | |
char testchars[8]; | |
uint64_t* test = reinterpret_cast<uint64_t*>(testchars); | |
*test = 70424; | |
auto reader = BinaryReader<LittleEndian>(); | |
reader.OpenBytes(reinterpret_cast<std::byte*>(testchars), sizeof(testchars)); | |
uint64_t out = reader.Read<uint64_t>(); | |
util::info("{}", out); | |
} | |
catch (std::exception e) | |
{ | |
util::critical(e.what()); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70424 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The only endianess that matters is the endianess of the data that is passed in, not the endianess of the machine. Since the test case passes in a variable from the machine, the result varies based on the machine. If this was a big endian machine, the output would be a different number. The template type argument endianess is the type of endian that it reads from. It always writes to machine endianess. If a file or byte array was read that is always a certain endian, the output would always be the same no matter the machine.