Debian Codegraph @

src/Codec/Compression/BZip/Unpack.chs

raw
1 {-# LANGUAGE TupleSections #-}
2
3 module Codec.Compression.BZip.Unpack ( decompress
4 , decompressErr
5 ) where
6
7 import Codec.Compression.BZip.Foreign.Common
8 import Codec.Compression.BZip.Foreign.Decompress
9 import Codec.Compression.BZip.Common
10 import Control.Applicative
11 import Control.Exception (evaluate, throw, try)
12 import Control.Monad.ST.Lazy as LazyST
13 import Control.Monad.ST.Lazy.Unsafe as LazyST
14 import qualified Data.ByteString as BS
15 import qualified Data.ByteString.Unsafe as BS
16 import qualified Data.ByteString.Lazy as BSL
17 import Data.Maybe (isNothing)
18 import Foreign.ForeignPtr (newForeignPtr, castForeignPtr, ForeignPtr, mallocForeignPtrBytes, withForeignPtr)
19 import Foreign.Ptr (Ptr, castPtr, nullPtr)
20 import System.IO.Unsafe (unsafePerformIO)
21
22 #include <bzlib.h>
23
24 -- | Return an error rather than throwing an exception.
25 --
26 -- @since 1.0.1.0
27 decompressErr :: BSL.ByteString -> Either BZError BSL.ByteString
28 decompressErr = unsafePerformIO . try . evaluate . decompress
29
30 -- | Don't use this on pathological input; it may not be secure
31 --
32 -- This does not handle nested streams
33 --
34 -- @since 0.1.1.0
35 decompress :: BSL.ByteString -> BSL.ByteString
36 decompress bsl =
37 let bss = BSL.toChunks bsl in
38 BSL.fromChunks $ LazyST.runST $ do
39 (p, bufOut) <- LazyST.unsafeIOToST $ do
40 ptr <- bzStreamInit
41 p <- castForeignPtr <$> newForeignPtr bZ2BzDecompressEnd (castPtr ptr)
42 bzDecompressInit p
43 bufOut <- mallocForeignPtrBytes bufSz
44 pure (p, bufOut)
45
46 bzDecompressChunks p bss bufOut
47
48 type Step = Ptr BzStream -> Maybe BS.ByteString -> [BS.ByteString] -> IO BZError -> IO (BZError, Maybe BS.ByteString, [BS.ByteString])
49
50 bzDecompressChunks :: ForeignPtr BzStream -> [BS.ByteString] -> ForeignPtr a -> LazyST.ST s [BS.ByteString]
51 bzDecompressChunks ptr' bs bufO =
52
53 fillBuf ptr' Nothing bs pushBytes bufO
54
55 where
56
57 fillBuf :: ForeignPtr BzStream -> Maybe BS.ByteString -> [BS.ByteString] -> Step -> ForeignPtr a -> LazyST.ST s [BS.ByteString]
58 fillBuf pForeign passFwd bs' step bufOutForeign = do
59 (ret, szOut, newBSAp, bs'', keepAlive) <- LazyST.unsafeIOToST $ do
60 withForeignPtr pForeign $ \p ->
61 withForeignPtr bufOutForeign $ \bufOut -> do
62
63 let act = do
64
65 {# set bz_stream.avail_out #} p bufSz
66 {# set bz_stream.next_out #} p (castPtr bufOut)
67
68 bZ2BzDecompress ptr'
69
70 (ret, keepAlive, bs'') <- step p passFwd bs' act
71
72 szOut <- fromIntegral <$> {# get bz_stream->avail_out #} p
73
74 let bytesAvail = bufSz - szOut
75
76 newBSAp <- if bytesAvail /= 0
77 then (:) <$> BS.packCStringLen (castPtr bufOut, bytesAvail)
78 else pure id
79
80 pure (ret, szOut, newBSAp, bs'', keepAlive)
81
82 let step' = if szOut == 0
83 then keepBytesAlive
84 else pushBytes
85
86 if ret == BzStreamEnd
87 then pure (newBSAp [])
88 else if null bs' && isNothing keepAlive && bufSz == szOut
89 then throw BzUnexpectedEof
90 else newBSAp <$> fillBuf pForeign keepAlive bs'' step' bufOutForeign
91
92 keepBytesAlive :: Ptr BzStream -> Maybe BS.ByteString -> [BS.ByteString] -> IO BZError -> IO (BZError, Maybe BS.ByteString, [BS.ByteString])
93 keepBytesAlive _ Nothing bs' act = (, Nothing, bs') <$> act
94 keepBytesAlive _ passFwd@(Just b) bs' act = do
95 BS.unsafeUseAsCStringLen b $ \_ ->
96
97 (, passFwd, bs') <$> act
98
99 pushBytes :: Ptr BzStream -> Maybe BS.ByteString -> [BS.ByteString] -> IO BZError -> IO (BZError, Maybe BS.ByteString, [BS.ByteString])
100 pushBytes _ _ [] act = (, Nothing, []) <$> act
101 pushBytes p _ (b:bs') act =
102 BS.unsafeUseAsCStringLen b $ \(buf, sz) -> do
103
104 {# set bz_stream.avail_in #} p (fromIntegral sz)
105 {# set bz_stream.next_in #} p buf
106
107 (, Just b, bs') <$> act
108
109 bufSz :: Integral a => a
110 bufSz = 64 * 1024
111
112 bzDecompressInit :: ForeignPtr BzStream -> IO ()
113 bzDecompressInit ptr' = do
114
115 withForeignPtr ptr' $ \p -> do
116
117 {# set bz_stream.next_in #} p nullPtr
118 {# set bz_stream.avail_in #} p 0
119
120 bZ2BzDecompressInit ptr' 0 False