From b7a8b597b00eedde277836eb8530ba742edcad5d Mon Sep 17 00:00:00 2001 From: Treeki Date: Wed, 25 Dec 2013 09:00:59 +0100 Subject: commit initial bits --- server/packetreader.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 server/packetreader.cpp (limited to 'server/packetreader.cpp') diff --git a/server/packetreader.cpp b/server/packetreader.cpp new file mode 100644 index 0000000..c28bcba --- /dev/null +++ b/server/packetreader.cpp @@ -0,0 +1,53 @@ +#include "packetreader.h" + +PacketReader::PacketReader(const QByteArray &buffer) { + m_pointer = (const quint8 *)buffer.constData(); + m_end = m_pointer + buffer.size(); +} + +PacketReader::PacketReader(const quint8 *data, int size) { + m_pointer = data; + m_end = m_pointer + size; +} + + +quint32 PacketReader::readU32() { + if ((m_pointer + 4) > m_end) + return 0; + + quint32 value = + m_pointer[0] | + (m_pointer[1] << 8) | + (m_pointer[2] << 16) | + (m_pointer[3] << 24); + + m_pointer += 4; + return value; +} + +bool PacketReader::readBytes(quint8 *output, int size) { + if ((m_pointer + size) > m_end) + return false; + + memcpy(output, m_pointer, size); + m_pointer += size; + + return true; +} + +QString PacketReader::readString(int maxSize) { + quint32 size = readU32() & ~1; + + if ((m_pointer + size) > m_end) + return QString::null; + if ((size / 2) > maxSize) { + m_pointer += size; + return QString::null; + } + + QString retVal(size / 2, QChar()); + memcpy(retVal.data(), m_pointer, size); + m_pointer += size; + + return retVal; +} -- cgit v1.2.3