From df18bfb4594c59baffdd05e4490b575976e6d4b4 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Mon, 27 Jul 2020 18:36:07 +0200 Subject: [PATCH] core/byteorder: add Big Endian implementations --- core/include/byteorder.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/core/include/byteorder.h b/core/include/byteorder.h index f305d4dff1..f2acea7f60 100644 --- a/core/include/byteorder.h +++ b/core/include/byteorder.h @@ -482,29 +482,52 @@ static inline uint64_t ntohll(uint64_t v) static inline uint16_t byteorder_bebuftohs(const uint8_t *buf) { +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ return (uint16_t)((buf[0] << 8) | (buf[1] << 0)); +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + return (uint16_t)((buf[1] << 8) | (buf[0] << 0)); +#endif } static inline uint32_t byteorder_bebuftohl(const uint8_t *buf) { +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ return (((uint32_t) buf[0] << 24) | ((uint32_t) buf[1] << 16) | ((uint32_t) buf[2] << 8) | ((uint32_t) buf[3] << 0)); +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + return (((uint32_t) buf[3] << 24) + | ((uint32_t) buf[2] << 16) + | ((uint32_t) buf[1] << 8) + | ((uint32_t) buf[0] << 0)); +#endif } static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val) { +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ buf[0] = (uint8_t)(val >> 8); buf[1] = (uint8_t)(val >> 0); +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + buf[1] = (uint8_t)(val >> 8); + buf[0] = (uint8_t)(val >> 0); +#endif } static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val) { +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ buf[0] = (uint8_t)(val >> 24); buf[1] = (uint8_t)(val >> 16); buf[2] = (uint8_t)(val >> 8); buf[3] = (uint8_t)(val >> 0); +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + buf[3] = (uint8_t)(val >> 24); + buf[2] = (uint8_t)(val >> 16); + buf[1] = (uint8_t)(val >> 8); + buf[0] = (uint8_t)(val >> 0); +#endif } #ifdef __cplusplus