minigzip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /* minigzip.c -- simulate gzip using the zlib compression library
  2. * Copyright (C) 1995-2006, 2010 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 "zlib.h"
  16. #include <stdio.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. # ifdef UNDER_CE
  30. # include <stdlib.h>
  31. # endif
  32. # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  33. #else
  34. # define SET_BINARY_MODE(file)
  35. #endif
  36. #ifdef VMS
  37. # define unlink delete
  38. # define GZ_SUFFIX "-gz"
  39. #endif
  40. #ifdef RISCOS
  41. # define unlink remove
  42. # define GZ_SUFFIX "-gz"
  43. # define fileno(file) file->__file
  44. #endif
  45. #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
  46. # include <unix.h> /* for fileno */
  47. #endif
  48. #if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE)
  49. #ifndef WIN32 /* unlink already in stdio.h for WIN32 */
  50. extern int unlink OF((const char *));
  51. #endif
  52. #endif
  53. #if defined(UNDER_CE)
  54. # include <windows.h>
  55. # define perror(s) pwinerror(s)
  56. /* Map the Windows error number in ERROR to a locale-dependent error
  57. message string and return a pointer to it. Typically, the values
  58. for ERROR come from GetLastError.
  59. The string pointed to shall not be modified by the application,
  60. but may be overwritten by a subsequent call to strwinerror
  61. The strwinerror function does not change the current setting
  62. of GetLastError. */
  63. static char *strwinerror (error)
  64. DWORD error;
  65. {
  66. static char buf[1024];
  67. wchar_t *msgbuf;
  68. DWORD lasterr = GetLastError();
  69. DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
  70. | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  71. NULL,
  72. error,
  73. 0, /* Default language */
  74. (LPVOID)&msgbuf,
  75. 0,
  76. NULL);
  77. if (chars != 0) {
  78. /* If there is an \r\n appended, zap it. */
  79. if (chars >= 2
  80. && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
  81. chars -= 2;
  82. msgbuf[chars] = 0;
  83. }
  84. if (chars > sizeof (buf) - 1) {
  85. chars = sizeof (buf) - 1;
  86. msgbuf[chars] = 0;
  87. }
  88. wcstombs(buf, msgbuf, chars + 1);
  89. LocalFree(msgbuf);
  90. }
  91. else {
  92. sprintf(buf, "unknown win32 error (%ld)", error);
  93. }
  94. SetLastError(lasterr);
  95. return buf;
  96. }
  97. static void pwinerror (s)
  98. const char *s;
  99. {
  100. if (s && *s)
  101. fprintf(stderr, "%s: %s\n", s, strwinerror(GetLastError ()));
  102. else
  103. fprintf(stderr, "%s\n", strwinerror(GetLastError ()));
  104. }
  105. #endif /* UNDER_CE */
  106. #ifndef GZ_SUFFIX
  107. # define GZ_SUFFIX ".gz"
  108. #endif
  109. #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
  110. #define BUFLEN 16384
  111. #define MAX_NAME_LEN 1024
  112. #ifdef MAXSEG_64K
  113. # define local static
  114. /* Needed for systems with limitation on stack size. */
  115. #else
  116. # define local
  117. #endif
  118. char *prog;
  119. void error OF((const char *msg));
  120. void gz_compress OF((FILE *in, gzFile out));
  121. #ifdef USE_MMAP
  122. int gz_compress_mmap OF((FILE *in, gzFile out));
  123. #endif
  124. void gz_uncompress OF((gzFile in, FILE *out));
  125. void file_compress OF((char *file, char *mode));
  126. void file_uncompress OF((char *file));
  127. int main OF((int argc, char *argv[]));
  128. /* ===========================================================================
  129. * Display error message and exit
  130. */
  131. void error(msg)
  132. const char *msg;
  133. {
  134. fprintf(stderr, "%s: %s\n", prog, msg);
  135. exit(1);
  136. }
  137. /* ===========================================================================
  138. * Compress input to output then close both files.
  139. */
  140. void gz_compress(in, out)
  141. FILE *in;
  142. gzFile out;
  143. {
  144. local char buf[BUFLEN];
  145. int len;
  146. int err;
  147. #ifdef USE_MMAP
  148. /* Try first compressing with mmap. If mmap fails (minigzip used in a
  149. * pipe), use the normal fread loop.
  150. */
  151. if (gz_compress_mmap(in, out) == Z_OK) return;
  152. #endif
  153. for (;;) {
  154. len = (int)fread(buf, 1, sizeof(buf), in);
  155. if (ferror(in)) {
  156. perror("fread");
  157. exit(1);
  158. }
  159. if (len == 0) break;
  160. if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
  161. }
  162. fclose(in);
  163. if (gzclose(out) != Z_OK) error("failed gzclose");
  164. }
  165. #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
  166. /* Try compressing the input file at once using mmap. Return Z_OK if
  167. * if success, Z_ERRNO otherwise.
  168. */
  169. int gz_compress_mmap(in, out)
  170. FILE *in;
  171. gzFile out;
  172. {
  173. int len;
  174. int err;
  175. int ifd = fileno(in);
  176. caddr_t buf; /* mmap'ed buffer for the entire input file */
  177. off_t buf_len; /* length of the input file */
  178. struct stat sb;
  179. /* Determine the size of the file, needed for mmap: */
  180. if (fstat(ifd, &sb) < 0) return Z_ERRNO;
  181. buf_len = sb.st_size;
  182. if (buf_len <= 0) return Z_ERRNO;
  183. /* Now do the actual mmap: */
  184. buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
  185. if (buf == (caddr_t)(-1)) return Z_ERRNO;
  186. /* Compress the whole file at once: */
  187. len = gzwrite(out, (char *)buf, (unsigned)buf_len);
  188. if (len != (int)buf_len) error(gzerror(out, &err));
  189. munmap(buf, buf_len);
  190. fclose(in);
  191. if (gzclose(out) != Z_OK) error("failed gzclose");
  192. return Z_OK;
  193. }
  194. #endif /* USE_MMAP */
  195. /* ===========================================================================
  196. * Uncompress input to output then close both files.
  197. */
  198. void gz_uncompress(in, out)
  199. gzFile in;
  200. FILE *out;
  201. {
  202. local char buf[BUFLEN];
  203. int len;
  204. int err;
  205. for (;;) {
  206. len = gzread(in, buf, sizeof(buf));
  207. if (len < 0) error (gzerror(in, &err));
  208. if (len == 0) break;
  209. if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
  210. error("failed fwrite");
  211. }
  212. }
  213. if (fclose(out)) error("failed fclose");
  214. if (gzclose(in) != Z_OK) error("failed gzclose");
  215. }
  216. /* ===========================================================================
  217. * Compress the given file: create a corresponding .gz file and remove the
  218. * original.
  219. */
  220. void file_compress(file, mode)
  221. char *file;
  222. char *mode;
  223. {
  224. local char outfile[MAX_NAME_LEN];
  225. FILE *in;
  226. gzFile out;
  227. if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
  228. fprintf(stderr, "%s: filename too long\n", prog);
  229. exit(1);
  230. }
  231. strcpy(outfile, file);
  232. strcat(outfile, GZ_SUFFIX);
  233. in = fopen(file, "rb");
  234. if (in == NULL) {
  235. perror(file);
  236. exit(1);
  237. }
  238. out = gzopen(outfile, mode);
  239. if (out == NULL) {
  240. fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
  241. exit(1);
  242. }
  243. gz_compress(in, out);
  244. unlink(file);
  245. }
  246. /* ===========================================================================
  247. * Uncompress the given file and remove the original.
  248. */
  249. void file_uncompress(file)
  250. char *file;
  251. {
  252. local char buf[MAX_NAME_LEN];
  253. char *infile, *outfile;
  254. FILE *out;
  255. gzFile in;
  256. size_t len = strlen(file);
  257. if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
  258. fprintf(stderr, "%s: filename too long\n", prog);
  259. exit(1);
  260. }
  261. strcpy(buf, file);
  262. if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
  263. infile = file;
  264. outfile = buf;
  265. outfile[len-3] = '\0';
  266. } else {
  267. outfile = file;
  268. infile = buf;
  269. strcat(infile, GZ_SUFFIX);
  270. }
  271. in = gzopen(infile, "rb");
  272. if (in == NULL) {
  273. fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
  274. exit(1);
  275. }
  276. out = fopen(outfile, "wb");
  277. if (out == NULL) {
  278. perror(file);
  279. exit(1);
  280. }
  281. gz_uncompress(in, out);
  282. unlink(infile);
  283. }
  284. /* ===========================================================================
  285. * Usage: minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...]
  286. * -c : write to standard output
  287. * -d : decompress
  288. * -f : compress with Z_FILTERED
  289. * -h : compress with Z_HUFFMAN_ONLY
  290. * -r : compress with Z_RLE
  291. * -1 to -9 : compression level
  292. */
  293. int main(argc, argv)
  294. int argc;
  295. char *argv[];
  296. {
  297. int copyout = 0;
  298. int uncompr = 0;
  299. gzFile file;
  300. char *bname, outmode[20];
  301. strcpy(outmode, "wb6 ");
  302. prog = argv[0];
  303. bname = strrchr(argv[0], '/');
  304. if (bname)
  305. bname++;
  306. else
  307. bname = argv[0];
  308. argc--, argv++;
  309. if (!strcmp(bname, "gunzip"))
  310. uncompr = 1;
  311. else if (!strcmp(bname, "zcat"))
  312. copyout = uncompr = 1;
  313. while (argc > 0) {
  314. if (strcmp(*argv, "-c") == 0)
  315. copyout = 1;
  316. else if (strcmp(*argv, "-d") == 0)
  317. uncompr = 1;
  318. else if (strcmp(*argv, "-f") == 0)
  319. outmode[3] = 'f';
  320. else if (strcmp(*argv, "-h") == 0)
  321. outmode[3] = 'h';
  322. else if (strcmp(*argv, "-r") == 0)
  323. outmode[3] = 'R';
  324. else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
  325. (*argv)[2] == 0)
  326. outmode[2] = (*argv)[1];
  327. else
  328. break;
  329. argc--, argv++;
  330. }
  331. if (outmode[3] == ' ')
  332. outmode[3] = 0;
  333. if (argc == 0) {
  334. SET_BINARY_MODE(stdin);
  335. SET_BINARY_MODE(stdout);
  336. if (uncompr) {
  337. file = gzdopen(fileno(stdin), "rb");
  338. if (file == NULL) error("can't gzdopen stdin");
  339. gz_uncompress(file, stdout);
  340. } else {
  341. file = gzdopen(fileno(stdout), outmode);
  342. if (file == NULL) error("can't gzdopen stdout");
  343. gz_compress(stdin, file);
  344. }
  345. } else {
  346. if (copyout) {
  347. SET_BINARY_MODE(stdout);
  348. }
  349. do {
  350. if (uncompr) {
  351. if (copyout) {
  352. file = gzopen(*argv, "rb");
  353. if (file == NULL)
  354. fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
  355. else
  356. gz_uncompress(file, stdout);
  357. } else {
  358. file_uncompress(*argv);
  359. }
  360. } else {
  361. if (copyout) {
  362. FILE * in = fopen(*argv, "rb");
  363. if (in == NULL) {
  364. perror(*argv);
  365. } else {
  366. file = gzdopen(fileno(stdout), outmode);
  367. if (file == NULL) error("can't gzdopen stdout");
  368. gz_compress(in, file);
  369. }
  370. } else {
  371. file_compress(*argv, outmode);
  372. }
  373. }
  374. } while (argv++, --argc);
  375. }
  376. return 0;
  377. }