diff --git a/src/endianhelper.h b/src/endianhelper.h index 37139c6..ee8f551 100644 --- a/src/endianhelper.h +++ b/src/endianhelper.h @@ -15,8 +15,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MSGPACK_SYSDEP_H__ -#define MSGPACK_SYSDEP_H__ +#ifndef MSGPACK_ENDIAN_HELPER_H +#define MSGPACK_ENDIAN_HELPER_H #include #if defined(_WIN32) && defined(_MSC_VER) && _MSC_VER >= 1600 @@ -44,11 +44,12 @@ # if defined(ntohs) # define _msgpack_be16(x) ntohs(x) # elif defined(_byteswap_ushort) || (defined(_MSC_VER) && _MSC_VER >= 1400) -# define _msgpack_be16(x) ((uint16_t)_byteswap_ushort((unsigned short)x)) +# define _msgpack_be16(x) ( \ + static_cast(_byteswap_ushort(static_cast(x))) ) # else # define _msgpack_be16(x) ( \ - ((((quint16)x) << 8) ) | \ - ((((quint16)x) >> 8) ) ) + (((static_cast(x)) << 8) ) | \ + (((static_cast(x)) >> 8) ) ) # endif #else # define _msgpack_be16(x) ntohs(x) @@ -58,13 +59,14 @@ # if defined(ntohl) # define _msgpack_be32(x) ntohl(x) # elif defined(_byteswap_ulong) || (defined(_MSC_VER) && _MSC_VER >= 1400) -# define _msgpack_be32(x) ((uint32_t)_byteswap_ulong((unsigned long)x)) +# define _msgpack_be32(x) ( \ + static_cast(_byteswap_ulong(static_cast(x))) ) # else # define _msgpack_be32(x) \ - ( ((((quint32)x) << 24) ) | \ - ((((quint32)x) << 8) & 0x00ff0000U ) | \ - ((((quint32)x) >> 8) & 0x0000ff00U ) | \ - ((((quint32)x) >> 24) ) ) + ( (((static_cast(x)) << 24) ) | \ + (((static_cast(x)) << 8) & 0x00ff0000U ) | \ + (((static_cast(x)) >> 8) & 0x0000ff00U ) | \ + (((static_cast(x)) >> 24) ) ) # endif #else # define _msgpack_be32(x) ntohl(x) @@ -78,35 +80,35 @@ # define _msgpack_be64(x) __DARWIN_OSSwapInt64(x) #else #define _msgpack_be64(x) \ - ( ((((quint64)x) << 56) ) | \ - ((((quint64)x) << 40) & 0x00ff000000000000ULL ) | \ - ((((quint64)x) << 24) & 0x0000ff0000000000ULL ) | \ - ((((quint64)x) << 8) & 0x000000ff00000000ULL ) | \ - ((((quint64)x) >> 8) & 0x00000000ff000000ULL ) | \ - ((((quint64)x) >> 24) & 0x0000000000ff0000ULL ) | \ - ((((quint64)x) >> 40) & 0x000000000000ff00ULL ) | \ - ((((quint64)x) >> 56) ) ) + ( (((static_cast(x)) << 56) ) | \ + (((static_cast(x)) << 40) & 0x00ff000000000000ULL ) | \ + (((static_cast(x)) << 24) & 0x0000ff0000000000ULL ) | \ + (((static_cast(x)) << 8) & 0x000000ff00000000ULL ) | \ + (((static_cast(x)) >> 8) & 0x00000000ff000000ULL ) | \ + (((static_cast(x)) >> 24) & 0x0000000000ff0000ULL ) | \ + (((static_cast(x)) >> 40) & 0x000000000000ff00ULL ) | \ + (((static_cast(x)) >> 56) ) ) #endif -#define _msgpack_load16(cast, from) ((cast)( \ - (((quint16)((quint8*)(from))[0]) << 8) | \ - (((quint16)((quint8*)(from))[1]) ) )) +#define _msgpack_load16(type, from) ((static_cast( \ + ((static_cast(static_cast(from)[0])) << 8) | \ + ((static_cast(static_cast(from)[1])) ) ))) -#define _msgpack_load32(cast, from) ((cast)( \ - (((quint32)((quint8*)(from))[0]) << 24) | \ - (((quint32)((quint8*)(from))[1]) << 16) | \ - (((quint32)((quint8*)(from))[2]) << 8) | \ - (((quint32)((quint8*)(from))[3]) ) )) +#define _msgpack_load32(type, from) ((static_cast( \ + ((static_cast(static_cast(from)[0])) << 24) | \ + ((static_cast(static_cast(from)[1])) << 16) | \ + ((static_cast(static_cast(from)[2])) << 8) | \ + ((static_cast(static_cast(from)[3])) ) ))) -#define _msgpack_load64(cast, from) ((cast)( \ - (((quint64)((quint8*)(from))[0]) << 56) | \ - (((quint64)((quint8*)(from))[1]) << 48) | \ - (((quint64)((quint8*)(from))[2]) << 40) | \ - (((quint64)((quint8*)(from))[3]) << 32) | \ - (((quint64)((quint8*)(from))[4]) << 24) | \ - (((quint64)((quint8*)(from))[5]) << 16) | \ - (((quint64)((quint8*)(from))[6]) << 8) | \ - (((quint64)((quint8*)(from))[7]) ) )) +#define _msgpack_load64(type, from) ((static_cast( \ + ((static_cast(static_cast(from)[0])) << 56) | \ + ((static_cast(static_cast(from)[1])) << 48) | \ + ((static_cast(static_cast(from)[2])) << 40) | \ + ((static_cast(static_cast(from)[3])) << 32) | \ + ((static_cast(static_cast(from)[4])) << 24) | \ + ((static_cast(static_cast(from)[5])) << 16) | \ + ((static_cast(static_cast(from)[6])) << 8) | \ + ((static_cast(static_cast(from)[7])) ) ))) #else @@ -114,25 +116,25 @@ #define _msgpack_be32(x) (x) #define _msgpack_be64(x) (x) -#define _msgpack_load16(cast, from) ((cast)( \ - (((quint16)((quint8*)from)[0]) << 8) | \ - (((quint16)((quint8*)from)[1]) ) )) +#define _msgpack_load16(type, from) (static_cast( \ + ((static_cast(static_cast(from))[0]) << 8) | \ + ((static_cast(static_cast(from))[1]) ) )) -#define _msgpack_load32(cast, from) ((cast)( \ - (((quint32)((quint8*)from)[0]) << 24) | \ - (((quint32)((quint8*)from)[1]) << 16) | \ - (((quint32)((quint8*)from)[2]) << 8) | \ - (((quint32)((quint8*)from)[3]) ) )) +#define _msgpack_load32(type, from) (static_cast( \ + ((static_cast(static_cast(from))[0]) << 24) | \ + ((static_cast(static_cast(from))[1]) << 16) | \ + ((static_cast(static_cast(from))[2]) << 8) | \ + ((static_cast(static_cast(from))[3]) ) )) -#define _msgpack_load64(cast, from) ((cast)( \ - (((quint64)((quint8*)from)[0]) << 56) | \ - (((quint64)((quint8*)from)[1]) << 48) | \ - (((quint64)((quint8*)from)[2]) << 40) | \ - (((quint64)((quint8*)from)[3]) << 32) | \ - (((quint64)((quint8*)from)[4]) << 24) | \ - (((quint64)((quint8*)from)[5]) << 16) | \ - (((quint64)((quint8*)from)[6]) << 8) | \ - (((quint64)((quint8*)from)[7]) ) )) +#define _msgpack_load64(type, from) ((static_cast( \ + ((static_cast(static_cast(from))[0]) << 56) | \ + ((static_cast(static_cast(from))[1]) << 48) | \ + ((static_cast(static_cast(from))[2]) << 40) | \ + ((static_cast(static_cast(from))[3]) << 32) | \ + ((static_cast(static_cast(from))[4]) << 24) | \ + ((static_cast(static_cast(from))[5]) << 16) | \ + ((static_cast(static_cast(from))[6]) << 8) | \ + ((static_cast(static_cast(from))[7]) ) ))) #endif @@ -144,13 +146,13 @@ do { quint64 val = _msgpack_be64(num); memcpy(to, &val, 8); } while(0) /* -#define _msgpack_load16(cast, from) \ - ({ cast val; memcpy(&val, (char*)from, 2); _msgpack_be16(val); }) -#define _msgpack_load32(cast, from) \ - ({ cast val; memcpy(&val, (char*)from, 4); _msgpack_be32(val); }) -#define _msgpack_load64(cast, from) \ - ({ cast val; memcpy(&val, (char*)from, 8); _msgpack_be64(val); }) +#define _msgpack_load16(type, from) \ + ({ type val; memcpy(&val, (char*)from, 2); _msgpack_be16(val); }) +#define _msgpack_load32(type, from) \ + ({ type val; memcpy(&val, (char*)from, 4); _msgpack_be32(val); }) +#define _msgpack_load64(type, from) \ + ({ type val; memcpy(&val, (char*)from, 8); _msgpack_be64(val); }) */ -#endif /* msgpack/sysdep.h */ +#endif /* msgpack/endianhelper.h */ diff --git a/src/msgpack.cpp b/src/msgpack.cpp index 3bc8e8e..99b2519 100644 --- a/src/msgpack.cpp +++ b/src/msgpack.cpp @@ -9,8 +9,8 @@ QVariant MsgPack::unpack(const QByteArray &data) { - quint8 *p = (quint8 *)data.data(); - quint8 *end = p + data.size() - 1; + const quint8 *p = reinterpret_cast(data.data()); + const quint8 *end = p + data.size() - 1; return MsgPackPrivate::unpack(p, end); } @@ -22,10 +22,11 @@ QByteArray MsgPack::pack(const QVariant &variant) ptrdiff_t size = MsgPackPrivate::pack(variant, nullptr, false, user_data) - static_cast(nullptr); QByteArray arr; - arr.resize(size); + arr.resize(static_cast(size)); // second run, pack it - MsgPackPrivate::pack(variant, (quint8 *)arr.data(), true, user_data); + MsgPackPrivate::pack(variant, reinterpret_cast(arr.data()), true, + user_data); return arr; } @@ -42,7 +43,7 @@ bool MsgPack::registerUnpacker(qint8 msgpackType, MsgPack::unpack_user_f unpacke qint8 MsgPack::msgpackType(int qType) { - return MsgPackPrivate::msgpack_type((QMetaType::Type)qType); + return MsgPackPrivate::msgpack_type(static_cast(qType)); } bool MsgPack::registerType(QMetaType::Type qType, quint8 msgpackType) diff --git a/src/private/unpack_p.cpp b/src/private/unpack_p.cpp index 56d8100..067309d 100644 --- a/src/private/unpack_p.cpp +++ b/src/private/unpack_p.cpp @@ -47,17 +47,18 @@ MsgPackPrivate::type_parser_f MsgPackPrivate::unpackers[32] = { QHash MsgPackPrivate::user_unpackers; QReadWriteLock MsgPackPrivate::unpackers_lock; -QVariant MsgPackPrivate::unpack(quint8 *p, quint8 *end) +QVariant MsgPackPrivate::unpack(const quint8 *p, const quint8 *end) { QVariantList d; QVariant v; - while (p <= end) { - p = unpack_type(v, p); + quint8* pos = const_cast(p); // FIXME + while (pos <= end) { + pos = unpack_type(v, pos); d.append(v); } - if (p - end > 1) + if (pos - end > 1) return QVariant(); if (d.length() == 1) @@ -117,19 +118,19 @@ quint8 * MsgPackPrivate::unpack_true(QVariant &v, quint8 *p) quint8 * MsgPackPrivate::unpack_positive_fixint(QVariant &v, quint8 *p) { - v = (quint32)*p; + v = static_cast(*p); return p + 1; } quint8 * MsgPackPrivate::unpack_negative_fixint(QVariant &v, quint8 *p) { - v = (qint8)*p; + v = static_cast(*p); return p + 1; } quint8 * MsgPackPrivate::unpack_uint8(QVariant &v, quint8 *p) { - v = (quint8)*(++p); + v = static_cast(*(++p)); return p + 1; } @@ -156,7 +157,7 @@ quint8 * MsgPackPrivate::unpack_uint64(QVariant &v, quint8 *p) quint8 * MsgPackPrivate::unpack_int8(QVariant &v, quint8 *p) { - v = (qint8)*(++p); + v = static_cast(*(++p)); return p + 1; } @@ -184,7 +185,7 @@ quint8 * MsgPackPrivate::unpack_int64(QVariant &v, quint8 *p) quint8 * MsgPackPrivate::unpack_float32(QVariant &v, quint8 *p) { float f; - quint8 *fp = (quint8 *)&f; + quint8 *fp = reinterpret_cast(&f); p++; #ifdef __LITTLE_ENDIAN__ for (int i = 0; i < 4; ++i) @@ -200,7 +201,7 @@ quint8 * MsgPackPrivate::unpack_float32(QVariant &v, quint8 *p) quint8 * MsgPackPrivate::unpack_float64(QVariant &v, quint8 *p) { double d; - quint8 *fd = (quint8 *)&d; + quint8 *fd = reinterpret_cast(&d); p++; #ifdef __LITTLE_ENDIAN__ for (int i = 0; i < 8; ++i) @@ -217,14 +218,14 @@ quint8 * MsgPackPrivate::unpack_fixstr(QVariant &v, quint8 *p) { int len = (*p) & 0x1f; // 0b00011111 p++; - v = QString::fromUtf8((char*)p, len); + v = QString::fromUtf8(reinterpret_cast(p), len); return p + len; } quint8 * MsgPackPrivate::unpack_str8(QVariant &v, quint8 *p) { int len = *(++p); - v = QString::fromUtf8((char*)(++p), len); + v = QString::fromUtf8(reinterpret_cast((++p)), len); return p + len; } @@ -233,7 +234,7 @@ quint8 * MsgPackPrivate::unpack_str16(QVariant &v, quint8 *p) p++; int len = _msgpack_load16(int, p); p += 2; - v = QString::fromUtf8((char*)p, len); + v = QString::fromUtf8(reinterpret_cast(p), len); return p + len; } @@ -242,14 +243,14 @@ quint8 * MsgPackPrivate::unpack_str32(QVariant &v, quint8 *p) p++; int len = _msgpack_load32(int, p); p += 4; - v = QString::fromUtf8((char*)p, len); + v = QString::fromUtf8(reinterpret_cast(p), len); return p + len; } quint8 * MsgPackPrivate::unpack_bin8(QVariant &v, quint8 *p) { int len = *(++p); - v = QByteArray((char*)(++p), len); + v = QByteArray(reinterpret_cast((++p)), len); return p + len; } @@ -258,7 +259,7 @@ quint8 * MsgPackPrivate::unpack_bin16(QVariant &v, quint8 *p) p++; int len = _msgpack_load16(int, p); p += 2; - v = QByteArray((char*)p, len); + v = QByteArray(reinterpret_cast(p), len); return p + len; } @@ -267,7 +268,7 @@ quint8 * MsgPackPrivate::unpack_bin32(QVariant &v, quint8 *p) p++; int len = _msgpack_load32(int, p); p += 4; - v = QByteArray((char*)p, len); + v = QByteArray(reinterpret_cast(p), len); return p + len; } @@ -346,38 +347,38 @@ quint8 *MsgPackPrivate::unpack_ext(QVariant &v, quint8 *p, qint8 type, quint32 l qWarning() << "MsgPack::unpack() unpacker for type" << type << "doesn't exist"; return p + len; } - QByteArray data((char *)p, len); + QByteArray data(reinterpret_cast(p), static_cast(len)); v = user_unpackers[type](data); return p + len; } quint8 * MsgPackPrivate::unpack_fixext1(QVariant &v, quint8 *p) { - qint8 type = *(++p); + qint8 type = static_cast(*(++p)); return unpack_ext(v, p + 1, type, 1); } quint8 * MsgPackPrivate::unpack_fixext2(QVariant &v, quint8 *p) { - qint8 type = *(++p); + qint8 type = static_cast(*(++p)); return unpack_ext(v, p + 1, type, 2); } quint8 * MsgPackPrivate::unpack_fixext4(QVariant &v, quint8 *p) { - qint8 type = *(++p); + qint8 type = static_cast(*(++p)); return unpack_ext(v, p + 1, type, 4); } quint8 * MsgPackPrivate::unpack_fixext8(QVariant &v, quint8 *p) { - qint8 type = *(++p); + qint8 type = static_cast(*(++p)); return unpack_ext(v, p + 1, type, 8); } quint8 * MsgPackPrivate::unpack_fixext16(QVariant &v, quint8 *p) { - qint8 type = *(++p); + qint8 type = static_cast(*(++p)); return unpack_ext(v, p + 1, type, 16); } @@ -386,7 +387,7 @@ quint8 * MsgPackPrivate::unpack_ext8(QVariant &v, quint8 *p) p++; quint32 len = *(p); p += 1; - qint8 type = *(p); + qint8 type = static_cast(*p); return unpack_ext(v, p + 1, type, len); } @@ -395,7 +396,7 @@ quint8 * MsgPackPrivate::unpack_ext16(QVariant &v, quint8 *p) p++; quint32 len = _msgpack_load16(quint32, p); p += 2; - qint8 type = *(p); + qint8 type = static_cast(*p); return unpack_ext(v, p + 1, type, len); } @@ -404,13 +405,13 @@ quint8 * MsgPackPrivate::unpack_ext32(QVariant &v, quint8 *p) p++; quint32 len = _msgpack_load32(quint32, p); p += 4; - qint8 type = *(p); + qint8 type = static_cast(*p); return unpack_ext(v, p + 1, type, len); } bool MsgPackPrivate::register_unpacker(qint8 msgpack_type, MsgPack::unpack_user_f unpacker) { - if (unpacker == 0) { + if (unpacker == nullptr) { qWarning() << "MsgPack::unpacker for type" << msgpack_type << "is invalid"; return false; } diff --git a/src/private/unpack_p.h b/src/private/unpack_p.h index cce8fe4..b44151a 100644 --- a/src/private/unpack_p.h +++ b/src/private/unpack_p.h @@ -23,7 +23,7 @@ extern QHash user_unpackers; extern QReadWriteLock unpackers_lock; // goes from p to end unpacking types with unpack_type function below -QVariant unpack(quint8 *p, quint8 *end); +QVariant unpack(const quint8 *p, const quint8 *end); // unpack some type, can be called recursively from other unpack functions quint8 * unpack_type(QVariant &v, quint8 *p);