1 /* example.c -- usage example of the zlib compression library
2 * Copyright (C) 1995-2006, 2011, 2016 Jean-loup Gailly
3 * For conditions of distribution and use, see copyright notice in zlib.h
16 #if defined(VMS) || defined(RISCOS)
17 # define TESTFILE "foo-gz"
19 # define TESTFILE "foo.gz"
22 #define CHECK_ERR(err, msg) { \
24 fprintf(stderr, "%s error: %d\n", msg, err); \
29 static z_const char hello[] = "hello, hello!";
30 /* "hello world" would be more standard, but the repeated "hello"
31 * stresses the compression code better, sorry...
34 static const char dictionary[] = "hello";
35 static uLong dictId; /* Adler32 value of the dictionary */
39 static void *myalloc(void *q, unsigned n, unsigned m) {
44 static void myfree(void *q, void *p) {
49 static alloc_func zalloc = myalloc;
50 static free_func zfree = myfree;
54 static alloc_func zalloc = (alloc_func)0;
55 static free_func zfree = (free_func)0;
57 /* ===========================================================================
58 * Test compress() and uncompress()
60 static void test_compress(Byte *compr, uLong comprLen, Byte *uncompr,
63 uLong len = (uLong)strlen(hello)+1;
65 err = compress(compr, &comprLen, (const Bytef*)hello, len);
66 CHECK_ERR(err, "compress");
68 strcpy((char*)uncompr, "garbage");
70 err = uncompress(uncompr, &uncomprLen, compr, comprLen);
71 CHECK_ERR(err, "uncompress");
73 if (strcmp((char*)uncompr, hello)) {
74 fprintf(stderr, "bad uncompress\n");
77 printf("uncompress(): %s\n", (char *)uncompr);
81 /* ===========================================================================
82 * Test read/write of .gz files
84 static void test_gzio(const char *fname, Byte *uncompr, uLong uncomprLen) {
86 fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
89 int len = (int)strlen(hello)+1;
93 file = gzopen(fname, "wb");
95 fprintf(stderr, "gzopen error\n");
99 if (gzputs(file, "ello") != 4) {
100 fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
103 if (gzprintf(file, ", %s!", "hello") != 8) {
104 fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
107 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
110 file = gzopen(fname, "rb");
112 fprintf(stderr, "gzopen error\n");
115 strcpy((char*)uncompr, "garbage");
117 if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
118 fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
121 if (strcmp((char*)uncompr, hello)) {
122 fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
125 printf("gzread(): %s\n", (char*)uncompr);
128 pos = gzseek(file, -8L, SEEK_CUR);
129 if (pos != 6 || gztell(file) != pos) {
130 fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
131 (long)pos, (long)gztell(file));
135 if (gzgetc(file) != ' ') {
136 fprintf(stderr, "gzgetc error\n");
140 if (gzungetc(' ', file) != ' ') {
141 fprintf(stderr, "gzungetc error\n");
145 gzgets(file, (char*)uncompr, (int)uncomprLen);
146 if (strlen((char*)uncompr) != 7) { /* " hello!" */
147 fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
150 if (strcmp((char*)uncompr, hello + 6)) {
151 fprintf(stderr, "bad gzgets after gzseek\n");
154 printf("gzgets() after gzseek: %s\n", (char*)uncompr);
163 /* ===========================================================================
164 * Test deflate() with small buffers
166 static void test_deflate(Byte *compr, uLong comprLen) {
167 z_stream c_stream; /* compression stream */
169 uLong len = (uLong)strlen(hello)+1;
171 c_stream.zalloc = zalloc;
172 c_stream.zfree = zfree;
173 c_stream.opaque = (voidpf)0;
175 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
176 CHECK_ERR(err, "deflateInit");
178 c_stream.next_in = (z_const unsigned char *)hello;
179 c_stream.next_out = compr;
181 while (c_stream.total_in != len && c_stream.total_out < comprLen) {
182 c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
183 err = deflate(&c_stream, Z_NO_FLUSH);
184 CHECK_ERR(err, "deflate");
186 /* Finish the stream, still forcing small buffers: */
188 c_stream.avail_out = 1;
189 err = deflate(&c_stream, Z_FINISH);
190 if (err == Z_STREAM_END) break;
191 CHECK_ERR(err, "deflate");
194 err = deflateEnd(&c_stream);
195 CHECK_ERR(err, "deflateEnd");
198 /* ===========================================================================
199 * Test inflate() with small buffers
201 static void test_inflate(Byte *compr, uLong comprLen, Byte *uncompr,
204 z_stream d_stream; /* decompression stream */
206 strcpy((char*)uncompr, "garbage");
208 d_stream.zalloc = zalloc;
209 d_stream.zfree = zfree;
210 d_stream.opaque = (voidpf)0;
212 d_stream.next_in = compr;
213 d_stream.avail_in = 0;
214 d_stream.next_out = uncompr;
216 err = inflateInit(&d_stream);
217 CHECK_ERR(err, "inflateInit");
219 while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
220 d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
221 err = inflate(&d_stream, Z_NO_FLUSH);
222 if (err == Z_STREAM_END) break;
223 CHECK_ERR(err, "inflate");
226 err = inflateEnd(&d_stream);
227 CHECK_ERR(err, "inflateEnd");
229 if (strcmp((char*)uncompr, hello)) {
230 fprintf(stderr, "bad inflate\n");
233 printf("inflate(): %s\n", (char *)uncompr);
237 /* ===========================================================================
238 * Test deflate() with large buffers and dynamic change of compression level
240 static void test_large_deflate(Byte *compr, uLong comprLen, Byte *uncompr,
242 z_stream c_stream; /* compression stream */
245 c_stream.zalloc = zalloc;
246 c_stream.zfree = zfree;
247 c_stream.opaque = (voidpf)0;
249 err = deflateInit(&c_stream, Z_BEST_SPEED);
250 CHECK_ERR(err, "deflateInit");
252 c_stream.next_out = compr;
253 c_stream.avail_out = (uInt)comprLen;
255 /* At this point, uncompr is still mostly zeroes, so it should compress
258 c_stream.next_in = uncompr;
259 c_stream.avail_in = (uInt)uncomprLen;
260 err = deflate(&c_stream, Z_NO_FLUSH);
261 CHECK_ERR(err, "deflate");
262 if (c_stream.avail_in != 0) {
263 fprintf(stderr, "deflate not greedy\n");
267 /* Feed in already compressed data and switch to no compression: */
268 deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
269 c_stream.next_in = compr;
270 c_stream.avail_in = (uInt)uncomprLen/2;
271 err = deflate(&c_stream, Z_NO_FLUSH);
272 CHECK_ERR(err, "deflate");
274 /* Switch back to compressing mode: */
275 deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
276 c_stream.next_in = uncompr;
277 c_stream.avail_in = (uInt)uncomprLen;
278 err = deflate(&c_stream, Z_NO_FLUSH);
279 CHECK_ERR(err, "deflate");
281 err = deflate(&c_stream, Z_FINISH);
282 if (err != Z_STREAM_END) {
283 fprintf(stderr, "deflate should report Z_STREAM_END\n");
286 err = deflateEnd(&c_stream);
287 CHECK_ERR(err, "deflateEnd");
290 /* ===========================================================================
291 * Test inflate() with large buffers
293 static void test_large_inflate(Byte *compr, uLong comprLen, Byte *uncompr,
296 z_stream d_stream; /* decompression stream */
298 strcpy((char*)uncompr, "garbage");
300 d_stream.zalloc = zalloc;
301 d_stream.zfree = zfree;
302 d_stream.opaque = (voidpf)0;
304 d_stream.next_in = compr;
305 d_stream.avail_in = (uInt)comprLen;
307 err = inflateInit(&d_stream);
308 CHECK_ERR(err, "inflateInit");
311 d_stream.next_out = uncompr; /* discard the output */
312 d_stream.avail_out = (uInt)uncomprLen;
313 err = inflate(&d_stream, Z_NO_FLUSH);
314 if (err == Z_STREAM_END) break;
315 CHECK_ERR(err, "large inflate");
318 err = inflateEnd(&d_stream);
319 CHECK_ERR(err, "inflateEnd");
321 if (d_stream.total_out != 2*uncomprLen + uncomprLen/2) {
322 fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
325 printf("large_inflate(): OK\n");
329 /* ===========================================================================
330 * Test deflate() with full flush
332 static void test_flush(Byte *compr, uLong *comprLen) {
333 z_stream c_stream; /* compression stream */
335 uInt len = (uInt)strlen(hello)+1;
337 c_stream.zalloc = zalloc;
338 c_stream.zfree = zfree;
339 c_stream.opaque = (voidpf)0;
341 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
342 CHECK_ERR(err, "deflateInit");
344 c_stream.next_in = (z_const unsigned char *)hello;
345 c_stream.next_out = compr;
346 c_stream.avail_in = 3;
347 c_stream.avail_out = (uInt)*comprLen;
348 err = deflate(&c_stream, Z_FULL_FLUSH);
349 CHECK_ERR(err, "deflate");
351 compr[3]++; /* force an error in first compressed block */
352 c_stream.avail_in = len - 3;
354 err = deflate(&c_stream, Z_FINISH);
355 if (err != Z_STREAM_END) {
356 CHECK_ERR(err, "deflate");
358 err = deflateEnd(&c_stream);
359 CHECK_ERR(err, "deflateEnd");
361 *comprLen = c_stream.total_out;
364 /* ===========================================================================
367 static void test_sync(Byte *compr, uLong comprLen, Byte *uncompr,
370 z_stream d_stream; /* decompression stream */
372 strcpy((char*)uncompr, "garbage");
374 d_stream.zalloc = zalloc;
375 d_stream.zfree = zfree;
376 d_stream.opaque = (voidpf)0;
378 d_stream.next_in = compr;
379 d_stream.avail_in = 2; /* just read the zlib header */
381 err = inflateInit(&d_stream);
382 CHECK_ERR(err, "inflateInit");
384 d_stream.next_out = uncompr;
385 d_stream.avail_out = (uInt)uncomprLen;
387 err = inflate(&d_stream, Z_NO_FLUSH);
388 CHECK_ERR(err, "inflate");
390 d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */
391 err = inflateSync(&d_stream); /* but skip the damaged part */
392 CHECK_ERR(err, "inflateSync");
394 err = inflate(&d_stream, Z_FINISH);
395 if (err != Z_STREAM_END) {
396 fprintf(stderr, "inflate should report Z_STREAM_END\n");
399 err = inflateEnd(&d_stream);
400 CHECK_ERR(err, "inflateEnd");
402 printf("after inflateSync(): hel%s\n", (char *)uncompr);
405 /* ===========================================================================
406 * Test deflate() with preset dictionary
408 static void test_dict_deflate(Byte *compr, uLong comprLen) {
409 z_stream c_stream; /* compression stream */
412 c_stream.zalloc = zalloc;
413 c_stream.zfree = zfree;
414 c_stream.opaque = (voidpf)0;
416 err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
417 CHECK_ERR(err, "deflateInit");
419 err = deflateSetDictionary(&c_stream,
420 (const Bytef*)dictionary, (int)sizeof(dictionary));
421 CHECK_ERR(err, "deflateSetDictionary");
423 dictId = c_stream.adler;
424 c_stream.next_out = compr;
425 c_stream.avail_out = (uInt)comprLen;
427 c_stream.next_in = (z_const unsigned char *)hello;
428 c_stream.avail_in = (uInt)strlen(hello)+1;
430 err = deflate(&c_stream, Z_FINISH);
431 if (err != Z_STREAM_END) {
432 fprintf(stderr, "deflate should report Z_STREAM_END\n");
435 err = deflateEnd(&c_stream);
436 CHECK_ERR(err, "deflateEnd");
439 /* ===========================================================================
440 * Test inflate() with a preset dictionary
442 static void test_dict_inflate(Byte *compr, uLong comprLen, Byte *uncompr,
445 z_stream d_stream; /* decompression stream */
447 strcpy((char*)uncompr, "garbage");
449 d_stream.zalloc = zalloc;
450 d_stream.zfree = zfree;
451 d_stream.opaque = (voidpf)0;
453 d_stream.next_in = compr;
454 d_stream.avail_in = (uInt)comprLen;
456 err = inflateInit(&d_stream);
457 CHECK_ERR(err, "inflateInit");
459 d_stream.next_out = uncompr;
460 d_stream.avail_out = (uInt)uncomprLen;
463 err = inflate(&d_stream, Z_NO_FLUSH);
464 if (err == Z_STREAM_END) break;
465 if (err == Z_NEED_DICT) {
466 if (d_stream.adler != dictId) {
467 fprintf(stderr, "unexpected dictionary");
470 err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
471 (int)sizeof(dictionary));
473 CHECK_ERR(err, "inflate with dict");
476 err = inflateEnd(&d_stream);
477 CHECK_ERR(err, "inflateEnd");
479 if (strcmp((char*)uncompr, hello)) {
480 fprintf(stderr, "bad inflate with dict\n");
483 printf("inflate with dictionary: %s\n", (char *)uncompr);
487 /* ===========================================================================
488 * Usage: example [output.gz [input.gz]]
491 int main(int argc, char *argv[]) {
492 Byte *compr, *uncompr;
493 uLong uncomprLen = 20000;
494 uLong comprLen = 3 * uncomprLen;
495 static const char* myVersion = ZLIB_VERSION;
497 if (zlibVersion()[0] != myVersion[0]) {
498 fprintf(stderr, "incompatible zlib version\n");
501 } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
502 fprintf(stderr, "warning: different zlib version linked: %s\n",
506 printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
507 ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());
509 compr = (Byte*)calloc((uInt)comprLen, 1);
510 uncompr = (Byte*)calloc((uInt)uncomprLen, 1);
511 /* compr and uncompr are cleared to avoid reading uninitialized
512 * data and to ensure that uncompr compresses well.
514 if (compr == Z_NULL || uncompr == Z_NULL) {
515 printf("out of memory\n");
523 test_compress(compr, comprLen, uncompr, uncomprLen);
525 test_gzio((argc > 1 ? argv[1] : TESTFILE),
526 uncompr, uncomprLen);
529 test_deflate(compr, comprLen);
530 test_inflate(compr, comprLen, uncompr, uncomprLen);
532 test_large_deflate(compr, comprLen, uncompr, uncomprLen);
533 test_large_inflate(compr, comprLen, uncompr, uncomprLen);
535 test_flush(compr, &comprLen);
536 test_sync(compr, comprLen, uncompr, uncomprLen);
537 comprLen = 3 * uncomprLen;
539 test_dict_deflate(compr, comprLen);
540 test_dict_inflate(compr, comprLen, uncompr, uncomprLen);