compress.c 2.8 KB

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