00001 #ifndef _LCORE_GenTypes_H_
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
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #ifdef __GNUG__
00053 # pragma interface
00054 #endif
00055
00056 #include <netinet/in.h>
00057
00058 #define _LCORE_GenTypes_H_
00059
00060 namespace strmod {
00061 namespace lcore {
00062
00063 #ifdef BYTE_ORDER_DEFINED
00064 # if !(defined(ORDER_UNIX) || defined(ORDER_XINU))
00065 # error Byte order defined macro is defined, but no valid byte orderings \
00066 are defined.
00067 # endif
00068 #else
00069 # if defined(ORDER_UNIX) || defined(ORDER_XINU)
00070 # error "Byte order defined macro isn't defined, but a valid order is."
00071 # endif
00072 #endif
00073
00074 #if defined(__MSDOS__) || defined(MSDOS)
00075 typedef unsigned long U4Byte;
00076 typedef long S4Byte;
00077 typedef unsigned int U2Byte;
00078 typedef int S2Byte;
00079 typedef unsigned char U1Byte;
00080 typedef signed char S1Byte;
00081 typedef float IEEE_Float32;
00082 typedef double IEEE_Float64;
00083 #if !defined(BYTE_ORDER_DEFINED)
00084 #define ORDER_XINU // All 8x86 or 80x86 MSDOS machines are ORDER_XINU
00085 #define BYTE_ORDER_DEFINED
00086 #endif
00087 #else // UNIX is default defined(UNIX)
00088 #ifdef __alpha
00089 typedef unsigned int U4Byte;
00090 #else
00091 typedef unsigned long U4Byte;
00092 #endif
00093 #ifdef __alpha
00094 typedef int S4Byte;
00095 #else
00096 typedef long S4Byte;
00097 #endif
00098 typedef unsigned short U2Byte;
00099 typedef signed short S2Byte;
00100 typedef unsigned char U1Byte;
00101 typedef signed char S1Byte;
00102 typedef float IEEE_Float32;
00103 typedef double IEEE_Float64;
00104 #if !defined(BYTE_ORDER_DEFINED)
00105 #define ORDER_UNIX
00106 #define BYTE_ORDER_DEFINED
00107 #endif
00108 #endif
00109
00110
00111 #ifdef __cplusplus
00112
00113
00114
00115
00116 #ifndef BYTE_ORDER_DEFINED
00117 # error No one has defined a byte ordering.
00118 #endif
00119
00120 inline void hton(const U1Byte n, U1Byte *c)
00121 {
00122 c[0] = n;
00123 }
00124 inline void ntoh(const U1Byte *c, U1Byte &n)
00125 {
00126 n = c[0];
00127 }
00128
00129 inline void hton(const S1Byte n, U1Byte *c)
00130 {
00131 *((S1Byte *)(c)) = n;
00132 }
00133 inline void ntoh(const U1Byte *c, S1Byte &n)
00134 {
00135 n = *((const S1Byte *)(c));
00136 }
00137
00138 #ifdef ORDER_UNIX
00139 inline void hton(const U4Byte n, U1Byte *c)
00140 {
00141 *((U4Byte *)(c)) = n;
00142 }
00143 inline void ntoh(const U1Byte *c, U4Byte &n)
00144 {
00145 n = *((const U4Byte *)(c));
00146 }
00147
00148 inline void hton(const S4Byte n, U1Byte *c)
00149 {
00150 *((S4Byte *)(c)) = n;
00151 }
00152 inline void ntoh(const U1Byte *c, S4Byte &n)
00153 {
00154 n = *((const S4Byte *)(c));
00155 }
00156
00157
00158 inline void hton(const U2Byte n, U1Byte *c)
00159 {
00160 *((U2Byte *)(c)) = n;
00161 }
00162 inline void ntoh(const U1Byte *c, U2Byte &n)
00163 {
00164 n = *((const U2Byte *)(c));
00165 }
00166
00167 inline void hton(const S2Byte n, U1Byte *c)
00168 {
00169 *((S2Byte *)(c)) = n;
00170 }
00171 inline void ntoh(const U1Byte *c, S2Byte &n)
00172 {
00173 n = *((const S2Byte *)(c));
00174 }
00175
00176
00177 inline void hton(const IEEE_Float32 n, U1Byte *c)
00178 {
00179 *((IEEE_Float32 *)(c)) = n;
00180 }
00181 inline void hton(const U1Byte *c, IEEE_Float32 &n)
00182 {
00183 n = *((const IEEE_Float32 *)(c));
00184 }
00185
00186 inline void hton(const IEEE_Float64 n, U1Byte *c)
00187 {
00188 *((IEEE_Float64 *)(c)) = n;
00189 }
00190 inline void hton(const U1Byte *c, IEEE_Float64 &n)
00191 {
00192 n = *((const IEEE_Float64 *)(c));
00193 }
00194
00195 #elif defined(ORDER_XINU)
00196
00197 # ifdef __GNUG__
00198 inline void hton(const U4Byte n, U1Byte *c)
00199 {
00200 *((U4Byte *)(c)) = htonl(n);
00201 }
00202 inline void ntoh(const U1Byte *c, U4Byte &n)
00203 {
00204 n = ntohl(*((const U4Byte *)(c)));
00205 }
00206 inline void hton(const S4Byte n, U1Byte *c)
00207 {
00208 *((S4Byte *)(c)) = htonl(n);
00209 }
00210 inline void ntoh(const U1Byte *c, S4Byte &n)
00211 {
00212 n = ntohl(*((const S4Byte *)(c)));
00213 }
00214
00215 inline void hton(const U2Byte n, U1Byte *c)
00216 {
00217 *((U2Byte *)(c)) = htons(n);
00218 }
00219 inline void ntoh(const U1Byte *c, U2Byte &n)
00220 {
00221 n = ntohs(*((const U2Byte *)(c)));
00222 }
00223 inline void hton(const S2Byte n, U1Byte *c)
00224 {
00225 *((S2Byte *)(c)) = htons(n);
00226 }
00227 inline void ntoh(const U1Byte *c, S2Byte &n)
00228 {
00229 n = ntohs(*((const S2Byte *)(c)));
00230 }
00231 #else
00232 inline void hton(const U4Byte n, U1Byte *c)
00233 {
00234 *c++ = U1Byte((n >> 24) & 0xffU);
00235 *c++ = U1Byte((n >> 16) & 0xffU);
00236 *c++ = U1Byte((n >> 8) & 0xffU);
00237 *c = U1Byte(n & 0xffU);
00238 }
00239 inline void ntoh(const U1Byte *c, U4Byte &n)
00240 {
00241 n = (U4Byte(c[0] & 0xffU) << 24) | (U4Byte(c[1] & 0xffU) << 16) |
00242 (U4Byte(c[2] & 0xffU) << 8) | U4Byte(c[3] & 0xffU);
00243 }
00244
00245 inline void hton(const S4Byte n, U1Byte *c)
00246 {
00247 hton(U4Byte(n), c);
00248 }
00249 inline void ntoh(const U1Byte *c, S4Byte &n) {
00250 n = (U4Byte(c[0] & 0xffU) << 24) | (U4Byte(c[1] & 0xffU) << 16) |
00251 (U4Byte(c[2] & 0xffU) << 8) | U4Byte(c[3] & 0xffU);
00252 }
00253
00254
00255 inline void hton(const U2Byte n, U1Byte *c)
00256 {
00257 *c++ = U1Byte((n >> 8) & 0xffU);
00258 *c = (n & 0xffU);
00259 }
00260 inline void ntoh(const U1Byte *c, U2Byte &n) {
00261 n = (U2Byte(c[0] & 0xffU) << 8) | U2Byte(c[1] & 0xffU);
00262 }
00263
00264 inline void hton(const S2Byte n, U1Byte *c)
00265 {
00266 hton(U2Byte(n), c);
00267 }
00268 inline void ntoh(const U1Byte *c, S2Byte &n)
00269 {
00270 n = S2Byte((U2Byte(c[0] & 0xffU) << 8) | U2Byte(c[1] & 0xffU));
00271 }
00272 #endif
00273
00274
00275 inline void hton(const IEEE_Float32 &n, U1Byte *c)
00276 {
00277 const U1Byte * const from = ((const U1Byte *)(&n));
00278
00279 c[3] = from[0];
00280 c[2] = from[1];
00281 c[1] = from[2];
00282 c[0] = from[3];
00283 }
00284 inline void ntoh(const U1Byte *c, IEEE_Float32 &n)
00285 {
00286 U1Byte * const to = ((U1Byte *)(&n));
00287
00288 to[0] = c[3];
00289 to[1] = c[2];
00290 to[2] = c[1];
00291 to[3] = c[0];
00292 }
00293
00294 inline void hton(const IEEE_Float64 &n, U1Byte *c)
00295 {
00296 const U1Byte * const from = ((const U1Byte *)(&n));
00297
00298 c[7] = from[0];
00299 c[6] = from[1];
00300 c[5] = from[2];
00301 c[4] = from[3];
00302 c[3] = from[4];
00303 c[2] = from[5];
00304 c[1] = from[6];
00305 c[0] = from[7];
00306 }
00307 inline void ntoh(const U1Byte *c, IEEE_Float64 &n)
00308 {
00309 U1Byte * const to = ((U1Byte *)(&n));
00310
00311 to[0] = c[7];
00312 to[1] = c[6];
00313 to[2] = c[5];
00314 to[3] = c[4];
00315 to[4] = c[3];
00316 to[5] = c[2];
00317 to[6] = c[1];
00318 to[7] = c[0];
00319 }
00320 #endif
00321 #endif
00322
00323 }
00324 }
00325
00326 #endif