kusano fc6ab3
/* zpipe.c: example of proper use of zlib's inflate() and deflate()
kusano fc6ab3
   Not copyrighted -- provided to the public domain
kusano fc6ab3
   Version 1.4  11 December 2005  Mark Adler */
kusano fc6ab3
kusano fc6ab3
/* Version history:
kusano fc6ab3
   1.0  30 Oct 2004  First version
kusano fc6ab3
   1.1   8 Nov 2004  Add void casting for unused return values
kusano fc6ab3
                     Use switch statement for inflate() return values
kusano fc6ab3
   1.2   9 Nov 2004  Add assertions to document zlib guarantees
kusano fc6ab3
   1.3   6 Apr 2005  Remove incorrect assertion in inf()
kusano fc6ab3
   1.4  11 Dec 2005  Add hack to avoid MSDOS end-of-line conversions
kusano fc6ab3
                     Avoid some compiler warnings for input and output buffers
kusano fc6ab3
 */
kusano fc6ab3
kusano fc6ab3
#include <stdio.h></stdio.h>
kusano fc6ab3
#include <string.h></string.h>
kusano fc6ab3
#include <assert.h></assert.h>
kusano fc6ab3
#include "zlib.h"
kusano fc6ab3
kusano fc6ab3
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
kusano fc6ab3
#  include <fcntl.h></fcntl.h>
kusano fc6ab3
#  include <io.h></io.h>
kusano fc6ab3
#  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
kusano fc6ab3
#else
kusano fc6ab3
#  define SET_BINARY_MODE(file)
kusano fc6ab3
#endif
kusano fc6ab3
kusano fc6ab3
#define CHUNK 16384
kusano fc6ab3
kusano fc6ab3
/* Compress from file source to file dest until EOF on source.
kusano fc6ab3
   def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
kusano fc6ab3
   allocated for processing, Z_STREAM_ERROR if an invalid compression
kusano fc6ab3
   level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
kusano fc6ab3
   version of the library linked do not match, or Z_ERRNO if there is
kusano fc6ab3
   an error reading or writing the files. */
kusano fc6ab3
int def(FILE *source, FILE *dest, int level)
kusano fc6ab3
{
kusano fc6ab3
    int ret, flush;
kusano fc6ab3
    unsigned have;
kusano fc6ab3
    z_stream strm;
kusano fc6ab3
    unsigned char in[CHUNK];
kusano fc6ab3
    unsigned char out[CHUNK];
kusano fc6ab3
kusano fc6ab3
    /* allocate deflate state */
kusano fc6ab3
    strm.zalloc = Z_NULL;
kusano fc6ab3
    strm.zfree = Z_NULL;
kusano fc6ab3
    strm.opaque = Z_NULL;
kusano fc6ab3
    ret = deflateInit(&strm, level);
kusano fc6ab3
    if (ret != Z_OK)
kusano fc6ab3
        return ret;
kusano fc6ab3
kusano fc6ab3
    /* compress until end of file */
kusano fc6ab3
    do {
kusano fc6ab3
        strm.avail_in = fread(in, 1, CHUNK, source);
kusano fc6ab3
        if (ferror(source)) {
kusano fc6ab3
            (void)deflateEnd(&strm);
kusano fc6ab3
            return Z_ERRNO;
kusano fc6ab3
        }
kusano fc6ab3
        flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
kusano fc6ab3
        strm.next_in = in;
kusano fc6ab3
kusano fc6ab3
        /* run deflate() on input until output buffer not full, finish
kusano fc6ab3
           compression if all of source has been read in */
kusano fc6ab3
        do {
kusano fc6ab3
            strm.avail_out = CHUNK;
kusano fc6ab3
            strm.next_out = out;
kusano fc6ab3
            ret = deflate(&strm, flush);    /* no bad return value */
kusano fc6ab3
            assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
kusano fc6ab3
            have = CHUNK - strm.avail_out;
kusano fc6ab3
            if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
kusano fc6ab3
                (void)deflateEnd(&strm);
kusano fc6ab3
                return Z_ERRNO;
kusano fc6ab3
            }
kusano fc6ab3
        } while (strm.avail_out == 0);
kusano fc6ab3
        assert(strm.avail_in == 0);     /* all input will be used */
kusano fc6ab3
kusano fc6ab3
        /* done when last data in file processed */
kusano fc6ab3
    } while (flush != Z_FINISH);
kusano fc6ab3
    assert(ret == Z_STREAM_END);        /* stream will be complete */
kusano fc6ab3
kusano fc6ab3
    /* clean up and return */
kusano fc6ab3
    (void)deflateEnd(&strm);
kusano fc6ab3
    return Z_OK;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/* Decompress from file source to file dest until stream ends or EOF.
kusano fc6ab3
   inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
kusano fc6ab3
   allocated for processing, Z_DATA_ERROR if the deflate data is
kusano fc6ab3
   invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
kusano fc6ab3
   the version of the library linked do not match, or Z_ERRNO if there
kusano fc6ab3
   is an error reading or writing the files. */
kusano fc6ab3
int inf(FILE *source, FILE *dest)
kusano fc6ab3
{
kusano fc6ab3
    int ret;
kusano fc6ab3
    unsigned have;
kusano fc6ab3
    z_stream strm;
kusano fc6ab3
    unsigned char in[CHUNK];
kusano fc6ab3
    unsigned char out[CHUNK];
kusano fc6ab3
kusano fc6ab3
    /* allocate inflate state */
kusano fc6ab3
    strm.zalloc = Z_NULL;
kusano fc6ab3
    strm.zfree = Z_NULL;
kusano fc6ab3
    strm.opaque = Z_NULL;
kusano fc6ab3
    strm.avail_in = 0;
kusano fc6ab3
    strm.next_in = Z_NULL;
kusano fc6ab3
    ret = inflateInit(&strm);
kusano fc6ab3
    if (ret != Z_OK)
kusano fc6ab3
        return ret;
kusano fc6ab3
kusano fc6ab3
    /* decompress until deflate stream ends or end of file */
kusano fc6ab3
    do {
kusano fc6ab3
        strm.avail_in = fread(in, 1, CHUNK, source);
kusano fc6ab3
        if (ferror(source)) {
kusano fc6ab3
            (void)inflateEnd(&strm);
kusano fc6ab3
            return Z_ERRNO;
kusano fc6ab3
        }
kusano fc6ab3
        if (strm.avail_in == 0)
kusano fc6ab3
            break;
kusano fc6ab3
        strm.next_in = in;
kusano fc6ab3
kusano fc6ab3
        /* run inflate() on input until output buffer not full */
kusano fc6ab3
        do {
kusano fc6ab3
            strm.avail_out = CHUNK;
kusano fc6ab3
            strm.next_out = out;
kusano fc6ab3
            ret = inflate(&strm, Z_NO_FLUSH);
kusano fc6ab3
            assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
kusano fc6ab3
            switch (ret) {
kusano fc6ab3
            case Z_NEED_DICT:
kusano fc6ab3
                ret = Z_DATA_ERROR;     /* and fall through */
kusano fc6ab3
            case Z_DATA_ERROR:
kusano fc6ab3
            case Z_MEM_ERROR:
kusano fc6ab3
                (void)inflateEnd(&strm);
kusano fc6ab3
                return ret;
kusano fc6ab3
            }
kusano fc6ab3
            have = CHUNK - strm.avail_out;
kusano fc6ab3
            if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
kusano fc6ab3
                (void)inflateEnd(&strm);
kusano fc6ab3
                return Z_ERRNO;
kusano fc6ab3
            }
kusano fc6ab3
        } while (strm.avail_out == 0);
kusano fc6ab3
kusano fc6ab3
        /* done when inflate() says it's done */
kusano fc6ab3
    } while (ret != Z_STREAM_END);
kusano fc6ab3
kusano fc6ab3
    /* clean up and return */
kusano fc6ab3
    (void)inflateEnd(&strm);
kusano fc6ab3
    return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/* report a zlib or i/o error */
kusano fc6ab3
void zerr(int ret)
kusano fc6ab3
{
kusano fc6ab3
    fputs("zpipe: ", stderr);
kusano fc6ab3
    switch (ret) {
kusano fc6ab3
    case Z_ERRNO:
kusano fc6ab3
        if (ferror(stdin))
kusano fc6ab3
            fputs("error reading stdin\n", stderr);
kusano fc6ab3
        if (ferror(stdout))
kusano fc6ab3
            fputs("error writing stdout\n", stderr);
kusano fc6ab3
        break;
kusano fc6ab3
    case Z_STREAM_ERROR:
kusano fc6ab3
        fputs("invalid compression level\n", stderr);
kusano fc6ab3
        break;
kusano fc6ab3
    case Z_DATA_ERROR:
kusano fc6ab3
        fputs("invalid or incomplete deflate data\n", stderr);
kusano fc6ab3
        break;
kusano fc6ab3
    case Z_MEM_ERROR:
kusano fc6ab3
        fputs("out of memory\n", stderr);
kusano fc6ab3
        break;
kusano fc6ab3
    case Z_VERSION_ERROR:
kusano fc6ab3
        fputs("zlib version mismatch!\n", stderr);
kusano fc6ab3
    }
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/* compress or decompress from stdin to stdout */
kusano fc6ab3
int main(int argc, char **argv)
kusano fc6ab3
{
kusano fc6ab3
    int ret;
kusano fc6ab3
kusano fc6ab3
    /* avoid end-of-line conversions */
kusano fc6ab3
    SET_BINARY_MODE(stdin);
kusano fc6ab3
    SET_BINARY_MODE(stdout);
kusano fc6ab3
kusano fc6ab3
    /* do compression if no arguments */
kusano fc6ab3
    if (argc == 1) {
kusano fc6ab3
        ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION);
kusano fc6ab3
        if (ret != Z_OK)
kusano fc6ab3
            zerr(ret);
kusano fc6ab3
        return ret;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* do decompression if -d specified */
kusano fc6ab3
    else if (argc == 2 && strcmp(argv[1], "-d") == 0) {
kusano fc6ab3
        ret = inf(stdin, stdout);
kusano fc6ab3
        if (ret != Z_OK)
kusano fc6ab3
            zerr(ret);
kusano fc6ab3
        return ret;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* otherwise, report usage */
kusano fc6ab3
    else {
kusano fc6ab3
        fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr);
kusano fc6ab3
        return 1;
kusano fc6ab3
    }
kusano fc6ab3
}