compress.c 20 KB

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