aboutsummaryrefslogtreecommitdiffstats
path: root/inflate.c
blob: 9b58101b477f172955793f6f3d4a3cc34430d343 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* $Id: inflate.c,v 1.4 2002/01/31 09:43:15 ukai Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <zlib.h>

#undef BUFSIZE
#define BUFSIZE 4096

int
main(int argc, char **argv)
{
    z_stream s;
    FILE *f;
    char inbuf[BUFSIZE], outbuf[BUFSIZE];
    int status, flush;

    if (argc > 1) {
	f = fopen(argv[1], "rb");
	if (!f) {
	    fprintf(stderr, "%s: cannot open %s\n", argv[0], argv[1]);
	    exit(1);
	}
    }
    else
	f = stdin;

    s.zalloc = Z_NULL;
    s.zfree = Z_NULL;
    s.opaque = Z_NULL;
    status = inflateInit(&s);
    if (status != Z_OK) {
	fprintf(stderr, "%s: inflateInit() %s\n", argv[0], zError(status));
	exit(1);
    }
    s.avail_in = 0;
    s.next_out = (Bytef *) outbuf;
    s.avail_out = sizeof(outbuf);
    flush = Z_NO_FLUSH;
    while (1) {
	if (s.avail_in == 0) {
	    s.next_in = (Bytef *) inbuf;
	    s.avail_in = fread(inbuf, 1, sizeof(inbuf), f);
	}
	status = inflate(&s, flush);
	if (status == Z_STREAM_END) {
	    if (sizeof(outbuf) - s.avail_out)
		fwrite(outbuf, 1, sizeof(outbuf) - s.avail_out, stdout);
	    break;
	}
	if (status != Z_OK) {
	    fprintf(stderr, "%s: inflate() %s\n", argv[0], zError(status));
	    exit(1);
	}
	if (s.avail_out == 0) {
	    fwrite(outbuf, 1, sizeof(outbuf), stdout);
	    s.next_out = (Bytef *) outbuf;
	    s.avail_out = sizeof(outbuf);
	}
    }
    inflateEnd(&s);
    fclose(f);
    return 0;
}