0 | module Codec.Compression.Zlib.Decompress
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.Array.Index
8 | import Data.Buffer.Builder
9 | import Data.Buffer.Core
11 | import System.Zlib.Constant
12 | import System.Zlib.Error
13 | import System.Zlib.Types
28 | inflatechunksize : Nat
29 | inflatechunksize = 32768
54 | decompressWith : {n : Nat}
57 | -> IO (Either (List ZlibError) AnyBuffer)
58 | decompressWith wrapper input =
59 | withDecompressor wrapper $
\decompressor =>
60 | go decompressor 0 (zeroLTE n) []
63 | -> (inputoffset : Nat)
64 | -> (0 offsetbounds : LTE inputoffset n)
66 | -> IO (Either (List ZlibError) AnyBuffer)
67 | go decompressor inputoffset offsetbounds chunks = do
68 | let inputlength : Nat
69 | inputlength = n `minus` inputoffset
70 | output <- mbuffer inflatechunksize
77 | {inputbounds = plusMinusLTE inputoffset n offsetbounds}
81 | {outputbounds = zeroOffsetInBounds inflatechunksize}
86 | consumed' = consumed result
88 | produced' = produced result
90 | nextoffset = inputoffset + consumed'
91 | chunkresult : IO (Either (List ZlibError) (List AnyBuffer))
97 | case isLTE produced' inflatechunksize of
99 | errs <- mkError "Codec.Compression.Zlib.Decompress.decompressWith.go"
100 | "inflate reported producing bytes beyond the output buffer"
102 | Yes producedBounds => do
103 | frozen <- freezeOutput produced' output {bounds = producedBounds}
104 | pure (Right (frozen :: chunks))
105 | Right chunks' <- chunkresult
108 | case status result == (zlibconstantToInt Z_STREAM_END) of
116 | case consumed' == 0 && produced' == 0 of
118 | errs <- mkError "Codec.Compression.Zlib.Decompress.decompressWith.go"
119 | "inflate made no progress: no input bytes were consumed and no output bytes were produced"
122 | case isLTE nextoffset n of
124 | assert_total (go decompressor nextoffset nextbounds chunks')
126 | errs <- mkError "Codec.Compression.Zlib.Decompress.decompressWith.go"
127 | ( "inflate reported consuming bytes beyond the input buffer: "
141 | decompress : {n : Nat}
143 | -> IO (Either (List ZlibError) AnyBuffer)
145 | decompressWith ZlibWrapper
150 | gzipDecompress : {n : Nat}
152 | -> IO (Either (List ZlibError) AnyBuffer)
154 | decompressWith GZipWrapper
160 | rawDecompress : {n : Nat}
162 | -> IO (Either (List ZlibError) AnyBuffer)
164 | decompressWith RawWrapper