Skip to content

Commit

Permalink
Remove unpackByteStringCStringLen
Browse files Browse the repository at this point in the history
  • Loading branch information
tdammers committed Jun 21, 2023
1 parent 1e67222 commit 00c4e4e
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ module Cardano.Crypto.Libsodium.Memory (
allocaBytes,

-- * ByteString memory access, generalized to 'MonadST'
unpackByteStringCStringLen,
packByteStringCStringLen,
) where

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ module Cardano.Crypto.Libsodium.Memory.Internal (
allocaBytes,

-- * ByteString memory access, generalized to 'MonadST'
unpackByteStringCStringLen,
packByteStringCStringLen,

-- * Helper
Expand All @@ -50,10 +49,8 @@ import Control.Monad.ST
import Control.Monad.ST.Unsafe (unsafeIOToST, unsafeSTToIO)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Unsafe as BS
import Data.Coerce (coerce)
import Data.Typeable
import Data.Word (Word8)
import Debug.Trace (traceShowM)
import Foreign.C.Error (errnoToIOError, getErrno)
import Foreign.C.String (CStringLen)
Expand All @@ -64,7 +61,7 @@ import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
import qualified Foreign.Marshal.Alloc as Foreign
import Foreign.Marshal.Utils (fillBytes)
import Foreign.Ptr (Ptr, nullPtr, castPtr)
import Foreign.Storable (Storable (peek), sizeOf, alignment, pokeByteOff)
import Foreign.Storable (Storable (peek), sizeOf, alignment)
import GHC.IO.Exception (ioException)
import GHC.TypeLits (KnownNat, natVal)
import NoThunks.Class (NoThunks, OnlyCheckWhnfNamed (..))
Expand Down Expand Up @@ -195,17 +192,6 @@ allocaBytes :: Int -> (Ptr a -> ST s b) -> ST s b
allocaBytes size f =
unsafeIOToST $ Foreign.allocaBytes size (unsafeSTToIO . f)

-- | Unpacks a ByteString into a temporary buffer and runs the provided 'ST'
-- function on it.
unpackByteStringCStringLen :: ByteString -> (CStringLen -> ST s a) -> ST s a
unpackByteStringCStringLen bs f = do
let len = BS.length bs
allocaBytes (len + 1) $ \buf -> do
unsafeIOToST $ BS.unsafeUseAsCString bs $ \ptr -> do
copyMem buf ptr (fromIntegral len)
pokeByteOff buf len (0 :: Word8)
f (buf, len)

packByteStringCStringLen :: MonadST m => CStringLen -> m ByteString
packByteStringCStringLen (ptr, len) =
withLiftST $ \lift -> lift . unsafeIOToST $ BS.packCStringLen (ptr, len)
Expand Down

0 comments on commit 00c4e4e

Please sign in to comment.