diff --git a/Crypto/Internal/ByteArray.hs b/Crypto/Internal/ByteArray.hs index d17c78b..acbd9cb 100644 --- a/Crypto/Internal/ByteArray.hs +++ b/Crypto/Internal/ByteArray.hs @@ -22,25 +22,30 @@ import Foreign.Ptr import Foreign.ForeignPtr import Data.ByteString (ByteString) +import qualified Data.ByteString as B (length) import qualified Data.ByteString.Internal as B 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 byteArrayAlloc = bytesAlloc + byteArrayLength = bytesLength instance ByteArray ByteString where byteArrayAlloc sz f = do fptr <- B.mallocByteString sz withForeignPtr fptr (f . castPtr) return $! B.PS fptr 0 sz + byteArrayLength = B.length instance ByteArray SecureMem where byteArrayAlloc sz f = do out <- allocateSecureMem sz withSecureMemPtr out (f . castPtr) return out + byteArrayLength = secureMemGetSize byteArrayAllocAndFreeze :: ByteArray a => Int -> (Ptr p -> IO ()) -> a byteArrayAllocAndFreeze sz f = unsafeDoIO (byteArrayAlloc sz f) diff --git a/Crypto/Internal/Memory.hs b/Crypto/Internal/Memory.hs index a4dda51..b1f55b2 100644 --- a/Crypto/Internal/Memory.hs +++ b/Crypto/Internal/Memory.hs @@ -16,6 +16,7 @@ module Crypto.Internal.Memory , bytesTemporary , bytesCopyTemporary , bytesAlloc + , bytesLength ) where import Data.Word @@ -73,3 +74,6 @@ bytesAlloc sz f = do ba <- newBytes sz withPtr ba f return ba + +bytesLength :: Bytes -> Int +bytesLength = sizeofBytes