m_data.clear();
}
+void NetworkPacket::checkReadOffset(u32 from_offset)
+{
+ if (from_offset >= m_datasize) {
+ std::stringstream ss;
+ ss << "Reading outside packet (offset: " <<
+ from_offset << ", packet size: " << getSize() << ")";
+ throw SerializationError(ss.str());
+ }
+}
+
void NetworkPacket::putRawPacket(u8 *data, u32 datasize, u16 peer_id)
{
// If a m_command is already set, we are rewriting on same packet
char* NetworkPacket::getString(u32 from_offset)
{
- if (from_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(from_offset);
return (char*)&m_data[from_offset];
}
NetworkPacket& NetworkPacket::operator>>(char& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readU8(&m_data[m_read_offset]);
char NetworkPacket::getChar(u32 offset)
{
- if (offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(offset);
return readU8(&m_data[offset]);
}
NetworkPacket& NetworkPacket::operator>>(bool& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readU8(&m_data[m_read_offset]);
NetworkPacket& NetworkPacket::operator>>(u8& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readU8(&m_data[m_read_offset]);
u8 NetworkPacket::getU8(u32 offset)
{
- if (offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(offset);
return readU8(&m_data[offset]);
}
return NULL;
}
- if (from_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(from_offset);
return (u8*)&m_data[from_offset];
}
NetworkPacket& NetworkPacket::operator>>(u16& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readU16(&m_data[m_read_offset]);
u16 NetworkPacket::getU16(u32 from_offset)
{
- if (from_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(from_offset);
return readU16(&m_data[from_offset]);
}
NetworkPacket& NetworkPacket::operator>>(u32& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readU32(&m_data[m_read_offset]);
NetworkPacket& NetworkPacket::operator>>(u64& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readU64(&m_data[m_read_offset]);
NetworkPacket& NetworkPacket::operator>>(float& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readF1000(&m_data[m_read_offset]);
NetworkPacket& NetworkPacket::operator>>(v2f& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readV2F1000(&m_data[m_read_offset]);
NetworkPacket& NetworkPacket::operator>>(v3f& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readV3F1000(&m_data[m_read_offset]);
NetworkPacket& NetworkPacket::operator>>(s16& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readS16(&m_data[m_read_offset]);
NetworkPacket& NetworkPacket::operator>>(s32& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readS32(&m_data[m_read_offset]);
NetworkPacket& NetworkPacket::operator>>(v3s16& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readV3S16(&m_data[m_read_offset]);
NetworkPacket& NetworkPacket::operator>>(v3s32& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readV3S32(&m_data[m_read_offset]);
NetworkPacket& NetworkPacket::operator>>(video::SColor& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readARGB8(&m_data[m_read_offset]);