21 #include "cvmfs_config.h"
31 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
34 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
36 # define SET_BINARY_MODE(file)
47 int def(FILE *source, FILE *dest,
int level)
52 unsigned char in[
CHUNK];
53 unsigned char out[
CHUNK];
59 ret = deflateInit(&strm, level);
65 strm.avail_in = fread(in, 1,
CHUNK, source);
67 (void)deflateEnd(&strm);
70 flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
76 strm.avail_out =
CHUNK;
78 ret = deflate(&strm, flush);
79 assert(ret != Z_STREAM_ERROR);
80 have =
CHUNK - strm.avail_out;
81 if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
82 (void)deflateEnd(&strm);
85 }
while (strm.avail_out == 0);
86 assert(strm.avail_in == 0);
89 }
while (flush != Z_FINISH);
90 assert(ret == Z_STREAM_END);
93 (void)deflateEnd(&strm);
103 int inf(FILE *source, FILE *dest)
108 unsigned char in[
CHUNK];
109 unsigned char out[
CHUNK];
112 strm.zalloc = Z_NULL;
114 strm.opaque = Z_NULL;
116 strm.next_in = Z_NULL;
117 ret = inflateInit(&strm);
123 strm.avail_in = fread(in, 1,
CHUNK, source);
124 if (ferror(source)) {
125 (void)inflateEnd(&strm);
128 if (strm.avail_in == 0)
134 strm.avail_out =
CHUNK;
136 ret = inflate(&strm, Z_NO_FLUSH);
137 assert(ret != Z_STREAM_ERROR);
143 (void)inflateEnd(&strm);
146 have =
CHUNK - strm.avail_out;
147 if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
148 (void)inflateEnd(&strm);
151 }
while (strm.avail_out == 0);
154 }
while (ret != Z_STREAM_END);
157 (void)inflateEnd(&strm);
158 return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
164 fputs(
"zpipe: ", stderr);
168 fputs(
"error reading stdin\n", stderr);
170 fputs(
"error writing stdout\n", stderr);
173 fputs(
"invalid compression level\n", stderr);
176 fputs(
"invalid or incomplete deflate data\n", stderr);
179 fputs(
"out of memory\n", stderr);
181 case Z_VERSION_ERROR:
182 fputs(
"zlib version mismatch!\n", stderr);
195 if (!options.
Has(
"decompress")) {
196 ret =
def(stdin, stdout, Z_DEFAULT_COMPRESSION);
202 ret =
inf(stdin, stdout);
bool Has(const std::string &key) const
#define SET_BINARY_MODE(file)
assert((mem||(size==0))&&"Out Of Memory")
int def(FILE *source, FILE *dest, int level)
virtual int Main(const Options &options)
int inf(FILE *source, FILE *dest)