From e7e99051910a9a8739d8a50225be868e5dc286d6 Mon Sep 17 00:00:00 2001 From: Jared Adams Date: Sat, 4 Jun 2011 12:25:25 -0600 Subject: Fix handling of 16- and 32-bit number in tmwAthena netcode --- src/net/tmwa/messagein.cpp | 19 +++++++++++++++++-- src/net/tmwa/messageout.cpp | 18 ++++++++++++------ 2 files changed, 29 insertions(+), 8 deletions(-) (limited to 'src/net/tmwa') diff --git a/src/net/tmwa/messagein.cpp b/src/net/tmwa/messagein.cpp index ca2d52ab..38bc14dd 100644 --- a/src/net/tmwa/messagein.cpp +++ b/src/net/tmwa/messagein.cpp @@ -21,6 +21,9 @@ #include "net/tmwa/messagein.h" +#include +#include + namespace TmwAthena { MessageIn::MessageIn(const char *data, unsigned int length): @@ -35,7 +38,13 @@ uint16_t MessageIn::readInt16() uint16_t value = 0; if (mPos + 2 <= mLength) { - value = (mData[mPos + 1] << 8) | mData[mPos]; +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + uint16_t swap; + memcpy(&swap, mData + mPos, sizeof(uint16_t)); + value = SDL_Swap16(swap); +#else + memcpy(&value, mData + mPos, sizeof(uint16_t)); +#endif } mPos += 2; return value; @@ -46,7 +55,13 @@ uint32_t MessageIn::readInt32() uint32_t value = 0; if (mPos + 4 <= mLength) { - value = (mData[mPos + 3] << 24) | (mData[mPos + 2] << 16) | (mData[mPos + 1] << 8) | mData[mPos]; +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + uint32_t swap; + memcpy(&swap, mData + mPos, sizeof(uint32_t)); + value = SDL_Swap32(swap); +#else + memcpy(&value, mData + mPos, sizeof(uint32_t)); +#endif } mPos += 4; return value; diff --git a/src/net/tmwa/messageout.cpp b/src/net/tmwa/messageout.cpp index 7c3a3c61..705b9a96 100644 --- a/src/net/tmwa/messageout.cpp +++ b/src/net/tmwa/messageout.cpp @@ -47,18 +47,24 @@ void MessageOut::expand(size_t bytes) void MessageOut::writeInt16(uint16_t value) { expand(2); - mData[mPos] = value; - mData[mPos + 1] = value >> 8; +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + uint16_t swap=SDL_Swap16(value); + memcpy(mData + mPos, &swap, sizeof(uint16_t)); +#else + memcpy(mData + mPos, &value, sizeof(uint16_t)); +#endif mPos += 2; } void MessageOut::writeInt32(uint32_t value) { expand(4); - mData[mPos] = value; - mData[mPos + 1] = value >> 8; - mData[mPos + 2] = value >> 16; - mData[mPos + 3] = value >> 24; +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + uint32_t swap=SDL_Swap32(value); + memcpy(mData + mPos, &swap, sizeof(uint32_t)); +#else + memcpy(mData + mPos, &value, sizeof(uint32_t)); +#endif mPos += 4; } -- cgit v1.2.3-60-g2f50