example.c 16.0 KB
Newer Older
M
Mark Adler 已提交
1
/* example.c -- usage example of the zlib compression library
M
Mark Adler 已提交
2
 * Copyright (C) 1995-2006 Jean-loup Gailly.
M
Mark Adler 已提交
3
 * For conditions of distribution and use, see copyright notice in zlib.h
M
Mark Adler 已提交
4 5
 */

M
Mark Adler 已提交
6
/* @(#) $Id$ */
M
Mark Adler 已提交
7 8

#include "zlib.h"
M
Mark Adler 已提交
9
#include <stdio.h>
M
Mark Adler 已提交
10

M
Mark Adler 已提交
11 12
#ifdef STDC
#  include <string.h>
M
Mark Adler 已提交
13
#  include <stdlib.h>
M
Mark Adler 已提交
14 15
#endif

M
Mark Adler 已提交
16 17 18 19 20 21
#if defined(VMS) || defined(RISCOS)
#  define TESTFILE "foo-gz"
#else
#  define TESTFILE "foo.gz"
#endif

M
Mark Adler 已提交
22 23 24
#define CHECK_ERR(err, msg) { \
    if (err != Z_OK) { \
        fprintf(stderr, "%s error: %d\n", msg, err); \
M
Mark Adler 已提交
25
        exit(1); \
M
Mark Adler 已提交
26 27 28
    } \
}

M
Mark Adler 已提交
29
const char hello[] = "hello, hello!";
M
Mark Adler 已提交
30 31 32
/* "hello world" would be more standard, but the repeated "hello"
 * stresses the compression code better, sorry...
 */
M
Mark Adler 已提交
33

M
Mark Adler 已提交
34 35 36 37
const char dictionary[] = "hello";
uLong dictId; /* Adler32 value of the dictionary */

void test_compress      OF((Byte *compr, uLong comprLen,
M
Mark Adler 已提交
38
                            Byte *uncompr, uLong uncomprLen));
M
Mark Adler 已提交
39 40
void test_gzio          OF((const char *fname,
                            Byte *uncompr, uLong uncomprLen));
M
Mark Adler 已提交
41 42
void test_deflate       OF((Byte *compr, uLong comprLen));
void test_inflate       OF((Byte *compr, uLong comprLen,
M
Mark Adler 已提交
43
                            Byte *uncompr, uLong uncomprLen));
M
Mark Adler 已提交
44
void test_large_deflate OF((Byte *compr, uLong comprLen,
M
Mark Adler 已提交
45
                            Byte *uncompr, uLong uncomprLen));
M
Mark Adler 已提交
46
void test_large_inflate OF((Byte *compr, uLong comprLen,
M
Mark Adler 已提交
47
                            Byte *uncompr, uLong uncomprLen));
M
Mark Adler 已提交
48
void test_flush         OF((Byte *compr, uLong *comprLen));
M
Mark Adler 已提交
49
void test_sync          OF((Byte *compr, uLong comprLen,
M
Mark Adler 已提交
50
                            Byte *uncompr, uLong uncomprLen));
M
Mark Adler 已提交
51 52
void test_dict_deflate  OF((Byte *compr, uLong comprLen));
void test_dict_inflate  OF((Byte *compr, uLong comprLen,
M
Mark Adler 已提交
53
                            Byte *uncompr, uLong uncomprLen));
M
Mark Adler 已提交
54
int  main               OF((int argc, char *argv[]));
M
Mark Adler 已提交
55

M
Mark Adler 已提交
56 57 58
/* ===========================================================================
 * Test compress() and uncompress()
 */
M
Mark Adler 已提交
59
void test_compress(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
60
    Byte *compr, *uncompr;
M
Mark Adler 已提交
61
    uLong comprLen, uncomprLen;
M
Mark Adler 已提交
62 63
{
    int err;
M
Mark Adler 已提交
64
    uLong len = (uLong)strlen(hello)+1;
M
Mark Adler 已提交
65

M
Mark Adler 已提交
66
    err = compress(compr, &comprLen, (const Bytef*)hello, len);
M
Mark Adler 已提交
67 68
    CHECK_ERR(err, "compress");

M
Mark Adler 已提交
69
    strcpy((char*)uncompr, "garbage");
M
Mark Adler 已提交
70 71 72 73

    err = uncompress(uncompr, &uncomprLen, compr, comprLen);
    CHECK_ERR(err, "uncompress");

M
Mark Adler 已提交
74
    if (strcmp((char*)uncompr, hello)) {
M
Mark Adler 已提交
75
        fprintf(stderr, "bad uncompress\n");
M
Mark Adler 已提交
76
        exit(1);
M
Mark Adler 已提交
77
    } else {
M
Mark Adler 已提交
78
        printf("uncompress(): %s\n", (char *)uncompr);
M
Mark Adler 已提交
79 80 81 82 83 84
    }
}

/* ===========================================================================
 * Test read/write of .gz files
 */
M
Mark Adler 已提交
85 86
void test_gzio(fname, uncompr, uncomprLen)
    const char *fname; /* compressed file name */
M
Mark Adler 已提交
87
    Byte *uncompr;
M
Mark Adler 已提交
88
    uLong uncomprLen;
M
Mark Adler 已提交
89
{
M
Mark Adler 已提交
90 91 92
#ifdef NO_GZCOMPRESS
    fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
#else
M
Mark Adler 已提交
93
    int err;
M
Mark Adler 已提交
94
    int len = (int)strlen(hello)+1;
M
Mark Adler 已提交
95
    gzFile file;
M
Mark Adler 已提交
96
    z_off_t pos;
M
Mark Adler 已提交
97

M
Mark Adler 已提交
98
    file = gzopen(fname, "wb");
M
Mark Adler 已提交
99
    if (file == NULL) {
M
Mark Adler 已提交
100 101
        fprintf(stderr, "gzopen error\n");
        exit(1);
M
Mark Adler 已提交
102
    }
M
Mark Adler 已提交
103
    gzputc(file, 'h');
M
Mark Adler 已提交
104 105
    if (gzputs(file, "ello") != 4) {
        fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
M
Mark Adler 已提交
106
        exit(1);
M
Mark Adler 已提交
107 108
    }
    if (gzprintf(file, ", %s!", "hello") != 8) {
M
Mark Adler 已提交
109
        fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
M
Mark Adler 已提交
110
        exit(1);
M
Mark Adler 已提交
111
    }
M
Mark Adler 已提交
112
    gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
M
Mark Adler 已提交
113 114
    gzclose(file);

M
Mark Adler 已提交
115
    file = gzopen(fname, "rb");
M
Mark Adler 已提交
116
    if (file == NULL) {
M
Mark Adler 已提交
117
        fprintf(stderr, "gzopen error\n");
M
Mark Adler 已提交
118
        exit(1);
M
Mark Adler 已提交
119
    }
M
Mark Adler 已提交
120
    strcpy((char*)uncompr, "garbage");
M
Mark Adler 已提交
121

M
Mark Adler 已提交
122
    if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
M
Mark Adler 已提交
123
        fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
M
Mark Adler 已提交
124
        exit(1);
M
Mark Adler 已提交
125
    }
M
Mark Adler 已提交
126
    if (strcmp((char*)uncompr, hello)) {
M
Mark Adler 已提交
127
        fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
M
Mark Adler 已提交
128
        exit(1);
M
Mark Adler 已提交
129
    } else {
M
Mark Adler 已提交
130
        printf("gzread(): %s\n", (char*)uncompr);
M
Mark Adler 已提交
131
    }
M
Mark Adler 已提交
132

M
Mark Adler 已提交
133 134
    pos = gzseek(file, -8L, SEEK_CUR);
    if (pos != 6 || gztell(file) != pos) {
M
Mark Adler 已提交
135 136 137
        fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
                (long)pos, (long)gztell(file));
        exit(1);
M
Mark Adler 已提交
138
    }
M
Mark Adler 已提交
139 140

    if (gzgetc(file) != ' ') {
M
Mark Adler 已提交
141 142
        fprintf(stderr, "gzgetc error\n");
        exit(1);
M
Mark Adler 已提交
143 144
    }

M
Mark Adler 已提交
145 146 147 148 149
    if (gzungetc(' ', file) != ' ') {
        fprintf(stderr, "gzungetc error\n");
        exit(1);
    }

M
Mark Adler 已提交
150 151
    gzgets(file, (char*)uncompr, (int)uncomprLen);
    if (strlen((char*)uncompr) != 7) { /* " hello!" */
M
Mark Adler 已提交
152
        fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
M
Mark Adler 已提交
153
        exit(1);
M
Mark Adler 已提交
154
    }
M
Mark Adler 已提交
155
    if (strcmp((char*)uncompr, hello + 6)) {
M
Mark Adler 已提交
156
        fprintf(stderr, "bad gzgets after gzseek\n");
M
Mark Adler 已提交
157
        exit(1);
M
Mark Adler 已提交
158
    } else {
M
Mark Adler 已提交
159
        printf("gzgets() after gzseek: %s\n", (char*)uncompr);
M
Mark Adler 已提交
160 161 162
    }

    gzclose(file);
M
Mark Adler 已提交
163
#endif
M
Mark Adler 已提交
164 165 166
}

/* ===========================================================================
M
Mark Adler 已提交
167
 * Test deflate() with small buffers
M
Mark Adler 已提交
168
 */
M
Mark Adler 已提交
169
void test_deflate(compr, comprLen)
M
Mark Adler 已提交
170
    Byte *compr;
M
Mark Adler 已提交
171
    uLong comprLen;
M
Mark Adler 已提交
172 173 174
{
    z_stream c_stream; /* compression stream */
    int err;
M
Mark Adler 已提交
175
    uLong len = (uLong)strlen(hello)+1;
M
Mark Adler 已提交
176 177 178

    c_stream.zalloc = (alloc_func)0;
    c_stream.zfree = (free_func)0;
M
Mark Adler 已提交
179
    c_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
180 181 182 183

    err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
    CHECK_ERR(err, "deflateInit");

M
Mark Adler 已提交
184
    c_stream.next_in  = (Bytef*)hello;
M
Mark Adler 已提交
185 186
    c_stream.next_out = compr;

M
Mark Adler 已提交
187
    while (c_stream.total_in != len && c_stream.total_out < comprLen) {
M
Mark Adler 已提交
188 189 190
        c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
        err = deflate(&c_stream, Z_NO_FLUSH);
        CHECK_ERR(err, "deflate");
M
Mark Adler 已提交
191 192
    }
    /* Finish the stream, still forcing small buffers: */
M
Mark Adler 已提交
193
    for (;;) {
M
Mark Adler 已提交
194 195 196 197
        c_stream.avail_out = 1;
        err = deflate(&c_stream, Z_FINISH);
        if (err == Z_STREAM_END) break;
        CHECK_ERR(err, "deflate");
M
Mark Adler 已提交
198
    }
M
Mark Adler 已提交
199 200 201 202 203 204 205 206

    err = deflateEnd(&c_stream);
    CHECK_ERR(err, "deflateEnd");
}

/* ===========================================================================
 * Test inflate() with small buffers
 */
M
Mark Adler 已提交
207
void test_inflate(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
208
    Byte *compr, *uncompr;
M
Mark Adler 已提交
209
    uLong comprLen, uncomprLen;
M
Mark Adler 已提交
210 211 212 213
{
    int err;
    z_stream d_stream; /* decompression stream */

M
Mark Adler 已提交
214
    strcpy((char*)uncompr, "garbage");
M
Mark Adler 已提交
215 216 217

    d_stream.zalloc = (alloc_func)0;
    d_stream.zfree = (free_func)0;
M
Mark Adler 已提交
218
    d_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
219 220

    d_stream.next_in  = compr;
M
Mark Adler 已提交
221
    d_stream.avail_in = 0;
M
Mark Adler 已提交
222 223
    d_stream.next_out = uncompr;

M
Mark Adler 已提交
224 225 226
    err = inflateInit(&d_stream);
    CHECK_ERR(err, "inflateInit");

M
Mark Adler 已提交
227
    while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
M
Mark Adler 已提交
228 229 230 231
        d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
        err = inflate(&d_stream, Z_NO_FLUSH);
        if (err == Z_STREAM_END) break;
        CHECK_ERR(err, "inflate");
M
Mark Adler 已提交
232 233 234 235 236
    }

    err = inflateEnd(&d_stream);
    CHECK_ERR(err, "inflateEnd");

M
Mark Adler 已提交
237
    if (strcmp((char*)uncompr, hello)) {
M
Mark Adler 已提交
238
        fprintf(stderr, "bad inflate\n");
M
Mark Adler 已提交
239
        exit(1);
M
Mark Adler 已提交
240
    } else {
M
Mark Adler 已提交
241
        printf("inflate(): %s\n", (char *)uncompr);
M
Mark Adler 已提交
242 243 244
    }
}

M
Mark Adler 已提交
245 246 247 248
/* ===========================================================================
 * Test deflate() with large buffers and dynamic change of compression level
 */
void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
249
    Byte *compr, *uncompr;
M
Mark Adler 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262
    uLong comprLen, uncomprLen;
{
    z_stream c_stream; /* compression stream */
    int err;

    c_stream.zalloc = (alloc_func)0;
    c_stream.zfree = (free_func)0;
    c_stream.opaque = (voidpf)0;

    err = deflateInit(&c_stream, Z_BEST_SPEED);
    CHECK_ERR(err, "deflateInit");

    c_stream.next_out = compr;
M
Mark Adler 已提交
263
    c_stream.avail_out = (uInt)comprLen;
M
Mark Adler 已提交
264 265 266 267 268 269 270 271 272 273

    /* At this point, uncompr is still mostly zeroes, so it should compress
     * very well:
     */
    c_stream.next_in = uncompr;
    c_stream.avail_in = (uInt)uncomprLen;
    err = deflate(&c_stream, Z_NO_FLUSH);
    CHECK_ERR(err, "deflate");
    if (c_stream.avail_in != 0) {
        fprintf(stderr, "deflate not greedy\n");
M
Mark Adler 已提交
274
        exit(1);
M
Mark Adler 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    }

    /* Feed in already compressed data and switch to no compression: */
    deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
    c_stream.next_in = compr;
    c_stream.avail_in = (uInt)comprLen/2;
    err = deflate(&c_stream, Z_NO_FLUSH);
    CHECK_ERR(err, "deflate");

    /* Switch back to compressing mode: */
    deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
    c_stream.next_in = uncompr;
    c_stream.avail_in = (uInt)uncomprLen;
    err = deflate(&c_stream, Z_NO_FLUSH);
    CHECK_ERR(err, "deflate");

    err = deflate(&c_stream, Z_FINISH);
    if (err != Z_STREAM_END) {
        fprintf(stderr, "deflate should report Z_STREAM_END\n");
M
Mark Adler 已提交
294
        exit(1);
M
Mark Adler 已提交
295 296 297 298 299 300 301 302 303
    }
    err = deflateEnd(&c_stream);
    CHECK_ERR(err, "deflateEnd");
}

/* ===========================================================================
 * Test inflate() with large buffers
 */
void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
304
    Byte *compr, *uncompr;
M
Mark Adler 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318
    uLong comprLen, uncomprLen;
{
    int err;
    z_stream d_stream; /* decompression stream */

    strcpy((char*)uncompr, "garbage");

    d_stream.zalloc = (alloc_func)0;
    d_stream.zfree = (free_func)0;
    d_stream.opaque = (voidpf)0;

    d_stream.next_in  = compr;
    d_stream.avail_in = (uInt)comprLen;

M
Mark Adler 已提交
319 320 321
    err = inflateInit(&d_stream);
    CHECK_ERR(err, "inflateInit");

M
Mark Adler 已提交
322 323
    for (;;) {
        d_stream.next_out = uncompr;            /* discard the output */
M
Mark Adler 已提交
324
        d_stream.avail_out = (uInt)uncomprLen;
M
Mark Adler 已提交
325 326 327 328 329 330 331 332 333 334
        err = inflate(&d_stream, Z_NO_FLUSH);
        if (err == Z_STREAM_END) break;
        CHECK_ERR(err, "large inflate");
    }

    err = inflateEnd(&d_stream);
    CHECK_ERR(err, "inflateEnd");

    if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
        fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
M
Mark Adler 已提交
335
        exit(1);
M
Mark Adler 已提交
336 337 338 339 340
    } else {
        printf("large_inflate(): OK\n");
    }
}

M
Mark Adler 已提交
341 342 343
/* ===========================================================================
 * Test deflate() with full flush
 */
M
Mark Adler 已提交
344
void test_flush(compr, comprLen)
M
Mark Adler 已提交
345
    Byte *compr;
M
Mark Adler 已提交
346
    uLong *comprLen;
M
Mark Adler 已提交
347 348 349
{
    z_stream c_stream; /* compression stream */
    int err;
M
Mark Adler 已提交
350
    uInt len = (uInt)strlen(hello)+1;
M
Mark Adler 已提交
351 352 353

    c_stream.zalloc = (alloc_func)0;
    c_stream.zfree = (free_func)0;
M
Mark Adler 已提交
354
    c_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
355 356 357 358

    err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
    CHECK_ERR(err, "deflateInit");

M
Mark Adler 已提交
359
    c_stream.next_in  = (Bytef*)hello;
M
Mark Adler 已提交
360 361
    c_stream.next_out = compr;
    c_stream.avail_in = 3;
M
Mark Adler 已提交
362
    c_stream.avail_out = (uInt)*comprLen;
M
Mark Adler 已提交
363 364 365 366 367 368 369 370
    err = deflate(&c_stream, Z_FULL_FLUSH);
    CHECK_ERR(err, "deflate");

    compr[3]++; /* force an error in first compressed block */
    c_stream.avail_in = len - 3;

    err = deflate(&c_stream, Z_FINISH);
    if (err != Z_STREAM_END) {
M
Mark Adler 已提交
371
        CHECK_ERR(err, "deflate");
M
Mark Adler 已提交
372 373 374
    }
    err = deflateEnd(&c_stream);
    CHECK_ERR(err, "deflateEnd");
M
Mark Adler 已提交
375 376

    *comprLen = c_stream.total_out;
M
Mark Adler 已提交
377 378 379 380 381
}

/* ===========================================================================
 * Test inflateSync()
 */
M
Mark Adler 已提交
382
void test_sync(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
383
    Byte *compr, *uncompr;
M
Mark Adler 已提交
384
    uLong comprLen, uncomprLen;
M
Mark Adler 已提交
385 386 387 388 389 390 391 392
{
    int err;
    z_stream d_stream; /* decompression stream */

    strcpy((char*)uncompr, "garbage");

    d_stream.zalloc = (alloc_func)0;
    d_stream.zfree = (free_func)0;
M
Mark Adler 已提交
393
    d_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
394

M
Mark Adler 已提交
395 396 397
    d_stream.next_in  = compr;
    d_stream.avail_in = 2; /* just read the zlib header */

M
Mark Adler 已提交
398 399 400 401
    err = inflateInit(&d_stream);
    CHECK_ERR(err, "inflateInit");

    d_stream.next_out = uncompr;
M
Mark Adler 已提交
402
    d_stream.avail_out = (uInt)uncomprLen;
M
Mark Adler 已提交
403 404 405 406

    inflate(&d_stream, Z_NO_FLUSH);
    CHECK_ERR(err, "inflate");

M
Mark Adler 已提交
407
    d_stream.avail_in = (uInt)comprLen-2;   /* read all compressed data */
M
Mark Adler 已提交
408
    err = inflateSync(&d_stream);           /* but skip the damaged part */
M
Mark Adler 已提交
409 410 411 412 413
    CHECK_ERR(err, "inflateSync");

    err = inflate(&d_stream, Z_FINISH);
    if (err != Z_DATA_ERROR) {
        fprintf(stderr, "inflate should report DATA_ERROR\n");
M
Mark Adler 已提交
414
        /* Because of incorrect adler32 */
M
Mark Adler 已提交
415
        exit(1);
M
Mark Adler 已提交
416 417 418 419
    }
    err = inflateEnd(&d_stream);
    CHECK_ERR(err, "inflateEnd");

M
Mark Adler 已提交
420
    printf("after inflateSync(): hel%s\n", (char *)uncompr);
M
Mark Adler 已提交
421 422
}

M
Mark Adler 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
/* ===========================================================================
 * Test deflate() with preset dictionary
 */
void test_dict_deflate(compr, comprLen)
    Byte *compr;
    uLong comprLen;
{
    z_stream c_stream; /* compression stream */
    int err;

    c_stream.zalloc = (alloc_func)0;
    c_stream.zfree = (free_func)0;
    c_stream.opaque = (voidpf)0;

    err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
    CHECK_ERR(err, "deflateInit");

    err = deflateSetDictionary(&c_stream,
M
Mark Adler 已提交
441
                               (const Bytef*)dictionary, sizeof(dictionary));
M
Mark Adler 已提交
442 443 444 445 446 447 448 449 450 451 452 453
    CHECK_ERR(err, "deflateSetDictionary");

    dictId = c_stream.adler;
    c_stream.next_out = compr;
    c_stream.avail_out = (uInt)comprLen;

    c_stream.next_in = (Bytef*)hello;
    c_stream.avail_in = (uInt)strlen(hello)+1;

    err = deflate(&c_stream, Z_FINISH);
    if (err != Z_STREAM_END) {
        fprintf(stderr, "deflate should report Z_STREAM_END\n");
M
Mark Adler 已提交
454
        exit(1);
M
Mark Adler 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
    }
    err = deflateEnd(&c_stream);
    CHECK_ERR(err, "deflateEnd");
}

/* ===========================================================================
 * Test inflate() with a preset dictionary
 */
void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
    Byte *compr, *uncompr;
    uLong comprLen, uncomprLen;
{
    int err;
    z_stream d_stream; /* decompression stream */

    strcpy((char*)uncompr, "garbage");

    d_stream.zalloc = (alloc_func)0;
    d_stream.zfree = (free_func)0;
    d_stream.opaque = (voidpf)0;

    d_stream.next_in  = compr;
    d_stream.avail_in = (uInt)comprLen;

M
Mark Adler 已提交
479 480 481
    err = inflateInit(&d_stream);
    CHECK_ERR(err, "inflateInit");

M
Mark Adler 已提交
482 483 484 485 486 487
    d_stream.next_out = uncompr;
    d_stream.avail_out = (uInt)uncomprLen;

    for (;;) {
        err = inflate(&d_stream, Z_NO_FLUSH);
        if (err == Z_STREAM_END) break;
M
Mark Adler 已提交
488 489 490 491 492 493 494 495
        if (err == Z_NEED_DICT) {
            if (d_stream.adler != dictId) {
                fprintf(stderr, "unexpected dictionary");
                exit(1);
            }
            err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
                                       sizeof(dictionary));
        }
M
Mark Adler 已提交
496 497 498 499 500 501 502 503
        CHECK_ERR(err, "inflate with dict");
    }

    err = inflateEnd(&d_stream);
    CHECK_ERR(err, "inflateEnd");

    if (strcmp((char*)uncompr, hello)) {
        fprintf(stderr, "bad inflate with dict\n");
M
Mark Adler 已提交
504
        exit(1);
M
Mark Adler 已提交
505
    } else {
M
Mark Adler 已提交
506
        printf("inflate with dictionary: %s\n", (char *)uncompr);
M
Mark Adler 已提交
507 508 509
    }
}

M
Mark Adler 已提交
510 511 512 513
/* ===========================================================================
 * Usage:  example [output.gz  [input.gz]]
 */

M
Mark Adler 已提交
514
int main(argc, argv)
M
Mark Adler 已提交
515 516 517
    int argc;
    char *argv[];
{
M
Mark Adler 已提交
518 519
    Byte *compr, *uncompr;
    uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
M
Mark Adler 已提交
520
    uLong uncomprLen = comprLen;
M
Mark Adler 已提交
521
    static const char* myVersion = ZLIB_VERSION;
M
Mark Adler 已提交
522

M
Mark Adler 已提交
523
    if (zlibVersion()[0] != myVersion[0]) {
M
Mark Adler 已提交
524 525
        fprintf(stderr, "incompatible zlib version\n");
        exit(1);
M
Mark Adler 已提交
526

M
Mark Adler 已提交
527
    } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
M
Mark Adler 已提交
528
        fprintf(stderr, "warning: different zlib version\n");
M
Mark Adler 已提交
529
    }
M
Mark Adler 已提交
530

M
Mark Adler 已提交
531
    printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
M
Mark Adler 已提交
532 533
            ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());

M
Mark Adler 已提交
534 535 536 537 538
    compr    = (Byte*)calloc((uInt)comprLen, 1);
    uncompr  = (Byte*)calloc((uInt)uncomprLen, 1);
    /* compr and uncompr are cleared to avoid reading uninitialized
     * data and to ensure that uncompr compresses well.
     */
M
Mark Adler 已提交
539 540
    if (compr == Z_NULL || uncompr == Z_NULL) {
        printf("out of memory\n");
M
Mark Adler 已提交
541
        exit(1);
M
Mark Adler 已提交
542 543
    }
    test_compress(compr, comprLen, uncompr, uncomprLen);
M
Mark Adler 已提交
544

M
Mark Adler 已提交
545
    test_gzio((argc > 1 ? argv[1] : TESTFILE),
M
Mark Adler 已提交
546
              uncompr, uncomprLen);
M
Mark Adler 已提交
547 548 549 550 551 552

    test_deflate(compr, comprLen);
    test_inflate(compr, comprLen, uncompr, uncomprLen);

    test_large_deflate(compr, comprLen, uncompr, uncomprLen);
    test_large_inflate(compr, comprLen, uncompr, uncomprLen);
M
Mark Adler 已提交
553

M
Mark Adler 已提交
554
    test_flush(compr, &comprLen);
M
Mark Adler 已提交
555
    test_sync(compr, comprLen, uncompr, uncomprLen);
M
Mark Adler 已提交
556
    comprLen = uncomprLen;
M
Mark Adler 已提交
557

M
Mark Adler 已提交
558 559 560
    test_dict_deflate(compr, comprLen);
    test_dict_inflate(compr, comprLen, uncompr, uncomprLen);

M
Mark Adler 已提交
561 562 563
    free(compr);
    free(uncompr);

M
Mark Adler 已提交
564
    return 0;
M
Mark Adler 已提交
565
}