From 71fe77da6816f1a2700d7040b1ac16b50d2e5199 Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Fri, 28 Aug 2015 17:10:27 +0100 Subject: [PATCH] [endianess] fix some issues on non supported arches --- Crypto/Internal/CompatPrim.hs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/Crypto/Internal/CompatPrim.hs b/Crypto/Internal/CompatPrim.hs index dd514e5..68c951f 100644 --- a/Crypto/Internal/CompatPrim.hs +++ b/Crypto/Internal/CompatPrim.hs @@ -24,6 +24,9 @@ module Crypto.Internal.CompatPrim ) where import GHC.Prim +#if !defined(ARCH_IS_LITTLE_ENDIAN) && !defined(ARCH_IS_BIG_ENDIAN) +import Data.Memory.Endian (getSystemEndianness, Endianness(..)) +#endif -- | byteswap Word# to or from Big Endian -- @@ -31,8 +34,10 @@ import GHC.Prim be32Prim :: Word# -> Word# #ifdef ARCH_IS_LITTLE_ENDIAN be32Prim = byteswap32Prim +#elif defined(ARCH_IS_BIG_ENDIAN) +be32Prim = id #else -be32Prim w = w +be32Prim w = if getSystemEndianness == LittleEndian then byteswap32Prim w else w #endif -- | byteswap Word# to or from Little Endian @@ -41,8 +46,10 @@ be32Prim w = w le32Prim :: Word# -> Word# #ifdef ARCH_IS_LITTLE_ENDIAN le32Prim w = w -#else +#elif defined(ARCH_IS_BIG_ENDIAN) le32Prim = byteswap32Prim +#else +le32Prim w = if getSystemEndianness == LittleEndian then w else byteswap32Prim w #endif -- | Simple compatibility for byteswap the lower 32 bits of a Word# @@ -69,11 +76,24 @@ convert4To32 a b c d = or# (or# c1 c2) (or# c3 c4) !c2 = uncheckedShiftL# b 16# !c3 = uncheckedShiftL# c 8# !c4 = d -#else +#elif defined(ARCH_IS_BIG_ENDIAN) !c1 = uncheckedShiftL# d 24# !c2 = uncheckedShiftL# c 16# !c3 = uncheckedShiftL# b 8# !c4 = a +#else + !c1 + | getSystemEndianness == LittleEndian = uncheckedShiftL# a 24# + | otherwise = uncheckedShiftL# d 24# + !c2 + | getSystemEndianness == LittleEndian = uncheckedShiftL# b 16# + | otherwise = uncheckedShiftL# c 16# + !c3 + | getSystemEndianness == LittleEndian = uncheckedShiftL# c 8# + | otherwise = uncheckedShiftL# b 8# + !c4 + | getSystemEndianness == LittleEndian = d + | otherwise = a #endif -- | Simple wrapper to handle pre 7.8 and future, where