compress.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright (c) Ian F. Darwin 1986-1995.
  3. * Software written by Ian F. Darwin and others;
  4. * maintained 1995-present by Christos Zoulas and others.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice immediately at the beginning of the file, without modification,
  11. * this list of conditions, and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /*
  29. * compress routines:
  30. * zmagic() - returns 0 if not recognized, uncompresses and prints
  31. * information if recognized
  32. * uncompress(method, old, n, newch) - uncompress old into new,
  33. * using method, return sizeof new
  34. */
  35. #include "file.h"
  36. #include "magic.h"
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #ifdef HAVE_UNISTD_H
  40. #include <unistd.h>
  41. #endif
  42. #include <string.h>
  43. #include <errno.h>
  44. #include <sys/types.h>
  45. #ifdef HAVE_SYS_WAIT_H
  46. #include <sys/wait.h>
  47. #endif
  48. #ifdef HAVE_LIBZ
  49. #include <zlib.h>
  50. #endif
  51. #ifndef lint
  52. FILE_RCSID("@(#)$Id: compress.c,v 1.42 2005/03/06 05:58:22 christos Exp $")
  53. #endif
  54. private struct {
  55. const char *magic;
  56. size_t maglen;
  57. const char *const argv[3];
  58. int silent;
  59. } compr[] = {
  60. { "\037\235", 2, { "gzip", "-cdq", NULL }, 1 }, /* compressed */
  61. /* Uncompress can get stuck; so use gzip first if we have it
  62. * Idea from Damien Clark, thanks! */
  63. { "\037\235", 2, { "uncompress", "-c", NULL }, 1 }, /* compressed */
  64. { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */
  65. { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */
  66. { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */
  67. /* the standard pack utilities do not accept standard input */
  68. { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */
  69. { "PK\3\4", 4, { "gzip", "-cdq", NULL }, 1 }, /* pkzipped, */
  70. /* ...only first file examined */
  71. { "BZh", 3, { "bzip2", "-cd", NULL }, 1 }, /* bzip2-ed */
  72. };
  73. private int ncompr = sizeof(compr) / sizeof(compr[0]);
  74. private ssize_t swrite(int, const void *, size_t);
  75. private ssize_t sread(int, void *, size_t);
  76. private size_t uncompressbuf(struct magic_set *, int, size_t,
  77. const unsigned char *, unsigned char **, size_t);
  78. #ifdef HAVE_LIBZ
  79. private size_t uncompressgzipped(struct magic_set *, const unsigned char *,
  80. unsigned char **, size_t);
  81. #endif
  82. protected int
  83. file_zmagic(struct magic_set *ms, int fd, const unsigned char *buf,
  84. size_t nbytes)
  85. {
  86. unsigned char *newbuf = NULL;
  87. size_t i, nsz;
  88. int rv = 0;
  89. if ((ms->flags & MAGIC_COMPRESS) == 0)
  90. return 0;
  91. for (i = 0; i < ncompr; i++) {
  92. if (nbytes < compr[i].maglen)
  93. continue;
  94. if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 &&
  95. (nsz = uncompressbuf(ms, fd, i, buf, &newbuf,
  96. nbytes)) != 0) {
  97. ms->flags &= ~MAGIC_COMPRESS;
  98. rv = -1;
  99. if (file_buffer(ms, -1, newbuf, nsz) == -1)
  100. goto error;
  101. if (file_printf(ms, " (") == -1)
  102. goto error;
  103. if (file_buffer(ms, -1, buf, nbytes) == -1)
  104. goto error;
  105. if (file_printf(ms, ")") == -1)
  106. goto error;
  107. rv = 1;
  108. break;
  109. }
  110. }
  111. error:
  112. if (newbuf)
  113. free(newbuf);
  114. ms->flags |= MAGIC_COMPRESS;
  115. return rv;
  116. }
  117. /*
  118. * `safe' write for sockets and pipes.
  119. */
  120. private ssize_t
  121. swrite(int fd, const void *buf, size_t n)
  122. {
  123. int rv;
  124. size_t rn = n;
  125. do
  126. switch (rv = write(fd, buf, n)) {
  127. case -1:
  128. if (errno == EINTR)
  129. continue;
  130. return -1;
  131. default:
  132. n -= rv;
  133. buf = ((const char *)buf) + rv;
  134. break;
  135. }
  136. while (n > 0);
  137. return rn;
  138. }
  139. /*
  140. * `safe' read for sockets and pipes.
  141. */
  142. private ssize_t
  143. sread(int fd, void *buf, size_t n)
  144. {
  145. int rv;
  146. size_t rn = n;
  147. do
  148. switch (rv = read(fd, buf, n)) {
  149. case -1:
  150. if (errno == EINTR)
  151. continue;
  152. return -1;
  153. case 0:
  154. return rn - n;
  155. default:
  156. n -= rv;
  157. buf = ((char *)buf) + rv;
  158. break;
  159. }
  160. while (n > 0);
  161. return rn;
  162. }
  163. protected int
  164. file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
  165. size_t nbytes)
  166. {
  167. char buf[4096];
  168. int r, tfd;
  169. (void)strcpy(buf, "/tmp/file.XXXXXX");
  170. #ifndef HAVE_MKSTEMP
  171. {
  172. char *ptr = mktemp(buf);
  173. tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
  174. r = errno;
  175. (void)unlink(ptr);
  176. errno = r;
  177. }
  178. #else
  179. tfd = mkstemp(buf);
  180. r = errno;
  181. (void)unlink(buf);
  182. errno = r;
  183. #endif
  184. if (tfd == -1) {
  185. file_error(ms, errno,
  186. "cannot create temporary file for pipe copy");
  187. return -1;
  188. }
  189. if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
  190. r = 1;
  191. else {
  192. while ((r = sread(fd, buf, sizeof(buf))) > 0)
  193. if (swrite(tfd, buf, (size_t)r) != r)
  194. break;
  195. }
  196. switch (r) {
  197. case -1:
  198. file_error(ms, errno, "error copying from pipe to temp file");
  199. return -1;
  200. case 0:
  201. break;
  202. default:
  203. file_error(ms, errno, "error while writing to temp file");
  204. return -1;
  205. }
  206. /*
  207. * We duplicate the file descriptor, because fclose on a
  208. * tmpfile will delete the file, but any open descriptors
  209. * can still access the phantom inode.
  210. */
  211. if ((fd = dup2(tfd, fd)) == -1) {
  212. file_error(ms, errno, "could not dup descriptor for temp file");
  213. return -1;
  214. }
  215. (void)close(tfd);
  216. if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
  217. file_badseek(ms);
  218. return -1;
  219. }
  220. return fd;
  221. }
  222. #ifdef HAVE_LIBZ
  223. #define FHCRC (1 << 1)
  224. #define FEXTRA (1 << 2)
  225. #define FNAME (1 << 3)
  226. #define FCOMMENT (1 << 4)
  227. private size_t
  228. uncompressgzipped(struct magic_set *ms, const unsigned char *old,
  229. unsigned char **newch, size_t n)
  230. {
  231. unsigned char flg = old[3];
  232. size_t data_start = 10;
  233. z_stream z;
  234. int rc;
  235. if (flg & FEXTRA) {
  236. if (data_start+1 >= n)
  237. return 0;
  238. data_start += 2 + old[data_start] + old[data_start + 1] * 256;
  239. }
  240. if (flg & FNAME) {
  241. while(data_start < n && old[data_start])
  242. data_start++;
  243. data_start++;
  244. }
  245. if(flg & FCOMMENT) {
  246. while(data_start < n && old[data_start])
  247. data_start++;
  248. data_start++;
  249. }
  250. if(flg & FHCRC)
  251. data_start += 2;
  252. if (data_start >= n)
  253. return 0;
  254. if ((*newch = (unsigned char *)malloc(HOWMANY + 1)) == NULL) {
  255. return 0;
  256. }
  257. /* XXX: const castaway, via strchr */
  258. z.next_in = (Bytef *)strchr((const char *)old + data_start,
  259. old[data_start]);
  260. z.avail_in = n - data_start;
  261. z.next_out = *newch;
  262. z.avail_out = HOWMANY;
  263. z.zalloc = Z_NULL;
  264. z.zfree = Z_NULL;
  265. z.opaque = Z_NULL;
  266. rc = inflateInit2(&z, -15);
  267. if (rc != Z_OK) {
  268. file_error(ms, 0, "zlib: %s", z.msg);
  269. return 0;
  270. }
  271. rc = inflate(&z, Z_SYNC_FLUSH);
  272. if (rc != Z_OK && rc != Z_STREAM_END) {
  273. file_error(ms, 0, "zlib: %s", z.msg);
  274. return 0;
  275. }
  276. n = (size_t)z.total_out;
  277. inflateEnd(&z);
  278. /* let's keep the nul-terminate tradition */
  279. (*newch)[n++] = '\0';
  280. return n;
  281. }
  282. #endif
  283. private size_t
  284. uncompressbuf(struct magic_set *ms, int fd, size_t method,
  285. const unsigned char *old, unsigned char **newch, size_t n)
  286. {
  287. int fdin[2], fdout[2];
  288. int r;
  289. #ifdef HAVE_LIBZ
  290. if (method == 2)
  291. return uncompressgzipped(ms, old, newch, n);
  292. #endif
  293. (void)fflush(stdout);
  294. (void)fflush(stderr);
  295. if ((fd != -1 && pipe(fdin) == -1) || pipe(fdout) == -1) {
  296. file_error(ms, errno, "cannot create pipe");
  297. return 0;
  298. }
  299. switch (fork()) {
  300. case 0: /* child */
  301. (void) close(0);
  302. if (fd != -1) {
  303. (void) dup(fd);
  304. (void) lseek(0, (off_t)0, SEEK_SET);
  305. } else {
  306. (void) dup(fdin[0]);
  307. (void) close(fdin[0]);
  308. (void) close(fdin[1]);
  309. }
  310. (void) close(1);
  311. (void) dup(fdout[1]);
  312. (void) close(fdout[0]);
  313. (void) close(fdout[1]);
  314. #ifndef DEBUG
  315. if (compr[method].silent)
  316. (void)close(2);
  317. #endif
  318. execvp(compr[method].argv[0],
  319. (char *const *)(intptr_t)compr[method].argv);
  320. #ifdef DEBUG
  321. (void)fprintf(stderr, "exec `%s' failed (%s)\n",
  322. compr[method].argv[0], strerror(errno));
  323. #endif
  324. exit(1);
  325. /*NOTREACHED*/
  326. case -1:
  327. file_error(ms, errno, "could not fork");
  328. return 0;
  329. default: /* parent */
  330. (void) close(fdout[1]);
  331. if (fd == -1) {
  332. (void) close(fdin[0]);
  333. /*
  334. * fork again, to avoid blocking because both
  335. * pipes filled
  336. */
  337. switch (fork()) {
  338. case 0: /* child */
  339. (void)close(fdout[0]);
  340. if (swrite(fdin[1], old, n) != n) {
  341. #ifdef DEBUG
  342. (void)fprintf(stderr,
  343. "Write failed (%s)\n",
  344. strerror(errno));
  345. #endif
  346. exit(1);
  347. }
  348. exit(0);
  349. /*NOTREACHED*/
  350. case -1:
  351. #ifdef DEBUG
  352. (void)fprintf(stderr, "Fork failed (%s)\n",
  353. strerror(errno));
  354. #endif
  355. exit(1);
  356. /*NOTREACHED*/
  357. default: /* parent */
  358. break;
  359. }
  360. (void) close(fdin[1]);
  361. fdin[1] = -1;
  362. }
  363. if ((*newch = (unsigned char *) malloc(HOWMANY + 1)) == NULL) {
  364. #ifdef DEBUG
  365. (void)fprintf(stderr, "Malloc failed (%s)\n",
  366. strerror(errno));
  367. #endif
  368. n = 0;
  369. goto err;
  370. }
  371. if ((r = sread(fdout[0], *newch, HOWMANY)) <= 0) {
  372. #ifdef DEBUG
  373. (void)fprintf(stderr, "Read failed (%s)\n",
  374. strerror(errno));
  375. #endif
  376. free(*newch);
  377. n = 0;
  378. newch[0] = '\0';
  379. goto err;
  380. } else {
  381. n = r;
  382. }
  383. /* NUL terminate, as every buffer is handled here. */
  384. (*newch)[n++] = '\0';
  385. err:
  386. if (fdin[1] != -1)
  387. (void) close(fdin[1]);
  388. (void) close(fdout[0]);
  389. #ifdef WNOHANG
  390. while (waitpid(-1, NULL, WNOHANG) != -1)
  391. continue;
  392. #else
  393. (void)wait(NULL);
  394. #endif
  395. return n;
  396. }
  397. }