mirror of
https://github.com/HarbourMasters/Shipwright.git
synced 2025-08-14 18:48:04 -07:00
Big-endian support (#909)
This commit is contained in:
parent
477cf7f6ec
commit
6818247317
22 changed files with 762 additions and 62 deletions
|
@ -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");
|
||||
|
||||
|
|
|
@ -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;
|
||||
};
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
};
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue