Skip to content

Instantly share code, notes, and snippets.

@banacorn
Created December 3, 2012 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banacorn/4197021 to your computer and use it in GitHub Desktop.
Save banacorn/4197021 to your computer and use it in GitHub Desktop.
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