Browse Source

Network packets should have network byte order

xad
Xadhoom 5 years ago committed by Anders Jenbo
parent
commit
4c6b17fdb8
  1. 62
      Source/engine.h
  2. 30
      Source/loadsave.cpp
  3. 1
      SourceX/dvlnet/packet.cpp
  4. 2
      SourceX/dvlnet/packet.h

62
Source/engine.h

@ -26,6 +26,40 @@
DEVILUTION_BEGIN_NAMESPACE
template <class T>
T SwapLE(T in)
{
static_assert((sizeof(T) == 1) || (sizeof(T) == 2) || (sizeof(T) == 4) || (sizeof(T) == 8),
"SwapLE called for unsupported size");
switch (sizeof(T)) {
case 1:
return in;
case 2:
return static_cast<T>(SDL_SwapLE16(static_cast<Uint16>(in)));
case 4:
return static_cast<T>(SDL_SwapLE32(static_cast<Uint32>(in)));
case 8:
return static_cast<T>(SDL_SwapLE64(static_cast<Uint64>(in)));
}
}
template <class T>
T SwapBE(T in)
{
static_assert((sizeof(T) == 1) || (sizeof(T) == 2) || (sizeof(T) == 4) || (sizeof(T) == 8),
"SwapBE called for unsupported size");
switch (sizeof(T)) {
case 1:
return in;
case 2:
return static_cast<T>(SDL_SwapBE16(static_cast<Uint16>(in)));
case 4:
return static_cast<T>(SDL_SwapBE32(static_cast<Uint32>(in)));
case 8:
return static_cast<T>(SDL_SwapBE64(static_cast<Uint64>(in)));
}
}
inline BYTE *CelGetFrameStart(BYTE *pCelBuff, int nCel)
{
DWORD *pFrameTable;
@ -105,27 +139,43 @@ struct CelOutputBuffer {
*
* Only use this if the buffer owns its data.
*/
void Free() {
void Free()
{
SDL_FreeSurface(this->surface);
this->surface = NULL;
}
int w() const { return region.w; }
int h() const { return region.h; }
int w() const
{
return region.w;
}
int h() const
{
return region.h;
}
BYTE *at(int x, int y) const
{
return static_cast<BYTE *>(surface->pixels) + region.x + x + surface->pitch * (region.y + y);
}
BYTE *begin() const { return at(0, 0); }
BYTE *end() const { return at(0, region.h); }
BYTE *begin() const
{
return at(0, 0);
}
BYTE *end() const
{
return at(0, region.h);
}
/**
* @brief Line width of the raw underlying byte buffer.
* May be wider than its logical width (for power-of-2 alignment).
*/
int pitch() { return surface->pitch; }
int pitch()
{
return surface->pitch;
}
bool in_bounds(Sint16 x, Sint16 y) const
{

30
Source/loadsave.cpp

@ -14,36 +14,6 @@ int giNumberOfSmithPremiumItems;
namespace {
template <class T>
T SwapLE(T in)
{
switch (sizeof(T)) {
case 2:
return SDL_SwapLE16(in);
case 4:
return SDL_SwapLE32(in);
case 8:
return SDL_SwapLE64(in);
default:
return in;
}
}
template <class T>
T SwapBE(T in)
{
switch (sizeof(T)) {
case 2:
return SDL_SwapBE16(in);
case 4:
return SDL_SwapBE32(in);
case 8:
return SDL_SwapBE64(in);
default:
return in;
}
}
class LoadHelper {
Uint8 *m_buffer;
Uint32 m_bufferPtr = 0;

1
SourceX/dvlnet/packet.cpp

@ -1,4 +1,5 @@
#include "dvlnet/packet.h"
#include "engine.h"
namespace dvl {
namespace net {

2
SourceX/dvlnet/packet.h

@ -172,6 +172,7 @@ void packet_in::process_element(T &x)
if (decrypted_buffer.size() < sizeof(T))
throw packet_exception();
std::memcpy(&x, decrypted_buffer.data(), sizeof(T));
x = SwapBE(x);
decrypted_buffer.erase(decrypted_buffer.begin(),
decrypted_buffer.begin() + sizeof(T));
}
@ -263,6 +264,7 @@ inline void packet_out::process_element(buffer_t &x)
template <class T>
void packet_out::process_element(T &x)
{
x = SwapBE(x);
encrypted_buffer.insert(encrypted_buffer.end(), begin(x), end(x));
}

Loading…
Cancel
Save