69 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) 72 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) 74 # define SET_BINARY_MODE(file) 98 unsigned char in[
CHUNK];
99 unsigned char out[
CHUNK];
102 strm.zalloc = Z_NULL;
104 strm.opaque = Z_NULL;
105 ret = deflateInit(&strm, level);
112 if (ferror(source)) {
113 (void)deflateEnd(&strm);
116 flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
122 strm.avail_out =
CHUNK;
124 ret = deflate(&strm, flush);
125 assert(ret != Z_STREAM_ERROR);
126 have =
CHUNK - strm.avail_out;
127 if (
fwrite(out, 1, have, dest) != have || ferror(dest)) {
128 (void)deflateEnd(&strm);
131 }
while (strm.avail_out == 0);
132 assert(strm.avail_in == 0);
135 }
while (flush != Z_FINISH);
136 assert(ret == Z_STREAM_END);
139 (void)deflateEnd(&strm);
158 unsigned char in[
CHUNK];
159 unsigned char out[
CHUNK];
162 strm.zalloc = Z_NULL;
164 strm.opaque = Z_NULL;
166 strm.next_in = Z_NULL;
167 ret = inflateInit(&strm);
174 if (ferror(source)) {
175 (void)inflateEnd(&strm);
178 if (strm.avail_in == 0)
184 strm.avail_out =
CHUNK;
186 ret = inflate(&strm, Z_NO_FLUSH);
187 assert(ret != Z_STREAM_ERROR);
193 (void)inflateEnd(&strm);
196 have =
CHUNK - strm.avail_out;
197 if (
fwrite(out, 1, have, dest) != have || ferror(dest)) {
198 (void)inflateEnd(&strm);
201 }
while (strm.avail_out == 0);
204 }
while (ret != Z_STREAM_END);
207 (void)inflateEnd(&strm);
211 #ifdef __UNIT_TEST_COMPRESS__ 215 int main(
int argc,
char *argv[])
218 printf(
"syntax: %s input_compressed_file output_file\n", argv[0]);
221 FILE *fp_in = fopen(argv[1],
"r");
222 FILE *fp_out = fopen(argv[2],
"wx");
224 if (fp_in ==
NULL || fp_out ==
NULL) {
225 perror(
"fail to open file:");
230 printf(
"cannot compress %s into %s\n", argv[1], argv[2]);
231 perror(
"compress fail:");
237 fp_out = fopen(argv[2],
"r");
238 FILE *fp_def = tmpfile();
241 printf(
"cannot decompress %s\n", argv[2]);
242 perror(
"compress fail:");
247 fseek(fp_def, 0, SEEK_SET);
248 fgets(buffer, 10, fp_def);
250 printf(
"file: '%s'\n", buffer);
enum compress_e compress_inflate(FILE *source, FILE *dest)
enum compress_e compress_deflate(FILE *source, FILE *dest, int level)
size_t MONITOR_EXT_WRAP_NAME() fread(void *ptr, size_t size, size_t count, FILE *stream)
int main(int argc, char *argv[])
size_t MONITOR_EXT_WRAP_NAME() fwrite(const void *ptr, size_t size, size_t count, FILE *stream)