#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; }