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