1 /* $NetBSD: zpipe.c,v 1.1.1.1 2006/01/14 20:11:09 christos Exp $ */
3 /* zpipe.c: example of proper use of zlib's inflate() and deflate()
4 Not copyrighted -- provided to the public domain
5 Version 1.2 9 November 2004 Mark Adler */
8 1.0 30 Oct 2004 First version
9 1.1 8 Nov 2004 Add void casting for unused return values
10 Use switch statement for inflate() return values
11 1.2 9 Nov 2004 Add assertions to document zlib guarantees
12 1.3 6 Apr 2005 Remove incorrect assertion in inf()
22 /* Compress from file source to file dest until EOF on source.
23 def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
24 allocated for processing, Z_STREAM_ERROR if an invalid compression
25 level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
26 version of the library linked do not match, or Z_ERRNO if there is
27 an error reading or writing the files. */
28 int def(FILE *source
, FILE *dest
, int level
)
36 /* allocate deflate state */
40 ret
= deflateInit(&strm
, level
);
44 /* compress until end of file */
46 strm
.avail_in
= fread(in
, 1, CHUNK
, source
);
48 (void)deflateEnd(&strm
);
51 flush
= feof(source
) ? Z_FINISH
: Z_NO_FLUSH
;
54 /* run deflate() on input until output buffer not full, finish
55 compression if all of source has been read in */
57 strm
.avail_out
= CHUNK
;
59 ret
= deflate(&strm
, flush
); /* no bad return value */
60 assert(ret
!= Z_STREAM_ERROR
); /* state not clobbered */
61 have
= CHUNK
- strm
.avail_out
;
62 if (fwrite(out
, 1, have
, dest
) != have
|| ferror(dest
)) {
63 (void)deflateEnd(&strm
);
66 } while (strm
.avail_out
== 0);
67 assert(strm
.avail_in
== 0); /* all input will be used */
69 /* done when last data in file processed */
70 } while (flush
!= Z_FINISH
);
71 assert(ret
== Z_STREAM_END
); /* stream will be complete */
73 /* clean up and return */
74 (void)deflateEnd(&strm
);
78 /* Decompress from file source to file dest until stream ends or EOF.
79 inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
80 allocated for processing, Z_DATA_ERROR if the deflate data is
81 invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
82 the version of the library linked do not match, or Z_ERRNO if there
83 is an error reading or writing the files. */
84 int inf(FILE *source
, FILE *dest
)
92 /* allocate inflate state */
97 strm
.next_in
= Z_NULL
;
98 ret
= inflateInit(&strm
);
102 /* decompress until deflate stream ends or end of file */
104 strm
.avail_in
= fread(in
, 1, CHUNK
, source
);
105 if (ferror(source
)) {
106 (void)inflateEnd(&strm
);
109 if (strm
.avail_in
== 0)
113 /* run inflate() on input until output buffer not full */
115 strm
.avail_out
= CHUNK
;
117 ret
= inflate(&strm
, Z_NO_FLUSH
);
118 assert(ret
!= Z_STREAM_ERROR
); /* state not clobbered */
121 ret
= Z_DATA_ERROR
; /* and fall through */
124 (void)inflateEnd(&strm
);
127 have
= CHUNK
- strm
.avail_out
;
128 if (fwrite(out
, 1, have
, dest
) != have
|| ferror(dest
)) {
129 (void)inflateEnd(&strm
);
132 } while (strm
.avail_out
== 0);
134 /* done when inflate() says it's done */
135 } while (ret
!= Z_STREAM_END
);
137 /* clean up and return */
138 (void)inflateEnd(&strm
);
139 return ret
== Z_STREAM_END
? Z_OK
: Z_DATA_ERROR
;
142 /* report a zlib or i/o error */
145 fputs("zpipe: ", stderr
);
149 fputs("error reading stdin\n", stderr
);
151 fputs("error writing stdout\n", stderr
);
154 fputs("invalid compression level\n", stderr
);
157 fputs("invalid or incomplete deflate data\n", stderr
);
160 fputs("out of memory\n", stderr
);
162 case Z_VERSION_ERROR
:
163 fputs("zlib version mismatch!\n", stderr
);
167 /* compress or decompress from stdin to stdout */
168 int main(int argc
, char **argv
)
172 /* do compression if no arguments */
174 ret
= def(stdin
, stdout
, Z_DEFAULT_COMPRESSION
);
180 /* do decompression if -d specified */
181 else if (argc
== 2 && strcmp(argv
[1], "-d") == 0) {
182 ret
= inf(stdin
, stdout
);
188 /* otherwise, report usage */
190 fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr
);