compress.c 12 KB

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