diff options
author | Tatsuya Kinoshita <tats@vega.ocn.ne.jp> | 2011-05-04 07:05:14 +0000 |
---|---|---|
committer | Tatsuya Kinoshita <tats@vega.ocn.ne.jp> | 2011-05-04 07:05:14 +0000 |
commit | 72f72d64a422d6628c4796f5c0bf2e508f134214 (patch) | |
tree | 0c9ea90cc53310832c977265521fb44db24a515e /inflate.c | |
parent | Adding upstream version 0.3 (diff) | |
download | w3m-upstream/0.5.1.tar.gz w3m-upstream/0.5.1.zip |
Adding upstream version 0.5.1upstream/0.5.1
Diffstat (limited to 'inflate.c')
-rw-r--r-- | inflate.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/inflate.c b/inflate.c new file mode 100644 index 0000000..c1847fa --- /dev/null +++ b/inflate.c @@ -0,0 +1,83 @@ +/* $Id: inflate.c,v 1.7 2002/01/31 18:28:24 ukai Exp $ */ +#include <stdio.h> +#include <stdlib.h> +#include <zlib.h> + +#undef BUFSIZE +#define BUFSIZE 4096 + +static char dummy_head[1 + 1] = { + 0x8 + 0x7 * 0x10, + (((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF, +}; + +int +main(int argc, char **argv) +{ + z_stream s; + FILE *f; + char inbuf[BUFSIZE], outbuf[BUFSIZE]; + int status, flush, retry = 0, len = 0; + + 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; + len = s.avail_in = fread(inbuf, 1, sizeof(inbuf), f); + } + status = inflate(&s, flush); + if (status == Z_STREAM_END || status == Z_BUF_ERROR) { + if (sizeof(outbuf) - s.avail_out) + fwrite(outbuf, 1, sizeof(outbuf) - s.avail_out, stdout); + break; + } + if (status == Z_DATA_ERROR && !retry++) { + status = inflateReset(&s); + if (status != Z_OK) { + fprintf(stderr, "%s: inflateReset() %s\n", argv[0], + zError(status)); + exit(1); + } + s.next_in = (Bytef *) dummy_head; + s.avail_in = sizeof(dummy_head); + status = inflate(&s, flush); + s.next_in = (Bytef *) inbuf; + s.avail_in = len; + continue; + } + 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); + } + retry = 1; + } + inflateEnd(&s); + fclose(f); + return 0; +} |