compress.c 2.9 KB

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