-- |
-- Module      : Crypto.Store.Util
-- License     : BSD-style
-- Maintainer  : Olivier Chéron <olivier.cheron@gmail.com>
-- Stability   : experimental
-- Portability : unknown
--
--
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
module Crypto.Store.Util
    ( (&&!)
    , reverseBytes
    , constAllEq
    , mapLeft
    , mapAsWord64LE
    ) where

import           Data.Bits
import           Data.ByteArray (ByteArray, ByteArrayAccess)
import qualified Data.ByteArray as B
import           Data.List
import           Data.Memory.Endian
import           Data.Word

import           Foreign.Ptr (plusPtr)
import           Foreign.Storable

import GHC.Exts

-- | This is a strict version of &&.
(&&!) :: Bool -> Bool -> Bool
&&! :: Bool -> Bool -> Bool
(&&!) Bool
x Bool
y = Int# -> Bool
isTrue# (Int# -> Int# -> Int#
andI# (forall {a}. a -> Int#
getTag# Bool
x) (forall {a}. a -> Int#
getTag# Bool
y))
  where getTag# :: a -> Int#
getTag# !a
z = forall {a}. a -> Int#
dataToTag# a
z
infixr 3 &&!

-- | Reverse a bytearray.
reverseBytes :: ByteArray ba => ba -> ba
#if MIN_VERSION_memory(0,14,18)
reverseBytes :: forall ba. ByteArray ba => ba -> ba
reverseBytes = forall ba. ByteArray ba => ba -> ba
B.reverse
#else
reverseBytes = B.pack . reverse . B.unpack
#endif

-- | Test if all bytes in a bytearray are equal to the value specified.  Runs in
-- constant time.
constAllEq :: ByteArrayAccess ba => Word8 -> ba -> Bool
constAllEq :: forall ba. ByteArrayAccess ba => Word8 -> ba -> Bool
constAllEq Word8
b = (forall a. Eq a => a -> a -> Bool
== Word8
0) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Word8 -> Word8 -> Word8
fn Word8
0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ByteArrayAccess a => a -> [Word8]
B.unpack
  where fn :: Word8 -> Word8 -> Word8
fn Word8
acc Word8
x = Word8
acc forall a. Bits a => a -> a -> a
.|. forall a. Bits a => a -> a -> a
xor Word8
b Word8
x

-- | Map over the left value.
mapLeft :: (a -> b) -> Either a c -> Either b c
mapLeft :: forall a b c. (a -> b) -> Either a c -> Either b c
mapLeft a -> b
f (Left a
a)  = forall a b. a -> Either a b
Left (a -> b
f a
a)
mapLeft a -> b
_ (Right c
c) = forall a b. b -> Either a b
Right c
c

-- | Same as 'Data.ByteArray.Mapping.mapAsWord64' but with little-endian words.
mapAsWord64LE :: ByteArray bs => (Word64 -> Word64) -> bs -> bs
mapAsWord64LE :: forall bs. ByteArray bs => (Word64 -> Word64) -> bs -> bs
mapAsWord64LE Word64 -> Word64
f bs
bs =
    forall a p. ByteArray a => Int -> (Ptr p -> IO ()) -> a
B.allocAndFreeze Int
len forall a b. (a -> b) -> a -> b
$ \Ptr (LE Word64)
dst ->
        forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
B.withByteArray bs
bs forall a b. (a -> b) -> a -> b
$ \Ptr (LE Word64)
src ->
            Int -> Ptr (LE Word64) -> Ptr (LE Word64) -> IO ()
loop (Int
len forall a. Integral a => a -> a -> a
`div` Int
8) Ptr (LE Word64)
dst Ptr (LE Word64)
src
  where
        len :: Int
len = forall ba. ByteArrayAccess ba => ba -> Int
B.length bs
bs

        loop :: Int -> Ptr (LE Word64) -> Ptr (LE Word64) -> IO ()
        loop :: Int -> Ptr (LE Word64) -> Ptr (LE Word64) -> IO ()
loop Int
0 Ptr (LE Word64)
_ Ptr (LE Word64)
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()
        loop Int
i Ptr (LE Word64)
d Ptr (LE Word64)
s = do
            LE Word64
w <- forall a. Storable a => Ptr a -> IO a
peek Ptr (LE Word64)
s
            let r :: Word64
r = Word64 -> Word64
f (forall a. ByteSwap a => LE a -> a
fromLE LE Word64
w)
            forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (LE Word64)
d (forall a. ByteSwap a => a -> LE a
toLE Word64
r)
            Int -> Ptr (LE Word64) -> Ptr (LE Word64) -> IO ()
loop (Int
i forall a. Num a => a -> a -> a
- Int
1) (Ptr (LE Word64)
d forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
8) (Ptr (LE Word64)
s forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
8)