* c-lex.c (narrowest_unsigned_type): Change itk to int.
[official-gcc.git] / zlib / examples / zpipe.c
blob26abb56a9ca63e3090759cc9b28169ef060e6c4f
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 */
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()
13 #include <stdio.h>
14 #include <string.h>
15 #include <assert.h>
16 #include "zlib.h"
18 #define CHUNK 16384
20 /* Compress from file source to file dest until EOF on source.
21 def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
22 allocated for processing, Z_STREAM_ERROR if an invalid compression
23 level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
24 version of the library linked do not match, or Z_ERRNO if there is
25 an error reading or writing the files. */
26 int def(FILE *source, FILE *dest, int level)
28 int ret, flush;
29 unsigned have;
30 z_stream strm;
31 char in[CHUNK];
32 char out[CHUNK];
34 /* allocate deflate state */
35 strm.zalloc = Z_NULL;
36 strm.zfree = Z_NULL;
37 strm.opaque = Z_NULL;
38 ret = deflateInit(&strm, level);
39 if (ret != Z_OK)
40 return ret;
42 /* compress until end of file */
43 do {
44 strm.avail_in = fread(in, 1, CHUNK, source);
45 if (ferror(source)) {
46 (void)deflateEnd(&strm);
47 return Z_ERRNO;
49 flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
50 strm.next_in = in;
52 /* run deflate() on input until output buffer not full, finish
53 compression if all of source has been read in */
54 do {
55 strm.avail_out = CHUNK;
56 strm.next_out = out;
57 ret = deflate(&strm, flush); /* no bad return value */
58 assert(ret != Z_STREAM_ERROR); /* state not clobbered */
59 have = CHUNK - strm.avail_out;
60 if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
61 (void)deflateEnd(&strm);
62 return Z_ERRNO;
64 } while (strm.avail_out == 0);
65 assert(strm.avail_in == 0); /* all input will be used */
67 /* done when last data in file processed */
68 } while (flush != Z_FINISH);
69 assert(ret == Z_STREAM_END); /* stream will be complete */
71 /* clean up and return */
72 (void)deflateEnd(&strm);
73 return Z_OK;
76 /* Decompress from file source to file dest until stream ends or EOF.
77 inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
78 allocated for processing, Z_DATA_ERROR if the deflate data is
79 invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
80 the version of the library linked do not match, or Z_ERRNO if there
81 is an error reading or writing the files. */
82 int inf(FILE *source, FILE *dest)
84 int ret;
85 unsigned have;
86 z_stream strm;
87 char in[CHUNK];
88 char out[CHUNK];
90 /* allocate inflate state */
91 strm.zalloc = Z_NULL;
92 strm.zfree = Z_NULL;
93 strm.opaque = Z_NULL;
94 strm.avail_in = 0;
95 strm.next_in = Z_NULL;
96 ret = inflateInit(&strm);
97 if (ret != Z_OK)
98 return ret;
100 /* decompress until deflate stream ends or end of file */
101 do {
102 strm.avail_in = fread(in, 1, CHUNK, source);
103 if (ferror(source)) {
104 (void)inflateEnd(&strm);
105 return Z_ERRNO;
107 if (strm.avail_in == 0)
108 break;
109 strm.next_in = in;
111 /* run inflate() on input until output buffer not full */
112 do {
113 strm.avail_out = CHUNK;
114 strm.next_out = out;
115 ret = inflate(&strm, Z_NO_FLUSH);
116 assert(ret != Z_STREAM_ERROR); /* state not clobbered */
117 switch (ret) {
118 case Z_NEED_DICT:
119 ret = Z_DATA_ERROR; /* and fall through */
120 case Z_DATA_ERROR:
121 case Z_MEM_ERROR:
122 (void)inflateEnd(&strm);
123 return ret;
125 have = CHUNK - strm.avail_out;
126 if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
127 (void)inflateEnd(&strm);
128 return Z_ERRNO;
130 } while (strm.avail_out == 0);
132 /* done when inflate() says it's done */
133 } while (ret != Z_STREAM_END);
135 /* clean up and return */
136 (void)inflateEnd(&strm);
137 return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
140 /* report a zlib or i/o error */
141 void zerr(int ret)
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);
165 /* compress or decompress from stdin to stdout */
166 int main(int argc, char **argv)
168 int ret;
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;
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;
186 /* otherwise, report usage */
187 else {
188 fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr);
189 return 1;