diff options
| author | GaryOderNichts <12049776+GaryOderNichts@users.noreply.github.com> | 2022-07-27 23:50:56 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-27 23:50:56 +0200 |
| commit | 07143e2d1c9e0b45e5960d87b1932ebba69166cf (patch) | |
| tree | 8b118ac34b2802d540944cfb9b3c4eef89f3f30b /ZAPDUtils/Utils/BinaryReader.cpp | |
| parent | 7c5825525f69090e7e28927d0f815d8fa1e21c93 (diff) | |
Big-endian support (#909)
Diffstat (limited to 'ZAPDUtils/Utils/BinaryReader.cpp')
| -rw-r--r-- | ZAPDUtils/Utils/BinaryReader.cpp | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/ZAPDUtils/Utils/BinaryReader.cpp b/ZAPDUtils/Utils/BinaryReader.cpp index 94ca98a..5916ce9 100644 --- a/ZAPDUtils/Utils/BinaryReader.cpp +++ b/ZAPDUtils/Utils/BinaryReader.cpp @@ -18,6 +18,16 @@ void BinaryReader::Close() stream->Close(); } +void BinaryReader::SetEndianness(Endianness endianness) +{ + this->endianness = endianness; +} + +Endianness BinaryReader::GetEndianness() const +{ + return endianness; +} + void BinaryReader::Seek(uint32_t offset, SeekOffsetType seekType) { stream->Seek(offset, seekType); @@ -28,11 +38,16 @@ uint32_t BinaryReader::GetBaseAddress() return stream->GetBaseAddress(); } -void BinaryReader::Read([[maybe_unused]] char* buffer, int32_t length) +void BinaryReader::Read(int32_t length) { stream->Read(length); } +void BinaryReader::Read(char* buffer, int32_t length) +{ + stream->Read(buffer, length); +} + char BinaryReader::ReadChar() { return (char)stream->ReadByte(); @@ -53,6 +68,10 @@ int16_t BinaryReader::ReadInt16() int16_t result = 0; stream->Read((char*)&result, sizeof(int16_t)); + + if (endianness != Endianness::Native) + result = BSWAP16(result); + return result; } @@ -61,6 +80,10 @@ int32_t BinaryReader::ReadInt32() int32_t result = 0; stream->Read((char*)&result, sizeof(int32_t)); + + if (endianness != Endianness::Native) + result = BSWAP32(result); + return result; } @@ -69,6 +92,10 @@ uint16_t BinaryReader::ReadUInt16() uint16_t result = 0; stream->Read((char*)&result, sizeof(uint16_t)); + + if (endianness != Endianness::Native) + result = BSWAP16(result); + return result; } @@ -77,6 +104,10 @@ uint32_t BinaryReader::ReadUInt32() uint32_t result = 0; stream->Read((char*)&result, sizeof(uint32_t)); + + if (endianness != Endianness::Native) + result = BSWAP32(result); + return result; } @@ -85,6 +116,10 @@ uint64_t BinaryReader::ReadUInt64() uint64_t result = 0; stream->Read((char*)&result, sizeof(uint64_t)); + + if (endianness != Endianness::Native) + result = BSWAP64(result); + return result; } @@ -94,6 +129,9 @@ float BinaryReader::ReadSingle() stream->Read((char*)&result, sizeof(float)); + if (endianness != Endianness::Native) + result = BitConverter::ToFloatBE((uint8_t*)&result, 0); + if (std::isnan(result)) throw std::runtime_error("BinaryReader::ReadSingle(): Error reading stream"); @@ -105,6 +143,10 @@ double BinaryReader::ReadDouble() double result = NAN; stream->Read((char*)&result, sizeof(double)); + + if (endianness != Endianness::Native) + result = BitConverter::ToDoubleBE((uint8_t*)&result, 0); + if (std::isnan(result)) throw std::runtime_error("BinaryReader::ReadDouble(): Error reading stream"); |
