{-# LINE 1 "OpenSSL/Objects.hsc" #-}
{-# LANGUAGE EmptyDataDecls           #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CApiFFI                  #-}
module OpenSSL.Objects
    ( ObjNameType(..)
    , getObjNames
    )
    where

import           Data.IORef
import           Foreign
import           Foreign.C


type ObjName  = Ptr OBJ_NAME
data {-# CTYPE "openssl/objects.h" "OBJ_NAME" #-} OBJ_NAME

type DoAllCallback = ObjName -> Ptr () -> IO ()


foreign import capi safe "openssl/objects.h OBJ_NAME_do_all"
        _NAME_do_all :: CInt -> FunPtr DoAllCallback -> Ptr () -> IO ()

foreign import capi safe "openssl/objects.h OBJ_NAME_do_all_sorted"
        _NAME_do_all_sorted :: CInt -> FunPtr DoAllCallback -> Ptr () -> IO ()

foreign import ccall "wrapper"
        mkDoAllCallback :: DoAllCallback -> IO (FunPtr DoAllCallback)


data ObjNameType = MDMethodType
                 | CipherMethodType
                 | PKeyMethodType
                 | CompMethodType

objNameTypeToInt :: ObjNameType -> CInt
objNameTypeToInt :: ObjNameType -> CInt
objNameTypeToInt ObjNameType
MDMethodType     = CInt
1
{-# LINE 38 "OpenSSL/Objects.hsc" #-}
objNameTypeToInt CipherMethodType = 2
{-# LINE 39 "OpenSSL/Objects.hsc" #-}
objNameTypeToInt PKeyMethodType   = 3
{-# LINE 40 "OpenSSL/Objects.hsc" #-}
objNameTypeToInt CompMethodType   = 4
{-# LINE 41 "OpenSSL/Objects.hsc" #-}


iterateObjNames :: ObjNameType -> Bool -> (ObjName -> IO ()) -> IO ()
iterateObjNames :: ObjNameType -> Bool -> (ObjName -> IO ()) -> IO ()
iterateObjNames ObjNameType
nameType Bool
wantSorted ObjName -> IO ()
cb
    = do FunPtr DoAllCallback
cbPtr <- DoAllCallback -> IO (FunPtr DoAllCallback)
mkDoAllCallback (DoAllCallback -> IO (FunPtr DoAllCallback))
-> DoAllCallback -> IO (FunPtr DoAllCallback)
forall a b. (a -> b) -> a -> b
$ \ ObjName
name Ptr ()
_ -> ObjName -> IO ()
cb ObjName
name
         let action :: CInt -> FunPtr DoAllCallback -> Ptr () -> IO ()
action = if Bool
wantSorted then
                          CInt -> FunPtr DoAllCallback -> Ptr () -> IO ()
_NAME_do_all_sorted
                      else
                          CInt -> FunPtr DoAllCallback -> Ptr () -> IO ()
_NAME_do_all
         CInt -> FunPtr DoAllCallback -> Ptr () -> IO ()
action (ObjNameType -> CInt
objNameTypeToInt ObjNameType
nameType) FunPtr DoAllCallback
cbPtr Ptr ()
forall a. Ptr a
nullPtr
         FunPtr DoAllCallback -> IO ()
forall a. FunPtr a -> IO ()
freeHaskellFunPtr FunPtr DoAllCallback
cbPtr


objNameStr :: ObjName -> IO String
objNameStr :: ObjName -> IO String
objNameStr ObjName
name
    = ((\ObjName
hsc_ptr -> ObjName -> Int -> IO CString
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff ObjName
hsc_ptr Int
8)) ObjName
name IO CString -> (CString -> IO String) -> IO String
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CString -> IO String
peekCString
{-# LINE 57 "OpenSSL/Objects.hsc" #-}


getObjNames :: ObjNameType -> Bool -> IO [String]
getObjNames :: ObjNameType -> Bool -> IO [String]
getObjNames ObjNameType
nameType Bool
wantSorted
    = do IORef [String]
listRef <- [String] -> IO (IORef [String])
forall a. a -> IO (IORef a)
newIORef []
         ObjNameType -> Bool -> (ObjName -> IO ()) -> IO ()
iterateObjNames ObjNameType
nameType Bool
wantSorted ((ObjName -> IO ()) -> IO ()) -> (ObjName -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ ObjName
name ->
             do String
nameStr <- ObjName -> IO String
objNameStr ObjName
name
                IORef [String] -> ([String] -> [String]) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef IORef [String]
listRef ([String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [String
nameStr])
         IORef [String] -> IO [String]
forall a. IORef a -> IO a
readIORef IORef [String]
listRef