xref: /aosp_15_r20/external/zlib/examples/zpipe.c (revision 86ee64e75fa5f8bce2c8c356138035642429cd05)
1 /* zpipe.c: example of proper use of zlib's inflate() and deflate()
2    Not copyrighted -- provided to the public domain
3    Version 1.4  11 December 2005  Mark Adler */
4 
5 /* Version history:
6    1.0  30 Oct 2004  First version
7    1.1   8 Nov 2004  Add void casting for unused return values
8                      Use switch statement for inflate() return values
9    1.2   9 Nov 2004  Add assertions to document zlib guarantees
10    1.3   6 Apr 2005  Remove incorrect assertion in inf()
11    1.4  11 Dec 2005  Add hack to avoid MSDOS end-of-line conversions
12                      Avoid some compiler warnings for input and output buffers
13  */
14 
15 #if defined(_WIN32) && !defined(_CRT_NONSTDC_NO_DEPRECATE)
16 #  define _CRT_NONSTDC_NO_DEPRECATE
17 #endif
18 
19 #include <stdio.h>
20 #include <string.h>
21 #include <assert.h>
22 #include "zlib.h"
23 
24 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
25 #  include <fcntl.h>
26 #  include <io.h>
27 #  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
28 #else
29 #  define SET_BINARY_MODE(file)
30 #endif
31 
32 #define CHUNK 16384
33 
34 /* Compress from file source to file dest until EOF on source.
35    def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
36    allocated for processing, Z_STREAM_ERROR if an invalid compression
37    level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
38    version of the library linked do not match, or Z_ERRNO if there is
39    an error reading or writing the files. */
def(FILE * source,FILE * dest,int level)40 int def(FILE *source, FILE *dest, int level)
41 {
42     int ret, flush;
43     unsigned have;
44     z_stream strm;
45     unsigned char in[CHUNK];
46     unsigned char out[CHUNK];
47 
48     /* allocate deflate state */
49     strm.zalloc = Z_NULL;
50     strm.zfree = Z_NULL;
51     strm.opaque = Z_NULL;
52     ret = deflateInit(&strm, level);
53     if (ret != Z_OK)
54         return ret;
55 
56     /* compress until end of file */
57     do {
58         strm.avail_in = fread(in, 1, CHUNK, source);
59         if (ferror(source)) {
60             (void)deflateEnd(&strm);
61             return Z_ERRNO;
62         }
63         flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
64         strm.next_in = in;
65 
66         /* run deflate() on input until output buffer not full, finish
67            compression if all of source has been read in */
68         do {
69             strm.avail_out = CHUNK;
70             strm.next_out = out;
71             ret = deflate(&strm, flush);    /* no bad return value */
72             assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
73             have = CHUNK - strm.avail_out;
74             if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
75                 (void)deflateEnd(&strm);
76                 return Z_ERRNO;
77             }
78         } while (strm.avail_out == 0);
79         assert(strm.avail_in == 0);     /* all input will be used */
80 
81         /* done when last data in file processed */
82     } while (flush != Z_FINISH);
83     assert(ret == Z_STREAM_END);        /* stream will be complete */
84 
85     /* clean up and return */
86     (void)deflateEnd(&strm);
87     return Z_OK;
88 }
89 
90 /* Decompress from file source to file dest until stream ends or EOF.
91    inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
92    allocated for processing, Z_DATA_ERROR if the deflate data is
93    invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
94    the version of the library linked do not match, or Z_ERRNO if there
95    is an error reading or writing the files. */
inf(FILE * source,FILE * dest)96 int inf(FILE *source, FILE *dest)
97 {
98     int ret;
99     unsigned have;
100     z_stream strm;
101     unsigned char in[CHUNK];
102     unsigned char out[CHUNK];
103 
104     /* allocate inflate state */
105     strm.zalloc = Z_NULL;
106     strm.zfree = Z_NULL;
107     strm.opaque = Z_NULL;
108     strm.avail_in = 0;
109     strm.next_in = Z_NULL;
110     ret = inflateInit(&strm);
111     if (ret != Z_OK)
112         return ret;
113 
114     /* decompress until deflate stream ends or end of file */
115     do {
116         strm.avail_in = fread(in, 1, CHUNK, source);
117         if (ferror(source)) {
118             (void)inflateEnd(&strm);
119             return Z_ERRNO;
120         }
121         if (strm.avail_in == 0)
122             break;
123         strm.next_in = in;
124 
125         /* run inflate() on input until output buffer not full */
126         do {
127             strm.avail_out = CHUNK;
128             strm.next_out = out;
129             ret = inflate(&strm, Z_NO_FLUSH);
130             assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
131             switch (ret) {
132             case Z_NEED_DICT:
133                 ret = Z_DATA_ERROR;     /* and fall through */
134             case Z_DATA_ERROR:
135             case Z_MEM_ERROR:
136                 (void)inflateEnd(&strm);
137                 return ret;
138             }
139             have = CHUNK - strm.avail_out;
140             if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
141                 (void)inflateEnd(&strm);
142                 return Z_ERRNO;
143             }
144         } while (strm.avail_out == 0);
145 
146         /* done when inflate() says it's done */
147     } while (ret != Z_STREAM_END);
148 
149     /* clean up and return */
150     (void)inflateEnd(&strm);
151     return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
152 }
153 
154 /* report a zlib or i/o error */
zerr(int ret)155 void zerr(int ret)
156 {
157     fputs("zpipe: ", stderr);
158     switch (ret) {
159     case Z_ERRNO:
160         if (ferror(stdin))
161             fputs("error reading stdin\n", stderr);
162         if (ferror(stdout))
163             fputs("error writing stdout\n", stderr);
164         break;
165     case Z_STREAM_ERROR:
166         fputs("invalid compression level\n", stderr);
167         break;
168     case Z_DATA_ERROR:
169         fputs("invalid or incomplete deflate data\n", stderr);
170         break;
171     case Z_MEM_ERROR:
172         fputs("out of memory\n", stderr);
173         break;
174     case Z_VERSION_ERROR:
175         fputs("zlib version mismatch!\n", stderr);
176     }
177 }
178 
179 /* compress or decompress from stdin to stdout */
main(int argc,char ** argv)180 int main(int argc, char **argv)
181 {
182     int ret;
183 
184     /* avoid end-of-line conversions */
185     SET_BINARY_MODE(stdin);
186     SET_BINARY_MODE(stdout);
187 
188     /* do compression if no arguments */
189     if (argc == 1) {
190         ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION);
191         if (ret != Z_OK)
192             zerr(ret);
193         return ret;
194     }
195 
196     /* do decompression if -d specified */
197     else if (argc == 2 && strcmp(argv[1], "-d") == 0) {
198         ret = inf(stdin, stdout);
199         if (ret != Z_OK)
200             zerr(ret);
201         return ret;
202     }
203 
204     /* otherwise, report usage */
205     else {
206         fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr);
207         return 1;
208     }
209 }
210