Compare commits

...

1 Commits

Author SHA1 Message Date
Ondřej Surý
498b86b04a Add fallback function for endian(3) family functions in isc/endian.h
Write simple bswap{16,32,64} macros and use them in place where
byteorder(3) functions are not appropriate (e.g. bigendian platforms
and 64-bit functions).
2019-05-29 10:17:56 +02:00

View File

@@ -79,6 +79,68 @@
#else
#error Platform not supported
#include <inttypes.h>
#ifndef bswap16
#define bswap16(x) ((uint16_t)((((uint16_t) (x) & 0xff00) >> 8) | \
(((uint16_t) (x) & 0x00ff) << 8)))
#endif /* bswap16 */
#ifndef bswap32
#define bswap32(x) ((uint32_t)((((uint32_t) (x) & 0xff000000) >> 24) | \
(((uint32_t) (x) & 0x00ff0000) >> 8) | \
(((uint32_t) (x) & 0x0000ff00) << 8) | \
(((uint32_t) (x) & 0x000000ff) << 24)))
#endif /* bswap32 */
#ifndef bswap64
#define bswap64(x) \
((uint64_t)((((uint64_t) (x) & 0xff00000000000000ull) >> 56) | \
(((uint64_t) (x) & 0x00ff000000000000ull) >> 40) | \
(((uint64_t) (x) & 0x0000ff0000000000ull) >> 24) | \
(((uint64_t) (x) & 0x000000ff00000000ull) >> 8) | \
(((uint64_t) (x) & 0x00000000ff000000ull) << 8) | \
(((uint64_t) (x) & 0x0000000000ff0000ull) << 24) | \
(((uint64_t) (x) & 0x000000000000ff00ull) << 40) | \
(((uint64_t) (x) & 0x00000000000000ffull) << 56)))
#endif /* bswap64 */
#if WORDS_BIGENDIAN
#define htobe16(x) (x)
#define htole16(x) bswap16(x)
#define be16toh(x) (x)
#define le16toh(x) bswap16(x)
#define htobe32(x) (x)
#define htole32(x) bswap32(x)
#define be32toh(x) (x)
#define le32toh(x) bswap32(x)
#define htobe64(x) (x)
#define htole64(x) bswap64(x)
#define be64toh(x) (x)
#define le64toh(x) bswap64(x)
#else /* WORDS_BIGENDIAN */
#include <arpa/inet.h>
#define htobe16(x) htons(x)
#define htole16(x) (x)
#define be16toh(x) ntohs(x)
#define le16toh(x) (x)
#define htobe32(x) htonl(x)
#define htole32(x) (x)
#define be32toh(x) ntohl(x)
#define le32toh(x) (x)
#define htobe64(x) bswap64(x)
#define htole64(x) (x)
#define be64toh(x) bswap64(x)
#define le64toh(x) (x)
#endif /* WORDS_BIGENDIAN */
#endif