I am trying to bind to a C function from an external C library which returns a pointer to a memory buffer and the length of the buffer :
extern int myfunc(int arg1, unsigned char **buffer, size_t *buffer_len);
I tried the following code but got a segmentation fault as a result.
{-# LANGUAGE ForeignFunctionInterface #-}
import Foreign
import Foreign.C.Types (CInt(..), CUChar(..), CSize(..))
import Foreign.Ptr (Ptr)
import Foreign.Marshal.Array (peekArray)
foreign import ccall unsafe "myfunc" c_myfunc :: CInt -> Ptr (Ptr CUChar) -> Ptr CSize -> IO (CInt)
getBuffer :: Int -> IO [CUChar]
getBuffer arg1 = do
alloca $ \buffer -> do
alloca $ \buflen -> do
res <- c_myfunc (CInt arg1) buffer buflen
buflen' <- fromIntegral <$> peek buflen
cs <- peekArray buflen' buffer -- return a [Ptr CUChar] of size buflen
cs' <- mapM peek cs -- return a [CUChar] of size buflen
return cs'
I assume that peekArray did allocate enough memory, so I am not sure where it did go wrong. Any help welcome.