compress.c 11 KB

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