[9] | 1 | /* zpipe.c: example of proper use of zlib's inflate() and deflate()
|
---|
| 2 | Not copyrighted -- provided to the public domain
|
---|
| 3 | Version 1.2 9 November 2004 Mark Adler */
|
---|
| 4 |
|
---|
| 5 | /* Version history:
|
---|
| 6 | 1.0 30 Oct 2004 First version
|
---|
| 7 | 1.1 8 Nov 2004 Add void casting for unused return values
|
---|
| 8 | Use switch statement for inflate() return values
|
---|
| 9 | 1.2 9 Nov 2004 Add assertions to document zlib guarantees
|
---|
| 10 | 1.3 6 Apr 2005 Remove incorrect assertion in inf()
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | #include <stdio.h>
|
---|
| 14 | #include <string.h>
|
---|
| 15 | #include <assert.h>
|
---|
| 16 | #include "zlib.h"
|
---|
| 17 |
|
---|
| 18 | #define CHUNK 16384
|
---|
| 19 |
|
---|
| 20 | /* Compress from file source to file dest until EOF on source.
|
---|
| 21 | def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
|
---|
| 22 | allocated for processing, Z_STREAM_ERROR if an invalid compression
|
---|
| 23 | level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
|
---|
| 24 | version of the library linked do not match, or Z_ERRNO if there is
|
---|
| 25 | an error reading or writing the files. */
|
---|
| 26 | int def(FILE *source, FILE *dest, int level)
|
---|
| 27 | {
|
---|
| 28 | int ret, flush;
|
---|
| 29 | unsigned have;
|
---|
| 30 | z_stream strm;
|
---|
| 31 | char in[CHUNK];
|
---|
| 32 | char out[CHUNK];
|
---|
| 33 |
|
---|
| 34 | /* allocate deflate state */
|
---|
| 35 | strm.zalloc = Z_NULL;
|
---|
| 36 | strm.zfree = Z_NULL;
|
---|
| 37 | strm.opaque = Z_NULL;
|
---|
| 38 | ret = deflateInit(&strm, level);
|
---|
| 39 | if (ret != Z_OK)
|
---|
| 40 | return ret;
|
---|
| 41 |
|
---|
| 42 | /* compress until end of file */
|
---|
| 43 | do {
|
---|
| 44 | strm.avail_in = fread(in, 1, CHUNK, source);
|
---|
| 45 | if (ferror(source)) {
|
---|
| 46 | (void)deflateEnd(&strm);
|
---|
| 47 | return Z_ERRNO;
|
---|
| 48 | }
|
---|
| 49 | flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
|
---|
| 50 | strm.next_in = in;
|
---|
| 51 |
|
---|
| 52 | /* run deflate() on input until output buffer not full, finish
|
---|
| 53 | compression if all of source has been read in */
|
---|
| 54 | do {
|
---|
| 55 | strm.avail_out = CHUNK;
|
---|
| 56 | strm.next_out = out;
|
---|
| 57 | ret = deflate(&strm, flush); /* no bad return value */
|
---|
| 58 | assert(ret != Z_STREAM_ERROR); /* state not clobbered */
|
---|
| 59 | have = CHUNK - strm.avail_out;
|
---|
| 60 | if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
|
---|
| 61 | (void)deflateEnd(&strm);
|
---|
| 62 | return Z_ERRNO;
|
---|
| 63 | }
|
---|
| 64 | } while (strm.avail_out == 0);
|
---|
| 65 | assert(strm.avail_in == 0); /* all input will be used */
|
---|
| 66 |
|
---|
| 67 | /* done when last data in file processed */
|
---|
| 68 | } while (flush != Z_FINISH);
|
---|
| 69 | assert(ret == Z_STREAM_END); /* stream will be complete */
|
---|
| 70 |
|
---|
| 71 | /* clean up and return */
|
---|
| 72 | (void)deflateEnd(&strm);
|
---|
| 73 | return Z_OK;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /* Decompress from file source to file dest until stream ends or EOF.
|
---|
| 77 | inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
|
---|
| 78 | allocated for processing, Z_DATA_ERROR if the deflate data is
|
---|
| 79 | invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
|
---|
| 80 | the version of the library linked do not match, or Z_ERRNO if there
|
---|
| 81 | is an error reading or writing the files. */
|
---|
| 82 | int inf(FILE *source, FILE *dest)
|
---|
| 83 | {
|
---|
| 84 | int ret;
|
---|
| 85 | unsigned have;
|
---|
| 86 | z_stream strm;
|
---|
| 87 | char in[CHUNK];
|
---|
| 88 | char out[CHUNK];
|
---|
| 89 |
|
---|
| 90 | /* allocate inflate state */
|
---|
| 91 | strm.zalloc = Z_NULL;
|
---|
| 92 | strm.zfree = Z_NULL;
|
---|
| 93 | strm.opaque = Z_NULL;
|
---|
| 94 | strm.avail_in = 0;
|
---|
| 95 | strm.next_in = Z_NULL;
|
---|
| 96 | ret = inflateInit(&strm);
|
---|
| 97 | if (ret != Z_OK)
|
---|
| 98 | return ret;
|
---|
| 99 |
|
---|
| 100 | /* decompress until deflate stream ends or end of file */
|
---|
| 101 | do {
|
---|
| 102 | strm.avail_in = fread(in, 1, CHUNK, source);
|
---|
| 103 | if (ferror(source)) {
|
---|
| 104 | (void)inflateEnd(&strm);
|
---|
| 105 | return Z_ERRNO;
|
---|
| 106 | }
|
---|
| 107 | if (strm.avail_in == 0)
|
---|
| 108 | break;
|
---|
| 109 | strm.next_in = in;
|
---|
| 110 |
|
---|
| 111 | /* run inflate() on input until output buffer not full */
|
---|
| 112 | do {
|
---|
| 113 | strm.avail_out = CHUNK;
|
---|
| 114 | strm.next_out = out;
|
---|
| 115 | ret = inflate(&strm, Z_NO_FLUSH);
|
---|
| 116 | assert(ret != Z_STREAM_ERROR); /* state not clobbered */
|
---|
| 117 | switch (ret) {
|
---|
| 118 | case Z_NEED_DICT:
|
---|
| 119 | ret = Z_DATA_ERROR; /* and fall through */
|
---|
| 120 | case Z_DATA_ERROR:
|
---|
| 121 | case Z_MEM_ERROR:
|
---|
| 122 | (void)inflateEnd(&strm);
|
---|
| 123 | return ret;
|
---|
| 124 | }
|
---|
| 125 | have = CHUNK - strm.avail_out;
|
---|
| 126 | if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
|
---|
| 127 | (void)inflateEnd(&strm);
|
---|
| 128 | return Z_ERRNO;
|
---|
| 129 | }
|
---|
| 130 | } while (strm.avail_out == 0);
|
---|
| 131 |
|
---|
| 132 | /* done when inflate() says it's done */
|
---|
| 133 | } while (ret != Z_STREAM_END);
|
---|
| 134 |
|
---|
| 135 | /* clean up and return */
|
---|
| 136 | (void)inflateEnd(&strm);
|
---|
| 137 | return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /* report a zlib or i/o error */
|
---|
| 141 | void zerr(int ret)
|
---|
| 142 | {
|
---|
| 143 | fputs("zpipe: ", stderr);
|
---|
| 144 | switch (ret) {
|
---|
| 145 | case Z_ERRNO:
|
---|
| 146 | if (ferror(stdin))
|
---|
| 147 | fputs("error reading stdin\n", stderr);
|
---|
| 148 | if (ferror(stdout))
|
---|
| 149 | fputs("error writing stdout\n", stderr);
|
---|
| 150 | break;
|
---|
| 151 | case Z_STREAM_ERROR:
|
---|
| 152 | fputs("invalid compression level\n", stderr);
|
---|
| 153 | break;
|
---|
| 154 | case Z_DATA_ERROR:
|
---|
| 155 | fputs("invalid or incomplete deflate data\n", stderr);
|
---|
| 156 | break;
|
---|
| 157 | case Z_MEM_ERROR:
|
---|
| 158 | fputs("out of memory\n", stderr);
|
---|
| 159 | break;
|
---|
| 160 | case Z_VERSION_ERROR:
|
---|
| 161 | fputs("zlib version mismatch!\n", stderr);
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /* compress or decompress from stdin to stdout */
|
---|
| 166 | int main(int argc, char **argv)
|
---|
| 167 | {
|
---|
| 168 | int ret;
|
---|
| 169 |
|
---|
| 170 | /* do compression if no arguments */
|
---|
| 171 | if (argc == 1) {
|
---|
| 172 | ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION);
|
---|
| 173 | if (ret != Z_OK)
|
---|
| 174 | zerr(ret);
|
---|
| 175 | return ret;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /* do decompression if -d specified */
|
---|
| 179 | else if (argc == 2 && strcmp(argv[1], "-d") == 0) {
|
---|
| 180 | ret = inf(stdin, stdout);
|
---|
| 181 | if (ret != Z_OK)
|
---|
| 182 | zerr(ret);
|
---|
| 183 | return ret;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /* otherwise, report usage */
|
---|
| 187 | else {
|
---|
| 188 | fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr);
|
---|
| 189 | return 1;
|
---|
| 190 | }
|
---|
| 191 | }
|
---|