summaryrefslogtreecommitdiff
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
parent7c5825525f69090e7e28927d0f815d8fa1e21c93 (diff)
Big-endian support (#909)
-rw-r--r--ZAPDUtils/Utils/BinaryReader.cpp44
-rw-r--r--ZAPDUtils/Utils/BinaryReader.h6
-rw-r--r--ZAPDUtils/Utils/BinaryWriter.cpp31
-rw-r--r--ZAPDUtils/Utils/BinaryWriter.h4
-rw-r--r--ZAPDUtils/Utils/BitConverter.h22
-rw-r--r--ZAPDUtils/Utils/Stream.h7
6 files changed, 105 insertions, 9 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");
diff --git a/ZAPDUtils/Utils/BinaryReader.h b/ZAPDUtils/Utils/BinaryReader.h
index a1994aa..0b18351 100644
--- a/ZAPDUtils/Utils/BinaryReader.h
+++ b/ZAPDUtils/Utils/BinaryReader.h
@@ -8,6 +8,7 @@
#include "../Vec2f.h"
#include "../Vec3f.h"
#include "../Vec3s.h"
+#include "BitConverter.h"
#include "Stream.h"
class BinaryReader
@@ -18,9 +19,13 @@ public:
void Close();
+ void SetEndianness(Endianness endianness);
+ Endianness GetEndianness() const;
+
void Seek(uint32_t offset, SeekOffsetType seekType);
uint32_t GetBaseAddress();
+ void Read(int32_t length);
void Read(char* buffer, int32_t length);
char ReadChar();
int8_t ReadByte();
@@ -41,4 +46,5 @@ public:
protected:
std::shared_ptr<Stream> stream;
+ Endianness endianness = Endianness::Native;
}; \ No newline at end of file
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);
diff --git a/ZAPDUtils/Utils/BinaryWriter.h b/ZAPDUtils/Utils/BinaryWriter.h
index e6dd841..67c8fcd 100644
--- a/ZAPDUtils/Utils/BinaryWriter.h
+++ b/ZAPDUtils/Utils/BinaryWriter.h
@@ -4,6 +4,7 @@
#include <memory>
#include <string>
#include <vector>
+#include "BitConverter.h"
#include "Stream.h"
class BinaryWriter
@@ -12,6 +13,8 @@ public:
BinaryWriter(Stream* nStream);
BinaryWriter(std::shared_ptr<Stream> nStream);
+ void SetEndianness(Endianness endianness);
+
std::shared_ptr<Stream> GetStream();
uint64_t GetBaseAddress();
uint64_t GetLength();
@@ -34,4 +37,5 @@ public:
protected:
std::shared_ptr<Stream> stream;
+ Endianness endianness = Endianness::Native;
}; \ No newline at end of file
diff --git a/ZAPDUtils/Utils/BitConverter.h b/ZAPDUtils/Utils/BitConverter.h
index 708d4b5..b62891d 100644
--- a/ZAPDUtils/Utils/BitConverter.h
+++ b/ZAPDUtils/Utils/BitConverter.h
@@ -5,6 +5,28 @@
#include <vector>
#include <cstring>
+#ifdef _MSC_VER
+#define BSWAP16 _byteswap_ushort
+#define BSWAP32 _byteswap_ulong
+#define BSWAP64 _byteswap_uint64
+#else
+#define BSWAP16 __builtin_bswap16
+#define BSWAP32 __builtin_bswap32
+#define BSWAP64 __builtin_bswap64
+#endif
+
+enum class Endianness
+{
+ Little = 0,
+ Big = 1,
+
+#if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) || defined(__BIG_ENDIAN__)
+ Native = Big,
+#else
+ Native = Little,
+#endif
+};
+
class BitConverter
{
public:
diff --git a/ZAPDUtils/Utils/Stream.h b/ZAPDUtils/Utils/Stream.h
index e73a9a7..e72d794 100644
--- a/ZAPDUtils/Utils/Stream.h
+++ b/ZAPDUtils/Utils/Stream.h
@@ -10,13 +10,6 @@ enum class SeekOffsetType
End
};
-// TODO: Eventually account for endianess in binaryreader and binarywriter
-enum class Endianess
-{
- Little = 0,
- Big = 1,
-};
-
class Stream
{
public: