29 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
32 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
34 # define SET_BINARY_MODE(file)
45 int def(FILE *source, FILE *dest,
int level)
50 unsigned char in[
CHUNK];
51 unsigned char out[
CHUNK];
57 ret = deflateInit(&strm, level);
63 strm.avail_in = fread(in, 1,
CHUNK, source);
65 (void)deflateEnd(&strm);
68 flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
74 strm.avail_out =
CHUNK;
76 ret = deflate(&strm, flush);
77 assert(ret != Z_STREAM_ERROR);
78 have =
CHUNK - strm.avail_out;
79 if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
80 (void)deflateEnd(&strm);
83 }
while (strm.avail_out == 0);
84 assert(strm.avail_in == 0);
87 }
while (flush != Z_FINISH);
88 assert(ret == Z_STREAM_END);
91 (void)deflateEnd(&strm);
101 int inf(FILE *source, FILE *dest)
106 unsigned char in[
CHUNK];
107 unsigned char out[
CHUNK];
110 strm.zalloc = Z_NULL;
112 strm.opaque = Z_NULL;
114 strm.next_in = Z_NULL;
115 ret = inflateInit(&strm);
121 strm.avail_in = fread(in, 1,
CHUNK, source);
122 if (ferror(source)) {
123 (void)inflateEnd(&strm);
126 if (strm.avail_in == 0)
132 strm.avail_out =
CHUNK;
134 ret = inflate(&strm, Z_NO_FLUSH);
135 assert(ret != Z_STREAM_ERROR);
141 (void)inflateEnd(&strm);
144 have =
CHUNK - strm.avail_out;
145 if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
146 (void)inflateEnd(&strm);
149 }
while (strm.avail_out == 0);
152 }
while (ret != Z_STREAM_END);
155 (void)inflateEnd(&strm);
156 return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
162 fputs(
"zpipe: ", stderr);
166 fputs(
"error reading stdin\n", stderr);
168 fputs(
"error writing stdout\n", stderr);
171 fputs(
"invalid compression level\n", stderr);
174 fputs(
"invalid or incomplete deflate data\n", stderr);
177 fputs(
"out of memory\n", stderr);
179 case Z_VERSION_ERROR:
180 fputs(
"zlib version mismatch!\n", stderr);
193 if (args.find(
'd') == args.end()) {
194 ret =
def(stdin, stdout, Z_DEFAULT_COMPRESSION);
200 ret =
inf(stdin, stdout);
#define SET_BINARY_MODE(file)
virtual int Main(const ArgumentList &args)
assert((mem||(size==0))&&"Out Of Memory")
int def(FILE *source, FILE *dest, int level)
std::map< char, SharedPtr< std::string > > ArgumentList
int inf(FILE *source, FILE *dest)