compress.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 "file.h"
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #ifdef HAVE_UNISTD_H
  12. #include <unistd.h>
  13. #endif
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <sys/types.h>
  17. #ifdef HAVE_SYS_WAIT_H
  18. #include <sys/wait.h>
  19. #endif
  20. #ifdef HAVE_LIBZ
  21. #include <zlib.h>
  22. #endif
  23. #ifndef lint
  24. FILE_RCSID("@(#)$Id: compress.c,v 1.20 2001/07/22 21:04:15 christos Exp $")
  25. #endif
  26. static struct {
  27. const char *magic;
  28. int maglen;
  29. const char *const argv[3];
  30. int silent;
  31. } compr[] = {
  32. { "\037\235", 2, { "gzip", "-cdq", NULL }, 1 }, /* compressed */
  33. /* Uncompress can get stuck; so use gzip first if we have it
  34. * Idea from Damien Clark, thanks! */
  35. { "\037\235", 2, { "uncompress", "-c", NULL }, 1 }, /* compressed */
  36. { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */
  37. { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */
  38. { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */
  39. /* the standard pack utilities do not accept standard input */
  40. { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */
  41. { "BZh", 3, { "bzip2", "-cd", NULL }, 1 }, /* bzip2-ed */
  42. };
  43. static int ncompr = sizeof(compr) / sizeof(compr[0]);
  44. static int uncompressbuffer __P((int, const unsigned char *, unsigned char **, int));
  45. static int swrite __P((int, const void *, size_t));
  46. static int sread __P((int, void *, size_t));
  47. int
  48. zmagic(buf, nbytes)
  49. unsigned char *buf;
  50. int nbytes;
  51. {
  52. unsigned char *newbuf;
  53. int newsize;
  54. int i;
  55. for (i = 0; i < ncompr; i++) {
  56. if (nbytes < compr[i].maglen)
  57. continue;
  58. if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 &&
  59. (newsize = uncompressbuffer(i, buf, &newbuf, nbytes)) != 0) {
  60. tryit(newbuf, newsize, 1);
  61. free(newbuf);
  62. printf(" (");
  63. tryit(buf, nbytes, 0);
  64. printf(")");
  65. return 1;
  66. }
  67. }
  68. if (i == ncompr)
  69. return 0;
  70. return 1;
  71. }
  72. /*
  73. * `safe' write for sockets and pipes.
  74. */
  75. static int
  76. swrite(fd, buf, n)
  77. int fd;
  78. const void *buf;
  79. size_t n;
  80. {
  81. int rv;
  82. size_t rn = n;
  83. do
  84. switch (rv = write(fd, buf, n)) {
  85. case -1:
  86. if (errno == EINTR)
  87. continue;
  88. return -1;
  89. default:
  90. n -= rv;
  91. buf = ((char *)buf) + rv;
  92. break;
  93. }
  94. while (n > 0);
  95. return rn;
  96. }
  97. /*
  98. * `safe' read for sockets and pipes.
  99. */
  100. static int
  101. sread(fd, buf, n)
  102. int fd;
  103. void *buf;
  104. size_t n;
  105. {
  106. int rv, seen_eof=0;
  107. size_t rn = n;
  108. do
  109. switch (rv = read(fd, buf, n)) {
  110. case -1:
  111. if (errno == EINTR)
  112. continue;
  113. return -1;
  114. case 0:
  115. seen_eof=1;
  116. break;
  117. default:
  118. n -= rv;
  119. buf = ((char *)buf) + rv;
  120. break;
  121. }
  122. while (n > 0 && !seen_eof);
  123. return rn-n;
  124. }
  125. #ifdef HAVE_LIBZ
  126. #define FHCRC (1 << 1)
  127. #define FEXTRA (1 << 2)
  128. #define FNAME (1 << 3)
  129. #define FCOMMENT (1 << 4)
  130. static int uncompressgzipped(const unsigned char *old,unsigned char **newch,int n)
  131. {
  132. unsigned char flg = old[3];
  133. int data_start = 10;
  134. z_stream z;
  135. int rc;
  136. if(flg & FEXTRA)
  137. data_start+=2+old[data_start]+old[data_start+1]*256;
  138. if(flg & FNAME)
  139. {
  140. while(old[data_start])
  141. data_start++;
  142. data_start++;
  143. }
  144. if(flg & FCOMMENT)
  145. {
  146. while(old[data_start])
  147. data_start++;
  148. data_start++;
  149. }
  150. if(flg & FHCRC)
  151. data_start+=2;
  152. if ((*newch = (unsigned char *) malloc(HOWMANY+1)) == NULL) {
  153. return 0;
  154. }
  155. z.next_in=old+data_start;
  156. z.avail_in=n-data_start;
  157. z.next_out=*newch;
  158. z.avail_out=HOWMANY;
  159. z.zalloc=Z_NULL;
  160. z.zfree=Z_NULL;
  161. z.opaque=Z_NULL;
  162. rc=inflateInit2(&z,-15);
  163. if(rc!=Z_OK)
  164. {
  165. fprintf(stderr,"zlib: %s\n",z.msg);
  166. return 0;
  167. }
  168. rc=inflate(&z,Z_SYNC_FLUSH);
  169. if(rc!=Z_OK && rc!=Z_STREAM_END)
  170. {
  171. fprintf(stderr,"zlib: %s\n",z.msg);
  172. return 0;
  173. }
  174. n=z.total_out;
  175. inflateEnd(&z);
  176. /* let's keep the nul-terminate tradition */
  177. (*newch)[n++] = '\0';
  178. return n;
  179. }
  180. #endif
  181. static int
  182. uncompressbuffer(method, old, newch, n)
  183. int method;
  184. const unsigned char *old;
  185. unsigned char **newch;
  186. int n;
  187. {
  188. int fdin[2], fdout[2];
  189. /* The buffer is NUL terminated, and we don't need that.
  190. */
  191. n--;
  192. #ifdef HAVE_LIBZ
  193. // if(old[0]=='\037' && old[1]=='\213') {
  194. if(method==2) {
  195. return uncompressgzipped(old,newch,n);
  196. }
  197. #endif
  198. if (pipe(fdin) == -1 || pipe(fdout) == -1) {
  199. error("cannot create pipe (%m).\n");
  200. /*NOTREACHED*/
  201. }
  202. switch (fork()) {
  203. case 0: /* child */
  204. (void) close(0);
  205. (void) dup(fdin[0]);
  206. (void) close(fdin[0]);
  207. (void) close(fdin[1]);
  208. (void) close(1);
  209. (void) dup(fdout[1]);
  210. (void) close(fdout[0]);
  211. (void) close(fdout[1]);
  212. if (compr[method].silent)
  213. (void) close(2);
  214. execvp(compr[method].argv[0],
  215. (char *const *)compr[method].argv);
  216. exit(1);
  217. /*NOTREACHED*/
  218. case -1:
  219. error("could not fork (%s).\n", strerror(errno));
  220. /*NOTREACHED*/
  221. default: /* parent */
  222. (void) close(fdin[0]);
  223. (void) close(fdout[1]);
  224. if (swrite(fdin[1], old, n) != n) {
  225. n = 0;
  226. goto err;
  227. }
  228. (void) close(fdin[1]);
  229. fdin[1] = -1;
  230. if ((*newch = (unsigned char *) malloc(HOWMANY+1)) == NULL) {
  231. n = 0;
  232. goto err;
  233. }
  234. if ((n = sread(fdout[0], *newch, HOWMANY)) <= 0) {
  235. free(*newch);
  236. n = 0;
  237. goto err;
  238. }
  239. /* NUL terminate, as every buffer is handled here.
  240. */
  241. (*newch)[n++] = '\0';
  242. err:
  243. if (fdin[1] != -1)
  244. (void) close(fdin[1]);
  245. (void) close(fdout[0]);
  246. (void) wait(NULL);
  247. return n;
  248. }
  249. }