compress.c 12 KB

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