2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
#include "cPacket.h"
|
2012-01-27 09:04:28 -05:00
|
|
|
#include "../Endianness.h"
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
// These checks cannot be done in preprocessor, since sizeof() is evaluated while compiling, so in preprocessing it's unknown.
|
|
|
|
// Check some basic type assumptions:
|
|
|
|
#if (sizeof(int) != 4)
|
|
|
|
#error "Bad size for int, protocol won't work"
|
2011-10-03 14:41:19 -04:00
|
|
|
#endif
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
#if (sizeof(float) != 4)
|
|
|
|
#error "Bad size for float, protocol won't work"
|
2011-10-03 14:41:19 -04:00
|
|
|
#endif
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
#if (sizeof(double) != 8)
|
|
|
|
#error "Bad size for double, protocol won't work"
|
|
|
|
#endif
|
|
|
|
*/
|
|
|
|
|
2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
int cPacket::ReadString16(const char * a_Data, int a_Size, AString & a_OutString )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
int TotalBytes = 0;
|
|
|
|
short StrLen;
|
|
|
|
HANDLE_PACKET_READ(ReadShort, StrLen, TotalBytes);
|
|
|
|
|
|
|
|
if (2 * StrLen > a_Size - TotalBytes)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
// The string is not yet complete in the buffer
|
|
|
|
return PACKET_INCOMPLETE;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
// Simple UTF-16 to UTF-8 conversion - discard higher bits, ignore multishort sequences:
|
|
|
|
a_OutString.clear();
|
|
|
|
a_OutString.reserve(StrLen);
|
|
|
|
short * UTF16 = (short *)(a_Data + TotalBytes);
|
|
|
|
for ( int i = 0; i < StrLen; ++i )
|
|
|
|
{
|
|
|
|
a_OutString.push_back( (char)ntohs(UTF16[i]) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return TotalBytes + StrLen * sizeof(short);
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
int cPacket::ReadShort(const char * a_Data, int a_Size, short & a_OutShort )
|
|
|
|
{
|
|
|
|
if (a_Size < 2)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
return PACKET_INCOMPLETE;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
2012-02-07 15:49:52 -05:00
|
|
|
a_OutShort = ntohs(*((short *)a_Data));
|
|
|
|
return 2;
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
int cPacket::ReadInteger(const char * a_Data, int a_Size, int & a_OutInteger )
|
|
|
|
{
|
|
|
|
if (a_Size < 4)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
return PACKET_INCOMPLETE;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
2012-02-07 15:49:52 -05:00
|
|
|
a_OutInteger = ntohl(*((int *)a_Data));
|
|
|
|
return 4;
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
int cPacket::ReadInteger(const char * a_Data, int a_Size, unsigned int & a_OutInteger )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
if (a_Size < 4)
|
|
|
|
{
|
|
|
|
return PACKET_INCOMPLETE;
|
|
|
|
}
|
|
|
|
a_OutInteger = ntohl(*((unsigned int *)a_Data));
|
|
|
|
return 4;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cPacket::ReadFloat(const char * a_Data, int a_Size, float & a_OutFloat )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
if (a_Size < sizeof(float))
|
|
|
|
{
|
|
|
|
return PACKET_INCOMPLETE;
|
|
|
|
}
|
|
|
|
a_OutFloat = NetworkToHostFloat4(a_Data);
|
|
|
|
return sizeof(float);
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cPacket::ReadDouble(const char * a_Data, int a_Size, double & a_OutDouble )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
if (a_Size < sizeof(double))
|
|
|
|
{
|
|
|
|
return PACKET_INCOMPLETE;
|
|
|
|
}
|
|
|
|
a_OutDouble = NetworkToHostDouble8(a_Data);
|
|
|
|
return sizeof(double);
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cPacket::ReadByte(const char * a_Data, int a_Size, char & a_OutByte )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
if (a_Size < 1)
|
|
|
|
{
|
|
|
|
return PACKET_INCOMPLETE;
|
|
|
|
}
|
|
|
|
a_OutByte = *a_Data;
|
|
|
|
return 1;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cPacket::ReadByte(const char * a_Data, int a_Size, unsigned char & a_OutByte )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
if (a_Size < 1)
|
|
|
|
{
|
|
|
|
return PACKET_INCOMPLETE;
|
|
|
|
}
|
|
|
|
a_OutByte = *((unsigned char *)a_Data);
|
|
|
|
return 1;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cPacket::ReadLong(const char * a_Data, int a_Size, long long & a_OutLong )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
if (a_Size < sizeof(a_OutLong))
|
|
|
|
{
|
|
|
|
return PACKET_INCOMPLETE;
|
|
|
|
}
|
|
|
|
a_OutLong = NetworkToHostLong8(a_Data);
|
|
|
|
return sizeof(a_OutLong);
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cPacket::ReadBool(const char * a_Data, int a_Size, bool & a_OutBool )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
if (a_Size < sizeof(bool))
|
|
|
|
{
|
|
|
|
return PACKET_INCOMPLETE;
|
|
|
|
}
|
|
|
|
a_OutBool = (*a_Data != 0);
|
|
|
|
return sizeof(bool);
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPacket::AppendString(AString & a_Dst, const AString & a_String)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
AppendShort(a_Dst, (unsigned short)a_String.size());
|
|
|
|
a_Dst.append(a_String);
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPacket::AppendString16(AString & a_Dst, const AString & a_String)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
AppendShort(a_Dst, (unsigned short)a_String.size());
|
|
|
|
std::auto_ptr<char> UTF16(new char[a_String.size() * sizeof( short ) ]);
|
2011-10-03 14:41:19 -04:00
|
|
|
for( unsigned int i = 0; i < a_String.size(); ++i )
|
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
UTF16.get()[i * sizeof( short )] = 0x00;
|
|
|
|
UTF16.get()[i * sizeof( short ) + 1] = a_String[i];
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
2012-02-07 15:49:52 -05:00
|
|
|
a_Dst.append(UTF16.get(), a_String.size() * sizeof(short));
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPacket::AppendShort(AString & a_Dst, short a_Short)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
short ConvertedShort = htons( a_Short );
|
2012-02-07 15:49:52 -05:00
|
|
|
a_Dst.append((const char *)&ConvertedShort, sizeof(short));
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPacket::AppendShort(AString & a_Dst, unsigned short a_Short)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
short ConvertedShort = htons( a_Short );
|
2012-02-07 15:49:52 -05:00
|
|
|
a_Dst.append((const char *)&ConvertedShort, sizeof(short));
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPacket::AppendInteger(AString & a_Dst, int a_Integer)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
int ConvertedInt = htonl( a_Integer );
|
2012-02-07 15:49:52 -05:00
|
|
|
a_Dst.append((const char *)&ConvertedInt, sizeof(int));
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPacket::AppendInteger(AString & a_Dst, unsigned int a_Integer)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
unsigned int ConvertedInt = htonl( a_Integer );
|
2012-02-07 15:49:52 -05:00
|
|
|
a_Dst.append((const char *)&ConvertedInt, sizeof(int));
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPacket::AppendFloat(AString & a_Dst, float a_Float)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
unsigned int ConvertedFloat = HostToNetwork4(&a_Float);
|
2012-02-07 15:49:52 -05:00
|
|
|
a_Dst.append((const char *)&ConvertedFloat, sizeof(int));
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPacket::AppendDouble(AString & a_Dst, const double & a_Double)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
unsigned long long ConvertedDouble = HostToNetwork8(&a_Double);
|
2012-02-07 15:49:52 -05:00
|
|
|
a_Dst.append((const char *)&ConvertedDouble, 8);
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPacket::AppendByte(AString & a_Dst, char a_Byte)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
a_Dst.append(&a_Byte, 1);
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPacket::AppendLong(AString & a_Dst, const long long & a_Long)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
unsigned long long ConvertedLong = HostToNetwork8(&a_Long);
|
2012-02-07 15:49:52 -05:00
|
|
|
a_Dst.append((const char *)&ConvertedLong, sizeof(a_Long));
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPacket::AppendBool(AString & a_Dst, bool a_Bool)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
a_Dst.append((const char *)&a_Bool, 1);
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPacket::AppendData(AString & a_Dst, const char * a_Data, unsigned int a_Size)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-07 15:49:52 -05:00
|
|
|
a_Dst.append(a_Data, a_Size);
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
2012-02-07 15:49:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|