add length abstraction for byte array
This commit is contained in:
parent
a4d3dc4d10
commit
37557af615
@ -22,25 +22,30 @@ import Foreign.Ptr
|
|||||||
import Foreign.ForeignPtr
|
import Foreign.ForeignPtr
|
||||||
|
|
||||||
import Data.ByteString (ByteString)
|
import Data.ByteString (ByteString)
|
||||||
|
import qualified Data.ByteString as B (length)
|
||||||
import qualified Data.ByteString.Internal as B
|
import qualified Data.ByteString.Internal as B
|
||||||
|
|
||||||
class ByteArray ba where
|
class ByteArray ba where
|
||||||
byteArrayAlloc :: Int -> (Ptr p -> IO ()) -> IO ba
|
byteArrayAlloc :: Int -> (Ptr p -> IO ()) -> IO ba
|
||||||
|
byteArrayLength :: ba -> Int
|
||||||
|
|
||||||
instance ByteArray Bytes where
|
instance ByteArray Bytes where
|
||||||
byteArrayAlloc = bytesAlloc
|
byteArrayAlloc = bytesAlloc
|
||||||
|
byteArrayLength = bytesLength
|
||||||
|
|
||||||
instance ByteArray ByteString where
|
instance ByteArray ByteString where
|
||||||
byteArrayAlloc sz f = do
|
byteArrayAlloc sz f = do
|
||||||
fptr <- B.mallocByteString sz
|
fptr <- B.mallocByteString sz
|
||||||
withForeignPtr fptr (f . castPtr)
|
withForeignPtr fptr (f . castPtr)
|
||||||
return $! B.PS fptr 0 sz
|
return $! B.PS fptr 0 sz
|
||||||
|
byteArrayLength = B.length
|
||||||
|
|
||||||
instance ByteArray SecureMem where
|
instance ByteArray SecureMem where
|
||||||
byteArrayAlloc sz f = do
|
byteArrayAlloc sz f = do
|
||||||
out <- allocateSecureMem sz
|
out <- allocateSecureMem sz
|
||||||
withSecureMemPtr out (f . castPtr)
|
withSecureMemPtr out (f . castPtr)
|
||||||
return out
|
return out
|
||||||
|
byteArrayLength = secureMemGetSize
|
||||||
|
|
||||||
byteArrayAllocAndFreeze :: ByteArray a => Int -> (Ptr p -> IO ()) -> a
|
byteArrayAllocAndFreeze :: ByteArray a => Int -> (Ptr p -> IO ()) -> a
|
||||||
byteArrayAllocAndFreeze sz f = unsafeDoIO (byteArrayAlloc sz f)
|
byteArrayAllocAndFreeze sz f = unsafeDoIO (byteArrayAlloc sz f)
|
||||||
|
|||||||
@ -16,6 +16,7 @@ module Crypto.Internal.Memory
|
|||||||
, bytesTemporary
|
, bytesTemporary
|
||||||
, bytesCopyTemporary
|
, bytesCopyTemporary
|
||||||
, bytesAlloc
|
, bytesAlloc
|
||||||
|
, bytesLength
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Word
|
import Data.Word
|
||||||
@ -73,3 +74,6 @@ bytesAlloc sz f = do
|
|||||||
ba <- newBytes sz
|
ba <- newBytes sz
|
||||||
withPtr ba f
|
withPtr ba f
|
||||||
return ba
|
return ba
|
||||||
|
|
||||||
|
bytesLength :: Bytes -> Int
|
||||||
|
bytesLength = sizeofBytes
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user