Debian Codegraph @

compat/zlib/examples/zpipe.c

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