From: Andreas Steffen Date: Wed, 3 Aug 2016 12:45:01 +0000 (+0200) Subject: utils: Defined uletoh16() and htole16() X-Git-Tag: 5.5.1dr1~1^2~6 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=8993cb556e43d69a356f6e95f608cfc0a752444b;p=thirdparty%2Fstrongswan.git utils: Defined uletoh16() and htole16() --- diff --git a/src/libstrongswan/utils/utils/byteorder.h b/src/libstrongswan/utils/utils/byteorder.h index 7c7e534202..0665ef363d 100644 --- a/src/libstrongswan/utils/utils/byteorder.h +++ b/src/libstrongswan/utils/utils/byteorder.h @@ -44,6 +44,21 @@ #define BITFIELD5(t, a, b, c, d, e,...) struct { t e; t d; t c; t b; t a; __VA_ARGS__} #endif +#ifndef le16toh +# if BYTE_ORDER == BIG_ENDIAN +# define le16toh(x) __builtin_bswap16(x) +# else +# define le16toh(x) (x) +# endif +#endif +#ifndef htole16 +# if BYTE_ORDER == BIG_ENDIAN +# define htole16(x) __builtin_bswap16(x) +# else +# define htole16(x) (x) +# endif +#endif + #ifndef le32toh # if BYTE_ORDER == BIG_ENDIAN # define le32toh(x) __builtin_bswap32(x) @@ -176,6 +191,33 @@ static inline uint64_t untoh64(void *network) return be64toh(tmp); } +/** + * Read a 16-bit value in little-endian order from unaligned address. + * + * @param p unaligned address to read little endian value from + * @return host order value + */ +static inline uint16_t uletoh16(void *p) +{ + uint16_t ret; + + memcpy(&ret, p, sizeof(ret)); + ret = le16toh(ret); + return ret; +} + +/** + * Write a 16-bit value in little-endian to an unaligned address. + * + * @param p host order 16-bit value + * @param v unaligned address to write little endian value to + */ +static inline void htoule16(void *p, uint16_t v) +{ + v = htole16(v); + memcpy(p, &v, sizeof(v)); +} + /** * Read a 32-bit value in little-endian order from unaligned address. *