zpipe.c

Go to the documentation of this file.
00001 /* zpipe.c: example of proper use of zlib's inflate() and deflate()
00002    Not copyrighted -- provided to the public domain
00003    Version 1.2  9 November 2004  Mark Adler */
00004 
00005 /* Version history:
00006    1.0  30 Oct 2004  First version
00007    1.1   8 Nov 2004  Add void casting for unused return values
00008                      Use switch statement for inflate() return values
00009    1.2   9 Nov 2004  Add assertions to document zlib guarantees
00010    1.3   6 Apr 2005  Remove incorrect assertion in inf()
00011  */
00012 
00013 #include <stdio.h>
00014 #include <string.h>
00015 #include <assert.h>
00016 #include "zlib.h"
00017 
00018 #define CHUNK 16384
00019 
00020 /* Compress from file source to file dest until EOF on source.
00021    def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
00022    allocated for processing, Z_STREAM_ERROR if an invalid compression
00023    level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
00024    version of the library linked do not match, or Z_ERRNO if there is
00025    an error reading or writing the files. */
00026 int def(FILE *source, FILE *dest, int level)
00027 {
00028     int ret, flush;
00029     unsigned have;
00030     z_stream strm;
00031     char in[CHUNK];
00032     char out[CHUNK];
00033 
00034     /* allocate deflate state */
00035     strm.zalloc = Z_NULL;
00036     strm.zfree = Z_NULL;
00037     strm.opaque = Z_NULL;
00038     ret = deflateInit(&strm, level);
00039     if (ret != Z_OK)
00040         return ret;
00041 
00042     /* compress until end of file */
00043     do {
00044         strm.avail_in = fread(in, 1, CHUNK, source);
00045         if (ferror(source)) {
00046             (void)deflateEnd(&strm);
00047             return Z_ERRNO;
00048         }
00049         flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
00050         strm.next_in = in;
00051 
00052         /* run deflate() on input until output buffer not full, finish
00053            compression if all of source has been read in */
00054         do {
00055             strm.avail_out = CHUNK;
00056             strm.next_out = out;
00057             ret = deflate(&strm, flush);    /* no bad return value */
00058             assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
00059             have = CHUNK - strm.avail_out;
00060             if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
00061                 (void)deflateEnd(&strm);
00062                 return Z_ERRNO;
00063             }
00064         } while (strm.avail_out == 0);
00065         assert(strm.avail_in == 0);     /* all input will be used */
00066 
00067         /* done when last data in file processed */
00068     } while (flush != Z_FINISH);
00069     assert(ret == Z_STREAM_END);        /* stream will be complete */
00070 
00071     /* clean up and return */
00072     (void)deflateEnd(&strm);
00073     return Z_OK;
00074 }
00075 
00076 /* Decompress from file source to file dest until stream ends or EOF.
00077    inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
00078    allocated for processing, Z_DATA_ERROR if the deflate data is
00079    invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
00080    the version of the library linked do not match, or Z_ERRNO if there
00081    is an error reading or writing the files. */
00082 int inf(FILE *source, FILE *dest)
00083 {
00084     int ret;
00085     unsigned have;
00086     z_stream strm;
00087     char in[CHUNK];
00088     char out[CHUNK];
00089 
00090     /* allocate inflate state */
00091     strm.zalloc = Z_NULL;
00092     strm.zfree = Z_NULL;
00093     strm.opaque = Z_NULL;
00094     strm.avail_in = 0;
00095     strm.next_in = Z_NULL;
00096     ret = inflateInit(&strm);
00097     if (ret != Z_OK)
00098         return ret;
00099 
00100     /* decompress until deflate stream ends or end of file */
00101     do {
00102         strm.avail_in = fread(in, 1, CHUNK, source);
00103         if (ferror(source)) {
00104             (void)inflateEnd(&strm);
00105             return Z_ERRNO;
00106         }
00107         if (strm.avail_in == 0)
00108             break;
00109         strm.next_in = in;
00110 
00111         /* run inflate() on input until output buffer not full */
00112         do {
00113             strm.avail_out = CHUNK;
00114             strm.next_out = out;
00115             ret = inflate(&strm, Z_NO_FLUSH);
00116             assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
00117             switch (ret) {
00118             case Z_NEED_DICT:
00119                 ret = Z_DATA_ERROR;     /* and fall through */
00120             case Z_DATA_ERROR:
00121             case Z_MEM_ERROR:
00122                 (void)inflateEnd(&strm);
00123                 return ret;
00124             }
00125             have = CHUNK - strm.avail_out;
00126             if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
00127                 (void)inflateEnd(&strm);
00128                 return Z_ERRNO;
00129             }
00130         } while (strm.avail_out == 0);
00131 
00132         /* done when inflate() says it's done */
00133     } while (ret != Z_STREAM_END);
00134 
00135     /* clean up and return */
00136     (void)inflateEnd(&strm);
00137     return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
00138 }
00139 
00140 /* report a zlib or i/o error */
00141 void zerr(int ret)
00142 {
00143     fputs("zpipe: ", stderr);
00144     switch (ret) {
00145     case Z_ERRNO:
00146         if (ferror(stdin))
00147             fputs("error reading stdin\n", stderr);
00148         if (ferror(stdout))
00149             fputs("error writing stdout\n", stderr);
00150         break;
00151     case Z_STREAM_ERROR:
00152         fputs("invalid compression level\n", stderr);
00153         break;
00154     case Z_DATA_ERROR:
00155         fputs("invalid or incomplete deflate data\n", stderr);
00156         break;
00157     case Z_MEM_ERROR:
00158         fputs("out of memory\n", stderr);
00159         break;
00160     case Z_VERSION_ERROR:
00161         fputs("zlib version mismatch!\n", stderr);
00162     }
00163 }
00164 
00165 /* compress or decompress from stdin to stdout */
00166 int main(int argc, char **argv)
00167 {
00168     int ret;
00169 
00170     /* do compression if no arguments */
00171     if (argc == 1) {
00172         ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION);
00173         if (ret != Z_OK)
00174             zerr(ret);
00175         return ret;
00176     }
00177 
00178     /* do decompression if -d specified */
00179     else if (argc == 2 && strcmp(argv[1], "-d") == 0) {
00180         ret = inf(stdin, stdout);
00181         if (ret != Z_OK)
00182             zerr(ret);
00183         return ret;
00184     }
00185 
00186     /* otherwise, report usage */
00187     else {
00188         fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr);
00189         return 1;
00190     }
00191 }

Generated on Fri Apr 14 22:57:29 2006 for minix by  doxygen 1.4.6