| 1 |
{-# LANGUAGE TupleSections #-} |
| 2 |
|
| 3 |
module Codec.Compression.BZip.Pack ( compress |
| 4 |
, compressWith |
| 5 |
) where |
| 6 |
|
| 7 |
import Codec.Compression.BZip.Foreign.Common |
| 8 |
import Codec.Compression.BZip.Foreign.Compress |
| 9 |
import Codec.Compression.BZip.Common |
| 10 |
import Control.Applicative |
| 11 |
import Control.Monad.ST.Lazy as LazyST |
| 12 |
import Control.Monad.ST.Lazy.Unsafe as LazyST |
| 13 |
import qualified Data.ByteString as BS |
| 14 |
import qualified Data.ByteString.Lazy as BSL |
| 15 |
import qualified Data.ByteString.Unsafe as BS |
| 16 |
import Foreign.C.Types (CInt) |
| 17 |
import Foreign.ForeignPtr (castForeignPtr, ForeignPtr, newForeignPtr, mallocForeignPtrBytes, withForeignPtr) |
| 18 |
import Foreign.Ptr (Ptr, castPtr, nullPtr) |
| 19 |
|
| 20 |
#include <bzlib.h> |
| 21 |
|
| 22 |
-- | @since 0.1.1.0 |
| 23 |
compress :: BSL.ByteString -> BSL.ByteString |
| 24 |
compress = compressWith 9 30 |
| 25 |
|
| 26 |
type Step = Ptr BzStream -> Maybe BS.ByteString -> [BS.ByteString] -> (BZAction -> IO BZError) -> IO (BZError, Maybe BS.ByteString, [BS.ByteString]) |
| 27 |
|
| 28 |
-- | See [bzlib manual](https://www.sourceware.org/bzip2/manual/manual.html#bzcompress-init) |
| 29 |
-- for information on compression parameters. |
| 30 |
-- |
| 31 |
-- @since 0.1.1.0 |
| 32 |
compressWith :: CInt -- ^ Block size (@1-9@) |
| 33 |
-> CInt -- ^ Work factor (@0-250@) |
| 34 |
-> BSL.ByteString |
| 35 |
-> BSL.ByteString |
| 36 |
compressWith blkSize wf 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 bZ2BzCompressEnd (castPtr ptr) |
| 42 |
bzCompressInit blkSize wf p |
| 43 |
bufOut <- mallocForeignPtrBytes bufSz |
| 44 |
pure (p, bufOut) |
| 45 |
|
| 46 |
bzCompressChunks p bss bufOut |
| 47 |
|
| 48 |
bzCompressChunks :: ForeignPtr BzStream -> [BS.ByteString] -> ForeignPtr a -> LazyST.ST s [BS.ByteString] |
| 49 |
bzCompressChunks ptr' bs bufO = do |
| 50 |
|
| 51 |
fillBuf ptr' Nothing bs pushBytes bufO |
| 52 |
|
| 53 |
where |
| 54 |
|
| 55 |
-- corresponds to inner loop in zlib example |
| 56 |
fillBuf :: ForeignPtr BzStream -> Maybe BS.ByteString -> [BS.ByteString] -> Step -> ForeignPtr a -> LazyST.ST s [BS.ByteString] |
| 57 |
fillBuf pForeign passFwd bs' step bufOutForeign = do |
| 58 |
(ret, szOut, newBSAp, bs'', keepAlive) <- LazyST.unsafeIOToST $ do |
| 59 |
withForeignPtr pForeign $ \p -> |
| 60 |
withForeignPtr bufOutForeign $ \bufOut -> do |
| 61 |
|
| 62 |
let act f = do |
| 63 |
|
| 64 |
{# set bz_stream.avail_out #} p bufSz |
| 65 |
{# set bz_stream.next_out #} p (castPtr bufOut) |
| 66 |
|
| 67 |
bZ2BzCompress ptr' f |
| 68 |
|
| 69 |
(ret, keepAlive, bs'') <- step p passFwd bs' act |
| 70 |
|
| 71 |
szOut <- fromIntegral <$> {# get bz_stream->avail_out #} p |
| 72 |
|
| 73 |
let bytesAvail = bufSz - szOut |
| 74 |
|
| 75 |
newBSAp <- if bytesAvail /= 0 |
| 76 |
then (:) <$> BS.packCStringLen (castPtr bufOut, bytesAvail) |
| 77 |
else pure id |
| 78 |
|
| 79 |
pure (ret, szOut, newBSAp, bs'', keepAlive) |
| 80 |
|
| 81 |
let step' = if szOut == 0 |
| 82 |
then keepBytesAlive |
| 83 |
else pushBytes |
| 84 |
|
| 85 |
if ret == BzStreamEnd |
| 86 |
then pure (newBSAp []) |
| 87 |
else newBSAp <$> fillBuf pForeign keepAlive bs'' step' bufOutForeign |
| 88 |
|
| 89 |
keepBytesAlive :: Ptr BzStream -> Maybe BS.ByteString -> [BS.ByteString] -> (BZAction -> IO BZError) -> IO (BZError, Maybe BS.ByteString, [BS.ByteString]) |
| 90 |
keepBytesAlive _ Nothing [] act = (, Nothing, []) <$> act BzFinish |
| 91 |
keepBytesAlive _ Nothing bs' act = (, Nothing, bs') <$> act BzRun |
| 92 |
keepBytesAlive _ passFwd@(Just b) [] act = |
| 93 |
BS.unsafeUseAsCStringLen b $ \_ -> |
| 94 |
|
| 95 |
(, passFwd, []) <$> act BzFinish |
| 96 |
keepBytesAlive _ passFwd@(Just b) bs' act = |
| 97 |
BS.unsafeUseAsCStringLen b $ \_ -> |
| 98 |
|
| 99 |
(, passFwd, bs') <$> act BzRun |
| 100 |
|
| 101 |
pushBytes :: Ptr BzStream -> Maybe BS.ByteString -> [BS.ByteString] -> (BZAction -> IO BZError) -> IO (BZError, Maybe BS.ByteString, [BS.ByteString]) |
| 102 |
pushBytes _ _ [] act = (, Nothing, []) <$> act BzFinish |
| 103 |
pushBytes p _ (b:bs') act = |
| 104 |
BS.unsafeUseAsCStringLen b $ \(buf, sz) -> do |
| 105 |
|
| 106 |
{# set bz_stream.avail_in #} p (fromIntegral sz) |
| 107 |
{# set bz_stream.next_in #} p buf |
| 108 |
|
| 109 |
(, Just b, bs') <$> act BzRun |
| 110 |
|
| 111 |
bufSz :: Integral a => a |
| 112 |
bufSz = 16 * 1024 |
| 113 |
|
| 114 |
bzCompressInit :: CInt -> CInt -> ForeignPtr BzStream -> IO () |
| 115 |
bzCompressInit blkSize wf ptr' = do |
| 116 |
|
| 117 |
withForeignPtr ptr' $ \p -> do |
| 118 |
|
| 119 |
{# set bz_stream.next_in #} p nullPtr |
| 120 |
{# set bz_stream.avail_in #} p 0 |
| 121 |
|
| 122 |
bZ2BzCompressInit ptr' blkSize 0 wf |