compress.c 11 KB

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