Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
use inlinePerformIO instead unsafePerformIO.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenthz committed Nov 7, 2012
1 parent 3bc6d95 commit f8bab5f
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 77 deletions.
13 changes: 6 additions & 7 deletions Crypto/Hash/MD2.hs
Expand Up @@ -24,15 +24,14 @@ module Crypto.Hash.MD2
) where

import Prelude hiding (init)
import System.IO.Unsafe (unsafePerformIO)
import Foreign.Ptr
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Storable
import Foreign.Marshal.Alloc
import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word

#ifdef HAVE_CRYPTOAPI
Expand Down Expand Up @@ -121,26 +120,26 @@ finalizeInternalIO ptr =
{-# NOINLINE init #-}
-- | init a context
init :: Ctx
init = unsafePerformIO $ withCtxNew $ c_md2_init
init = inlinePerformIO $ withCtxNew $ c_md2_init

{-# NOINLINE update #-}
-- | update a context with a bytestring
update :: Ctx -> ByteString -> Ctx
update ctx d = unsafePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d
update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d

{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString
finalize ctx = unsafePerformIO $ withCtxThrow ctx finalizeInternalIO
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO

{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString
hash d = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_md2_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr

{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString
hashlazy l = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_md2_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
13 changes: 6 additions & 7 deletions Crypto/Hash/MD4.hs
Expand Up @@ -24,15 +24,14 @@ module Crypto.Hash.MD4
) where

import Prelude hiding (init)
import System.IO.Unsafe (unsafePerformIO)
import Foreign.Ptr
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Storable
import Foreign.Marshal.Alloc
import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word

#ifdef HAVE_CRYPTOAPI
Expand Down Expand Up @@ -121,26 +120,26 @@ finalizeInternalIO ptr =
{-# NOINLINE init #-}
-- | init a context
init :: Ctx
init = unsafePerformIO $ withCtxNew $ c_md4_init
init = inlinePerformIO $ withCtxNew $ c_md4_init

{-# NOINLINE update #-}
-- | update a context with a bytestring
update :: Ctx -> ByteString -> Ctx
update ctx d = unsafePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d
update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d

{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString
finalize ctx = unsafePerformIO $ withCtxThrow ctx finalizeInternalIO
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO

{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString
hash d = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_md4_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr

{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString
hashlazy l = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_md4_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
13 changes: 6 additions & 7 deletions Crypto/Hash/MD5.hs
Expand Up @@ -24,15 +24,14 @@ module Crypto.Hash.MD5
) where

import Prelude hiding (init)
import System.IO.Unsafe (unsafePerformIO)
import Foreign.Ptr
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Storable
import Foreign.Marshal.Alloc
import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word

#ifdef HAVE_CRYPTOAPI
Expand Down Expand Up @@ -121,26 +120,26 @@ finalizeInternalIO ptr =
{-# NOINLINE init #-}
-- | init a context
init :: Ctx
init = unsafePerformIO $ withCtxNew $ c_md5_init
init = inlinePerformIO $ withCtxNew $ c_md5_init

{-# NOINLINE update #-}
-- | update a context with a bytestring
update :: Ctx -> ByteString -> Ctx
update ctx d = unsafePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d
update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d

{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString
finalize ctx = unsafePerformIO $ withCtxThrow ctx finalizeInternalIO
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO

{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString
hash d = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_md5_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr

{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString
hashlazy l = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_md5_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
13 changes: 6 additions & 7 deletions Crypto/Hash/RIPEMD160.hs
Expand Up @@ -24,15 +24,14 @@ module Crypto.Hash.RIPEMD160
) where

import Prelude hiding (init)
import System.IO.Unsafe (unsafePerformIO)
import Foreign.Ptr
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Storable
import Foreign.Marshal.Alloc
import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word

#ifdef HAVE_CRYPTOAPI
Expand Down Expand Up @@ -121,26 +120,26 @@ finalizeInternalIO ptr =
{-# NOINLINE init #-}
-- | init a context
init :: Ctx
init = unsafePerformIO $ withCtxNew $ c_ripemd160_init
init = inlinePerformIO $ withCtxNew $ c_ripemd160_init

{-# NOINLINE update #-}
-- | update a context with a bytestring
update :: Ctx -> ByteString -> Ctx
update ctx d = unsafePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d
update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d

{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString
finalize ctx = unsafePerformIO $ withCtxThrow ctx finalizeInternalIO
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO

{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString
hash d = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_ripemd160_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr

{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString
hashlazy l = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_ripemd160_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
13 changes: 6 additions & 7 deletions Crypto/Hash/SHA1.hs
Expand Up @@ -24,15 +24,14 @@ module Crypto.Hash.SHA1
) where

import Prelude hiding (init)
import System.IO.Unsafe (unsafePerformIO)
import Foreign.Ptr
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Storable
import Foreign.Marshal.Alloc
import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word

#ifdef HAVE_CRYPTOAPI
Expand Down Expand Up @@ -121,26 +120,26 @@ finalizeInternalIO ptr =
{-# NOINLINE init #-}
-- | init a context
init :: Ctx
init = unsafePerformIO $ withCtxNew $ c_sha1_init
init = inlinePerformIO $ withCtxNew $ c_sha1_init

{-# NOINLINE update #-}
-- | update a context with a bytestring
update :: Ctx -> ByteString -> Ctx
update ctx d = unsafePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d
update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d

{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString
finalize ctx = unsafePerformIO $ withCtxThrow ctx finalizeInternalIO
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO

{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString
hash d = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha1_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr

{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString
hashlazy l = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha1_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
13 changes: 6 additions & 7 deletions Crypto/Hash/SHA224.hs
Expand Up @@ -24,15 +24,14 @@ module Crypto.Hash.SHA224
) where

import Prelude hiding (init)
import System.IO.Unsafe (unsafePerformIO)
import Foreign.Ptr
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Storable
import Foreign.Marshal.Alloc
import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word

#ifdef HAVE_CRYPTOAPI
Expand Down Expand Up @@ -121,26 +120,26 @@ finalizeInternalIO ptr =
{-# NOINLINE init #-}
-- | init a context
init :: Ctx
init = unsafePerformIO $ withCtxNew $ c_sha224_init
init = inlinePerformIO $ withCtxNew $ c_sha224_init

{-# NOINLINE update #-}
-- | update a context with a bytestring
update :: Ctx -> ByteString -> Ctx
update ctx d = unsafePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d
update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d

{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString
finalize ctx = unsafePerformIO $ withCtxThrow ctx finalizeInternalIO
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO

{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString
hash d = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha224_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr

{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString
hashlazy l = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha224_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
13 changes: 6 additions & 7 deletions Crypto/Hash/SHA256.hs
Expand Up @@ -24,15 +24,14 @@ module Crypto.Hash.SHA256
) where

import Prelude hiding (init)
import System.IO.Unsafe (unsafePerformIO)
import Foreign.Ptr
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Storable
import Foreign.Marshal.Alloc
import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word

#ifdef HAVE_CRYPTOAPI
Expand Down Expand Up @@ -121,26 +120,26 @@ finalizeInternalIO ptr =
{-# NOINLINE init #-}
-- | init a context
init :: Ctx
init = unsafePerformIO $ withCtxNew $ c_sha256_init
init = inlinePerformIO $ withCtxNew $ c_sha256_init

{-# NOINLINE update #-}
-- | update a context with a bytestring
update :: Ctx -> ByteString -> Ctx
update ctx d = unsafePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d
update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d

{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString
finalize ctx = unsafePerformIO $ withCtxThrow ctx finalizeInternalIO
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO

{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
hash :: ByteString -> ByteString
hash d = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hash d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha256_init ptr >> updateInternalIO ptr d >> finalizeInternalIO ptr

{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
hashlazy :: L.ByteString -> ByteString
hashlazy l = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hashlazy l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha256_init ptr >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr
13 changes: 6 additions & 7 deletions Crypto/Hash/SHA3.hs
Expand Up @@ -23,15 +23,14 @@ module Crypto.Hash.SHA3
) where

import Prelude hiding (init)
import System.IO.Unsafe (unsafePerformIO)
import Foreign.Ptr
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Storable
import Foreign.Marshal.Alloc
import qualified Data.ByteString.Lazy as L
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.ByteString.Internal (create, toForeignPtr)
import Data.ByteString.Internal (create, toForeignPtr, inlinePerformIO)
import Data.Word

data Ctx = Ctx !ByteString
Expand Down Expand Up @@ -97,26 +96,26 @@ finalizeInternalIO ptr =
{-# NOINLINE init #-}
-- | init a context
init :: Int -> Ctx
init hashlen = unsafePerformIO $ withCtxNew (\ctx -> c_sha3_init ctx (fromIntegral hashlen))
init hashlen = inlinePerformIO $ withCtxNew (\ctx -> c_sha3_init ctx (fromIntegral hashlen))

{-# NOINLINE update #-}
-- | update a context with a bytestring
update :: Ctx -> ByteString -> Ctx
update ctx d = unsafePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d
update ctx d = inlinePerformIO $ withCtxCopy ctx $ \ptr -> updateInternalIO ptr d

{-# NOINLINE finalize #-}
-- | finalize the context into a digest bytestring
finalize :: Ctx -> ByteString
finalize ctx = unsafePerformIO $ withCtxThrow ctx finalizeInternalIO
finalize ctx = inlinePerformIO $ withCtxThrow ctx finalizeInternalIO

{-# NOINLINE hash #-}
-- | hash a strict bytestring into a digest bytestring
hash :: Int -> ByteString -> ByteString
hash hashlen d = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hash hashlen d = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha3_init ptr (fromIntegral hashlen) >> updateInternalIO ptr d >> finalizeInternalIO ptr

{-# NOINLINE hashlazy #-}
-- | hash a lazy bytestring into a digest bytestring
hashlazy :: Int -> L.ByteString -> ByteString
hashlazy hashlen l = unsafePerformIO $ withCtxNewThrow $ \ptr -> do
hashlazy hashlen l = inlinePerformIO $ withCtxNewThrow $ \ptr -> do
c_sha3_init ptr (fromIntegral hashlen) >> mapM_ (updateInternalIO ptr) (L.toChunks l) >> finalizeInternalIO ptr

0 comments on commit f8bab5f

Please sign in to comment.