compress.c 11 KB

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