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

M
Mark Adler 已提交
6
/* $Id: example.c,v 1.8 1995/05/02 15:52:32 jloup Exp $ */
M
Mark Adler 已提交
7 8 9 10

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

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

M
Mark Adler 已提交
15
#ifndef __GO32__
M
Mark Adler 已提交
16
extern void exit  __P((int));
M
Mark Adler 已提交
17
#endif
M
Mark Adler 已提交
18

M
Mark Adler 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#define BUFLEN 4096

#define local static
/* For MSDOS and other systems with limitation on stack size. For Unix,
    #define local
   works also.
 */

#define CHECK_ERR(err, msg) { \
    if (err != Z_OK) { \
        fprintf(stderr, "%s error: %d\n", msg, err); \
	exit(1); \
    } \
}

char *hello = "hello world";

M
Mark Adler 已提交
36 37 38 39 40 41
void test_compress __P((void));
void test_gzio     __P((char *out, char *in));
void test_deflate  __P((Byte compr[]));
void test_inflate  __P((Byte compr[]));
void main          __P((int argc, char *argv[]));

M
Mark Adler 已提交
42 43 44 45 46 47 48 49 50 51 52 53
/* ===========================================================================
 * Test compress() and uncompress()
 */
void test_compress()
{
    local Byte compr[BUFLEN];
    uLong comprLen = sizeof(compr);
    local Byte uncompr[BUFLEN];
    uLong uncomprLen = sizeof(uncompr);
    int err;
    uLong len = strlen(hello)+1;

M
Mark Adler 已提交
54
    err = compress(compr, &comprLen, (Byte*)hello, len);
M
Mark Adler 已提交
55 56
    CHECK_ERR(err, "compress");

M
Mark Adler 已提交
57
    strcpy((char*)uncompr, "garbage");
M
Mark Adler 已提交
58 59 60 61

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

M
Mark Adler 已提交
62
    if (strcmp((char*)uncompr, hello)) {
M
Mark Adler 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76
	fprintf(stderr, "bad uncompress\n");
    } else {
	printf("uncompress(): %s\n", uncompr);
    }
}

/* ===========================================================================
 * Test read/write of .gz files
 */
void test_gzio(out, in)
    char *out; /* output file */
    char *in;  /* input file */
{
    local Byte uncompr[BUFLEN];
M
Mark Adler 已提交
77
    int uncomprLen = sizeof(uncompr);
M
Mark Adler 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    int err;
    int len = strlen(hello)+1;
    gzFile file;

    file = gzopen(out, "wb");
    if (file == NULL) {
	fprintf(stderr, "gzopen error\n");
	exit(1);
    }

    if (gzwrite(file, hello, len) != len) {
	fprintf(stderr, "gzwrite err: %s\n", gzerror(file, &err));
    }
    gzclose(file);

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

    uncomprLen = gzread(file, uncompr, uncomprLen);
    if (uncomprLen != len) {
	fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
    }
    gzclose(file);

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

/* ===========================================================================
M
Mark Adler 已提交
113
 * Test deflate() with small buffers
M
Mark Adler 已提交
114
 */
M
Mark Adler 已提交
115
void test_deflate(compr)
M
Mark Adler 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    Byte compr[];
{
    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;

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

    c_stream.next_in  = (Byte*)hello;
    c_stream.next_out = compr;

M
Mark Adler 已提交
131
    while (c_stream.total_in != (uLong)len) {
M
Mark Adler 已提交
132 133 134 135 136
	c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
	err = deflate(&c_stream, Z_NO_FLUSH);
	CHECK_ERR(err, "deflate");
    }
    /* Finish the stream, still forcing small buffers: */
M
Mark Adler 已提交
137
    for (;;) {
M
Mark Adler 已提交
138 139
	c_stream.avail_out = 1;
	err = deflate(&c_stream, Z_FINISH);
M
Mark Adler 已提交
140
	if (err == Z_STREAM_END) break;
M
Mark Adler 已提交
141
	CHECK_ERR(err, "deflate");
M
Mark Adler 已提交
142
    }
M
Mark Adler 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

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

/* ===========================================================================
 * Test inflate() with small buffers
 */
void test_inflate(compr)
    Byte compr[];
{
    local Byte uncompr[BUFLEN];
    int err;
    z_stream d_stream; /* decompression stream */

M
Mark Adler 已提交
158
    strcpy((char*)uncompr, "garbage");
M
Mark Adler 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

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

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

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

    for (;;) {
	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");
    }

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

M
Mark Adler 已提交
179
    if (strcmp((char*)uncompr, hello)) {
M
Mark Adler 已提交
180 181 182 183 184 185
	fprintf(stderr, "bad inflate\n");
    } else {
	printf("inflate(): %s\n", uncompr);
    }
}

M
Mark Adler 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 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 254 255 256 257
/* ===========================================================================
 * Test deflate() with full flush
 */
void test_flush(compr)
    Byte compr[];
{
    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;

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

    c_stream.next_in  = (Byte*)hello;
    c_stream.next_out = compr;
    c_stream.avail_in = 3;
    c_stream.avail_out = BUFLEN;
    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) {
	CHECK_ERR(err, "deflate");
    }
    err = deflateEnd(&c_stream);
    CHECK_ERR(err, "deflateEnd");
}

/* ===========================================================================
 * Test inflateSync()
 */
void test_sync(compr)
    Byte compr[];
{
    local Byte uncompr[BUFLEN];
    int err;
    z_stream d_stream; /* decompression stream */

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

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

    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 */
    d_stream.avail_out = sizeof(uncompr);

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

    d_stream.avail_in = BUFLEN-2; /* let inflate read all compressed data */
    err = inflateSync(&d_stream); /* skip the damaged part */
    CHECK_ERR(err, "inflateSync");

    err = inflate(&d_stream, Z_FINISH);
    if (err != Z_DATA_ERROR) {
        fprintf(stderr, "inflate should report DATA_ERROR\n");
	/* Because of incorrect adler32 */
    }
    err = inflateEnd(&d_stream);
    CHECK_ERR(err, "inflateEnd");

M
Mark Adler 已提交
258
    printf("after inflateSync(): hel%s\n", uncompr);
M
Mark Adler 已提交
259 260
}

M
Mark Adler 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
/* ===========================================================================
 * Usage:  example [output.gz  [input.gz]]
 */

void main(argc, argv)
    int argc;
    char *argv[];
{
    local Byte compr[BUFLEN];

    if (zlib_version[0] != ZLIB_VERSION[0]) {
	fprintf(stderr, "incompatible zlib version\n");
	exit(1);

    } else if (strcmp(zlib_version, ZLIB_VERSION) != 0) {
	fprintf(stderr, "warning: different zlib version\n");
    }
    test_compress();

    test_gzio((argc > 1 ? argv[1] : "foo.gz"),
	      (argc > 2 ? argv[2] : "foo.gz"));

M
Mark Adler 已提交
283
    test_deflate(compr);
M
Mark Adler 已提交
284 285
    test_inflate(compr);

M
Mark Adler 已提交
286 287 288
    test_flush(compr);
    test_sync(compr);

M
Mark Adler 已提交
289 290
    exit(0);
}