Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef EndianHelperHPP
00033 #define EndianHelperHPP
00034
00035 #include <zeusbase/System/ZObject.h>
00036
00037 BEGIN_NAMESPACE_Zeus
00038
00039
00043
00044 template <typename T> class TEndianHelper : public TZObject
00045 {
00046 public:
00047
00048
00053
00054 inline T convertFromLittleEndianBuffer(const Uint8* pucSource)
00055 {
00056 return convertFromLittleEndianHelper(pucSource, reinterpret_cast<T*>(0));
00057 }
00058
00059
00064
00065 inline T convertFromBigEndianBuffer(const Uint8* pucSource)
00066 {
00067 return convertFromBigEndianHelper(pucSource, reinterpret_cast<T*>(0));
00068 }
00069
00070
00077
00078 inline T convertFromBuffer(const Uint8* pucSource)
00079 {
00080 #if defined(ZEUS_BIG_ENDIAN)
00081 return convertFromBigEndianHelper(pucSource, reinterpret_cast<T*>(0));
00082 #else
00083 return convertFromLittleEndianHelper(pucSource, reinterpret_cast<T*>(0));
00084 #endif
00085 }
00086
00087
00090
00091 inline T toLittleEndian(T tSource)
00092 {
00093 #if defined(ZEUS_BIG_ENDIAN)
00094 return swapBytes<T>(tSource);
00095 #else
00096 return tSource;
00097 #endif
00098 }
00099
00100
00101
00104
00105 inline T toBigEndian(T tSource)
00106 {
00107 #if defined(ZEUS_BIG_ENDIAN)
00108 return tSource;
00109 #else
00110 return swapBytes<T>(tSource);
00111 #endif
00112 }
00113
00114
00120
00121 inline void toLittleEndianStream(T tSource, Uint8* pucDest)
00122 {
00123 #if defined(ZEUS_BIG_ENDIAN)
00124 swapBytes<T>(tSource, pucDest);
00125 #else
00126 memcpy(pucDest, &tSource, sizeof(T));
00127 #endif
00128 }
00129
00130
00136
00137 inline void toBigEndianStream(T source, Uint8* pucDest)
00138 {
00139 #if defined(ZEUS_BIG_ENDIAN)
00140 memcpy(pucDest, &tSource, sizeof(T));
00141 #else
00142 swapBytes<T>(tSource, pucDest);
00143 #endif
00144 }
00145
00146
00149
00150 inline void swapBytes(const T tSource, Uint8* pucDest)
00151 {
00152 Uint8* pucSource = reinterpret_cast<const Uint8*>(&tSource);
00153 Int iSize = sizeof(T);
00154
00155 for (Int i = 0; i < iSize; ++i)
00156 {
00157 pucDest[i] = pucSource[iSize - 1 - i];
00158 }
00159 }
00160
00161
00164
00165 inline Uint16 swapBytes(Uint16 usSource)
00166 {
00167 return 0 |
00168 ((usSource & 0x00ff) << 8) |
00169 ((usSource & 0xff00) >> 8);
00170 }
00171
00172
00175
00176 inline Uint32 swapBytes(Uint32 ulSource)
00177 {
00178 return 0 |
00179 ((ulSource & 0x000000ff) << 24) |
00180 ((ulSource & 0x0000ff00) << 8) |
00181 ((ulSource & 0x00ff0000) >> 8) |
00182 ((ulSource & 0xff000000) >> 24);
00183 }
00184
00185
00188
00189 inline Uint64 swapBytes(Uint64 uldSource)
00190 {
00191 return 0 |
00192 ((uldSource & 0x00000000000000ffLL) << 56) |
00193 ((uldSource & 0x000000000000ff00LL) << 40) |
00194 ((uldSource & 0x0000000000ff0000LL) << 24) |
00195 ((uldSource & 0x00000000ff000000LL) << 8) |
00196 ((uldSource & 0x000000ff00000000LL) >> 8) |
00197 ((uldSource & 0x0000ff0000000000LL) >> 24) |
00198 ((uldSource & 0x00ff000000000000LL) >> 40) |
00199 ((uldSource & 0xff00000000000000LL) >> 56);
00200 }
00201
00202
00205
00206 inline Int16 swapBytes(Int16 sSource)
00207 {
00208 return (Int16)swapBytes<Uint16>(Uint16(sSource));
00209 }
00210
00211
00214
00215 inline Int32 swapBytes(Int32 lSource)
00216 {
00217 return (Int32)swapBytes<Uint32>(Uint32(lSource));
00218 }
00219
00220
00223
00224 inline Int64 swapBytes(Int64 ldSource)
00225 {
00226 return (Int64)swapBytes<Uint64>(Uint64(ldSource));
00227 }
00228
00229 private:
00230
00232
00233
00236
00237 inline Uint16 convertFromLittleEndianHelper(const Uint8* pucSource, Uint16* )
00238 {
00239 return 0 |
00240 pucSource[0] |
00241 pucSource[1] * 0x0100;
00242 }
00243
00244
00247
00248 inline Uint32 convertFromLittleEndianHelper(const Uint8* pucSource, Uint32* )
00249 {
00250 return 0 |
00251 pucSource[0] |
00252 pucSource[1] * Uint32(0x00000100) |
00253 pucSource[2] * Uint32(0x00010000) |
00254 pucSource[3] * Uint32(0x01000000);
00255 }
00256
00257
00260
00261 inline Uint64 convertFromLittleEndianHelper(const Uint8* pucSource, Uint64* )
00262 {
00263 return 0 |
00264 pucSource[0] |
00265 pucSource[1] * Uint64(0x0000000000000100LL) |
00266 pucSource[2] * Uint64(0x0000000000010000LL) |
00267 pucSource[3] * Uint64(0x0000000001000000LL) |
00268 pucSource[4] * Uint64(0x0000000100000000LL) |
00269 pucSource[5] * Uint64(0x0000010000000000LL) |
00270 pucSource[6] * Uint64(0x0001000000000000LL) |
00271 pucSource[7] * Uint64(0x0100000000000000LL);
00272 }
00273
00274
00277
00278 inline Int16 convertFromLittleEndianHelper(const Uint8* pucSource, Int16* )
00279 {
00280 return static_cast<Int16>(convertFromLittleEndianHelper(pucSource, reinterpret_cast<Uint16*>(0)));
00281 }
00282
00283
00286
00287 inline Int32 convertFromLittleEndianHelper(const Uint8* pucSource, Int32* )
00288 {
00289 return static_cast<Int32>(convertFromLittleEndianHelper(pucSource, reinterpret_cast<Uint32*>(0)));
00290 }
00291
00292
00295
00296 inline Int64 convertFromLittleEndianHelper(const Uint8* pucSource, Int64* )
00297 {
00298 return static_cast<Int64>(convertFromLittleEndianHelper(pucSource, reinterpret_cast<Uint64*>(0)));
00299 }
00300
00302
00304
00305
00308
00309 inline Uint16 convertFromBigEndianHelper(const Uint8* pucSource, Uint16* dest)
00310 {
00311 return 0 |
00312 pucSource[1] |
00313 pucSource[0] * 0x0100;
00314 }
00315
00316
00319
00320 inline Uint32 convertFromBigEndianHelper(const Uint8* pucSource, Uint32* dest)
00321 {
00322 return 0 |
00323 pucSource[3] |
00324 pucSource[2] * Uint32(0x00000100) |
00325 pucSource[1] * Uint32(0x00010000) |
00326 pucSource[0] * Uint32(0x01000000);
00327 }
00328
00329
00332
00333 inline Uint64 convertFromBigEndianHelper(const Uint8* source, Uint64* dest)
00334 {
00335 return 0 |
00336 pucSource[7] |
00337 pucSource[6] * Uint64(0x0000000000000100LL) |
00338 pucSource[5] * Uint64(0x0000000000010000LL) |
00339 pucSource[4] * Uint64(0x0000000001000000LL) |
00340 pucSource[3] * Uint64(0x0000000100000000LL) |
00341 pucSource[2] * Uint64(0x0000010000000000LL) |
00342 pucSource[1] * Uint64(0x0001000000000000LL) |
00343 pucSource[0] * Uint64(0x0100000000000000LL);
00344 }
00345
00346
00349
00350 inline Int16 convertFromBigEndianHelper(const Uint8* pucSource, Int16* )
00351 {
00352 return static_cast<Int16>(convertFromBigEndianHelper(pucSource, reinterpret_cast<Uint16*>(0)));
00353 }
00354
00355
00358
00359 inline Int32 convertFromBigEndianHelper(const Uint8* pucSource, Int32* )
00360 {
00361 return static_cast<Int32>(convertFromBigEndianHelper(pucSource, reinterpret_cast<Uint32*>(0)));
00362 }
00363
00364
00367
00368 inline Int64 convertFromBigEndianHelper(const Uint8* pucSource, Int64* )
00369 {
00370 return static_cast<Int64>(convertFromBigEndianHelper(pucSource, reinterpret_cast<Uint64*>(0)));
00371 }
00372
00374 };
00375
00376
00377
00378 END_NAMESPACE_Zeus
00379
00380 #endif