Module: kamailio
Branch: master
Commit: 8805959c996fce90c34e2f0bc750db172bfda0d8
URL:
https://github.com/kamailio/kamailio/commit/8805959c996fce90c34e2f0bc750db1…
Author: Spencer Thomason <spencer(a)whiteskycommunications.com>
Committer: Spencer Thomason <spencer(a)whiteskycommunications.com>
Date: 2016-07-07T01:33:29-07:00
core: add generic byte swap macros
- add bswap16() and bswap32() for 16 and 32 bit endianness swap
---
Modified: endianness.h
---
Diff:
https://github.com/kamailio/kamailio/commit/8805959c996fce90c34e2f0bc750db1…
Patch:
https://github.com/kamailio/kamailio/commit/8805959c996fce90c34e2f0bc750db1…
---
diff --git a/endianness.h b/endianness.h
index 53f0fba..e6edec2 100644
--- a/endianness.h
+++ b/endianness.h
@@ -30,6 +30,8 @@
* detected endianness corresponds to
* the runtime detected one and -1 on
* error (recommended action: bail out)
+ * - bswap16() - 16 bit byte swap
+ * - bswap32() - 32 bit byte swap
*
* Implementation notes:
* Endian macro names/tests for different OSes:
@@ -130,6 +132,29 @@ extern int endianness_sanity_check(void);
#error BUG: both little & big endian detected in the same time
#endif
+#if defined __IS_LITTLE_ENDIAN
+#include <arpa/inet.h>
+#define bswap16(x) ntohs(x)
+#define bswap32(x) ntohl(x)
+#else /* !__IS_LITTLE_ENDIAN */
+#include <stdint.h>
+#if defined __GNUC__ && (__GNUC__ > 4 || (__GNUC__ == 4 &&
__GNUC_MINOR__ >= 8))
+/* need at least GCC 4.8 for __builtin_bswap16 on all archs */
+#define bswap16(x) ((uint16_t)__builtin_bswap16(x))
+#else
+#define bswap16(x) (((uint16_t)(x) >> 8) | \
+ ((uint16_t)(x) << 8))
+#endif
+#if defined __GNUC__ && (__GNUC__ > 4 || (__GNUC__ == 4 &&
__GNUC_MINOR__ >= 3))
+/* GCC >= 4.3 provides __builtin_bswap32 */
+#define bswap32(x) ((uint32_t)__builtin_bswap32(x))
+#else
+#define bswap32(x) (((uint32_t)(x) << 24) | \
+ (((uint32_t)(x) << 8) & 0xff0000) | \
+ (((uint32_t)(x) >> 8) & 0xff00) | \
+ ((uint32_t)(x) >> 24))
+#endif
+#endif /* !__IS_LITTLE_ENDIAN */
#endif /* _endianness_h */