199767f8919635c4928607450d9e0abb932109ceToomas Soome/* example.c -- usage example of the zlib compression library
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (C) 1995-2006, 2011 Jean-loup Gailly.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * For conditions of distribution and use, see copyright notice in zlib.h
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* @(#) $Id$ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* "hello world" would be more standard, but the repeated "hello"
199767f8919635c4928607450d9e0abb932109ceToomas Soome * stresses the compression code better, sorry...
199767f8919635c4928607450d9e0abb932109ceToomas SoomeuLong dictId; /* Adler32 value of the dictionary */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid test_deflate OF((Byte *compr, uLong comprLen));
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid test_inflate OF((Byte *compr, uLong comprLen,
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid test_large_deflate OF((Byte *compr, uLong comprLen,
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid test_large_inflate OF((Byte *compr, uLong comprLen,
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid test_flush OF((Byte *compr, uLong *comprLen));
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid test_dict_deflate OF((Byte *compr, uLong comprLen));
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid test_dict_inflate OF((Byte *compr, uLong comprLen,
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned n, m;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return calloc(n, m);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else /* !Z_SOLO */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid test_compress OF((Byte *compr, uLong comprLen,
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Test compress() and uncompress()
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid test_compress(compr, comprLen, uncompr, uncomprLen)
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = compress(compr, &comprLen, (const Bytef*)hello, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = uncompress(uncompr, &uncomprLen, compr, comprLen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Test read/write of .gz files
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
199767f8919635c4928607450d9e0abb932109ceToomas Soome gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strlen((char*)uncompr) != 7) { /* " hello!" */
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("gzgets() after gzseek: %s\n", (char*)uncompr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* Z_SOLO */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Test deflate() with small buffers
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
199767f8919635c4928607450d9e0abb932109ceToomas Soome c_stream.next_in = (z_const unsigned char *)hello;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (c_stream.total_in != len && c_stream.total_out < comprLen) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Finish the stream, still forcing small buffers: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Test inflate() with small buffers
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid test_inflate(compr, comprLen, uncompr, uncomprLen)
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Test deflate() with large buffers and dynamic change of compression level
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid test_large_deflate(compr, comprLen, uncompr, uncomprLen)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* At this point, uncompr is still mostly zeroes, so it should compress
199767f8919635c4928607450d9e0abb932109ceToomas Soome * very well:
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Feed in already compressed data and switch to no compression: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Switch back to compressing mode: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(stderr, "deflate should report Z_STREAM_END\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Test inflate() with large buffers
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid test_large_inflate(compr, comprLen, uncompr, uncomprLen)
199767f8919635c4928607450d9e0abb932109ceToomas Soome d_stream.next_out = uncompr; /* discard the output */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Test deflate() with full flush
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
199767f8919635c4928607450d9e0abb932109ceToomas Soome c_stream.next_in = (z_const unsigned char *)hello;
199767f8919635c4928607450d9e0abb932109ceToomas Soome compr[3]++; /* force an error in first compressed block */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Test inflateSync()
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid test_sync(compr, comprLen, uncompr, uncomprLen)
199767f8919635c4928607450d9e0abb932109ceToomas Soome d_stream.avail_in = 2; /* just read the zlib header */
199767f8919635c4928607450d9e0abb932109ceToomas Soome d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = inflateSync(&d_stream); /* but skip the damaged part */
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(stderr, "inflate should report DATA_ERROR\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Because of incorrect adler32 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("after inflateSync(): hel%s\n", (char *)uncompr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Test deflate() with preset dictionary
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
199767f8919635c4928607450d9e0abb932109ceToomas Soome (const Bytef*)dictionary, (int)sizeof(dictionary));
199767f8919635c4928607450d9e0abb932109ceToomas Soome c_stream.next_in = (z_const unsigned char *)hello;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(stderr, "deflate should report Z_STREAM_END\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Test inflate() with a preset dictionary
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (int)sizeof(dictionary));
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("inflate with dictionary: %s\n", (char *)uncompr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ===========================================================================
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Usage: example [output.gz [input.gz]]
199767f8919635c4928607450d9e0abb932109ceToomas Soome uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(stderr, "incompatible zlib version\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(stderr, "warning: different zlib version\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* compr and uncompr are cleared to avoid reading uninitialized
199767f8919635c4928607450d9e0abb932109ceToomas Soome * data and to ensure that uncompr compresses well.
199767f8919635c4928607450d9e0abb932109ceToomas Soome test_compress(compr, comprLen, uncompr, uncomprLen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome test_inflate(compr, comprLen, uncompr, uncomprLen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome test_large_deflate(compr, comprLen, uncompr, uncomprLen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome test_large_inflate(compr, comprLen, uncompr, uncomprLen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome test_sync(compr, comprLen, uncompr, uncomprLen);