compress.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * compress routines:
  3. * zmagic() - returns 0 if not recognized, uncompresses and prints
  4. * information if recognized
  5. * uncompress(method, old, n, newch) - uncompress old into new,
  6. * using method, return sizeof new
  7. */
  8. #include "file.h"
  9. #ifdef __CYGWIN__
  10. #include <errno.h>
  11. #endif
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #ifdef HAVE_UNISTD_H
  15. #include <unistd.h>
  16. #endif
  17. #include <string.h>
  18. #ifdef HAVE_SYS_WAIT_H
  19. #include <sys/wait.h>
  20. #endif
  21. #ifndef lint
  22. FILE_RCSID("@(#)$Id: compress.c,v 1.14 1999/10/31 22:23:03 christos Exp $")
  23. #endif
  24. static struct {
  25. const char *magic;
  26. int maglen;
  27. const char *const argv[3];
  28. int silent;
  29. } compr[] = {
  30. { "\037\235", 2, { "uncompress", "-c", NULL }, 0 }, /* compressed */
  31. { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */
  32. { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */
  33. { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */
  34. /* the standard pack utilities do not accept standard input */
  35. { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */
  36. };
  37. static int ncompr = sizeof(compr) / sizeof(compr[0]);
  38. static int uncompress __P((int, const unsigned char *, unsigned char **, int));
  39. int
  40. zmagic(buf, nbytes)
  41. unsigned char *buf;
  42. int nbytes;
  43. {
  44. unsigned char *newbuf;
  45. int newsize;
  46. int i;
  47. for (i = 0; i < ncompr; i++) {
  48. if (nbytes < compr[i].maglen)
  49. continue;
  50. if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0)
  51. break;
  52. }
  53. if (i == ncompr)
  54. return 0;
  55. if ((newsize = uncompress(i, buf, &newbuf, nbytes)) != 0) {
  56. tryit(newbuf, newsize, 1);
  57. free(newbuf);
  58. printf(" (");
  59. tryit(buf, nbytes, 0);
  60. printf(")");
  61. }
  62. return 1;
  63. }
  64. static int
  65. uncompress(method, old, newch, n)
  66. int method;
  67. const unsigned char *old;
  68. unsigned char **newch;
  69. int n;
  70. {
  71. int fdin[2], fdout[2];
  72. if (pipe(fdin) == -1 || pipe(fdout) == -1) {
  73. error("cannot create pipe (%s).\n", strerror(errno));
  74. /*NOTREACHED*/
  75. }
  76. switch (fork()) {
  77. case 0: /* child */
  78. (void) close(0);
  79. (void) dup(fdin[0]);
  80. (void) close(fdin[0]);
  81. (void) close(fdin[1]);
  82. (void) close(1);
  83. (void) dup(fdout[1]);
  84. (void) close(fdout[0]);
  85. (void) close(fdout[1]);
  86. if (compr[method].silent)
  87. (void) close(2);
  88. execvp(compr[method].argv[0],
  89. (char *const *)compr[method].argv);
  90. error("could not execute `%s' (%s).\n",
  91. compr[method].argv[0], strerror(errno));
  92. /*NOTREACHED*/
  93. case -1:
  94. error("could not fork (%s).\n", strerror(errno));
  95. /*NOTREACHED*/
  96. default: /* parent */
  97. (void) close(fdin[0]);
  98. (void) close(fdout[1]);
  99. if (write(fdin[1], old, n) != n) {
  100. error("write failed (%s).\n", strerror(errno));
  101. /*NOTREACHED*/
  102. }
  103. (void) close(fdin[1]);
  104. if ((*newch = (unsigned char *) malloc(n)) == NULL) {
  105. error("out of memory.\n");
  106. /*NOTREACHED*/
  107. }
  108. if ((n = read(fdout[0], *newch, n)) <= 0) {
  109. free(*newch);
  110. error("read failed (%s).\n", strerror(errno));
  111. /*NOTREACHED*/
  112. }
  113. (void) close(fdout[0]);
  114. (void) wait(NULL);
  115. return n;
  116. }
  117. }