diff options
Diffstat (limited to 'ZAPDTR/ZAPDUtils/Utils/BinaryReader.cpp')
| -rw-r--r-- | ZAPDTR/ZAPDUtils/Utils/BinaryReader.cpp | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/ZAPDTR/ZAPDUtils/Utils/BinaryReader.cpp b/ZAPDTR/ZAPDUtils/Utils/BinaryReader.cpp index 94ca98a01..5916ce982 100644 --- a/ZAPDTR/ZAPDUtils/Utils/BinaryReader.cpp +++ b/ZAPDTR/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"); |
