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
00033
00034
00035
00036
00037 #ifndef Foundation_ByteOrder_INCLUDED
00038 #define Foundation_ByteOrder_INCLUDED
00039
00040
00041 #include "Poco/Foundation.h"
00042 #include "Poco/Types.h"
00043
00044
00045 namespace Poco {
00046
00047
00048 class Foundation_API ByteOrder
00052 {
00053 public:
00054 static Int16 flipBytes(Int16 value);
00055 static UInt16 flipBytes(UInt16 value);
00056 static Int32 flipBytes(Int32 value);
00057 static UInt32 flipBytes(UInt32 value);
00058 #if defined(POCO_HAVE_INT64)
00059 static Int64 flipBytes(Int64 value);
00060 static UInt64 flipBytes(UInt64 value);
00061 #endif
00062
00063 static Int16 toBigEndian(Int16 value);
00064 static UInt16 toBigEndian (UInt16 value);
00065 static Int32 toBigEndian(Int32 value);
00066 static UInt32 toBigEndian (UInt32 value);
00067 #if defined(POCO_HAVE_INT64)
00068 static Int64 toBigEndian(Int64 value);
00069 static UInt64 toBigEndian (UInt64 value);
00070 #endif
00071
00072 static Int16 fromBigEndian(Int16 value);
00073 static UInt16 fromBigEndian (UInt16 value);
00074 static Int32 fromBigEndian(Int32 value);
00075 static UInt32 fromBigEndian (UInt32 value);
00076 #if defined(POCO_HAVE_INT64)
00077 static Int64 fromBigEndian(Int64 value);
00078 static UInt64 fromBigEndian (UInt64 value);
00079 #endif
00080
00081 static Int16 toLittleEndian(Int16 value);
00082 static UInt16 toLittleEndian (UInt16 value);
00083 static Int32 toLittleEndian(Int32 value);
00084 static UInt32 toLittleEndian (UInt32 value);
00085 #if defined(POCO_HAVE_INT64)
00086 static Int64 toLittleEndian(Int64 value);
00087 static UInt64 toLittleEndian (UInt64 value);
00088 #endif
00089
00090 static Int16 fromLittleEndian(Int16 value);
00091 static UInt16 fromLittleEndian (UInt16 value);
00092 static Int32 fromLittleEndian(Int32 value);
00093 static UInt32 fromLittleEndian (UInt32 value);
00094 #if defined(POCO_HAVE_INT64)
00095 static Int64 fromLittleEndian(Int64 value);
00096 static UInt64 fromLittleEndian (UInt64 value);
00097 #endif
00098
00099 static Int16 toNetwork(Int16 value);
00100 static UInt16 toNetwork (UInt16 value);
00101 static Int32 toNetwork(Int32 value);
00102 static UInt32 toNetwork (UInt32 value);
00103 #if defined(POCO_HAVE_INT64)
00104 static Int64 toNetwork(Int64 value);
00105 static UInt64 toNetwork (UInt64 value);
00106 #endif
00107
00108 static Int16 fromNetwork(Int16 value);
00109 static UInt16 fromNetwork (UInt16 value);
00110 static Int32 fromNetwork(Int32 value);
00111 static UInt32 fromNetwork (UInt32 value);
00112 #if defined(POCO_HAVE_INT64)
00113 static Int64 fromNetwork(Int64 value);
00114 static UInt64 fromNetwork (UInt64 value);
00115 #endif
00116 };
00117
00118
00119
00120
00121
00122 inline UInt16 ByteOrder::flipBytes(UInt16 value)
00123 {
00124 return ((value >> 8) & 0x00FF) | ((value << 8) & 0xFF00);
00125 }
00126
00127
00128 inline Int16 ByteOrder::flipBytes(Int16 value)
00129 {
00130 return Int16(flipBytes(UInt16(value)));
00131 }
00132
00133
00134 inline UInt32 ByteOrder::flipBytes(UInt32 value)
00135 {
00136 return ((value >> 24) & 0x000000FF) | ((value >> 8) & 0x0000FF00)
00137 | ((value << 8) & 0x00FF0000) | ((value << 24) & 0xFF000000);
00138 }
00139
00140
00141 inline Int32 ByteOrder::flipBytes(Int32 value)
00142 {
00143 return Int32(flipBytes(UInt32(value)));
00144 }
00145
00146
00147 #if defined(POCO_HAVE_INT64)
00148 inline UInt64 ByteOrder::flipBytes(UInt64 value)
00149 {
00150 UInt32 hi = UInt32(value >> 32);
00151 UInt32 lo = UInt32(value & 0xFFFFFFFF);
00152 return UInt64(flipBytes(hi)) | (UInt64(flipBytes(lo)) << 32);
00153 }
00154
00155
00156 inline Int64 ByteOrder::flipBytes(Int64 value)
00157 {
00158 return Int64(flipBytes(UInt64(value)));
00159 }
00160 #endif // POCO_HAVE_INT64
00161
00162
00163
00164
00165
00166 #define POCO_IMPLEMENT_BYTEORDER_NOOP_(op, type) \
00167 inline type ByteOrder::op(type value) \
00168 { \
00169 return value; \
00170 }
00171 #define POCO_IMPLEMENT_BYTEORDER_FLIP_(op, type) \
00172 inline type ByteOrder::op(type value) \
00173 { \
00174 return flipBytes(value); \
00175 }
00176
00177
00178 #if defined(POCO_HAVE_INT64)
00179 #define POCO_IMPLEMENT_BYTEORDER_NOOP(op) \
00180 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, Int16) \
00181 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, UInt16) \
00182 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, Int32) \
00183 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, UInt32) \
00184 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, Int64) \
00185 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, UInt64)
00186 #define POCO_IMPLEMENT_BYTEORDER_FLIP(op) \
00187 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, Int16) \
00188 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, UInt16) \
00189 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, Int32) \
00190 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, UInt32) \
00191 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, Int64) \
00192 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, UInt64)
00193 #else
00194 #define POCO_IMPLEMENT_BYTEORDER_NOOP(op) \
00195 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, Int16) \
00196 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, UInt16) \
00197 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, Int32) \
00198 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, UInt32)
00199 #define POCO_IMPLEMENT_BYTEORDER_FLIP(op) \
00200 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, Int16) \
00201 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, UInt16) \
00202 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, Int32) \
00203 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, UInt32)
00204 #endif
00205
00206
00207 #if defined(POCO_ARCH_BIG_ENDIAN)
00208 #define POCO_IMPLEMENT_BYTEORDER_BIG POCO_IMPLEMENT_BYTEORDER_NOOP
00209 #define POCO_IMPLEMENT_BYTEORDER_LIT POCO_IMPLEMENT_BYTEORDER_FLIP
00210 #else
00211 #define POCO_IMPLEMENT_BYTEORDER_BIG POCO_IMPLEMENT_BYTEORDER_FLIP
00212 #define POCO_IMPLEMENT_BYTEORDER_LIT POCO_IMPLEMENT_BYTEORDER_NOOP
00213 #endif
00214
00215
00216 POCO_IMPLEMENT_BYTEORDER_BIG(toBigEndian)
00217 POCO_IMPLEMENT_BYTEORDER_BIG(fromBigEndian)
00218 POCO_IMPLEMENT_BYTEORDER_BIG(toNetwork)
00219 POCO_IMPLEMENT_BYTEORDER_BIG(fromNetwork)
00220 POCO_IMPLEMENT_BYTEORDER_LIT(toLittleEndian)
00221 POCO_IMPLEMENT_BYTEORDER_LIT(fromLittleEndian)
00222
00223
00224 }
00225
00226
00227 #endif // Foundation_ByteOrder_INCLUDED