1 /* David Leonard, 2004. Public domain. */ 2 /* $Id$ */ 3 4 /* 5 * Machine-dependent types and endian conversion. 6 * 7 * The following macros convert from specific to native endianness: 8 * 9 * swap16(x) swap32(x) 10 * 11 * letoh16(x) letoh32(x) 12 * betoh16(x) betoh32(x) 13 * htole16(x) htole32(x) 14 * htobe16(x) htobe32(x) 15 * 16 * These integer types should also be defined after include this file: 17 * 18 * u_int8_t int8_t 19 * u_int16_t int16_t 20 * u_int32_t int32_t 21 * 22 * The Berkeley socket macros ntohs, ntohl, htons, htonl are assumed to 23 * work on 16 (?to?s) and 32 (?to?l) bit integers. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/param.h> 28 #if defined(BSD) 29 # include <sys/endian.h> 30 # if defined(be16toh) && !defined(betoh16) 31 /* Sigh. FreeBSD just has to be different. */ 32 # define betoh16(x) be16toh(x) 33 # define betoh32(x) be32toh(x) 34 # define letoh16(x) le16toh(x) 35 # define letoh32(x) le32toh(x) 36 # define swap32(x) bswap32(x) 37 # endif 38 #endif 39 40 #if defined(__linux__) || defined(__FreeBSD_kernel__) 41 # include <endian.h> 42 # include <byteswap.h> 43 # define swap16(x) bswap_16(x) 44 # define swap32(x) bswap_32(x) 45 # if defined(WORDS_BIGENDIAN) 46 # define letoh16(x) swap16(x) 47 # define letoh32(x) swap32(x) 48 # define betoh16(x) (x) 49 # define betoh32(x) (x) 50 # else 51 # define letoh16(x) (x) 52 # define letoh32(x) (x) 53 # define betoh16(x) swap16(x) 54 # define betoh32(x) swap32(x) 55 # endif 56 #endif 57