compress.c 17 KB

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