about summary refs log tree commit diff stats
path: root/src/tools
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-10-26 21:43:50 +0000
committerJames Booth <boothj5@gmail.com>2014-10-26 21:43:50 +0000
commit6c980969e051676dbd7ee62e19b8463ad57e7852 (patch)
tree94f9b1a9fa86dba3b9ab0748bd27a4907545f59a /src/tools
parentd7911664e1fe0a000c06b5cf953992745341a56a (diff)
downloadprofani-tty-6c980969e051676dbd7ee62e19b8463ad57e7852.tar.gz
Added compile time checks for big endian from @pasis
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/p_sha1.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/tools/p_sha1.c b/src/tools/p_sha1.c
index 1936b89c..11adede3 100644
--- a/src/tools/p_sha1.c
+++ b/src/tools/p_sha1.c
@@ -135,11 +135,23 @@ void SHAPrintContext(P_P_SHA1_CTX *context, char *msg){
 
 static uint32_t host_to_be(uint32_t i)
 {
+#define le_to_be(i) ((rol((i),24) & 0xFF00FF00) | (rol((i),8) & 0x00FF00FF))
+#if defined(__BIG_ENDIAN__) || \
+    (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
+    __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
+    return i;
+#elif defined(__LITTLE_ENDIAN__) || \
+    (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
+    __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
+    return le_to_be(i);
+#else /* fallback to run-time check */
     static const union {
-        unsigned u;
+        uint32_t u;
         unsigned char c;
     } check = {1};
-    return check.c ? (rol(i,24)&0xFF00FF00)|(rol(i,8)&0x00FF00FF) : i;
+
+    return check.c ? le_to_be(i) : i;
+#endif
 }
 
 /* Hash a single 512-bit block. This is the core of the algorithm. */