{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE CPP #-}
module Distribution.Client.GZipUtils (
maybeDecompress,
) where
import Prelude ()
import Distribution.Client.Compat.Prelude
import Codec.Compression.Zlib.Internal
import Data.ByteString.Lazy.Internal as BS (ByteString(Empty, Chunk))
#ifndef MIN_VERSION_zlib
#define MIN_VERSION_zlib(x,y,z) 1
#endif
#if MIN_VERSION_zlib(0,6,0)
import Control.Exception (throw)
import Control.Monad.ST.Lazy (ST, runST)
import qualified Data.ByteString as Strict
#endif
maybeDecompress :: ByteString -> ByteString
#if MIN_VERSION_zlib(0,6,0)
maybeDecompress :: ByteString -> ByteString
maybeDecompress ByteString
bytes = (forall s. ST s ByteString) -> ByteString
forall a. (forall s. ST s a) -> a
runST (ByteString -> DecompressStream (ST s) -> ST s ByteString
forall (m :: * -> *).
Monad m =>
ByteString -> DecompressStream m -> m ByteString
go ByteString
bytes DecompressStream (ST s)
forall s. DecompressStream (ST s)
decompressor)
where
decompressor :: DecompressStream (ST s)
decompressor :: DecompressStream (ST s)
decompressor = Format -> DecompressParams -> DecompressStream (ST s)
forall s. Format -> DecompressParams -> DecompressStream (ST s)
decompressST Format
gzipOrZlibFormat DecompressParams
defaultDecompressParams
go :: Monad m => ByteString -> DecompressStream m -> m ByteString
go :: ByteString -> DecompressStream m -> m ByteString
go ByteString
cs (DecompressOutputAvailable ByteString
bs m (DecompressStream m)
k) = (ByteString -> ByteString) -> m ByteString -> m ByteString
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (ByteString -> ByteString -> ByteString
Chunk ByteString
bs) (m ByteString -> m ByteString) -> m ByteString -> m ByteString
forall a b. (a -> b) -> a -> b
$ ByteString -> DecompressStream m -> m ByteString
forall (m :: * -> *).
Monad m =>
ByteString -> DecompressStream m -> m ByteString
go' ByteString
cs (DecompressStream m -> m ByteString)
-> m (DecompressStream m) -> m ByteString
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m (DecompressStream m)
k
go ByteString
_ (DecompressStreamEnd ByteString
_bs ) = ByteString -> m ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
Empty
go ByteString
_ (DecompressStreamError DecompressError
_err ) = ByteString -> m ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
bytes
go ByteString
cs (DecompressInputRequired ByteString -> m (DecompressStream m)
k) = ByteString -> DecompressStream m -> m ByteString
forall (m :: * -> *).
Monad m =>
ByteString -> DecompressStream m -> m ByteString
go ByteString
cs' (DecompressStream m -> m ByteString)
-> m (DecompressStream m) -> m ByteString
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ByteString -> m (DecompressStream m)
k ByteString
c
where
(ByteString
c, ByteString
cs') = ByteString -> (ByteString, ByteString)
uncons ByteString
cs
go' :: Monad m => ByteString -> DecompressStream m -> m ByteString
go' :: ByteString -> DecompressStream m -> m ByteString
go' ByteString
cs (DecompressOutputAvailable ByteString
bs m (DecompressStream m)
k) = (ByteString -> ByteString) -> m ByteString -> m ByteString
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (ByteString -> ByteString -> ByteString
Chunk ByteString
bs) (m ByteString -> m ByteString) -> m ByteString -> m ByteString
forall a b. (a -> b) -> a -> b
$ ByteString -> DecompressStream m -> m ByteString
forall (m :: * -> *).
Monad m =>
ByteString -> DecompressStream m -> m ByteString
go' ByteString
cs (DecompressStream m -> m ByteString)
-> m (DecompressStream m) -> m ByteString
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m (DecompressStream m)
k
go' ByteString
_ (DecompressStreamEnd ByteString
_bs ) = ByteString -> m ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
Empty
go' ByteString
_ (DecompressStreamError DecompressError
err ) = DecompressError -> m ByteString
forall a e. Exception e => e -> a
throw DecompressError
err
go' ByteString
cs (DecompressInputRequired ByteString -> m (DecompressStream m)
k) = ByteString -> DecompressStream m -> m ByteString
forall (m :: * -> *).
Monad m =>
ByteString -> DecompressStream m -> m ByteString
go' ByteString
cs' (DecompressStream m -> m ByteString)
-> m (DecompressStream m) -> m ByteString
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ByteString -> m (DecompressStream m)
k ByteString
c
where
(ByteString
c, ByteString
cs') = ByteString -> (ByteString, ByteString)
uncons ByteString
cs
uncons :: ByteString -> (Strict.ByteString, ByteString)
uncons :: ByteString -> (ByteString, ByteString)
uncons ByteString
Empty = (ByteString
Strict.empty, ByteString
Empty)
uncons (Chunk ByteString
c ByteString
cs) = (ByteString
c, ByteString
cs)
#else
maybeDecompress bytes = foldStream $ decompressWithErrors gzipOrZlibFormat defaultDecompressParams bytes
where
foldStream (StreamError _ _) = bytes
foldStream somethingElse = doFold somethingElse
doFold StreamEnd = BS.Empty
doFold (StreamChunk bs stream) = BS.Chunk bs (doFold stream)
doFold (StreamError _ msg) = error $ "Codec.Compression.Zlib: " ++ msg
#endif