diff options
author | Andreas Steffen <andreas.steffen@strongswan.org> | 2016-08-03 14:45:01 +0200 |
---|---|---|
committer | Andreas Steffen <andreas.steffen@strongswan.org> | 2016-08-06 12:09:05 +0200 |
commit | 8993cb556e43d69a356f6e95f608cfc0a752444b (patch) | |
tree | 52101245fe49ed3a1d3d26b7fe025d6017b61c96 | |
parent | b8070e2c85895a367ee176ae66551ae993ca68cc (diff) | |
download | strongswan-8993cb556e43d69a356f6e95f608cfc0a752444b.tar.bz2 strongswan-8993cb556e43d69a356f6e95f608cfc0a752444b.tar.xz |
utils: Defined uletoh16() and htole16()
-rw-r--r-- | src/libstrongswan/utils/utils/byteorder.h | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/libstrongswan/utils/utils/byteorder.h b/src/libstrongswan/utils/utils/byteorder.h index 7c7e53420..0665ef363 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) @@ -177,6 +192,33 @@ static inline uint64_t untoh64(void *network) } /** + * 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. * * @param p unaligned address to read little endian value from |