[Internal] add a function to set buffer's bytes (i.e. memset)

This commit is contained in:
Vincent Hanquez 2015-04-18 06:52:27 +01:00
parent dc1215dbd7
commit 09365a745f

View File

@ -13,12 +13,13 @@ module Crypto.Internal.Bytes
, tempBufCreate , tempBufCreate
, bufXor , bufXor
, bufCopy , bufCopy
, bufSet
) where ) where
import Control.Applicative ((<$>), (<*>)) import Control.Applicative ((<$>), (<*>))
import Foreign.Ptr (Ptr, plusPtr) import Foreign.Ptr (Ptr, plusPtr)
import Foreign.ForeignPtr (withForeignPtr) import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Storable (peek, poke) import Foreign.Storable (peek, poke, pokeByteOff)
import Foreign.Marshal.Alloc (allocaBytesAligned) import Foreign.Marshal.Alloc (allocaBytesAligned)
import Data.ByteString (ByteString) import Data.ByteString (ByteString)
import Data.Bits (xor) import Data.Bits (xor)
@ -48,3 +49,10 @@ bufXor d s1 s2 n = do
bufCopy :: Ptr Word8 -> Ptr Word8 -> Int -> IO () bufCopy :: Ptr Word8 -> Ptr Word8 -> Int -> IO ()
bufCopy dst src n = memcpy dst src (fromIntegral n) bufCopy dst src n = memcpy dst src (fromIntegral n)
-- | Set @n number of bytes to the same value @v
bufSet :: Ptr Word8 -> Word8 -> Int -> IO ()
bufSet start v n = loop 0
where loop i
| i == n = return ()
| otherwise = pokeByteOff start i v >> loop (i+1)