Skip to content

Instantly share code, notes, and snippets.

@banacorn

banacorn/banana.hs

Created Dec 3, 2012
Embed
What would you like to do?
adapter :: (Ptr CUChar -> CUInt -> Ptr CUChar -> Ptr CUInt -> IO CInt) -> Int -> String -> String
adapter function chunkSize content = map cuchar2char $ unsafePerformIO $ do
-- allocate buffers
inputBuffer <- mallocArray chunkSize
outputBuffer <- mallocArray chunkSize
outputSize <- mallocBytes 32
-- put it in
pokeArray inputBuffer (map char2cuchar content)
poke outputSize (fromIntegral chunkSize)
-- compress
function inputBuffer (fromIntegral chunkSize) outputBuffer outputSize
-- take it out
output <- peekArray chunkSize outputBuffer
-- free buffers
free inputBuffer
free outputBuffer
free outputSize
return output
compress :: Int -> String -> String
compress = adapter c_compress
decompress :: Int -> String -> String
decompress = adapter c_decompress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment