compress.c 11 KB

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