annotate src/zlib-1.2.7/examples/zpipe.c @ 23:619f715526df sv_v2.1

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