compress.c 18 KB

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