compress.c 2.9 KB

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