From b8cfd96db10fc4341ffd3b552d28d95f716e895e Mon Sep 17 00:00:00 2001 From: Jessica Hamilton Date: Sun, 10 Sep 2017 07:37:32 +1200 Subject: [PATCH] endian.h: add BSD extensions when _BSD_SOURCE is defined. --- headers/posix/endian.h | 49 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/headers/posix/endian.h b/headers/posix/endian.h index ba93bb128a..02b5c329f8 100644 --- a/headers/posix/endian.h +++ b/headers/posix/endian.h @@ -7,7 +7,9 @@ #include - +#ifdef _BSD_SOURCE +#include +#endif /* Defines architecture dependent endian constants. * The constant reflects the byte order, "4" is the most @@ -28,4 +30,49 @@ #define __LITTLE_ENDIAN LITTLE_ENDIAN #define __BYTE_ORDER BYTE_ORDER +#ifdef _BSD_SOURCE + +/* + * General byte order swapping functions. + */ +#define bswap16(x) __swap_int16(x) +#define bswap32(x) __swap_int32(x) +#define bswap64(x) __swap_int64(x) + +/* + * Host to big endian, host to little endian, big endian to host, and little + * endian to host byte order functions as detailed in byteorder(9). + */ +#if BYTE_ORDER == LITTLE_ENDIAN +#define htobe16(x) bswap16((x)) +#define htobe32(x) bswap32((x)) +#define htobe64(x) bswap64((x)) +#define htole16(x) ((uint16_t)(x)) +#define htole32(x) ((uint32_t)(x)) +#define htole64(x) ((uint64_t)(x)) + +#define be16toh(x) bswap16((x)) +#define be32toh(x) bswap32((x)) +#define be64toh(x) bswap64((x)) +#define le16toh(x) ((uint16_t)(x)) +#define le32toh(x) ((uint32_t)(x)) +#define le64toh(x) ((uint64_t)(x)) +#else /* BYTE_ORDER != LITTLE_ENDIAN */ +#define htobe16(x) ((uint16_t)(x)) +#define htobe32(x) ((uint32_t)(x)) +#define htobe64(x) ((uint64_t)(x)) +#define htole16(x) bswap16((x)) +#define htole32(x) bswap32((x)) +#define htole64(x) bswap64((x)) + +#define be16toh(x) ((uint16_t)(x)) +#define be32toh(x) ((uint32_t)(x)) +#define be64toh(x) ((uint64_t)(x)) +#define le16toh(x) bswap16((x)) +#define le32toh(x) bswap32((x)) +#define le64toh(x) bswap64((x)) +#endif /* BYTE_ORDER == LITTLE_ENDIAN */ + +#endif /* _BSD_SOURCE */ + #endif /* _ENDIAN_H_ */