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