{-# LINE 1 "src/LibLzma.hsc" #-}
{-# LANGUAGE RecordWildCards, DeriveDataTypeable #-}
module LibLzma
( LzmaStream
, LzmaRet(..)
, IntegrityCheck(..)
, CompressionLevel(..)
, newDecodeLzmaStream
, DecompressParams(..)
, defaultDecompressParams
, newEncodeLzmaStream
, CompressParams(..)
, defaultCompressParams
, runLzmaStream
, endLzmaStream
, LzmaAction(..)
) where
import Control.Applicative
import Control.Exception
import Control.Monad
import Control.Monad.ST.Strict (ST)
import Control.Monad.ST.Unsafe (unsafeIOToST)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Internal as BS
import qualified Data.ByteString.Unsafe as BS
import Data.Typeable
import Foreign
import Prelude
newtype LzmaStream = LS (ForeignPtr LzmaStream)
data LzmaRet = LzmaRetOK
| LzmaRetStreamEnd
| LzmaRetUnsupportedCheck
| LzmaRetGetCheck
| LzmaRetMemError
| LzmaRetMemlimitError
| LzmaRetFormatError
| LzmaRetOptionsError
| LzmaRetDataError
| LzmaRetBufError
| LzmaRetProgError
deriving (LzmaRet -> LzmaRet -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LzmaRet -> LzmaRet -> Bool
$c/= :: LzmaRet -> LzmaRet -> Bool
== :: LzmaRet -> LzmaRet -> Bool
$c== :: LzmaRet -> LzmaRet -> Bool
Eq,Eq LzmaRet
LzmaRet -> LzmaRet -> Bool
LzmaRet -> LzmaRet -> Ordering
LzmaRet -> LzmaRet -> LzmaRet
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: LzmaRet -> LzmaRet -> LzmaRet
$cmin :: LzmaRet -> LzmaRet -> LzmaRet
max :: LzmaRet -> LzmaRet -> LzmaRet
$cmax :: LzmaRet -> LzmaRet -> LzmaRet
>= :: LzmaRet -> LzmaRet -> Bool
$c>= :: LzmaRet -> LzmaRet -> Bool
> :: LzmaRet -> LzmaRet -> Bool
$c> :: LzmaRet -> LzmaRet -> Bool
<= :: LzmaRet -> LzmaRet -> Bool
$c<= :: LzmaRet -> LzmaRet -> Bool
< :: LzmaRet -> LzmaRet -> Bool
$c< :: LzmaRet -> LzmaRet -> Bool
compare :: LzmaRet -> LzmaRet -> Ordering
$ccompare :: LzmaRet -> LzmaRet -> Ordering
Ord,ReadPrec [LzmaRet]
ReadPrec LzmaRet
Int -> ReadS LzmaRet
ReadS [LzmaRet]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [LzmaRet]
$creadListPrec :: ReadPrec [LzmaRet]
readPrec :: ReadPrec LzmaRet
$creadPrec :: ReadPrec LzmaRet
readList :: ReadS [LzmaRet]
$creadList :: ReadS [LzmaRet]
readsPrec :: Int -> ReadS LzmaRet
$creadsPrec :: Int -> ReadS LzmaRet
Read,Int -> LzmaRet -> ShowS
[LzmaRet] -> ShowS
LzmaRet -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LzmaRet] -> ShowS
$cshowList :: [LzmaRet] -> ShowS
show :: LzmaRet -> String
$cshow :: LzmaRet -> String
showsPrec :: Int -> LzmaRet -> ShowS
$cshowsPrec :: Int -> LzmaRet -> ShowS
Show,Typeable)
instance Exception LzmaRet
toLzmaRet :: Int -> Maybe LzmaRet
toLzmaRet :: Int -> Maybe LzmaRet
toLzmaRet Int
i = case Int
i of
(Int
0) -> forall a. a -> Maybe a
Just LzmaRet
LzmaRetOK
{-# LINE 66 "src/LibLzma.hsc" #-}
(1) -> Just LzmaRetStreamEnd
{-# LINE 67 "src/LibLzma.hsc" #-}
(3) -> Just LzmaRetUnsupportedCheck
{-# LINE 68 "src/LibLzma.hsc" #-}
(4) -> Just LzmaRetGetCheck
{-# LINE 69 "src/LibLzma.hsc" #-}
(5) -> Just LzmaRetMemError
{-# LINE 70 "src/LibLzma.hsc" #-}
(6) -> Just LzmaRetMemlimitError
{-# LINE 71 "src/LibLzma.hsc" #-}
(7) -> Just LzmaRetFormatError
{-# LINE 72 "src/LibLzma.hsc" #-}
(8) -> Just LzmaRetOptionsError
{-# LINE 73 "src/LibLzma.hsc" #-}
(9) -> Just LzmaRetDataError
{-# LINE 74 "src/LibLzma.hsc" #-}
(10) -> Just LzmaRetBufError
{-# LINE 75 "src/LibLzma.hsc" #-}
(11) -> Just LzmaRetProgError
{-# LINE 76 "src/LibLzma.hsc" #-}
_ -> Nothing
data IntegrityCheck = IntegrityCheckNone
| IntegrityCheckCrc32
| IntegrityCheckCrc64
| IntegrityCheckSha256
deriving (IntegrityCheck -> IntegrityCheck -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IntegrityCheck -> IntegrityCheck -> Bool
$c/= :: IntegrityCheck -> IntegrityCheck -> Bool
== :: IntegrityCheck -> IntegrityCheck -> Bool
$c== :: IntegrityCheck -> IntegrityCheck -> Bool
Eq,Eq IntegrityCheck
IntegrityCheck -> IntegrityCheck -> Bool
IntegrityCheck -> IntegrityCheck -> Ordering
IntegrityCheck -> IntegrityCheck -> IntegrityCheck
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: IntegrityCheck -> IntegrityCheck -> IntegrityCheck
$cmin :: IntegrityCheck -> IntegrityCheck -> IntegrityCheck
max :: IntegrityCheck -> IntegrityCheck -> IntegrityCheck
$cmax :: IntegrityCheck -> IntegrityCheck -> IntegrityCheck
>= :: IntegrityCheck -> IntegrityCheck -> Bool
$c>= :: IntegrityCheck -> IntegrityCheck -> Bool
> :: IntegrityCheck -> IntegrityCheck -> Bool
$c> :: IntegrityCheck -> IntegrityCheck -> Bool
<= :: IntegrityCheck -> IntegrityCheck -> Bool
$c<= :: IntegrityCheck -> IntegrityCheck -> Bool
< :: IntegrityCheck -> IntegrityCheck -> Bool
$c< :: IntegrityCheck -> IntegrityCheck -> Bool
compare :: IntegrityCheck -> IntegrityCheck -> Ordering
$ccompare :: IntegrityCheck -> IntegrityCheck -> Ordering
Ord,ReadPrec [IntegrityCheck]
ReadPrec IntegrityCheck
Int -> ReadS IntegrityCheck
ReadS [IntegrityCheck]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [IntegrityCheck]
$creadListPrec :: ReadPrec [IntegrityCheck]
readPrec :: ReadPrec IntegrityCheck
$creadPrec :: ReadPrec IntegrityCheck
readList :: ReadS [IntegrityCheck]
$creadList :: ReadS [IntegrityCheck]
readsPrec :: Int -> ReadS IntegrityCheck
$creadsPrec :: Int -> ReadS IntegrityCheck
Read,Int -> IntegrityCheck -> ShowS
[IntegrityCheck] -> ShowS
IntegrityCheck -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [IntegrityCheck] -> ShowS
$cshowList :: [IntegrityCheck] -> ShowS
show :: IntegrityCheck -> String
$cshow :: IntegrityCheck -> String
showsPrec :: Int -> IntegrityCheck -> ShowS
$cshowsPrec :: Int -> IntegrityCheck -> ShowS
Show,Typeable)
fromIntegrityCheck :: IntegrityCheck -> Int
fromIntegrityCheck :: IntegrityCheck -> Int
fromIntegrityCheck IntegrityCheck
lc = case IntegrityCheck
lc of
IntegrityCheck
IntegrityCheckNone -> Int
0
{-# LINE 88 "src/LibLzma.hsc" #-}
IntegrityCheckCrc32 -> 1
{-# LINE 89 "src/LibLzma.hsc" #-}
IntegrityCheckCrc64 -> 4
{-# LINE 90 "src/LibLzma.hsc" #-}
IntegrityCheckSha256 -> 10
{-# LINE 91 "src/LibLzma.hsc" #-}
data CompressionLevel = CompressionLevel0
| CompressionLevel1
| CompressionLevel2
| CompressionLevel3
| CompressionLevel4
| CompressionLevel5
| CompressionLevel6
| CompressionLevel7
| CompressionLevel8
| CompressionLevel9
deriving (CompressionLevel -> CompressionLevel -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CompressionLevel -> CompressionLevel -> Bool
$c/= :: CompressionLevel -> CompressionLevel -> Bool
== :: CompressionLevel -> CompressionLevel -> Bool
$c== :: CompressionLevel -> CompressionLevel -> Bool
Eq,Eq CompressionLevel
CompressionLevel -> CompressionLevel -> Bool
CompressionLevel -> CompressionLevel -> Ordering
CompressionLevel -> CompressionLevel -> CompressionLevel
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CompressionLevel -> CompressionLevel -> CompressionLevel
$cmin :: CompressionLevel -> CompressionLevel -> CompressionLevel
max :: CompressionLevel -> CompressionLevel -> CompressionLevel
$cmax :: CompressionLevel -> CompressionLevel -> CompressionLevel
>= :: CompressionLevel -> CompressionLevel -> Bool
$c>= :: CompressionLevel -> CompressionLevel -> Bool
> :: CompressionLevel -> CompressionLevel -> Bool
$c> :: CompressionLevel -> CompressionLevel -> Bool
<= :: CompressionLevel -> CompressionLevel -> Bool
$c<= :: CompressionLevel -> CompressionLevel -> Bool
< :: CompressionLevel -> CompressionLevel -> Bool
$c< :: CompressionLevel -> CompressionLevel -> Bool
compare :: CompressionLevel -> CompressionLevel -> Ordering
$ccompare :: CompressionLevel -> CompressionLevel -> Ordering
Ord,ReadPrec [CompressionLevel]
ReadPrec CompressionLevel
Int -> ReadS CompressionLevel
ReadS [CompressionLevel]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CompressionLevel]
$creadListPrec :: ReadPrec [CompressionLevel]
readPrec :: ReadPrec CompressionLevel
$creadPrec :: ReadPrec CompressionLevel
readList :: ReadS [CompressionLevel]
$creadList :: ReadS [CompressionLevel]
readsPrec :: Int -> ReadS CompressionLevel
$creadsPrec :: Int -> ReadS CompressionLevel
Read,Int -> CompressionLevel -> ShowS
[CompressionLevel] -> ShowS
CompressionLevel -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CompressionLevel] -> ShowS
$cshowList :: [CompressionLevel] -> ShowS
show :: CompressionLevel -> String
$cshow :: CompressionLevel -> String
showsPrec :: Int -> CompressionLevel -> ShowS
$cshowsPrec :: Int -> CompressionLevel -> ShowS
Show,Int -> CompressionLevel
CompressionLevel -> Int
CompressionLevel -> [CompressionLevel]
CompressionLevel -> CompressionLevel
CompressionLevel -> CompressionLevel -> [CompressionLevel]
CompressionLevel
-> CompressionLevel -> CompressionLevel -> [CompressionLevel]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: CompressionLevel
-> CompressionLevel -> CompressionLevel -> [CompressionLevel]
$cenumFromThenTo :: CompressionLevel
-> CompressionLevel -> CompressionLevel -> [CompressionLevel]
enumFromTo :: CompressionLevel -> CompressionLevel -> [CompressionLevel]
$cenumFromTo :: CompressionLevel -> CompressionLevel -> [CompressionLevel]
enumFromThen :: CompressionLevel -> CompressionLevel -> [CompressionLevel]
$cenumFromThen :: CompressionLevel -> CompressionLevel -> [CompressionLevel]
enumFrom :: CompressionLevel -> [CompressionLevel]
$cenumFrom :: CompressionLevel -> [CompressionLevel]
fromEnum :: CompressionLevel -> Int
$cfromEnum :: CompressionLevel -> Int
toEnum :: Int -> CompressionLevel
$ctoEnum :: Int -> CompressionLevel
pred :: CompressionLevel -> CompressionLevel
$cpred :: CompressionLevel -> CompressionLevel
succ :: CompressionLevel -> CompressionLevel
$csucc :: CompressionLevel -> CompressionLevel
Enum,Typeable)
data DecompressParams = DecompressParams
{ DecompressParams -> Bool
decompressTellNoCheck :: !Bool
, DecompressParams -> Bool
decompressTellUnsupportedCheck :: !Bool
, DecompressParams -> Bool
decompressTellAnyCheck :: !Bool
, DecompressParams -> Bool
decompressConcatenated :: !Bool
, DecompressParams -> Bool
decompressAutoDecoder :: !Bool
, DecompressParams -> Word64
decompressMemLimit :: !Word64
} deriving (DecompressParams -> DecompressParams -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DecompressParams -> DecompressParams -> Bool
$c/= :: DecompressParams -> DecompressParams -> Bool
== :: DecompressParams -> DecompressParams -> Bool
$c== :: DecompressParams -> DecompressParams -> Bool
Eq,Int -> DecompressParams -> ShowS
[DecompressParams] -> ShowS
DecompressParams -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DecompressParams] -> ShowS
$cshowList :: [DecompressParams] -> ShowS
show :: DecompressParams -> String
$cshow :: DecompressParams -> String
showsPrec :: Int -> DecompressParams -> ShowS
$cshowsPrec :: Int -> DecompressParams -> ShowS
Show)
defaultDecompressParams :: DecompressParams
defaultDecompressParams :: DecompressParams
defaultDecompressParams = DecompressParams {Bool
Word64
decompressMemLimit :: Word64
decompressAutoDecoder :: Bool
decompressConcatenated :: Bool
decompressTellAnyCheck :: Bool
decompressTellUnsupportedCheck :: Bool
decompressTellNoCheck :: Bool
decompressMemLimit :: Word64
decompressAutoDecoder :: Bool
decompressConcatenated :: Bool
decompressTellAnyCheck :: Bool
decompressTellUnsupportedCheck :: Bool
decompressTellNoCheck :: Bool
..}
where
decompressTellNoCheck :: Bool
decompressTellNoCheck = Bool
False
decompressTellUnsupportedCheck :: Bool
decompressTellUnsupportedCheck = Bool
False
decompressTellAnyCheck :: Bool
decompressTellAnyCheck = Bool
False
decompressConcatenated :: Bool
decompressConcatenated = Bool
True
decompressAutoDecoder :: Bool
decompressAutoDecoder = Bool
False
decompressMemLimit :: Word64
decompressMemLimit = forall a. Bounded a => a
maxBound
data CompressParams = CompressParams
{ CompressParams -> IntegrityCheck
compressIntegrityCheck :: !IntegrityCheck
, CompressParams -> CompressionLevel
compressLevel :: !CompressionLevel
, CompressParams -> Bool
compressLevelExtreme :: !Bool
} deriving (CompressParams -> CompressParams -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CompressParams -> CompressParams -> Bool
$c/= :: CompressParams -> CompressParams -> Bool
== :: CompressParams -> CompressParams -> Bool
$c== :: CompressParams -> CompressParams -> Bool
Eq,Int -> CompressParams -> ShowS
[CompressParams] -> ShowS
CompressParams -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CompressParams] -> ShowS
$cshowList :: [CompressParams] -> ShowS
show :: CompressParams -> String
$cshow :: CompressParams -> String
showsPrec :: Int -> CompressParams -> ShowS
$cshowsPrec :: Int -> CompressParams -> ShowS
Show)
defaultCompressParams :: CompressParams
defaultCompressParams :: CompressParams
defaultCompressParams = CompressParams {Bool
CompressionLevel
IntegrityCheck
compressLevelExtreme :: Bool
compressLevel :: CompressionLevel
compressIntegrityCheck :: IntegrityCheck
compressLevelExtreme :: Bool
compressLevel :: CompressionLevel
compressIntegrityCheck :: IntegrityCheck
..}
where
compressIntegrityCheck :: IntegrityCheck
compressIntegrityCheck = IntegrityCheck
IntegrityCheckCrc64
compressLevel :: CompressionLevel
compressLevel = CompressionLevel
CompressionLevel6
compressLevelExtreme :: Bool
compressLevelExtreme = Bool
False
newDecodeLzmaStream :: DecompressParams -> ST s (Either LzmaRet LzmaStream)
newDecodeLzmaStream :: forall s. DecompressParams -> ST s (Either LzmaRet LzmaStream)
newDecodeLzmaStream (DecompressParams {Bool
Word64
decompressMemLimit :: Word64
decompressAutoDecoder :: Bool
decompressConcatenated :: Bool
decompressTellAnyCheck :: Bool
decompressTellUnsupportedCheck :: Bool
decompressTellNoCheck :: Bool
decompressMemLimit :: DecompressParams -> Word64
decompressAutoDecoder :: DecompressParams -> Bool
decompressConcatenated :: DecompressParams -> Bool
decompressTellAnyCheck :: DecompressParams -> Bool
decompressTellUnsupportedCheck :: DecompressParams -> Bool
decompressTellNoCheck :: DecompressParams -> Bool
..}) = forall a s. IO a -> ST s a
unsafeIOToST forall a b. (a -> b) -> a -> b
$ do
ForeignPtr LzmaStream
fp <- forall a. Int -> IO (ForeignPtr a)
mallocForeignPtrBytes ((Int
136))
{-# LINE 165 "src/LibLzma.hsc" #-}
addForeignPtrFinalizer c_hs_lzma_done_funptr fp
Int
rc <- forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr LzmaStream
fp (\Ptr LzmaStream
ptr -> Ptr LzmaStream -> Bool -> Word64 -> Word32 -> IO Int
c_hs_lzma_init_decoder Ptr LzmaStream
ptr Bool
decompressAutoDecoder Word64
decompressMemLimit Word32
flags')
LzmaRet
rc' <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"newDecodeLzmaStream: invalid return code") forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Int -> Maybe LzmaRet
toLzmaRet Int
rc
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case LzmaRet
rc' of
LzmaRet
LzmaRetOK -> forall a b. b -> Either a b
Right (ForeignPtr LzmaStream -> LzmaStream
LS ForeignPtr LzmaStream
fp)
LzmaRet
_ -> forall a b. a -> Either a b
Left LzmaRet
rc'
where
flags' :: Word32
flags' =
(if Bool
decompressTellNoCheck then (Word32
1) else Word32
0) forall a. Bits a => a -> a -> a
.|.
{-# LINE 175 "src/LibLzma.hsc" #-}
(if Bool
decompressTellUnsupportedCheck then (Word32
2) else Word32
0) forall a. Bits a => a -> a -> a
.|.
{-# LINE 176 "src/LibLzma.hsc" #-}
(if Bool
decompressTellAnyCheck then (Word32
4) else Word32
0) forall a. Bits a => a -> a -> a
.|.
{-# LINE 177 "src/LibLzma.hsc" #-}
(if Bool
decompressConcatenated then (Word32
8) else Word32
0)
{-# LINE 178 "src/LibLzma.hsc" #-}
newEncodeLzmaStream :: CompressParams -> ST s (Either LzmaRet LzmaStream)
newEncodeLzmaStream :: forall s. CompressParams -> ST s (Either LzmaRet LzmaStream)
newEncodeLzmaStream (CompressParams {Bool
CompressionLevel
IntegrityCheck
compressLevelExtreme :: Bool
compressLevel :: CompressionLevel
compressIntegrityCheck :: IntegrityCheck
compressLevelExtreme :: CompressParams -> Bool
compressLevel :: CompressParams -> CompressionLevel
compressIntegrityCheck :: CompressParams -> IntegrityCheck
..}) = forall a s. IO a -> ST s a
unsafeIOToST forall a b. (a -> b) -> a -> b
$ do
ForeignPtr LzmaStream
fp <- forall a. Int -> IO (ForeignPtr a)
mallocForeignPtrBytes ((Int
136))
{-# LINE 182 "src/LibLzma.hsc" #-}
addForeignPtrFinalizer c_hs_lzma_done_funptr fp
Int
rc <- forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr LzmaStream
fp (\Ptr LzmaStream
ptr -> Ptr LzmaStream -> Word32 -> Int -> IO Int
c_hs_lzma_init_encoder Ptr LzmaStream
ptr Word32
preset Int
check)
LzmaRet
rc' <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"newDecodeLzmaStream: invalid return code") forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Int -> Maybe LzmaRet
toLzmaRet Int
rc
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case LzmaRet
rc' of
LzmaRet
LzmaRetOK -> forall a b. b -> Either a b
Right (ForeignPtr LzmaStream -> LzmaStream
LS ForeignPtr LzmaStream
fp)
LzmaRet
_ -> forall a b. a -> Either a b
Left LzmaRet
rc'
where
preset :: Word32
preset = forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Enum a => a -> Int
fromEnum CompressionLevel
compressLevel) forall a. Bits a => a -> a -> a
.|.
(if Bool
compressLevelExtreme then (Word32
2147483648) else Word32
0)
{-# LINE 193 "src/LibLzma.hsc" #-}
check = fromIntegrityCheck compressIntegrityCheck
data LzmaAction = LzmaRun
| LzmaSyncFlush
| LzmaFullFlush
| LzmaFinish
deriving (LzmaAction -> LzmaAction -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LzmaAction -> LzmaAction -> Bool
$c/= :: LzmaAction -> LzmaAction -> Bool
== :: LzmaAction -> LzmaAction -> Bool
$c== :: LzmaAction -> LzmaAction -> Bool
Eq,Int -> LzmaAction -> ShowS
[LzmaAction] -> ShowS
LzmaAction -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LzmaAction] -> ShowS
$cshowList :: [LzmaAction] -> ShowS
show :: LzmaAction -> String
$cshow :: LzmaAction -> String
showsPrec :: Int -> LzmaAction -> ShowS
$cshowsPrec :: Int -> LzmaAction -> ShowS
Show)
runLzmaStream :: LzmaStream -> ByteString -> LzmaAction -> Int -> ST s (LzmaRet,Int,ByteString)
runLzmaStream :: forall s.
LzmaStream
-> ByteString
-> LzmaAction
-> Int
-> ST s (LzmaRet, Int, ByteString)
runLzmaStream (LS ForeignPtr LzmaStream
ls) ByteString
ibs LzmaAction
action0 Int
buflen
| Int
buflen forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return (LzmaRet
LzmaRetOptionsError,Int
0,ByteString
BS.empty)
| Bool
otherwise = forall a s. IO a -> ST s a
unsafeIOToST forall a b. (a -> b) -> a -> b
$ forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr LzmaStream
ls forall a b. (a -> b) -> a -> b
$ \Ptr LzmaStream
lsptr ->
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.unsafeUseAsCStringLen ByteString
ibs forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
ibsptr, Int
ibslen) -> do
(ByteString
obuf,LzmaRet
rc) <- forall a.
Int -> (Ptr Word8 -> IO (Int, Int, a)) -> IO (ByteString, a)
BS.createAndTrim' Int
buflen forall a b. (a -> b) -> a -> b
$ \Ptr Word8
bufptr -> do
Int
rc' <- Ptr LzmaStream
-> Int -> Ptr Word8 -> Int -> Ptr Word8 -> Int -> IO Int
c_hs_lzma_run Ptr LzmaStream
lsptr Int
action (forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
ibsptr) Int
ibslen Ptr Word8
bufptr Int
buflen
LzmaRet
rc'' <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"runLzmaStream: invalid return code") forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Int -> Maybe LzmaRet
toLzmaRet Int
rc'
Int
availOut <- ((\Ptr LzmaStream
hsc_ptr -> forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr LzmaStream
hsc_ptr Int
32)) Ptr LzmaStream
lsptr
{-# LINE 211 "src/LibLzma.hsc" #-}
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int
buflen forall a. Ord a => a -> a -> Bool
>= Int
availOut Bool -> Bool -> Bool
&& Int
availOut forall a. Ord a => a -> a -> Bool
>= Int
0) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"runLzmaStream: invalid avail_out"
let produced :: Int
produced = Int
buflen forall a. Num a => a -> a -> a
- Int
availOut
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
0, Int
produced, LzmaRet
rc'')
Int
availIn <- ((\Ptr LzmaStream
hsc_ptr -> forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr LzmaStream
hsc_ptr Int
8)) Ptr LzmaStream
lsptr
{-# LINE 218 "src/LibLzma.hsc" #-}
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int
ibslen forall a. Ord a => a -> a -> Bool
>= Int
availIn Bool -> Bool -> Bool
&& Int
availIn forall a. Ord a => a -> a -> Bool
>= Int
0) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"runLzmaStream: invalid avail_in"
let consumed :: Int
consumed = Int
ibslen forall a. Num a => a -> a -> a
- Int
availIn
forall (m :: * -> *) a. Monad m => a -> m a
return (LzmaRet
rc, forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
consumed, ByteString
obuf)
where
action :: Int
action = case LzmaAction
action0 of
LzmaAction
LzmaRun -> Int
0
{-# LINE 227 "src/LibLzma.hsc" #-}
LzmaAction
LzmaSyncFlush -> Int
1
{-# LINE 228 "src/LibLzma.hsc" #-}
LzmaAction
LzmaFullFlush -> Int
2
{-# LINE 229 "src/LibLzma.hsc" #-}
LzmaAction
LzmaFinish -> Int
3
{-# LINE 230 "src/LibLzma.hsc" #-}
endLzmaStream :: LzmaStream -> ST s ()
endLzmaStream :: forall s. LzmaStream -> ST s ()
endLzmaStream (LS ForeignPtr LzmaStream
ls) = forall a s. IO a -> ST s a
unsafeIOToST forall a b. (a -> b) -> a -> b
$ forall a. ForeignPtr a -> IO ()
finalizeForeignPtr ForeignPtr LzmaStream
ls
foreign import ccall "hs_lzma_init_decoder"
c_hs_lzma_init_decoder :: Ptr LzmaStream -> Bool -> Word64 -> Word32 -> IO Int
foreign import ccall "hs_lzma_init_encoder"
c_hs_lzma_init_encoder :: Ptr LzmaStream -> Word32 -> Int -> IO Int
foreign import ccall "hs_lzma_run"
c_hs_lzma_run :: Ptr LzmaStream -> Int -> Ptr Word8 -> Int -> Ptr Word8 -> Int -> IO Int
foreign import ccall "&hs_lzma_done"
c_hs_lzma_done_funptr :: FunPtr (Ptr LzmaStream -> IO ())