compress.c 2.8 KB

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