zpipe.c 6.2 KB

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