compress.c 17 KB

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