1 /* example.c -- usage example of the zlib compression library
2 * Copyright (C) 1995-2006 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
16 #if TARGET_MSDOS == 16 && (defined(__SMALL__) || defined(__MEDIUM__))
21 #if defined(VMS) || defined(RISCOS)
22 # define TESTFILE "foo-gz"
24 # define TESTFILE "foo.gz"
27 #define CHECK_ERR(err, msg) { \
29 fprintf(stderr, "%s error: %d\n", msg, err); \
34 const char hello[] = "hello, hello!";
35 /* "hello world" would be more standard, but the repeated "hello"
36 * stresses the compression code better, sorry...
39 const char dictionary[] = "hello";
40 uLong dictId; /* Adler32 value of the dictionary */
42 void test_compress OF((Byte *compr, uLong comprLen,
43 Byte *uncompr, uLong uncomprLen));
44 void test_gzio OF((const char *fname,
45 Byte *uncompr, uLong uncomprLen));
46 void test_deflate OF((Byte *compr, uLong comprLen));
47 void test_inflate OF((Byte *compr, uLong comprLen,
48 Byte *uncompr, uLong uncomprLen));
49 void test_large_deflate OF((Byte *compr, uLong comprLen,
50 Byte *uncompr, uLong uncomprLen));
51 void test_large_inflate OF((Byte *compr, uLong comprLen,
52 Byte *uncompr, uLong uncomprLen));
53 void test_flush OF((Byte *compr, uLong *comprLen));
54 void test_sync OF((Byte *compr, uLong comprLen,
55 Byte *uncompr, uLong uncomprLen));
56 void test_dict_deflate OF((Byte *compr, uLong comprLen));
57 void test_dict_inflate OF((Byte *compr, uLong comprLen,
58 Byte *uncompr, uLong uncomprLen));
59 int main OF((int argc, char *argv[]));
61 /* ===========================================================================
62 * Test compress() and uncompress()
64 void test_compress(compr, comprLen, uncompr, uncomprLen)
65 Byte *compr, *uncompr;
66 uLong comprLen, uncomprLen;
69 uLong len = (uLong)strlen(hello)+1;
71 err = compress(compr, &comprLen, (const Bytef*)hello, len);
72 CHECK_ERR(err, "compress");
74 strcpy((char*)uncompr, "garbage");
76 err = uncompress(uncompr, &uncomprLen, compr, comprLen);
77 CHECK_ERR(err, "uncompress");
79 if (strcmp((char*)uncompr, hello)) {
80 fprintf(stderr, "bad uncompress\n");
83 printf("uncompress(): %s\n", (char *)uncompr);
87 /* ===========================================================================
88 * Test read/write of .gz files
90 void test_gzio(fname, uncompr, uncomprLen)
91 const char *fname; /* compressed file name */
96 fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
99 int len = (int)strlen(hello)+1;
103 file = gzopen(fname, "wb");
105 fprintf(stderr, "gzopen error\n");
109 if (gzputs(file, "ello") != 4) {
110 fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
113 if (gzprintf(file, ", %s!", "hello") != 8) {
114 fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
117 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
120 file = gzopen(fname, "rb");
122 fprintf(stderr, "gzopen error\n");
125 strcpy((char*)uncompr, "garbage");
127 if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
128 fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
131 if (strcmp((char*)uncompr, hello)) {
132 fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
135 printf("gzread(): %s\n", (char*)uncompr);
138 pos = gzseek(file, -8L, SEEK_CUR);
139 if (pos != 6 || gztell(file) != pos) {
140 fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
141 (long)pos, (long)gztell(file));
145 if (gzgetc(file) != ' ') {
146 fprintf(stderr, "gzgetc error\n");
150 if (gzungetc(' ', file) != ' ') {
151 fprintf(stderr, "gzungetc error\n");
155 gzgets(file, (char*)uncompr, (int)uncomprLen);
156 if (strlen((char*)uncompr) != 7) { /* " hello!" */
157 fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
160 if (strcmp((char*)uncompr, hello + 6)) {
161 fprintf(stderr, "bad gzgets after gzseek\n");
164 printf("gzgets() after gzseek: %s\n", (char*)uncompr);
171 /* ===========================================================================
172 * Test deflate() with small buffers
174 void test_deflate(compr, comprLen)
178 z_stream c_stream; /* compression stream */
180 uLong len = (uLong)strlen(hello)+1;
182 c_stream.zalloc = (alloc_func)0;
183 c_stream.zfree = (free_func)0;
184 c_stream.opaque = (voidpf)0;
186 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
187 CHECK_ERR(err, "deflateInit");
189 c_stream.next_in = (Bytef*)hello;
190 c_stream.next_out = compr;
192 while (c_stream.total_in != len && c_stream.total_out < comprLen) {
193 c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
194 err = deflate(&c_stream, Z_NO_FLUSH);
195 CHECK_ERR(err, "deflate");
197 /* Finish the stream, still forcing small buffers: */
199 c_stream.avail_out = 1;
200 err = deflate(&c_stream, Z_FINISH);
201 if (err == Z_STREAM_END) break;
202 CHECK_ERR(err, "deflate");
205 err = deflateEnd(&c_stream);
206 CHECK_ERR(err, "deflateEnd");
209 /* ===========================================================================
210 * Test inflate() with small buffers
212 void test_inflate(compr, comprLen, uncompr, uncomprLen)
213 Byte *compr, *uncompr;
214 uLong comprLen, uncomprLen;
217 z_stream d_stream; /* decompression stream */
219 strcpy((char*)uncompr, "garbage");
221 d_stream.zalloc = (alloc_func)0;
222 d_stream.zfree = (free_func)0;
223 d_stream.opaque = (voidpf)0;
225 d_stream.next_in = compr;
226 d_stream.avail_in = 0;
227 d_stream.next_out = uncompr;
229 err = inflateInit(&d_stream);
230 CHECK_ERR(err, "inflateInit");
232 while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
233 d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
234 err = inflate(&d_stream, Z_NO_FLUSH);
235 if (err == Z_STREAM_END) break;
236 CHECK_ERR(err, "inflate");
239 err = inflateEnd(&d_stream);
240 CHECK_ERR(err, "inflateEnd");
242 if (strcmp((char*)uncompr, hello)) {
243 fprintf(stderr, "bad inflate\n");
246 printf("inflate(): %s\n", (char *)uncompr);
250 /* ===========================================================================
251 * Test deflate() with large buffers and dynamic change of compression level
253 void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
254 Byte *compr, *uncompr;
255 uLong comprLen, uncomprLen;
257 z_stream c_stream; /* compression stream */
260 c_stream.zalloc = (alloc_func)0;
261 c_stream.zfree = (free_func)0;
262 c_stream.opaque = (voidpf)0;
264 err = deflateInit(&c_stream, Z_BEST_SPEED);
265 CHECK_ERR(err, "deflateInit");
267 c_stream.next_out = compr;
268 c_stream.avail_out = (uInt)comprLen;
270 /* At this point, uncompr is still mostly zeroes, so it should compress
273 c_stream.next_in = uncompr;
274 c_stream.avail_in = (uInt)uncomprLen;
275 err = deflate(&c_stream, Z_NO_FLUSH);
276 CHECK_ERR(err, "deflate");
277 if (c_stream.avail_in != 0) {
278 fprintf(stderr, "deflate not greedy\n");
282 /* Feed in already compressed data and switch to no compression: */
283 deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
284 c_stream.next_in = compr;
285 c_stream.avail_in = (uInt)comprLen/2;
286 err = deflate(&c_stream, Z_NO_FLUSH);
287 CHECK_ERR(err, "deflate");
289 /* Switch back to compressing mode: */
290 deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
291 c_stream.next_in = uncompr;
292 c_stream.avail_in = (uInt)uncomprLen;
293 err = deflate(&c_stream, Z_NO_FLUSH);
294 CHECK_ERR(err, "deflate");
296 err = deflate(&c_stream, Z_FINISH);
297 if (err != Z_STREAM_END) {
298 fprintf(stderr, "deflate should report Z_STREAM_END\n");
301 err = deflateEnd(&c_stream);
302 CHECK_ERR(err, "deflateEnd");
305 /* ===========================================================================
306 * Test inflate() with large buffers
308 void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
309 Byte *compr, *uncompr;
310 uLong comprLen, uncomprLen;
313 z_stream d_stream; /* decompression stream */
315 strcpy((char*)uncompr, "garbage");
317 d_stream.zalloc = (alloc_func)0;
318 d_stream.zfree = (free_func)0;
319 d_stream.opaque = (voidpf)0;
321 d_stream.next_in = compr;
322 d_stream.avail_in = (uInt)comprLen;
324 err = inflateInit(&d_stream);
325 CHECK_ERR(err, "inflateInit");
328 d_stream.next_out = uncompr; /* discard the output */
329 d_stream.avail_out = (uInt)uncomprLen;
330 err = inflate(&d_stream, Z_NO_FLUSH);
331 if (err == Z_STREAM_END) break;
332 CHECK_ERR(err, "large inflate");
335 err = inflateEnd(&d_stream);
336 CHECK_ERR(err, "inflateEnd");
338 if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
339 fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
342 printf("large_inflate(): OK\n");
346 /* ===========================================================================
347 * Test deflate() with full flush
349 void test_flush(compr, comprLen)
353 z_stream c_stream; /* compression stream */
355 uInt len = (uInt)strlen(hello)+1;
357 c_stream.zalloc = (alloc_func)0;
358 c_stream.zfree = (free_func)0;
359 c_stream.opaque = (voidpf)0;
361 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
362 CHECK_ERR(err, "deflateInit");
364 c_stream.next_in = (Bytef*)hello;
365 c_stream.next_out = compr;
366 c_stream.avail_in = 3;
367 c_stream.avail_out = (uInt)*comprLen;
368 err = deflate(&c_stream, Z_FULL_FLUSH);
369 CHECK_ERR(err, "deflate");
371 compr[3]++; /* force an error in first compressed block */
372 c_stream.avail_in = len - 3;
374 err = deflate(&c_stream, Z_FINISH);
375 if (err != Z_STREAM_END) {
376 CHECK_ERR(err, "deflate");
378 err = deflateEnd(&c_stream);
379 CHECK_ERR(err, "deflateEnd");
381 *comprLen = c_stream.total_out;
384 /* ===========================================================================
387 void test_sync(compr, comprLen, uncompr, uncomprLen)
388 Byte *compr, *uncompr;
389 uLong comprLen, uncomprLen;
392 z_stream d_stream; /* decompression stream */
394 strcpy((char*)uncompr, "garbage");
396 d_stream.zalloc = (alloc_func)0;
397 d_stream.zfree = (free_func)0;
398 d_stream.opaque = (voidpf)0;
400 d_stream.next_in = compr;
401 d_stream.avail_in = 2; /* just read the zlib header */
403 err = inflateInit(&d_stream);
404 CHECK_ERR(err, "inflateInit");
406 d_stream.next_out = uncompr;
407 d_stream.avail_out = (uInt)uncomprLen;
409 inflate(&d_stream, Z_NO_FLUSH);
410 CHECK_ERR(err, "inflate");
412 d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */
413 err = inflateSync(&d_stream); /* but skip the damaged part */
414 CHECK_ERR(err, "inflateSync");
416 err = inflate(&d_stream, Z_FINISH);
417 if (err != Z_DATA_ERROR) {
418 fprintf(stderr, "inflate should report DATA_ERROR\n");
419 /* Because of incorrect adler32 */
422 err = inflateEnd(&d_stream);
423 CHECK_ERR(err, "inflateEnd");
425 printf("after inflateSync(): hel%s\n", (char *)uncompr);
428 /* ===========================================================================
429 * Test deflate() with preset dictionary
431 void test_dict_deflate(compr, comprLen)
435 z_stream c_stream; /* compression stream */
438 c_stream.zalloc = (alloc_func)0;
439 c_stream.zfree = (free_func)0;
440 c_stream.opaque = (voidpf)0;
442 err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
443 CHECK_ERR(err, "deflateInit");
445 err = deflateSetDictionary(&c_stream,
446 (const Bytef*)dictionary, sizeof(dictionary));
447 CHECK_ERR(err, "deflateSetDictionary");
449 dictId = c_stream.adler;
450 c_stream.next_out = compr;
451 c_stream.avail_out = (uInt)comprLen;
453 c_stream.next_in = (Bytef*)hello;
454 c_stream.avail_in = (uInt)strlen(hello)+1;
456 err = deflate(&c_stream, Z_FINISH);
457 if (err != Z_STREAM_END) {
458 fprintf(stderr, "deflate should report Z_STREAM_END\n");
461 err = deflateEnd(&c_stream);
462 CHECK_ERR(err, "deflateEnd");
465 /* ===========================================================================
466 * Test inflate() with a preset dictionary
468 void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
469 Byte *compr, *uncompr;
470 uLong comprLen, uncomprLen;
473 z_stream d_stream; /* decompression stream */
475 strcpy((char*)uncompr, "garbage");
477 d_stream.zalloc = (alloc_func)0;
478 d_stream.zfree = (free_func)0;
479 d_stream.opaque = (voidpf)0;
481 d_stream.next_in = compr;
482 d_stream.avail_in = (uInt)comprLen;
484 err = inflateInit(&d_stream);
485 CHECK_ERR(err, "inflateInit");
487 d_stream.next_out = uncompr;
488 d_stream.avail_out = (uInt)uncomprLen;
491 err = inflate(&d_stream, Z_NO_FLUSH);
492 if (err == Z_STREAM_END) break;
493 if (err == Z_NEED_DICT) {
494 if (d_stream.adler != dictId) {
495 fprintf(stderr, "unexpected dictionary");
498 err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
501 CHECK_ERR(err, "inflate with dict");
504 err = inflateEnd(&d_stream);
505 CHECK_ERR(err, "inflateEnd");
507 if (strcmp((char*)uncompr, hello)) {
508 fprintf(stderr, "bad inflate with dict\n");
511 printf("inflate with dictionary: %s\n", (char *)uncompr);
515 /* ===========================================================================
516 * Usage: example [output.gz [input.gz]]
523 Byte *compr, *uncompr;
524 uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
525 uLong uncomprLen = comprLen;
526 static const char* myVersion = ZLIB_VERSION;
528 if (zlibVersion()[0] != myVersion[0]) {
529 fprintf(stderr, "incompatible zlib version\n");
532 } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
533 fprintf(stderr, "warning: different zlib version\n");
536 printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
537 ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());
539 compr = (Byte*)calloc((uInt)comprLen, 1);
540 uncompr = (Byte*)calloc((uInt)uncomprLen, 1);
541 /* compr and uncompr are cleared to avoid reading uninitialized
542 * data and to ensure that uncompr compresses well.
544 if (compr == Z_NULL || uncompr == Z_NULL) {
545 printf("out of memory\n");
549 test_compress(compr, comprLen, uncompr, uncomprLen);
551 test_gzio((argc > 1 ? argv[1] : TESTFILE),
552 uncompr, uncomprLen);
554 test_deflate(compr, comprLen);
555 test_inflate(compr, comprLen, uncompr, uncomprLen);
557 test_large_deflate(compr, comprLen, uncompr, uncomprLen);
558 test_large_inflate(compr, comprLen, uncompr, uncomprLen);
560 test_flush(compr, &comprLen);
561 test_sync(compr, comprLen, uncompr, uncomprLen);
562 comprLen = uncomprLen;
564 test_dict_deflate(compr, comprLen);
565 test_dict_inflate(compr, comprLen, uncompr, uncomprLen);