about summary refs log tree commit diff homepage
path: root/deflate.3
blob: 96b2921ba17003c4e112d3305832482cecbf6b6b (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
.Dd November 9, 2018
.Dt DEFLATE 3
.Os
.
.Sh NAME
.Nm deflate ,
.Nm deflateInit ,
.Nm deflateEnd
.Nd deflate compression
.
.Sh LIBRARY
.Lb libz
.
.Sh SYNOPSIS
.In zlib.h
.Ft int
.Fn deflateInit "z_streamp strm" "int level"
.Ft int
.Fn deflate "z_streamp strm" "int flush"
.Ft int
.Fn deflateEnd "z_streamp strm"
.
.Sh DESCRIPTION
The
.Vt z_streamp
type is defined as follows:
.
.Bd -literal -offset indent
typedef voidpf (*alloc_func) (voidpf opaque, uInt items, uInt size);
typedef void   (*free_func)  (voidpf opaque, voidpf address);

typedef struct z_stream_s {
	z_const Bytef *next_in;
	uInt     avail_in;
	uLong    total_in;

	Bytef    *next_out;
	uInt     avail_out;
	uLong    total_out;

	z_const char *msg;
	struct internal_state FAR *state;

	alloc_func zalloc;
	free_func  zfree;
	voidpf     opaque;

	int     data_type;
	uLong   adler;
	uLong   reserved;
} z_stream;

typedef z_stream FAR *z_streamp;
.Ed
.
.Pp
The fields of
.Vt z_stream
are as follows:
.
.Bl -tag -width "data_type"
.It Fa next_in
next input byte
.It Fa avail_in
number of bytes available at
.Fa next_in
.It Fa total_in
total number of input bytes read so far
.It Fa next_out
next output byte will go here
.It Fa avail_out
remaining free space at
.Fa next_out
.It Fa total_out
total number of bytes output so far
.It Fa msg
last error message,
.Dv NULL
if no error
.It Fa state
not visible by applications
.It Fa zalloc
used to allocate the internal state
.It Fa zfree
used to free the internal state
.It Fa opaque
private data object passed to
.Fa zalloc
and
.Fa zfree
.It data_type
best guess about the data type:
binary or text for
.Fn deflate ,
or the decoding state for
.Xr inflate 3
.It adler
Adler-32 or CRC-32 value of the uncompressed data
.It reserved
reserved for future use
.El
.
.Pp
.Fn deflateInit
initializes the internal stream state for compression.
The fields
.Fa zalloc ,
.Fa zfree
and
.Fa opaque
must be initialized before by the caller.
If
.Fa zalloc
and
.Fa zfree
are set to
.Dv Z_NULL ,
.Fn deflateInit
updates them to use default allocation functions.
.Fn deflateInit
does not perform any compression:
this will be done by
.Fn deflate .
.
.Pp
The compression
.Fa level
must be
.Dv Z_DEFAULT_COMPRESSION ,
or between 0 and 9:
1 gives best speed,
9 gives best compression,
0 gives no compression at all
(the input data is simply copied a block at a time).
.Dv Z_DEFAULT_COMPRESSION
requests a default compromise between speed and compression
(currently equivalent to level 6).
.
.Pp
.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
.Fn deflateEnd .
.
.Pp
.Dv Z_FINISH
can be used in the first
.Fn deflate
call after
.Fn deflateInit
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.
.
.Pp
.Fn deflateEnd
causes all dynamically allocated data structures
for this stream to be freed.
This function discards any unprocessed input
and does not flush any pending output.
.
.Sh RETURN VALUES
.Fn deflateInit
returns
.Dv Z_OK
if success,
.Dv Z_MEM_ERROR
if there was not enough memory,
.Dv Z_STREAM_ERROR
if
.Fa level
is not a valid compression level,
or
.Dv Z_VERSION_ERROR
if the zlib library version
.Pq Xr zlibVersion 3
is incompatible with the version assumed by the caller
.Pq Dv ZLIB_VERSION .
.Fa msg
is set to null
if there is no error message.
.
.Pp
.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.
.
.Pp
.Fn deflateEnd
returns
.Dv Z_OK
if success,
.Dv Z_STREAM_ERROR
if the stream state was inconsistent,
.Dv Z_DATA_ERROR
if the stream was freed prematurely
(some input or output was discarded).
In the error case,
.Fa msg
may be set but then points to a static string
(which must not be deallocated).