diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ef8be1..307146b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,7 +55,7 @@ configure_file( IMMEDIATE @ONLY) set(QMSGPACK_MAJOR "0") -set(QMSGPACK_MINOR "0") +set(QMSGPACK_MINOR "1") set(QMSGPACK_VERSION "0") set(MSGPACK_QT_LIB_VERSION_STRING "${QMSGPACK_MAJOR}.${QMSGPACK_MINOR}.${QMSGPACK_VERSION}") diff --git a/src/msgpack-qt.pro b/src/msgpack-qt.pro index cfb1e2f..f1c6d18 100644 --- a/src/msgpack-qt.pro +++ b/src/msgpack-qt.pro @@ -11,15 +11,19 @@ QT -= gui TARGET = qmsgpack CONFIG -= app_bundle -TEMPLATE = app -#DEFINES += MSGPACK_MAKE_LIB -target.path = ../build +TEMPLATE = lib +DEFINES += MSGPACK_MAKE_LIB +DESTDIR = $$_PRO_FILE_PWD_/../bin INSTALLS += target QMAKE_CXXFLAGS += -fPIC +CONFIG += debug_and_release +CONFIG(debug, debug|release) { + TARGET = $$join(TARGET,,,d) +} -SOURCES += main.cpp \ - msgpack.cpp \ + +SOURCES += msgpack.cpp \ msgpack_common.cpp \ private/pack_p.cpp \ private/unpack_p.cpp diff --git a/src/msgpack.cpp b/src/msgpack.cpp index 278ab50..0fb155d 100644 --- a/src/msgpack.cpp +++ b/src/msgpack.cpp @@ -1,5 +1,4 @@ #include "msgpack.h" -#include #include "private/unpack_p.h" #include "private/pack_p.h" #include @@ -12,7 +11,6 @@ QVariant MsgPack::unpack(const QByteArray &data) return MsgPackPrivate::unpack(p, end); } - QByteArray MsgPack::pack(const QVariant &variant) { quint8 *p = 0; @@ -27,14 +25,17 @@ QByteArray MsgPack::pack(const QVariant &variant) return arr; } - bool MsgPack::registerPacker(QMetaType::Type qType, qint8 msgpackType, MsgPack::pack_user_f packer) { return MsgPackPrivate::register_packer(qType, msgpackType, packer); } - bool MsgPack::registerUnpacker(qint8 msgpackType, MsgPack::unpack_user_f unpacker) { return MsgPackPrivate::register_unpacker(msgpackType, unpacker); } + +void MsgPack::setCompatibilityModeEnabled(bool enabled) +{ + MsgPackPrivate::compatibilityMode = enabled; +} diff --git a/src/msgpack.h b/src/msgpack.h index 2592e34..5a86fae 100644 --- a/src/msgpack.h +++ b/src/msgpack.h @@ -12,6 +12,8 @@ namespace MsgPack MSGPACK_EXPORT QByteArray pack(const QVariant &variant); MSGPACK_EXPORT bool registerPacker(QMetaType::Type qType, qint8 msgpackType, pack_user_f packer); + + MSGPACK_EXPORT void setCompatibilityModeEnabled(bool enabled); } #endif // MSGPACK_H diff --git a/src/msgpack_common.cpp b/src/msgpack_common.cpp index 894435d..8e42e73 100644 --- a/src/msgpack_common.cpp +++ b/src/msgpack_common.cpp @@ -6,4 +6,4 @@ QString MsgPack::version() .arg(MSGPACK_MAJOR) .arg(MSGPACK_MINOR) .arg(MSGPACK_VERSION); -} \ No newline at end of file +} diff --git a/src/msgpack_common.h b/src/msgpack_common.h index a765236..3c62f7c 100644 --- a/src/msgpack_common.h +++ b/src/msgpack_common.h @@ -4,7 +4,7 @@ #include #define MSGPACK_MAJOR 0 -#define MSGPACK_MINOR 0 +#define MSGPACK_MINOR 1 #define MSGPACK_VERSION 0 namespace MsgPack { @@ -15,9 +15,14 @@ namespace MsgPack { * return type size */ typedef quint32 (*pack_user_f)(const QVariant &variant, QByteArray &data, bool write); - +/** + * unpack user type to QVariant + */ typedef QVariant (*unpack_user_f)(const QByteArray &data); - +/** + * @brief version + * @return current version + */ QString version(); } diff --git a/src/msgpack_common.h.in b/src/msgpack_common.h.in index 231c1b5..9e140bc 100644 --- a/src/msgpack_common.h.in +++ b/src/msgpack_common.h.in @@ -15,9 +15,14 @@ namespace MsgPack { * return type size */ typedef quint32 (*pack_user_f)(const QVariant &variant, QByteArray &data, bool write); - +/** + * unpack user type to QVariant + */ typedef QVariant (*unpack_user_f)(const QByteArray &data); - +/** + * @brief version + * @return current version + */ QString version(); } diff --git a/src/private/pack_p.cpp b/src/private/pack_p.cpp index 79f4492..0380df8 100644 --- a/src/private/pack_p.cpp +++ b/src/private/pack_p.cpp @@ -4,6 +4,7 @@ #include QHash MsgPackPrivate::user_packers; +bool MsgPackPrivate::compatibilityMode = false; quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr) { @@ -17,7 +18,7 @@ quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr) else if (t == QMetaType::QString) p = pack_string(v.toString(), p, wr); else if (t == QMetaType::QVariantList) - p = pack_list(v.toList(), p, wr); + p = pack_array(v.toList(), p, wr); else if (t == QMetaType::QStringList) p = pack_stringlist(v.toStringList(), p, wr); else if (t == QMetaType::LongLong) @@ -27,7 +28,7 @@ quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr) else if (t == QMetaType::Double) p = pack_double(v.toDouble(), p, wr); else if (t == QMetaType::QByteArray) - p = pack_array(v.toByteArray(), p, wr); + p = pack_bin(v.toByteArray(), p, wr); else if (t == QMetaType::QVariantMap) p = pack_map(v.toMap(), p, wr); else { @@ -122,7 +123,7 @@ quint8 *MsgPackPrivate::pack_bool(const QVariant &v, quint8 *p, bool wr) return p + 1; } -quint8 *MsgPackPrivate::pack_listlen(quint32 len, quint8 *p, bool wr) +quint8 *MsgPackPrivate::pack_arraylen(quint32 len, quint8 *p, bool wr) { if (len <= 15) { if (wr) *p = 0x90 | len; @@ -141,10 +142,10 @@ quint8 *MsgPackPrivate::pack_listlen(quint32 len, quint8 *p, bool wr) return p; } -quint8 *MsgPackPrivate::pack_list(const QVariantList &list, quint8 *p, bool wr) +quint8 *MsgPackPrivate::pack_array(const QVariantList &list, quint8 *p, bool wr) { int len = list.length(); - p = pack_listlen(len, p, wr); + p = pack_arraylen(len, p, wr); foreach (QVariant item, list) p = pack(item, p, wr); return p; @@ -153,7 +154,7 @@ quint8 *MsgPackPrivate::pack_list(const QVariantList &list, quint8 *p, bool wr) quint8 *MsgPackPrivate::pack_stringlist(const QStringList &list, quint8 *p, bool wr) { int len = list.length(); - p = pack_listlen(len, p, wr); + p = pack_arraylen(len, p, wr); foreach (QString item, list) p = pack_string(item, p, wr); return p; @@ -165,7 +166,8 @@ quint8 *MsgPackPrivate::pack_string(const QString &str, quint8 *p, bool wr) if (len <= 31) { if (wr) *p = 0xa0 | len; p++; - } else if (len <= std::numeric_limits::max()) { + } else if (len <= std::numeric_limits::max() && + compatibilityMode == false) { if (wr) *p = 0xd9; p++; if (wr) *p = len; @@ -203,21 +205,22 @@ quint8 *MsgPackPrivate::pack_double(double i, quint8 *p, bool wr) return p + 8; } -quint8 *MsgPackPrivate::pack_array(const QByteArray &arr, quint8 *p, bool wr) +quint8 *MsgPackPrivate::pack_bin(const QByteArray &arr, quint8 *p, bool wr) { int len = arr.length(); - if (len <= std::numeric_limits::max()) { + if (len <= std::numeric_limits::max() && + compatibilityMode == false) { if (wr) *p = 0xc4; p++; if (wr) *p = len; p++; } else if (len <= std::numeric_limits::max()) { - if (wr) *p = 0xc5; + if (wr) *p = compatibilityMode ? 0xc5 : 0xda; p++; if (wr) _msgpack_store16(p, len); p += 2; } else { - if (wr) *p = 0xc6; + if (wr) *p = compatibilityMode ? 0xc6 : 0xdb; p++; if (wr) _msgpack_store32(p, len); p += 4; diff --git a/src/private/pack_p.h b/src/private/pack_p.h index 5b8ceaf..ed0d5b3 100644 --- a/src/private/pack_p.h +++ b/src/private/pack_p.h @@ -13,6 +13,7 @@ typedef struct { } packer_t; bool register_packer(QMetaType::Type q_type, qint8 msgpack_type, MsgPack::pack_user_f packer); extern QHash user_packers; +extern bool compatibilityMode; quint8 * pack(const QVariant &v, quint8 *p, bool wr); @@ -23,13 +24,13 @@ quint8 * pack_ulonglong(quint64 i, quint8 *p, bool wr); quint8 * pack_bool(const QVariant &v, quint8 *p, bool wr); -quint8 * pack_listlen(quint32 len, quint8 *p, bool wr); -quint8 * pack_list(const QVariantList &list, quint8 *p, bool wr); +quint8 * pack_arraylen(quint32 len, quint8 *p, bool wr); +quint8 * pack_array(const QVariantList &list, quint8 *p, bool wr); quint8 * pack_stringlist(const QStringList &list, quint8 *p, bool wr); quint8 * pack_string(const QString &str, quint8 *p, bool wr); quint8 * pack_double(double i, quint8 *p, bool wr); -quint8 * pack_array(const QByteArray &arr, quint8 *p, bool wr); +quint8 * pack_bin(const QByteArray &arr, quint8 *p, bool wr); quint8 * pack_map(const QVariantMap &map, quint8 *p, bool wr); quint8 * pack_user(const QVariant &v, quint8 *p, bool wr); }