diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..5580da3 --- /dev/null +++ b/main.cpp @@ -0,0 +1,17 @@ +#include +#include +#include + +void test(quint8 *p) { + p++; +} + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + qDebug() << "MsgPack"; + qDebug() << MsgPack::deserialize(QByteArray::fromHex("fffeece1e0")); + + return a.exec(); +} diff --git a/msgpack.cpp b/msgpack.cpp new file mode 100644 index 0000000..1ff2eb6 --- /dev/null +++ b/msgpack.cpp @@ -0,0 +1,29 @@ +#include "msgpack.h" +#include + +QVariantList MsgPack::deserialize(const QByteArray &data) +{ + QVariantList d; + int i = 0; + + quint8 *p = (quint8 *)data.data(); + while (i < data.size()) { + if (p[i] <= 127) { // positive fixint 0x00 - 0x7f + quint32 val = (quint32)p[i]; + d.append(QVariant(val)); + i += 1; + } + if (p[i] >= 0xe0) { // negative fixint 0xe0 - 0xff + quint8 val8 = (quint8)p[i]; + val8 &= ~((1 << 7) | (1 << 6) | (1 << 5)); + qint32 val = 31 - val8; + val ^= 0xffffffff; + d.append(QVariant((qint32)val)); + i += 1; + } + + + } + + return d; +} diff --git a/msgpack.h b/msgpack.h new file mode 100644 index 0000000..635a0dc --- /dev/null +++ b/msgpack.h @@ -0,0 +1,12 @@ +#ifndef MSGPACK_H +#define MSGPACK_H +#include +#include + +namespace MsgPack +{ + + QVariantList deserialize(const QByteArray &data); +} + +#endif // MSGPACK_H diff --git a/private/msgpack_p.cpp b/private/msgpack_p.cpp new file mode 100644 index 0000000..097438a --- /dev/null +++ b/private/msgpack_p.cpp @@ -0,0 +1,6 @@ +#include "msgpack_p.h" + +static quint8 MsgPackPrivate::data_sizes[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // fixmap upto 15 elements + +}; diff --git a/private/msgpack_p.h b/private/msgpack_p.h new file mode 100644 index 0000000..02ce193 --- /dev/null +++ b/private/msgpack_p.h @@ -0,0 +1,33 @@ +#ifndef MSGPACK_P_H +#define MSGPACK_P_H +#include + +/* parser functions: + * QVariant _type_(quint8 *p, int &i); + * parses some type, which data is stored at p + * + */ + +namespace MsgPackPrivate +{ +/* data sizes for types from 0x7f to 0xe0 + * (without positive and negative fixint) + ** fixed size types: + * possible values: 1, 2, 3, 4, 5, 6, 9, 10, 18 - bytes + * 254 - size is in next 1 byte + * 253 - 2 + * 251 - 4 + ** ext formats: + * 126 - size is in next byte + 1 type byte + * 125 - 2 byte size + 1 type byte + * 123 - 4 byte size + 1 type byte + */ + + static quint8 data_sizes[96]; + + QVariant positive_fixint(quint8 *p); + + +} + +#endif // MSGPACK_P_H diff --git a/private/types.gnumeric b/private/types.gnumeric new file mode 100644 index 0000000..7202466 Binary files /dev/null and b/private/types.gnumeric differ diff --git a/qmsgpack.pro b/qmsgpack.pro new file mode 100644 index 0000000..94dc41c --- /dev/null +++ b/qmsgpack.pro @@ -0,0 +1,24 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2014-09-07T17:43:05 +# +#------------------------------------------------- + +QT += core + +QT -= gui + +TARGET = qmsgpack +#CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + + +SOURCES += main.cpp \ + msgpack.cpp \ + private/msgpack_p.cpp + +HEADERS += \ + msgpack.h \ + private/msgpack_p.h