• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

contrib/zlib/examples/zpipe.c

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

Generated on Wed Oct 20 2010 11:12:17 for APBS by  doxygen 1.7.2