0 | module System.Zlib.Raw
  1 |
  2 | import Data.Buffer
  3 |
  4 | %default total
  5 |
  6 | --------------------------------------------------------------------------------
  7 | --          Stream Allocation
  8 | --------------------------------------------------------------------------------
  9 |
 10 | ||| Allocates a new opaque zlib stream handle.
 11 | |||
 12 | ||| The returned pointer refers to storage owned by the C shim. The stream must
 13 | ||| eventually be released with `prim__freeStream`.
 14 | |||
 15 | ||| This function only allocates the stream structure; it does not initialize
 16 | ||| the stream for compression or decompression.
 17 | |||
 18 | export %foreign "C:zlib_idris_stream_new,zlib-idris"
 19 | prim__newStream : PrimIO AnyPtr
 20 |
 21 | ||| Releases an opaque zlib stream handle previously allocated with
 22 | ||| `prim__newStream`.
 23 | |||
 24 | ||| Any initialized compression or decompression state must first be finalized
 25 | ||| with `prim__deflateEnd` or `prim__inflateEnd`, respectively.
 26 | |||
 27 | export %foreign "C:zlib_idris_stream_free,zlib-idris"
 28 | prim__freeStream : AnyPtr -> PrimIO ()
 29 |
 30 | --------------------------------------------------------------------------------
 31 | --          Library Information
 32 | --------------------------------------------------------------------------------
 33 |
 34 | ||| Returns the version string of the zlib implementation linked by the
 35 | ||| `zlib-idris` C shim.
 36 | |||
 37 | export %foreign "C:zlib_idris_version,zlib-idris"
 38 | prim__zlibVersion : PrimIO String
 39 |
 40 | --------------------------------------------------------------------------------
 41 | --          Compression
 42 | --------------------------------------------------------------------------------
 43 |
 44 | ||| Initializes an opaque stream for compression using zlib's default method,
 45 | ||| window size, memory level, and compression strategy.
 46 | |||
 47 | ||| The supplied integer is a zlib compression level, such as
 48 | ||| `Z_DEFAULT_COMPRESSION`.
 49 | |||
 50 | ||| Unlike zlib's `deflateInit_`, the zlib version and `z_stream` structure
 51 | ||| size are supplied by the C shim, preventing ABI-sensitive details from
 52 | ||| crossing the Idris FFI boundary.
 53 | |||
 54 | export %foreign "C:zlib_idris_deflate_init,zlib-idris"
 55 | prim__deflateInit : AnyPtr -> Int -> PrimIO Int
 56 |
 57 | ||| Initializes an opaque stream for compression with explicit zlib
 58 | ||| configuration.
 59 | |||
 60 | ||| The arguments following the stream are, in order:
 61 | |||
 62 | ||| - compression level
 63 | ||| - compression method
 64 | ||| - window bits
 65 | ||| - memory level
 66 | ||| - compression strategy
 67 | |||
 68 | ||| The C shim supplies `ZLIB_VERSION` and `sizeof(z_stream)` directly when
 69 | ||| invoking `deflateInit2`, keeping those ABI details out of Idris.
 70 | |||
 71 | export %foreign "C:zlib_idris_deflate_init2,zlib-idris"
 72 | prim__deflateInit2 : AnyPtr -> Int -> Int -> Int -> Int -> Int -> PrimIO Int
 73 |
 74 | ||| Compresses bytes from an Idris `Buffer` into another Idris `Buffer`.
 75 | |||
 76 | export %foreign "C:zlib_idris_deflate_buffer,zlib-idris"
 77 | prim__deflateBuffer : AnyPtr -> Buffer -> Bits32 -> Bits32 -> Buffer -> Bits32 -> Bits32 -> Int -> PrimIO Int
 78 |
 79 | ||| Releases all zlib resources associated with an initialized compression
 80 | ||| stream.
 81 | |||
 82 | ||| This does not free the opaque stream handle itself. Call
 83 | ||| `prim__freeStream` afterwards to release that storage.
 84 | |||
 85 | export %foreign "C:zlib_idris_deflate_end,zlib-idris"
 86 | prim__deflateEnd : AnyPtr -> PrimIO Int
 87 |
 88 | ||| Resets an initialized compression stream while preserving its allocated
 89 | ||| zlib state so that it can be reused for another compression operation.
 90 | |||
 91 | export %foreign "C:zlib_idris_deflate_reset,zlib-idris"
 92 | prim__deflateReset : AnyPtr -> PrimIO Int
 93 |
 94 | ||| Return an upper bound on the output capacity required to compress the
 95 | ||| supplied number of input bytes using an initialized deflate stream.
 96 | |||
 97 | ||| `UINT64_MAX` is returned by the C shim when the input size or resulting
 98 | ||| bound cannot be represented safely by the one-shot zlib buffer interface.
 99 | |||
100 | export %foreign "C:zlib_idris_deflate_bound,zlib-idris"
101 | prim__deflateBound : AnyPtr -> Bits64 -> PrimIO Bits64
102 |
103 | --------------------------------------------------------------------------------
104 | --          Decompression
105 | --------------------------------------------------------------------------------
106 |
107 | ||| Initializes an opaque stream for decompression using the standard zlib
108 | ||| wrapper format.
109 | |||
110 | ||| `ZLIB_VERSION` and `sizeof(z_stream)` are supplied by the C shim rather
111 | ||| than crossing the Idris FFI boundary.
112 | |||
113 | export %foreign "C:zlib_idris_inflate_init,zlib-idris"
114 | prim__inflateInit : AnyPtr -> PrimIO Int
115 |
116 | ||| Initializes an opaque stream for decompression with explicit `windowBits`.
117 | |||
118 | ||| This is used to select standard zlib, gzip, or raw DEFLATE streams.
119 | ||| `ZLIB_VERSION` and `sizeof(z_stream)` are supplied by the C shim.
120 | |||
121 | export %foreign "C:zlib_idris_inflate_init2,zlib-idris"
122 | prim__inflateInit2 : AnyPtr -> Int -> PrimIO Int
123 |
124 | ||| Decompresses bytes from an Idris `Buffer` into another Idris `Buffer`.
125 | |||
126 | export %foreign "C:zlib_idris_inflate_buffer,zlib-idris"
127 | prim__inflateBuffer : AnyPtr -> Buffer -> Bits32 -> Bits32 -> Buffer -> Bits32 -> Bits32 -> Int -> PrimIO Int
128 |
129 | ||| Releases all zlib resources associated with an initialized decompression
130 | ||| stream.
131 | |||
132 | ||| This does not free the opaque stream handle itself. Call
133 | ||| `prim__freeStream` afterwards to release that storage.
134 | |||
135 | export %foreign "C:zlib_idris_inflate_end,zlib-idris"
136 | prim__inflateEnd : AnyPtr -> PrimIO Int
137 |
138 | ||| Resets an initialized decompression stream while retaining its allocated
139 | ||| zlib state so that it can be reused.
140 | |||
141 | export
142 | %foreign "C:zlib_idris_inflate_reset,zlib-idris"
143 | prim__inflateReset : AnyPtr -> PrimIO Int
144 |
145 | --------------------------------------------------------------------------------
146 | --          Buffer Progress
147 | --------------------------------------------------------------------------------
148 |
149 | ||| Returns the number of input bytes consumed by the most recent
150 | ||| `prim__deflateBuffer` or `prim__inflateBuffer` call.
151 | |||
152 | export %foreign "C:zlib_idris_consumed,zlib-idris"
153 | prim__consumed : AnyPtr -> PrimIO Bits32
154 |
155 | ||| Returns the number of output bytes produced by the most recent
156 | ||| `prim__deflateBuffer` or `prim__inflateBuffer` call.
157 | |||
158 | export %foreign "C:zlib_idris_produced,zlib-idris"
159 | prim__produced : AnyPtr -> PrimIO Bits32
160 |
161 | --------------------------------------------------------------------------------
162 | --          Checksums
163 | --------------------------------------------------------------------------------
164 |
165 | ||| Update a CRC-32 checksum using the contents of an Idris `Buffer`.
166 | |||
167 | ||| The checksum value and result are represented as fixed-width 32-bit
168 | ||| values even on platforms where zlib's `uLong` is wider.
169 | |||
170 | export %foreign "C:zlib_idris_crc32,zlib-idris"
171 | prim__crc32 : Bits32 -> Buffer -> Bits32 -> PrimIO Bits32
172 |
173 | ||| Update an Adler-32 checksum using the contents of an Idris `Buffer`.
174 | |||
175 | ||| The checksum value and result are represented as fixed-width 32-bit
176 | ||| values even on platforms where zlib's `uLong` is wider.
177 | |||
178 | export %foreign "C:zlib_idris_adler32,zlib-idris"
179 | prim__adler32 : Bits32 -> Buffer -> Bits32 -> PrimIO Bits32
180 |