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