add withByteArray

This commit is contained in:
Vincent Hanquez 2015-03-29 10:43:11 +01:00
parent 37557af615
commit f3bf67df99
2 changed files with 10 additions and 1 deletions

View File

@ -28,10 +28,12 @@ import qualified Data.ByteString.Internal as B
class ByteArray ba where
byteArrayAlloc :: Int -> (Ptr p -> IO ()) -> IO ba
byteArrayLength :: ba -> Int
withByteArray :: ba -> (Ptr p -> IO a) -> IO a
instance ByteArray Bytes where
byteArrayAlloc = bytesAlloc
byteArrayAlloc = bytesAlloc
byteArrayLength = bytesLength
withByteArray = withBytes
instance ByteArray ByteString where
byteArrayAlloc sz f = do
@ -39,6 +41,8 @@ instance ByteArray ByteString where
withForeignPtr fptr (f . castPtr)
return $! B.PS fptr 0 sz
byteArrayLength = B.length
withByteArray b f = withForeignPtr fptr $ \ptr -> f (ptr `plusPtr` off)
where (fptr, off, _) = B.toForeignPtr b
instance ByteArray SecureMem where
byteArrayAlloc sz f = do
@ -46,6 +50,7 @@ instance ByteArray SecureMem where
withSecureMemPtr out (f . castPtr)
return out
byteArrayLength = secureMemGetSize
withByteArray b f = withSecureMemPtr b (f . castPtr)
byteArrayAllocAndFreeze :: ByteArray a => Int -> (Ptr p -> IO ()) -> a
byteArrayAllocAndFreeze sz f = unsafeDoIO (byteArrayAlloc sz f)

View File

@ -17,6 +17,7 @@ module Crypto.Internal.Memory
, bytesCopyTemporary
, bytesAlloc
, bytesLength
, withBytes
) where
import Data.Word
@ -77,3 +78,6 @@ bytesAlloc sz f = do
bytesLength :: Bytes -> Int
bytesLength = sizeofBytes
withBytes :: Bytes -> (Ptr p -> IO a) -> IO a
withBytes = withPtr