1 /* example.c -- usage example of the zlib compression library
2 * Copyright (C) 1995-2003 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
16 extern void exit OF((int));
19 #if defined(VMS) || defined(RISCOS)
20 # define TESTFILE "foo-gz"
22 # define TESTFILE "foo.gz"
25 #define CHECK_ERR(err, msg) { \
27 fprintf(stderr, "%s error: %d\n", msg, err); \
32 const char hello[] = "hello, hello!";
33 /* "hello world" would be more standard, but the repeated "hello"
34 * stresses the compression code better, sorry...
37 const char dictionary[] = "hello";
38 uLong dictId; /* Adler32 value of the dictionary */
40 void test_compress OF((Byte *compr, uLong comprLen,
41 Byte *uncompr, uLong uncomprLen));
42 void test_gzio OF((const char *fname,
43 Byte *uncompr, uLong uncomprLen));
44 void test_deflate OF((Byte *compr, uLong comprLen));
45 void test_inflate OF((Byte *compr, uLong comprLen,
46 Byte *uncompr, uLong uncomprLen));
47 void test_large_deflate OF((Byte *compr, uLong comprLen,
48 Byte *uncompr, uLong uncomprLen));
49 void test_large_inflate OF((Byte *compr, uLong comprLen,
50 Byte *uncompr, uLong uncomprLen));
51 void test_flush OF((Byte *compr, uLong *comprLen));
52 void test_sync OF((Byte *compr, uLong comprLen,
53 Byte *uncompr, uLong uncomprLen));
54 void test_dict_deflate OF((Byte *compr, uLong comprLen));
55 void test_dict_inflate OF((Byte *compr, uLong comprLen,
56 Byte *uncompr, uLong uncomprLen));
57 int main OF((int argc, char *argv[]));
59 /* ===========================================================================
60 * Test compress() and uncompress()
62 void test_compress(compr, comprLen, uncompr, uncomprLen)
63 Byte *compr, *uncompr;
64 uLong comprLen, uncomprLen;
67 uLong len = (uLong)strlen(hello)+1;
69 err = compress(compr, &comprLen, (const Bytef*)hello, len);
70 CHECK_ERR(err, "compress");
72 strcpy((char*)uncompr, "garbage");
74 err = uncompress(uncompr, &uncomprLen, compr, comprLen);
75 CHECK_ERR(err, "uncompress");
77 if (strcmp((char*)uncompr, hello)) {
78 fprintf(stderr, "bad uncompress\n");
81 printf("uncompress(): %s\n", (char *)uncompr);
85 /* ===========================================================================
86 * Test read/write of .gz files
88 void test_gzio(fname, uncompr, uncomprLen)
89 const char *fname; /* compressed file name */
94 fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
97 int len = (int)strlen(hello)+1;
101 file = gzopen(fname, "wb");
103 fprintf(stderr, "gzopen error\n");
107 if (gzputs(file, "ello") != 4) {
108 fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
111 if (gzprintf(file, ", %s!", "hello") != 8) {
112 fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
115 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
118 file = gzopen(fname, "rb");
120 fprintf(stderr, "gzopen error\n");
123 strcpy((char*)uncompr, "garbage");
125 if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
126 fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
129 if (strcmp((char*)uncompr, hello)) {
130 fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
133 printf("gzread(): %s\n", (char*)uncompr);
136 pos = gzseek(file, -8L, SEEK_CUR);
137 if (pos != 6 || gztell(file) != pos) {
138 fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
139 (long)pos, (long)gztell(file));
143 if (gzgetc(file) != ' ') {
144 fprintf(stderr, "gzgetc error\n");
148 if (gzungetc(' ', file) != ' ') {
149 fprintf(stderr, "gzungetc error\n");
153 gzgets(file, (char*)uncompr, (int)uncomprLen);
154 if (strlen((char*)uncompr) != 7) { /* " hello!" */
155 fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
158 if (strcmp((char*)uncompr, hello + 6)) {
159 fprintf(stderr, "bad gzgets after gzseek\n");
162 printf("gzgets() after gzseek: %s\n", (char*)uncompr);
169 /* ===========================================================================
170 * Test deflate() with small buffers
172 void test_deflate(compr, comprLen)
176 z_stream c_stream; /* compression stream */
178 uLong len = (uLong)strlen(hello)+1;
180 c_stream.zalloc = (alloc_func)0;
181 c_stream.zfree = (free_func)0;
182 c_stream.opaque = (voidpf)0;
184 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
185 CHECK_ERR(err, "deflateInit");
187 c_stream.next_in = (Bytef*)hello;
188 c_stream.next_out = compr;
190 while (c_stream.total_in != len && c_stream.total_out < comprLen) {
191 c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
192 err = deflate(&c_stream, Z_NO_FLUSH);
193 CHECK_ERR(err, "deflate");
195 /* Finish the stream, still forcing small buffers: */
197 c_stream.avail_out = 1;
198 err = deflate(&c_stream, Z_FINISH);
199 if (err == Z_STREAM_END) break;
200 CHECK_ERR(err, "deflate");
203 err = deflateEnd(&c_stream);
204 CHECK_ERR(err, "deflateEnd");
207 /* ===========================================================================
208 * Test inflate() with small buffers
210 void test_inflate(compr, comprLen, uncompr, uncomprLen)
211 Byte *compr, *uncompr;
212 uLong comprLen, uncomprLen;
215 z_stream d_stream; /* decompression stream */
217 strcpy((char*)uncompr, "garbage");
219 d_stream.zalloc = (alloc_func)0;
220 d_stream.zfree = (free_func)0;
221 d_stream.opaque = (voidpf)0;
223 d_stream.next_in = compr;
224 d_stream.avail_in = 0;
225 d_stream.next_out = uncompr;
227 err = inflateInit(&d_stream);
228 CHECK_ERR(err, "inflateInit");
230 while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
231 d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
232 err = inflate(&d_stream, Z_NO_FLUSH);
233 if (err == Z_STREAM_END) break;
234 CHECK_ERR(err, "inflate");
237 err = inflateEnd(&d_stream);
238 CHECK_ERR(err, "inflateEnd");
240 if (strcmp((char*)uncompr, hello)) {
241 fprintf(stderr, "bad inflate\n");
244 printf("inflate(): %s\n", (char *)uncompr);
248 /* ===========================================================================
249 * Test deflate() with large buffers and dynamic change of compression level
251 void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
252 Byte *compr, *uncompr;
253 uLong comprLen, uncomprLen;
255 z_stream c_stream; /* compression stream */
258 c_stream.zalloc = (alloc_func)0;
259 c_stream.zfree = (free_func)0;
260 c_stream.opaque = (voidpf)0;
262 err = deflateInit(&c_stream, Z_BEST_SPEED);
263 CHECK_ERR(err, "deflateInit");
265 c_stream.next_out = compr;
266 c_stream.avail_out = (uInt)comprLen;
268 /* At this point, uncompr is still mostly zeroes, so it should compress
271 c_stream.next_in = uncompr;
272 c_stream.avail_in = (uInt)uncomprLen;
273 err = deflate(&c_stream, Z_NO_FLUSH);
274 CHECK_ERR(err, "deflate");
275 if (c_stream.avail_in != 0) {
276 fprintf(stderr, "deflate not greedy\n");
280 /* Feed in already compressed data and switch to no compression: */
281 deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
282 c_stream.next_in = compr;
283 c_stream.avail_in = (uInt)comprLen/2;
284 err = deflate(&c_stream, Z_NO_FLUSH);
285 CHECK_ERR(err, "deflate");
287 /* Switch back to compressing mode: */
288 deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
289 c_stream.next_in = uncompr;
290 c_stream.avail_in = (uInt)uncomprLen;
291 err = deflate(&c_stream, Z_NO_FLUSH);
292 CHECK_ERR(err, "deflate");
294 err = deflate(&c_stream, Z_FINISH);
295 if (err != Z_STREAM_END) {
296 fprintf(stderr, "deflate should report Z_STREAM_END\n");
299 err = deflateEnd(&c_stream);
300 CHECK_ERR(err, "deflateEnd");
303 /* ===========================================================================
304 * Test inflate() with large buffers
306 void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
307 Byte *compr, *uncompr;
308 uLong comprLen, uncomprLen;
311 z_stream d_stream; /* decompression stream */
313 strcpy((char*)uncompr, "garbage");
315 d_stream.zalloc = (alloc_func)0;
316 d_stream.zfree = (free_func)0;
317 d_stream.opaque = (voidpf)0;
319 d_stream.next_in = compr;
320 d_stream.avail_in = (uInt)comprLen;
322 err = inflateInit(&d_stream);
323 CHECK_ERR(err, "inflateInit");
326 d_stream.next_out = uncompr; /* discard the output */
327 d_stream.avail_out = (uInt)uncomprLen;
328 err = inflate(&d_stream, Z_NO_FLUSH);
329 if (err == Z_STREAM_END) break;
330 CHECK_ERR(err, "large inflate");
333 err = inflateEnd(&d_stream);
334 CHECK_ERR(err, "inflateEnd");
336 if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
337 fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
340 printf("large_inflate(): OK\n");
344 /* ===========================================================================
345 * Test deflate() with full flush
347 void test_flush(compr, comprLen)
351 z_stream c_stream; /* compression stream */
353 uInt len = (uInt)strlen(hello)+1;
355 c_stream.zalloc = (alloc_func)0;
356 c_stream.zfree = (free_func)0;
357 c_stream.opaque = (voidpf)0;
359 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
360 CHECK_ERR(err, "deflateInit");
362 c_stream.next_in = (Bytef*)hello;
363 c_stream.next_out = compr;
364 c_stream.avail_in = 3;
365 c_stream.avail_out = (uInt)*comprLen;
366 err = deflate(&c_stream, Z_FULL_FLUSH);
367 CHECK_ERR(err, "deflate");
369 compr[3]++; /* force an error in first compressed block */
370 c_stream.avail_in = len - 3;
372 err = deflate(&c_stream, Z_FINISH);
373 if (err != Z_STREAM_END) {
374 CHECK_ERR(err, "deflate");
376 err = deflateEnd(&c_stream);
377 CHECK_ERR(err, "deflateEnd");
379 *comprLen = c_stream.total_out;
382 /* ===========================================================================
385 void test_sync(compr, comprLen, uncompr, uncomprLen)
386 Byte *compr, *uncompr;
387 uLong comprLen, uncomprLen;
390 z_stream d_stream; /* decompression stream */
392 strcpy((char*)uncompr, "garbage");
394 d_stream.zalloc = (alloc_func)0;
395 d_stream.zfree = (free_func)0;
396 d_stream.opaque = (voidpf)0;
398 d_stream.next_in = compr;
399 d_stream.avail_in = 2; /* just read the zlib header */
401 err = inflateInit(&d_stream);
402 CHECK_ERR(err, "inflateInit");
404 d_stream.next_out = uncompr;
405 d_stream.avail_out = (uInt)uncomprLen;
407 inflate(&d_stream, Z_NO_FLUSH);
408 CHECK_ERR(err, "inflate");
410 d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */
411 err = inflateSync(&d_stream); /* but skip the damaged part */
412 CHECK_ERR(err, "inflateSync");
414 err = inflate(&d_stream, Z_FINISH);
415 if (err != Z_DATA_ERROR) {
416 fprintf(stderr, "inflate should report DATA_ERROR\n");
417 /* Because of incorrect adler32 */
420 err = inflateEnd(&d_stream);
421 CHECK_ERR(err, "inflateEnd");
423 printf("after inflateSync(): hel%s\n", (char *)uncompr);
426 /* ===========================================================================
427 * Test deflate() with preset dictionary
429 void test_dict_deflate(compr, comprLen)
433 z_stream c_stream; /* compression stream */
436 c_stream.zalloc = (alloc_func)0;
437 c_stream.zfree = (free_func)0;
438 c_stream.opaque = (voidpf)0;
440 err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
441 CHECK_ERR(err, "deflateInit");
443 err = deflateSetDictionary(&c_stream,
444 (const Bytef*)dictionary, sizeof(dictionary));
445 CHECK_ERR(err, "deflateSetDictionary");
447 dictId = c_stream.adler;
448 c_stream.next_out = compr;
449 c_stream.avail_out = (uInt)comprLen;
451 c_stream.next_in = (Bytef*)hello;
452 c_stream.avail_in = (uInt)strlen(hello)+1;
454 err = deflate(&c_stream, Z_FINISH);
455 if (err != Z_STREAM_END) {
456 fprintf(stderr, "deflate should report Z_STREAM_END\n");
459 err = deflateEnd(&c_stream);
460 CHECK_ERR(err, "deflateEnd");
463 /* ===========================================================================
464 * Test inflate() with a preset dictionary
466 void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
467 Byte *compr, *uncompr;
468 uLong comprLen, uncomprLen;
471 z_stream d_stream; /* decompression stream */
473 strcpy((char*)uncompr, "garbage");
475 d_stream.zalloc = (alloc_func)0;
476 d_stream.zfree = (free_func)0;
477 d_stream.opaque = (voidpf)0;
479 d_stream.next_in = compr;
480 d_stream.avail_in = (uInt)comprLen;
482 err = inflateInit(&d_stream);
483 CHECK_ERR(err, "inflateInit");
485 d_stream.next_out = uncompr;
486 d_stream.avail_out = (uInt)uncomprLen;
489 err = inflate(&d_stream, Z_NO_FLUSH);
490 if (err == Z_STREAM_END) break;
491 if (err == Z_NEED_DICT) {
492 if (d_stream.adler != dictId) {
493 fprintf(stderr, "unexpected dictionary");
496 err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
499 CHECK_ERR(err, "inflate with dict");
502 err = inflateEnd(&d_stream);
503 CHECK_ERR(err, "inflateEnd");
505 if (strcmp((char*)uncompr, hello)) {
506 fprintf(stderr, "bad inflate with dict\n");
509 printf("inflate with dictionary: %s\n", (char *)uncompr);
513 /* ===========================================================================
514 * Usage: example [output.gz [input.gz]]
521 Byte *compr, *uncompr;
522 uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
523 uLong uncomprLen = comprLen;
524 static const char* myVersion = ZLIB_VERSION;
526 if (zlibVersion()[0] != myVersion[0]) {
527 fprintf(stderr, "incompatible zlib version\n");
530 } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
531 fprintf(stderr, "warning: different zlib version\n");
534 printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
535 ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());
537 compr = (Byte*)calloc((uInt)comprLen, 1);
538 uncompr = (Byte*)calloc((uInt)uncomprLen, 1);
539 /* compr and uncompr are cleared to avoid reading uninitialized
540 * data and to ensure that uncompr compresses well.
542 if (compr == Z_NULL || uncompr == Z_NULL) {
543 printf("out of memory\n");
546 test_compress(compr, comprLen, uncompr, uncomprLen);
548 test_gzio((argc > 1 ? argv[1] : TESTFILE),
549 uncompr, uncomprLen);
551 test_deflate(compr, comprLen);
552 test_inflate(compr, comprLen, uncompr, uncomprLen);
554 test_large_deflate(compr, comprLen, uncompr, uncomprLen);
555 test_large_inflate(compr, comprLen, uncompr, uncomprLen);
557 test_flush(compr, &comprLen);
558 test_sync(compr, comprLen, uncompr, uncomprLen);
559 comprLen = uncomprLen;
561 test_dict_deflate(compr, comprLen);
562 test_dict_inflate(compr, comprLen, uncompr, uncomprLen);