michael@0: /* example.c -- usage example of the zlib compression library michael@0: * Copyright (C) 1995-2006 Jean-loup Gailly. michael@0: * For conditions of distribution and use, see copyright notice in zlib.h michael@0: */ michael@0: michael@0: /* @(#) $Id$ */ michael@0: michael@0: #include "zlib.h" michael@0: #include michael@0: michael@0: #ifdef STDC michael@0: # include michael@0: # include michael@0: #endif michael@0: michael@0: #if defined(VMS) || defined(RISCOS) michael@0: # define TESTFILE "foo-gz" michael@0: #else michael@0: # define TESTFILE "foo.gz" michael@0: #endif michael@0: michael@0: #define CHECK_ERR(err, msg) { \ michael@0: if (err != Z_OK) { \ michael@0: fprintf(stderr, "%s error: %d\n", msg, err); \ michael@0: exit(1); \ michael@0: } \ michael@0: } michael@0: michael@0: const char hello[] = "hello, hello!"; michael@0: /* "hello world" would be more standard, but the repeated "hello" michael@0: * stresses the compression code better, sorry... michael@0: */ michael@0: michael@0: const char dictionary[] = "hello"; michael@0: uLong dictId; /* Adler32 value of the dictionary */ michael@0: michael@0: void test_compress OF((Byte *compr, uLong comprLen, michael@0: Byte *uncompr, uLong uncomprLen)); michael@0: void test_gzio OF((const char *fname, michael@0: Byte *uncompr, uLong uncomprLen)); michael@0: void test_deflate OF((Byte *compr, uLong comprLen)); michael@0: void test_inflate OF((Byte *compr, uLong comprLen, michael@0: Byte *uncompr, uLong uncomprLen)); michael@0: void test_large_deflate OF((Byte *compr, uLong comprLen, michael@0: Byte *uncompr, uLong uncomprLen)); michael@0: void test_large_inflate OF((Byte *compr, uLong comprLen, michael@0: Byte *uncompr, uLong uncomprLen)); michael@0: void test_flush OF((Byte *compr, uLong *comprLen)); michael@0: void test_sync OF((Byte *compr, uLong comprLen, michael@0: Byte *uncompr, uLong uncomprLen)); michael@0: void test_dict_deflate OF((Byte *compr, uLong comprLen)); michael@0: void test_dict_inflate OF((Byte *compr, uLong comprLen, michael@0: Byte *uncompr, uLong uncomprLen)); michael@0: int main OF((int argc, char *argv[])); michael@0: michael@0: /* =========================================================================== michael@0: * Test compress() and uncompress() michael@0: */ michael@0: void test_compress(compr, comprLen, uncompr, uncomprLen) michael@0: Byte *compr, *uncompr; michael@0: uLong comprLen, uncomprLen; michael@0: { michael@0: int err; michael@0: uLong len = (uLong)strlen(hello)+1; michael@0: michael@0: err = compress(compr, &comprLen, (const Bytef*)hello, len); michael@0: CHECK_ERR(err, "compress"); michael@0: michael@0: strcpy((char*)uncompr, "garbage"); michael@0: michael@0: err = uncompress(uncompr, &uncomprLen, compr, comprLen); michael@0: CHECK_ERR(err, "uncompress"); michael@0: michael@0: if (strcmp((char*)uncompr, hello)) { michael@0: fprintf(stderr, "bad uncompress\n"); michael@0: exit(1); michael@0: } else { michael@0: printf("uncompress(): %s\n", (char *)uncompr); michael@0: } michael@0: } michael@0: michael@0: /* =========================================================================== michael@0: * Test read/write of .gz files michael@0: */ michael@0: void test_gzio(fname, uncompr, uncomprLen) michael@0: const char *fname; /* compressed file name */ michael@0: Byte *uncompr; michael@0: uLong uncomprLen; michael@0: { michael@0: #ifdef NO_GZCOMPRESS michael@0: fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n"); michael@0: #else michael@0: int err; michael@0: int len = (int)strlen(hello)+1; michael@0: gzFile file; michael@0: z_off_t pos; michael@0: michael@0: file = gzopen(fname, "wb"); michael@0: if (file == NULL) { michael@0: fprintf(stderr, "gzopen error\n"); michael@0: exit(1); michael@0: } michael@0: gzputc(file, 'h'); michael@0: if (gzputs(file, "ello") != 4) { michael@0: fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err)); michael@0: exit(1); michael@0: } michael@0: if (gzprintf(file, ", %s!", "hello") != 8) { michael@0: fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err)); michael@0: exit(1); michael@0: } michael@0: gzseek(file, 1L, SEEK_CUR); /* add one zero byte */ michael@0: gzclose(file); michael@0: michael@0: file = gzopen(fname, "rb"); michael@0: if (file == NULL) { michael@0: fprintf(stderr, "gzopen error\n"); michael@0: exit(1); michael@0: } michael@0: strcpy((char*)uncompr, "garbage"); michael@0: michael@0: if (gzread(file, uncompr, (unsigned)uncomprLen) != len) { michael@0: fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); michael@0: exit(1); michael@0: } michael@0: if (strcmp((char*)uncompr, hello)) { michael@0: fprintf(stderr, "bad gzread: %s\n", (char*)uncompr); michael@0: exit(1); michael@0: } else { michael@0: printf("gzread(): %s\n", (char*)uncompr); michael@0: } michael@0: michael@0: pos = gzseek(file, -8L, SEEK_CUR); michael@0: if (pos != 6 || gztell(file) != pos) { michael@0: fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n", michael@0: (long)pos, (long)gztell(file)); michael@0: exit(1); michael@0: } michael@0: michael@0: if (gzgetc(file) != ' ') { michael@0: fprintf(stderr, "gzgetc error\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: if (gzungetc(' ', file) != ' ') { michael@0: fprintf(stderr, "gzungetc error\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: gzgets(file, (char*)uncompr, (int)uncomprLen); michael@0: if (strlen((char*)uncompr) != 7) { /* " hello!" */ michael@0: fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err)); michael@0: exit(1); michael@0: } michael@0: if (strcmp((char*)uncompr, hello + 6)) { michael@0: fprintf(stderr, "bad gzgets after gzseek\n"); michael@0: exit(1); michael@0: } else { michael@0: printf("gzgets() after gzseek: %s\n", (char*)uncompr); michael@0: } michael@0: michael@0: gzclose(file); michael@0: #endif michael@0: } michael@0: michael@0: /* =========================================================================== michael@0: * Test deflate() with small buffers michael@0: */ michael@0: void test_deflate(compr, comprLen) michael@0: Byte *compr; michael@0: uLong comprLen; michael@0: { michael@0: z_stream c_stream; /* compression stream */ michael@0: int err; michael@0: uLong len = (uLong)strlen(hello)+1; michael@0: michael@0: c_stream.zalloc = (alloc_func)0; michael@0: c_stream.zfree = (free_func)0; michael@0: c_stream.opaque = (voidpf)0; michael@0: michael@0: err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); michael@0: CHECK_ERR(err, "deflateInit"); michael@0: michael@0: c_stream.next_in = (Bytef*)hello; michael@0: c_stream.next_out = compr; michael@0: michael@0: while (c_stream.total_in != len && c_stream.total_out < comprLen) { michael@0: c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */ michael@0: err = deflate(&c_stream, Z_NO_FLUSH); michael@0: CHECK_ERR(err, "deflate"); michael@0: } michael@0: /* Finish the stream, still forcing small buffers: */ michael@0: for (;;) { michael@0: c_stream.avail_out = 1; michael@0: err = deflate(&c_stream, Z_FINISH); michael@0: if (err == Z_STREAM_END) break; michael@0: CHECK_ERR(err, "deflate"); michael@0: } michael@0: michael@0: err = deflateEnd(&c_stream); michael@0: CHECK_ERR(err, "deflateEnd"); michael@0: } michael@0: michael@0: /* =========================================================================== michael@0: * Test inflate() with small buffers michael@0: */ michael@0: void test_inflate(compr, comprLen, uncompr, uncomprLen) michael@0: Byte *compr, *uncompr; michael@0: uLong comprLen, uncomprLen; michael@0: { michael@0: int err; michael@0: z_stream d_stream; /* decompression stream */ michael@0: michael@0: strcpy((char*)uncompr, "garbage"); michael@0: michael@0: d_stream.zalloc = (alloc_func)0; michael@0: d_stream.zfree = (free_func)0; michael@0: d_stream.opaque = (voidpf)0; michael@0: michael@0: d_stream.next_in = compr; michael@0: d_stream.avail_in = 0; michael@0: d_stream.next_out = uncompr; michael@0: michael@0: err = inflateInit(&d_stream); michael@0: CHECK_ERR(err, "inflateInit"); michael@0: michael@0: while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) { michael@0: d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ michael@0: err = inflate(&d_stream, Z_NO_FLUSH); michael@0: if (err == Z_STREAM_END) break; michael@0: CHECK_ERR(err, "inflate"); michael@0: } michael@0: michael@0: err = inflateEnd(&d_stream); michael@0: CHECK_ERR(err, "inflateEnd"); michael@0: michael@0: if (strcmp((char*)uncompr, hello)) { michael@0: fprintf(stderr, "bad inflate\n"); michael@0: exit(1); michael@0: } else { michael@0: printf("inflate(): %s\n", (char *)uncompr); michael@0: } michael@0: } michael@0: michael@0: /* =========================================================================== michael@0: * Test deflate() with large buffers and dynamic change of compression level michael@0: */ michael@0: void test_large_deflate(compr, comprLen, uncompr, uncomprLen) michael@0: Byte *compr, *uncompr; michael@0: uLong comprLen, uncomprLen; michael@0: { michael@0: z_stream c_stream; /* compression stream */ michael@0: int err; michael@0: michael@0: c_stream.zalloc = (alloc_func)0; michael@0: c_stream.zfree = (free_func)0; michael@0: c_stream.opaque = (voidpf)0; michael@0: michael@0: err = deflateInit(&c_stream, Z_BEST_SPEED); michael@0: CHECK_ERR(err, "deflateInit"); michael@0: michael@0: c_stream.next_out = compr; michael@0: c_stream.avail_out = (uInt)comprLen; michael@0: michael@0: /* At this point, uncompr is still mostly zeroes, so it should compress michael@0: * very well: michael@0: */ michael@0: c_stream.next_in = uncompr; michael@0: c_stream.avail_in = (uInt)uncomprLen; michael@0: err = deflate(&c_stream, Z_NO_FLUSH); michael@0: CHECK_ERR(err, "deflate"); michael@0: if (c_stream.avail_in != 0) { michael@0: fprintf(stderr, "deflate not greedy\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: /* Feed in already compressed data and switch to no compression: */ michael@0: deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY); michael@0: c_stream.next_in = compr; michael@0: c_stream.avail_in = (uInt)comprLen/2; michael@0: err = deflate(&c_stream, Z_NO_FLUSH); michael@0: CHECK_ERR(err, "deflate"); michael@0: michael@0: /* Switch back to compressing mode: */ michael@0: deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED); michael@0: c_stream.next_in = uncompr; michael@0: c_stream.avail_in = (uInt)uncomprLen; michael@0: err = deflate(&c_stream, Z_NO_FLUSH); michael@0: CHECK_ERR(err, "deflate"); michael@0: michael@0: err = deflate(&c_stream, Z_FINISH); michael@0: if (err != Z_STREAM_END) { michael@0: fprintf(stderr, "deflate should report Z_STREAM_END\n"); michael@0: exit(1); michael@0: } michael@0: err = deflateEnd(&c_stream); michael@0: CHECK_ERR(err, "deflateEnd"); michael@0: } michael@0: michael@0: /* =========================================================================== michael@0: * Test inflate() with large buffers michael@0: */ michael@0: void test_large_inflate(compr, comprLen, uncompr, uncomprLen) michael@0: Byte *compr, *uncompr; michael@0: uLong comprLen, uncomprLen; michael@0: { michael@0: int err; michael@0: z_stream d_stream; /* decompression stream */ michael@0: michael@0: strcpy((char*)uncompr, "garbage"); michael@0: michael@0: d_stream.zalloc = (alloc_func)0; michael@0: d_stream.zfree = (free_func)0; michael@0: d_stream.opaque = (voidpf)0; michael@0: michael@0: d_stream.next_in = compr; michael@0: d_stream.avail_in = (uInt)comprLen; michael@0: michael@0: err = inflateInit(&d_stream); michael@0: CHECK_ERR(err, "inflateInit"); michael@0: michael@0: for (;;) { michael@0: d_stream.next_out = uncompr; /* discard the output */ michael@0: d_stream.avail_out = (uInt)uncomprLen; michael@0: err = inflate(&d_stream, Z_NO_FLUSH); michael@0: if (err == Z_STREAM_END) break; michael@0: CHECK_ERR(err, "large inflate"); michael@0: } michael@0: michael@0: err = inflateEnd(&d_stream); michael@0: CHECK_ERR(err, "inflateEnd"); michael@0: michael@0: if (d_stream.total_out != 2*uncomprLen + comprLen/2) { michael@0: fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out); michael@0: exit(1); michael@0: } else { michael@0: printf("large_inflate(): OK\n"); michael@0: } michael@0: } michael@0: michael@0: /* =========================================================================== michael@0: * Test deflate() with full flush michael@0: */ michael@0: void test_flush(compr, comprLen) michael@0: Byte *compr; michael@0: uLong *comprLen; michael@0: { michael@0: z_stream c_stream; /* compression stream */ michael@0: int err; michael@0: uInt len = (uInt)strlen(hello)+1; michael@0: michael@0: c_stream.zalloc = (alloc_func)0; michael@0: c_stream.zfree = (free_func)0; michael@0: c_stream.opaque = (voidpf)0; michael@0: michael@0: err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); michael@0: CHECK_ERR(err, "deflateInit"); michael@0: michael@0: c_stream.next_in = (Bytef*)hello; michael@0: c_stream.next_out = compr; michael@0: c_stream.avail_in = 3; michael@0: c_stream.avail_out = (uInt)*comprLen; michael@0: err = deflate(&c_stream, Z_FULL_FLUSH); michael@0: CHECK_ERR(err, "deflate"); michael@0: michael@0: compr[3]++; /* force an error in first compressed block */ michael@0: c_stream.avail_in = len - 3; michael@0: michael@0: err = deflate(&c_stream, Z_FINISH); michael@0: if (err != Z_STREAM_END) { michael@0: CHECK_ERR(err, "deflate"); michael@0: } michael@0: err = deflateEnd(&c_stream); michael@0: CHECK_ERR(err, "deflateEnd"); michael@0: michael@0: *comprLen = c_stream.total_out; michael@0: } michael@0: michael@0: /* =========================================================================== michael@0: * Test inflateSync() michael@0: */ michael@0: void test_sync(compr, comprLen, uncompr, uncomprLen) michael@0: Byte *compr, *uncompr; michael@0: uLong comprLen, uncomprLen; michael@0: { michael@0: int err; michael@0: z_stream d_stream; /* decompression stream */ michael@0: michael@0: strcpy((char*)uncompr, "garbage"); michael@0: michael@0: d_stream.zalloc = (alloc_func)0; michael@0: d_stream.zfree = (free_func)0; michael@0: d_stream.opaque = (voidpf)0; michael@0: michael@0: d_stream.next_in = compr; michael@0: d_stream.avail_in = 2; /* just read the zlib header */ michael@0: michael@0: err = inflateInit(&d_stream); michael@0: CHECK_ERR(err, "inflateInit"); michael@0: michael@0: d_stream.next_out = uncompr; michael@0: d_stream.avail_out = (uInt)uncomprLen; michael@0: michael@0: inflate(&d_stream, Z_NO_FLUSH); michael@0: CHECK_ERR(err, "inflate"); michael@0: michael@0: d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */ michael@0: err = inflateSync(&d_stream); /* but skip the damaged part */ michael@0: CHECK_ERR(err, "inflateSync"); michael@0: michael@0: err = inflate(&d_stream, Z_FINISH); michael@0: if (err != Z_DATA_ERROR) { michael@0: fprintf(stderr, "inflate should report DATA_ERROR\n"); michael@0: /* Because of incorrect adler32 */ michael@0: exit(1); michael@0: } michael@0: err = inflateEnd(&d_stream); michael@0: CHECK_ERR(err, "inflateEnd"); michael@0: michael@0: printf("after inflateSync(): hel%s\n", (char *)uncompr); michael@0: } michael@0: michael@0: /* =========================================================================== michael@0: * Test deflate() with preset dictionary michael@0: */ michael@0: void test_dict_deflate(compr, comprLen) michael@0: Byte *compr; michael@0: uLong comprLen; michael@0: { michael@0: z_stream c_stream; /* compression stream */ michael@0: int err; michael@0: michael@0: c_stream.zalloc = (alloc_func)0; michael@0: c_stream.zfree = (free_func)0; michael@0: c_stream.opaque = (voidpf)0; michael@0: michael@0: err = deflateInit(&c_stream, Z_BEST_COMPRESSION); michael@0: CHECK_ERR(err, "deflateInit"); michael@0: michael@0: err = deflateSetDictionary(&c_stream, michael@0: (const Bytef*)dictionary, sizeof(dictionary)); michael@0: CHECK_ERR(err, "deflateSetDictionary"); michael@0: michael@0: dictId = c_stream.adler; michael@0: c_stream.next_out = compr; michael@0: c_stream.avail_out = (uInt)comprLen; michael@0: michael@0: c_stream.next_in = (Bytef*)hello; michael@0: c_stream.avail_in = (uInt)strlen(hello)+1; michael@0: michael@0: err = deflate(&c_stream, Z_FINISH); michael@0: if (err != Z_STREAM_END) { michael@0: fprintf(stderr, "deflate should report Z_STREAM_END\n"); michael@0: exit(1); michael@0: } michael@0: err = deflateEnd(&c_stream); michael@0: CHECK_ERR(err, "deflateEnd"); michael@0: } michael@0: michael@0: /* =========================================================================== michael@0: * Test inflate() with a preset dictionary michael@0: */ michael@0: void test_dict_inflate(compr, comprLen, uncompr, uncomprLen) michael@0: Byte *compr, *uncompr; michael@0: uLong comprLen, uncomprLen; michael@0: { michael@0: int err; michael@0: z_stream d_stream; /* decompression stream */ michael@0: michael@0: strcpy((char*)uncompr, "garbage"); michael@0: michael@0: d_stream.zalloc = (alloc_func)0; michael@0: d_stream.zfree = (free_func)0; michael@0: d_stream.opaque = (voidpf)0; michael@0: michael@0: d_stream.next_in = compr; michael@0: d_stream.avail_in = (uInt)comprLen; michael@0: michael@0: err = inflateInit(&d_stream); michael@0: CHECK_ERR(err, "inflateInit"); michael@0: michael@0: d_stream.next_out = uncompr; michael@0: d_stream.avail_out = (uInt)uncomprLen; michael@0: michael@0: for (;;) { michael@0: err = inflate(&d_stream, Z_NO_FLUSH); michael@0: if (err == Z_STREAM_END) break; michael@0: if (err == Z_NEED_DICT) { michael@0: if (d_stream.adler != dictId) { michael@0: fprintf(stderr, "unexpected dictionary"); michael@0: exit(1); michael@0: } michael@0: err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary, michael@0: sizeof(dictionary)); michael@0: } michael@0: CHECK_ERR(err, "inflate with dict"); michael@0: } michael@0: michael@0: err = inflateEnd(&d_stream); michael@0: CHECK_ERR(err, "inflateEnd"); michael@0: michael@0: if (strcmp((char*)uncompr, hello)) { michael@0: fprintf(stderr, "bad inflate with dict\n"); michael@0: exit(1); michael@0: } else { michael@0: printf("inflate with dictionary: %s\n", (char *)uncompr); michael@0: } michael@0: } michael@0: michael@0: /* =========================================================================== michael@0: * Usage: example [output.gz [input.gz]] michael@0: */ michael@0: michael@0: int main(argc, argv) michael@0: int argc; michael@0: char *argv[]; michael@0: { michael@0: Byte *compr, *uncompr; michael@0: uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */ michael@0: uLong uncomprLen = comprLen; michael@0: static const char* myVersion = ZLIB_VERSION; michael@0: michael@0: if (zlibVersion()[0] != myVersion[0]) { michael@0: fprintf(stderr, "incompatible zlib version\n"); michael@0: exit(1); michael@0: michael@0: } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) { michael@0: fprintf(stderr, "warning: different zlib version\n"); michael@0: } michael@0: michael@0: printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n", michael@0: ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags()); michael@0: michael@0: compr = (Byte*)calloc((uInt)comprLen, 1); michael@0: uncompr = (Byte*)calloc((uInt)uncomprLen, 1); michael@0: /* compr and uncompr are cleared to avoid reading uninitialized michael@0: * data and to ensure that uncompr compresses well. michael@0: */ michael@0: if (compr == Z_NULL || uncompr == Z_NULL) { michael@0: printf("out of memory\n"); michael@0: exit(1); michael@0: } michael@0: test_compress(compr, comprLen, uncompr, uncomprLen); michael@0: michael@0: test_gzio((argc > 1 ? argv[1] : TESTFILE), michael@0: uncompr, uncomprLen); michael@0: michael@0: test_deflate(compr, comprLen); michael@0: test_inflate(compr, comprLen, uncompr, uncomprLen); michael@0: michael@0: test_large_deflate(compr, comprLen, uncompr, uncomprLen); michael@0: test_large_inflate(compr, comprLen, uncompr, uncomprLen); michael@0: michael@0: test_flush(compr, &comprLen); michael@0: test_sync(compr, comprLen, uncompr, uncomprLen); michael@0: comprLen = uncomprLen; michael@0: michael@0: test_dict_deflate(compr, comprLen); michael@0: test_dict_inflate(compr, comprLen, uncompr, uncomprLen); michael@0: michael@0: free(compr); michael@0: free(uncompr); michael@0: michael@0: return 0; michael@0: }