about summary refs log tree commit diff homepage
path: root/deflate.3
blob: 7df313eec20b3e16b2a78feb6bc9ada513fbba6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
.Dd January 15, 2017
.Dt DEFLATE 3
.Os
.
.Sh NAME
.Nm deflate
.Nd deflate compression
.
.Sh LIBRARY
.Lb libz
.
.Sh SYNOPSIS
.In zlib.h
.Ft int
.Fn deflate "z_streamp strm" "int flush"
.
.Sh DESCRIPTION
.Fn deflate
compresses as much data as possible,
and stops when the input buffer becomes empty
or the output buffer becomes full.
It may introduce some output latency
(reading input without producing any output)
except when forced the flush.
.
.Pp
The detailed semantics are as follows.
.Fn deflate
performs one or both of the following actions:
.
.Bl -dash
.It
Compress more input starting at
.Fa next_in
and update
.Fa next_in
and
.Fa avail_in
accordingly.
If not all input can be processed
(because there is not enough room in the output buffer),
.Fa next_in
and
.Fa avail_in
are updated
and processing will resume at this point
for the next call of
.Fn deflate .
.
.It
Generate more output starting at
.Fa next_out
and update
.Fa next_out
and
.Fa avail_out
accordingly.
This action is forced if the parameter
.Fa flush
is non-zero.
Forcing flush frequently degrades the compression ratio,
so this parameter should be set only when necessary.
Some output may be provided even if
.Fa flush
is zero.
.El
.
.Pp
Before the call of
.Fn deflate ,
the application should ensure that
at least one of the actions is possible,
by providing more input
and/or consuming more output,
and updating
.Fa avail_in
or
.Fa avail_out
accordingly;
.Fa avail_out
should never be zero before the call.
The application can consume the compressed output
when it wants,
for example when the output buffer is full
.Po
.Fa avail_out
== 0
.Pc ,
or after each call of
.Fn deflate .
If
.Fn deflate
returns
.Dv Z_OK
and with zero
.Fa avail_out ,
it must be called again after making room in the output buffer
because there might be more output pending.
See
.Xr deflatePending 3 ,
which can be used if desired to determine
whether or not there is more output in that case.
.
.Pp
Normally the parameter
.Fa flush
is set to
.Dv Z_NO_FLUSH ,
which allows
.Fn deflate
to decide how much data to accumulate before producing output,
in order to maximize compression.
.
.Pp
If the parameter
.Fa flush
is set to
.Dv Z_SYNC_FLUSH ,
all pending output is flushed to the output buffer
and the output is aligned on a byte boundary,
so that the decompressor can get all input data available so far.
.Po
In particular
.Fa avail_in
is zero after the call if enough output space
has been provided before the call.
.Pc \&
Flushing may degrade compression for some compression algorithms
and so it should be used only when necessary.
This completes the current deflate block
and follows it with an empty stored block
that is three bits plus filler bits to the next byte,
followed by four bytes
(00 00 ff ff).
.
.Pp
If
.Fa flush
is set to
.Dv Z_PARTIAL_FLUSH ,
all pending output is flushed to the output buffer,
but the output is not aligned to a byte boundary.
All of the input data so far will be available to the decompressor,
as for
.Dv Z_SYNC_FLUSH .
This completes the current deflate block
and follows it with an empty fixed codes block
that is 10 bits long.
This assures that enough bytes are output
in order for the decompressor to finish the block
before the empty fixed codes block.
.
.Pp
If
.Fa flush
is set to
.Dv Z_BLOCK ,
a deflate block is completed and emitted,
as for
.Dv Z_SYNC_FLUSH ,
but the output is not aligned on a byte boundary,
and up to seven bits of the current block
are held to be written as the next byte
after the next deflate block is completed.
In this case,
the decompressor may not be provided enough bits
at this point in order to complete decompression
of the data provided so far to the compressor.
It may need to wait for the next block to be emitted.
This is for advanced applications
that need to control the emission of deflate blocks.
.
.Pp
If
.Fa flush
is set to
.Dv Z_FULL_FLUSH ,
all output is flushed as with
.Dv Z_SYNC_FLUSH ,
and the compression state is reset
so that decompression can restart from this point
if previous compressed data has been damaged
or if random access is desired.
Using
.Dv Z_FULL_FLUSH
too often can seriously degrade compression.
.
.Pp
If
.Fn deflate
returns with
.Fa avail_out
== 0,
this function must be called again
with the same value of the
.Fa flush
parameter
and more output space
.Po
updated
.Fa avail_out
.Pc ,
until the flush is complete
.Po
.Fn deflate
returns with non-zero
.Fa avail_out
.Pc .
In the case of a
.Dv Z_FULL_FLUSH
or
.Dv Z_SYNC_FLUSH ,
make sure that
.Fa avail_out
is greater than six
to avoid repeated flush markers
due to
.Fa avail_out
== 0
on return.
.
.Pp
If the parameter
.Fa flush
is set to
.Dv Z_FINISH ,
pending input is processed,
pending output is flushed and
.Fn deflate
returns with
.Dv Z_STREAM_END
if there was enough output space.
If
.Fn deflate
returns with
.Dv Z_OK
or
.Dv Z_BUF_ERROR ,
this function must be called again with
.Dv Z_FINISH
and more output space
.Pq updated Fa avail_out
but no more input data,
until it returns with
.Dv Z_STREAM_END
or an error.
After
.Fn deflate
has returned
.Dv Z_STREAM_END ,
the only possible operations on the stream are
.Xr deflateReset 3
or
.Xr deflateEnd 3 .
.
.Pp
.Dv Z_FINISH
can be used in the first
.Fn deflate
call after
.Xr deflateInit 3
if all the compression is to be done in a single step.
In order to complete in one call,
.Fa avail_out
must be at least the value returned by
.Xr deflateBound 3 .
Then
.Fn deflate
is guaranteed to return
.Dv Z_STREAM_END .
If not enough output space is provided,
.Fn deflate
will not return
.Dv Z_STREAM_END ,
and it must be called again as described above.
.
.Pp
.Fn deflate
sets
.Fa strm->adler
to the Adler-32 checksum
of all input read so far
.Po
that is,
.Fa total_in
bytes
.Pc .
If a gzip stream is being generated,
then
.Fa strm->adler
will be the CRC-32 checksum of the input read so far.
See
.Xr deflateInit2 3 .
.
.Pp
.Fn deflate
may update
.Fa strm->data_type
if it can make a good guess
about the input data type
.Po
.Dv Z_BINARY
or
.Dv Z_TEXT
.Pc .
If in doubt,
the date is considered binary.
This field is only for information purposes
and does not affect the compression algorithm in any manner.
.
.Sh RETURN VALUES
.Fn deflate
returns
.Dv Z_OK
if some progress has been made
(more input processed or more output produced),
.Dv Z_STREAM_END
if all input has been consumed
and all output has been produced
.Po
only when
.Fa flush
is set to
.Dv Z_FINISH
.Pc ,
.Dv Z_STREAM_ERROR
if the stream state was inconsistent
.Po
for example if
.Fa next_in
or
.Fa next_out
was
.Dv Z_NULL
or the state was inadvertently written over
by the application
.Pc ,
or
.Dv Z_BUF_ERROR
if no progress is possible
.Po
for example
.Fa avail_in
or
.Fa avail_out
was zero
.Pc .
Note that
.Dv Z_BUF_ERROR
is not fatal,
and
.Fn deflate
can be called again with more input and more output space
to continue compressing.
.
.Sh SEE ALSO
.Xr deflateEnd 3 ,
.Xr deflateInit 3 ,
.Xr deflatePending 3 ,
.Xr inflate 3
.
.Sh HISTORY
This manual page was converted from
.In zlib.h
to mdoc format by
.An C. McEnroe Aq Mt june@causal.agency .
.
.Sh AUTHORS
.An Jean-loup Gailly Aq Mt jloup@gzip.org
.An Mark Adler Aq Mt madler@alumni.caltech.edu