1 /* example.c -- usage example of the zlib compression library
2 * Copyright (C) 1995-1998 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 *out, const char *in,
43 Byte *uncompr, int 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 = 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(out, in, uncompr, uncomprLen)
89 const char *out; /* compressed output file */
90 const char *in; /* compressed input file */
95 int len = strlen(hello)+1;
99 file = gzopen(out, "wb");
101 fprintf(stderr, "gzopen error\n");
105 if (gzputs(file, "ello") != 4) {
106 fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
109 if (gzprintf(file, ", %s!", "hello") != 8) {
110 fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
113 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
116 file = gzopen(in, "rb");
118 fprintf(stderr, "gzopen error\n");
120 strcpy((char*)uncompr, "garbage");
122 uncomprLen = gzread(file, uncompr, (unsigned)uncomprLen);
123 if (uncomprLen != len) {
124 fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
127 if (strcmp((char*)uncompr, hello)) {
128 fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
131 printf("gzread(): %s\n", (char *)uncompr);
134 pos = gzseek(file, -8L, SEEK_CUR);
135 if (pos != 6 || gztell(file) != pos) {
136 fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
137 (long)pos, (long)gztell(file));
141 if (gzgetc(file) != ' ') {
142 fprintf(stderr, "gzgetc error\n");
146 gzgets(file, (char*)uncompr, uncomprLen);
147 uncomprLen = strlen((char*)uncompr);
148 if (uncomprLen != 6) { /* "hello!" */
149 fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
152 if (strcmp((char*)uncompr, hello+7)) {
153 fprintf(stderr, "bad gzgets after gzseek\n");
156 printf("gzgets() after gzseek: %s\n", (char *)uncompr);
162 /* ===========================================================================
163 * Test deflate() with small buffers
165 void test_deflate(compr, comprLen)
169 z_stream c_stream; /* compression stream */
171 int len = strlen(hello)+1;
173 c_stream.zalloc = (alloc_func)0;
174 c_stream.zfree = (free_func)0;
175 c_stream.opaque = (voidpf)0;
177 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
178 CHECK_ERR(err, "deflateInit");
180 c_stream.next_in = (Bytef*)hello;
181 c_stream.next_out = compr;
183 while (c_stream.total_in != (uLong)len && c_stream.total_out < comprLen) {
184 c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
185 err = deflate(&c_stream, Z_NO_FLUSH);
186 CHECK_ERR(err, "deflate");
188 /* Finish the stream, still forcing small buffers: */
190 c_stream.avail_out = 1;
191 err = deflate(&c_stream, Z_FINISH);
192 if (err == Z_STREAM_END) break;
193 CHECK_ERR(err, "deflate");
196 err = deflateEnd(&c_stream);
197 CHECK_ERR(err, "deflateEnd");
200 /* ===========================================================================
201 * Test inflate() with small buffers
203 void test_inflate(compr, comprLen, uncompr, uncomprLen)
204 Byte *compr, *uncompr;
205 uLong comprLen, uncomprLen;
208 z_stream d_stream; /* decompression stream */
210 strcpy((char*)uncompr, "garbage");
212 d_stream.zalloc = (alloc_func)0;
213 d_stream.zfree = (free_func)0;
214 d_stream.opaque = (voidpf)0;
216 d_stream.next_in = compr;
217 d_stream.avail_in = 0;
218 d_stream.next_out = uncompr;
220 err = inflateInit(&d_stream);
221 CHECK_ERR(err, "inflateInit");
223 while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
224 d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
225 err = inflate(&d_stream, Z_NO_FLUSH);
226 if (err == Z_STREAM_END) break;
227 CHECK_ERR(err, "inflate");
230 err = inflateEnd(&d_stream);
231 CHECK_ERR(err, "inflateEnd");
233 if (strcmp((char*)uncompr, hello)) {
234 fprintf(stderr, "bad inflate\n");
237 printf("inflate(): %s\n", (char *)uncompr);
241 /* ===========================================================================
242 * Test deflate() with large buffers and dynamic change of compression level
244 void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
245 Byte *compr, *uncompr;
246 uLong comprLen, uncomprLen;
248 z_stream c_stream; /* compression stream */
251 c_stream.zalloc = (alloc_func)0;
252 c_stream.zfree = (free_func)0;
253 c_stream.opaque = (voidpf)0;
255 err = deflateInit(&c_stream, Z_BEST_SPEED);
256 CHECK_ERR(err, "deflateInit");
258 c_stream.next_out = compr;
259 c_stream.avail_out = (uInt)comprLen;
261 /* At this point, uncompr is still mostly zeroes, so it should compress
264 c_stream.next_in = uncompr;
265 c_stream.avail_in = (uInt)uncomprLen;
266 err = deflate(&c_stream, Z_NO_FLUSH);
267 CHECK_ERR(err, "deflate");
268 if (c_stream.avail_in != 0) {
269 fprintf(stderr, "deflate not greedy\n");
273 /* Feed in already compressed data and switch to no compression: */
274 deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
275 c_stream.next_in = compr;
276 c_stream.avail_in = (uInt)comprLen/2;
277 err = deflate(&c_stream, Z_NO_FLUSH);
278 CHECK_ERR(err, "deflate");
280 /* Switch back to compressing mode: */
281 deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
282 c_stream.next_in = uncompr;
283 c_stream.avail_in = (uInt)uncomprLen;
284 err = deflate(&c_stream, Z_NO_FLUSH);
285 CHECK_ERR(err, "deflate");
287 err = deflate(&c_stream, Z_FINISH);
288 if (err != Z_STREAM_END) {
289 fprintf(stderr, "deflate should report Z_STREAM_END\n");
292 err = deflateEnd(&c_stream);
293 CHECK_ERR(err, "deflateEnd");
296 /* ===========================================================================
297 * Test inflate() with large buffers
299 void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
300 Byte *compr, *uncompr;
301 uLong comprLen, uncomprLen;
304 z_stream d_stream; /* decompression stream */
306 strcpy((char*)uncompr, "garbage");
308 d_stream.zalloc = (alloc_func)0;
309 d_stream.zfree = (free_func)0;
310 d_stream.opaque = (voidpf)0;
312 d_stream.next_in = compr;
313 d_stream.avail_in = (uInt)comprLen;
315 err = inflateInit(&d_stream);
316 CHECK_ERR(err, "inflateInit");
319 d_stream.next_out = uncompr; /* discard the output */
320 d_stream.avail_out = (uInt)uncomprLen;
321 err = inflate(&d_stream, Z_NO_FLUSH);
322 if (err == Z_STREAM_END) break;
323 CHECK_ERR(err, "large inflate");
326 err = inflateEnd(&d_stream);
327 CHECK_ERR(err, "inflateEnd");
329 if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
330 fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
333 printf("large_inflate(): OK\n");
337 /* ===========================================================================
338 * Test deflate() with full flush
340 void test_flush(compr, comprLen)
344 z_stream c_stream; /* compression stream */
346 int len = strlen(hello)+1;
348 c_stream.zalloc = (alloc_func)0;
349 c_stream.zfree = (free_func)0;
350 c_stream.opaque = (voidpf)0;
352 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
353 CHECK_ERR(err, "deflateInit");
355 c_stream.next_in = (Bytef*)hello;
356 c_stream.next_out = compr;
357 c_stream.avail_in = 3;
358 c_stream.avail_out = (uInt)*comprLen;
359 err = deflate(&c_stream, Z_FULL_FLUSH);
360 CHECK_ERR(err, "deflate");
362 compr[3]++; /* force an error in first compressed block */
363 c_stream.avail_in = len - 3;
365 err = deflate(&c_stream, Z_FINISH);
366 if (err != Z_STREAM_END) {
367 CHECK_ERR(err, "deflate");
369 err = deflateEnd(&c_stream);
370 CHECK_ERR(err, "deflateEnd");
372 *comprLen = c_stream.total_out;
375 /* ===========================================================================
378 void test_sync(compr, comprLen, uncompr, uncomprLen)
379 Byte *compr, *uncompr;
380 uLong comprLen, uncomprLen;
383 z_stream d_stream; /* decompression stream */
385 strcpy((char*)uncompr, "garbage");
387 d_stream.zalloc = (alloc_func)0;
388 d_stream.zfree = (free_func)0;
389 d_stream.opaque = (voidpf)0;
391 d_stream.next_in = compr;
392 d_stream.avail_in = 2; /* just read the zlib header */
394 err = inflateInit(&d_stream);
395 CHECK_ERR(err, "inflateInit");
397 d_stream.next_out = uncompr;
398 d_stream.avail_out = (uInt)uncomprLen;
400 inflate(&d_stream, Z_NO_FLUSH);
401 CHECK_ERR(err, "inflate");
403 d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */
404 err = inflateSync(&d_stream); /* but skip the damaged part */
405 CHECK_ERR(err, "inflateSync");
407 err = inflate(&d_stream, Z_FINISH);
408 if (err != Z_DATA_ERROR) {
409 fprintf(stderr, "inflate should report DATA_ERROR\n");
410 /* Because of incorrect adler32 */
413 err = inflateEnd(&d_stream);
414 CHECK_ERR(err, "inflateEnd");
416 printf("after inflateSync(): hel%s\n", (char *)uncompr);
419 /* ===========================================================================
420 * Test deflate() with preset dictionary
422 void test_dict_deflate(compr, comprLen)
426 z_stream c_stream; /* compression stream */
429 c_stream.zalloc = (alloc_func)0;
430 c_stream.zfree = (free_func)0;
431 c_stream.opaque = (voidpf)0;
433 err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
434 CHECK_ERR(err, "deflateInit");
436 err = deflateSetDictionary(&c_stream,
437 (const Bytef*)dictionary, sizeof(dictionary));
438 CHECK_ERR(err, "deflateSetDictionary");
440 dictId = c_stream.adler;
441 c_stream.next_out = compr;
442 c_stream.avail_out = (uInt)comprLen;
444 c_stream.next_in = (Bytef*)hello;
445 c_stream.avail_in = (uInt)strlen(hello)+1;
447 err = deflate(&c_stream, Z_FINISH);
448 if (err != Z_STREAM_END) {
449 fprintf(stderr, "deflate should report Z_STREAM_END\n");
452 err = deflateEnd(&c_stream);
453 CHECK_ERR(err, "deflateEnd");
456 /* ===========================================================================
457 * Test inflate() with a preset dictionary
459 void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
460 Byte *compr, *uncompr;
461 uLong comprLen, uncomprLen;
464 z_stream d_stream; /* decompression stream */
466 strcpy((char*)uncompr, "garbage");
468 d_stream.zalloc = (alloc_func)0;
469 d_stream.zfree = (free_func)0;
470 d_stream.opaque = (voidpf)0;
472 d_stream.next_in = compr;
473 d_stream.avail_in = (uInt)comprLen;
475 err = inflateInit(&d_stream);
476 CHECK_ERR(err, "inflateInit");
478 d_stream.next_out = uncompr;
479 d_stream.avail_out = (uInt)uncomprLen;
482 err = inflate(&d_stream, Z_NO_FLUSH);
483 if (err == Z_STREAM_END) break;
484 if (err == Z_NEED_DICT) {
485 if (d_stream.adler != dictId) {
486 fprintf(stderr, "unexpected dictionary");
489 err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
492 CHECK_ERR(err, "inflate with dict");
495 err = inflateEnd(&d_stream);
496 CHECK_ERR(err, "inflateEnd");
498 if (strcmp((char*)uncompr, hello)) {
499 fprintf(stderr, "bad inflate with dict\n");
502 printf("inflate with dictionary: %s\n", (char *)uncompr);
506 /* ===========================================================================
507 * Usage: example [output.gz [input.gz]]
514 Byte *compr, *uncompr;
515 uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
516 uLong uncomprLen = comprLen;
517 static const char* myVersion = ZLIB_VERSION;
519 if (zlibVersion()[0] != myVersion[0]) {
520 fprintf(stderr, "incompatible zlib version\n");
523 } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
524 fprintf(stderr, "warning: different zlib version\n");
527 compr = (Byte*)calloc((uInt)comprLen, 1);
528 uncompr = (Byte*)calloc((uInt)uncomprLen, 1);
529 /* compr and uncompr are cleared to avoid reading uninitialized
530 * data and to ensure that uncompr compresses well.
532 if (compr == Z_NULL || uncompr == Z_NULL) {
533 printf("out of memory\n");
536 test_compress(compr, comprLen, uncompr, uncomprLen);
538 test_gzio((argc > 1 ? argv[1] : TESTFILE),
539 (argc > 2 ? argv[2] : TESTFILE),
540 uncompr, (int)uncomprLen);
542 test_deflate(compr, comprLen);
543 test_inflate(compr, comprLen, uncompr, uncomprLen);
545 test_large_deflate(compr, comprLen, uncompr, uncomprLen);
546 test_large_inflate(compr, comprLen, uncompr, uncomprLen);
548 test_flush(compr, &comprLen);
549 test_sync(compr, comprLen, uncompr, uncomprLen);
550 comprLen = uncomprLen;
552 test_dict_deflate(compr, comprLen);
553 test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
556 return 0; /* to avoid warning */