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