From 6fd8e5ee43dd0f87eb4e57b86e84770fcd95db25 Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Thu, 17 Aug 2023 00:35:58 -0600 Subject: Added StormLib, nlohmann, BinaryTools and CLI11 --- lib/binarytools/BinaryReader.cpp | 193 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 lib/binarytools/BinaryReader.cpp (limited to 'lib/binarytools/BinaryReader.cpp') diff --git a/lib/binarytools/BinaryReader.cpp b/lib/binarytools/BinaryReader.cpp new file mode 100644 index 0000000..3aff001 --- /dev/null +++ b/lib/binarytools/BinaryReader.cpp @@ -0,0 +1,193 @@ +#include "BinaryReader.h" +#include "MemoryStream.h" +#include +#include + +LUS::BinaryReader::BinaryReader(char* nBuffer, size_t nBufferSize) { + mStream = std::make_shared(nBuffer, nBufferSize); +} + +LUS::BinaryReader::BinaryReader(Stream* nStream) { + mStream.reset(nStream); +} + +LUS::BinaryReader::BinaryReader(std::shared_ptr nStream) { + mStream = nStream; +} + +void LUS::BinaryReader::Close() { + mStream->Close(); +} + +void LUS::BinaryReader::SetEndianness(Endianness endianness) { + this->mEndianness = endianness; +} + +LUS::Endianness LUS::BinaryReader::GetEndianness() const { + return mEndianness; +} + +void LUS::BinaryReader::Seek(int32_t offset, SeekOffsetType seekType) { + mStream->Seek(offset, seekType); +} + +uint32_t LUS::BinaryReader::GetBaseAddress() { + return mStream->GetBaseAddress(); +} + +void LUS::BinaryReader::Read(int32_t length) { + mStream->Read(length); +} + +void LUS::BinaryReader::Read(char* buffer, int32_t length) { + mStream->Read(buffer, length); +} + +char LUS::BinaryReader::ReadChar() { + return (char)mStream->ReadByte(); +} + +int8_t LUS::BinaryReader::ReadInt8() { + return mStream->ReadByte(); +} + +int16_t LUS::BinaryReader::ReadInt16() { + int16_t result = 0; + mStream->Read((char*)&result, sizeof(int16_t)); + if (mEndianness != Endianness::Native) { + result = BSWAP16(result); + } + + return result; +} + +int32_t LUS::BinaryReader::ReadInt32() { + int32_t result = 0; + + mStream->Read((char*)&result, sizeof(int32_t)); + + if (mEndianness != Endianness::Native) { + result = BSWAP32(result); + } + + return result; +} + +uint8_t LUS::BinaryReader::ReadUByte() { + return (uint8_t)mStream->ReadByte(); +} + +uint16_t LUS::BinaryReader::ReadUInt16() { + uint16_t result = 0; + + mStream->Read((char*)&result, sizeof(uint16_t)); + + if (mEndianness != Endianness::Native) { + result = BSWAP16(result); + } + + return result; +} + +uint32_t LUS::BinaryReader::ReadUInt32() { + uint32_t result = 0; + + mStream->Read((char*)&result, sizeof(uint32_t)); + + if (mEndianness != Endianness::Native) { + result = BSWAP32(result); + } + + return result; +} + +uint64_t LUS::BinaryReader::ReadUInt64() { + uint64_t result = 0; + + mStream->Read((char*)&result, sizeof(uint64_t)); + + if (mEndianness != Endianness::Native) { + result = BSWAP64(result); + } + + return result; +} + +float LUS::BinaryReader::ReadFloat() { + float result = NAN; + + mStream->Read((char*)&result, sizeof(float)); + + if (mEndianness != Endianness::Native) { + float tmp; + char* dst = (char*)&tmp; + char* src = (char*)&result; + dst[3] = src[0]; + dst[2] = src[1]; + dst[1] = src[2]; + dst[0] = src[3]; + result = tmp; + } + + if (std::isnan(result)) { + throw std::runtime_error("BinaryReader::ReadFloat(): Error reading stream"); + } + + return result; +} + +double LUS::BinaryReader::ReadDouble() { + double result = NAN; + + mStream->Read((char*)&result, sizeof(double)); + + if (mEndianness != Endianness::Native) { + double tmp; + char* dst = (char*)&tmp; + char* src = (char*)&result; + dst[7] = src[0]; + dst[6] = src[1]; + dst[5] = src[2]; + dst[4] = src[3]; + dst[3] = src[4]; + dst[2] = src[5]; + dst[1] = src[6]; + dst[0] = src[7]; + result = tmp; + } + + if (std::isnan(result)) { + throw std::runtime_error("BinaryReader::ReadDouble(): Error reading stream"); + } + + return result; +} + +std::string LUS::BinaryReader::ReadString() { + std::string res; + int numChars = ReadInt32(); + for (int i = 0; i < numChars; i++) { + res += ReadChar(); + } + return res; +} + +std::string LUS::BinaryReader::ReadCString() { + std::string res; + + unsigned char c = 0; + do { + if (mStream->GetBaseAddress() >= mStream->GetLength()) { + break; + } + + c = ReadChar(); + res += c; + } while (c != '\0'); + + return res; +} + +std::vector LUS::BinaryReader::ToVector() { + return mStream->ToVector(); +} -- cgit v1.2.3