minigzip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /* minigzip.c -- simulate gzip using the zlib compression library
  2. * Copyright (C) 1995-2006, 2010, 2011 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. #ifdef Z_SOLO
  119. /* for Z_SOLO, create simplified gz* functions using deflate and inflate */
  120. #if defined(Z_HAVE_UNISTD_H) || defined(Z_LARGE)
  121. # include <unistd.h> /* for unlink() */
  122. #endif
  123. void *myalloc OF((void *, unsigned, unsigned));
  124. void myfree OF((void *, void *));
  125. void *myalloc(q, n, m)
  126. void *q;
  127. unsigned n, m;
  128. {
  129. q = Z_NULL;
  130. return calloc(n, m);
  131. }
  132. void myfree(q, p)
  133. void *q, *p;
  134. {
  135. q = Z_NULL;
  136. free(p);
  137. }
  138. typedef struct gzFile_s {
  139. FILE *file;
  140. int write;
  141. int err;
  142. char *msg;
  143. z_stream strm;
  144. } *gzFile;
  145. gzFile gzopen OF((const char *, const char *));
  146. gzFile gzdopen OF((int, const char *));
  147. gzFile gz_open OF((const char *, int, const char *));
  148. gzFile gzopen(path, mode)
  149. const char *path;
  150. const char *mode;
  151. {
  152. return gz_open(path, -1, mode);
  153. }
  154. gzFile gzdopen(fd, mode)
  155. int fd;
  156. const char *mode;
  157. {
  158. return gz_open(NULL, fd, mode);
  159. }
  160. gzFile gz_open(path, fd, mode)
  161. const char *path;
  162. int fd;
  163. const char *mode;
  164. {
  165. gzFile gz;
  166. int ret;
  167. gz = malloc(sizeof(struct gzFile_s));
  168. if (gz == NULL)
  169. return NULL;
  170. gz->write = strchr(mode, 'w') != NULL;
  171. gz->strm.zalloc = myalloc;
  172. gz->strm.zfree = myfree;
  173. gz->strm.opaque = Z_NULL;
  174. if (gz->write)
  175. ret = deflateInit2(&(gz->strm), -1, 8, 15 + 16, 8, 0);
  176. else {
  177. gz->strm.next_in = 0;
  178. gz->strm.avail_in = Z_NULL;
  179. ret = inflateInit2(&(gz->strm), 15 + 16);
  180. }
  181. if (ret != Z_OK) {
  182. free(gz);
  183. return NULL;
  184. }
  185. gz->file = path == NULL ? fdopen(fd, gz->write ? "wb" : "rb") :
  186. fopen(path, gz->write ? "wb" : "rb");
  187. if (gz->file == NULL) {
  188. gz->write ? deflateEnd(&(gz->strm)) : inflateEnd(&(gz->strm));
  189. free(gz);
  190. return NULL;
  191. }
  192. gz->err = 0;
  193. gz->msg = "";
  194. return gz;
  195. }
  196. int gzwrite OF((gzFile, const void *, unsigned));
  197. int gzwrite(gz, buf, len)
  198. gzFile gz;
  199. const void *buf;
  200. unsigned len;
  201. {
  202. z_stream *strm;
  203. unsigned char out[BUFLEN];
  204. if (gz == NULL || !gz->write)
  205. return 0;
  206. strm = &(gz->strm);
  207. strm->next_in = (void *)buf;
  208. strm->avail_in = len;
  209. do {
  210. strm->next_out = out;
  211. strm->avail_out = BUFLEN;
  212. (void)deflate(strm, Z_NO_FLUSH);
  213. fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
  214. } while (strm->avail_out == 0);
  215. return len;
  216. }
  217. int gzread OF((gzFile, void *, unsigned));
  218. int gzread(gz, buf, len)
  219. gzFile gz;
  220. void *buf;
  221. unsigned len;
  222. {
  223. int ret;
  224. unsigned got;
  225. unsigned char in[1];
  226. z_stream *strm;
  227. if (gz == NULL || gz->write)
  228. return 0;
  229. if (gz->err)
  230. return 0;
  231. strm = &(gz->strm);
  232. strm->next_out = (void *)buf;
  233. strm->avail_out = len;
  234. do {
  235. got = fread(in, 1, 1, gz->file);
  236. if (got == 0)
  237. break;
  238. strm->next_in = in;
  239. strm->avail_in = 1;
  240. ret = inflate(strm, Z_NO_FLUSH);
  241. if (ret == Z_DATA_ERROR) {
  242. gz->err = Z_DATA_ERROR;
  243. gz->msg = strm->msg;
  244. return 0;
  245. }
  246. if (ret == Z_STREAM_END)
  247. inflateReset(strm);
  248. } while (strm->avail_out);
  249. return len - strm->avail_out;
  250. }
  251. int gzclose OF((gzFile));
  252. int gzclose(gz)
  253. gzFile gz;
  254. {
  255. z_stream *strm;
  256. unsigned char out[BUFLEN];
  257. if (gz == NULL)
  258. return Z_STREAM_ERROR;
  259. strm = &(gz->strm);
  260. if (gz->write) {
  261. strm->next_in = Z_NULL;
  262. strm->avail_in = 0;
  263. do {
  264. strm->next_out = out;
  265. strm->avail_out = BUFLEN;
  266. (void)deflate(strm, Z_FINISH);
  267. fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
  268. } while (strm->avail_out == 0);
  269. deflateEnd(strm);
  270. }
  271. else
  272. inflateEnd(strm);
  273. fclose(gz->file);
  274. free(gz);
  275. return Z_OK;
  276. }
  277. const char *gzerror OF((gzFile, int *));
  278. const char *gzerror(gz, err)
  279. gzFile gz;
  280. int *err;
  281. {
  282. *err = gz->err;
  283. return gz->msg;
  284. }
  285. #endif
  286. char *prog;
  287. void error OF((const char *msg));
  288. void gz_compress OF((FILE *in, gzFile out));
  289. #ifdef USE_MMAP
  290. int gz_compress_mmap OF((FILE *in, gzFile out));
  291. #endif
  292. void gz_uncompress OF((gzFile in, FILE *out));
  293. void file_compress OF((char *file, char *mode));
  294. void file_uncompress OF((char *file));
  295. int main OF((int argc, char *argv[]));
  296. /* ===========================================================================
  297. * Display error message and exit
  298. */
  299. void error(msg)
  300. const char *msg;
  301. {
  302. fprintf(stderr, "%s: %s\n", prog, msg);
  303. exit(1);
  304. }
  305. /* ===========================================================================
  306. * Compress input to output then close both files.
  307. */
  308. void gz_compress(in, out)
  309. FILE *in;
  310. gzFile out;
  311. {
  312. local char buf[BUFLEN];
  313. int len;
  314. int err;
  315. #ifdef USE_MMAP
  316. /* Try first compressing with mmap. If mmap fails (minigzip used in a
  317. * pipe), use the normal fread loop.
  318. */
  319. if (gz_compress_mmap(in, out) == Z_OK) return;
  320. #endif
  321. for (;;) {
  322. len = (int)fread(buf, 1, sizeof(buf), in);
  323. if (ferror(in)) {
  324. perror("fread");
  325. exit(1);
  326. }
  327. if (len == 0) break;
  328. if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
  329. }
  330. fclose(in);
  331. if (gzclose(out) != Z_OK) error("failed gzclose");
  332. }
  333. #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
  334. /* Try compressing the input file at once using mmap. Return Z_OK if
  335. * if success, Z_ERRNO otherwise.
  336. */
  337. int gz_compress_mmap(in, out)
  338. FILE *in;
  339. gzFile out;
  340. {
  341. int len;
  342. int err;
  343. int ifd = fileno(in);
  344. caddr_t buf; /* mmap'ed buffer for the entire input file */
  345. off_t buf_len; /* length of the input file */
  346. struct stat sb;
  347. /* Determine the size of the file, needed for mmap: */
  348. if (fstat(ifd, &sb) < 0) return Z_ERRNO;
  349. buf_len = sb.st_size;
  350. if (buf_len <= 0) return Z_ERRNO;
  351. /* Now do the actual mmap: */
  352. buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
  353. if (buf == (caddr_t)(-1)) return Z_ERRNO;
  354. /* Compress the whole file at once: */
  355. len = gzwrite(out, (char *)buf, (unsigned)buf_len);
  356. if (len != (int)buf_len) error(gzerror(out, &err));
  357. munmap(buf, buf_len);
  358. fclose(in);
  359. if (gzclose(out) != Z_OK) error("failed gzclose");
  360. return Z_OK;
  361. }
  362. #endif /* USE_MMAP */
  363. /* ===========================================================================
  364. * Uncompress input to output then close both files.
  365. */
  366. void gz_uncompress(in, out)
  367. gzFile in;
  368. FILE *out;
  369. {
  370. local char buf[BUFLEN];
  371. int len;
  372. int err;
  373. for (;;) {
  374. len = gzread(in, buf, sizeof(buf));
  375. if (len < 0) error (gzerror(in, &err));
  376. if (len == 0) break;
  377. if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
  378. error("failed fwrite");
  379. }
  380. }
  381. if (fclose(out)) error("failed fclose");
  382. if (gzclose(in) != Z_OK) error("failed gzclose");
  383. }
  384. /* ===========================================================================
  385. * Compress the given file: create a corresponding .gz file and remove the
  386. * original.
  387. */
  388. void file_compress(file, mode)
  389. char *file;
  390. char *mode;
  391. {
  392. local char outfile[MAX_NAME_LEN];
  393. FILE *in;
  394. gzFile out;
  395. if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
  396. fprintf(stderr, "%s: filename too long\n", prog);
  397. exit(1);
  398. }
  399. strcpy(outfile, file);
  400. strcat(outfile, GZ_SUFFIX);
  401. in = fopen(file, "rb");
  402. if (in == NULL) {
  403. perror(file);
  404. exit(1);
  405. }
  406. out = gzopen(outfile, mode);
  407. if (out == NULL) {
  408. fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
  409. exit(1);
  410. }
  411. gz_compress(in, out);
  412. unlink(file);
  413. }
  414. /* ===========================================================================
  415. * Uncompress the given file and remove the original.
  416. */
  417. void file_uncompress(file)
  418. char *file;
  419. {
  420. local char buf[MAX_NAME_LEN];
  421. char *infile, *outfile;
  422. FILE *out;
  423. gzFile in;
  424. size_t len = strlen(file);
  425. if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
  426. fprintf(stderr, "%s: filename too long\n", prog);
  427. exit(1);
  428. }
  429. strcpy(buf, file);
  430. if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
  431. infile = file;
  432. outfile = buf;
  433. outfile[len-3] = '\0';
  434. } else {
  435. outfile = file;
  436. infile = buf;
  437. strcat(infile, GZ_SUFFIX);
  438. }
  439. in = gzopen(infile, "rb");
  440. if (in == NULL) {
  441. fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
  442. exit(1);
  443. }
  444. out = fopen(outfile, "wb");
  445. if (out == NULL) {
  446. perror(file);
  447. exit(1);
  448. }
  449. gz_uncompress(in, out);
  450. unlink(infile);
  451. }
  452. /* ===========================================================================
  453. * Usage: minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...]
  454. * -c : write to standard output
  455. * -d : decompress
  456. * -f : compress with Z_FILTERED
  457. * -h : compress with Z_HUFFMAN_ONLY
  458. * -r : compress with Z_RLE
  459. * -1 to -9 : compression level
  460. */
  461. int main(argc, argv)
  462. int argc;
  463. char *argv[];
  464. {
  465. int copyout = 0;
  466. int uncompr = 0;
  467. gzFile file;
  468. char *bname, outmode[20];
  469. strcpy(outmode, "wb6 ");
  470. prog = argv[0];
  471. bname = strrchr(argv[0], '/');
  472. if (bname)
  473. bname++;
  474. else
  475. bname = argv[0];
  476. argc--, argv++;
  477. if (!strcmp(bname, "gunzip"))
  478. uncompr = 1;
  479. else if (!strcmp(bname, "zcat"))
  480. copyout = uncompr = 1;
  481. while (argc > 0) {
  482. if (strcmp(*argv, "-c") == 0)
  483. copyout = 1;
  484. else if (strcmp(*argv, "-d") == 0)
  485. uncompr = 1;
  486. else if (strcmp(*argv, "-f") == 0)
  487. outmode[3] = 'f';
  488. else if (strcmp(*argv, "-h") == 0)
  489. outmode[3] = 'h';
  490. else if (strcmp(*argv, "-r") == 0)
  491. outmode[3] = 'R';
  492. else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
  493. (*argv)[2] == 0)
  494. outmode[2] = (*argv)[1];
  495. else
  496. break;
  497. argc--, argv++;
  498. }
  499. if (outmode[3] == ' ')
  500. outmode[3] = 0;
  501. if (argc == 0) {
  502. SET_BINARY_MODE(stdin);
  503. SET_BINARY_MODE(stdout);
  504. if (uncompr) {
  505. file = gzdopen(fileno(stdin), "rb");
  506. if (file == NULL) error("can't gzdopen stdin");
  507. gz_uncompress(file, stdout);
  508. } else {
  509. file = gzdopen(fileno(stdout), outmode);
  510. if (file == NULL) error("can't gzdopen stdout");
  511. gz_compress(stdin, file);
  512. }
  513. } else {
  514. if (copyout) {
  515. SET_BINARY_MODE(stdout);
  516. }
  517. do {
  518. if (uncompr) {
  519. if (copyout) {
  520. file = gzopen(*argv, "rb");
  521. if (file == NULL)
  522. fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
  523. else
  524. gz_uncompress(file, stdout);
  525. } else {
  526. file_uncompress(*argv);
  527. }
  528. } else {
  529. if (copyout) {
  530. FILE * in = fopen(*argv, "rb");
  531. if (in == NULL) {
  532. perror(*argv);
  533. } else {
  534. file = gzdopen(fileno(stdout), outmode);
  535. if (file == NULL) error("can't gzdopen stdout");
  536. gz_compress(in, file);
  537. }
  538. } else {
  539. file_compress(*argv, outmode);
  540. }
  541. }
  542. } while (argv++, --argc);
  543. }
  544. return 0;
  545. }