compress.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #ifdef HAVE_SYS_WAIT_H
  46. #include <sys/wait.h>
  47. #endif
  48. #ifdef HAVE_LIBZ
  49. #include <zlib.h>
  50. #endif
  51. #ifndef lint
  52. FILE_RCSID("@(#)$Id: compress.c,v 1.38 2004/09/11 19:15:57 christos Exp $")
  53. #endif
  54. private struct {
  55. const char *magic;
  56. size_t maglen;
  57. const char *const argv[3];
  58. int silent;
  59. } compr[] = {
  60. { "\037\235", 2, { "gzip", "-cdq", NULL }, 1 }, /* compressed */
  61. /* Uncompress can get stuck; so use gzip first if we have it
  62. * Idea from Damien Clark, thanks! */
  63. { "\037\235", 2, { "uncompress", "-c", NULL }, 1 }, /* compressed */
  64. { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */
  65. { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */
  66. { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */
  67. /* the standard pack utilities do not accept standard input */
  68. { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */
  69. { "BZh", 3, { "bzip2", "-cd", NULL }, 1 }, /* bzip2-ed */
  70. };
  71. private int ncompr = sizeof(compr) / sizeof(compr[0]);
  72. private ssize_t swrite(int, const void *, size_t);
  73. private ssize_t sread(int, void *, size_t);
  74. private size_t uncompressbuf(struct magic_set *, size_t, const unsigned char *,
  75. unsigned char **, size_t);
  76. #ifdef HAVE_LIBZ
  77. private size_t uncompressgzipped(struct magic_set *, const unsigned char *,
  78. unsigned char **, size_t);
  79. #endif
  80. protected int
  81. file_zmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
  82. {
  83. unsigned char *newbuf = NULL;
  84. size_t i, nsz;
  85. int rv = 0;
  86. if ((ms->flags & MAGIC_COMPRESS) == 0)
  87. return 0;
  88. for (i = 0; i < ncompr; i++) {
  89. if (nbytes < compr[i].maglen)
  90. continue;
  91. if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 &&
  92. (nsz = uncompressbuf(ms, i, buf, &newbuf, nbytes)) != 0) {
  93. ms->flags &= ~MAGIC_COMPRESS;
  94. rv = -1;
  95. if (file_buffer(ms, newbuf, nsz) == -1)
  96. goto error;
  97. if (file_printf(ms, " (") == -1)
  98. goto error;
  99. if (file_buffer(ms, buf, nbytes) == -1)
  100. goto error;
  101. if (file_printf(ms, ")") == -1)
  102. goto error;
  103. rv = 1;
  104. break;
  105. }
  106. }
  107. error:
  108. if (newbuf)
  109. free(newbuf);
  110. ms->flags |= MAGIC_COMPRESS;
  111. return rv;
  112. }
  113. /*
  114. * `safe' write for sockets and pipes.
  115. */
  116. private ssize_t
  117. swrite(int fd, const void *buf, size_t n)
  118. {
  119. int rv;
  120. size_t rn = n;
  121. do
  122. switch (rv = write(fd, buf, n)) {
  123. case -1:
  124. if (errno == EINTR)
  125. continue;
  126. return -1;
  127. default:
  128. n -= rv;
  129. buf = ((const char *)buf) + rv;
  130. break;
  131. }
  132. while (n > 0);
  133. return rn;
  134. }
  135. /*
  136. * `safe' read for sockets and pipes.
  137. */
  138. private ssize_t
  139. sread(int fd, void *buf, size_t n)
  140. {
  141. int rv;
  142. size_t rn = n;
  143. do
  144. switch (rv = read(fd, buf, n)) {
  145. case -1:
  146. if (errno == EINTR)
  147. continue;
  148. return -1;
  149. case 0:
  150. return rn - n;
  151. default:
  152. n -= rv;
  153. buf = ((char *)buf) + rv;
  154. break;
  155. }
  156. while (n > 0);
  157. return rn;
  158. }
  159. protected int
  160. file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
  161. size_t nbytes)
  162. {
  163. char buf[4096];
  164. int r, tfd;
  165. (void)strcpy(buf, "/tmp/file.XXXXXX");
  166. #ifndef HAVE_MKSTEMP
  167. {
  168. char *ptr = mktemp(buf);
  169. tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
  170. r = errno;
  171. (void)unlink(ptr);
  172. errno = r;
  173. }
  174. #else
  175. tfd = mkstemp(buf);
  176. r = errno;
  177. (void)unlink(buf);
  178. errno = r;
  179. #endif
  180. if (tfd == -1) {
  181. file_error(ms, errno,
  182. "cannot create temporary file for pipe copy");
  183. return -1;
  184. }
  185. if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
  186. r = 1;
  187. else {
  188. while ((r = sread(fd, buf, sizeof(buf))) > 0)
  189. if (swrite(tfd, buf, (size_t)r) != r)
  190. break;
  191. }
  192. switch (r) {
  193. case -1:
  194. file_error(ms, errno, "error copying from pipe to temp file");
  195. return -1;
  196. case 0:
  197. break;
  198. default:
  199. file_error(ms, errno, "error while writing to temp file");
  200. return -1;
  201. }
  202. /*
  203. * We duplicate the file descriptor, because fclose on a
  204. * tmpfile will delete the file, but any open descriptors
  205. * can still access the phantom inode.
  206. */
  207. if ((fd = dup2(tfd, fd)) == -1) {
  208. file_error(ms, errno, "could not dup descriptor for temp file");
  209. return -1;
  210. }
  211. (void)close(tfd);
  212. if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
  213. file_badseek(ms);
  214. return -1;
  215. }
  216. return fd;
  217. }
  218. #ifdef HAVE_LIBZ
  219. #define FHCRC (1 << 1)
  220. #define FEXTRA (1 << 2)
  221. #define FNAME (1 << 3)
  222. #define FCOMMENT (1 << 4)
  223. private size_t
  224. uncompressgzipped(struct magic_set *ms, const unsigned char *old,
  225. unsigned char **newch, size_t n)
  226. {
  227. unsigned char flg = old[3];
  228. size_t data_start = 10;
  229. z_stream z;
  230. int rc;
  231. if (flg & FEXTRA) {
  232. if (data_start+1 >= n)
  233. return 0;
  234. data_start += 2 + old[data_start] + old[data_start + 1] * 256;
  235. }
  236. if (flg & FNAME) {
  237. while(data_start < n && old[data_start])
  238. data_start++;
  239. data_start++;
  240. }
  241. if(flg & FCOMMENT) {
  242. while(data_start < n && old[data_start])
  243. data_start++;
  244. data_start++;
  245. }
  246. if(flg & FHCRC)
  247. data_start += 2;
  248. if (data_start >= n)
  249. return 0;
  250. if ((*newch = (unsigned char *)malloc(HOWMANY + 1)) == NULL) {
  251. return 0;
  252. }
  253. /* XXX: const castaway, via strchr */
  254. z.next_in = (Bytef *)strchr((const char *)old + data_start,
  255. old[data_start]);
  256. z.avail_in = n - data_start;
  257. z.next_out = *newch;
  258. z.avail_out = HOWMANY;
  259. z.zalloc = Z_NULL;
  260. z.zfree = Z_NULL;
  261. z.opaque = Z_NULL;
  262. rc = inflateInit2(&z, -15);
  263. if (rc != Z_OK) {
  264. file_error(ms, 0, "zlib: %s", z.msg);
  265. return 0;
  266. }
  267. rc = inflate(&z, Z_SYNC_FLUSH);
  268. if (rc != Z_OK && rc != Z_STREAM_END) {
  269. file_error(ms, 0, "zlib: %s", z.msg);
  270. return 0;
  271. }
  272. n = (size_t)z.total_out;
  273. inflateEnd(&z);
  274. /* let's keep the nul-terminate tradition */
  275. (*newch)[n++] = '\0';
  276. return n;
  277. }
  278. #endif
  279. private size_t
  280. uncompressbuf(struct magic_set *ms, size_t method, const unsigned char *old,
  281. unsigned char **newch, size_t n)
  282. {
  283. int fdin[2], fdout[2];
  284. int r;
  285. /* The buffer is NUL terminated, and we don't need that. */
  286. n--;
  287. #ifdef HAVE_LIBZ
  288. if (method == 2)
  289. return uncompressgzipped(ms, old, newch, n);
  290. #endif
  291. if (pipe(fdin) == -1 || pipe(fdout) == -1) {
  292. file_error(ms, errno, "cannot create pipe");
  293. return 0;
  294. }
  295. switch (fork()) {
  296. case 0: /* child */
  297. (void) close(0);
  298. (void) dup(fdin[0]);
  299. (void) close(fdin[0]);
  300. (void) close(fdin[1]);
  301. (void) close(1);
  302. (void) dup(fdout[1]);
  303. (void) close(fdout[0]);
  304. (void) close(fdout[1]);
  305. if (compr[method].silent)
  306. (void) close(2);
  307. execvp(compr[method].argv[0],
  308. (char *const *)compr[method].argv);
  309. exit(1);
  310. /*NOTREACHED*/
  311. case -1:
  312. file_error(ms, errno, "could not fork");
  313. return 0;
  314. default: /* parent */
  315. (void) close(fdin[0]);
  316. (void) close(fdout[1]);
  317. /* fork again, to avoid blocking because both pipes filled */
  318. switch (fork()) {
  319. case 0: /* child */
  320. (void)close(fdout[0]);
  321. if (swrite(fdin[1], old, n) != n)
  322. exit(1);
  323. exit(0);
  324. /*NOTREACHED*/
  325. case -1:
  326. exit(1);
  327. /*NOTREACHED*/
  328. default: /* parent */
  329. break;
  330. }
  331. (void) close(fdin[1]);
  332. fdin[1] = -1;
  333. if ((*newch = (unsigned char *) malloc(HOWMANY + 1)) == NULL) {
  334. n = 0;
  335. goto err;
  336. }
  337. if ((r = sread(fdout[0], *newch, HOWMANY)) <= 0) {
  338. free(*newch);
  339. n = 0;
  340. newch[0] = '\0';
  341. goto err;
  342. } else {
  343. n = r;
  344. }
  345. /* NUL terminate, as every buffer is handled here. */
  346. (*newch)[n++] = '\0';
  347. err:
  348. if (fdin[1] != -1)
  349. (void) close(fdin[1]);
  350. (void) close(fdout[0]);
  351. #ifdef WNOHANG
  352. while (waitpid(-1, NULL, WNOHANG) != -1)
  353. continue;
  354. #else
  355. (void)wait(NULL);
  356. #endif
  357. return n;
  358. }
  359. }