diff options
Diffstat (limited to '')
-rw-r--r-- | source/ByteBuffer.cpp | 54 | ||||
-rw-r--r-- | source/ByteBuffer.h | 2 |
2 files changed, 56 insertions, 0 deletions
diff --git a/source/ByteBuffer.cpp b/source/ByteBuffer.cpp index 1cdd2f430..8f2b76c1f 100644 --- a/source/ByteBuffer.cpp +++ b/source/ByteBuffer.cpp @@ -13,6 +13,25 @@ +// Try to determine endianness: +#if ( \ + defined(__i386__) || defined(__alpha__) || \ + defined(__ia64) || defined(__ia64__) || \ + defined(_M_IX86) || defined(_M_IA64) || \ + defined(_M_ALPHA) || defined(__amd64) || \ + defined(__amd64__) || defined(_M_AMD64) || \ + defined(__x86_64) || defined(__x86_64__) || \ + defined(_M_X64) || defined(__bfin__) || \ + defined(__ARMEL__) || \ + (defined(_WIN32) && defined(__ARM__) && defined(_MSC_VER)) \ +) + #define IS_LITTLE_ENDIAN +#elif defined (__ARMEB__) + #define IS_BIG_ENDIAN +#else + #error Cannot determine endianness of this platform +#endif + // If a string sent over the protocol is larger than this, a warning is emitted to the console #define MAX_STRING_SIZE (512 KiB) @@ -416,6 +435,25 @@ bool cByteBuffer::ReadVarUTF8String(AString & a_Value) +bool cByteBuffer::ReadLEInt(int & a_Value) +{ + CHECK_THREAD; + CheckValid(); + NEEDBYTES(4); + ReadBuf(&a_Value, 4); + + #ifdef IS_BIG_ENDIAN + // Convert: + a_Value = ((a_Value >> 24) & 0xff) | ((a_Value >> 16) & 0xff00) | ((a_Value >> 8) & 0xff0000) | (a_Value & 0xff000000); + #endif + + return true; +} + + + + + bool cByteBuffer::WriteChar(char a_Value) { CHECK_THREAD; @@ -572,6 +610,22 @@ bool cByteBuffer::WriteVarUTF8String(const AString & a_Value) +bool cByteBuffer::WriteLEInt(int a_Value) +{ + CHECK_THREAD; + CheckValid(); + #ifdef IS_LITTLE_ENDIAN + return WriteBuf((const char *)&a_Value, 4); + #else + int Value = ((a_Value >> 24) & 0xff) | ((a_Value >> 16) & 0xff00) | ((a_Value >> 8) & 0xff0000) | (a_Value & 0xff000000); + return WriteBuf((const char *)&Value, 4); + #endif +} + + + + + bool cByteBuffer::ReadBuf(void * a_Buffer, int a_Count) { CHECK_THREAD; diff --git a/source/ByteBuffer.h b/source/ByteBuffer.h index 21abb0377..a9dd7f5ea 100644 --- a/source/ByteBuffer.h +++ b/source/ByteBuffer.h @@ -60,6 +60,7 @@ public: bool ReadBEUTF16String16(AString & a_Value); // string length as BE short, then string as UTF-16BE bool ReadVarInt (UInt32 & a_Value); bool ReadVarUTF8String (AString & a_Value); // string length as VarInt, then string as UTF-8 + bool ReadLEInt (int & a_Value); /// Reads VarInt, assigns it to anything that can be assigned from an UInt32 (unsigned short, char, Byte, double, ...) template <typename T> bool ReadVarInt(T & a_Value) @@ -85,6 +86,7 @@ public: bool WriteBEUTF16String16(const AString & a_Value); // string length as BE short, then string as UTF-16BE bool WriteVarInt (UInt32 a_Value); bool WriteVarUTF8String (const AString & a_Value); // string length as VarInt, then string as UTF-8 + bool WriteLEInt (int a_Value); /// Reads a_Count bytes into a_Buffer; returns true if successful bool ReadBuf(void * a_Buffer, int a_Count); |