Permalink
Browse files

Cosmetic changes suggested by hlint.

Ignore-this: 559b7ed22eed958a551c5ff2e45f7d45

darcs-hash:20100208161936-62b54-98ba470151d95547710165407d37848902960576.gz
  • Loading branch information...
depressed-pho committed Feb 8, 2010
1 parent 231b6c8 commit be44eaae669049b4c8baa4b9cf3f7f96896f1216
View
@@ -90,8 +90,8 @@ peekASN1Integer intPtr
allocaASN1Integer :: (Ptr ASN1_INTEGER -> IO a) -> IO a
-allocaASN1Integer m
- = bracket _ASN1_INTEGER_new _ASN1_INTEGER_free m
+allocaASN1Integer
+ = bracket _ASN1_INTEGER_new _ASN1_INTEGER_free
withASN1Integer :: Integer -> (Ptr ASN1_INTEGER -> IO a) -> IO a
@@ -149,13 +149,13 @@ peekASN1Time time
allocaASN1Time :: (Ptr ASN1_TIME -> IO a) -> IO a
-allocaASN1Time m
- = bracket _ASN1_TIME_new _ASN1_TIME_free m
+allocaASN1Time
+ = bracket _ASN1_TIME_new _ASN1_TIME_free
withASN1Time :: UTCTime -> (Ptr ASN1_TIME -> IO a) -> IO a
withASN1Time utc m
= allocaASN1Time $ \ time ->
- do _ASN1_TIME_set time (fromIntegral $ (round $ utcTimeToPOSIXSeconds utc :: Integer))
+ do _ASN1_TIME_set time (fromIntegral (round $ utcTimeToPOSIXSeconds utc :: Integer))
>>= failIfNull_
m time
View
@@ -113,7 +113,8 @@ new method
wrapBioPtr :: Ptr BIO_ -> IO BIO
-wrapBioPtr bioPtr = Conc.newForeignPtr bioPtr (_free bioPtr) >>= return . BIO
+wrapBioPtr bioPtr
+ = fmap BIO (Conc.newForeignPtr bioPtr (_free bioPtr))
withBioPtr :: BIO -> (Ptr BIO_ -> IO a) -> IO a
@@ -174,13 +175,13 @@ bioJoin (a:b:xs) = bioPush a b >> bioJoin (b:xs)
setFlags :: BIO -> CInt -> IO ()
setFlags bio flags
- = withBioPtr bio $ \ bioPtr ->
- _set_flags bioPtr flags
+ = withBioPtr bio $ flip _set_flags flags
+
bioShouldRetry :: BIO -> IO Bool
bioShouldRetry bio
= withBioPtr bio $ \ bioPtr ->
- _should_retry bioPtr >>= return . (/= 0)
+ fmap (/= 0) (_should_retry bioPtr)
{- ctrl --------------------------------------------------------------------- -}
@@ -215,7 +216,7 @@ bioReset bio
bioEOF :: BIO -> IO Bool
bioEOF bio
= withBioPtr bio $ \ bioPtr ->
- _eof bioPtr >>= return . (== 1)
+ fmap (==1) (_eof bioPtr)
{- I/O ---------------------------------------------------------------------- -}
@@ -253,7 +254,7 @@ bioReadBS bio maxLen
-- |@'bioReadLBS' bio@ lazily reads all data in @bio@, then return a
-- LazyByteString.
bioReadLBS :: BIO -> IO L.ByteString
-bioReadLBS bio = lazyRead >>= return . L.fromChunks
+bioReadLBS bio = fmap L.fromChunks lazyRead
where
chunkSize = L.defaultChunkSize
View
@@ -20,7 +20,7 @@ module OpenSSL.Cipher
, aesCTR)
where
-import Control.Monad (when)
+import Control.Monad (when, unless)
import Data.IORef
import Foreign
import Foreign.C.Types
@@ -69,7 +69,7 @@ newAESCtx :: Mode -- ^ For CTR mode, this must always be Encrypt
-> IO AESCtx
newAESCtx mode key iv = do
let keyLen = BS.length key * 8
- when (not $ any ((==) keyLen) [128, 192, 256]) $ fail "Bad AES key length"
+ unless (any (keyLen ==) [128, 192, 256]) $ fail "Bad AES key length"
when (BS.length iv /= 16) $ fail "Bad AES128 iv length"
ctx <- mallocForeignPtrBytes (#size AES_KEY)
withForeignPtr ctx $ \ctxPtr ->
@@ -105,7 +105,7 @@ aesCTR :: AESCtx -- ^ context
-> BS.ByteString -- ^ input, any number of bytes
-> IO BS.ByteString
aesCTR (AESCtx _ _ _ _ Decrypt) _ = fail "the context mode must be Encrypt"
-aesCTR (AESCtx ctx iv ecounter nref Encrypt) input = do
+aesCTR (AESCtx ctx iv ecounter nref Encrypt) input =
withForeignPtr ctx $ \ctxPtr ->
withForeignPtr iv $ \ivPtr ->
withForeignPtr ecounter $ \ecptr ->
View
@@ -62,7 +62,7 @@ class DSAKey k where
dsaSize dsa
= unsafePerformIO $
withDSAPtr dsa $ \ dsaPtr ->
- _size dsaPtr >>= return . fromIntegral
+ fmap fromIntegral (_size dsaPtr)
-- |Return the public prime number of the key.
dsaP :: k -> Integer
@@ -89,7 +89,7 @@ class DSAKey k where
instance DSAKey DSAPubKey where
withDSAPtr (DSAPubKey fp) = withForeignPtr fp
peekDSAPtr dsaPtr = _pubDup dsaPtr >>= absorbDSAPtr
- absorbDSAPtr dsaPtr = newForeignPtr _free dsaPtr >>= return . Just . DSAPubKey
+ absorbDSAPtr dsaPtr = fmap (Just . DSAPubKey) (newForeignPtr _free dsaPtr)
instance DSAKey DSAKeyPair where
@@ -103,14 +103,14 @@ instance DSAKey DSAKeyPair where
absorbDSAPtr dsaPtr
= do hasP <- hasDSAPrivateKey dsaPtr
if hasP then
- newForeignPtr _free dsaPtr >>= return . Just . DSAKeyPair
+ fmap (Just . DSAKeyPair) (newForeignPtr _free dsaPtr)
else
return Nothing
hasDSAPrivateKey :: Ptr DSA -> IO Bool
hasDSAPrivateKey dsaPtr
- = (#peek DSA, priv_key) dsaPtr >>= return . (/= nullPtr)
+ = fmap (/= nullPtr) ((#peek DSA, priv_key) dsaPtr)
foreign import ccall unsafe "&DSA_free"
@@ -162,8 +162,8 @@ generateDSAParameters :: Int -- ^ The number of bits in the generated prime: 51
-> IO (Int, Int, Integer, Integer, Integer) -- ^ (iteration count, generator count, p, q, g)
generateDSAParameters nbits mseed = do
when (nbits < 512 || nbits > 1024) $ fail "Invalid DSA bit size"
- alloca (\i1 -> do
- alloca (\i2 -> do
+ alloca (\i1 ->
+ alloca (\i2 ->
(\x -> case mseed of
Nothing -> x (nullPtr, 0)
Just seed -> BS.useAsCStringLen seed x) (\(seedptr, seedlen) -> do
@@ -204,11 +204,11 @@ generateDSAKey :: Integer -- ^ p
-> IO DSAKeyPair
generateDSAKey p q g = do
ptr <- _dsa_new
- newBN p >>= return . unwrapBN >>= (#poke DSA, p) ptr
- newBN q >>= return . unwrapBN >>= (#poke DSA, q) ptr
- newBN g >>= return . unwrapBN >>= (#poke DSA, g) ptr
+ fmap unwrapBN (newBN p) >>= (#poke DSA, p) ptr
+ fmap unwrapBN (newBN q) >>= (#poke DSA, q) ptr
+ fmap unwrapBN (newBN g) >>= (#poke DSA, g) ptr
_dsa_generate_key ptr
- newForeignPtr _free ptr >>= return . DSAKeyPair
+ fmap DSAKeyPair (newForeignPtr _free ptr)
-- |Return the private key @x@.
dsaPrivate :: DSAKeyPair -> Integer
@@ -242,47 +242,47 @@ dsaKeyPairToTuple dsa
tupleToDSAPubKey :: (Integer, Integer, Integer, Integer) -> DSAPubKey
tupleToDSAPubKey (p, q, g, pub) = unsafePerformIO $ do
ptr <- _dsa_new
- newBN p >>= return . unwrapBN >>= (#poke DSA, p) ptr
- newBN q >>= return . unwrapBN >>= (#poke DSA, q) ptr
- newBN g >>= return . unwrapBN >>= (#poke DSA, g) ptr
- newBN pub >>= return . unwrapBN >>= (#poke DSA, pub_key) ptr
+ fmap unwrapBN (newBN p ) >>= (#poke DSA, p) ptr
+ fmap unwrapBN (newBN q ) >>= (#poke DSA, q) ptr
+ fmap unwrapBN (newBN g ) >>= (#poke DSA, g) ptr
+ fmap unwrapBN (newBN pub) >>= (#poke DSA, pub_key) ptr
(#poke DSA, priv_key) ptr nullPtr
- newForeignPtr _free ptr >>= return . DSAPubKey
+ fmap DSAPubKey (newForeignPtr _free ptr)
-- | Convert a tuple of members (in the same format as from
-- 'dsaPubKeyToTuple') into a DSAPubKey object
tupleToDSAKeyPair :: (Integer, Integer, Integer, Integer, Integer) -> DSAKeyPair
tupleToDSAKeyPair (p, q, g, pub, pri) = unsafePerformIO $ do
ptr <- _dsa_new
- newBN p >>= return . unwrapBN >>= (#poke DSA, p) ptr
- newBN q >>= return . unwrapBN >>= (#poke DSA, q) ptr
- newBN g >>= return . unwrapBN >>= (#poke DSA, g) ptr
- newBN pub >>= return . unwrapBN >>= (#poke DSA, pub_key ) ptr
- newBN pri >>= return . unwrapBN >>= (#poke DSA, priv_key) ptr
- newForeignPtr _free ptr >>= return . DSAKeyPair
+ fmap unwrapBN (newBN p ) >>= (#poke DSA, p) ptr
+ fmap unwrapBN (newBN q ) >>= (#poke DSA, q) ptr
+ fmap unwrapBN (newBN g ) >>= (#poke DSA, g) ptr
+ fmap unwrapBN (newBN pub) >>= (#poke DSA, pub_key ) ptr
+ fmap unwrapBN (newBN pri) >>= (#poke DSA, priv_key) ptr
+ fmap DSAKeyPair (newForeignPtr _free ptr)
-- | A utility function to generate both the parameters and the key pair at the
-- same time. Saves serialising and deserialising the parameters too
generateDSAParametersAndKey :: Int -- ^ The number of bits in the generated prime: 512 <= x <= 1024
-> Maybe BS.ByteString -- ^ optional seed, its length must be 20 bytes
-> IO DSAKeyPair
-generateDSAParametersAndKey nbits mseed = do
+generateDSAParametersAndKey nbits mseed =
(\x -> case mseed of
Nothing -> x (nullPtr, 0)
Just seed -> BS.useAsCStringLen seed x) (\(seedptr, seedlen) -> do
ptr <- _generate_params (fromIntegral nbits) seedptr (fromIntegral seedlen) nullPtr nullPtr nullPtr nullPtr
failIfNull_ ptr
_dsa_generate_key ptr
- newForeignPtr _free ptr >>= return . DSAKeyPair)
+ fmap DSAKeyPair (newForeignPtr _free ptr))
-- | Sign pre-digested data. The DSA specs call for SHA1 to be used so, if you
-- use anything else, YMMV. Returns a pair of Integers which, together, are
-- the signature
signDigestedDataWithDSA :: DSAKeyPair -> BS.ByteString -> IO (Integer, Integer)
-signDigestedDataWithDSA dsa bytes = do
- BS.useAsCStringLen bytes (\(ptr, len) -> do
- alloca (\rptr -> do
- alloca (\sptr -> do
+signDigestedDataWithDSA dsa bytes =
+ BS.useAsCStringLen bytes (\(ptr, len) ->
+ alloca (\rptr ->
+ alloca (\sptr ->
withDSAPtr dsa (\dsaptr -> do
_dsa_sign dsaptr ptr (fromIntegral len) rptr sptr >>= failIf_ (== 0)
r <- peek rptr >>= peekBN . wrapBN
@@ -293,12 +293,13 @@ signDigestedDataWithDSA dsa bytes = do
-- | Verify pre-digested data given a signature.
verifyDigestedDataWithDSA :: DSAKey k => k -> BS.ByteString -> (Integer, Integer) -> IO Bool
-verifyDigestedDataWithDSA dsa bytes (r, s) = do
- BS.useAsCStringLen bytes (\(ptr, len) -> do
- withBN r (\bnR -> do
- withBN s (\bnS -> do
- withDSAPtr dsa (\dsaptr -> do
- _dsa_verify dsaptr ptr (fromIntegral len) (unwrapBN bnR) (unwrapBN bnS) >>= return . (== 1)))))
+verifyDigestedDataWithDSA dsa bytes (r, s) =
+ BS.useAsCStringLen bytes (\(ptr, len) ->
+ withBN r (\bnR ->
+ withBN s (\bnS ->
+ withDSAPtr dsa (\dsaptr ->
+ fmap (== 1)
+ (_dsa_verify dsaptr ptr (fromIntegral len) (unwrapBN bnR) (unwrapBN bnS))))))
instance Eq DSAPubKey where
View
@@ -21,11 +21,11 @@ foreign import ccall unsafe "ERR_error_string"
getError :: IO Integer
-getError = _get_error >>= return . fromIntegral
+getError = fmap fromIntegral _get_error
peekError :: IO Integer
-peekError = _peek_error >>= return . fromIntegral
+peekError = fmap fromIntegral _peek_error
errorString :: Integer -> IO String
View
@@ -54,7 +54,8 @@ encodeBlock inBS
= unsafePerformIO $
unsafeUseAsCStringLen inBS $ \ (inBuf, inLen) ->
createAndTrim maxOutLen $ \ outBuf ->
- _EncodeBlock (castPtr outBuf) inBuf (fromIntegral inLen) >>= return . fromIntegral
+ fmap fromIntegral
+ (_EncodeBlock (castPtr outBuf) inBuf (fromIntegral inLen))
where
maxOutLen = (inputLen `div` 3 + 1) * 4 + 1 -- +1: '\0'
inputLen = B8.length inBS
View
@@ -109,8 +109,7 @@ foreign import ccall unsafe "HsOpenSSL_EVP_CIPHER_CTX_block_size"
newCtx :: IO CipherCtx
newCtx = do ctx <- mallocForeignPtrBytes (#size EVP_CIPHER_CTX)
- withForeignPtr ctx $ \ ctxPtr ->
- _ctx_init ctxPtr
+ withForeignPtr ctx _ctx_init
addForeignPtrFinalizer _ctx_cleanup ctx
return $ CipherCtx ctx
@@ -157,11 +156,11 @@ cipherStrictLBS :: Cipher -- ^ Cipher
-> CryptoMode -- ^ Encrypt\/Decrypt
-> L8.ByteString -- ^ Input
-> IO L8.ByteString
-cipherStrictLBS (Cipher c) key iv mode input = do
- allocaBytes (#size EVP_CIPHER_CTX) $ \cptr -> do
- bracket_ (_ctx_init cptr) (_ctx_cleanup' cptr) $ do
- unsafeUseAsCStringLen key $ \(keyp,_) -> do
- unsafeUseAsCStringLen iv $ \(ivp, _) -> do
+cipherStrictLBS (Cipher c) key iv mode input =
+ allocaBytes (#size EVP_CIPHER_CTX) $ \cptr ->
+ bracket_ (_ctx_init cptr) (_ctx_cleanup' cptr) $
+ unsafeUseAsCStringLen key $ \(keyp,_) ->
+ unsafeUseAsCStringLen iv $ \(ivp, _) -> do
failIf_ (/= 1) =<< _CipherInit cptr c keyp ivp (cryptoModeToInt mode)
cc <- fmap CipherCtx (newForeignPtr_ cptr)
rr <- cipherUpdateBS cc `mapM` L8.toChunks input
View
@@ -90,8 +90,7 @@ foreign import ccall unsafe "&EVP_MD_CTX_cleanup"
newCtx :: IO DigestCtx
newCtx = do ctx <- mallocForeignPtrBytes (#size EVP_MD_CTX)
- withForeignPtr ctx $ \ ctxPtr ->
- _ctx_init ctxPtr
+ withForeignPtr ctx _ctx_init
addForeignPtrFinalizer _ctx_cleanup ctx
return $ DigestCtx ctx
@@ -168,22 +167,19 @@ digest md input
-- |@'digestBS'@ digests a chunk of data.
digestBS :: Digest -> B8.ByteString -> String
digestBS md input
- = unsafePerformIO $
- do ctx <- digestStrictly md input
- digestFinal ctx
+ = unsafePerformIO
+ (digestStrictly md input >>= digestFinal)
digestBS' :: Digest -> B8.ByteString -> B8.ByteString
digestBS' md input
- = unsafePerformIO $
- do ctx <- digestStrictly md input
- digestFinalBS ctx
+ = unsafePerformIO
+ (digestStrictly md input >>= digestFinalBS)
-- |@'digestLBS'@ digests a stream of data.
digestLBS :: Digest -> L8.ByteString -> String
digestLBS md input
- = unsafePerformIO $
- do ctx <- digestLazily md input
- digestFinal ctx
+ = unsafePerformIO
+ (digestLazily md input >>= digestFinal)
{- HMAC ---------------------------------------------------------------------- -}
View
@@ -183,8 +183,8 @@ foreign import ccall unsafe "&EVP_PKEY_free"
wrapPKeyPtr :: Ptr EVP_PKEY -> IO VaguePKey
-wrapPKeyPtr ptr
- = newForeignPtr _pkey_free ptr >>= return . VaguePKey
+wrapPKeyPtr
+ = fmap VaguePKey . newForeignPtr _pkey_free
withPKeyPtr' :: PKey k => k -> (Ptr EVP_PKEY -> IO a) -> IO a
@@ -226,9 +226,7 @@ rsaFromPKey pk
do pkeyType <- (#peek EVP_PKEY, type) pkeyPtr :: IO CInt
case pkeyType of
(#const EVP_PKEY_RSA)
- -> do rsaPtr <- _get1_RSA pkeyPtr
- rsaM <- absorbRSAPtr rsaPtr
- return rsaM
+ -> _get1_RSA pkeyPtr >>= absorbRSAPtr
_ -> return Nothing
instance PublicKey RSAPubKey
@@ -268,9 +266,7 @@ dsaFromPKey pk
do pkeyType <- (#peek EVP_PKEY, type) pkeyPtr :: IO CInt
case pkeyType of
(#const EVP_PKEY_DSA)
- -> do dsaPtr <- _get1_DSA pkeyPtr
- dsaM <- absorbDSAPtr dsaPtr
- return dsaM
+ -> _get1_DSA pkeyPtr >>= absorbDSAPtr
_ -> return Nothing
instance PublicKey DSAPubKey
View
@@ -52,8 +52,7 @@ iterateObjNames nameType wantSorted cb
objNameStr :: ObjName -> IO String
objNameStr name
- = do strPtr <- (#peek OBJ_NAME, name) name
- peekCString strPtr
+ = (#peek OBJ_NAME, name) name >>= peekCString
getObjNames :: ObjNameType -> Bool -> IO [String]
Oops, something went wrong.

0 comments on commit be44eaa

Please sign in to comment.