{-# language ScopedTypeVariables #-}
{-# language DataKinds #-}
module System.Nix.Internal.Truncation
( truncateInNixWay
)
where
import qualified Data.ByteString as Bytes
truncateInNixWay
:: Int -> Bytes.ByteString -> Bytes.ByteString
truncateInNixWay :: Int -> ByteString -> ByteString
truncateInNixWay Int
n ByteString
c =
[Word8] -> ByteString
Bytes.pack forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> Word8
truncOutputByte [Int
0 .. Int
nforall a. Num a => a -> a -> a
-Int
1]
where
truncOutputByte :: Int -> Word8
truncOutputByte :: Int -> Word8
truncOutputByte Int
i = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (Int -> Word8 -> Int -> Word8
aux Int
i) Word8
0 [Int
0 .. ByteString -> Int
Bytes.length ByteString
c forall a. Num a => a -> a -> a
- Int
1]
inputByte :: Int -> Word8
inputByte :: Int -> Word8
inputByte Int
j = HasCallStack => ByteString -> Int -> Word8
Bytes.index ByteString
c Int
j
aux :: Int -> Word8 -> Int -> Word8
aux :: Int -> Word8 -> Int -> Word8
aux Int
i Word8
x Int
j =
forall a. a -> a -> Bool -> a
bool
forall a. a -> a
id
(forall a. Bits a => a -> a -> a
`xor` Int -> Word8
inputByte Int
j)
(Int
j forall a. Integral a => a -> a -> a
`mod` Int
n forall a. Eq a => a -> a -> Bool
== Int
i)
Word8
x