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