diff options
author | Treeki <treeki@gmail.com> | 2013-12-25 09:00:59 +0100 |
---|---|---|
committer | Treeki <treeki@gmail.com> | 2013-12-25 09:00:59 +0100 |
commit | b7a8b597b00eedde277836eb8530ba742edcad5d (patch) | |
tree | 1b959fa0eec02ff4e22e168aa4379b2b64575ce3 /server/packetreader.cpp | |
download | bounce_qt-b7a8b597b00eedde277836eb8530ba742edcad5d.tar.gz bounce_qt-b7a8b597b00eedde277836eb8530ba742edcad5d.zip |
Diffstat (limited to '')
-rw-r--r-- | server/packetreader.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
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; +} |