Created
January 18, 2023 13:58
-
-
Save exarkun/dd5dc9303ad8bc708369b4594baab741 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Data.Int ( Int64 ) | |
import Foreign.Ptr ( Ptr, castPtr ) | |
import Foreign.C.String ( CString ) | |
import Foreign.Storable ( peek, peekElemOff ) | |
import Foreign.Marshal.Alloc ( allocaBytes ) | |
import Foreign.Marshal.Array ( advancePtr ) | |
import Foreign.Storable ( sizeOf ) | |
import Data.Word ( Word8 ) | |
import Data.ByteString ( ByteString, useAsCStringLen, packCStringLen ) | |
import Control.Monad ( zipWithM ) | |
foreign import ccall unsafe "botan_zfec_encode" zfec_encode :: Word -> Word -> Ptr Word8 -> Word -> Ptr (Ptr (Ptr Word8)) -> Ptr (Ptr Word) -> IO Int | |
main :: IO () | |
main = do | |
let | |
input = "These are not word8s" | |
k = 1 | |
n = 3 | |
allocaBytes (n * sizeOf (undefined :: Ptr (Ptr (Ptr Word8)))) $ \outputBlocksPtr -> do | |
allocaBytes (n * sizeOf (undefined :: Ptr (Ptr Word))) $ \outputSizesPtr -> do | |
useAsCStringLen input $ \(inputPtr, inputLen) -> do | |
outputBlocks :: Ptr (Ptr Word8) <- peek outputBlocksPtr | |
outputSizes :: Ptr Word <- peek outputSizesPtr | |
print ("Output sizes before", outputSizes) | |
result <- zfec_encode k (fromIntegral n) (castPtr inputPtr) (fromIntegral inputLen) outputBlocksPtr outputSizesPtr | |
print result | |
print ("Output sizes after", outputSizes) | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment