You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

minigzip.c 7.7KB

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