example.c 14.2 KB
Newer Older
M
Mark Adler 已提交
1
/* example.c -- usage example of the zlib compression library
M
Mark Adler 已提交
2
 * Copyright (C) 1995-1996 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: example.c,v 1.16 1996/05/23 17:11:28 me Exp $ */
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 44 45 46 47 48 49
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));
void test_flush         OF((Byte *compr, uLong comprLen));
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
    } else {
M
Mark Adler 已提交
73
        printf("uncompress(): %s\n", uncompr);
M
Mark Adler 已提交
74 75 76 77 78 79
    }
}

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

    file = gzopen(out, "wb");
    if (file == NULL) {
M
Mark Adler 已提交
92 93
        fprintf(stderr, "gzopen error\n");
        exit(1);
M
Mark Adler 已提交
94 95
    }

M
Mark Adler 已提交
96
    if (gzwrite(file, (const voidp)hello, (unsigned)len) != len) {
M
Mark Adler 已提交
97
        fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err));
M
Mark Adler 已提交
98 99 100 101 102
    }
    gzclose(file);

    file = gzopen(in, "rb");
    if (file == NULL) {
M
Mark Adler 已提交
103
        fprintf(stderr, "gzopen error\n");
M
Mark Adler 已提交
104
    }
M
Mark Adler 已提交
105
    strcpy((char*)uncompr, "garbage");
M
Mark Adler 已提交
106

M
Mark Adler 已提交
107
    uncomprLen = gzread(file, uncompr, (unsigned)uncomprLen);
M
Mark Adler 已提交
108
    if (uncomprLen != len) {
M
Mark Adler 已提交
109
        fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
M
Mark Adler 已提交
110 111 112
    }
    gzclose(file);

M
Mark Adler 已提交
113
    if (strcmp((char*)uncompr, hello)) {
M
Mark Adler 已提交
114
        fprintf(stderr, "bad gzread\n");
M
Mark Adler 已提交
115
    } else {
M
Mark Adler 已提交
116
        printf("gzread(): %s\n", uncompr);
M
Mark Adler 已提交
117 118 119 120
    }
}

/* ===========================================================================
M
Mark Adler 已提交
121
 * Test deflate() with small buffers
M
Mark Adler 已提交
122
 */
M
Mark Adler 已提交
123
void test_deflate(compr, comprLen)
M
Mark Adler 已提交
124
    Byte *compr;
M
Mark Adler 已提交
125
    uLong comprLen;
M
Mark Adler 已提交
126 127 128 129 130 131 132
{
    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 已提交
133
    c_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
134 135 136 137

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

M
Mark Adler 已提交
138
    c_stream.next_in  = (Bytef*)hello;
M
Mark Adler 已提交
139 140
    c_stream.next_out = compr;

M
Mark Adler 已提交
141
    while (c_stream.total_in != (uLong)len && c_stream.total_out < comprLen) {
M
Mark Adler 已提交
142 143 144
        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 已提交
145 146
    }
    /* Finish the stream, still forcing small buffers: */
M
Mark Adler 已提交
147
    for (;;) {
M
Mark Adler 已提交
148 149 150 151
        c_stream.avail_out = 1;
        err = deflate(&c_stream, Z_FINISH);
        if (err == Z_STREAM_END) break;
        CHECK_ERR(err, "deflate");
M
Mark Adler 已提交
152
    }
M
Mark Adler 已提交
153 154 155 156 157 158 159 160

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

/* ===========================================================================
 * Test inflate() with small buffers
 */
M
Mark Adler 已提交
161
void test_inflate(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
162
    Byte *compr, *uncompr;
M
Mark Adler 已提交
163
    uLong comprLen, uncomprLen;
M
Mark Adler 已提交
164 165 166 167
{
    int err;
    z_stream d_stream; /* decompression stream */

M
Mark Adler 已提交
168
    strcpy((char*)uncompr, "garbage");
M
Mark Adler 已提交
169 170 171

    d_stream.zalloc = (alloc_func)0;
    d_stream.zfree = (free_func)0;
M
Mark Adler 已提交
172
    d_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
173 174 175 176 177 178 179

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

    d_stream.next_in  = compr;
    d_stream.next_out = uncompr;

M
Mark Adler 已提交
180
    while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
M
Mark Adler 已提交
181 182 183 184
        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 已提交
185 186 187 188 189
    }

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

M
Mark Adler 已提交
190
    if (strcmp((char*)uncompr, hello)) {
M
Mark Adler 已提交
191
        fprintf(stderr, "bad inflate\n");
M
Mark Adler 已提交
192
    } else {
M
Mark Adler 已提交
193
        printf("inflate(): %s\n", uncompr);
M
Mark Adler 已提交
194 195 196
    }
}

M
Mark Adler 已提交
197 198 199 200
/* ===========================================================================
 * Test deflate() with large buffers and dynamic change of compression level
 */
void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
201
    Byte *compr, *uncompr;
M
Mark Adler 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214
    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 已提交
215
    c_stream.avail_out = (uInt)comprLen;
M
Mark Adler 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

    /* 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");
    }

    /* 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");
    }
    err = deflateEnd(&c_stream);
    CHECK_ERR(err, "deflateEnd");
}

/* ===========================================================================
 * Test inflate() with large buffers
 */
void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
254
    Byte *compr, *uncompr;
M
Mark Adler 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    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;

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

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

    for (;;) {
        d_stream.next_out = uncompr;            /* discard the output */
M
Mark Adler 已提交
274
	d_stream.avail_out = (uInt)uncomprLen;
M
Mark Adler 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
        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);
    } else {
        printf("large_inflate(): OK\n");
    }
}

M
Mark Adler 已提交
290 291 292
/* ===========================================================================
 * Test deflate() with full flush
 */
M
Mark Adler 已提交
293
void test_flush(compr, comprLen)
M
Mark Adler 已提交
294
    Byte *compr;
M
Mark Adler 已提交
295
    uLong comprLen;
M
Mark Adler 已提交
296 297 298 299 300 301 302
{
    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 已提交
303
    c_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
304 305 306 307

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

M
Mark Adler 已提交
308
    c_stream.next_in  = (Bytef*)hello;
M
Mark Adler 已提交
309 310
    c_stream.next_out = compr;
    c_stream.avail_in = 3;
M
Mark Adler 已提交
311
    c_stream.avail_out = (uInt)comprLen;
M
Mark Adler 已提交
312 313 314 315 316 317 318 319
    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 已提交
320
        CHECK_ERR(err, "deflate");
M
Mark Adler 已提交
321 322 323 324 325 326 327 328
    }
    err = deflateEnd(&c_stream);
    CHECK_ERR(err, "deflateEnd");
}

/* ===========================================================================
 * Test inflateSync()
 */
M
Mark Adler 已提交
329
void test_sync(compr, comprLen, uncompr, uncomprLen)
M
Mark Adler 已提交
330
    Byte *compr, *uncompr;
M
Mark Adler 已提交
331
    uLong comprLen, uncomprLen;
M
Mark Adler 已提交
332 333 334 335 336 337 338 339
{
    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 已提交
340
    d_stream.opaque = (voidpf)0;
M
Mark Adler 已提交
341 342 343 344 345 346 347

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

    d_stream.next_in  = compr;
    d_stream.next_out = uncompr;
    d_stream.avail_in = 2; /* just read the zlib header */
M
Mark Adler 已提交
348
    d_stream.avail_out = (uInt)uncomprLen;
M
Mark Adler 已提交
349 350 351 352

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

M
Mark Adler 已提交
353
    d_stream.avail_in = (uInt)comprLen-2;   /* read all compressed data */
M
Mark Adler 已提交
354
    err = inflateSync(&d_stream);           /* but skip the damaged part */
M
Mark Adler 已提交
355 356 357 358 359
    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 已提交
360
        /* Because of incorrect adler32 */
M
Mark Adler 已提交
361 362 363 364
    }
    err = inflateEnd(&d_stream);
    CHECK_ERR(err, "inflateEnd");

M
Mark Adler 已提交
365
    printf("after inflateSync(): hel%s\n", uncompr);
M
Mark Adler 已提交
366 367
}

M
Mark Adler 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 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 443 444 445 446 447 448 449 450 451 452
/* ===========================================================================
 * 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");
    }
    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;

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

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

    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");
    } else {
        printf("inflate with dictionary: %s\n", uncompr);
    }
}

M
Mark Adler 已提交
453 454 455 456
/* ===========================================================================
 * Usage:  example [output.gz  [input.gz]]
 */

M
Mark Adler 已提交
457
int main(argc, argv)
M
Mark Adler 已提交
458 459 460
    int argc;
    char *argv[];
{
M
Mark Adler 已提交
461 462
    Byte *compr, *uncompr;
    uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
M
Mark Adler 已提交
463
    uLong uncomprLen = comprLen;
M
Mark Adler 已提交
464

M
Mark Adler 已提交
465
    if (zlibVersion()[0] != ZLIB_VERSION[0]) {
M
Mark Adler 已提交
466 467
        fprintf(stderr, "incompatible zlib version\n");
        exit(1);
M
Mark Adler 已提交
468

M
Mark Adler 已提交
469
    } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
M
Mark Adler 已提交
470
        fprintf(stderr, "warning: different zlib version\n");
M
Mark Adler 已提交
471
    }
M
Mark Adler 已提交
472

M
Mark Adler 已提交
473 474 475 476 477
    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 已提交
478 479 480 481 482 483
    if (compr == Z_NULL || uncompr == Z_NULL) {
        printf("out of memory\n");
	exit(1);
    }

    test_compress(compr, comprLen, uncompr, uncomprLen);
M
Mark Adler 已提交
484 485

    test_gzio((argc > 1 ? argv[1] : "foo.gz"),
M
Mark Adler 已提交
486 487 488 489 490 491 492 493
              (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 已提交
494

M
Mark Adler 已提交
495 496
    test_flush(compr, comprLen);
    test_sync(compr, comprLen, uncompr, uncomprLen);
M
Mark Adler 已提交
497

M
Mark Adler 已提交
498 499 500
    test_dict_deflate(compr, comprLen);
    test_dict_inflate(compr, comprLen, uncompr, uncomprLen);

M
Mark Adler 已提交
501
    exit(0);
M
Mark Adler 已提交
502
    return 0; /* to avoid warning */
M
Mark Adler 已提交
503
}