{-# LINE 1 "src/LibBrotli.hsc" #-}
{-# LANGUAGE BangPatterns       #-}
{-# LANGUAGE CApiFFI            #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE RecordWildCards    #-}

-- Copyright (C) 2016  Herbert Valerio Riedel
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.

-- | Internal low-level bindings to libbrotli
module LibBrotli
   ( CompressParams(..), defaultCompressParams
   , CompressionLevel(..), CompressionWindowSize(..), CompressionMode(..)
   , DecompressParams(..), defaultDecompressParams
   , BrotliState(..), BrotliEncOp(..)
   , newBrotliEncoder, newBrotliDecoder
   , finalizeBrotliEncoder, finalizeBrotliDecoder
   , runBrotliEncoder, runBrotliDecoder
   , readBrotliEncoder, readBrotliDecoder
   , brotliEncoderVersion, brotliDecoderVersion
   , BrotliDecoderErrorCode(..), showBrotliDecoderErrorCode
   ) where

import           Control.Applicative
import           Control.Monad
import           Control.Exception
import           Control.Monad.ST.Strict (ST)
import           Control.Monad.ST.Unsafe (unsafeIOToST)
import           Data.ByteString (ByteString)
import           Data.Typeable (Typeable)
import           Foreign
import           Foreign.C
import           System.IO.Unsafe as Unsafe (unsafePerformIO)
import           Prelude
import qualified Data.ByteString as BS
import qualified Data.ByteString.Unsafe as BS
import           Control.Monad.Trans.Maybe (MaybeT(..))



packBS :: Ptr Word8 -> CSize -> IO ByteString
packBS :: Ptr Word8 -> CSize -> IO ByteString
packBS Ptr Word8
p CSize
sz
  | CSize
sz CSize -> CSize -> Bool
forall a. Ord a => a -> a -> Bool
> CSize
0    = CStringLen -> IO ByteString
BS.packCStringLen (Ptr Word8 -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
p, CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
sz)
  | Bool
otherwise = ByteString -> IO ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
BS.empty

----------------------------------------------------------------------------

type BrotliBool = Int32
{-# LINE 60 "src/LibBrotli.hsc" #-}

fromBrotliBool :: BrotliBool -> Maybe Bool
fromBrotliBool :: BrotliBool -> Maybe Bool
fromBrotliBool BrotliBool
1  = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
{-# LINE 63 "src/LibBrotli.hsc" #-}
fromBrotliBool 0 = Just False
{-# LINE 64 "src/LibBrotli.hsc" #-}
fromBrotliBool _ = Nothing

toBrotliBool :: Bool -> BrotliBool
toBrotliBool :: Bool -> BrotliBool
toBrotliBool Bool
False =  BrotliBool
0
{-# LINE 68 "src/LibBrotli.hsc" #-}
toBrotliBool True  =  1
{-# LINE 69 "src/LibBrotli.hsc" #-}

----------------------------------------------------------------------------

-- | Errors signalled by the Brotli decoder. Use 'showBrotliDecoderErrorCode' to convert the numeric code into an error message.
newtype BrotliDecoderErrorCode = BrotliDecoderErrorCode Int deriving (BrotliDecoderErrorCode -> BrotliDecoderErrorCode -> Bool
(BrotliDecoderErrorCode -> BrotliDecoderErrorCode -> Bool)
-> (BrotliDecoderErrorCode -> BrotliDecoderErrorCode -> Bool)
-> Eq BrotliDecoderErrorCode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BrotliDecoderErrorCode -> BrotliDecoderErrorCode -> Bool
$c/= :: BrotliDecoderErrorCode -> BrotliDecoderErrorCode -> Bool
== :: BrotliDecoderErrorCode -> BrotliDecoderErrorCode -> Bool
$c== :: BrotliDecoderErrorCode -> BrotliDecoderErrorCode -> Bool
Eq,Int -> BrotliDecoderErrorCode -> ShowS
[BrotliDecoderErrorCode] -> ShowS
BrotliDecoderErrorCode -> String
(Int -> BrotliDecoderErrorCode -> ShowS)
-> (BrotliDecoderErrorCode -> String)
-> ([BrotliDecoderErrorCode] -> ShowS)
-> Show BrotliDecoderErrorCode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BrotliDecoderErrorCode] -> ShowS
$cshowList :: [BrotliDecoderErrorCode] -> ShowS
show :: BrotliDecoderErrorCode -> String
$cshow :: BrotliDecoderErrorCode -> String
showsPrec :: Int -> BrotliDecoderErrorCode -> ShowS
$cshowsPrec :: Int -> BrotliDecoderErrorCode -> ShowS
Show,Typeable)

instance Exception BrotliDecoderErrorCode

-- | Convert numeric 'BrotliDecoderErrorCode' into textual error message.
{-# NOINLINE showBrotliDecoderErrorCode #-}
showBrotliDecoderErrorCode :: BrotliDecoderErrorCode -> String
showBrotliDecoderErrorCode :: BrotliDecoderErrorCode -> String
showBrotliDecoderErrorCode (BrotliDecoderErrorCode Int
ec) = IO String -> String
forall a. IO a -> a
Unsafe.unsafePerformIO (IO String -> String) -> IO String -> String
forall a b. (a -> b) -> a -> b
$ do
  Ptr CChar
bufptr <- BrotliBool -> IO (Ptr CChar)
c_BrotliDecoderErrorString (Int -> BrotliBool
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
ec)
  if Ptr CChar
bufptr Ptr CChar -> Ptr CChar -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr CChar
forall a. Ptr a
nullPtr then String -> IO String
forall (f :: * -> *) a. Applicative f => a -> f a
pure String
"" else Ptr CChar -> IO String
peekCAString Ptr CChar
bufptr

----------------------------------------------------------------------------

-- internal state enumeration
data BrotliState
    = BSNeedsInput -- NB: initial state
    | BSHasOutput
    | BSFinished
    | BSFail
    | BSInternalError -- the impossible happened
    deriving (BrotliState -> BrotliState -> Bool
(BrotliState -> BrotliState -> Bool)
-> (BrotliState -> BrotliState -> Bool) -> Eq BrotliState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BrotliState -> BrotliState -> Bool
$c/= :: BrotliState -> BrotliState -> Bool
== :: BrotliState -> BrotliState -> Bool
$c== :: BrotliState -> BrotliState -> Bool
Eq,Int -> BrotliState -> ShowS
[BrotliState] -> ShowS
BrotliState -> String
(Int -> BrotliState -> ShowS)
-> (BrotliState -> String)
-> ([BrotliState] -> ShowS)
-> Show BrotliState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BrotliState] -> ShowS
$cshowList :: [BrotliState] -> ShowS
show :: BrotliState -> String
$cshow :: BrotliState -> String
showsPrec :: Int -> BrotliState -> ShowS
$cshowsPrec :: Int -> BrotliState -> ShowS
Show)

type HsBrotliState = Word32
{-# LINE 96 "src/LibBrotli.hsc" #-}

fromHsBrotliState :: HsBrotliState -> BrotliState
fromHsBrotliState :: HsBrotliState -> BrotliState
fromHsBrotliState HsBrotliState
x = case HsBrotliState
x of
  HsBrotliState
0 -> BrotliState
BSNeedsInput
{-# LINE 100 "src/LibBrotli.hsc" #-}
  1  -> BSHasOutput
{-# LINE 101 "src/LibBrotli.hsc" #-}
  2    -> BSFinished
{-# LINE 102 "src/LibBrotli.hsc" #-}
  3        -> BSFail
{-# LINE 103 "src/LibBrotli.hsc" #-}
  _                          -> BSInternalError

----------------------------------------------------------------------------

-- | Set of parameters for compression. The defaults are 'defaultCompressParams'.
data CompressParams = CompressParams
    { CompressParams -> CompressionLevel
compressLevel :: !CompressionLevel -- ^ 'CompressParams' field: See documentation of 'CompressionLevel'
    , CompressParams -> CompressionWindowSize
compressWindowSize :: !CompressionWindowSize -- ^ 'CompressParams' field: See documentation of 'CompressionWindowSize'
    , CompressParams -> CompressionMode
compressMode  :: !CompressionMode  -- ^ 'CompressParams' field: See documentation of 'CompressionMode'
    , CompressParams -> HsBrotliState
compressSizeHint :: !Word32 -- ^ 'CompressParams' field: Estimated total input size. The default value is @0@, which means that the total input size is unknown.
    }

-- | Set of parameters for decompression. The defaults are
-- 'defaultDecompressParams'.
data DecompressParams = DecompressParams
    { DecompressParams -> Bool
decompressDisableRingBufferReallocation :: !Bool -- ^ 'DecompressParams' field: If 'True', ring buffer is allocated according to window size, despite the real size of the content. (default: 'False')
    }

-- | The default set of parameters for compression. This is typically
-- used with the 'Codec.Compression.Brotli.compressWith' function with specific parameters
-- overridden.
defaultCompressParams :: CompressParams
defaultCompressParams :: CompressParams
defaultCompressParams = CompressParams :: CompressionLevel
-> CompressionWindowSize
-> CompressionMode
-> HsBrotliState
-> CompressParams
CompressParams {HsBrotliState
CompressionWindowSize
CompressionLevel
CompressionMode
compressSizeHint :: HsBrotliState
compressMode :: CompressionMode
compressWindowSize :: CompressionWindowSize
compressLevel :: CompressionLevel
compressSizeHint :: HsBrotliState
compressMode :: CompressionMode
compressWindowSize :: CompressionWindowSize
compressLevel :: CompressionLevel
..}
  where
    compressLevel :: CompressionLevel
compressLevel      = CompressionLevel
forall a. Bounded a => a
maxBound
    compressWindowSize :: CompressionWindowSize
compressWindowSize = CompressionWindowSize
CompressionWindowBits22
    compressMode :: CompressionMode
compressMode       = CompressionMode
CompressionModeGeneric
    compressSizeHint :: HsBrotliState
compressSizeHint   = HsBrotliState
0

-- | The default set of parameters for decompression. This is
-- typically used with the 'Codec.Compression.Brotli.decompressWith' function with specific
-- parameters overridden.
defaultDecompressParams :: DecompressParams
defaultDecompressParams :: DecompressParams
defaultDecompressParams = Bool -> DecompressParams
DecompressParams Bool
False

-- | C pointer to @BrotliEncoderState@
newtype BrotliEncoder = BrotliEncoder (ForeignPtr BrotliEncoder)

-- | C pointer to @BrotliDecoderState@
newtype BrotliDecoder = BrotliDecoder (ForeignPtr BrotliDecoder)

newBrotliEncoder :: CompressParams -> ST s (Maybe BrotliEncoder)
newBrotliEncoder :: CompressParams -> ST s (Maybe BrotliEncoder)
newBrotliEncoder CompressParams{HsBrotliState
CompressionWindowSize
CompressionLevel
CompressionMode
compressSizeHint :: HsBrotliState
compressMode :: CompressionMode
compressWindowSize :: CompressionWindowSize
compressLevel :: CompressionLevel
compressSizeHint :: CompressParams -> HsBrotliState
compressMode :: CompressParams -> CompressionMode
compressWindowSize :: CompressParams -> CompressionWindowSize
compressLevel :: CompressParams -> CompressionLevel
..} = IO (Maybe BrotliEncoder) -> ST s (Maybe BrotliEncoder)
forall a s. IO a -> ST s a
unsafeIOToST (IO (Maybe BrotliEncoder) -> ST s (Maybe BrotliEncoder))
-> IO (Maybe BrotliEncoder) -> ST s (Maybe BrotliEncoder)
forall a b. (a -> b) -> a -> b
$ MaybeT IO BrotliEncoder -> IO (Maybe BrotliEncoder)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT IO BrotliEncoder -> IO (Maybe BrotliEncoder))
-> MaybeT IO BrotliEncoder -> IO (Maybe BrotliEncoder)
forall a b. (a -> b) -> a -> b
$ do
    BrotliEncoder
fp <- IO (Maybe BrotliEncoder) -> MaybeT IO BrotliEncoder
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT IO (Maybe BrotliEncoder)
createBrotliEncoder

    Bool -> MaybeT IO () -> MaybeT IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (HsBrotliState
11 HsBrotliState -> HsBrotliState -> Bool
forall a. Eq a => a -> a -> Bool
== HsBrotliState
qualityParm) (MaybeT IO () -> MaybeT IO ()) -> MaybeT IO () -> MaybeT IO ()
forall a b. (a -> b) -> a -> b
$ do
{-# LINE 149 "src/LibBrotli.hsc" #-}
      rc <- setParm fp 1 qualityParm
{-# LINE 150 "src/LibBrotli.hsc" #-}
      unless rc $ fail "invalid BROTLI_PARAM_QUALITY"

    Bool -> MaybeT IO () -> MaybeT IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (HsBrotliState
0 HsBrotliState -> HsBrotliState -> Bool
forall a. Eq a => a -> a -> Bool
== HsBrotliState
modeParm) (MaybeT IO () -> MaybeT IO ()) -> MaybeT IO () -> MaybeT IO ()
forall a b. (a -> b) -> a -> b
$ do
{-# LINE 153 "src/LibBrotli.hsc" #-}
      rc <- setParm fp 0 modeParm
{-# LINE 154 "src/LibBrotli.hsc" #-}
      unless rc $ fail "invalid BROTLI_PARAM_MODE"

    Bool -> MaybeT IO () -> MaybeT IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (HsBrotliState
22 HsBrotliState -> HsBrotliState -> Bool
forall a. Eq a => a -> a -> Bool
== HsBrotliState
winParm) (MaybeT IO () -> MaybeT IO ()) -> MaybeT IO () -> MaybeT IO ()
forall a b. (a -> b) -> a -> b
$ do
{-# LINE 157 "src/LibBrotli.hsc" #-}
      unless ((10 <= winParm) && (winParm <= 24)) $
{-# LINE 158 "src/LibBrotli.hsc" #-}
        fail "invalid BROTLI_PARAM_LGWIN (internal inconsistency)"

      rc <- setParm fp 2 winParm
{-# LINE 161 "src/LibBrotli.hsc" #-}
      unless rc $ fail "invalid BROTLI_PARAM_LGWIN"

    Bool -> MaybeT IO () -> MaybeT IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (HsBrotliState
0 HsBrotliState -> HsBrotliState -> Bool
forall a. Eq a => a -> a -> Bool
== HsBrotliState
compressSizeHint) (MaybeT IO () -> MaybeT IO ()) -> MaybeT IO () -> MaybeT IO ()
forall a b. (a -> b) -> a -> b
$ do
      Bool
rc <- BrotliEncoder -> HsBrotliState -> HsBrotliState -> MaybeT IO Bool
setParm BrotliEncoder
fp HsBrotliState
5 HsBrotliState
compressSizeHint
{-# LINE 165 "src/LibBrotli.hsc" #-}
      unless rc $ fail "invalid BROTLI_PARAM_SIZE_HINT"

    BrotliEncoder -> MaybeT IO BrotliEncoder
forall (f :: * -> *) a. Applicative f => a -> f a
pure BrotliEncoder
fp
  where
    setParm :: BrotliEncoder -> HsBrotliState -> HsBrotliState -> MaybeT IO Bool
setParm (BrotliEncoder ForeignPtr BrotliEncoder
fp) HsBrotliState
k HsBrotliState
v = IO (Maybe Bool) -> MaybeT IO Bool
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (ForeignPtr BrotliEncoder
-> (Ptr BrotliEncoder -> IO (Maybe Bool)) -> IO (Maybe Bool)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr BrotliEncoder
fp ((Ptr BrotliEncoder -> IO (Maybe Bool)) -> IO (Maybe Bool))
-> (Ptr BrotliEncoder -> IO (Maybe Bool)) -> IO (Maybe Bool)
forall a b. (a -> b) -> a -> b
$ \Ptr BrotliEncoder
p -> BrotliBool -> Maybe Bool
fromBrotliBool (BrotliBool -> Maybe Bool) -> IO BrotliBool -> IO (Maybe Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr BrotliEncoder
-> HsBrotliState -> HsBrotliState -> IO BrotliBool
c_BrotliEncoderSetParameter Ptr BrotliEncoder
p HsBrotliState
k HsBrotliState
v)

    qualityParm :: HsBrotliState
qualityParm = Int -> HsBrotliState
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CompressionLevel -> Int
forall a. Enum a => a -> Int
fromEnum CompressionLevel
compressLevel)
    winParm :: HsBrotliState
winParm = Int -> HsBrotliState
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CompressionWindowSize -> Int
forall a. Enum a => a -> Int
fromEnum CompressionWindowSize
compressWindowSize)
    modeParm :: HsBrotliState
modeParm    = case CompressionMode
compressMode of
                    CompressionMode
CompressionModeGeneric -> HsBrotliState
0
{-# LINE 175 "src/LibBrotli.hsc" #-}
                    CompressionMode
CompressionModeText    -> HsBrotliState
1
{-# LINE 176 "src/LibBrotli.hsc" #-}
                    CompressionMode
CompressionModeFont    -> HsBrotliState
2
{-# LINE 177 "src/LibBrotli.hsc" #-}

newBrotliDecoder :: DecompressParams -> ST s (Maybe BrotliDecoder)
newBrotliDecoder :: DecompressParams -> ST s (Maybe BrotliDecoder)
newBrotliDecoder DecompressParams{Bool
decompressDisableRingBufferReallocation :: Bool
decompressDisableRingBufferReallocation :: DecompressParams -> Bool
..} = IO (Maybe BrotliDecoder) -> ST s (Maybe BrotliDecoder)
forall a s. IO a -> ST s a
unsafeIOToST (IO (Maybe BrotliDecoder) -> ST s (Maybe BrotliDecoder))
-> IO (Maybe BrotliDecoder) -> ST s (Maybe BrotliDecoder)
forall a b. (a -> b) -> a -> b
$ MaybeT IO BrotliDecoder -> IO (Maybe BrotliDecoder)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT IO BrotliDecoder -> IO (Maybe BrotliDecoder))
-> MaybeT IO BrotliDecoder -> IO (Maybe BrotliDecoder)
forall a b. (a -> b) -> a -> b
$ do
    BrotliDecoder
fp <- IO (Maybe BrotliDecoder) -> MaybeT IO BrotliDecoder
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT IO (Maybe BrotliDecoder)
createBrotliDecoder

    Bool -> MaybeT IO () -> MaybeT IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
decompressDisableRingBufferReallocation (MaybeT IO () -> MaybeT IO ()) -> MaybeT IO () -> MaybeT IO ()
forall a b. (a -> b) -> a -> b
$ do
      Bool
rc <- BrotliDecoder -> HsBrotliState -> HsBrotliState -> MaybeT IO Bool
setParm BrotliDecoder
fp HsBrotliState
0
{-# LINE 184 "src/LibBrotli.hsc" #-}
                       (BrotliBool -> HsBrotliState
forall a b. (Integral a, Num b) => a -> b
fromIntegral (BrotliBool -> HsBrotliState) -> BrotliBool -> HsBrotliState
forall a b. (a -> b) -> a -> b
$ Bool -> BrotliBool
toBrotliBool Bool
decompressDisableRingBufferReallocation)
      Bool -> MaybeT IO () -> MaybeT IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
rc (MaybeT IO () -> MaybeT IO ()) -> MaybeT IO () -> MaybeT IO ()
forall a b. (a -> b) -> a -> b
$ String -> MaybeT IO ()
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"invalid BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION"

    BrotliDecoder -> MaybeT IO BrotliDecoder
forall (f :: * -> *) a. Applicative f => a -> f a
pure BrotliDecoder
fp
  where
    setParm :: BrotliDecoder -> HsBrotliState -> HsBrotliState -> MaybeT IO Bool
setParm (BrotliDecoder ForeignPtr BrotliDecoder
fp) HsBrotliState
k HsBrotliState
v = IO (Maybe Bool) -> MaybeT IO Bool
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (ForeignPtr BrotliDecoder
-> (Ptr BrotliDecoder -> IO (Maybe Bool)) -> IO (Maybe Bool)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr BrotliDecoder
fp ((Ptr BrotliDecoder -> IO (Maybe Bool)) -> IO (Maybe Bool))
-> (Ptr BrotliDecoder -> IO (Maybe Bool)) -> IO (Maybe Bool)
forall a b. (a -> b) -> a -> b
$ \Ptr BrotliDecoder
p -> BrotliBool -> Maybe Bool
fromBrotliBool (BrotliBool -> Maybe Bool) -> IO BrotliBool -> IO (Maybe Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr BrotliDecoder
-> HsBrotliState -> HsBrotliState -> IO BrotliBool
c_BrotliDecoderSetParameter Ptr BrotliDecoder
p HsBrotliState
k HsBrotliState
v)

createBrotliEncoder :: IO (Maybe BrotliEncoder)
createBrotliEncoder :: IO (Maybe BrotliEncoder)
createBrotliEncoder = IO (Maybe BrotliEncoder) -> IO (Maybe BrotliEncoder)
forall a. IO a -> IO a
mask_ (IO (Maybe BrotliEncoder) -> IO (Maybe BrotliEncoder))
-> IO (Maybe BrotliEncoder) -> IO (Maybe BrotliEncoder)
forall a b. (a -> b) -> a -> b
$ do
    Ptr BrotliEncoder
p <- Ptr () -> Ptr () -> Ptr () -> IO (Ptr BrotliEncoder)
c_BrotliEncoderCreateInstance Ptr ()
forall a. Ptr a
nullPtr Ptr ()
forall a. Ptr a
nullPtr Ptr ()
forall a. Ptr a
nullPtr
    case () of
      ()
_ | Ptr BrotliEncoder
p Ptr BrotliEncoder -> Ptr BrotliEncoder -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr BrotliEncoder
forall a. Ptr a
nullPtr -> Maybe BrotliEncoder -> IO (Maybe BrotliEncoder)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe BrotliEncoder
forall a. Maybe a
Nothing
        | Bool
otherwise -> do
            !ForeignPtr BrotliEncoder
fp <- FinalizerPtr BrotliEncoder
-> Ptr BrotliEncoder -> IO (ForeignPtr BrotliEncoder)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr BrotliEncoder
cp_BrotliEncoderDestroyInstance Ptr BrotliEncoder
p
            Maybe BrotliEncoder -> IO (Maybe BrotliEncoder)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BrotliEncoder -> Maybe BrotliEncoder
forall a. a -> Maybe a
Just (ForeignPtr BrotliEncoder -> BrotliEncoder
BrotliEncoder ForeignPtr BrotliEncoder
fp))

createBrotliDecoder :: IO (Maybe BrotliDecoder)
createBrotliDecoder :: IO (Maybe BrotliDecoder)
createBrotliDecoder = IO (Maybe BrotliDecoder) -> IO (Maybe BrotliDecoder)
forall a. IO a -> IO a
mask_ (IO (Maybe BrotliDecoder) -> IO (Maybe BrotliDecoder))
-> IO (Maybe BrotliDecoder) -> IO (Maybe BrotliDecoder)
forall a b. (a -> b) -> a -> b
$ do
    Ptr BrotliDecoder
p <- Ptr () -> Ptr () -> Ptr () -> IO (Ptr BrotliDecoder)
c_BrotliDecoderCreateInstance Ptr ()
forall a. Ptr a
nullPtr Ptr ()
forall a. Ptr a
nullPtr Ptr ()
forall a. Ptr a
nullPtr
    case () of
      ()
_ | Ptr BrotliDecoder
p Ptr BrotliDecoder -> Ptr BrotliDecoder -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr BrotliDecoder
forall a. Ptr a
nullPtr -> Maybe BrotliDecoder -> IO (Maybe BrotliDecoder)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe BrotliDecoder
forall a. Maybe a
Nothing
        | Bool
otherwise -> do
            !ForeignPtr BrotliDecoder
fp <- FinalizerPtr BrotliDecoder
-> Ptr BrotliDecoder -> IO (ForeignPtr BrotliDecoder)
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr BrotliDecoder
cp_BrotliDecoderDestroyInstance Ptr BrotliDecoder
p
            Maybe BrotliDecoder -> IO (Maybe BrotliDecoder)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BrotliDecoder -> Maybe BrotliDecoder
forall a. a -> Maybe a
Just (ForeignPtr BrotliDecoder -> BrotliDecoder
BrotliDecoder ForeignPtr BrotliDecoder
fp))

finalizeBrotliEncoder :: BrotliEncoder -> ST s ()
finalizeBrotliEncoder :: BrotliEncoder -> ST s ()
finalizeBrotliEncoder (BrotliEncoder ForeignPtr BrotliEncoder
s) = IO () -> ST s ()
forall a s. IO a -> ST s a
unsafeIOToST (ForeignPtr BrotliEncoder -> IO ()
forall a. ForeignPtr a -> IO ()
finalizeForeignPtr ForeignPtr BrotliEncoder
s)

finalizeBrotliDecoder :: BrotliDecoder -> ST s ()
finalizeBrotliDecoder :: BrotliDecoder -> ST s ()
finalizeBrotliDecoder (BrotliDecoder ForeignPtr BrotliDecoder
s) = IO () -> ST s ()
forall a s. IO a -> ST s a
unsafeIOToST (ForeignPtr BrotliDecoder -> IO ()
forall a. ForeignPtr a -> IO ()
finalizeForeignPtr ForeignPtr BrotliDecoder
s)

data BrotliEncOp
     = BrotliEncOpProcess
     | BrotliEncOpFlush
     | BrotliEncOpFinish
--   | BrotliEncOpEmitMetadata -- TODO

runBrotliEncoder :: BrotliEncoder
                 -> ByteString
                 -> BrotliEncOp
                 -> ST s (BrotliState,Int) {- (state, unused-input) -}
runBrotliEncoder :: BrotliEncoder
-> ByteString -> BrotliEncOp -> ST s (BrotliState, Int)
runBrotliEncoder (BrotliEncoder ForeignPtr BrotliEncoder
fp) ByteString
ibs BrotliEncOp
action0
  = IO (BrotliState, Int) -> ST s (BrotliState, Int)
forall a s. IO a -> ST s a
unsafeIOToST (IO (BrotliState, Int) -> ST s (BrotliState, Int))
-> IO (BrotliState, Int) -> ST s (BrotliState, Int)
forall a b. (a -> b) -> a -> b
$ ForeignPtr BrotliEncoder
-> (Ptr BrotliEncoder -> IO (BrotliState, Int))
-> IO (BrotliState, Int)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr BrotliEncoder
fp ((Ptr BrotliEncoder -> IO (BrotliState, Int))
 -> IO (BrotliState, Int))
-> (Ptr BrotliEncoder -> IO (BrotliState, Int))
-> IO (BrotliState, Int)
forall a b. (a -> b) -> a -> b
$ \Ptr BrotliEncoder
encPtr -> do
      ByteString
-> (CStringLen -> IO (BrotliState, Int)) -> IO (BrotliState, Int)
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.unsafeUseAsCStringLen ByteString
ibs ((CStringLen -> IO (BrotliState, Int)) -> IO (BrotliState, Int))
-> (CStringLen -> IO (BrotliState, Int)) -> IO (BrotliState, Int)
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
ibsptr, Int
ibslen) ->
        CSize
-> (Ptr CSize -> IO (BrotliState, Int)) -> IO (BrotliState, Int)
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
ibslen) ((Ptr CSize -> IO (BrotliState, Int)) -> IO (BrotliState, Int))
-> (Ptr CSize -> IO (BrotliState, Int)) -> IO (BrotliState, Int)
forall a b. (a -> b) -> a -> b
$ \Ptr CSize
availIn ->
          Ptr CChar
-> (Ptr (Ptr CChar) -> IO (BrotliState, Int))
-> IO (BrotliState, Int)
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with Ptr CChar
ibsptr ((Ptr (Ptr CChar) -> IO (BrotliState, Int))
 -> IO (BrotliState, Int))
-> (Ptr (Ptr CChar) -> IO (BrotliState, Int))
-> IO (BrotliState, Int)
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr CChar)
nextIn -> do
            CSize
-> (Ptr CSize -> IO (BrotliState, Int)) -> IO (BrotliState, Int)
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with CSize
0 ((Ptr CSize -> IO (BrotliState, Int)) -> IO (BrotliState, Int))
-> (Ptr CSize -> IO (BrotliState, Int)) -> IO (BrotliState, Int)
forall a b. (a -> b) -> a -> b
$ \Ptr CSize
availOut -> do
              BrotliState
rc <- HsBrotliState -> BrotliState
fromHsBrotliState (HsBrotliState -> BrotliState)
-> IO HsBrotliState -> IO BrotliState
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                    Ptr BrotliEncoder
-> HsBrotliState
-> Ptr CSize
-> Ptr (Ptr Word8)
-> Ptr CSize
-> Ptr (Ptr Word8)
-> Ptr CSize
-> IO HsBrotliState
c_BrotliEncoderCompressStream Ptr BrotliEncoder
encPtr HsBrotliState
action Ptr CSize
availIn (Ptr (Ptr CChar) -> Ptr (Ptr Word8)
forall a b. Ptr a -> Ptr b
castPtr Ptr (Ptr CChar)
nextIn) Ptr CSize
availOut Ptr (Ptr Word8)
forall a. Ptr a
nullPtr Ptr CSize
forall a. Ptr a
nullPtr
              Int
availIn' <- CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CSize -> Int) -> IO CSize -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CSize -> IO CSize
forall a. Storable a => Ptr a -> IO a
peek Ptr CSize
availIn
              (BrotliState, Int) -> IO (BrotliState, Int)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BrotliState
rc, Int
availIn')
  where
    action :: HsBrotliState
action = case BrotliEncOp
action0 of
               BrotliEncOp
BrotliEncOpProcess -> HsBrotliState
0
{-# LINE 238 "src/LibBrotli.hsc" #-}
               BrotliEncOp
BrotliEncOpFinish  -> HsBrotliState
2
{-# LINE 239 "src/LibBrotli.hsc" #-}
               BrotliEncOp
BrotliEncOpFlush   -> HsBrotliState
1
{-# LINE 240 "src/LibBrotli.hsc" #-}

runBrotliDecoder :: BrotliDecoder
                 -> ByteString
                 -> ST s (BrotliState,BrotliDecoderErrorCode,Int {- unused-input -})
runBrotliDecoder :: BrotliDecoder
-> ByteString -> ST s (BrotliState, BrotliDecoderErrorCode, Int)
runBrotliDecoder (BrotliDecoder ForeignPtr BrotliDecoder
fp) ByteString
ibs
  = IO (BrotliState, BrotliDecoderErrorCode, Int)
-> ST s (BrotliState, BrotliDecoderErrorCode, Int)
forall a s. IO a -> ST s a
unsafeIOToST (IO (BrotliState, BrotliDecoderErrorCode, Int)
 -> ST s (BrotliState, BrotliDecoderErrorCode, Int))
-> IO (BrotliState, BrotliDecoderErrorCode, Int)
-> ST s (BrotliState, BrotliDecoderErrorCode, Int)
forall a b. (a -> b) -> a -> b
$ ForeignPtr BrotliDecoder
-> (Ptr BrotliDecoder
    -> IO (BrotliState, BrotliDecoderErrorCode, Int))
-> IO (BrotliState, BrotliDecoderErrorCode, Int)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr BrotliDecoder
fp ((Ptr BrotliDecoder
  -> IO (BrotliState, BrotliDecoderErrorCode, Int))
 -> IO (BrotliState, BrotliDecoderErrorCode, Int))
-> (Ptr BrotliDecoder
    -> IO (BrotliState, BrotliDecoderErrorCode, Int))
-> IO (BrotliState, BrotliDecoderErrorCode, Int)
forall a b. (a -> b) -> a -> b
$ \Ptr BrotliDecoder
encPtr -> do
      ByteString
-> (CStringLen -> IO (BrotliState, BrotliDecoderErrorCode, Int))
-> IO (BrotliState, BrotliDecoderErrorCode, Int)
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.unsafeUseAsCStringLen ByteString
ibs ((CStringLen -> IO (BrotliState, BrotliDecoderErrorCode, Int))
 -> IO (BrotliState, BrotliDecoderErrorCode, Int))
-> (CStringLen -> IO (BrotliState, BrotliDecoderErrorCode, Int))
-> IO (BrotliState, BrotliDecoderErrorCode, Int)
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
ibsptr, Int
ibslen) ->
        CSize
-> (Ptr CSize -> IO (BrotliState, BrotliDecoderErrorCode, Int))
-> IO (BrotliState, BrotliDecoderErrorCode, Int)
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
ibslen) ((Ptr CSize -> IO (BrotliState, BrotliDecoderErrorCode, Int))
 -> IO (BrotliState, BrotliDecoderErrorCode, Int))
-> (Ptr CSize -> IO (BrotliState, BrotliDecoderErrorCode, Int))
-> IO (BrotliState, BrotliDecoderErrorCode, Int)
forall a b. (a -> b) -> a -> b
$ \Ptr CSize
availIn ->
          Ptr CChar
-> (Ptr (Ptr CChar)
    -> IO (BrotliState, BrotliDecoderErrorCode, Int))
-> IO (BrotliState, BrotliDecoderErrorCode, Int)
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with Ptr CChar
ibsptr ((Ptr (Ptr CChar) -> IO (BrotliState, BrotliDecoderErrorCode, Int))
 -> IO (BrotliState, BrotliDecoderErrorCode, Int))
-> (Ptr (Ptr CChar)
    -> IO (BrotliState, BrotliDecoderErrorCode, Int))
-> IO (BrotliState, BrotliDecoderErrorCode, Int)
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr CChar)
nextIn -> do
            CSize
-> (Ptr CSize -> IO (BrotliState, BrotliDecoderErrorCode, Int))
-> IO (BrotliState, BrotliDecoderErrorCode, Int)
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with CSize
0 ((Ptr CSize -> IO (BrotliState, BrotliDecoderErrorCode, Int))
 -> IO (BrotliState, BrotliDecoderErrorCode, Int))
-> (Ptr CSize -> IO (BrotliState, BrotliDecoderErrorCode, Int))
-> IO (BrotliState, BrotliDecoderErrorCode, Int)
forall a b. (a -> b) -> a -> b
$ \Ptr CSize
availOut -> do
              BrotliState
rc <- HsBrotliState -> BrotliState
fromHsBrotliState (HsBrotliState -> BrotliState)
-> IO HsBrotliState -> IO BrotliState
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                    Ptr BrotliDecoder
-> Ptr CSize
-> Ptr (Ptr Word8)
-> Ptr CSize
-> Ptr (Ptr Word8)
-> Ptr CSize
-> IO HsBrotliState
c_BrotliDecoderDecompressStream Ptr BrotliDecoder
encPtr Ptr CSize
availIn (Ptr (Ptr CChar) -> Ptr (Ptr Word8)
forall a b. Ptr a -> Ptr b
castPtr Ptr (Ptr CChar)
nextIn) Ptr CSize
availOut Ptr (Ptr Word8)
forall a. Ptr a
nullPtr Ptr CSize
forall a. Ptr a
nullPtr
              Int
availIn' <- CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CSize -> Int) -> IO CSize -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CSize -> IO CSize
forall a. Storable a => Ptr a -> IO a
peek Ptr CSize
availIn
              BrotliDecoderErrorCode
ecode <- Int -> BrotliDecoderErrorCode
BrotliDecoderErrorCode (Int -> BrotliDecoderErrorCode)
-> IO Int -> IO BrotliDecoderErrorCode
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> if BrotliState
rc BrotliState -> BrotliState -> Bool
forall a. Eq a => a -> a -> Bool
== BrotliState
BSFail
                then BrotliBool -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (BrotliBool -> Int) -> IO BrotliBool -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr BrotliDecoder -> IO BrotliBool
c_BrotliDecoderGetErrorCode Ptr BrotliDecoder
encPtr
                else Int -> IO Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
0
              (BrotliState, BrotliDecoderErrorCode, Int)
-> IO (BrotliState, BrotliDecoderErrorCode, Int)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BrotliState
rc, BrotliDecoderErrorCode
ecode, Int
availIn')

readBrotliEncoder :: BrotliEncoder -> Int {- max-output -} -> ST s (BrotliState, ByteString)
readBrotliEncoder :: BrotliEncoder -> Int -> ST s (BrotliState, ByteString)
readBrotliEncoder (BrotliEncoder ForeignPtr BrotliEncoder
fp) Int
maxobs
  = IO (BrotliState, ByteString) -> ST s (BrotliState, ByteString)
forall a s. IO a -> ST s a
unsafeIOToST (IO (BrotliState, ByteString) -> ST s (BrotliState, ByteString))
-> IO (BrotliState, ByteString) -> ST s (BrotliState, ByteString)
forall a b. (a -> b) -> a -> b
$ ForeignPtr BrotliEncoder
-> (Ptr BrotliEncoder -> IO (BrotliState, ByteString))
-> IO (BrotliState, ByteString)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr BrotliEncoder
fp ((Ptr BrotliEncoder -> IO (BrotliState, ByteString))
 -> IO (BrotliState, ByteString))
-> (Ptr BrotliEncoder -> IO (BrotliState, ByteString))
-> IO (BrotliState, ByteString)
forall a b. (a -> b) -> a -> b
$ \Ptr BrotliEncoder
encPtr -> do
      CSize
-> (Ptr CSize -> IO (BrotliState, ByteString))
-> IO (BrotliState, ByteString)
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
maxobs) ((Ptr CSize -> IO (BrotliState, ByteString))
 -> IO (BrotliState, ByteString))
-> (Ptr CSize -> IO (BrotliState, ByteString))
-> IO (BrotliState, ByteString)
forall a b. (a -> b) -> a -> b
$ \Ptr CSize
availOutPtr -> do
        (Ptr (Ptr Word8) -> IO (BrotliState, ByteString))
-> IO (BrotliState, ByteString)
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr Word8) -> IO (BrotliState, ByteString))
 -> IO (BrotliState, ByteString))
-> (Ptr (Ptr Word8) -> IO (BrotliState, ByteString))
-> IO (BrotliState, ByteString)
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr Word8)
obsptrptr -> do
          BrotliState
rc <- HsBrotliState -> BrotliState
fromHsBrotliState (HsBrotliState -> BrotliState)
-> IO HsBrotliState -> IO BrotliState
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                Ptr BrotliEncoder
-> Ptr CSize -> Ptr (Ptr Word8) -> IO HsBrotliState
c_BrotliEncoderTakeOutput Ptr BrotliEncoder
encPtr Ptr CSize
availOutPtr Ptr (Ptr Word8)
obsptrptr
          CSize
availOut' <- Ptr CSize -> IO CSize
forall a. Storable a => Ptr a -> IO a
peek Ptr CSize
availOutPtr
          Ptr Word8
obsptr <- Ptr (Ptr Word8) -> IO (Ptr Word8)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr Word8)
obsptrptr
          ByteString
buf <- Ptr Word8 -> CSize -> IO ByteString
packBS Ptr Word8
obsptr CSize
availOut'
          (BrotliState, ByteString) -> IO (BrotliState, ByteString)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BrotliState
rc, ByteString
buf)

readBrotliDecoder :: BrotliDecoder -> Int {- max-output -} -> ST s (BrotliState, ByteString)
readBrotliDecoder :: BrotliDecoder -> Int -> ST s (BrotliState, ByteString)
readBrotliDecoder (BrotliDecoder ForeignPtr BrotliDecoder
fp) Int
maxobs
  = IO (BrotliState, ByteString) -> ST s (BrotliState, ByteString)
forall a s. IO a -> ST s a
unsafeIOToST (IO (BrotliState, ByteString) -> ST s (BrotliState, ByteString))
-> IO (BrotliState, ByteString) -> ST s (BrotliState, ByteString)
forall a b. (a -> b) -> a -> b
$ ForeignPtr BrotliDecoder
-> (Ptr BrotliDecoder -> IO (BrotliState, ByteString))
-> IO (BrotliState, ByteString)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr BrotliDecoder
fp ((Ptr BrotliDecoder -> IO (BrotliState, ByteString))
 -> IO (BrotliState, ByteString))
-> (Ptr BrotliDecoder -> IO (BrotliState, ByteString))
-> IO (BrotliState, ByteString)
forall a b. (a -> b) -> a -> b
$ \Ptr BrotliDecoder
encPtr -> do
      CSize
-> (Ptr CSize -> IO (BrotliState, ByteString))
-> IO (BrotliState, ByteString)
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
maxobs) ((Ptr CSize -> IO (BrotliState, ByteString))
 -> IO (BrotliState, ByteString))
-> (Ptr CSize -> IO (BrotliState, ByteString))
-> IO (BrotliState, ByteString)
forall a b. (a -> b) -> a -> b
$ \Ptr CSize
availOutPtr -> do
        (Ptr (Ptr Word8) -> IO (BrotliState, ByteString))
-> IO (BrotliState, ByteString)
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr Word8) -> IO (BrotliState, ByteString))
 -> IO (BrotliState, ByteString))
-> (Ptr (Ptr Word8) -> IO (BrotliState, ByteString))
-> IO (BrotliState, ByteString)
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr Word8)
obsptrptr -> do
          BrotliState
rc <- HsBrotliState -> BrotliState
fromHsBrotliState (HsBrotliState -> BrotliState)
-> IO HsBrotliState -> IO BrotliState
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                Ptr BrotliDecoder
-> Ptr CSize -> Ptr (Ptr Word8) -> IO HsBrotliState
c_BrotliDecoderTakeOutput Ptr BrotliDecoder
encPtr Ptr CSize
availOutPtr Ptr (Ptr Word8)
obsptrptr
          CSize
availOut' <- Ptr CSize -> IO CSize
forall a. Storable a => Ptr a -> IO a
peek Ptr CSize
availOutPtr
          Ptr Word8
obsptr <- Ptr (Ptr Word8) -> IO (Ptr Word8)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr Word8)
obsptrptr
          ByteString
buf <- Ptr Word8 -> CSize -> IO ByteString
packBS Ptr Word8
obsptr CSize
availOut'
          (BrotliState, ByteString) -> IO (BrotliState, ByteString)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BrotliState
rc, ByteString
buf)

----------------------------------------------------------------------------

-- | Encoding profile
data CompressionMode
    = CompressionModeGeneric -- ^ Format-agnostic default mode
    | CompressionModeText    -- ^ UTF-8 formatted text data
    | CompressionModeFont    -- ^ Compression mode used in WOFF 2.0
    deriving (CompressionMode -> CompressionMode -> Bool
(CompressionMode -> CompressionMode -> Bool)
-> (CompressionMode -> CompressionMode -> Bool)
-> Eq CompressionMode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CompressionMode -> CompressionMode -> Bool
$c/= :: CompressionMode -> CompressionMode -> Bool
== :: CompressionMode -> CompressionMode -> Bool
$c== :: CompressionMode -> CompressionMode -> Bool
Eq,ReadPrec [CompressionMode]
ReadPrec CompressionMode
Int -> ReadS CompressionMode
ReadS [CompressionMode]
(Int -> ReadS CompressionMode)
-> ReadS [CompressionMode]
-> ReadPrec CompressionMode
-> ReadPrec [CompressionMode]
-> Read CompressionMode
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CompressionMode]
$creadListPrec :: ReadPrec [CompressionMode]
readPrec :: ReadPrec CompressionMode
$creadPrec :: ReadPrec CompressionMode
readList :: ReadS [CompressionMode]
$creadList :: ReadS [CompressionMode]
readsPrec :: Int -> ReadS CompressionMode
$creadsPrec :: Int -> ReadS CompressionMode
Read,Int -> CompressionMode -> ShowS
[CompressionMode] -> ShowS
CompressionMode -> String
(Int -> CompressionMode -> ShowS)
-> (CompressionMode -> String)
-> ([CompressionMode] -> ShowS)
-> Show CompressionMode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CompressionMode] -> ShowS
$cshowList :: [CompressionMode] -> ShowS
show :: CompressionMode -> String
$cshow :: CompressionMode -> String
showsPrec :: Int -> CompressionMode -> ShowS
$cshowsPrec :: Int -> CompressionMode -> ShowS
Show,Typeable)

-- | Compression quality setting
data CompressionLevel
    = CompressionLevel0 -- ^ fastest/lowest compression level
    | CompressionLevel1
    | CompressionLevel2
    | CompressionLevel3
    | CompressionLevel4
    | CompressionLevel5
    | CompressionLevel6
    | CompressionLevel7
    | CompressionLevel8
    | CompressionLevel9
    | CompressionLevel10
    | CompressionLevel11 -- ^ slowest/highest compression level (default)
    deriving (CompressionLevel -> CompressionLevel -> Bool
(CompressionLevel -> CompressionLevel -> Bool)
-> (CompressionLevel -> CompressionLevel -> Bool)
-> Eq CompressionLevel
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
Eq CompressionLevel
-> (CompressionLevel -> CompressionLevel -> Ordering)
-> (CompressionLevel -> CompressionLevel -> Bool)
-> (CompressionLevel -> CompressionLevel -> Bool)
-> (CompressionLevel -> CompressionLevel -> Bool)
-> (CompressionLevel -> CompressionLevel -> Bool)
-> (CompressionLevel -> CompressionLevel -> CompressionLevel)
-> (CompressionLevel -> CompressionLevel -> CompressionLevel)
-> Ord 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
$cp1Ord :: Eq CompressionLevel
Ord,ReadPrec [CompressionLevel]
ReadPrec CompressionLevel
Int -> ReadS CompressionLevel
ReadS [CompressionLevel]
(Int -> ReadS CompressionLevel)
-> ReadS [CompressionLevel]
-> ReadPrec CompressionLevel
-> ReadPrec [CompressionLevel]
-> Read 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
(Int -> CompressionLevel -> ShowS)
-> (CompressionLevel -> String)
-> ([CompressionLevel] -> ShowS)
-> Show CompressionLevel
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]
(CompressionLevel -> CompressionLevel)
-> (CompressionLevel -> CompressionLevel)
-> (Int -> CompressionLevel)
-> (CompressionLevel -> Int)
-> (CompressionLevel -> [CompressionLevel])
-> (CompressionLevel -> CompressionLevel -> [CompressionLevel])
-> (CompressionLevel -> CompressionLevel -> [CompressionLevel])
-> (CompressionLevel
    -> CompressionLevel -> CompressionLevel -> [CompressionLevel])
-> Enum 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,CompressionLevel
CompressionLevel -> CompressionLevel -> Bounded CompressionLevel
forall a. a -> a -> Bounded a
maxBound :: CompressionLevel
$cmaxBound :: CompressionLevel
minBound :: CompressionLevel
$cminBound :: CompressionLevel
Bounded)

-- | Recommended sliding LZ77 window size.
--
-- The encoder may reduce this value (if e.g. input is much smaller than window size).
--
data CompressionWindowSize
    = CompressionWindowBits10 -- ^ 1008 bytes
    | CompressionWindowBits11 -- ^ 2032 bytes
    | CompressionWindowBits12 -- ^ 4080 bytes
    | CompressionWindowBits13 -- ^ 8176 bytes
    | CompressionWindowBits14 -- ^ 16368 bytes
    | CompressionWindowBits15 -- ^ 32752 bytes
    | CompressionWindowBits16 -- ^ 65520 bytes
    | CompressionWindowBits17 -- ^ 131056 bytes
    | CompressionWindowBits18 -- ^ 262128 bytes
    | CompressionWindowBits19 -- ^ 524272 bytes
    | CompressionWindowBits20 -- ^ 1048560 bytes
    | CompressionWindowBits21 -- ^ 2097136 bytes
    | CompressionWindowBits22 -- ^ 4194288 bytes (default)
    | CompressionWindowBits23 -- ^ 8388592 bytes
    | CompressionWindowBits24 -- ^ 16777200 bytes
    deriving (CompressionWindowSize -> CompressionWindowSize -> Bool
(CompressionWindowSize -> CompressionWindowSize -> Bool)
-> (CompressionWindowSize -> CompressionWindowSize -> Bool)
-> Eq CompressionWindowSize
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CompressionWindowSize -> CompressionWindowSize -> Bool
$c/= :: CompressionWindowSize -> CompressionWindowSize -> Bool
== :: CompressionWindowSize -> CompressionWindowSize -> Bool
$c== :: CompressionWindowSize -> CompressionWindowSize -> Bool
Eq,Eq CompressionWindowSize
Eq CompressionWindowSize
-> (CompressionWindowSize -> CompressionWindowSize -> Ordering)
-> (CompressionWindowSize -> CompressionWindowSize -> Bool)
-> (CompressionWindowSize -> CompressionWindowSize -> Bool)
-> (CompressionWindowSize -> CompressionWindowSize -> Bool)
-> (CompressionWindowSize -> CompressionWindowSize -> Bool)
-> (CompressionWindowSize
    -> CompressionWindowSize -> CompressionWindowSize)
-> (CompressionWindowSize
    -> CompressionWindowSize -> CompressionWindowSize)
-> Ord CompressionWindowSize
CompressionWindowSize -> CompressionWindowSize -> Bool
CompressionWindowSize -> CompressionWindowSize -> Ordering
CompressionWindowSize
-> CompressionWindowSize -> CompressionWindowSize
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 :: CompressionWindowSize
-> CompressionWindowSize -> CompressionWindowSize
$cmin :: CompressionWindowSize
-> CompressionWindowSize -> CompressionWindowSize
max :: CompressionWindowSize
-> CompressionWindowSize -> CompressionWindowSize
$cmax :: CompressionWindowSize
-> CompressionWindowSize -> CompressionWindowSize
>= :: CompressionWindowSize -> CompressionWindowSize -> Bool
$c>= :: CompressionWindowSize -> CompressionWindowSize -> Bool
> :: CompressionWindowSize -> CompressionWindowSize -> Bool
$c> :: CompressionWindowSize -> CompressionWindowSize -> Bool
<= :: CompressionWindowSize -> CompressionWindowSize -> Bool
$c<= :: CompressionWindowSize -> CompressionWindowSize -> Bool
< :: CompressionWindowSize -> CompressionWindowSize -> Bool
$c< :: CompressionWindowSize -> CompressionWindowSize -> Bool
compare :: CompressionWindowSize -> CompressionWindowSize -> Ordering
$ccompare :: CompressionWindowSize -> CompressionWindowSize -> Ordering
$cp1Ord :: Eq CompressionWindowSize
Ord,ReadPrec [CompressionWindowSize]
ReadPrec CompressionWindowSize
Int -> ReadS CompressionWindowSize
ReadS [CompressionWindowSize]
(Int -> ReadS CompressionWindowSize)
-> ReadS [CompressionWindowSize]
-> ReadPrec CompressionWindowSize
-> ReadPrec [CompressionWindowSize]
-> Read CompressionWindowSize
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CompressionWindowSize]
$creadListPrec :: ReadPrec [CompressionWindowSize]
readPrec :: ReadPrec CompressionWindowSize
$creadPrec :: ReadPrec CompressionWindowSize
readList :: ReadS [CompressionWindowSize]
$creadList :: ReadS [CompressionWindowSize]
readsPrec :: Int -> ReadS CompressionWindowSize
$creadsPrec :: Int -> ReadS CompressionWindowSize
Read,Int -> CompressionWindowSize -> ShowS
[CompressionWindowSize] -> ShowS
CompressionWindowSize -> String
(Int -> CompressionWindowSize -> ShowS)
-> (CompressionWindowSize -> String)
-> ([CompressionWindowSize] -> ShowS)
-> Show CompressionWindowSize
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CompressionWindowSize] -> ShowS
$cshowList :: [CompressionWindowSize] -> ShowS
show :: CompressionWindowSize -> String
$cshow :: CompressionWindowSize -> String
showsPrec :: Int -> CompressionWindowSize -> ShowS
$cshowsPrec :: Int -> CompressionWindowSize -> ShowS
Show,Typeable,CompressionWindowSize
CompressionWindowSize
-> CompressionWindowSize -> Bounded CompressionWindowSize
forall a. a -> a -> Bounded a
maxBound :: CompressionWindowSize
$cmaxBound :: CompressionWindowSize
minBound :: CompressionWindowSize
$cminBound :: CompressionWindowSize
Bounded)

-- | This 'Enum' instance is offset by 10.
--
-- >>> toEnum CompressionWindowBits10
-- 10
--
-- >>> fromEnum 22 :: CompressionWindowSize
-- CompressionWindowBits22
--
instance Enum CompressionWindowSize where
  toEnum :: Int -> CompressionWindowSize
toEnum Int
i = case Int
i of
    Int
10 -> CompressionWindowSize
CompressionWindowBits10
    Int
11 -> CompressionWindowSize
CompressionWindowBits11
    Int
12 -> CompressionWindowSize
CompressionWindowBits12
    Int
13 -> CompressionWindowSize
CompressionWindowBits13
    Int
14 -> CompressionWindowSize
CompressionWindowBits14
    Int
15 -> CompressionWindowSize
CompressionWindowBits15
    Int
16 -> CompressionWindowSize
CompressionWindowBits16
    Int
17 -> CompressionWindowSize
CompressionWindowBits17
    Int
18 -> CompressionWindowSize
CompressionWindowBits18
    Int
19 -> CompressionWindowSize
CompressionWindowBits19
    Int
20 -> CompressionWindowSize
CompressionWindowBits20
    Int
21 -> CompressionWindowSize
CompressionWindowBits21
    Int
22 -> CompressionWindowSize
CompressionWindowBits22
    Int
23 -> CompressionWindowSize
CompressionWindowBits23
    Int
24 -> CompressionWindowSize
CompressionWindowBits24
    Int
_  -> String -> CompressionWindowSize
forall a. HasCallStack => String -> a
error String
"toEnum(CompressionWindowSize): bad argument"

  fromEnum :: CompressionWindowSize -> Int
fromEnum CompressionWindowSize
x = case CompressionWindowSize
x of
    CompressionWindowSize
CompressionWindowBits10 -> Int
10
    CompressionWindowSize
CompressionWindowBits11 -> Int
11
    CompressionWindowSize
CompressionWindowBits12 -> Int
12
    CompressionWindowSize
CompressionWindowBits13 -> Int
13
    CompressionWindowSize
CompressionWindowBits14 -> Int
14
    CompressionWindowSize
CompressionWindowBits15 -> Int
15
    CompressionWindowSize
CompressionWindowBits16 -> Int
16
    CompressionWindowSize
CompressionWindowBits17 -> Int
17
    CompressionWindowSize
CompressionWindowBits18 -> Int
18
    CompressionWindowSize
CompressionWindowBits19 -> Int
19
    CompressionWindowSize
CompressionWindowBits20 -> Int
20
    CompressionWindowSize
CompressionWindowBits21 -> Int
21
    CompressionWindowSize
CompressionWindowBits22 -> Int
22
    CompressionWindowSize
CompressionWindowBits23 -> Int
23
    CompressionWindowSize
CompressionWindowBits24 -> Int
24

----------------------------------------------------------------------------
-- FFI imports
--
-- encoder

foreign import capi "hs_brotli.h BrotliEncoderCreateInstance"
    c_BrotliEncoderCreateInstance :: Ptr () -> Ptr () -> Ptr () -> IO (Ptr BrotliEncoder)

-- foreign import capi "hs_brotli.h BrotliEncoderDestroyInstance"
--     c_BrotliEncoderDestroyInstance :: Ptr BrotliEncoder -> IO ()

foreign import capi "hs_brotli.h &BrotliEncoderDestroyInstance"
    cp_BrotliEncoderDestroyInstance :: FunPtr (Ptr BrotliEncoder -> IO ())

foreign import capi "hs_brotli.h HsBrotliEncoderCompressStream"
    c_BrotliEncoderCompressStream :: Ptr BrotliEncoder
                                  -> Word32
{-# LINE 390 "src/LibBrotli.hsc" #-}
                                  -> Ptr CSize       {- available_in -}
                                  -> Ptr (Ptr Word8) {- next_in -}
                                  -> Ptr CSize       {- available_out -}
                                  -> Ptr (Ptr Word8) {- next_out -}
                                  -> Ptr CSize       {- total_out -}
                                  -> IO HsBrotliState

-- foreign import capi unsafe "hs_brotli.h BrotliEncoderIsFinished"
--     c_BrotliEncoderIsFinished :: Ptr BrotliEncoder -> IO BrotliBool
--
-- foreign import capi unsafe "hs_brotli.h BrotliEncoderHasMoreOutput"
--     c_BrotliEncoderHasMoreOutput :: Ptr BrotliEncoder -> IO BrotliBool

foreign import capi unsafe "hs_brotli.h HsBrotliEncoderTakeOutput"
    c_BrotliEncoderTakeOutput :: Ptr BrotliEncoder -> Ptr CSize -> Ptr (Ptr Word8) -> IO HsBrotliState

-- e.g. 0x01000000
foreign import capi unsafe "hs_brotli.h BrotliEncoderVersion" brotliEncoderVersion :: Word32


-- BROTLI_BOOL BrotliEncoderSetParameter(BrotliEncoderState *state, BrotliEncoderParameter param, uint32_t value)

foreign import capi unsafe "hs_brotli.h BrotliEncoderSetParameter"
    c_BrotliEncoderSetParameter :: Ptr BrotliEncoder -> Word32 -> Word32 -> IO BrotliBool
{-# LINE 414 "src/LibBrotli.hsc" #-}

----------------------------------------------------------------------------
-- decoder

foreign import capi "hs_brotli.h BrotliDecoderCreateInstance"
    c_BrotliDecoderCreateInstance :: Ptr () -> Ptr () -> Ptr () -> IO (Ptr BrotliDecoder)

-- foreign import capi "hs_brotli.h BrotliDecoderDestroyInstance"
--     c_BrotliDecoderDestroyInstance :: Ptr BrotliDecoder -> IO ()

foreign import capi "hs_brotli.h &BrotliDecoderDestroyInstance"
    cp_BrotliDecoderDestroyInstance :: FunPtr (Ptr BrotliDecoder -> IO ())

foreign import capi "hs_brotli.h HsBrotliDecoderDecompressStream"
    c_BrotliDecoderDecompressStream :: Ptr BrotliDecoder
                                    -> Ptr CSize       {- available_in -}
                                    -> Ptr (Ptr Word8) {- next_in -}
                                    -> Ptr CSize       {- available_out -}
                                    -> Ptr (Ptr Word8) {- next_out -}
                                    -> Ptr CSize       {- total_out -}
                                    -> IO Word32
{-# LINE 435 "src/LibBrotli.hsc" #-}

-- foreign import capi unsafe "hs_brotli.h BrotliDecoderIsFinished"
--     c_BrotliDecoderIsFinished :: Ptr BrotliDecoder -> IO BrotliBool
--
-- foreign import capi unsafe "hs_brotli.h BrotliDecoderHasMoreOutput"
--     c_BrotliDecoderHasMoreOutput :: Ptr BrotliDecoder -> IO BrotliBool

foreign import capi unsafe "hs_brotli.h HsBrotliDecoderTakeOutput"
    c_BrotliDecoderTakeOutput :: Ptr BrotliDecoder -> Ptr CSize -> Ptr (Ptr Word8) -> IO HsBrotliState

-- e.g. 0x01000000
foreign import capi unsafe "hs_brotli.h BrotliDecoderVersion" brotliDecoderVersion :: Word32

foreign import capi unsafe "hs_brotli.h BrotliDecoderSetParameter"
    c_BrotliDecoderSetParameter :: Ptr BrotliDecoder -> Word32 -> Word32 -> IO BrotliBool
{-# LINE 450 "src/LibBrotli.hsc" #-}

foreign import capi unsafe "hs_brotli.h BrotliDecoderGetErrorCode"
    c_BrotliDecoderGetErrorCode :: Ptr BrotliDecoder -> IO Int32
{-# LINE 453 "src/LibBrotli.hsc" #-}

foreign import capi unsafe "hs_brotli.h HsBrotliDecoderErrorString"
    c_BrotliDecoderErrorString :: Int32 -> IO CString
{-# LINE 456 "src/LibBrotli.hsc" #-}