example.c 16.5 KB
Newer Older
M
Mark Adler 已提交
1
/* example.c -- usage example of the zlib compression library
M
Mark Adler 已提交
2
 * Copyright (C) 1995-2006, 2011, 2016 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
    } \
}

29
static z_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

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

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

52 53 54 55 56 57 58 59 60 61

#ifdef Z_SOLO

void *myalloc OF((void *, unsigned, unsigned));
void myfree OF((void *, void *));

void *myalloc(q, n, m)
    void *q;
    unsigned n, m;
{
62
    (void)q;
63 64 65 66 67
    return calloc(n, m);
}

void myfree(void *q, void *p)
{
68
    (void)q;
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    free(p);
}

static alloc_func zalloc = myalloc;
static free_func zfree = myfree;

#else /* !Z_SOLO */

static alloc_func zalloc = (alloc_func)0;
static free_func zfree = (free_func)0;

void test_compress      OF((Byte *compr, uLong comprLen,
                            Byte *uncompr, uLong uncomprLen));
void test_gzio          OF((const char *fname,
                            Byte *uncompr, uLong uncomprLen));

M
Mark Adler 已提交
85 86 87
/* ===========================================================================
 * Test compress() and uncompress()
 */
M
Mark Adler 已提交
88
void test_compress(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
89
    Byte *compr, *uncompr;
M
Mark Adler 已提交
90
    uLong comprLen, uncomprLen;
M
Mark Adler 已提交
91 92
{
    int err;
M
Mark Adler 已提交
93
    uLong len = (uLong)strlen(hello)+1;
M
Mark Adler 已提交
94

M
Mark Adler 已提交
95
    err = compress(compr, &comprLen, (const Bytef*)hello, len);
M
Mark Adler 已提交
96 97
    CHECK_ERR(err, "compress");

M
Mark Adler 已提交
98
    strcpy((char*)uncompr, "garbage");
M
Mark Adler 已提交
99 100 101 102

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

M
Mark Adler 已提交
103
    if (strcmp((char*)uncompr, hello)) {
M
Mark Adler 已提交
104
        fprintf(stderr, "bad uncompress\n");
M
Mark Adler 已提交
105
        exit(1);
M
Mark Adler 已提交
106
    } else {
M
Mark Adler 已提交
107
        printf("uncompress(): %s\n", (char *)uncompr);
M
Mark Adler 已提交
108 109 110 111 112 113
    }
}

/* ===========================================================================
 * Test read/write of .gz files
 */
M
Mark Adler 已提交
114 115
void test_gzio(fname, uncompr, uncomprLen)
    const char *fname; /* compressed file name */
M
Mark Adler 已提交
116
    Byte *uncompr;
M
Mark Adler 已提交
117
    uLong uncomprLen;
M
Mark Adler 已提交
118
{
M
Mark Adler 已提交
119 120 121
#ifdef NO_GZCOMPRESS
    fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
#else
M
Mark Adler 已提交
122
    int err;
M
Mark Adler 已提交
123
    int len = (int)strlen(hello)+1;
M
Mark Adler 已提交
124
    gzFile file;
M
Mark Adler 已提交
125
    z_off_t pos;
M
Mark Adler 已提交
126

M
Mark Adler 已提交
127
    file = gzopen(fname, "wb");
M
Mark Adler 已提交
128
    if (file == NULL) {
M
Mark Adler 已提交
129 130
        fprintf(stderr, "gzopen error\n");
        exit(1);
M
Mark Adler 已提交
131
    }
M
Mark Adler 已提交
132
    gzputc(file, 'h');
M
Mark Adler 已提交
133 134
    if (gzputs(file, "ello") != 4) {
        fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
M
Mark Adler 已提交
135
        exit(1);
M
Mark Adler 已提交
136 137
    }
    if (gzprintf(file, ", %s!", "hello") != 8) {
M
Mark Adler 已提交
138
        fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
M
Mark Adler 已提交
139
        exit(1);
M
Mark Adler 已提交
140
    }
M
Mark Adler 已提交
141
    gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
M
Mark Adler 已提交
142 143
    gzclose(file);

M
Mark Adler 已提交
144
    file = gzopen(fname, "rb");
M
Mark Adler 已提交
145
    if (file == NULL) {
M
Mark Adler 已提交
146
        fprintf(stderr, "gzopen error\n");
M
Mark Adler 已提交
147
        exit(1);
M
Mark Adler 已提交
148
    }
M
Mark Adler 已提交
149
    strcpy((char*)uncompr, "garbage");
M
Mark Adler 已提交
150

M
Mark Adler 已提交
151
    if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
M
Mark Adler 已提交
152
        fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
M
Mark Adler 已提交
153
        exit(1);
M
Mark Adler 已提交
154
    }
M
Mark Adler 已提交
155
    if (strcmp((char*)uncompr, hello)) {
M
Mark Adler 已提交
156
        fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
M
Mark Adler 已提交
157
        exit(1);
M
Mark Adler 已提交
158
    } else {
M
Mark Adler 已提交
159
        printf("gzread(): %s\n", (char*)uncompr);
M
Mark Adler 已提交
160
    }
M
Mark Adler 已提交
161

M
Mark Adler 已提交
162 163
    pos = gzseek(file, -8L, SEEK_CUR);
    if (pos != 6 || gztell(file) != pos) {
M
Mark Adler 已提交
164 165 166
        fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
                (long)pos, (long)gztell(file));
        exit(1);
M
Mark Adler 已提交
167
    }
M
Mark Adler 已提交
168 169

    if (gzgetc(file) != ' ') {
M
Mark Adler 已提交
170 171
        fprintf(stderr, "gzgetc error\n");
        exit(1);
M
Mark Adler 已提交
172 173
    }

M
Mark Adler 已提交
174 175 176 177 178
    if (gzungetc(' ', file) != ' ') {
        fprintf(stderr, "gzungetc error\n");
        exit(1);
    }

M
Mark Adler 已提交
179 180
    gzgets(file, (char*)uncompr, (int)uncomprLen);
    if (strlen((char*)uncompr) != 7) { /* " hello!" */
M
Mark Adler 已提交
181
        fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
M
Mark Adler 已提交
182
        exit(1);
M
Mark Adler 已提交
183
    }
M
Mark Adler 已提交
184
    if (strcmp((char*)uncompr, hello + 6)) {
M
Mark Adler 已提交
185
        fprintf(stderr, "bad gzgets after gzseek\n");
M
Mark Adler 已提交
186
        exit(1);
M
Mark Adler 已提交
187
    } else {
M
Mark Adler 已提交
188
        printf("gzgets() after gzseek: %s\n", (char*)uncompr);
M
Mark Adler 已提交
189 190 191
    }

    gzclose(file);
M
Mark Adler 已提交
192
#endif
M
Mark Adler 已提交
193 194
}

195 196
#endif /* Z_SOLO */

M
Mark Adler 已提交
197
/* ===========================================================================
M
Mark Adler 已提交
198
 * Test deflate() with small buffers
M
Mark Adler 已提交
199
 */
M
Mark Adler 已提交
200
void test_deflate(compr, comprLen)
M
Mark Adler 已提交
201
    Byte *compr;
M
Mark Adler 已提交
202
    uLong comprLen;
M
Mark Adler 已提交
203 204 205
{
    z_stream c_stream; /* compression stream */
    int err;
M
Mark Adler 已提交
206
    uLong len = (uLong)strlen(hello)+1;
M
Mark Adler 已提交
207

208 209
    c_stream.zalloc = zalloc;
    c_stream.zfree = zfree;
M
Mark Adler 已提交
210
    c_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
211 212 213 214

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

215
    c_stream.next_in  = (z_const unsigned char *)hello;
M
Mark Adler 已提交
216 217
    c_stream.next_out = compr;

M
Mark Adler 已提交
218
    while (c_stream.total_in != len && c_stream.total_out < comprLen) {
M
Mark Adler 已提交
219 220 221
        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 已提交
222 223
    }
    /* Finish the stream, still forcing small buffers: */
M
Mark Adler 已提交
224
    for (;;) {
M
Mark Adler 已提交
225 226 227 228
        c_stream.avail_out = 1;
        err = deflate(&c_stream, Z_FINISH);
        if (err == Z_STREAM_END) break;
        CHECK_ERR(err, "deflate");
M
Mark Adler 已提交
229
    }
M
Mark Adler 已提交
230 231 232 233 234 235 236 237

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

/* ===========================================================================
 * Test inflate() with small buffers
 */
M
Mark Adler 已提交
238
void test_inflate(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
239
    Byte *compr, *uncompr;
M
Mark Adler 已提交
240
    uLong comprLen, uncomprLen;
M
Mark Adler 已提交
241 242 243 244
{
    int err;
    z_stream d_stream; /* decompression stream */

M
Mark Adler 已提交
245
    strcpy((char*)uncompr, "garbage");
M
Mark Adler 已提交
246

247 248
    d_stream.zalloc = zalloc;
    d_stream.zfree = zfree;
M
Mark Adler 已提交
249
    d_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
250 251

    d_stream.next_in  = compr;
M
Mark Adler 已提交
252
    d_stream.avail_in = 0;
M
Mark Adler 已提交
253 254
    d_stream.next_out = uncompr;

M
Mark Adler 已提交
255 256 257
    err = inflateInit(&d_stream);
    CHECK_ERR(err, "inflateInit");

M
Mark Adler 已提交
258
    while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
M
Mark Adler 已提交
259 260 261 262
        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 已提交
263 264 265 266 267
    }

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

M
Mark Adler 已提交
268
    if (strcmp((char*)uncompr, hello)) {
M
Mark Adler 已提交
269
        fprintf(stderr, "bad inflate\n");
M
Mark Adler 已提交
270
        exit(1);
M
Mark Adler 已提交
271
    } else {
M
Mark Adler 已提交
272
        printf("inflate(): %s\n", (char *)uncompr);
M
Mark Adler 已提交
273 274 275
    }
}

M
Mark Adler 已提交
276 277 278 279
/* ===========================================================================
 * Test deflate() with large buffers and dynamic change of compression level
 */
void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
280
    Byte *compr, *uncompr;
M
Mark Adler 已提交
281 282 283 284 285
    uLong comprLen, uncomprLen;
{
    z_stream c_stream; /* compression stream */
    int err;

286 287
    c_stream.zalloc = zalloc;
    c_stream.zfree = zfree;
M
Mark Adler 已提交
288 289 290 291 292 293
    c_stream.opaque = (voidpf)0;

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

    c_stream.next_out = compr;
M
Mark Adler 已提交
294
    c_stream.avail_out = (uInt)comprLen;
M
Mark Adler 已提交
295 296 297 298 299 300 301 302 303 304

    /* 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 已提交
305
        exit(1);
M
Mark Adler 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    }

    /* 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 已提交
325
        exit(1);
M
Mark Adler 已提交
326 327 328 329 330 331 332 333 334
    }
    err = deflateEnd(&c_stream);
    CHECK_ERR(err, "deflateEnd");
}

/* ===========================================================================
 * Test inflate() with large buffers
 */
void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
335
    Byte *compr, *uncompr;
M
Mark Adler 已提交
336 337 338 339 340 341 342
    uLong comprLen, uncomprLen;
{
    int err;
    z_stream d_stream; /* decompression stream */

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

343 344
    d_stream.zalloc = zalloc;
    d_stream.zfree = zfree;
M
Mark Adler 已提交
345 346 347 348 349
    d_stream.opaque = (voidpf)0;

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

M
Mark Adler 已提交
350 351 352
    err = inflateInit(&d_stream);
    CHECK_ERR(err, "inflateInit");

M
Mark Adler 已提交
353 354
    for (;;) {
        d_stream.next_out = uncompr;            /* discard the output */
M
Mark Adler 已提交
355
        d_stream.avail_out = (uInt)uncomprLen;
M
Mark Adler 已提交
356 357 358 359 360 361 362 363 364 365
        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 已提交
366
        exit(1);
M
Mark Adler 已提交
367 368 369 370 371
    } else {
        printf("large_inflate(): OK\n");
    }
}

M
Mark Adler 已提交
372 373 374
/* ===========================================================================
 * Test deflate() with full flush
 */
M
Mark Adler 已提交
375
void test_flush(compr, comprLen)
M
Mark Adler 已提交
376
    Byte *compr;
M
Mark Adler 已提交
377
    uLong *comprLen;
M
Mark Adler 已提交
378 379 380
{
    z_stream c_stream; /* compression stream */
    int err;
M
Mark Adler 已提交
381
    uInt len = (uInt)strlen(hello)+1;
M
Mark Adler 已提交
382

383 384
    c_stream.zalloc = zalloc;
    c_stream.zfree = zfree;
M
Mark Adler 已提交
385
    c_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
386 387 388 389

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

390
    c_stream.next_in  = (z_const unsigned char *)hello;
M
Mark Adler 已提交
391 392
    c_stream.next_out = compr;
    c_stream.avail_in = 3;
M
Mark Adler 已提交
393
    c_stream.avail_out = (uInt)*comprLen;
M
Mark Adler 已提交
394 395 396 397 398 399 400 401
    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 已提交
402
        CHECK_ERR(err, "deflate");
M
Mark Adler 已提交
403 404 405
    }
    err = deflateEnd(&c_stream);
    CHECK_ERR(err, "deflateEnd");
M
Mark Adler 已提交
406 407

    *comprLen = c_stream.total_out;
M
Mark Adler 已提交
408 409 410 411 412
}

/* ===========================================================================
 * Test inflateSync()
 */
M
Mark Adler 已提交
413
void test_sync(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
414
    Byte *compr, *uncompr;
M
Mark Adler 已提交
415
    uLong comprLen, uncomprLen;
M
Mark Adler 已提交
416 417 418 419 420 421
{
    int err;
    z_stream d_stream; /* decompression stream */

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

422 423
    d_stream.zalloc = zalloc;
    d_stream.zfree = zfree;
M
Mark Adler 已提交
424
    d_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
425

M
Mark Adler 已提交
426 427 428
    d_stream.next_in  = compr;
    d_stream.avail_in = 2; /* just read the zlib header */

M
Mark Adler 已提交
429 430 431 432
    err = inflateInit(&d_stream);
    CHECK_ERR(err, "inflateInit");

    d_stream.next_out = uncompr;
M
Mark Adler 已提交
433
    d_stream.avail_out = (uInt)uncomprLen;
M
Mark Adler 已提交
434

435
    err = inflate(&d_stream, Z_NO_FLUSH);
M
Mark Adler 已提交
436 437
    CHECK_ERR(err, "inflate");

M
Mark Adler 已提交
438
    d_stream.avail_in = (uInt)comprLen-2;   /* read all compressed data */
M
Mark Adler 已提交
439
    err = inflateSync(&d_stream);           /* but skip the damaged part */
M
Mark Adler 已提交
440 441 442 443 444
    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 已提交
445
        /* Because of incorrect adler32 */
M
Mark Adler 已提交
446
        exit(1);
M
Mark Adler 已提交
447 448 449 450
    }
    err = inflateEnd(&d_stream);
    CHECK_ERR(err, "inflateEnd");

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

M
Mark Adler 已提交
454 455 456 457 458 459 460 461 462 463
/* ===========================================================================
 * Test deflate() with preset dictionary
 */
void test_dict_deflate(compr, comprLen)
    Byte *compr;
    uLong comprLen;
{
    z_stream c_stream; /* compression stream */
    int err;

464 465
    c_stream.zalloc = zalloc;
    c_stream.zfree = zfree;
M
Mark Adler 已提交
466 467 468 469 470 471
    c_stream.opaque = (voidpf)0;

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

    err = deflateSetDictionary(&c_stream,
M
Mark Adler 已提交
472
                (const Bytef*)dictionary, (int)sizeof(dictionary));
M
Mark Adler 已提交
473 474 475 476 477 478
    CHECK_ERR(err, "deflateSetDictionary");

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

479
    c_stream.next_in = (z_const unsigned char *)hello;
M
Mark Adler 已提交
480 481 482 483 484
    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 已提交
485
        exit(1);
M
Mark Adler 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
    }
    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");

503 504
    d_stream.zalloc = zalloc;
    d_stream.zfree = zfree;
M
Mark Adler 已提交
505 506 507 508 509
    d_stream.opaque = (voidpf)0;

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

M
Mark Adler 已提交
510 511 512
    err = inflateInit(&d_stream);
    CHECK_ERR(err, "inflateInit");

M
Mark Adler 已提交
513 514 515 516 517 518
    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 已提交
519 520 521 522 523 524
        if (err == Z_NEED_DICT) {
            if (d_stream.adler != dictId) {
                fprintf(stderr, "unexpected dictionary");
                exit(1);
            }
            err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
M
Mark Adler 已提交
525
                                       (int)sizeof(dictionary));
M
Mark Adler 已提交
526
        }
M
Mark Adler 已提交
527 528 529 530 531 532 533 534
        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 已提交
535
        exit(1);
M
Mark Adler 已提交
536
    } else {
M
Mark Adler 已提交
537
        printf("inflate with dictionary: %s\n", (char *)uncompr);
M
Mark Adler 已提交
538 539 540
    }
}

M
Mark Adler 已提交
541 542 543 544
/* ===========================================================================
 * Usage:  example [output.gz  [input.gz]]
 */

M
Mark Adler 已提交
545
int main(argc, argv)
M
Mark Adler 已提交
546 547 548
    int argc;
    char *argv[];
{
M
Mark Adler 已提交
549 550
    Byte *compr, *uncompr;
    uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
M
Mark Adler 已提交
551
    uLong uncomprLen = comprLen;
M
Mark Adler 已提交
552
    static const char* myVersion = ZLIB_VERSION;
M
Mark Adler 已提交
553

M
Mark Adler 已提交
554
    if (zlibVersion()[0] != myVersion[0]) {
M
Mark Adler 已提交
555 556
        fprintf(stderr, "incompatible zlib version\n");
        exit(1);
M
Mark Adler 已提交
557

M
Mark Adler 已提交
558
    } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
M
Mark Adler 已提交
559
        fprintf(stderr, "warning: different zlib version\n");
M
Mark Adler 已提交
560
    }
M
Mark Adler 已提交
561

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

M
Mark Adler 已提交
565 566 567 568 569
    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 已提交
570 571
    if (compr == Z_NULL || uncompr == Z_NULL) {
        printf("out of memory\n");
M
Mark Adler 已提交
572
        exit(1);
M
Mark Adler 已提交
573
    }
574 575

#ifdef Z_SOLO
576 577
    (void)argc;
    (void)argv;
578
#else
M
Mark Adler 已提交
579
    test_compress(compr, comprLen, uncompr, uncomprLen);
M
Mark Adler 已提交
580

M
Mark Adler 已提交
581
    test_gzio((argc > 1 ? argv[1] : TESTFILE),
M
Mark Adler 已提交
582
              uncompr, uncomprLen);
583
#endif
M
Mark Adler 已提交
584 585 586 587 588 589

    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 已提交
590

M
Mark Adler 已提交
591
    test_flush(compr, &comprLen);
M
Mark Adler 已提交
592
    test_sync(compr, comprLen, uncompr, uncomprLen);
M
Mark Adler 已提交
593
    comprLen = uncomprLen;
M
Mark Adler 已提交
594

M
Mark Adler 已提交
595 596 597
    test_dict_deflate(compr, comprLen);
    test_dict_inflate(compr, comprLen, uncompr, uncomprLen);

M
Mark Adler 已提交
598 599 600
    free(compr);
    free(uncompr);

M
Mark Adler 已提交
601
    return 0;
M
Mark Adler 已提交
602
}