summaryrefslogtreecommitdiff
path: root/ZAPDUtils/Utils/BinaryWriter.cpp
diff options
context:
space:
mode:
authorGaryOderNichts <12049776+GaryOderNichts@users.noreply.github.com>2022-07-27 23:50:56 +0200
committerGitHub <noreply@github.com>2022-07-27 23:50:56 +0200
commit07143e2d1c9e0b45e5960d87b1932ebba69166cf (patch)
tree8b118ac34b2802d540944cfb9b3c4eef89f3f30b /ZAPDUtils/Utils/BinaryWriter.cpp
parent7c5825525f69090e7e28927d0f815d8fa1e21c93 (diff)
Big-endian support (#909)
Diffstat (limited to 'ZAPDUtils/Utils/BinaryWriter.cpp')
-rw-r--r--ZAPDUtils/Utils/BinaryWriter.cpp31
1 files changed, 30 insertions, 1 deletions
diff --git a/ZAPDUtils/Utils/BinaryWriter.cpp b/ZAPDUtils/Utils/BinaryWriter.cpp
index c456bde..aa7840f 100644
--- a/ZAPDUtils/Utils/BinaryWriter.cpp
+++ b/ZAPDUtils/Utils/BinaryWriter.cpp
@@ -10,6 +10,11 @@ BinaryWriter::BinaryWriter(std::shared_ptr<Stream> nStream)
stream = nStream;
}
+void BinaryWriter::SetEndianness(Endianness endianness)
+{
+ this->endianness = endianness;
+}
+
void BinaryWriter::Close()
{
stream->Close();
@@ -47,16 +52,25 @@ void BinaryWriter::Write(uint8_t value)
void BinaryWriter::Write(int16_t value)
{
+ if (endianness != Endianness::Native)
+ value = BSWAP16(value);
+
stream->Write((char*)&value, sizeof(int16_t));
}
void BinaryWriter::Write(uint16_t value)
{
+ if (endianness != Endianness::Native)
+ value = BSWAP16(value);
+
stream->Write((char*)&value, sizeof(uint16_t));
}
void BinaryWriter::Write(int32_t value)
{
+ if (endianness != Endianness::Native)
+ value = BSWAP32(value);
+
stream->Write((char*)&value, sizeof(int32_t));
}
@@ -68,33 +82,48 @@ void BinaryWriter::Write(int32_t valueA, int32_t valueB)
void BinaryWriter::Write(uint32_t value)
{
+ if (endianness != Endianness::Native)
+ value = BSWAP32(value);
+
stream->Write((char*)&value, sizeof(uint32_t));
}
void BinaryWriter::Write(int64_t value)
{
+ if (endianness != Endianness::Native)
+ value = BSWAP64(value);
+
stream->Write((char*)&value, sizeof(int64_t));
}
void BinaryWriter::Write(uint64_t value)
{
+ if (endianness != Endianness::Native)
+ value = BSWAP64(value);
+
stream->Write((char*)&value, sizeof(uint64_t));
}
void BinaryWriter::Write(float value)
{
+ if (endianness != Endianness::Native)
+ value = BitConverter::ToFloatBE((uint8_t*)&value, 0);
+
stream->Write((char*)&value, sizeof(float));
}
void BinaryWriter::Write(double value)
{
+ if (endianness != Endianness::Native)
+ value = BitConverter::ToDoubleBE((uint8_t*)&value, 0);
+
stream->Write((char*)&value, sizeof(double));
}
void BinaryWriter::Write(const std::string& str)
{
int strLen = str.size();
- stream->Write((char*)&strLen, sizeof(int));
+ Write(strLen);
for (char c : str)
stream->WriteByte(c);