am7xxx: add a portable_endian.h
authorAntonio Ospite <ao2@ao2.it>
Thu, 8 May 2014 09:22:31 +0000 (11:22 +0200)
committerAntonio Ospite <ao2@ao2.it>
Thu, 8 May 2014 09:22:31 +0000 (11:22 +0200)
Add a portable_endian.h to define platform specific implementations for
the functions found on most unix-like systems in endian.h.

src/portable_endian.h [new file with mode: 0644]
src/serialize.c

diff --git a/src/portable_endian.h b/src/portable_endian.h
new file mode 100644 (file)
index 0000000..1741f18
--- /dev/null
@@ -0,0 +1,65 @@
+/* 
+ * Public domain, stripped down version of:
+ * https://gist.github.com/panzi/6856583
+ */
+
+#ifndef __PORTABLE_ENDIAN_H
+#define __PORTABLE_ENDIAN_H
+
+#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
+
+#      define __WINDOWS__
+
+#endif
+
+#if defined(__linux__) || defined(__CYGWIN__)
+
+#      include <endian.h>
+
+#elif defined(__APPLE__)
+
+#      include <libkern/OSByteOrder.h>
+
+#      define htole32(x) OSSwapHostToLittleInt32(x)
+#      define le32toh(x) OSSwapLittleToHostInt32(x)
+
+#elif defined(__OpenBSD__)
+
+#      include <sys/endian.h>
+
+#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
+
+#      include <sys/endian.h>
+
+#      define le32toh(x) letoh32(x)
+
+#elif defined(__WINDOWS__)
+
+#      include <winsock2.h>
+#      include <sys/param.h>
+
+#      if BYTE_ORDER == LITTLE_ENDIAN
+
+#              define htole32(x) (x)
+#              define le32toh(x) (x)
+
+#      elif BYTE_ORDER == BIG_ENDIAN
+
+               /* that would be xbox 360 */
+
+#              define htole32(x) __builtin_bswap32(x)
+#              define le32toh(x) __builtin_bswap32(x)
+
+#      else
+
+#              error byte order not supported
+
+#      endif
+
+#else
+
+#error platform not supported
+
+#endif
+
+#endif /* __PORTABLE_ENDIAN_H */
index 72e3fd4..61536a0 100644 (file)
 
 #include <string.h>
 
-#ifdef __MINGW32__
-#define le32toh(x) (x)
-#define htole32(x) (x)
-#else
-#include <endian.h>
-#endif
-
+#include "portable_endian.h"
 #include "serialize.h"
 
 uint8_t get_8(uint8_t **bufferp)