summaryrefslogtreecommitdiff
path: root/server/packetreader.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--server/packetreader.cpp53
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;
+}