1
/* example.c -- usage example of the zlib compression library
2
* Copyright (C) 1995-1996 Jean-loup Gailly.
3
* For conditions of distribution and use, see copyright notice in zlib.h
6
/* $Id: example.c,v 1.3 2003/02/08 15:00:10 wtc%netscape.com Exp $ */
15
extern void exit OF((int));
18
#define CHECK_ERR(err, msg) { \
20
fprintf(stderr, "%s error: %d\n", msg, err); \
25
const char hello[] = "hello, hello!";
26
/* "hello world" would be more standard, but the repeated "hello"
27
* stresses the compression code better, sorry...
30
const char dictionary[] = "hello";
31
uLong dictId; /* Adler32 value of the dictionary */
33
void test_compress OF((Byte *compr, uLong comprLen,
34
Byte *uncompr, uLong uncomprLen));
35
void test_gzio OF((const char *out, const char *in,
36
Byte *uncompr, int uncomprLen));
37
void test_deflate OF((Byte *compr, uLong comprLen));
38
void test_inflate OF((Byte *compr, uLong comprLen,
39
Byte *uncompr, uLong uncomprLen));
40
void test_large_deflate OF((Byte *compr, uLong comprLen,
41
Byte *uncompr, uLong uncomprLen));
42
void test_large_inflate OF((Byte *compr, uLong comprLen,
43
Byte *uncompr, uLong uncomprLen));
44
void test_flush OF((Byte *compr, uLong comprLen));
45
void test_sync OF((Byte *compr, uLong comprLen,
46
Byte *uncompr, uLong uncomprLen));
47
void test_dict_deflate OF((Byte *compr, uLong comprLen));
48
void test_dict_inflate OF((Byte *compr, uLong comprLen,
49
Byte *uncompr, uLong uncomprLen));
50
int main OF((int argc, char *argv[]));
52
/* ===========================================================================
53
* Test compress() and uncompress()
55
void test_compress(compr, comprLen, uncompr, uncomprLen)
56
Byte *compr, *uncompr;
57
uLong comprLen, uncomprLen;
60
uLong len = strlen(hello)+1;
62
err = compress(compr, &comprLen, (const Bytef*)hello, len);
63
CHECK_ERR(err, "compress");
65
strcpy((char*)uncompr, "garbage");
67
err = uncompress(uncompr, &uncomprLen, compr, comprLen);
68
CHECK_ERR(err, "uncompress");
70
if (strcmp((char*)uncompr, hello)) {
71
fprintf(stderr, "bad uncompress\n");
73
printf("uncompress(): %s\n", uncompr);
77
/* ===========================================================================
78
* Test read/write of .gz files
80
void test_gzio(out, in, uncompr, uncomprLen)
81
const char *out; /* output file */
82
const char *in; /* input file */
87
int len = strlen(hello)+1;
90
file = gzopen(out, "wb");
92
fprintf(stderr, "gzopen error\n");
96
if (gzwrite(file, (const voidp)hello, (unsigned)len) != len) {
97
fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err));
101
file = gzopen(in, "rb");
103
fprintf(stderr, "gzopen error\n");
105
strcpy((char*)uncompr, "garbage");
107
uncomprLen = gzread(file, uncompr, (unsigned)uncomprLen);
108
if (uncomprLen != len) {
109
fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
113
if (strcmp((char*)uncompr, hello)) {
114
fprintf(stderr, "bad gzread\n");
116
printf("gzread(): %s\n", uncompr);
120
/* ===========================================================================
121
* Test deflate() with small buffers
123
void test_deflate(compr, comprLen)
127
z_stream c_stream; /* compression stream */
129
int len = strlen(hello)+1;
131
c_stream.zalloc = (alloc_func)0;
132
c_stream.zfree = (free_func)0;
133
c_stream.opaque = (voidpf)0;
135
err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
136
CHECK_ERR(err, "deflateInit");
138
c_stream.next_in = (Bytef*)hello;
139
c_stream.next_out = compr;
141
while (c_stream.total_in != (uLong)len && c_stream.total_out < comprLen) {
142
c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
143
err = deflate(&c_stream, Z_NO_FLUSH);
144
CHECK_ERR(err, "deflate");
146
/* Finish the stream, still forcing small buffers: */
148
c_stream.avail_out = 1;
149
err = deflate(&c_stream, Z_FINISH);
150
if (err == Z_STREAM_END) break;
151
CHECK_ERR(err, "deflate");
154
err = deflateEnd(&c_stream);
155
CHECK_ERR(err, "deflateEnd");
158
/* ===========================================================================
159
* Test inflate() with small buffers
161
void test_inflate(compr, comprLen, uncompr, uncomprLen)
162
Byte *compr, *uncompr;
163
uLong comprLen, uncomprLen;
166
z_stream d_stream; /* decompression stream */
168
strcpy((char*)uncompr, "garbage");
170
d_stream.zalloc = (alloc_func)0;
171
d_stream.zfree = (free_func)0;
172
d_stream.opaque = (voidpf)0;
174
err = inflateInit(&d_stream);
175
CHECK_ERR(err, "inflateInit");
177
d_stream.next_in = compr;
178
d_stream.next_out = uncompr;
180
while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
181
d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
182
err = inflate(&d_stream, Z_NO_FLUSH);
183
if (err == Z_STREAM_END) break;
184
CHECK_ERR(err, "inflate");
187
err = inflateEnd(&d_stream);
188
CHECK_ERR(err, "inflateEnd");
190
if (strcmp((char*)uncompr, hello)) {
191
fprintf(stderr, "bad inflate\n");
193
printf("inflate(): %s\n", uncompr);
197
/* ===========================================================================
198
* Test deflate() with large buffers and dynamic change of compression level
200
void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
201
Byte *compr, *uncompr;
202
uLong comprLen, uncomprLen;
204
z_stream c_stream; /* compression stream */
207
c_stream.zalloc = (alloc_func)0;
208
c_stream.zfree = (free_func)0;
209
c_stream.opaque = (voidpf)0;
211
err = deflateInit(&c_stream, Z_BEST_SPEED);
212
CHECK_ERR(err, "deflateInit");
214
c_stream.next_out = compr;
215
c_stream.avail_out = (uInt)comprLen;
217
/* At this point, uncompr is still mostly zeroes, so it should compress
220
c_stream.next_in = uncompr;
221
c_stream.avail_in = (uInt)uncomprLen;
222
err = deflate(&c_stream, Z_NO_FLUSH);
223
CHECK_ERR(err, "deflate");
224
if (c_stream.avail_in != 0) {
225
fprintf(stderr, "deflate not greedy\n");
228
/* Feed in already compressed data and switch to no compression: */
229
deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
230
c_stream.next_in = compr;
231
c_stream.avail_in = (uInt)comprLen/2;
232
err = deflate(&c_stream, Z_NO_FLUSH);
233
CHECK_ERR(err, "deflate");
235
/* Switch back to compressing mode: */
236
deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
237
c_stream.next_in = uncompr;
238
c_stream.avail_in = (uInt)uncomprLen;
239
err = deflate(&c_stream, Z_NO_FLUSH);
240
CHECK_ERR(err, "deflate");
242
err = deflate(&c_stream, Z_FINISH);
243
if (err != Z_STREAM_END) {
244
fprintf(stderr, "deflate should report Z_STREAM_END\n");
246
err = deflateEnd(&c_stream);
247
CHECK_ERR(err, "deflateEnd");
250
/* ===========================================================================
251
* Test inflate() with large buffers
253
void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
254
Byte *compr, *uncompr;
255
uLong comprLen, uncomprLen;
258
z_stream d_stream; /* decompression stream */
260
strcpy((char*)uncompr, "garbage");
262
d_stream.zalloc = (alloc_func)0;
263
d_stream.zfree = (free_func)0;
264
d_stream.opaque = (voidpf)0;
266
err = inflateInit(&d_stream);
267
CHECK_ERR(err, "inflateInit");
269
d_stream.next_in = compr;
270
d_stream.avail_in = (uInt)comprLen;
273
d_stream.next_out = uncompr; /* discard the output */
274
d_stream.avail_out = (uInt)uncomprLen;
275
err = inflate(&d_stream, Z_NO_FLUSH);
276
if (err == Z_STREAM_END) break;
277
CHECK_ERR(err, "large inflate");
280
err = inflateEnd(&d_stream);
281
CHECK_ERR(err, "inflateEnd");
283
if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
284
fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
286
printf("large_inflate(): OK\n");
290
/* ===========================================================================
291
* Test deflate() with full flush
293
void test_flush(compr, comprLen)
297
z_stream c_stream; /* compression stream */
299
int len = strlen(hello)+1;
301
c_stream.zalloc = (alloc_func)0;
302
c_stream.zfree = (free_func)0;
303
c_stream.opaque = (voidpf)0;
305
err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
306
CHECK_ERR(err, "deflateInit");
308
c_stream.next_in = (Bytef*)hello;
309
c_stream.next_out = compr;
310
c_stream.avail_in = 3;
311
c_stream.avail_out = (uInt)comprLen;
312
err = deflate(&c_stream, Z_FULL_FLUSH);
313
CHECK_ERR(err, "deflate");
315
compr[3]++; /* force an error in first compressed block */
316
c_stream.avail_in = len - 3;
318
err = deflate(&c_stream, Z_FINISH);
319
if (err != Z_STREAM_END) {
320
CHECK_ERR(err, "deflate");
322
err = deflateEnd(&c_stream);
323
CHECK_ERR(err, "deflateEnd");
326
/* ===========================================================================
329
void test_sync(compr, comprLen, uncompr, uncomprLen)
330
Byte *compr, *uncompr;
331
uLong comprLen, uncomprLen;
334
z_stream d_stream; /* decompression stream */
336
strcpy((char*)uncompr, "garbage");
338
d_stream.zalloc = (alloc_func)0;
339
d_stream.zfree = (free_func)0;
340
d_stream.opaque = (voidpf)0;
342
err = inflateInit(&d_stream);
343
CHECK_ERR(err, "inflateInit");
345
d_stream.next_in = compr;
346
d_stream.next_out = uncompr;
347
d_stream.avail_in = 2; /* just read the zlib header */
348
d_stream.avail_out = (uInt)uncomprLen;
350
inflate(&d_stream, Z_NO_FLUSH);
351
CHECK_ERR(err, "inflate");
353
d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */
354
err = inflateSync(&d_stream); /* but skip the damaged part */
355
CHECK_ERR(err, "inflateSync");
357
err = inflate(&d_stream, Z_FINISH);
358
if (err != Z_DATA_ERROR) {
359
fprintf(stderr, "inflate should report DATA_ERROR\n");
360
/* Because of incorrect adler32 */
362
err = inflateEnd(&d_stream);
363
CHECK_ERR(err, "inflateEnd");
365
printf("after inflateSync(): hel%s\n", uncompr);
368
/* ===========================================================================
369
* Test deflate() with preset dictionary
371
void test_dict_deflate(compr, comprLen)
375
z_stream c_stream; /* compression stream */
378
c_stream.zalloc = (alloc_func)0;
379
c_stream.zfree = (free_func)0;
380
c_stream.opaque = (voidpf)0;
382
err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
383
CHECK_ERR(err, "deflateInit");
385
err = deflateSetDictionary(&c_stream,
386
(const Bytef*)dictionary, sizeof(dictionary));
387
CHECK_ERR(err, "deflateSetDictionary");
389
dictId = c_stream.adler;
390
c_stream.next_out = compr;
391
c_stream.avail_out = (uInt)comprLen;
393
c_stream.next_in = (Bytef*)hello;
394
c_stream.avail_in = (uInt)strlen(hello)+1;
396
err = deflate(&c_stream, Z_FINISH);
397
if (err != Z_STREAM_END) {
398
fprintf(stderr, "deflate should report Z_STREAM_END\n");
400
err = deflateEnd(&c_stream);
401
CHECK_ERR(err, "deflateEnd");
404
/* ===========================================================================
405
* Test inflate() with a preset dictionary
407
void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
408
Byte *compr, *uncompr;
409
uLong comprLen, uncomprLen;
412
z_stream d_stream; /* decompression stream */
414
strcpy((char*)uncompr, "garbage");
416
d_stream.zalloc = (alloc_func)0;
417
d_stream.zfree = (free_func)0;
418
d_stream.opaque = (voidpf)0;
420
err = inflateInit(&d_stream);
421
CHECK_ERR(err, "inflateInit");
423
d_stream.next_in = compr;
424
d_stream.avail_in = (uInt)comprLen;
426
d_stream.next_out = uncompr;
427
d_stream.avail_out = (uInt)uncomprLen;
430
err = inflate(&d_stream, Z_NO_FLUSH);
431
if (err == Z_STREAM_END) break;
432
if (err == Z_NEED_DICT) {
433
if (d_stream.adler != dictId) {
434
fprintf(stderr, "unexpected dictionary");
437
err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
440
CHECK_ERR(err, "inflate with dict");
443
err = inflateEnd(&d_stream);
444
CHECK_ERR(err, "inflateEnd");
446
if (strcmp((char*)uncompr, hello)) {
447
fprintf(stderr, "bad inflate with dict\n");
449
printf("inflate with dictionary: %s\n", uncompr);
453
/* ===========================================================================
454
* Usage: example [output.gz [input.gz]]
461
Byte *compr, *uncompr;
462
uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
463
uLong uncomprLen = comprLen;
465
if (zlibVersion()[0] != ZLIB_VERSION[0]) {
466
fprintf(stderr, "incompatible zlib version\n");
469
} else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
470
fprintf(stderr, "warning: different zlib version\n");
473
compr = (Byte*)calloc((uInt)comprLen, 1);
474
uncompr = (Byte*)calloc((uInt)uncomprLen, 1);
475
/* compr and uncompr are cleared to avoid reading uninitialized
476
* data and to ensure that uncompr compresses well.
478
if (compr == Z_NULL || uncompr == Z_NULL) {
479
printf("out of memory\n");
483
test_compress(compr, comprLen, uncompr, uncomprLen);
485
test_gzio((argc > 1 ? argv[1] : "foo.gz"),
486
(argc > 2 ? argv[2] : "foo.gz"),
487
uncompr, (int)uncomprLen);
489
test_deflate(compr, comprLen);
490
test_inflate(compr, comprLen, uncompr, uncomprLen);
492
test_large_deflate(compr, comprLen, uncompr, uncomprLen);
493
test_large_inflate(compr, comprLen, uncompr, uncomprLen);
495
test_flush(compr, comprLen);
496
test_sync(compr, comprLen, uncompr, uncomprLen);
498
test_dict_deflate(compr, comprLen);
499
test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
502
return 0; /* to avoid warning */