Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Codec.hs @ 8c337f87

History | View | Annotate | Download (1.6 kB)

1
{-| Provides interface to the 'zlib' library.
2

    
3
-}
4

    
5
{-
6

    
7
Copyright (C) 2014 Google Inc.
8

    
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
13

    
14
This program is distributed in the hope that it will be useful, but
15
WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
General Public License for more details.
18

    
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
02110-1301, USA.
23

    
24
-}
25

    
26
module Ganeti.Codec
27
  ( compressZlib
28
  , decompressZlib
29
  ) where
30

    
31
import Codec.Compression.Zlib (compress)
32
import qualified Codec.Compression.Zlib.Internal as I
33
import Control.Monad.Error
34
import qualified Data.ByteString.Lazy as BL
35
import qualified Data.ByteString.Lazy.Internal as BL
36
import Data.Monoid (mempty)
37

    
38
-- | Compresses a lazy bytestring.
39
compressZlib :: BL.ByteString -> BL.ByteString
40
compressZlib = compress
41

    
42
-- | Decompresses a lazy bytestring, throwing decoding errors using
43
-- 'throwError'.
44
decompressZlib :: (MonadError e m, Error e) => BL.ByteString -> m BL.ByteString
45
decompressZlib = I.foldDecompressStream
46
                     (liftM . BL.chunk)
47
                     (return mempty)
48
                     (const $ throwError . strMsg . ("Zlib: " ++))
49
                 . I.decompressWithErrors
50
                     I.zlibFormat
51
                     I.defaultDecompressParams