Ticket #2194: Stub.hs

File Stub.hs, 894 bytes (added by alatter, 6 years ago)
Line 
1{-# LANGUAGE ForeignFunctionInterface#-}
2
3module Stub where
4
5import Foreign
6import Foreign.C
7import Foreign.Ptr
8import Foreign.Marshal.Alloc
9
10import Data.Word
11
12type NESSIEStruct = Ptr ()
13
14foreign import ccall unsafe "NESSIEinit"
15        initNessie :: NESSIEStruct -> IO ()
16
17foreign import ccall unsafe "NESSIEadd"
18        addNessie :: CString -> CULong -> NESSIEStruct -> IO ()
19
20foreign import ccall unsafe "NESSIEfinalize"
21        finalizeNessie :: NESSIEStruct -> CString -> IO ()
22
23nsize = 1000;
24digest_bytes = 256;
25
26digest :: [Word8] -> [Word8]
27
28digest ws = unsafePerformIO $
29    allocaBytes (fromIntegral nsize) $ \pNessie ->
30    allocaBytes (fromIntegral digest_bytes) $ \pOut ->
31    withArray ws $ \pData -> do
32    initNessie pNessie
33    addNessie (castPtr pData) (fromIntegral $ length ws) pNessie
34    finalizeNessie pNessie (castPtr pOut)
35    peekArray (fromIntegral digest_bytes) pOut