    -- GENERATED by C->Haskell Compiler, version 0.28.6 Switcheroo, 25 November 2017 (Haskell)
    -- Edit the ORIGNAL .chs file instead!

    {-# LINE 1 "src/Codec/Compression/BZip/Foreign/Common.chs" #-}
    {-# LANGUAGE DeriveDataTypeable #-}

    module Codec.Compression.BZip.Foreign.Common ( -- * Types
                                                   BZAction (..)
                                                 , BZError (..)
                                                 , BzStream
                                                 -- * Helper
                                                 , bzWrap
                                                 -- * Contributed functions
                                                 , bZ2BzlibVersion
                                                 ) where
    import qualified Foreign.C.String as C2HSImp
    import qualified Foreign.C.Types as C2HSImp
    import qualified Foreign.Ptr as C2HSImp
    import qualified System.IO.Unsafe as C2HSImp

    import Control.Applicative
    import Control.Exception (Exception, throw)
    import Data.Typeable (Typeable)
    import Foreign.C.Types (CInt)

    data BZAction = BzRun
                  | BzFlush
                  | BzFinish
    instance Enum BZAction where
      succ :: BZAction -> BZAction
    succ BzRun = BZAction
    BzFlush
      succ BzFlush = BZAction
    BzFinish
      succ BzFinish = error "BZAction.succ: BzFinish has no successor"

      pred :: BZAction -> BZAction
    pred BzFlush = BZAction
    BzRun
      pred BzFinish = BzFlush
      pred BzRun = [Char] -> BZAction
    forall a. HasCallStack => [Char] -> a
    error "BZAction.pred: BzRun has no predecessor"

      enumFromTo from to = go from
        where
          end = fromEnum to
          go :: t -> [t]
    go v :: t
    v = case Int -> Int -> Ordering
    forall a. Ord a => a -> a -> Ordering
    compare (t -> Int
    forall a. Enum a => a -> Int
    fromEnum t
    v) Int
    end of
                     LT -> t
    v t -> [t] -> [t]
    forall a. a -> [a] -> [a]
    : t -> [t]
    go (t -> t
    forall a. Enum a => a -> a
    succ t
    v)
                     EQ -> [t
    v]
                     GT -> []

      enumFrom :: BZAction -> [BZAction]
    enumFrom from :: BZAction
    from = BZAction -> BZAction -> [BZAction]
    forall a. Enum a => a -> a -> [a]
    enumFromTo BZAction
    from BZAction
    BzFinish

      fromEnum BzRun = 0
      fromEnum BzFlush = 1
      fromEnum BzFinish = 2

      toEnum 0 = BzRun
      toEnum 1 = BzFlush
      toEnum 2 = BzFinish
      toEnum unmatched = error ("BZAction.toEnum: Cannot match " ++ show unmatched)

    {-# LINE 24 "src/Codec/Compression/BZip/Foreign/Common.chs" #-}

    data BZError = BzConfigError
                 | BzOutbuffFull
                 | BzUnexpectedEof
                 | BzIoError
                 | BzDataErrorMagic
                 | BzDataError
                 | BzMemError
                 | BzParamError
                 | BzSequenceError
                 | BzOk
                 | BzRunOk
                 | BzFlushOk
                 | BzFinishOk
                 | BzStreamEnd
      deriving (Eq,Show,Typeable)
    instance Enum BZError where
      succ BzConfigError = BzOutbuffFull
      succ BzOutbuffFull = BzUnexpectedEof
      succ BzUnexpectedEof = BzIoError
      succ BzIoError = BzDataErrorMagic
      succ BzDataErrorMagic = BzDataError
      succ BzDataError = BzMemError
      succ BzMemError = BzParamError
      succ BzParamError = BzSequenceError
      succ BzSequenceError = BzOk
      succ BzOk = BzRunOk
      succ BzRunOk = BzFlushOk
      succ BzFlushOk = BzFinishOk
      succ BzFinishOk = BzStreamEnd
      succ BzStreamEnd = error "BZError.succ: BzStreamEnd has no successor"

      pred BzOutbuffFull = BzConfigError
      pred BzUnexpectedEof = BzOutbuffFull
      pred BzIoError = BzUnexpectedEof
      pred BzDataErrorMagic = BzIoError
      pred BzDataError = BZError
    BzDataErrorMagic
      pred BzMemError = BZError
    BzDataError
      pred BzParamError = BZError
    BzMemError
      pred BzSequenceError = BZError
    BzParamError
      pred BzOk = BZError
    BzSequenceError
      pred BzRunOk = BZError
    BzOk
      pred BzFlushOk = BZError
    BzRunOk
      pred BzFinishOk = BZError
    BzFlushOk
      pred BzStreamEnd = BZError
    BzFinishOk
      pred BzConfigError = [Char] -> BZError
    forall a. HasCallStack => [Char] -> a
    error "BZError.pred: BzConfigError has no predecessor"

      enumFromTo :: BZError -> BZError -> [BZError]
    enumFromTo from :: BZError
    from to :: BZError
    to = BZError -> [BZError]
    forall t. Enum t => t -> [t]
    go BZError
    from
        where
          end :: Int
    end = BZError -> Int
    forall a. Enum a => a -> Int
    fromEnum BZError
    to
          go :: t -> [t]
    go v :: t
    v = case Int -> Int -> Ordering
    forall a. Ord a => a -> a -> Ordering
    compare (t -> Int
    forall a. Enum a => a -> Int
    fromEnum t
    v) Int
    end of
                     LT -> t
    v t -> [t] -> [t]
    forall a. a -> [a] -> [a]
    : t -> [t]
    go (t -> t
    forall a. Enum a => a -> a
    succ t
    v)
                     EQ -> [t
    v]
                     GT -> []

      enumFrom :: BZError -> [BZError]
    enumFrom from :: BZError
    from = BZError -> BZError -> [BZError]
    forall a. Enum a => a -> a -> [a]
    enumFromTo BZError
    from BZError
    BzStreamEnd

      fromEnum :: BZError -> Int
    fromEnum BzConfigError = (-9)
      fromEnum BzOutbuffFull = (-8)
      fromEnum BzUnexpectedEof = (-7)
      fromEnum BzIoError = (-6)
      fromEnum BzDataErrorMagic = (-5)
      fromEnum BzDataError = (-4)
      fromEnum BzMemError = (-3)
      fromEnum BzParamError = (-2)
      fromEnum BzSequenceError = (-1)
      fromEnum BzOk = 0
      fromEnum BzRunOk = 1
      fromEnum BzFlushOk = 2
      fromEnum BzFinishOk = 3
      fromEnum BzStreamEnd = 4

      toEnum :: Int -> BZError
    toEnum (-9) = BZError
    BzConfigError
      toEnum (-8) = BZError
    BzOutbuffFull
      toEnum (-7) = BZError
    BzUnexpectedEof
      toEnum (-6) = BZError
    BzIoError
      toEnum (-5) = BZError
    BzDataErrorMagic
      toEnum (-4) = BZError
    BzDataError
      toEnum (-3) = BZError
    BzMemError
      toEnum (-2) = BZError
    BzParamError
      toEnum (-1) = BZError
    BzSequenceError
      toEnum 0 = BZError
    BzOk
      toEnum 1 = BZError
    BzRunOk
      toEnum 2 = BZError
    BzFlushOk
      toEnum 3 = BZError
    BzFinishOk
      toEnum 4 = BZError
    BzStreamEnd
      toEnum unmatched :: Int
    unmatched = [Char] -> BZError
    forall a. HasCallStack => [Char] -> a
    error ("BZError.toEnum: Cannot match " [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Int -> [Char]
    forall a. Show a => a -> [Char]
    show Int
    unmatched)

    {-# LINE 41 "src/Codec/Compression/BZip/Foreign/Common.chs" #-}

    instance Exception BZError where

    -- | Abstract type
    data BzStream

    -- Contributed functions
    bZ2BzlibVersion :: (String)
    bZ2BzlibVersion =
      C2HSImp.unsafePerformIO $
      bZ2BzlibVersion'_ >>= \res ->
      C2HSImp.peekCString res >>= \res' ->
      return (res')

    {-# LINE 49 "src/Codec/Compression/BZip/Foreign/Common.chs" #-}

    bzWrap :: CInt -> IO BZError
    bzWrap err =
        let err' = toEnum (fromIntegral err) in
        case err' of
            BzOk        -> pure err'
            BzRunOk     -> pure err'
            BzFlushOk   -> pure err'
            BzFinishOk  -> pure err'
            BzStreamEnd -> pure err'
            x           -> throw x

    foreign import ccall safe "Codec/Compression/BZip/Foreign/Common.chs.h BZ2_bzlibVersion"
      bZ2BzlibVersion'_ :: (IO (C2HSImp.Ptr C2HSImp.CChar))