00001 #ifndef BSWAP_H_INCLUDED
00002 #define BSWAP_H_INCLUDED
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <config.h>
00024
00025 #if defined(WORDS_BIGENDIAN)
00026
00027 #define B2N_16(x) (void)(x)
00028 #define B2N_32(x) (void)(x)
00029 #define B2N_64(x) (void)(x)
00030
00031 #else
00032
00033
00034 #if defined(HAVE_SYS_PARAM_H)
00035 #include <sys/param.h>
00036 #endif
00037
00038 #if defined(__linux__)
00039 #include <byteswap.h>
00040 #define B2N_16(x) x = bswap_16(x)
00041 #define B2N_32(x) x = bswap_32(x)
00042 #define B2N_64(x) x = bswap_64(x)
00043
00044 #elif defined(__NetBSD__)
00045 #include <sys/endian.h>
00046 #define B2N_16(x) BE16TOH(x)
00047 #define B2N_32(x) BE32TOH(x)
00048 #define B2N_64(x) BE64TOH(x)
00049
00050 #elif defined(__OpenBSD__)
00051 #include <sys/endian.h>
00052 #define B2N_16(x) x = swap16(x)
00053 #define B2N_32(x) x = swap32(x)
00054 #define B2N_64(x) x = swap64(x)
00055
00056 #elif defined(__FreeBSD__) && __FreeBSD_version >= 470000
00057 #include <sys/endian.h>
00058 #define B2N_16(x) x = be16toh(x)
00059 #define B2N_32(x) x = be32toh(x)
00060 #define B2N_64(x) x = be64toh(x)
00061
00062 #elif defined(CONFIG_DARWIN)
00063 #include <libkern/OSByteOrder.h>
00064 #define B2N_16(x) x = OSSwapBigToHostConstInt16(x)
00065 #define B2N_32(x) x = OSSwapBigToHostConstInt32(x)
00066 #define B2N_64(x) x = OSSwapBigToHostConstInt64(x)
00067
00068
00069
00070
00071
00072
00073
00074 #elif defined(__FreeBSD__) || defined(__sun) || defined(__bsdi__) || defined(WIN32) || defined(__CYGWIN__)
00075 #define B2N_16(x) \
00076 x = ((((x) & 0xff00) >> 8) | \
00077 (((x) & 0x00ff) << 8))
00078 #define B2N_32(x) \
00079 x = ((((x) & 0xff000000) >> 24) | \
00080 (((x) & 0x00ff0000) >> 8) | \
00081 (((x) & 0x0000ff00) << 8) | \
00082 (((x) & 0x000000ff) << 24))
00083 #define B2N_64(x) \
00084 x = ((((x) & 0xff00000000000000) >> 56) | \
00085 (((x) & 0x00ff000000000000) >> 40) | \
00086 (((x) & 0x0000ff0000000000) >> 24) | \
00087 (((x) & 0x000000ff00000000) >> 8) | \
00088 (((x) & 0x00000000ff000000) << 8) | \
00089 (((x) & 0x0000000000ff0000) << 24) | \
00090 (((x) & 0x000000000000ff00) << 40) | \
00091 (((x) & 0x00000000000000ff) << 56))
00092
00093 #else
00094
00095
00096
00097
00098 #error "You need to add endian swap macros for you're system"
00099
00100 #endif
00101
00102 #endif
00103
00104 #endif