minigzip.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* minigzip.c -- simulate gzip using the zlib compression library
  2. * Copyright (C) 1995-2005 Jean-loup Gailly.
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. /*
  6. * minigzip is a minimal implementation of the gzip utility. This is
  7. * only an example of using zlib and isn't meant to replace the
  8. * full-featured gzip. No attempt is made to deal with file systems
  9. * limiting names to 14 or 8+3 characters, etc... Error checking is
  10. * very limited. So use minigzip only for testing; use gzip for the
  11. * real thing. On MSDOS, use only on file names without extension
  12. * or in pipe mode.
  13. */
  14. /* @(#) $Id$ */
  15. #include <stdio.h>
  16. #include "zlib.h"
  17. #ifdef STDC
  18. # include <string.h>
  19. # include <stdlib.h>
  20. #endif
  21. #ifdef USE_MMAP
  22. # include <sys/types.h>
  23. # include <sys/mman.h>
  24. # include <sys/stat.h>
  25. #endif
  26. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
  27. # include <fcntl.h>
  28. # include <io.h>
  29. # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  30. #else
  31. # define SET_BINARY_MODE(file)
  32. #endif
  33. #ifdef VMS
  34. # define unlink delete
  35. # define GZ_SUFFIX "-gz"
  36. #endif
  37. #ifdef RISCOS
  38. # define unlink remove
  39. # define GZ_SUFFIX "-gz"
  40. # define fileno(file) file->__file
  41. #endif
  42. #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
  43. # include <unix.h> /* for fileno */
  44. #endif
  45. #ifndef WIN32 /* unlink already in stdio.h for WIN32 */
  46. extern int unlink OF((const char *));
  47. #endif
  48. #ifndef GZ_SUFFIX
  49. # define GZ_SUFFIX ".gz"
  50. #endif
  51. #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
  52. #define BUFLEN 16384
  53. #define MAX_NAME_LEN 1024
  54. #ifdef MAXSEG_64K
  55. # define local static
  56. /* Needed for systems with limitation on stack size. */
  57. #else
  58. # define local
  59. #endif
  60. char *prog;
  61. void error OF((const char *msg));
  62. void gz_compress OF((FILE *in, gzFile out));
  63. #ifdef USE_MMAP
  64. int gz_compress_mmap OF((FILE *in, gzFile out));
  65. #endif
  66. void gz_uncompress OF((gzFile in, FILE *out));
  67. void file_compress OF((char *file, char *mode));
  68. void file_uncompress OF((char *file));
  69. int main OF((int argc, char *argv[]));
  70. /* ===========================================================================
  71. * Display error message and exit
  72. */
  73. void error(msg)
  74. const char *msg;
  75. {
  76. fprintf(stderr, "%s: %s\n", prog, msg);
  77. exit(1);
  78. }
  79. /* ===========================================================================
  80. * Compress input to output then close both files.
  81. */
  82. void gz_compress(in, out)
  83. FILE *in;
  84. gzFile out;
  85. {
  86. local char buf[BUFLEN];
  87. int len;
  88. int err;
  89. #ifdef USE_MMAP
  90. /* Try first compressing with mmap. If mmap fails (minigzip used in a
  91. * pipe), use the normal fread loop.
  92. */
  93. if (gz_compress_mmap(in, out) == Z_OK) return;
  94. #endif
  95. for (;;) {
  96. len = (int)fread(buf, 1, sizeof(buf), in);
  97. if (ferror(in)) {
  98. perror("fread");
  99. exit(1);
  100. }
  101. if (len == 0) break;
  102. if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
  103. }
  104. fclose(in);
  105. if (gzclose(out) != Z_OK) error("failed gzclose");
  106. }
  107. #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
  108. /* Try compressing the input file at once using mmap. Return Z_OK if
  109. * if success, Z_ERRNO otherwise.
  110. */
  111. int gz_compress_mmap(in, out)
  112. FILE *in;
  113. gzFile out;
  114. {
  115. int len;
  116. int err;
  117. int ifd = fileno(in);
  118. caddr_t buf; /* mmap'ed buffer for the entire input file */
  119. off_t buf_len; /* length of the input file */
  120. struct stat sb;
  121. /* Determine the size of the file, needed for mmap: */
  122. if (fstat(ifd, &sb) < 0) return Z_ERRNO;
  123. buf_len = sb.st_size;
  124. if (buf_len <= 0) return Z_ERRNO;
  125. /* Now do the actual mmap: */
  126. buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
  127. if (buf == (caddr_t)(-1)) return Z_ERRNO;
  128. /* Compress the whole file at once: */
  129. len = gzwrite(out, (char *)buf, (unsigned)buf_len);
  130. if (len != (int)buf_len) error(gzerror(out, &err));
  131. munmap(buf, buf_len);
  132. fclose(in);
  133. if (gzclose(out) != Z_OK) error("failed gzclose");
  134. return Z_OK;
  135. }
  136. #endif /* USE_MMAP */
  137. /* ===========================================================================
  138. * Uncompress input to output then close both files.
  139. */
  140. void gz_uncompress(in, out)
  141. gzFile in;
  142. FILE *out;
  143. {
  144. local char buf[BUFLEN];
  145. int len;
  146. int err;
  147. for (;;) {
  148. len = gzread(in, buf, sizeof(buf));
  149. if (len < 0) error (gzerror(in, &err));
  150. if (len == 0) break;
  151. if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
  152. error("failed fwrite");
  153. }
  154. }
  155. if (fclose(out)) error("failed fclose");
  156. if (gzclose(in) != Z_OK) error("failed gzclose");
  157. }
  158. /* ===========================================================================
  159. * Compress the given file: create a corresponding .gz file and remove the
  160. * original.
  161. */
  162. void file_compress(file, mode)
  163. char *file;
  164. char *mode;
  165. {
  166. local char outfile[MAX_NAME_LEN];
  167. FILE *in;
  168. gzFile out;
  169. strcpy(outfile, file);
  170. strcat(outfile, GZ_SUFFIX);
  171. in = fopen(file, "rb");
  172. if (in == NULL) {
  173. perror(file);
  174. exit(1);
  175. }
  176. out = gzopen(outfile, mode);
  177. if (out == NULL) {
  178. fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
  179. exit(1);
  180. }
  181. gz_compress(in, out);
  182. unlink(file);
  183. }
  184. /* ===========================================================================
  185. * Uncompress the given file and remove the original.
  186. */
  187. void file_uncompress(file)
  188. char *file;
  189. {
  190. local char buf[MAX_NAME_LEN];
  191. char *infile, *outfile;
  192. FILE *out;
  193. gzFile in;
  194. uInt len = (uInt)strlen(file);
  195. strcpy(buf, file);
  196. if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
  197. infile = file;
  198. outfile = buf;
  199. outfile[len-3] = '\0';
  200. } else {
  201. outfile = file;
  202. infile = buf;
  203. strcat(infile, GZ_SUFFIX);
  204. }
  205. in = gzopen(infile, "rb");
  206. if (in == NULL) {
  207. fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
  208. exit(1);
  209. }
  210. out = fopen(outfile, "wb");
  211. if (out == NULL) {
  212. perror(file);
  213. exit(1);
  214. }
  215. gz_uncompress(in, out);
  216. unlink(infile);
  217. }
  218. /* ===========================================================================
  219. * Usage: minigzip [-d] [-f] [-h] [-r] [-1 to -9] [files...]
  220. * -d : decompress
  221. * -f : compress with Z_FILTERED
  222. * -h : compress with Z_HUFFMAN_ONLY
  223. * -r : compress with Z_RLE
  224. * -1 to -9 : compression level
  225. */
  226. int main(argc, argv)
  227. int argc;
  228. char *argv[];
  229. {
  230. int uncompr = 0;
  231. gzFile file;
  232. char outmode[20];
  233. strcpy(outmode, "wb6 ");
  234. prog = argv[0];
  235. argc--, argv++;
  236. while (argc > 0) {
  237. if (strcmp(*argv, "-d") == 0)
  238. uncompr = 1;
  239. else if (strcmp(*argv, "-f") == 0)
  240. outmode[3] = 'f';
  241. else if (strcmp(*argv, "-h") == 0)
  242. outmode[3] = 'h';
  243. else if (strcmp(*argv, "-r") == 0)
  244. outmode[3] = 'R';
  245. else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
  246. (*argv)[2] == 0)
  247. outmode[2] = (*argv)[1];
  248. else
  249. break;
  250. argc--, argv++;
  251. }
  252. if (outmode[3] == ' ')
  253. outmode[3] = 0;
  254. if (argc == 0) {
  255. SET_BINARY_MODE(stdin);
  256. SET_BINARY_MODE(stdout);
  257. if (uncompr) {
  258. file = gzdopen(fileno(stdin), "rb");
  259. if (file == NULL) error("can't gzdopen stdin");
  260. gz_uncompress(file, stdout);
  261. } else {
  262. file = gzdopen(fileno(stdout), outmode);
  263. if (file == NULL) error("can't gzdopen stdout");
  264. gz_compress(stdin, file);
  265. }
  266. } else {
  267. do {
  268. if (uncompr) {
  269. file_uncompress(*argv);
  270. } else {
  271. file_compress(*argv, outmode);
  272. }
  273. } while (argv++, --argc);
  274. }
  275. return 0;
  276. }