summaryrefslogtreecommitdiff
path: root/daemon/unpacker.h
diff options
context:
space:
mode:
Diffstat (limited to 'daemon/unpacker.h')
-rw-r--r--daemon/unpacker.h92
1 files changed, 92 insertions, 0 deletions
diff --git a/daemon/unpacker.h b/daemon/unpacker.h
new file mode 100644
index 0000000..46e6d57
--- /dev/null
+++ b/daemon/unpacker.h
@@ -0,0 +1,92 @@
+#ifndef UNPACKER_H
+#define UNPACKER_H
+
+#include <QtEndian>
+#include <QByteArray>
+#include <QString>
+#include <QUuid>
+#include <QVariantMap>
+#include <QLoggingCategory>
+
+class Unpacker
+{
+ static QLoggingCategory l;
+
+public:
+ Unpacker(const QByteArray &data);
+
+ template <typename T>
+ T read();
+
+ template <typename T>
+ T readLE();
+
+ QByteArray readBytes(int n);
+
+ QString readFixedString(int n);
+
+ QUuid readUuid();
+
+ QMap<int, QVariant> readDict();
+
+ void skip(int n);
+
+ bool bad() const;
+
+private:
+ const uchar * p();
+ bool checkBad(int n = 0);
+
+ const QByteArray &_buf;
+ int _offset;
+ bool _bad;
+};
+
+inline Unpacker::Unpacker(const QByteArray &data)
+ : _buf(data), _offset(0), _bad(false)
+{
+}
+
+template <typename T>
+inline T Unpacker::read()
+{
+ if (checkBad(sizeof(T))) return 0;
+ const uchar *u = p();
+ _offset += sizeof(T);
+ return qFromBigEndian<T>(u);
+}
+
+template <typename T>
+inline T Unpacker::readLE()
+{
+ if (checkBad(sizeof(T))) return 0;
+ const uchar *u = p();
+ _offset += sizeof(T);
+ return qFromLittleEndian<T>(u);
+}
+
+inline void Unpacker::skip(int n)
+{
+ _offset += n;
+ checkBad();
+}
+
+inline bool Unpacker::bad() const
+{
+ return _bad;
+}
+
+inline const uchar * Unpacker::p()
+{
+ return reinterpret_cast<const uchar *>(&_buf.constData()[_offset]);
+}
+
+inline bool Unpacker::checkBad(int n)
+{
+ if (_offset + n > _buf.size()) {
+ _bad = true;
+ }
+ return _bad;
+}
+
+#endif // UNPACKER_H