annotate src/zlib-1.2.8/examples/zpipe.c @ 169:223a55898ab9 tip default

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