compress.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. #include <sys/ioctl.h>
  46. #ifdef HAVE_SYS_WAIT_H
  47. #include <sys/wait.h>
  48. #endif
  49. #ifdef HAVE_LIBZ
  50. #include <zlib.h>
  51. #endif
  52. #ifndef lint
  53. FILE_RCSID("@(#)$Id: compress.c,v 1.45 2006/10/31 19:37:17 christos Exp $")
  54. #endif
  55. private struct {
  56. const char *magic;
  57. size_t maglen;
  58. const char *const argv[3];
  59. int silent;
  60. } compr[] = {
  61. { "\037\235", 2, { "gzip", "-cdq", NULL }, 1 }, /* compressed */
  62. /* Uncompress can get stuck; so use gzip first if we have it
  63. * Idea from Damien Clark, thanks! */
  64. { "\037\235", 2, { "uncompress", "-c", NULL }, 1 }, /* compressed */
  65. { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */
  66. { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */
  67. { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */
  68. /* the standard pack utilities do not accept standard input */
  69. { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */
  70. { "PK\3\4", 4, { "gzip", "-cdq", NULL }, 1 }, /* pkzipped, */
  71. /* ...only first file examined */
  72. { "BZh", 3, { "bzip2", "-cd", NULL }, 1 }, /* bzip2-ed */
  73. };
  74. private int ncompr = sizeof(compr) / sizeof(compr[0]);
  75. #define NODATA ((size_t)~0)
  76. private ssize_t swrite(int, const void *, size_t);
  77. private size_t uncompressbuf(struct magic_set *, int, size_t,
  78. const unsigned char *, unsigned char **, size_t);
  79. #ifdef HAVE_LIBZ
  80. private size_t uncompressgzipped(struct magic_set *, const unsigned char *,
  81. unsigned char **, size_t);
  82. #endif
  83. protected int
  84. file_zmagic(struct magic_set *ms, int fd, const unsigned char *buf,
  85. size_t nbytes)
  86. {
  87. unsigned char *newbuf = NULL;
  88. size_t i, nsz;
  89. int rv = 0;
  90. if ((ms->flags & MAGIC_COMPRESS) == 0)
  91. return 0;
  92. for (i = 0; i < ncompr; i++) {
  93. if (nbytes < compr[i].maglen)
  94. continue;
  95. if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 &&
  96. (nsz = uncompressbuf(ms, fd, i, buf, &newbuf,
  97. nbytes)) != NODATA) {
  98. ms->flags &= ~MAGIC_COMPRESS;
  99. rv = -1;
  100. if (file_buffer(ms, -1, newbuf, nsz) == -1)
  101. goto error;
  102. if (file_printf(ms, " (") == -1)
  103. goto error;
  104. if (file_buffer(ms, -1, buf, nbytes) == -1)
  105. goto error;
  106. if (file_printf(ms, ")") == -1)
  107. goto error;
  108. rv = 1;
  109. break;
  110. }
  111. }
  112. error:
  113. if (newbuf)
  114. free(newbuf);
  115. ms->flags |= MAGIC_COMPRESS;
  116. return rv;
  117. }
  118. /*
  119. * `safe' write for sockets and pipes.
  120. */
  121. private ssize_t
  122. swrite(int fd, const void *buf, size_t n)
  123. {
  124. int rv;
  125. size_t rn = n;
  126. do
  127. switch (rv = write(fd, buf, n)) {
  128. case -1:
  129. if (errno == EINTR)
  130. continue;
  131. return -1;
  132. default:
  133. n -= rv;
  134. buf = ((const char *)buf) + rv;
  135. break;
  136. }
  137. while (n > 0);
  138. return rn;
  139. }
  140. /*
  141. * `safe' read for sockets and pipes.
  142. */
  143. protected ssize_t
  144. sread(int fd, void *buf, size_t n)
  145. {
  146. int rv;
  147. #ifdef FIONREAD
  148. int t = 0;
  149. #endif
  150. size_t rn = n;
  151. if (fd == STDIN_FILENO)
  152. goto nocheck;
  153. #ifdef FIONREAD
  154. if ((ioctl(fd, FIONREAD, &t) < 0) || (t == 0)) {
  155. #ifdef FD_ZERO
  156. for (;;) {
  157. fd_set check;
  158. struct timeval tout = {0, 100 * 1000};
  159. FD_ZERO(&check);
  160. FD_SET(fd, &check);
  161. /*
  162. * Avoid soft deadlock: do not read if there
  163. * is nothing to read from sockets and pipes.
  164. */
  165. if (select(fd + 1, &check, NULL, NULL, &tout) <= 0) {
  166. if (errno == EINTR || errno == EAGAIN)
  167. continue;
  168. return 0;
  169. }
  170. break;
  171. }
  172. #endif
  173. (void)ioctl(fd, FIONREAD, &t);
  174. }
  175. if (t > 0 && (size_t)t < n) {
  176. n = t;
  177. rn = n;
  178. }
  179. #endif
  180. nocheck:
  181. do
  182. switch ((rv = read(fd, buf, n))) {
  183. case -1:
  184. if (errno == EINTR)
  185. continue;
  186. return -1;
  187. case 0:
  188. return rn - n;
  189. default:
  190. n -= rv;
  191. buf = ((char *)buf) + rv;
  192. break;
  193. }
  194. while (n > 0);
  195. return rn;
  196. }
  197. protected int
  198. file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
  199. size_t nbytes)
  200. {
  201. char buf[4096];
  202. int r, tfd;
  203. (void)strcpy(buf, "/tmp/file.XXXXXX");
  204. #ifndef HAVE_MKSTEMP
  205. {
  206. char *ptr = mktemp(buf);
  207. tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
  208. r = errno;
  209. (void)unlink(ptr);
  210. errno = r;
  211. }
  212. #else
  213. tfd = mkstemp(buf);
  214. r = errno;
  215. (void)unlink(buf);
  216. errno = r;
  217. #endif
  218. if (tfd == -1) {
  219. file_error(ms, errno,
  220. "cannot create temporary file for pipe copy");
  221. return -1;
  222. }
  223. if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
  224. r = 1;
  225. else {
  226. while ((r = sread(fd, buf, sizeof(buf))) > 0)
  227. if (swrite(tfd, buf, (size_t)r) != r)
  228. break;
  229. }
  230. switch (r) {
  231. case -1:
  232. file_error(ms, errno, "error copying from pipe to temp file");
  233. return -1;
  234. case 0:
  235. break;
  236. default:
  237. file_error(ms, errno, "error while writing to temp file");
  238. return -1;
  239. }
  240. /*
  241. * We duplicate the file descriptor, because fclose on a
  242. * tmpfile will delete the file, but any open descriptors
  243. * can still access the phantom inode.
  244. */
  245. if ((fd = dup2(tfd, fd)) == -1) {
  246. file_error(ms, errno, "could not dup descriptor for temp file");
  247. return -1;
  248. }
  249. (void)close(tfd);
  250. if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
  251. file_badseek(ms);
  252. return -1;
  253. }
  254. return fd;
  255. }
  256. #ifdef HAVE_LIBZ
  257. #define FHCRC (1 << 1)
  258. #define FEXTRA (1 << 2)
  259. #define FNAME (1 << 3)
  260. #define FCOMMENT (1 << 4)
  261. private size_t
  262. uncompressgzipped(struct magic_set *ms, const unsigned char *old,
  263. unsigned char **newch, size_t n)
  264. {
  265. unsigned char flg = old[3];
  266. size_t data_start = 10;
  267. z_stream z;
  268. int rc;
  269. if (flg & FEXTRA) {
  270. if (data_start+1 >= n)
  271. return 0;
  272. data_start += 2 + old[data_start] + old[data_start + 1] * 256;
  273. }
  274. if (flg & FNAME) {
  275. while(data_start < n && old[data_start])
  276. data_start++;
  277. data_start++;
  278. }
  279. if(flg & FCOMMENT) {
  280. while(data_start < n && old[data_start])
  281. data_start++;
  282. data_start++;
  283. }
  284. if(flg & FHCRC)
  285. data_start += 2;
  286. if (data_start >= n)
  287. return 0;
  288. if ((*newch = (unsigned char *)malloc(HOWMANY + 1)) == NULL) {
  289. return 0;
  290. }
  291. /* XXX: const castaway, via strchr */
  292. z.next_in = (Bytef *)strchr((const char *)old + data_start,
  293. old[data_start]);
  294. z.avail_in = n - data_start;
  295. z.next_out = *newch;
  296. z.avail_out = HOWMANY;
  297. z.zalloc = Z_NULL;
  298. z.zfree = Z_NULL;
  299. z.opaque = Z_NULL;
  300. rc = inflateInit2(&z, -15);
  301. if (rc != Z_OK) {
  302. file_error(ms, 0, "zlib: %s", z.msg);
  303. return 0;
  304. }
  305. rc = inflate(&z, Z_SYNC_FLUSH);
  306. if (rc != Z_OK && rc != Z_STREAM_END) {
  307. file_error(ms, 0, "zlib: %s", z.msg);
  308. return 0;
  309. }
  310. n = (size_t)z.total_out;
  311. inflateEnd(&z);
  312. /* let's keep the nul-terminate tradition */
  313. (*newch)[n] = '\0';
  314. return n;
  315. }
  316. #endif
  317. private size_t
  318. uncompressbuf(struct magic_set *ms, int fd, size_t method,
  319. const unsigned char *old, unsigned char **newch, size_t n)
  320. {
  321. int fdin[2], fdout[2];
  322. int r;
  323. #ifdef HAVE_LIBZ
  324. if (method == 2)
  325. return uncompressgzipped(ms, old, newch, n);
  326. #endif
  327. (void)fflush(stdout);
  328. (void)fflush(stderr);
  329. if ((fd != -1 && pipe(fdin) == -1) || pipe(fdout) == -1) {
  330. file_error(ms, errno, "cannot create pipe");
  331. return NODATA;
  332. }
  333. switch (fork()) {
  334. case 0: /* child */
  335. (void) close(0);
  336. if (fd != -1) {
  337. (void) dup(fd);
  338. (void) lseek(0, (off_t)0, SEEK_SET);
  339. } else {
  340. (void) dup(fdin[0]);
  341. (void) close(fdin[0]);
  342. (void) close(fdin[1]);
  343. }
  344. (void) close(1);
  345. (void) dup(fdout[1]);
  346. (void) close(fdout[0]);
  347. (void) close(fdout[1]);
  348. #ifndef DEBUG
  349. if (compr[method].silent)
  350. (void)close(2);
  351. #endif
  352. execvp(compr[method].argv[0],
  353. (char *const *)(intptr_t)compr[method].argv);
  354. #ifdef DEBUG
  355. (void)fprintf(stderr, "exec `%s' failed (%s)\n",
  356. compr[method].argv[0], strerror(errno));
  357. #endif
  358. exit(1);
  359. /*NOTREACHED*/
  360. case -1:
  361. file_error(ms, errno, "could not fork");
  362. return NODATA;
  363. default: /* parent */
  364. (void) close(fdout[1]);
  365. if (fd == -1) {
  366. (void) close(fdin[0]);
  367. /*
  368. * fork again, to avoid blocking because both
  369. * pipes filled
  370. */
  371. switch (fork()) {
  372. case 0: /* child */
  373. (void)close(fdout[0]);
  374. if (swrite(fdin[1], old, n) != n) {
  375. #ifdef DEBUG
  376. (void)fprintf(stderr,
  377. "Write failed (%s)\n",
  378. strerror(errno));
  379. #endif
  380. exit(1);
  381. }
  382. exit(0);
  383. /*NOTREACHED*/
  384. case -1:
  385. #ifdef DEBUG
  386. (void)fprintf(stderr, "Fork failed (%s)\n",
  387. strerror(errno));
  388. #endif
  389. exit(1);
  390. /*NOTREACHED*/
  391. default: /* parent */
  392. break;
  393. }
  394. (void) close(fdin[1]);
  395. fdin[1] = -1;
  396. }
  397. if ((*newch = (unsigned char *) malloc(HOWMANY + 1)) == NULL) {
  398. #ifdef DEBUG
  399. (void)fprintf(stderr, "Malloc failed (%s)\n",
  400. strerror(errno));
  401. #endif
  402. n = 0;
  403. goto err;
  404. }
  405. if ((r = sread(fdout[0], *newch, HOWMANY)) <= 0) {
  406. #ifdef DEBUG
  407. (void)fprintf(stderr, "Read failed (%s)\n",
  408. strerror(errno));
  409. #endif
  410. free(*newch);
  411. n = 0;
  412. newch[0] = '\0';
  413. goto err;
  414. } else {
  415. n = r;
  416. }
  417. /* NUL terminate, as every buffer is handled here. */
  418. (*newch)[n] = '\0';
  419. err:
  420. if (fdin[1] != -1)
  421. (void) close(fdin[1]);
  422. (void) close(fdout[0]);
  423. #ifdef WNOHANG
  424. while (waitpid(-1, NULL, WNOHANG) != -1)
  425. continue;
  426. #else
  427. (void)wait(NULL);
  428. #endif
  429. return n;
  430. }
  431. }