compress.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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.100 2016/10/24 18:02:17 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 <ctype.h>
  47. #include <stdarg.h>
  48. #ifdef HAVE_SIGNAL_H
  49. #include <signal.h>
  50. # ifndef HAVE_SIG_T
  51. typedef void (*sig_t)(int);
  52. # endif /* HAVE_SIG_T */
  53. #endif
  54. #if !defined(__MINGW32__) && !defined(WIN32)
  55. #include <sys/ioctl.h>
  56. #endif
  57. #ifdef HAVE_SYS_WAIT_H
  58. #include <sys/wait.h>
  59. #endif
  60. #if defined(HAVE_SYS_TIME_H)
  61. #include <sys/time.h>
  62. #endif
  63. #if defined(HAVE_ZLIB_H)
  64. #define BUILTIN_DECOMPRESS
  65. #include <zlib.h>
  66. #endif
  67. #ifdef DEBUG
  68. int tty = -1;
  69. #define DPRINTF(...) do { \
  70. if (tty == -1) \
  71. tty = open("/dev/tty", O_RDWR); \
  72. if (tty == -1) \
  73. abort(); \
  74. dprintf(tty, __VA_ARGS__); \
  75. } while (/*CONSTCOND*/0)
  76. #else
  77. #define DPRINTF(...)
  78. #endif
  79. #ifdef ZLIBSUPPORT
  80. /*
  81. * The following python code is not really used because ZLIBSUPPORT is only
  82. * defined if we have a built-in zlib, and the built-in zlib handles that.
  83. */
  84. static const char zlibcode[] =
  85. "import sys, zlib; sys.stdout.write(zlib.decompress(sys.stdin.read()))";
  86. static const char *zlib_args[] = { "python", "-c", zlibcode, NULL };
  87. static int
  88. zlibcmp(const unsigned char *buf)
  89. {
  90. unsigned short x = 1;
  91. unsigned char *s = (unsigned char *)&x;
  92. if ((buf[0] & 0xf) != 8 || (buf[0] & 0x80) != 0)
  93. return 0;
  94. if (s[0] != 1) /* endianness test */
  95. x = buf[0] | (buf[1] << 8);
  96. else
  97. x = buf[1] | (buf[0] << 8);
  98. if (x % 31)
  99. return 0;
  100. return 1;
  101. }
  102. #endif
  103. #define gzip_flags "-cd"
  104. #define lrzip_flags "-do"
  105. #define lzip_flags gzip_flags
  106. static const char *gzip_args[] = {
  107. "gzip", gzip_flags, NULL
  108. };
  109. static const char *uncompress_args[] = {
  110. "uncompress", "-c", NULL
  111. };
  112. static const char *bzip2_args[] = {
  113. "bzip2", "-cd", NULL
  114. };
  115. static const char *lzip_args[] = {
  116. "lzip", lzip_flags, NULL
  117. };
  118. static const char *xz_args[] = {
  119. "xz", "-cd", NULL
  120. };
  121. static const char *lrzip_args[] = {
  122. "lrzip", lrzip_flags, NULL
  123. };
  124. static const char *lz4_args[] = {
  125. "lz4", "-cd", NULL
  126. };
  127. static const char *zstd_args[] = {
  128. "zstd", "-cd", NULL
  129. };
  130. private const struct {
  131. const void *magic;
  132. size_t maglen;
  133. const char **argv;
  134. } compr[] = {
  135. { "\037\235", 2, gzip_args }, /* compressed */
  136. /* Uncompress can get stuck; so use gzip first if we have it
  137. * Idea from Damien Clark, thanks! */
  138. { "\037\235", 2, uncompress_args }, /* compressed */
  139. { "\037\213", 2, gzip_args }, /* gzipped */
  140. { "\037\236", 2, gzip_args }, /* frozen */
  141. { "\037\240", 2, gzip_args }, /* SCO LZH */
  142. /* the standard pack utilities do not accept standard input */
  143. { "\037\036", 2, gzip_args }, /* packed */
  144. { "PK\3\4", 4, gzip_args }, /* pkzipped, */
  145. /* ...only first file examined */
  146. { "BZh", 3, bzip2_args }, /* bzip2-ed */
  147. { "LZIP", 4, lzip_args }, /* lzip-ed */
  148. { "\3757zXZ\0", 6, xz_args }, /* XZ Utils */
  149. { "LRZI", 4, lrzip_args }, /* LRZIP */
  150. { "\004\"M\030",4, lz4_args }, /* LZ4 */
  151. { "\x28\xB5\x2F\xFD", 4, zstd_args }, /* zstd */
  152. #ifdef ZLIBSUPPORT
  153. { RCAST(const void *, zlibcmp), 0, zlib_args }, /* zlib */
  154. #endif
  155. };
  156. #define OKDATA 0
  157. #define NODATA 1
  158. #define ERRDATA 2
  159. private ssize_t swrite(int, const void *, size_t);
  160. #if HAVE_FORK
  161. private size_t ncompr = sizeof(compr) / sizeof(compr[0]);
  162. private int uncompressbuf(int, size_t, size_t, const unsigned char *,
  163. unsigned char **, size_t *);
  164. #ifdef BUILTIN_DECOMPRESS
  165. private int uncompresszlib(const unsigned char *, unsigned char **, size_t,
  166. size_t *, int);
  167. private int uncompressgzipped(const unsigned char *, unsigned char **, size_t,
  168. size_t *);
  169. #endif
  170. static int makeerror(unsigned char **, size_t *, const char *, ...)
  171. __attribute__((__format__(__printf__, 3, 4)));
  172. private const char *methodname(size_t);
  173. protected int
  174. file_zmagic(struct magic_set *ms, int fd, const char *name,
  175. const unsigned char *buf, size_t nbytes)
  176. {
  177. unsigned char *newbuf = NULL;
  178. size_t i, nsz;
  179. char *rbuf;
  180. file_pushbuf_t *pb;
  181. int urv, prv, rv = 0;
  182. int mime = ms->flags & MAGIC_MIME;
  183. #ifdef HAVE_SIGNAL_H
  184. sig_t osigpipe;
  185. #endif
  186. if ((ms->flags & MAGIC_COMPRESS) == 0)
  187. return 0;
  188. #ifdef HAVE_SIGNAL_H
  189. osigpipe = signal(SIGPIPE, SIG_IGN);
  190. #endif
  191. for (i = 0; i < ncompr; i++) {
  192. int zm;
  193. if (nbytes < compr[i].maglen)
  194. continue;
  195. #ifdef ZLIBSUPPORT
  196. if (compr[i].maglen == 0)
  197. zm = (RCAST(int (*)(const unsigned char *),
  198. CCAST(void *, compr[i].magic)))(buf);
  199. else
  200. #endif
  201. zm = memcmp(buf, compr[i].magic, compr[i].maglen) == 0;
  202. if (!zm)
  203. continue;
  204. nsz = nbytes;
  205. urv = uncompressbuf(fd, ms->bytes_max, i, buf, &newbuf, &nsz);
  206. DPRINTF("uncompressbuf = %d, %s, %zu\n", urv, (char *)newbuf,
  207. nsz);
  208. switch (urv) {
  209. case OKDATA:
  210. case ERRDATA:
  211. ms->flags &= ~MAGIC_COMPRESS;
  212. if (urv == ERRDATA)
  213. prv = file_printf(ms, "%s ERROR: %s",
  214. methodname(i), newbuf);
  215. else
  216. prv = file_buffer(ms, -1, name, newbuf, nsz);
  217. if (prv == -1)
  218. goto error;
  219. rv = 1;
  220. if ((ms->flags & MAGIC_COMPRESS_TRANSP) != 0)
  221. goto out;
  222. if (mime != MAGIC_MIME && mime != 0)
  223. goto out;
  224. if ((file_printf(ms,
  225. mime ? " compressed-encoding=" : " (")) == -1)
  226. goto error;
  227. if ((pb = file_push_buffer(ms)) == NULL)
  228. goto error;
  229. /*
  230. * XXX: If file_buffer fails here, we overwrite
  231. * the compressed text. FIXME.
  232. */
  233. if (file_buffer(ms, -1, NULL, buf, nbytes) == -1)
  234. goto error;
  235. if ((rbuf = file_pop_buffer(ms, pb)) != NULL) {
  236. if (file_printf(ms, "%s", rbuf) == -1) {
  237. free(rbuf);
  238. goto error;
  239. }
  240. free(rbuf);
  241. }
  242. if (!mime && file_printf(ms, ")") == -1)
  243. goto error;
  244. /*FALLTHROUGH*/
  245. case NODATA:
  246. break;
  247. default:
  248. abort();
  249. /*NOTREACHED*/
  250. error:
  251. rv = -1;
  252. break;
  253. }
  254. }
  255. out:
  256. DPRINTF("rv = %d\n", rv);
  257. #ifdef HAVE_SIGNAL_H
  258. (void)signal(SIGPIPE, osigpipe);
  259. #endif
  260. free(newbuf);
  261. ms->flags |= MAGIC_COMPRESS;
  262. DPRINTF("Zmagic returns %d\n", rv);
  263. return rv;
  264. }
  265. #endif
  266. /*
  267. * `safe' write for sockets and pipes.
  268. */
  269. private ssize_t
  270. swrite(int fd, const void *buf, size_t n)
  271. {
  272. ssize_t rv;
  273. size_t rn = n;
  274. do
  275. switch (rv = write(fd, buf, n)) {
  276. case -1:
  277. if (errno == EINTR)
  278. continue;
  279. return -1;
  280. default:
  281. n -= rv;
  282. buf = CAST(const char *, buf) + rv;
  283. break;
  284. }
  285. while (n > 0);
  286. return rn;
  287. }
  288. /*
  289. * `safe' read for sockets and pipes.
  290. */
  291. protected ssize_t
  292. sread(int fd, void *buf, size_t n, int canbepipe __attribute__((__unused__)))
  293. {
  294. ssize_t rv;
  295. #ifdef FIONREAD
  296. int t = 0;
  297. #endif
  298. size_t rn = n;
  299. if (fd == STDIN_FILENO)
  300. goto nocheck;
  301. #ifdef FIONREAD
  302. if (canbepipe && (ioctl(fd, FIONREAD, &t) == -1 || t == 0)) {
  303. #ifdef FD_ZERO
  304. ssize_t cnt;
  305. for (cnt = 0;; cnt++) {
  306. fd_set check;
  307. struct timeval tout = {0, 100 * 1000};
  308. int selrv;
  309. FD_ZERO(&check);
  310. FD_SET(fd, &check);
  311. /*
  312. * Avoid soft deadlock: do not read if there
  313. * is nothing to read from sockets and pipes.
  314. */
  315. selrv = select(fd + 1, &check, NULL, NULL, &tout);
  316. if (selrv == -1) {
  317. if (errno == EINTR || errno == EAGAIN)
  318. continue;
  319. } else if (selrv == 0 && cnt >= 5) {
  320. return 0;
  321. } else
  322. break;
  323. }
  324. #endif
  325. (void)ioctl(fd, FIONREAD, &t);
  326. }
  327. if (t > 0 && (size_t)t < n) {
  328. n = t;
  329. rn = n;
  330. }
  331. #endif
  332. nocheck:
  333. do
  334. switch ((rv = read(fd, buf, n))) {
  335. case -1:
  336. if (errno == EINTR)
  337. continue;
  338. return -1;
  339. case 0:
  340. return rn - n;
  341. default:
  342. n -= rv;
  343. buf = CAST(char *, CCAST(void *, buf)) + rv;
  344. break;
  345. }
  346. while (n > 0);
  347. return rn;
  348. }
  349. protected int
  350. file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
  351. size_t nbytes)
  352. {
  353. char buf[4096];
  354. ssize_t r;
  355. int tfd;
  356. (void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf);
  357. #ifndef HAVE_MKSTEMP
  358. {
  359. char *ptr = mktemp(buf);
  360. tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
  361. r = errno;
  362. (void)unlink(ptr);
  363. errno = r;
  364. }
  365. #else
  366. {
  367. int te;
  368. tfd = mkstemp(buf);
  369. te = errno;
  370. (void)unlink(buf);
  371. errno = te;
  372. }
  373. #endif
  374. if (tfd == -1) {
  375. file_error(ms, errno,
  376. "cannot create temporary file for pipe copy");
  377. return -1;
  378. }
  379. if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
  380. r = 1;
  381. else {
  382. while ((r = sread(fd, buf, sizeof(buf), 1)) > 0)
  383. if (swrite(tfd, buf, (size_t)r) != r)
  384. break;
  385. }
  386. switch (r) {
  387. case -1:
  388. file_error(ms, errno, "error copying from pipe to temp file");
  389. return -1;
  390. case 0:
  391. break;
  392. default:
  393. file_error(ms, errno, "error while writing to temp file");
  394. return -1;
  395. }
  396. /*
  397. * We duplicate the file descriptor, because fclose on a
  398. * tmpfile will delete the file, but any open descriptors
  399. * can still access the phantom inode.
  400. */
  401. if ((fd = dup2(tfd, fd)) == -1) {
  402. file_error(ms, errno, "could not dup descriptor for temp file");
  403. return -1;
  404. }
  405. (void)close(tfd);
  406. if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
  407. file_badseek(ms);
  408. return -1;
  409. }
  410. return fd;
  411. }
  412. #if HAVE_FORK
  413. #ifdef BUILTIN_DECOMPRESS
  414. #define FHCRC (1 << 1)
  415. #define FEXTRA (1 << 2)
  416. #define FNAME (1 << 3)
  417. #define FCOMMENT (1 << 4)
  418. private int
  419. uncompressgzipped(const unsigned char *old, unsigned char **newch,
  420. size_t bytes_max, size_t *n)
  421. {
  422. unsigned char flg = old[3];
  423. size_t data_start = 10;
  424. if (flg & FEXTRA) {
  425. if (data_start + 1 >= *n)
  426. goto err;
  427. data_start += 2 + old[data_start] + old[data_start + 1] * 256;
  428. }
  429. if (flg & FNAME) {
  430. while(data_start < *n && old[data_start])
  431. data_start++;
  432. data_start++;
  433. }
  434. if (flg & FCOMMENT) {
  435. while(data_start < *n && old[data_start])
  436. data_start++;
  437. data_start++;
  438. }
  439. if (flg & FHCRC)
  440. data_start += 2;
  441. if (data_start >= *n)
  442. goto err;
  443. *n -= data_start;
  444. old += data_start;
  445. return uncompresszlib(old, newch, bytes_max, n, 0);
  446. err:
  447. return makeerror(newch, n, "File too short");
  448. }
  449. private int
  450. uncompresszlib(const unsigned char *old, unsigned char **newch,
  451. size_t bytes_max, size_t *n, int zlib)
  452. {
  453. int rc;
  454. z_stream z;
  455. if ((*newch = CAST(unsigned char *, malloc(bytes_max + 1))) == NULL)
  456. return makeerror(newch, n, "No buffer, %s", strerror(errno));
  457. z.next_in = CCAST(Bytef *, old);
  458. z.avail_in = CAST(uint32_t, *n);
  459. z.next_out = *newch;
  460. z.avail_out = bytes_max;
  461. z.zalloc = Z_NULL;
  462. z.zfree = Z_NULL;
  463. z.opaque = Z_NULL;
  464. /* LINTED bug in header macro */
  465. rc = zlib ? inflateInit(&z) : inflateInit2(&z, -15);
  466. if (rc != Z_OK)
  467. goto err;
  468. rc = inflate(&z, Z_SYNC_FLUSH);
  469. if (rc != Z_OK && rc != Z_STREAM_END)
  470. goto err;
  471. *n = (size_t)z.total_out;
  472. rc = inflateEnd(&z);
  473. if (rc != Z_OK)
  474. goto err;
  475. /* let's keep the nul-terminate tradition */
  476. (*newch)[*n] = '\0';
  477. return OKDATA;
  478. err:
  479. strlcpy((char *)*newch, z.msg ? z.msg : zError(rc), bytes_max);
  480. *n = strlen((char *)*newch);
  481. return ERRDATA;
  482. }
  483. #endif
  484. static int
  485. makeerror(unsigned char **buf, size_t *len, const char *fmt, ...)
  486. {
  487. char *msg;
  488. va_list ap;
  489. int rv;
  490. va_start(ap, fmt);
  491. rv = vasprintf(&msg, fmt, ap);
  492. va_end(ap);
  493. if (rv < 0) {
  494. *buf = NULL;
  495. *len = 0;
  496. return NODATA;
  497. }
  498. *buf = (unsigned char *)msg;
  499. *len = strlen(msg);
  500. return ERRDATA;
  501. }
  502. static void
  503. closefd(int *fd, size_t i)
  504. {
  505. if (fd[i] == -1)
  506. return;
  507. (void) close(fd[i]);
  508. fd[i] = -1;
  509. }
  510. static void
  511. closep(int *fd)
  512. {
  513. size_t i;
  514. for (i = 0; i < 2; i++)
  515. closefd(fd, i);
  516. }
  517. static void
  518. copydesc(int i, int *fd)
  519. {
  520. int j = fd[i == STDIN_FILENO ? 0 : 1];
  521. if (j == i)
  522. return;
  523. if (dup2(j, i) == -1) {
  524. DPRINTF("dup(%d, %d) failed (%s)\n", j, i, strerror(errno));
  525. exit(1);
  526. }
  527. closep(fd);
  528. }
  529. static void
  530. writechild(int fdp[3][2], const void *old, size_t n)
  531. {
  532. int status;
  533. closefd(fdp[STDIN_FILENO], 0);
  534. /*
  535. * fork again, to avoid blocking because both
  536. * pipes filled
  537. */
  538. switch (fork()) {
  539. case 0: /* child */
  540. closefd(fdp[STDOUT_FILENO], 0);
  541. if (swrite(fdp[STDIN_FILENO][1], old, n) != (ssize_t)n) {
  542. DPRINTF("Write failed (%s)\n", strerror(errno));
  543. exit(1);
  544. }
  545. exit(0);
  546. /*NOTREACHED*/
  547. case -1:
  548. DPRINTF("Fork failed (%s)\n", strerror(errno));
  549. exit(1);
  550. /*NOTREACHED*/
  551. default: /* parent */
  552. if (wait(&status) == -1) {
  553. DPRINTF("Wait failed (%s)\n", strerror(errno));
  554. exit(1);
  555. }
  556. DPRINTF("Grandchild wait return %#x\n", status);
  557. }
  558. closefd(fdp[STDIN_FILENO], 1);
  559. }
  560. static ssize_t
  561. filter_error(unsigned char *ubuf, ssize_t n)
  562. {
  563. char *p;
  564. char *buf;
  565. ubuf[n] = '\0';
  566. buf = (char *)ubuf;
  567. while (isspace((unsigned char)*buf))
  568. buf++;
  569. DPRINTF("Filter error[[[%s]]]\n", buf);
  570. if ((p = strchr((char *)buf, '\n')) != NULL)
  571. *p = '\0';
  572. if ((p = strchr((char *)buf, ';')) != NULL)
  573. *p = '\0';
  574. if ((p = strrchr((char *)buf, ':')) != NULL) {
  575. ++p;
  576. while (isspace((unsigned char)*p))
  577. p++;
  578. n = strlen(p);
  579. memmove(ubuf, p, n + 1);
  580. }
  581. DPRINTF("Filter error after[[[%s]]]\n", (char *)ubuf);
  582. if (islower(*ubuf))
  583. *ubuf = toupper(*ubuf);
  584. return n;
  585. }
  586. private const char *
  587. methodname(size_t method)
  588. {
  589. #ifdef BUILTIN_DECOMPRESS
  590. /* FIXME: This doesn't cope with bzip2 */
  591. if (method == 2 || compr[method].maglen == 0)
  592. return "zlib";
  593. #endif
  594. return compr[method].argv[0];
  595. }
  596. private int
  597. uncompressbuf(int fd, size_t bytes_max, size_t method, const unsigned char *old,
  598. unsigned char **newch, size_t* n)
  599. {
  600. int fdp[3][2];
  601. int status, rv;
  602. size_t i;
  603. ssize_t r;
  604. #ifdef BUILTIN_DECOMPRESS
  605. /* FIXME: This doesn't cope with bzip2 */
  606. if (method == 2)
  607. return uncompressgzipped(old, newch, bytes_max, n);
  608. if (compr[method].maglen == 0)
  609. return uncompresszlib(old, newch, bytes_max, n, 1);
  610. #endif
  611. (void)fflush(stdout);
  612. (void)fflush(stderr);
  613. for (i = 0; i < __arraycount(fdp); i++)
  614. fdp[i][0] = fdp[i][1] = -1;
  615. if ((fd == -1 && pipe(fdp[STDIN_FILENO]) == -1) ||
  616. pipe(fdp[STDOUT_FILENO]) == -1 || pipe(fdp[STDERR_FILENO]) == -1) {
  617. closep(fdp[STDIN_FILENO]);
  618. closep(fdp[STDOUT_FILENO]);
  619. return makeerror(newch, n, "Cannot create pipe, %s",
  620. strerror(errno));
  621. }
  622. switch (fork()) {
  623. case 0: /* child */
  624. if (fd != -1) {
  625. fdp[STDIN_FILENO][0] = fd;
  626. (void) lseek(fd, (off_t)0, SEEK_SET);
  627. }
  628. for (i = 0; i < __arraycount(fdp); i++)
  629. copydesc(i, fdp[i]);
  630. (void)execvp(compr[method].argv[0],
  631. (char *const *)(intptr_t)compr[method].argv);
  632. dprintf(STDERR_FILENO, "exec `%s' failed, %s",
  633. compr[method].argv[0], strerror(errno));
  634. exit(1);
  635. /*NOTREACHED*/
  636. case -1:
  637. return makeerror(newch, n, "Cannot fork, %s",
  638. strerror(errno));
  639. default: /* parent */
  640. for (i = 1; i < __arraycount(fdp); i++)
  641. closefd(fdp[i], 1);
  642. /* Write the buffer data to the child, if we don't have fd */
  643. if (fd == -1)
  644. writechild(fdp, old, *n);
  645. *newch = CAST(unsigned char *, malloc(bytes_max + 1));
  646. if (*newch == NULL) {
  647. rv = makeerror(newch, n, "No buffer, %s",
  648. strerror(errno));
  649. goto err;
  650. }
  651. rv = OKDATA;
  652. if ((r = sread(fdp[STDOUT_FILENO][0], *newch, bytes_max, 0)) > 0)
  653. break;
  654. DPRINTF("Read stdout failed %d (%s)\n", fdp[STDOUT_FILENO][0],
  655. r != -1 ? strerror(errno) : "no data");
  656. rv = ERRDATA;
  657. if (r == 0 &&
  658. (r = sread(fdp[STDERR_FILENO][0], *newch, bytes_max, 0)) > 0)
  659. {
  660. r = filter_error(*newch, r);
  661. break;
  662. }
  663. free(*newch);
  664. if (r == 0)
  665. rv = makeerror(newch, n, "Read failed, %s",
  666. strerror(errno));
  667. else
  668. rv = makeerror(newch, n, "No data");
  669. goto err;
  670. }
  671. *n = r;
  672. /* NUL terminate, as every buffer is handled here. */
  673. (*newch)[*n] = '\0';
  674. err:
  675. closefd(fdp[STDIN_FILENO], 1);
  676. closefd(fdp[STDOUT_FILENO], 0);
  677. closefd(fdp[STDERR_FILENO], 0);
  678. if (wait(&status) == -1) {
  679. free(*newch);
  680. rv = makeerror(newch, n, "Wait failed, %s", strerror(errno));
  681. DPRINTF("Child wait return %#x\n", status);
  682. } else if (!WIFEXITED(status)) {
  683. DPRINTF("Child not exited (0x%x)\n", status);
  684. } else if (WEXITSTATUS(status) != 0) {
  685. DPRINTF("Child exited (0x%d)\n", WEXITSTATUS(status));
  686. }
  687. closefd(fdp[STDIN_FILENO], 0);
  688. DPRINTF("Returning %p n=%zu rv=%d\n", *newch, *n, rv);
  689. return rv;
  690. }
  691. #endif