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