author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#ifndef QENDIAN_H |
|
43 |
#define QENDIAN_H |
|
44 |
||
45 |
#include <QtCore/qglobal.h> |
|
46 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
47 |
#ifdef Q_OS_LINUX |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
48 |
# include <features.h> |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
49 |
#endif |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
50 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
51 |
#ifdef __GLIBC__ |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
52 |
#include <byteswap.h> |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
53 |
#endif |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
54 |
|
0 | 55 |
QT_BEGIN_HEADER |
56 |
||
57 |
QT_BEGIN_NAMESPACE |
|
58 |
||
59 |
QT_MODULE(Core) |
|
60 |
||
61 |
/* |
|
62 |
* ENDIAN FUNCTIONS |
|
63 |
*/ |
|
64 |
inline void qbswap_helper(const uchar *src, uchar *dest, int size) |
|
65 |
{ |
|
66 |
for (int i = 0; i < size ; ++i) dest[i] = src[size - 1 - i]; |
|
67 |
} |
|
68 |
||
69 |
/* |
|
70 |
* qbswap(const T src, const uchar *dest); |
|
71 |
* Changes the byte order of \a src from big endian to little endian or vice versa |
|
72 |
* and stores the result in \a dest. |
|
73 |
* There is no alignment requirements for \a dest. |
|
74 |
*/ |
|
75 |
template <typename T> inline void qbswap(const T src, uchar *dest) |
|
76 |
{ |
|
77 |
qbswap_helper(reinterpret_cast<const uchar *>(&src), dest, sizeof(T)); |
|
78 |
} |
|
79 |
||
80 |
// Used to implement a type-safe and alignment-safe copy operation |
|
81 |
// If you want to avoid the memcopy, you must write specializations for this function |
|
82 |
template <typename T> inline void qToUnaligned(const T src, uchar *dest) |
|
83 |
{ |
|
84 |
qMemCopy(dest, &src, sizeof(T)); |
|
85 |
} |
|
86 |
||
87 |
/* T qFromLittleEndian(const uchar *src) |
|
88 |
* This function will read a little-endian encoded value from \a src |
|
89 |
* and return the value in host-endian encoding. |
|
90 |
* There is no requirement that \a src must be aligned. |
|
91 |
*/ |
|
92 |
#if defined Q_CC_MSVC && _MSC_VER < 1300 || defined Q_CC_SUN |
|
93 |
inline quint64 qFromLittleEndian_helper(const uchar *src, quint64 *dest) |
|
94 |
{ |
|
95 |
return 0 |
|
96 |
| src[0] |
|
97 |
| src[1] * Q_UINT64_C(0x0000000000000100) |
|
98 |
| src[2] * Q_UINT64_C(0x0000000000010000) |
|
99 |
| src[3] * Q_UINT64_C(0x0000000001000000) |
|
100 |
| src[4] * Q_UINT64_C(0x0000000100000000) |
|
101 |
| src[5] * Q_UINT64_C(0x0000010000000000) |
|
102 |
| src[6] * Q_UINT64_C(0x0001000000000000) |
|
103 |
| src[7] * Q_UINT64_C(0x0100000000000000); |
|
104 |
} |
|
105 |
||
106 |
inline quint32 qFromLittleEndian_helper(const uchar *src, quint32 *dest) |
|
107 |
{ |
|
108 |
return 0 |
|
109 |
| src[0] |
|
110 |
| src[1] * quint32(0x00000100) |
|
111 |
| src[2] * quint32(0x00010000) |
|
112 |
| src[3] * quint32(0x01000000); |
|
113 |
} |
|
114 |
||
115 |
inline quint16 qFromLittleEndian_helper(const uchar *src, quint16 *dest) |
|
116 |
{ |
|
117 |
return 0 |
|
118 |
| src[0] |
|
119 |
| src[1] * 0x0100; |
|
120 |
} |
|
121 |
||
122 |
inline qint64 qFromLittleEndian_helper(const uchar *src, qint64 * dest) |
|
123 |
{ return static_cast<qint64>(qFromLittleEndian_helper(src, reinterpret_cast<quint64*>(0))); } |
|
124 |
inline qint32 qFromLittleEndian_helper(const uchar *src, qint32 * dest) |
|
125 |
{ return static_cast<qint32>(qFromLittleEndian_helper(src, reinterpret_cast<quint32*>(0))); } |
|
126 |
inline qint16 qFromLittleEndian_helper(const uchar *src, qint16 * dest) |
|
127 |
{ return static_cast<qint16>(qFromLittleEndian_helper(src, reinterpret_cast<quint16*>(0))); } |
|
128 |
||
129 |
template <class T> inline T qFromLittleEndian(const uchar *src) |
|
130 |
{ |
|
131 |
return qFromLittleEndian_helper(src, reinterpret_cast<T*>(0)); |
|
132 |
} |
|
133 |
||
134 |
#else |
|
135 |
template <typename T> inline T qFromLittleEndian(const uchar *src); |
|
136 |
template <> inline quint64 qFromLittleEndian<quint64>(const uchar *src) |
|
137 |
{ |
|
138 |
return 0 |
|
139 |
| src[0] |
|
140 |
| src[1] * Q_UINT64_C(0x0000000000000100) |
|
141 |
| src[2] * Q_UINT64_C(0x0000000000010000) |
|
142 |
| src[3] * Q_UINT64_C(0x0000000001000000) |
|
143 |
| src[4] * Q_UINT64_C(0x0000000100000000) |
|
144 |
| src[5] * Q_UINT64_C(0x0000010000000000) |
|
145 |
| src[6] * Q_UINT64_C(0x0001000000000000) |
|
146 |
| src[7] * Q_UINT64_C(0x0100000000000000); |
|
147 |
} |
|
148 |
||
149 |
template <> inline quint32 qFromLittleEndian<quint32>(const uchar *src) |
|
150 |
{ |
|
151 |
return 0 |
|
152 |
| src[0] |
|
153 |
| src[1] * quint32(0x00000100) |
|
154 |
| src[2] * quint32(0x00010000) |
|
155 |
| src[3] * quint32(0x01000000); |
|
156 |
} |
|
157 |
||
158 |
template <> inline quint16 qFromLittleEndian<quint16>(const uchar *src) |
|
159 |
{ |
|
160 |
return quint16(0 |
|
161 |
| src[0] |
|
162 |
| src[1] * 0x0100); |
|
163 |
} |
|
164 |
||
165 |
// signed specializations |
|
166 |
template <> inline qint64 qFromLittleEndian<qint64>(const uchar *src) |
|
167 |
{ return static_cast<qint64>(qFromLittleEndian<quint64>(src)); } |
|
168 |
||
169 |
template <> inline qint32 qFromLittleEndian<qint32>(const uchar *src) |
|
170 |
{ return static_cast<qint32>(qFromLittleEndian<quint32>(src)); } |
|
171 |
||
172 |
template <> inline qint16 qFromLittleEndian<qint16>(const uchar *src) |
|
173 |
{ return static_cast<qint16>(qFromLittleEndian<quint16>(src)); } |
|
174 |
#endif |
|
175 |
||
176 |
/* This function will read a big-endian (also known as network order) encoded value from \a src |
|
177 |
* and return the value in host-endian encoding. |
|
178 |
* There is no requirement that \a src must be aligned. |
|
179 |
*/ |
|
180 |
#if defined Q_CC_MSVC && _MSC_VER < 1300 || defined Q_CC_SUN |
|
181 |
inline quint64 qFromBigEndian_helper(const uchar *src, quint64 *dest) |
|
182 |
{ |
|
183 |
return 0 |
|
184 |
| src[7] |
|
185 |
| src[6] * Q_UINT64_C(0x0000000000000100) |
|
186 |
| src[5] * Q_UINT64_C(0x0000000000010000) |
|
187 |
| src[4] * Q_UINT64_C(0x0000000001000000) |
|
188 |
| src[3] * Q_UINT64_C(0x0000000100000000) |
|
189 |
| src[2] * Q_UINT64_C(0x0000010000000000) |
|
190 |
| src[1] * Q_UINT64_C(0x0001000000000000) |
|
191 |
| src[0] * Q_UINT64_C(0x0100000000000000); |
|
192 |
} |
|
193 |
||
194 |
inline quint32 qFromBigEndian_helper(const uchar *src, quint32 * dest) |
|
195 |
{ |
|
196 |
return 0 |
|
197 |
| src[3] |
|
198 |
| src[2] * quint32(0x00000100) |
|
199 |
| src[1] * quint32(0x00010000) |
|
200 |
| src[0] * quint32(0x01000000); |
|
201 |
} |
|
202 |
||
203 |
inline quint16 qFromBigEndian_helper(const uchar *src, quint16 * des) |
|
204 |
{ |
|
205 |
return 0 |
|
206 |
| src[1] |
|
207 |
| src[0] * 0x0100; |
|
208 |
} |
|
209 |
||
210 |
||
211 |
inline qint64 qFromBigEndian_helper(const uchar *src, qint64 * dest) |
|
212 |
{ return static_cast<qint64>(qFromBigEndian_helper(src, reinterpret_cast<quint64*>(0))); } |
|
213 |
inline qint32 qFromBigEndian_helper(const uchar *src, qint32 * dest) |
|
214 |
{ return static_cast<qint32>(qFromBigEndian_helper(src, reinterpret_cast<quint32*>(0))); } |
|
215 |
inline qint16 qFromBigEndian_helper(const uchar *src, qint16 * dest) |
|
216 |
{ return static_cast<qint16>(qFromBigEndian_helper(src, reinterpret_cast<quint16*>(0))); } |
|
217 |
||
218 |
template <class T> inline T qFromBigEndian(const uchar *src) |
|
219 |
{ |
|
220 |
return qFromBigEndian_helper(src, reinterpret_cast<T*>(0)); |
|
221 |
} |
|
222 |
||
223 |
#else |
|
224 |
template <class T> inline T qFromBigEndian(const uchar *src); |
|
225 |
template<> |
|
226 |
inline quint64 qFromBigEndian<quint64>(const uchar *src) |
|
227 |
{ |
|
228 |
return 0 |
|
229 |
| src[7] |
|
230 |
| src[6] * Q_UINT64_C(0x0000000000000100) |
|
231 |
| src[5] * Q_UINT64_C(0x0000000000010000) |
|
232 |
| src[4] * Q_UINT64_C(0x0000000001000000) |
|
233 |
| src[3] * Q_UINT64_C(0x0000000100000000) |
|
234 |
| src[2] * Q_UINT64_C(0x0000010000000000) |
|
235 |
| src[1] * Q_UINT64_C(0x0001000000000000) |
|
236 |
| src[0] * Q_UINT64_C(0x0100000000000000); |
|
237 |
} |
|
238 |
||
239 |
template<> |
|
240 |
inline quint32 qFromBigEndian<quint32>(const uchar *src) |
|
241 |
{ |
|
242 |
return 0 |
|
243 |
| src[3] |
|
244 |
| src[2] * quint32(0x00000100) |
|
245 |
| src[1] * quint32(0x00010000) |
|
246 |
| src[0] * quint32(0x01000000); |
|
247 |
} |
|
248 |
||
249 |
template<> |
|
250 |
inline quint16 qFromBigEndian<quint16>(const uchar *src) |
|
251 |
{ |
|
252 |
return quint16( 0 |
|
253 |
| src[1] |
|
254 |
| src[0] * quint16(0x0100)); |
|
255 |
} |
|
256 |
||
257 |
||
258 |
// signed specializations |
|
259 |
template <> inline qint64 qFromBigEndian<qint64>(const uchar *src) |
|
260 |
{ return static_cast<qint64>(qFromBigEndian<quint64>(src)); } |
|
261 |
||
262 |
template <> inline qint32 qFromBigEndian<qint32>(const uchar *src) |
|
263 |
{ return static_cast<qint32>(qFromBigEndian<quint32>(src)); } |
|
264 |
||
265 |
template <> inline qint16 qFromBigEndian<qint16>(const uchar *src) |
|
266 |
{ return static_cast<qint16>(qFromBigEndian<quint16>(src)); } |
|
267 |
#endif |
|
268 |
/* |
|
269 |
* T qbswap(T source). |
|
270 |
* Changes the byte order of a value from big endian to little endian or vice versa. |
|
271 |
* This function can be used if you are not concerned about alignment issues, |
|
272 |
* and it is therefore a bit more convenient and in most cases more efficient. |
|
273 |
*/ |
|
274 |
template <typename T> T qbswap(T source); |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
275 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
276 |
#ifdef __GLIBC__ |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
277 |
template <> inline quint64 qbswap<quint64>(quint64 source) |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
278 |
{ |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
279 |
return bswap_64(source); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
280 |
} |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
281 |
template <> inline quint32 qbswap<quint32>(quint32 source) |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
282 |
{ |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
283 |
return bswap_32(source); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
284 |
} |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
285 |
template <> inline quint16 qbswap<quint16>(quint16 source) |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
286 |
{ |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
287 |
return bswap_16(source); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
288 |
} |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
289 |
#else |
0 | 290 |
template <> inline quint64 qbswap<quint64>(quint64 source) |
291 |
{ |
|
292 |
return 0 |
|
293 |
| ((source & Q_UINT64_C(0x00000000000000ff)) << 56) |
|
294 |
| ((source & Q_UINT64_C(0x000000000000ff00)) << 40) |
|
295 |
| ((source & Q_UINT64_C(0x0000000000ff0000)) << 24) |
|
296 |
| ((source & Q_UINT64_C(0x00000000ff000000)) << 8) |
|
297 |
| ((source & Q_UINT64_C(0x000000ff00000000)) >> 8) |
|
298 |
| ((source & Q_UINT64_C(0x0000ff0000000000)) >> 24) |
|
299 |
| ((source & Q_UINT64_C(0x00ff000000000000)) >> 40) |
|
300 |
| ((source & Q_UINT64_C(0xff00000000000000)) >> 56); |
|
301 |
} |
|
302 |
||
303 |
template <> inline quint32 qbswap<quint32>(quint32 source) |
|
304 |
{ |
|
305 |
return 0 |
|
306 |
| ((source & 0x000000ff) << 24) |
|
307 |
| ((source & 0x0000ff00) << 8) |
|
308 |
| ((source & 0x00ff0000) >> 8) |
|
309 |
| ((source & 0xff000000) >> 24); |
|
310 |
} |
|
311 |
||
312 |
template <> inline quint16 qbswap<quint16>(quint16 source) |
|
313 |
{ |
|
314 |
return quint16( 0 |
|
315 |
| ((source & 0x00ff) << 8) |
|
316 |
| ((source & 0xff00) >> 8) ); |
|
317 |
} |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
318 |
#endif // __GLIBC__ |
0 | 319 |
|
320 |
// signed specializations |
|
321 |
template <> inline qint64 qbswap<qint64>(qint64 source) |
|
322 |
{ |
|
323 |
return qbswap<quint64>(quint64(source)); |
|
324 |
} |
|
325 |
||
326 |
template <> inline qint32 qbswap<qint32>(qint32 source) |
|
327 |
{ |
|
328 |
return qbswap<quint32>(quint32(source)); |
|
329 |
} |
|
330 |
||
331 |
template <> inline qint16 qbswap<qint16>(qint16 source) |
|
332 |
{ |
|
333 |
return qbswap<quint16>(quint16(source)); |
|
334 |
} |
|
335 |
||
336 |
#if Q_BYTE_ORDER == Q_BIG_ENDIAN |
|
337 |
||
338 |
template <typename T> inline T qToBigEndian(T source) |
|
339 |
{ return source; } |
|
340 |
template <typename T> inline T qFromBigEndian(T source) |
|
341 |
{ return source; } |
|
342 |
template <typename T> inline T qToLittleEndian(T source) |
|
343 |
{ return qbswap<T>(source); } |
|
344 |
template <typename T> inline T qFromLittleEndian(T source) |
|
345 |
{ return qbswap<T>(source); } |
|
346 |
template <typename T> inline void qToBigEndian(T src, uchar *dest) |
|
347 |
{ qToUnaligned<T>(src, dest); } |
|
348 |
template <typename T> inline void qToLittleEndian(T src, uchar *dest) |
|
349 |
{ qbswap<T>(src, dest); } |
|
350 |
#else // Q_LITTLE_ENDIAN |
|
351 |
||
352 |
template <typename T> inline T qToBigEndian(T source) |
|
353 |
{ return qbswap<T>(source); } |
|
354 |
template <typename T> inline T qFromBigEndian(T source) |
|
355 |
{ return qbswap<T>(source); } |
|
356 |
template <typename T> inline T qToLittleEndian(T source) |
|
357 |
{ return source; } |
|
358 |
template <typename T> inline T qFromLittleEndian(T source) |
|
359 |
{ return source; } |
|
360 |
template <typename T> inline void qToBigEndian(T src, uchar *dest) |
|
361 |
{ qbswap<T>(src, dest); } |
|
362 |
template <typename T> inline void qToLittleEndian(T src, uchar *dest) |
|
363 |
{ qToUnaligned<T>(src, dest); } |
|
364 |
||
365 |
#endif // Q_BYTE_ORDER == Q_BIG_ENDIAN |
|
366 |
||
367 |
QT_END_NAMESPACE |
|
368 |
||
369 |
QT_END_HEADER |
|
370 |
||
371 |
#endif // QENDIAN_H |