/* zpipe.c: example of proper use of zlib's inflate() and deflate()
Not copyrighted -- provided to the public domain
Version 1.4 11 December 2005 Mark Adler */
/* Version history:
1.0 30 Oct 2004 First version
1.1 8 Nov 2004 Add void casting for unused return values
Use switch statement for inflate() return values
1.2 9 Nov 2004 Add assertions to document zlib guarantees
1.3 6 Apr 2005 Remove incorrect assertion in inf()
1.4 11 Dec 2005 Add hack to avoid MSDOS end-of-line conversions
Avoid some compiler warnings for input and output buffers
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "zlib.h"
# include <fcntl.h>
# include <io.h>
#else
#endif
/* Compress from file source to file dest until EOF on source.
def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
allocated for processing, Z_STREAM_ERROR if an invalid compression
level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
version of the library linked do not match, or Z_ERRNO if there is
an error reading or writing the files. */
{
unsigned have;
/* allocate deflate state */
return ret;
/* compress until end of file */
do {
(void)deflateEnd(&strm);
return Z_ERRNO;
}
/* run deflate() on input until output buffer not full, finish
compression if all of source has been read in */
do {
(void)deflateEnd(&strm);
return Z_ERRNO;
}
/* done when last data in file processed */
/* clean up and return */
(void)deflateEnd(&strm);
return Z_OK;
}
/* Decompress from file source to file dest until stream ends or EOF.
inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
allocated for processing, Z_DATA_ERROR if the deflate data is
invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
the version of the library linked do not match, or Z_ERRNO if there
is an error reading or writing the files. */
{
int ret;
unsigned have;
/* allocate inflate state */
return ret;
/* decompress until deflate stream ends or end of file */
do {
(void)inflateEnd(&strm);
return Z_ERRNO;
}
break;
/* run inflate() on input until output buffer not full */
do {
switch (ret) {
case Z_NEED_DICT:
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
return ret;
}
(void)inflateEnd(&strm);
return Z_ERRNO;
}
/* done when inflate() says it's done */
} while (ret != Z_STREAM_END);
/* clean up and return */
(void)inflateEnd(&strm);
}
/* report a zlib or i/o error */
{
switch (ret) {
case Z_ERRNO:
break;
case Z_STREAM_ERROR:
break;
case Z_DATA_ERROR:
break;
case Z_MEM_ERROR:
break;
case Z_VERSION_ERROR:
}
}
/* compress or decompress from stdin to stdout */
{
int ret;
/* avoid end-of-line conversions */
/* do compression if no arguments */
if (argc == 1) {
return ret;
}
/* do decompression if -d specified */
return ret;
}
/* otherwise, report usage */
else {
return 1;
}
}