zlib-bindings 0.1.1.3 → 0.1.1.4
raw patch · 3 files changed
+58/−18 lines, 3 files
Files
- Codec/Zlib.hs +13/−17
- test/main.hs +44/−0
- zlib-bindings.cabal +1/−1
Codec/Zlib.hs view
@@ -202,23 +202,19 @@ -> Bool -> Popper drain fbuff fzstr mbs func isFinish = withForeignPtr fzstr $ \zstr -> keepAlive mbs $ do- a <- c_get_avail_in zstr- if a == 0 && not isFinish- then return Nothing- else withForeignPtr fbuff $ \buff -> do- res <- func zstr- when (res < 0 && res /= zBufError)- $ throwIO $ ZlibException $ fromIntegral res- avail <- c_get_avail_out zstr- let size = defaultChunkSize - fromIntegral avail- let toOutput = avail == 0 || (isFinish && size /= 0)- if toOutput- then do- bs <- S.packCStringLen (buff, size)- c_set_avail_out zstr buff- $ fromIntegral defaultChunkSize- return $ Just bs- else return Nothing+ res <- func zstr+ when (res < 0 && res /= zBufError)+ $ throwIO $ ZlibException $ fromIntegral res+ avail <- c_get_avail_out zstr+ let size = defaultChunkSize - fromIntegral avail+ toOutput = avail == 0 || (isFinish && size /= 0)+ if toOutput+ then withForeignPtr fbuff $ \buff -> do+ bs <- S.packCStringLen (buff, size)+ c_set_avail_out zstr buff+ $ fromIntegral defaultChunkSize+ return $ Just bs+ else return Nothing -- | As explained in 'feedInflate', inflation buffers your decompressed
test/main.hs view
@@ -11,8 +11,10 @@ import qualified Data.ByteString as S import qualified Data.ByteString.Char8 as S8 import qualified Data.ByteString.Lazy as L+import qualified Data.ByteString.Lazy.Internal as LI import Control.Monad (foldM, forM_, forM) import System.IO.Unsafe (unsafePerformIO)+import qualified Codec.Compression.Zlib.Raw as Raw decompress' :: L.ByteString -> L.ByteString decompress' gziped = unsafePerformIO $ do@@ -204,3 +206,45 @@ callback "finish" "" $ finishDeflate def it "zlib" $ helper defaultWindowBits it "gzip" $ helper $ WindowBits 31+ describe "large raw #9" $ do+ let size = fromIntegral $ LI.defaultChunkSize * 4 + 1+ input = L.replicate size 10+ it "compressing" $ do+ output <- fmap Raw.decompress $ compressRaw input+ L.all (== 10) output `shouldBe` True+ L.length output `shouldBe` L.length input+ it "decompressing" $ do+ output <- decompressRaw $ Raw.compress input+ L.all (== 10) output `shouldBe` True+ L.length output `shouldBe` L.length input++rawWindowBits :: WindowBits+rawWindowBits = WindowBits (-15)++decompressRaw :: L.ByteString -> IO L.ByteString+decompressRaw gziped = do+ inf <- initInflate rawWindowBits+ ungziped <- foldM (go' inf) id $ L.toChunks gziped+ final <- finishInflate inf+ return $ L.fromChunks $ ungziped [final]+ where+ go' inf front bs = feedInflate inf bs >>= go front+ go front x = do+ y <- x+ case y of+ Nothing -> return front+ Just z -> go (front . (:) z) x++compressRaw :: L.ByteString -> IO L.ByteString+compressRaw raw = do+ def <- initDeflate 1 rawWindowBits+ gziped <- foldM (go' def) id $ L.toChunks raw+ gziped' <- go gziped $ finishDeflate def+ return $ L.fromChunks $ gziped' []+ where+ go' def front bs = feedDeflate def bs >>= go front+ go front x = do+ y <- x+ case y of+ Nothing -> return front+ Just z -> go (front . (:) z) x
zlib-bindings.cabal view
@@ -1,5 +1,5 @@ name: zlib-bindings-version: 0.1.1.3+version: 0.1.1.4 license: BSD3 license-file: LICENSE author: Michael Snoyman <michael@snoyman.com>