summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorSander Vrijders <sander.vrijders@intec.ugent.be>2017-02-16 10:43:08 +0000
committerdimitri staessens <dimitri.staessens@intec.ugent.be>2017-02-16 10:43:08 +0000
commitb56b15e89e524b6e81f9794a7faad42d9b548660 (patch)
tree8d4bb82d156046dc910c3bfbcfb13d52a1c36ccc /include
parent2fee864da506c1e6944c7caa2b6dcbe746165ca4 (diff)
parent1434ab198b38cbc9096809a6cadfe6759736cc9f (diff)
downloadouroboros-b56b15e89e524b6e81f9794a7faad42d9b548660.tar.gz
ouroboros-b56b15e89e524b6e81f9794a7faad42d9b548660.zip
Merged in sandervrijders/ouroboros/be-endianness (pull request #376)
lib: Revise endian header and SHA3
Diffstat (limited to 'include')
-rw-r--r--include/ouroboros/endian.h164
1 files changed, 52 insertions, 112 deletions
diff --git a/include/ouroboros/endian.h b/include/ouroboros/endian.h
index a2745595..46f384ec 100644
--- a/include/ouroboros/endian.h
+++ b/include/ouroboros/endian.h
@@ -4,9 +4,7 @@
* Endianness
*
* Dimitri Staessens <dimitri.staessens@intec.ugent.be>
- *
- * This implementation is adapted and redistributed from the RHASH
- * project
+ * Sander Vrijders <sander.vrijders@intec.ugent.be>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
@@ -23,126 +21,68 @@
* 02110-1301 USA
*/
-/*
- * byte_order.h - byte order related platform dependent routines,
- *
- * Copyright: 2008-2012 Aleksey Kravchenko <rhash.admin@gmail.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. Use this program at your own risk!
- */
-
#ifndef OUROBOROS_ENDIAN_H
#define OUROBOROS_ENDIAN_H
-#include <stdint.h>
-#include <unistd.h>
-#include <stdlib.h>
+#if defined(__linux__) || defined(__CYGWIN__)
+
+#define _BSD_SOURCE
+#define __USE_BSD
+#define _DEFAULT_SOURCE
-#ifdef __GLIBC__
#include <endian.h>
-#elif defined(__FreeBSD__)
+#include <features.h>
+
+#define betoh16(x) be16toh(x)
+#define letoh16(x) le16toh(x)
+#define betoh32(x) be32toh(x)
+#define letoh32(x) le32toh(x)
+#define betoh64(x) be64toh(x)
+#define letoh64(x) le64toh(x)
+
+#elif defined(__NetBSD__) || defined(__FreeBSD__)
+
#include <sys/endian.h>
-#endif
-/* if x86 compatible cpu */
-#if defined(i386) || defined(__i386__) || defined(__i486__) || \
- defined(__i586__) || defined(__i686__) || defined(__pentium__) || \
- defined(__pentiumpro__) || defined(__pentium4__) || \
- defined(__nocona__) || defined(prescott) || defined(__core2__) || \
- defined(__k6__) || defined(__k8__) || defined(__athlon__) || \
- defined(__amd64) || defined(__amd64__) || \
- defined(__x86_64) || defined(__x86_64__) || defined(_M_IX86) || \
- defined(_M_AMD64) || defined(_M_IA64) || defined(_M_X64)
-/* detect if x86-64 instruction set is supported */
-# if defined(_LP64) || defined(__LP64__) || defined(__x86_64) || \
- defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
-# define CPU_X64
-# else
-# define CPU_IA32
-# endif
-#endif
+#define betoh16(x) be16toh(x)
+#define letoh16(x) le16toh(x)
+#define betoh32(x) be32toh(x)
+#define letoh32(x) le32toh(x)
+#define betoh64(x) be64toh(x)
+#define letoh64(x) le64toh(x)
-/* detect CPU endianness */
-#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
- __BYTE_ORDER == __LITTLE_ENDIAN) || \
- defined(CPU_IA32) || defined(CPU_X64) || \
- defined(__ia64) || defined(__ia64__) || defined(__alpha__) || \
- defined(_M_ALPHA) || defined(vax) || defined(MIPSEL) || \
- defined(_ARM_) || defined(__arm__)
-#define CPU_LITTLE_ENDIAN
-#define IS_BIG_ENDIAN 0
-#define IS_LITTLE_ENDIAN 1
-#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
- __BYTE_ORDER == __BIG_ENDIAN) || \
- defined(__sparc) || defined(__sparc__) || defined(sparc) || \
- defined(_ARCH_PPC) || defined(_ARCH_PPC64) || defined(_POWER) || \
- defined(__POWERPC__) || defined(POWERPC) || defined(__powerpc) || \
- defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || \
- defined(__hpux) || defined(_MIPSEB) || defined(mc68000) || \
- defined(__s390__) || defined(__s390x__) || defined(sel)
-#define CPU_BIG_ENDIAN
-#define IS_BIG_ENDIAN 1
-#define IS_LITTLE_ENDIAN 0
-#else
-# error "Can't detect CPU architecture."
-#endif
+#elif defined(__APPLE__)
+
+#include <libkern/OSByteOrder.h>
+
+#define htobe16(x) OSSwapHostToBigInt16(x)
+#define htole16(x) OSSwapHostToLittleInt16(x)
+#define betoh16(x) OSSwapBigToHostInt16(x)
+#define letoh16(x) OSSwapLittleToHostInt16(x)
+
+#define htobe32(x) OSSwapHostToBigInt32(x)
+#define htole32(x) OSSwapHostToLittleInt32(x)
+#define betoh32(x) OSSwapBigToHostInt32(x)
+#define letoh32(x) OSSwapLittleToHostInt32(x)
+
+#define htobe64(x) OSSwapHostToBigInt64(x)
+#define htole64(x) OSSwapHostToLittleInt64(x)
+#define betoh64(x) OSSwapBigToHostInt64(x)
+#define letoh64(x) OSSwapLittleToHostInt64(x)
+
+#elif defined(__OpenBSD__)
+
+#include <sys/endian.h>
-#if defined(__GNUC__) && (__GNUC__ >= 4) && \
- (__GNUC__ > 4 || __GNUC_MINOR__ >= 3)
-/* for GCC >= 4.3 */
-#define bswap_32(x) __builtin_bswap32(x)
-#elif !defined(__STRICT_ANSI__)
-/* general bswap_32 definition */
-static inline uint32_t bswap_32(uint32_t x) {
- x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0x00FF00FF);
- return (x >> 16) | (x << 16);
-}
-#else
-#define bswap_32(x) ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
- (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
-#endif /* bswap_32 */
-
-#if defined(__GNUC__) && (__GNUC__ >= 4) && \
- (__GNUC__ > 4 || __GNUC_MINOR__ >= 3)
-#define bswap_64(x) __builtin_bswap64(x)
-#elif defined (bswap64)
-#define bswap_64 bswap64
-#else
-#if !defined(__STRICT_ANSI__)
-static inline uint64_t bswap_64(uint64_t x) {
- union {
- uint64_t ll;
- uint32_t l[2];
- } w, r;
- w.ll = x;
- r.l[0] = bswap_32(w.l[1]);
- r.l[1] = bswap_32(w.l[0]);
- return r.ll;
-}
#else
-#error "bswap_64 unsupported"
-#endif
+
+#error OS currently not supported
+
#endif
-#ifdef CPU_LITTLE_ENDIAN
-#define hton64(x) bswap_64(x)
-#define hton32(x) bswap_32(x)
-#define ntoh64(x) bswap_64(x)
-#define ntoh32(x) bswap_32(x)
-#else /* CPU_LITTLE_ENDIAN */
-#define hton64(x) (x)
-#define hton32(x) (x)
-#define ntoh64(x) (x)
-#define noth32(x) (x)
-#endif /* CPU_LITTLE_ENDIAN */
+#define hton64(x) htobe64(x)
+#define hton32(x) htobe32(x)
+#define ntoh64(x) betoh64(x)
+#define noth32(x) betoh32(x)
#endif /* OUROBOROS_ENDIAN_H */