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

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

#include <stdio.h>
#include "zlib.h"

M
Mark Adler 已提交
11 12
#ifdef STDC
#  include <string.h>
M
Mark Adler 已提交
13 14 15
#  include <stdlib.h>
#else
   extern void exit  OF((int));
M
Mark Adler 已提交
16 17
#endif

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

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

M
Mark Adler 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43
const char dictionary[] = "hello";
uLong dictId; /* Adler32 value of the dictionary */

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

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

M
Mark Adler 已提交
62
    err = compress(compr, &comprLen, (const Bytef*)hello, len);
M
Mark Adler 已提交
63 64
    CHECK_ERR(err, "compress");

M
Mark Adler 已提交
65
    strcpy((char*)uncompr, "garbage");
M
Mark Adler 已提交
66 67 68 69

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

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

/* ===========================================================================
 * Test read/write of .gz files
 */
M
Mark Adler 已提交
81
void test_gzio(out, in, uncompr, uncomprLen)
M
Mark Adler 已提交
82 83 84
    const char *out; /* output file */
    const char *in;  /* input file */
    Byte *uncompr;
M
Mark Adler 已提交
85
    int  uncomprLen;
M
Mark Adler 已提交
86 87 88 89
{
    int err;
    int len = strlen(hello)+1;
    gzFile file;
M
Mark Adler 已提交
90
    z_off_t pos;
M
Mark Adler 已提交
91 92 93

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

    file = gzopen(in, "rb");
    if (file == NULL) {
M
Mark Adler 已提交
111
        fprintf(stderr, "gzopen error\n");
M
Mark Adler 已提交
112
    }
M
Mark Adler 已提交
113
    strcpy((char*)uncompr, "garbage");
M
Mark Adler 已提交
114

M
Mark Adler 已提交
115
    uncomprLen = gzread(file, uncompr, (unsigned)uncomprLen);
M
Mark Adler 已提交
116
    if (uncomprLen != len) {
M
Mark Adler 已提交
117
        fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
M
Mark Adler 已提交
118
	exit(1);
M
Mark Adler 已提交
119
    }
M
Mark Adler 已提交
120
    if (strcmp((char*)uncompr, hello)) {
M
Mark Adler 已提交
121 122
        fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
	exit(1);
M
Mark Adler 已提交
123
    } else {
M
Mark Adler 已提交
124
        printf("gzread(): %s\n", uncompr);
M
Mark Adler 已提交
125
    }
M
Mark Adler 已提交
126

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

    if (gzgetc(file) != ' ') {
	fprintf(stderr, "gzgetc error\n");
	exit(1);
    }

M
Mark Adler 已提交
139 140 141 142
    gzgets(file, (char*)uncompr, uncomprLen);
    uncomprLen = strlen((char*)uncompr);
    if (uncomprLen != 6) { /* "hello!" */
        fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
M
Mark Adler 已提交
143
	exit(1);
M
Mark Adler 已提交
144 145
    }
    if (strcmp((char*)uncompr, hello+7)) {
M
Mark Adler 已提交
146
        fprintf(stderr, "bad gzgets after gzseek\n");
M
Mark Adler 已提交
147
	exit(1);
M
Mark Adler 已提交
148
    } else {
M
Mark Adler 已提交
149
        printf("gzgets() after gzseek: %s\n", uncompr);
M
Mark Adler 已提交
150 151 152
    }

    gzclose(file);
M
Mark Adler 已提交
153 154 155
}

/* ===========================================================================
M
Mark Adler 已提交
156
 * Test deflate() with small buffers
M
Mark Adler 已提交
157
 */
M
Mark Adler 已提交
158
void test_deflate(compr, comprLen)
M
Mark Adler 已提交
159
    Byte *compr;
M
Mark Adler 已提交
160
    uLong comprLen;
M
Mark Adler 已提交
161 162 163 164 165 166 167
{
    z_stream c_stream; /* compression stream */
    int err;
    int len = strlen(hello)+1;

    c_stream.zalloc = (alloc_func)0;
    c_stream.zfree = (free_func)0;
M
Mark Adler 已提交
168
    c_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
169 170 171 172

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

M
Mark Adler 已提交
173
    c_stream.next_in  = (Bytef*)hello;
M
Mark Adler 已提交
174 175
    c_stream.next_out = compr;

M
Mark Adler 已提交
176
    while (c_stream.total_in != (uLong)len && c_stream.total_out < comprLen) {
M
Mark Adler 已提交
177 178 179
        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 已提交
180 181
    }
    /* Finish the stream, still forcing small buffers: */
M
Mark Adler 已提交
182
    for (;;) {
M
Mark Adler 已提交
183 184 185 186
        c_stream.avail_out = 1;
        err = deflate(&c_stream, Z_FINISH);
        if (err == Z_STREAM_END) break;
        CHECK_ERR(err, "deflate");
M
Mark Adler 已提交
187
    }
M
Mark Adler 已提交
188 189 190 191 192 193 194 195

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

/* ===========================================================================
 * Test inflate() with small buffers
 */
M
Mark Adler 已提交
196
void test_inflate(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
197
    Byte *compr, *uncompr;
M
Mark Adler 已提交
198
    uLong comprLen, uncomprLen;
M
Mark Adler 已提交
199 200 201 202
{
    int err;
    z_stream d_stream; /* decompression stream */

M
Mark Adler 已提交
203
    strcpy((char*)uncompr, "garbage");
M
Mark Adler 已提交
204 205 206

    d_stream.zalloc = (alloc_func)0;
    d_stream.zfree = (free_func)0;
M
Mark Adler 已提交
207
    d_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
208 209

    d_stream.next_in  = compr;
M
Mark Adler 已提交
210
    d_stream.avail_in = 0;
M
Mark Adler 已提交
211 212
    d_stream.next_out = uncompr;

M
Mark Adler 已提交
213 214 215
    err = inflateInit(&d_stream);
    CHECK_ERR(err, "inflateInit");

M
Mark Adler 已提交
216
    while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
M
Mark Adler 已提交
217 218 219 220
        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 已提交
221 222 223 224 225
    }

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

M
Mark Adler 已提交
226
    if (strcmp((char*)uncompr, hello)) {
M
Mark Adler 已提交
227
        fprintf(stderr, "bad inflate\n");
M
Mark Adler 已提交
228
	exit(1);
M
Mark Adler 已提交
229
    } else {
M
Mark Adler 已提交
230
        printf("inflate(): %s\n", uncompr);
M
Mark Adler 已提交
231 232 233
    }
}

M
Mark Adler 已提交
234 235 236 237
/* ===========================================================================
 * Test deflate() with large buffers and dynamic change of compression level
 */
void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
238
    Byte *compr, *uncompr;
M
Mark Adler 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251
    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 已提交
252
    c_stream.avail_out = (uInt)comprLen;
M
Mark Adler 已提交
253 254 255 256 257 258 259 260 261 262

    /* 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 已提交
263
	exit(1);
M
Mark Adler 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    }

    /* 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 已提交
283
	exit(1);
M
Mark Adler 已提交
284 285 286 287 288 289 290 291 292
    }
    err = deflateEnd(&c_stream);
    CHECK_ERR(err, "deflateEnd");
}

/* ===========================================================================
 * Test inflate() with large buffers
 */
void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
293
    Byte *compr, *uncompr;
M
Mark Adler 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307
    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 已提交
308 309 310
    err = inflateInit(&d_stream);
    CHECK_ERR(err, "inflateInit");

M
Mark Adler 已提交
311 312
    for (;;) {
        d_stream.next_out = uncompr;            /* discard the output */
M
Mark Adler 已提交
313
	d_stream.avail_out = (uInt)uncomprLen;
M
Mark Adler 已提交
314 315 316 317 318 319 320 321 322 323
        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 已提交
324
	exit(1);
M
Mark Adler 已提交
325 326 327 328 329
    } else {
        printf("large_inflate(): OK\n");
    }
}

M
Mark Adler 已提交
330 331 332
/* ===========================================================================
 * Test deflate() with full flush
 */
M
Mark Adler 已提交
333
void test_flush(compr, comprLen)
M
Mark Adler 已提交
334
    Byte *compr;
M
Mark Adler 已提交
335
    uLong *comprLen;
M
Mark Adler 已提交
336 337 338 339 340 341 342
{
    z_stream c_stream; /* compression stream */
    int err;
    int len = strlen(hello)+1;

    c_stream.zalloc = (alloc_func)0;
    c_stream.zfree = (free_func)0;
M
Mark Adler 已提交
343
    c_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
344 345 346 347

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

M
Mark Adler 已提交
348
    c_stream.next_in  = (Bytef*)hello;
M
Mark Adler 已提交
349 350
    c_stream.next_out = compr;
    c_stream.avail_in = 3;
M
Mark Adler 已提交
351
    c_stream.avail_out = (uInt)*comprLen;
M
Mark Adler 已提交
352 353 354 355 356 357 358 359
    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 已提交
360
        CHECK_ERR(err, "deflate");
M
Mark Adler 已提交
361 362 363
    }
    err = deflateEnd(&c_stream);
    CHECK_ERR(err, "deflateEnd");
M
Mark Adler 已提交
364 365

    *comprLen = c_stream.total_out;
M
Mark Adler 已提交
366 367 368 369 370
}

/* ===========================================================================
 * Test inflateSync()
 */
M
Mark Adler 已提交
371
void test_sync(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
372
    Byte *compr, *uncompr;
M
Mark Adler 已提交
373
    uLong comprLen, uncomprLen;
M
Mark Adler 已提交
374 375 376 377 378 379 380 381
{
    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 已提交
382
    d_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
383

M
Mark Adler 已提交
384 385 386
    d_stream.next_in  = compr;
    d_stream.avail_in = 2; /* just read the zlib header */

M
Mark Adler 已提交
387 388 389 390
    err = inflateInit(&d_stream);
    CHECK_ERR(err, "inflateInit");

    d_stream.next_out = uncompr;
M
Mark Adler 已提交
391
    d_stream.avail_out = (uInt)uncomprLen;
M
Mark Adler 已提交
392 393 394 395

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

M
Mark Adler 已提交
396
    d_stream.avail_in = (uInt)comprLen-2;   /* read all compressed data */
M
Mark Adler 已提交
397
    err = inflateSync(&d_stream);           /* but skip the damaged part */
M
Mark Adler 已提交
398 399 400 401 402
    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 已提交
403
        /* Because of incorrect adler32 */
M
Mark Adler 已提交
404
	exit(1);
M
Mark Adler 已提交
405 406 407 408
    }
    err = inflateEnd(&d_stream);
    CHECK_ERR(err, "inflateEnd");

M
Mark Adler 已提交
409
    printf("after inflateSync(): hel%s\n", uncompr);
M
Mark Adler 已提交
410 411
}

M
Mark Adler 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
/* ===========================================================================
 * 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,
			       (const Bytef*)dictionary, sizeof(dictionary));
    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 已提交
443
	exit(1);
M
Mark Adler 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    }
    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 已提交
468 469 470
    err = inflateInit(&d_stream);
    CHECK_ERR(err, "inflateInit");

M
Mark Adler 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    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;
	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));
	}
        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 已提交
493
	exit(1);
M
Mark Adler 已提交
494 495 496 497 498
    } else {
        printf("inflate with dictionary: %s\n", uncompr);
    }
}

M
Mark Adler 已提交
499 500 501 502
/* ===========================================================================
 * Usage:  example [output.gz  [input.gz]]
 */

M
Mark Adler 已提交
503
int main(argc, argv)
M
Mark Adler 已提交
504 505 506
    int argc;
    char *argv[];
{
M
Mark Adler 已提交
507 508
    Byte *compr, *uncompr;
    uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
M
Mark Adler 已提交
509
    uLong uncomprLen = comprLen;
M
Mark Adler 已提交
510
    static const char* myVersion = ZLIB_VERSION;
M
Mark Adler 已提交
511

M
Mark Adler 已提交
512
    if (zlibVersion()[0] != myVersion[0]) {
M
Mark Adler 已提交
513 514
        fprintf(stderr, "incompatible zlib version\n");
        exit(1);
M
Mark Adler 已提交
515

M
Mark Adler 已提交
516
    } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
M
Mark Adler 已提交
517
        fprintf(stderr, "warning: different zlib version\n");
M
Mark Adler 已提交
518
    }
M
Mark Adler 已提交
519

M
Mark Adler 已提交
520 521 522 523 524
    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 已提交
525 526 527 528 529
    if (compr == Z_NULL || uncompr == Z_NULL) {
        printf("out of memory\n");
	exit(1);
    }
    test_compress(compr, comprLen, uncompr, uncomprLen);
M
Mark Adler 已提交
530 531

    test_gzio((argc > 1 ? argv[1] : "foo.gz"),
M
Mark Adler 已提交
532 533 534 535 536 537 538 539
              (argc > 2 ? argv[2] : "foo.gz"),
	      uncompr, (int)uncomprLen);

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

M
Mark Adler 已提交
541
    test_flush(compr, &comprLen);
M
Mark Adler 已提交
542
    test_sync(compr, comprLen, uncompr, uncomprLen);
M
Mark Adler 已提交
543
    comprLen = uncomprLen;
M
Mark Adler 已提交
544

M
Mark Adler 已提交
545 546 547
    test_dict_deflate(compr, comprLen);
    test_dict_inflate(compr, comprLen, uncompr, uncomprLen);

M
Mark Adler 已提交
548
    exit(0);
M
Mark Adler 已提交
549
    return 0; /* to avoid warning */
M
Mark Adler 已提交
550
}