minigzip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /* minigzip.c -- simulate gzip using the zlib compression library
  2. * Copyright (C) 1995-2006, 2010, 2011, 2016 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. #if defined(_MSC_VER) && _MSC_VER < 1900
  37. # define snprintf _snprintf
  38. #endif
  39. #ifdef VMS
  40. # define unlink delete
  41. # define GZ_SUFFIX "-gz"
  42. #endif
  43. #ifdef RISCOS
  44. # define unlink remove
  45. # define GZ_SUFFIX "-gz"
  46. # define fileno(file) file->__file
  47. #endif
  48. #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
  49. # include <unix.h> /* for fileno */
  50. #endif
  51. #if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE)
  52. #ifndef WIN32 /* unlink already in stdio.h for WIN32 */
  53. extern int unlink(const char *);
  54. #endif
  55. #endif
  56. #if defined(UNDER_CE)
  57. # include <windows.h>
  58. # define perror(s) pwinerror(s)
  59. /* Map the Windows error number in ERROR to a locale-dependent error
  60. message string and return a pointer to it. Typically, the values
  61. for ERROR come from GetLastError.
  62. The string pointed to shall not be modified by the application,
  63. but may be overwritten by a subsequent call to strwinerror
  64. The strwinerror function does not change the current setting
  65. of GetLastError. */
  66. static char *strwinerror (error)
  67. DWORD error;
  68. {
  69. static char buf[1024];
  70. wchar_t *msgbuf;
  71. DWORD lasterr = GetLastError();
  72. DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
  73. | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  74. NULL,
  75. error,
  76. 0, /* Default language */
  77. (LPVOID)&msgbuf,
  78. 0,
  79. NULL);
  80. if (chars != 0) {
  81. /* If there is an \r\n appended, zap it. */
  82. if (chars >= 2
  83. && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
  84. chars -= 2;
  85. msgbuf[chars] = 0;
  86. }
  87. if (chars > sizeof (buf) - 1) {
  88. chars = sizeof (buf) - 1;
  89. msgbuf[chars] = 0;
  90. }
  91. wcstombs(buf, msgbuf, chars + 1);
  92. LocalFree(msgbuf);
  93. }
  94. else {
  95. sprintf(buf, "unknown win32 error (%ld)", error);
  96. }
  97. SetLastError(lasterr);
  98. return buf;
  99. }
  100. static void pwinerror (s)
  101. const char *s;
  102. {
  103. if (s && *s)
  104. fprintf(stderr, "%s: %s\n", s, strwinerror(GetLastError ()));
  105. else
  106. fprintf(stderr, "%s\n", strwinerror(GetLastError ()));
  107. }
  108. #endif /* UNDER_CE */
  109. #ifndef GZ_SUFFIX
  110. # define GZ_SUFFIX ".gz"
  111. #endif
  112. #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
  113. #define BUFLEN 16384
  114. #define MAX_NAME_LEN 1024
  115. #ifdef MAXSEG_64K
  116. # define local static
  117. /* Needed for systems with limitation on stack size. */
  118. #else
  119. # define local
  120. #endif
  121. #ifdef Z_SOLO
  122. /* for Z_SOLO, create simplified gz* functions using deflate and inflate */
  123. #if defined(Z_HAVE_UNISTD_H) || defined(Z_LARGE)
  124. # include <unistd.h> /* for unlink() */
  125. #endif
  126. static void *myalloc(void *q, unsigned n, unsigned m) {
  127. (void)q;
  128. return calloc(n, m);
  129. }
  130. static void myfree(void *q, void *p) {
  131. (void)q;
  132. free(p);
  133. }
  134. typedef struct gzFile_s {
  135. FILE *file;
  136. int write;
  137. int err;
  138. char *msg;
  139. z_stream strm;
  140. } *gzFile;
  141. static gzFile gz_open(const char *path, int fd, const char *mode) {
  142. gzFile gz;
  143. int ret;
  144. gz = malloc(sizeof(struct gzFile_s));
  145. if (gz == NULL)
  146. return NULL;
  147. gz->write = strchr(mode, 'w') != NULL;
  148. gz->strm.zalloc = myalloc;
  149. gz->strm.zfree = myfree;
  150. gz->strm.opaque = Z_NULL;
  151. if (gz->write)
  152. ret = deflateInit2(&(gz->strm), -1, 8, 15 + 16, 8, 0);
  153. else {
  154. gz->strm.next_in = 0;
  155. gz->strm.avail_in = Z_NULL;
  156. ret = inflateInit2(&(gz->strm), 15 + 16);
  157. }
  158. if (ret != Z_OK) {
  159. free(gz);
  160. return NULL;
  161. }
  162. gz->file = path == NULL ? fdopen(fd, gz->write ? "wb" : "rb") :
  163. fopen(path, gz->write ? "wb" : "rb");
  164. if (gz->file == NULL) {
  165. gz->write ? deflateEnd(&(gz->strm)) : inflateEnd(&(gz->strm));
  166. free(gz);
  167. return NULL;
  168. }
  169. gz->err = 0;
  170. gz->msg = "";
  171. return gz;
  172. }
  173. static gzFile gzopen(const char *path, const char *mode) {
  174. return gz_open(path, -1, mode);
  175. }
  176. static gzFile gzdopen(int fd, const char *mode) {
  177. return gz_open(NULL, fd, mode);
  178. }
  179. static int gzwrite(gzFile gz, const void *buf, unsigned len) {
  180. z_stream *strm;
  181. unsigned char out[BUFLEN];
  182. if (gz == NULL || !gz->write)
  183. return 0;
  184. strm = &(gz->strm);
  185. strm->next_in = (void *)buf;
  186. strm->avail_in = len;
  187. do {
  188. strm->next_out = out;
  189. strm->avail_out = BUFLEN;
  190. (void)deflate(strm, Z_NO_FLUSH);
  191. fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
  192. } while (strm->avail_out == 0);
  193. return len;
  194. }
  195. static int gzread(gzFile gz, void *buf, unsigned len) {
  196. int ret;
  197. unsigned got;
  198. unsigned char in[1];
  199. z_stream *strm;
  200. if (gz == NULL || gz->write)
  201. return 0;
  202. if (gz->err)
  203. return 0;
  204. strm = &(gz->strm);
  205. strm->next_out = (void *)buf;
  206. strm->avail_out = len;
  207. do {
  208. got = fread(in, 1, 1, gz->file);
  209. if (got == 0)
  210. break;
  211. strm->next_in = in;
  212. strm->avail_in = 1;
  213. ret = inflate(strm, Z_NO_FLUSH);
  214. if (ret == Z_DATA_ERROR) {
  215. gz->err = Z_DATA_ERROR;
  216. gz->msg = strm->msg;
  217. return 0;
  218. }
  219. if (ret == Z_STREAM_END)
  220. inflateReset(strm);
  221. } while (strm->avail_out);
  222. return len - strm->avail_out;
  223. }
  224. static int gzclose(gzFile gz) {
  225. z_stream *strm;
  226. unsigned char out[BUFLEN];
  227. if (gz == NULL)
  228. return Z_STREAM_ERROR;
  229. strm = &(gz->strm);
  230. if (gz->write) {
  231. strm->next_in = Z_NULL;
  232. strm->avail_in = 0;
  233. do {
  234. strm->next_out = out;
  235. strm->avail_out = BUFLEN;
  236. (void)deflate(strm, Z_FINISH);
  237. fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
  238. } while (strm->avail_out == 0);
  239. deflateEnd(strm);
  240. }
  241. else
  242. inflateEnd(strm);
  243. fclose(gz->file);
  244. free(gz);
  245. return Z_OK;
  246. }
  247. static const char *gzerror(gzFile gz, int *err) {
  248. *err = gz->err;
  249. return gz->msg;
  250. }
  251. #endif
  252. static char *prog;
  253. /* ===========================================================================
  254. * Display error message and exit
  255. */
  256. static void error(const char *msg) {
  257. fprintf(stderr, "%s: %s\n", prog, msg);
  258. exit(1);
  259. }
  260. #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <[email protected]> */
  261. /* Try compressing the input file at once using mmap. Return Z_OK if
  262. * success, Z_ERRNO otherwise.
  263. */
  264. static int gz_compress_mmap(FILE *in, gzFile out) {
  265. int len;
  266. int err;
  267. int ifd = fileno(in);
  268. caddr_t buf; /* mmap'ed buffer for the entire input file */
  269. off_t buf_len; /* length of the input file */
  270. struct stat sb;
  271. /* Determine the size of the file, needed for mmap: */
  272. if (fstat(ifd, &sb) < 0) return Z_ERRNO;
  273. buf_len = sb.st_size;
  274. if (buf_len <= 0) return Z_ERRNO;
  275. /* Now do the actual mmap: */
  276. buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
  277. if (buf == (caddr_t)(-1)) return Z_ERRNO;
  278. /* Compress the whole file at once: */
  279. len = gzwrite(out, (char *)buf, (unsigned)buf_len);
  280. if (len != (int)buf_len) error(gzerror(out, &err));
  281. munmap(buf, buf_len);
  282. fclose(in);
  283. if (gzclose(out) != Z_OK) error("failed gzclose");
  284. return Z_OK;
  285. }
  286. #endif /* USE_MMAP */
  287. /* ===========================================================================
  288. * Compress input to output then close both files.
  289. */
  290. static void gz_compress(FILE *in, gzFile out) {
  291. local char buf[BUFLEN];
  292. int len;
  293. int err;
  294. #ifdef USE_MMAP
  295. /* Try first compressing with mmap. If mmap fails (minigzip used in a
  296. * pipe), use the normal fread loop.
  297. */
  298. if (gz_compress_mmap(in, out) == Z_OK) return;
  299. #endif
  300. for (;;) {
  301. len = (int)fread(buf, 1, sizeof(buf), in);
  302. if (ferror(in)) {
  303. perror("fread");
  304. exit(1);
  305. }
  306. if (len == 0) break;
  307. if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
  308. }
  309. fclose(in);
  310. if (gzclose(out) != Z_OK) error("failed gzclose");
  311. }
  312. /* ===========================================================================
  313. * Uncompress input to output then close both files.
  314. */
  315. static void gz_uncompress(gzFile in, FILE *out) {
  316. local char buf[BUFLEN];
  317. int len;
  318. int err;
  319. for (;;) {
  320. len = gzread(in, buf, sizeof(buf));
  321. if (len < 0) error (gzerror(in, &err));
  322. if (len == 0) break;
  323. if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
  324. error("failed fwrite");
  325. }
  326. }
  327. if (fclose(out)) error("failed fclose");
  328. if (gzclose(in) != Z_OK) error("failed gzclose");
  329. }
  330. /* ===========================================================================
  331. * Compress the given file: create a corresponding .gz file and remove the
  332. * original.
  333. */
  334. static void file_compress(char *file, char *mode) {
  335. local char outfile[MAX_NAME_LEN];
  336. FILE *in;
  337. gzFile out;
  338. if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
  339. fprintf(stderr, "%s: filename too long\n", prog);
  340. exit(1);
  341. }
  342. #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
  343. snprintf(outfile, sizeof(outfile), "%s%s", file, GZ_SUFFIX);
  344. #else
  345. strcpy(outfile, file);
  346. strcat(outfile, GZ_SUFFIX);
  347. #endif
  348. in = fopen(file, "rb");
  349. if (in == NULL) {
  350. perror(file);
  351. exit(1);
  352. }
  353. out = gzopen(outfile, mode);
  354. if (out == NULL) {
  355. fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
  356. exit(1);
  357. }
  358. gz_compress(in, out);
  359. unlink(file);
  360. }
  361. /* ===========================================================================
  362. * Uncompress the given file and remove the original.
  363. */
  364. static void file_uncompress(char *file) {
  365. local char buf[MAX_NAME_LEN];
  366. char *infile, *outfile;
  367. FILE *out;
  368. gzFile in;
  369. z_size_t len = strlen(file);
  370. if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
  371. fprintf(stderr, "%s: filename too long\n", prog);
  372. exit(1);
  373. }
  374. #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
  375. snprintf(buf, sizeof(buf), "%s", file);
  376. #else
  377. strcpy(buf, file);
  378. #endif
  379. if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
  380. infile = file;
  381. outfile = buf;
  382. outfile[len-3] = '\0';
  383. } else {
  384. outfile = file;
  385. infile = buf;
  386. #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
  387. snprintf(buf + len, sizeof(buf) - len, "%s", GZ_SUFFIX);
  388. #else
  389. strcat(infile, GZ_SUFFIX);
  390. #endif
  391. }
  392. in = gzopen(infile, "rb");
  393. if (in == NULL) {
  394. fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
  395. exit(1);
  396. }
  397. out = fopen(outfile, "wb");
  398. if (out == NULL) {
  399. perror(file);
  400. exit(1);
  401. }
  402. gz_uncompress(in, out);
  403. unlink(infile);
  404. }
  405. /* ===========================================================================
  406. * Usage: minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...]
  407. * -c : write to standard output
  408. * -d : decompress
  409. * -f : compress with Z_FILTERED
  410. * -h : compress with Z_HUFFMAN_ONLY
  411. * -r : compress with Z_RLE
  412. * -1 to -9 : compression level
  413. */
  414. int main(int argc, char *argv[]) {
  415. int copyout = 0;
  416. int uncompr = 0;
  417. gzFile file;
  418. char *bname, outmode[20];
  419. #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
  420. snprintf(outmode, sizeof(outmode), "%s", "wb6 ");
  421. #else
  422. strcpy(outmode, "wb6 ");
  423. #endif
  424. prog = argv[0];
  425. bname = strrchr(argv[0], '/');
  426. if (bname)
  427. bname++;
  428. else
  429. bname = argv[0];
  430. argc--, argv++;
  431. if (!strcmp(bname, "gunzip"))
  432. uncompr = 1;
  433. else if (!strcmp(bname, "zcat"))
  434. copyout = uncompr = 1;
  435. while (argc > 0) {
  436. if (strcmp(*argv, "-c") == 0)
  437. copyout = 1;
  438. else if (strcmp(*argv, "-d") == 0)
  439. uncompr = 1;
  440. else if (strcmp(*argv, "-f") == 0)
  441. outmode[3] = 'f';
  442. else if (strcmp(*argv, "-h") == 0)
  443. outmode[3] = 'h';
  444. else if (strcmp(*argv, "-r") == 0)
  445. outmode[3] = 'R';
  446. else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
  447. (*argv)[2] == 0)
  448. outmode[2] = (*argv)[1];
  449. else
  450. break;
  451. argc--, argv++;
  452. }
  453. if (outmode[3] == ' ')
  454. outmode[3] = 0;
  455. if (argc == 0) {
  456. SET_BINARY_MODE(stdin);
  457. SET_BINARY_MODE(stdout);
  458. if (uncompr) {
  459. file = gzdopen(fileno(stdin), "rb");
  460. if (file == NULL) error("can't gzdopen stdin");
  461. gz_uncompress(file, stdout);
  462. } else {
  463. file = gzdopen(fileno(stdout), outmode);
  464. if (file == NULL) error("can't gzdopen stdout");
  465. gz_compress(stdin, file);
  466. }
  467. } else {
  468. if (copyout) {
  469. SET_BINARY_MODE(stdout);
  470. }
  471. do {
  472. if (uncompr) {
  473. if (copyout) {
  474. file = gzopen(*argv, "rb");
  475. if (file == NULL)
  476. fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
  477. else
  478. gz_uncompress(file, stdout);
  479. } else {
  480. file_uncompress(*argv);
  481. }
  482. } else {
  483. if (copyout) {
  484. FILE * in = fopen(*argv, "rb");
  485. if (in == NULL) {
  486. perror(*argv);
  487. } else {
  488. file = gzdopen(fileno(stdout), outmode);
  489. if (file == NULL) error("can't gzdopen stdout");
  490. gz_compress(in, file);
  491. }
  492. } else {
  493. file_compress(*argv, outmode);
  494. }
  495. }
  496. } while (argv++, --argc);
  497. }
  498. return 0;
  499. }