zlib-bindings 0.0.2 → 0.0.3
raw patch · 5 files changed
+68/−9 lines, 5 files
Files
- Codec/Zlib.hs +25/−2
- Codec/Zlib/Lowlevel.hs +4/−0
- c/helper.c +5/−0
- test/main.hs +32/−1
- zlib-bindings.cabal +2/−6
Codec/Zlib.hs view
@@ -25,14 +25,16 @@ , initInflateWithDictionary , withInflateInput , finishInflate+ , flushInflate -- * Deflate , Deflate , initDeflate , initDeflateWithDictionary , withDeflateInput , finishDeflate+ , flushDeflate -- * Data types- , WindowBits (WindowBits)+ , WindowBits (..) , defaultWindowBits , ZlibException (..) ) where@@ -214,8 +216,20 @@ withForeignPtr fbuff $ \buff -> do avail <- c_get_avail_out zstr let size = defaultChunkSize - fromIntegral avail- S.packCStringLen (buff, size)+ bs <- S.packCStringLen (buff, size)+ c_set_avail_out zstr buff+ $ fromIntegral defaultChunkSize+ return bs +-- | Flush the inflation buffer. Useful for interactive application.+--+-- This is actually a synonym for 'finishInflate'. It is provided for its more+-- semantic name.+--+-- Since 0.0.3+flushInflate :: Inflate -> IO S.ByteString+flushInflate = finishInflate+ -- | Feed the given 'S.ByteString' to the deflater. This function takes a -- function argument which takes a \"popper\". A popper is an IO action that -- will return the next bit of deflated data, returning 'Nothing' when there is@@ -244,3 +258,12 @@ withForeignPtr fzstr $ \zstr -> f $ drain fbuff zstr c_call_deflate_finish True +-- | Flush the deflation buffer. Useful for interactive application.+--+-- Internally this passes Z_SYNC_FLUSH to the zlib library.+--+-- Since 0.0.3+flushDeflate :: Deflate -> (IO (Maybe S.ByteString) -> IO a) -> IO a+flushDeflate (Deflate (fzstr, fbuff)) f =+ withForeignPtr fzstr $ \zstr ->+ f $ drain fbuff zstr c_call_deflate_flush True
Codec/Zlib/Lowlevel.hs view
@@ -16,6 +16,7 @@ , c_call_inflate_noflush , c_call_deflate_noflush , c_call_deflate_finish+ , c_call_deflate_flush , c_call_deflate_set_dictionary , c_call_inflate_set_dictionary ) where@@ -80,6 +81,9 @@ foreign import ccall unsafe "call_deflate_finish" c_call_deflate_finish :: ZStream' -> IO CInt++foreign import ccall unsafe "call_deflate_flush"+ c_call_deflate_flush :: ZStream' -> IO CInt foreign import ccall unsafe "deflate_set_dictionary" c_call_deflate_set_dictionary :: ZStream' -> Ptr CChar -> CUInt -> IO ()
c/helper.c view
@@ -81,6 +81,11 @@ return deflate(stream, Z_NO_FLUSH); } +int call_deflate_flush (z_stream *stream)+{+ return deflate(stream, Z_SYNC_FLUSH);+}+ int call_deflate_finish (z_stream *stream) { return deflate(stream, Z_FINISH);
test/main.hs view
@@ -13,7 +13,7 @@ import qualified Data.ByteString as S import qualified Data.ByteString.Char8 as S8 import qualified Data.ByteString.Lazy as L-import Control.Monad (foldM)+import Control.Monad (foldM, forM_, forM) import System.IO.Unsafe (unsafePerformIO) decompress' :: L.ByteString -> L.ByteString@@ -174,3 +174,34 @@ deflated <- foldM (go' def) id $ L.toChunks lbs deflated' <- finishDeflate def $ go deflated return $ lbs == decompress (L.fromChunks (deflated' []))++ describe "flushing" $ do+ let helper wb = do+ let bss0 = replicate 5000 "abc"+ def <- initDeflate 9 wb+ inf <- initInflate wb++ let popList pop = do+ mx <- pop+ case mx of+ Nothing -> return []+ Just x -> do+ xs <- popList pop+ return $ x : xs++ let callback name expected pop = do+ bssDeflated <- popList pop+ bsInflated <- fmap (S.concat . concat) $ forM bssDeflated $ \bs -> do+ x <- withInflateInput inf bs popList+ y <- flushInflate inf+ return $ x ++ [y]+ if bsInflated == expected+ then return ()+ else error $ "callback " ++ name ++ ", got: " ++ show bsInflated ++ ", expected: " ++ show expected++ forM_ (zip [1..] bss0) $ \(i, bs) -> do+ withDeflateInput def bs $ callback ("loop" ++ show (i :: Int)) ""+ flushDeflate def $ callback ("loop" ++ show (i :: Int)) bs+ finishDeflate def $ callback "finish" ""+ it "zlib" $ helper defaultWindowBits+ it "gzip" $ helper $ WindowBits 31
zlib-bindings.cabal view
@@ -1,10 +1,11 @@ name: zlib-bindings-version: 0.0.2+version: 0.0.3 license: BSD3 license-file: LICENSE author: Michael Snoyman <michael@snoyman.com> maintainer: Michael Snoyman <michael@snoyman.com> synopsis: Low-level bindings to the zlib package.+description: Provides necessary functions for producing a streaming interface. This is used for example by @zlib-conduit@ and @zlib-enum@. category: Codec stability: Experimental cabal-version: >= 1.8@@ -13,13 +14,8 @@ extra-source-files: cbits/crc32.h cbits/inffast.h cbits/inflate.h cbits/trees.h cbits/deflate.h cbits/inffixed.h cbits/inftrees.h cbits/zutil.h, test/main.hs-flag test- description: Build the test executable.- default: False library- if flag(test)- Buildable: False build-depends: base >= 4 && < 5 , bytestring >= 0.9.1.4 && < 0.10 , zlib >= 0.5.2.0 && < 0.6