[internal] add some new function to copy and alloc + ret
This commit is contained in:
parent
3adfa4ab2a
commit
cd216f5ee9
@ -13,8 +13,11 @@
|
|||||||
module Crypto.Internal.ByteArray
|
module Crypto.Internal.ByteArray
|
||||||
( ByteArray(..)
|
( ByteArray(..)
|
||||||
, ByteArrayAccess(..)
|
, ByteArrayAccess(..)
|
||||||
|
, byteArrayAlloc
|
||||||
, byteArrayAllocAndFreeze
|
, byteArrayAllocAndFreeze
|
||||||
, empty
|
, empty
|
||||||
|
, byteArrayCopy
|
||||||
|
, byteArrayCopyRet
|
||||||
, byteArrayCopyAndFreeze
|
, byteArrayCopyAndFreeze
|
||||||
, byteArraySplit
|
, byteArraySplit
|
||||||
, byteArrayXor
|
, byteArrayXor
|
||||||
@ -50,32 +53,35 @@ class ByteArrayAccess ba where
|
|||||||
withByteArray :: ba -> (Ptr p -> IO a) -> IO a
|
withByteArray :: ba -> (Ptr p -> IO a) -> IO a
|
||||||
|
|
||||||
class ByteArrayAccess ba => ByteArray ba where
|
class ByteArrayAccess ba => ByteArray ba where
|
||||||
byteArrayAlloc :: Int -> (Ptr p -> IO ()) -> IO ba
|
byteArrayAllocRet :: Int -> (Ptr p -> IO a) -> IO (a, ba)
|
||||||
|
|
||||||
|
byteArrayAlloc :: ByteArray ba => Int -> (Ptr p -> IO ()) -> IO ba
|
||||||
|
byteArrayAlloc n f = snd `fmap` byteArrayAllocRet n f
|
||||||
|
|
||||||
instance ByteArrayAccess Bytes where
|
instance ByteArrayAccess Bytes where
|
||||||
byteArrayLength = bytesLength
|
byteArrayLength = bytesLength
|
||||||
withByteArray = withBytes
|
withByteArray = withBytes
|
||||||
instance ByteArray Bytes where
|
instance ByteArray Bytes where
|
||||||
byteArrayAlloc = bytesAlloc
|
byteArrayAllocRet = bytesAllocRet
|
||||||
|
|
||||||
instance ByteArrayAccess ByteString where
|
instance ByteArrayAccess ByteString where
|
||||||
byteArrayLength = B.length
|
byteArrayLength = B.length
|
||||||
withByteArray b f = withForeignPtr fptr $ \ptr -> f (ptr `plusPtr` off)
|
withByteArray b f = withForeignPtr fptr $ \ptr -> f (ptr `plusPtr` off)
|
||||||
where (fptr, off, _) = B.toForeignPtr b
|
where (fptr, off, _) = B.toForeignPtr b
|
||||||
instance ByteArray ByteString where
|
instance ByteArray ByteString where
|
||||||
byteArrayAlloc sz f = do
|
byteArrayAllocRet sz f = do
|
||||||
fptr <- B.mallocByteString sz
|
fptr <- B.mallocByteString sz
|
||||||
withForeignPtr fptr (f . castPtr)
|
r <- withForeignPtr fptr (f . castPtr)
|
||||||
return $! B.PS fptr 0 sz
|
return (r, B.PS fptr 0 sz)
|
||||||
|
|
||||||
instance ByteArrayAccess SecureMem where
|
instance ByteArrayAccess SecureMem where
|
||||||
byteArrayLength = secureMemGetSize
|
byteArrayLength = secureMemGetSize
|
||||||
withByteArray b f = withSecureMemPtr b (f . castPtr)
|
withByteArray b f = withSecureMemPtr b (f . castPtr)
|
||||||
instance ByteArray SecureMem where
|
instance ByteArray SecureMem where
|
||||||
byteArrayAlloc sz f = do
|
byteArrayAllocRet sz f = do
|
||||||
out <- allocateSecureMem sz
|
out <- allocateSecureMem sz
|
||||||
withSecureMemPtr out (f . castPtr)
|
r <- withSecureMemPtr out (f . castPtr)
|
||||||
return out
|
return (r, out)
|
||||||
|
|
||||||
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)
|
||||||
@ -123,6 +129,18 @@ byteArrayConcat allBs = byteArrayAllocAndFreeze total (loop allBs)
|
|||||||
withByteArray b $ \p -> bufCopy dst p sz
|
withByteArray b $ \p -> bufCopy dst p sz
|
||||||
loop bs (dst `plusPtr` sz)
|
loop bs (dst `plusPtr` sz)
|
||||||
|
|
||||||
|
byteArrayCopy :: (ByteArray bs1, ByteArray bs2) => bs1 -> (Ptr p -> IO ()) -> IO bs2
|
||||||
|
byteArrayCopy bs f =
|
||||||
|
byteArrayAlloc (byteArrayLength bs) $ \d -> do
|
||||||
|
withByteArray bs $ \s -> bufCopy d s (byteArrayLength bs)
|
||||||
|
f (castPtr d)
|
||||||
|
|
||||||
|
byteArrayCopyRet :: (ByteArray bs1, ByteArray bs2) => bs1 -> (Ptr p -> IO a) -> IO (a, bs2)
|
||||||
|
byteArrayCopyRet bs f =
|
||||||
|
byteArrayAllocRet (byteArrayLength bs) $ \d -> do
|
||||||
|
withByteArray bs $ \s -> bufCopy d s (byteArrayLength bs)
|
||||||
|
f (castPtr d)
|
||||||
|
|
||||||
byteArrayCopyAndFreeze :: (ByteArray bs1, ByteArray bs2) => bs1 -> (Ptr p -> IO ()) -> bs2
|
byteArrayCopyAndFreeze :: (ByteArray bs1, ByteArray bs2) => bs1 -> (Ptr p -> IO ()) -> bs2
|
||||||
byteArrayCopyAndFreeze bs f =
|
byteArrayCopyAndFreeze bs f =
|
||||||
byteArrayAllocAndFreeze (byteArrayLength bs) $ \d -> do
|
byteArrayAllocAndFreeze (byteArrayLength bs) $ \d -> do
|
||||||
|
|||||||
@ -16,6 +16,7 @@ module Crypto.Internal.Memory
|
|||||||
, bytesTemporary
|
, bytesTemporary
|
||||||
, bytesCopyTemporary
|
, bytesCopyTemporary
|
||||||
, bytesAlloc
|
, bytesAlloc
|
||||||
|
, bytesAllocRet
|
||||||
, bytesLength
|
, bytesLength
|
||||||
, withBytes
|
, withBytes
|
||||||
, SecureBytes
|
, SecureBytes
|
||||||
@ -75,6 +76,12 @@ bytesAlloc sz f = do
|
|||||||
withPtr ba f
|
withPtr ba f
|
||||||
return ba
|
return ba
|
||||||
|
|
||||||
|
bytesAllocRet :: Int -> (Ptr p -> IO a) -> IO (a, Bytes)
|
||||||
|
bytesAllocRet sz f = do
|
||||||
|
ba <- newBytes sz
|
||||||
|
r <- withPtr ba f
|
||||||
|
return (r, ba)
|
||||||
|
|
||||||
bytesLength :: Bytes -> Int
|
bytesLength :: Bytes -> Int
|
||||||
bytesLength = sizeofBytes
|
bytesLength = sizeofBytes
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user