0 | module Codec.Compression.Zlib.Compress
  1 |
  2 | import Codec.Compression.Zlib.Internal
  3 | import Codec.Compression.Zlib.Proofs
  4 | import Codec.Compression.Zlib.Types
  5 | import Codec.Compression.Zlib.Utilities
  6 | import Data.Buffer
  7 | import Data.Buffer.Core
  8 | import Data.Linear.Token
  9 | import System.Zlib.Constant
 10 | import System.Zlib.Error
 11 | import System.Zlib.Types
 12 |
 13 | %default total
 14 |
 15 | --------------------------------------------------------------------------------
 16 | --          Compression
 17 | --------------------------------------------------------------------------------
 18 |
 19 | ||| Compress an immutable byte buffer using the requested compression level
 20 | ||| and wrapper format.
 21 | |||
 22 | ||| The compressor is acquired and released using `withCompressor`, ensuring
 23 | ||| that the underlying zlib stream is finalized and freed even if compression
 24 | ||| fails.
 25 | |||
 26 | ||| The output buffer is allocated once using `deflateBound`, which computes
 27 | ||| sufficient capacity for compressing the complete input with `Finish`.
 28 | ||| Compression therefore requires a single call to `deflateBuffer`.
 29 | |||
 30 | ||| Input and output bytes are passed to zlib without intermediate copies.
 31 | ||| On the Chez backend, their underlying bytevectors are borrowed directly by
 32 | ||| the C shim for the duration of the foreign call.
 33 | |||
 34 | ||| The returned `AnyBuffer` contains exactly the number of bytes produced by
 35 | ||| zlib, although its underlying allocation may be larger because the used
 36 | ||| prefix is wrapped without copying.
 37 | |||
 38 | ||| Inputs too large to be represented by zlib's one-shot `avail_in` /
 39 | ||| `avail_out` interface are rejected. Large-input support is provided by the
 40 | ||| incremental compression API rather than by this one-shot operation.
 41 | |||
 42 | export
 43 | compressWith :  {n : Nat}
 44 |              -> ZlibCompressionLevel
 45 |              -> Wrapper
 46 |              -> IBuffer n
 47 |              -> IO (Either (List ZlibError) AnyBuffer)
 48 | compressWith level wrapper input =
 49 |   withCompressor level wrapper $ \compressor => do
 50 |     Right capacity <- deflateBound compressor n
 51 |       | Left errs =>
 52 |           pure (Left errs)
 53 |     output       <- mbuffer capacity
 54 |     Right result <-
 55 |       deflateBuffer
 56 |         compressor
 57 |         0
 58 |         n
 59 |         input
 60 |         {inputbounds = zeroOffsetInBounds n}
 61 |         0
 62 |         capacity
 63 |         output
 64 |         {outputbounds = zeroOffsetInBounds capacity}
 65 |         Finish
 66 |       | Left errs =>
 67 |           pure (Left errs)
 68 |     case status result == (zlibconstantToInt Z_STREAM_END) of
 69 |       False => do
 70 |         errs <- mkError "Codec.Compression.Zlib.Compress.compressWith"
 71 |                         ( "Expected Z_STREAM_END after single-pass compression, but zlib returned "
 72 |                             ++ show (status result)
 73 |                         )
 74 |         pure (Left errs)
 75 |       True =>
 76 |         case consumed result == n of
 77 |           False => do
 78 |             errs <- mkError "Codec.Compression.Zlib.Compress.compressWith"
 79 |                             ( "zlib completed compression without consuming the complete input: "
 80 |                                 ++ show (consumed result)
 81 |                                 ++ " of "
 82 |                                 ++ show n
 83 |                                 ++ " bytes consumed"
 84 |                             )
 85 |             pure (Left errs)
 86 |           True =>
 87 |             case isLTE (produced result) capacity of
 88 |               No _               => do
 89 |                 errs <- mkError "Codec.Compression.Zlib.Compress.compressWith"
 90 |                                 "deflate reported producing bytes beyond the output buffer"
 91 |                 pure (Left errs)
 92 |               Yes producedBounds => do
 93 |                 frozen <- freezeOutput (produced result) output {bounds = producedBounds}
 94 |                 pure (Right frozen)
 95 |
 96 | --------------------------------------------------------------------------------
 97 | --          Convenience Operations
 98 | --------------------------------------------------------------------------------
 99 |
100 | ||| Compress an immutable byte buffer using the standard zlib wrapper and
101 | ||| zlib's default compression level.
102 | |||
103 | export
104 | compress :  {n : Nat}
105 |          -> IBuffer n
106 |          -> IO (Either (List ZlibError) AnyBuffer)
107 | compress =
108 |   compressWith DefaultCompression ZlibWrapper
109 |
110 | ||| Compress an immutable byte buffer using the gzip wrapper and zlib's
111 | ||| default compression level.
112 | |||
113 | export
114 | gzipCompress :  {n : Nat}
115 |              -> IBuffer n
116 |              -> IO (Either (List ZlibError) AnyBuffer)
117 | gzipCompress =
118 |   compressWith DefaultCompression GZipWrapper
119 |
120 | ||| Compress an immutable byte buffer as a raw DEFLATE stream with no zlib or
121 | ||| gzip wrapper, using zlib's default compression level.
122 | |||
123 | export
124 | rawCompress :  {n : Nat}
125 |             -> IBuffer n
126 |             -> IO (Either (List ZlibError) AnyBuffer)
127 | rawCompress =
128 |   compressWith DefaultCompression RawWrapper
129 |