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