module Codec.Compression.Hopfli.Raw (
Options(Options)
, Format(..)
, compress
) where
import qualified Codec.Compression.GZip as G
import qualified Codec.Compression.Zlib as Z
import qualified Codec.Compression.Zlib.Raw as ZR
import qualified Data.ByteString as B
import Data.ByteString.Internal (fromForeignPtr, toForeignPtr)
import qualified Data.ByteString.Lazy as BL
import Foreign hiding (unsafePerformIO)
import Foreign.C.Types
import Prelude hiding (length)
import System.IO.Unsafe (unsafePerformIO)
data Options = Options { verbose :: CInt
, verbose_more :: CInt
, numiterations :: CInt
, blocksplitting :: CInt
, blocksplittinglast :: CInt
, blocksplittingmax :: CInt } deriving (Show)
instance Storable Options where
sizeOf _ = ((24))
alignment _ = alignment (undefined :: CInt)
peek ptr = do
verbose <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) ptr
verbose_more <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) ptr
numiterations <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) ptr
blocksplitting <- ((\hsc_ptr -> peekByteOff hsc_ptr 12)) ptr
blocksplittinglast <- ((\hsc_ptr -> peekByteOff hsc_ptr 16)) ptr
blocksplittingmax <- ((\hsc_ptr -> peekByteOff hsc_ptr 20)) ptr
return $ Options{..}
poke ptr (Options verbose' verbose_more' numiterations' blocksplitting' blocksplittinglast' blocksplittingmax') = do
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) ptr verbose'
((\hsc_ptr -> pokeByteOff hsc_ptr 4)) ptr verbose_more'
((\hsc_ptr -> pokeByteOff hsc_ptr 8)) ptr numiterations'
((\hsc_ptr -> pokeByteOff hsc_ptr 12)) ptr blocksplitting'
((\hsc_ptr -> pokeByteOff hsc_ptr 16)) ptr blocksplittinglast'
((\hsc_ptr -> pokeByteOff hsc_ptr 20)) ptr blocksplittingmax'
data Format = GZIP | ZLIB | DEFLATE deriving (Show, Eq)
newtype ZopfliFormat = ZopfliFormat CInt
fromFormat :: Format -> ZopfliFormat
fromFormat GZIP = ZopfliFormat 0
fromFormat ZLIB = ZopfliFormat 1
fromFormat DEFLATE = ZopfliFormat 2
foreign import ccall unsafe "zopfli.h ZopfliCompress"
c_zopfli_compress :: Ptr Options -> ZopfliFormat -> Ptr Word8 -> CSize -> Ptr (Ptr Word8) -> Ptr CSize -> IO ()
compress :: Options -> Format -> B.ByteString -> B.ByteString
compress _ GZIP "" = B.concat . BL.toChunks $ G.compress ""
compress _ ZLIB "" = B.concat . BL.toChunks $ Z.compress ""
compress _ DEFLATE "" = B.concat . BL.toChunks $ ZR.compress ""
compress options format input = unsafePerformIO $ do
let (inputFptr, start, length) = toForeignPtr input
withForeignPtr inputFptr $ \inputPtr -> do
alloca $ \outSizePtr -> do
poke outSizePtr 0
alloca $ \optionsPtr -> do
poke optionsPtr options
alloca $ \outPtrPtr -> do
poke outPtrPtr nullPtr
_ <- c_zopfli_compress optionsPtr (fromFormat format) (plusPtr inputPtr start) (fromIntegral length) outPtrPtr outSizePtr
outSize <- peek outSizePtr
resultPtr <- throwIfNull "Zopfli compression failed" (peek outPtrPtr)
resultFptr <- newForeignPtr finalizerFree resultPtr
return $ fromForeignPtr resultFptr 0 (fromIntegral outSize)