compress.c 17 KB

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