minigzip.c 15 KB

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