1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 #include <QtCore/qglobal.h>
47 // include stdlib.h and hope that it defines __GLIBC__ for glibc-based systems
63 inline void qbswap_helper(const uchar *src, uchar *dest, int size)
65 for (int i = 0; i < size ; ++i) dest[i] = src[size - 1 - i];
69 * qbswap(const T src, const uchar *dest);
70 * Changes the byte order of \a src from big endian to little endian or vice versa
71 * and stores the result in \a dest.
72 * There is no alignment requirements for \a dest.
74 template <typename T> inline void qbswap(const T src, uchar *dest)
76 qbswap_helper(reinterpret_cast<const uchar *>(&src), dest, sizeof(T));
79 // Used to implement a type-safe and alignment-safe copy operation
80 // If you want to avoid the memcopy, you must write specializations for this function
81 template <typename T> inline void qToUnaligned(const T src, uchar *dest)
83 qMemCopy(dest, &src, sizeof(T));
86 /* T qFromLittleEndian(const uchar *src)
87 * This function will read a little-endian encoded value from \a src
88 * and return the value in host-endian encoding.
89 * There is no requirement that \a src must be aligned.
92 inline quint64 qFromLittleEndian_helper(const uchar *src, quint64 *dest)
96 | src[1] * Q_UINT64_C(0x0000000000000100)
97 | src[2] * Q_UINT64_C(0x0000000000010000)
98 | src[3] * Q_UINT64_C(0x0000000001000000)
99 | src[4] * Q_UINT64_C(0x0000000100000000)
100 | src[5] * Q_UINT64_C(0x0000010000000000)
101 | src[6] * Q_UINT64_C(0x0001000000000000)
102 | src[7] * Q_UINT64_C(0x0100000000000000);
105 inline quint32 qFromLittleEndian_helper(const uchar *src, quint32 *dest)
109 | src[1] * quint32(0x00000100)
110 | src[2] * quint32(0x00010000)
111 | src[3] * quint32(0x01000000);
114 inline quint16 qFromLittleEndian_helper(const uchar *src, quint16 *dest)
121 inline qint64 qFromLittleEndian_helper(const uchar *src, qint64 * dest)
122 { return static_cast<qint64>(qFromLittleEndian_helper(src, reinterpret_cast<quint64*>(0))); }
123 inline qint32 qFromLittleEndian_helper(const uchar *src, qint32 * dest)
124 { return static_cast<qint32>(qFromLittleEndian_helper(src, reinterpret_cast<quint32*>(0))); }
125 inline qint16 qFromLittleEndian_helper(const uchar *src, qint16 * dest)
126 { return static_cast<qint16>(qFromLittleEndian_helper(src, reinterpret_cast<quint16*>(0))); }
128 template <class T> inline T qFromLittleEndian(const uchar *src)
130 return qFromLittleEndian_helper(src, reinterpret_cast<T*>(0));
134 template <typename T> inline T qFromLittleEndian(const uchar *src);
135 template <> inline quint64 qFromLittleEndian<quint64>(const uchar *src)
139 | src[1] * Q_UINT64_C(0x0000000000000100)
140 | src[2] * Q_UINT64_C(0x0000000000010000)
141 | src[3] * Q_UINT64_C(0x0000000001000000)
142 | src[4] * Q_UINT64_C(0x0000000100000000)
143 | src[5] * Q_UINT64_C(0x0000010000000000)
144 | src[6] * Q_UINT64_C(0x0001000000000000)
145 | src[7] * Q_UINT64_C(0x0100000000000000);
148 template <> inline quint32 qFromLittleEndian<quint32>(const uchar *src)
152 | src[1] * quint32(0x00000100)
153 | src[2] * quint32(0x00010000)
154 | src[3] * quint32(0x01000000);
157 template <> inline quint16 qFromLittleEndian<quint16>(const uchar *src)
164 // signed specializations
165 template <> inline qint64 qFromLittleEndian<qint64>(const uchar *src)
166 { return static_cast<qint64>(qFromLittleEndian<quint64>(src)); }
168 template <> inline qint32 qFromLittleEndian<qint32>(const uchar *src)
169 { return static_cast<qint32>(qFromLittleEndian<quint32>(src)); }
171 template <> inline qint16 qFromLittleEndian<qint16>(const uchar *src)
172 { return static_cast<qint16>(qFromLittleEndian<quint16>(src)); }
175 /* This function will read a big-endian (also known as network order) encoded value from \a src
176 * and return the value in host-endian encoding.
177 * There is no requirement that \a src must be aligned.
180 inline quint64 qFromBigEndian_helper(const uchar *src, quint64 *dest)
184 | src[6] * Q_UINT64_C(0x0000000000000100)
185 | src[5] * Q_UINT64_C(0x0000000000010000)
186 | src[4] * Q_UINT64_C(0x0000000001000000)
187 | src[3] * Q_UINT64_C(0x0000000100000000)
188 | src[2] * Q_UINT64_C(0x0000010000000000)
189 | src[1] * Q_UINT64_C(0x0001000000000000)
190 | src[0] * Q_UINT64_C(0x0100000000000000);
193 inline quint32 qFromBigEndian_helper(const uchar *src, quint32 * dest)
197 | src[2] * quint32(0x00000100)
198 | src[1] * quint32(0x00010000)
199 | src[0] * quint32(0x01000000);
202 inline quint16 qFromBigEndian_helper(const uchar *src, quint16 * des)
210 inline qint64 qFromBigEndian_helper(const uchar *src, qint64 * dest)
211 { return static_cast<qint64>(qFromBigEndian_helper(src, reinterpret_cast<quint64*>(0))); }
212 inline qint32 qFromBigEndian_helper(const uchar *src, qint32 * dest)
213 { return static_cast<qint32>(qFromBigEndian_helper(src, reinterpret_cast<quint32*>(0))); }
214 inline qint16 qFromBigEndian_helper(const uchar *src, qint16 * dest)
215 { return static_cast<qint16>(qFromBigEndian_helper(src, reinterpret_cast<quint16*>(0))); }
217 template <class T> inline T qFromBigEndian(const uchar *src)
219 return qFromBigEndian_helper(src, reinterpret_cast<T*>(0));
223 template <class T> inline T qFromBigEndian(const uchar *src);
225 inline quint64 qFromBigEndian<quint64>(const uchar *src)
229 | src[6] * Q_UINT64_C(0x0000000000000100)
230 | src[5] * Q_UINT64_C(0x0000000000010000)
231 | src[4] * Q_UINT64_C(0x0000000001000000)
232 | src[3] * Q_UINT64_C(0x0000000100000000)
233 | src[2] * Q_UINT64_C(0x0000010000000000)
234 | src[1] * Q_UINT64_C(0x0001000000000000)
235 | src[0] * Q_UINT64_C(0x0100000000000000);
239 inline quint32 qFromBigEndian<quint32>(const uchar *src)
243 | src[2] * quint32(0x00000100)
244 | src[1] * quint32(0x00010000)
245 | src[0] * quint32(0x01000000);
249 inline quint16 qFromBigEndian<quint16>(const uchar *src)
253 | src[0] * quint16(0x0100));
257 // signed specializations
258 template <> inline qint64 qFromBigEndian<qint64>(const uchar *src)
259 { return static_cast<qint64>(qFromBigEndian<quint64>(src)); }
261 template <> inline qint32 qFromBigEndian<qint32>(const uchar *src)
262 { return static_cast<qint32>(qFromBigEndian<quint32>(src)); }
264 template <> inline qint16 qFromBigEndian<qint16>(const uchar *src)
265 { return static_cast<qint16>(qFromBigEndian<quint16>(src)); }
268 * T qbswap(T source).
269 * Changes the byte order of a value from big endian to little endian or vice versa.
270 * This function can be used if you are not concerned about alignment issues,
271 * and it is therefore a bit more convenient and in most cases more efficient.
273 template <typename T> T qbswap(T source);
276 template <> inline quint64 qbswap<quint64>(quint64 source)
278 return bswap_64(source);
280 template <> inline quint32 qbswap<quint32>(quint32 source)
282 return bswap_32(source);
284 template <> inline quint16 qbswap<quint16>(quint16 source)
286 return bswap_16(source);
289 template <> inline quint64 qbswap<quint64>(quint64 source)
292 | ((source & Q_UINT64_C(0x00000000000000ff)) << 56)
293 | ((source & Q_UINT64_C(0x000000000000ff00)) << 40)
294 | ((source & Q_UINT64_C(0x0000000000ff0000)) << 24)
295 | ((source & Q_UINT64_C(0x00000000ff000000)) << 8)
296 | ((source & Q_UINT64_C(0x000000ff00000000)) >> 8)
297 | ((source & Q_UINT64_C(0x0000ff0000000000)) >> 24)
298 | ((source & Q_UINT64_C(0x00ff000000000000)) >> 40)
299 | ((source & Q_UINT64_C(0xff00000000000000)) >> 56);
302 template <> inline quint32 qbswap<quint32>(quint32 source)
305 | ((source & 0x000000ff) << 24)
306 | ((source & 0x0000ff00) << 8)
307 | ((source & 0x00ff0000) >> 8)
308 | ((source & 0xff000000) >> 24);
311 template <> inline quint16 qbswap<quint16>(quint16 source)
314 | ((source & 0x00ff) << 8)
315 | ((source & 0xff00) >> 8) );
319 // signed specializations
320 template <> inline qint64 qbswap<qint64>(qint64 source)
322 return qbswap<quint64>(quint64(source));
325 template <> inline qint32 qbswap<qint32>(qint32 source)
327 return qbswap<quint32>(quint32(source));
330 template <> inline qint16 qbswap<qint16>(qint16 source)
332 return qbswap<quint16>(quint16(source));
335 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
337 template <typename T> inline T qToBigEndian(T source)
339 template <typename T> inline T qFromBigEndian(T source)
341 template <typename T> inline T qToLittleEndian(T source)
342 { return qbswap<T>(source); }
343 template <typename T> inline T qFromLittleEndian(T source)
344 { return qbswap<T>(source); }
345 template <typename T> inline void qToBigEndian(T src, uchar *dest)
346 { qToUnaligned<T>(src, dest); }
347 template <typename T> inline void qToLittleEndian(T src, uchar *dest)
348 { qbswap<T>(src, dest); }
349 #else // Q_LITTLE_ENDIAN
351 template <typename T> inline T qToBigEndian(T source)
352 { return qbswap<T>(source); }
353 template <typename T> inline T qFromBigEndian(T source)
354 { return qbswap<T>(source); }
355 template <typename T> inline T qToLittleEndian(T source)
357 template <typename T> inline T qFromLittleEndian(T source)
359 template <typename T> inline void qToBigEndian(T src, uchar *dest)
360 { qbswap<T>(src, dest); }
361 template <typename T> inline void qToLittleEndian(T src, uchar *dest)
362 { qToUnaligned<T>(src, dest); }
364 #endif // Q_BYTE_ORDER == Q_BIG_ENDIAN
366 template <> inline quint8 qbswap<quint8>(quint8 source)